Debian Codegraph @

plugins/dm.gui/XdFileChooserDialog.cpp

raw
1 #include "XdFileChooserDialog.h"
2
3 #include "wxutil/dialog/MessageBox.h"
4
5 #include "i18n.h"
6 #include "ui/imainframe.h"
7
8 #include "ReadableEditorDialog.h"
9
10 #include <wx/stattext.h>
11 #include <wx/sizer.h>
12
13 #include "ui/idialogmanager.h"
14
15 namespace ui
16 {
17
18 namespace
19 {
20 const char* const WINDOW_TITLE = N_("Choose a file...");
21 }
22
23 int XdFileChooserDialog::Import(const std::string& defName,
24 XData::XDataPtr& newXData,
25 std::string& filename,
26 XData::XDataLoaderPtr& loader,
27 ReadableEditorDialog* editorDialog)
28 {
29 // Import the file:
30 XData::XDataMap xdMap;
31
32 if (loader->importDef(defName,xdMap))
33 {
34 if (xdMap.size() > 1)
35 {
36 // The requested definition has been defined in multiple files. Use the XdFileChooserDialog to pick a file.
37 // Optimally, the preview renderer would already show the selected definition.
38 XdFileChooserDialog* fcDialog = new XdFileChooserDialog(defName, xdMap, editorDialog);
39
40 int result = fcDialog->ShowModal();
41
42 if (result == wxID_OK)
43 {
44 XData::XDataMap::iterator ChosenIt = xdMap.find(fcDialog->_chosenFile);
45 filename = ChosenIt->first;
46 newXData = ChosenIt->second;
47 }
48
49 fcDialog->Destroy();
50
51 return result;
52 }
53 else
54 {
55 filename = xdMap.begin()->first;
56 newXData = xdMap.begin()->second;
57
58 if (loader->getImportSummary().size() > 1)
59 {
60 std::string msg = fmt::format(_("{0} successfully imported."), defName);
61 msg += "\n\nHowever, there were some problems.\n\n";
62 msg += _("Do you want to open the import summary?");
63
64 wxutil::Messagebox dialog(_("Problems during import"),
65 msg,
66 ui::IDialog::MESSAGE_ASK, editorDialog
67 );
68
69 if (dialog.run() == ui::IDialog::RESULT_YES)
70 {
71 editorDialog->showXdImportSummary();
72 }
73 }
74 }
75
76 return wxID_OK;
77 }
78
79 throw ImportFailedException(_("Import failed"));
80 }
81
82 XdFileChooserDialog::XdFileChooserDialog(const std::string& defName,
83 const XData::XDataMap& xdMap,
84 ReadableEditorDialog* editorDialog) :
85 DialogBase(_(WINDOW_TITLE), editorDialog),
86 _listStore(new wxutil::TreeModel(_columns, true)),
87 _treeview(NULL),
88 _editorDialog(editorDialog),
89 _defName(defName)
90 {
91 SetSizer(new wxBoxSizer(wxVERTICAL));
92
93 // Add a vbox for the dialog elements
94 wxBoxSizer* vbox = new wxBoxSizer(wxVERTICAL);
95 GetSizer()->Add(vbox, 1, wxEXPAND | wxALL, 12);
96
97 // Create topLabel
98 wxStaticText* topLabel = new wxStaticText(this, wxID_ANY,
99 _("The requested definition has been found in multiple Files. Choose the file:"));
100
101 // Create the list of files:
102 _treeview = wxutil::TreeView::CreateWithModel(this, _listStore.get(), wxDV_NO_HEADER);
103
104 _treeview->AppendTextColumn(_("File"), _columns.name.getColumnIndex(),
105 wxDATAVIEW_CELL_INERT, wxCOL_WIDTH_AUTOSIZE, wxALIGN_NOT, wxDATAVIEW_COL_SORTABLE);
106
107 // Append all xdMap-entries to the list.
108 for (XData::XDataMap::const_iterator it = xdMap.begin(); it != xdMap.end(); ++it)
109 {
110 wxutil::TreeModel::Row row = _listStore->AddItem();
111
112 row[_columns.name] = it->first;
113
114 row.SendItemAdded();
115 }
116
117 // Connect the selection change signal
118 _treeview->Connect(wxEVT_DATAVIEW_SELECTION_CHANGED,
119 wxDataViewEventHandler(XdFileChooserDialog::onSelectionChanged), NULL, this);
120
121 //Add everything to the vbox and to the window.
122 vbox->Add(topLabel, 0, wxBOTTOM, 6);
123 vbox->Add(_treeview, 1, wxEXPAND | wxBOTTOM, 6);
124 vbox->Add(CreateStdDialogButtonSizer(wxOK | wxCANCEL), 0, wxALIGN_RIGHT);
125 }
126
127 void XdFileChooserDialog::onSelectionChanged(wxDataViewEvent& ev)
128 {
129 wxDataViewItem item = _treeview->GetSelection();
130
131 if (item.IsOk())
132 {
133 wxutil::TreeModel::Row row(item, *_listStore);
134 _chosenFile = row[_columns.name];
135
136 _editorDialog->updateGuiView(this, "", _defName, _chosenFile.substr(_chosenFile.find("/")+1));
137 }
138 }
139
140 } // namespace ui