| 1 |
#include "XDataSelector.h" |
| 2 |
|
| 3 |
#include "i18n.h" |
| 4 |
#include "ui/imainframe.h" |
| 5 |
|
| 6 |
#include "ReadableEditorDialog.h" |
| 7 |
|
| 8 |
#include "wxutil/Bitmap.h" |
| 9 |
#include <wx/sizer.h> |
| 10 |
|
| 11 |
namespace ui |
| 12 |
{ |
| 13 |
|
| 14 |
namespace |
| 15 |
{ |
| 16 |
const char* const WINDOW_TITLE = N_("Choose an XData Definition..."); |
| 17 |
const int WINDOW_WIDTH = 500; |
| 18 |
const int WINDOW_HEIGHT = 600; |
| 19 |
|
| 20 |
const char* const XDATA_ICON = "sr_icon_readable.png"; |
| 21 |
const char* const FOLDER_ICON = "folder16.png"; |
| 22 |
} |
| 23 |
|
| 24 |
XDataSelector::XDataSelector(const XData::StringVectorMap& files, ReadableEditorDialog* editorDialog) : |
| 25 |
DialogBase(_(WINDOW_TITLE), editorDialog), |
| 26 |
_store(new wxutil::TreeModel(_columns)), |
| 27 |
_files(files), |
| 28 |
_editorDialog(editorDialog), |
| 29 |
_xdataIcon(wxutil::GetLocalBitmap(XDATA_ICON)), |
| 30 |
_folderIcon(wxutil::GetLocalBitmap(FOLDER_ICON)) |
| 31 |
{ |
| 32 |
fillTree(); |
| 33 |
|
| 34 |
SetSize(WINDOW_WIDTH, WINDOW_HEIGHT); |
| 35 |
|
| 36 |
// Add a vbox for the dialog elements |
| 37 |
SetSizer(new wxBoxSizer(wxVERTICAL)); |
| 38 |
|
| 39 |
// Add a vbox for the dialog elements |
| 40 |
wxBoxSizer* vbox = new wxBoxSizer(wxVERTICAL); |
| 41 |
GetSizer()->Add(vbox, 1, wxEXPAND | wxALL, 12); |
| 42 |
|
| 43 |
// Create the treeview |
| 44 |
_view = wxutil::TreeView::CreateWithModel(this, _store.get(), wxDV_NO_HEADER); |
| 45 |
|
| 46 |
_view->AppendIconTextColumn(_("Xdata Path"), _columns.name.getColumnIndex(), |
| 47 |
wxDATAVIEW_CELL_INERT, wxCOL_WIDTH_AUTOSIZE, wxALIGN_NOT, wxDATAVIEW_COL_SORTABLE); |
| 48 |
|
| 49 |
_view->Connect(wxEVT_DATAVIEW_SELECTION_CHANGED, |
| 50 |
wxDataViewEventHandler(XDataSelector::onSelectionChanged), NULL, this); |
| 51 |
|
| 52 |
// Use the TreeModel's full string search function |
| 53 |
_view->AddSearchColumn(_columns.name); |
| 54 |
|
| 55 |
vbox->Add(_view, 1, wxEXPAND | wxBOTTOM, 6); |
| 56 |
vbox->Add(CreateStdDialogButtonSizer(wxOK | wxCANCEL), 0, wxALIGN_RIGHT); |
| 57 |
|
| 58 |
FindWindowById(wxID_OK, this)->Enable(false); |
| 59 |
|
| 60 |
CenterOnParent(); |
| 61 |
} |
| 62 |
|
| 63 |
std::string XDataSelector::run(const XData::StringVectorMap& files, ReadableEditorDialog* editorDialog) |
| 64 |
{ |
| 65 |
XDataSelector* dialog = new XDataSelector(files, editorDialog); |
| 66 |
|
| 67 |
std::string rv = ""; |
| 68 |
|
| 69 |
if (dialog->ShowModal() == wxID_OK) |
| 70 |
{ |
| 71 |
rv = dialog->_selection; |
| 72 |
} |
| 73 |
|
| 74 |
dialog->Destroy(); |
| 75 |
|
| 76 |
return rv; |
| 77 |
} |
| 78 |
|
| 79 |
void XDataSelector::visit(wxutil::TreeModel& /* store */, wxutil::TreeModel::Row& row, |
| 80 |
const std::string& path, bool isExplicit) |
| 81 |
{ |
| 82 |
row[_columns.name] = wxVariant(wxDataViewIconText(path.substr(path.rfind("/") + 1), |
| 83 |
isExplicit ? _xdataIcon : _folderIcon)); |
| 84 |
row[_columns.fullName] = path; |
| 85 |
row[_columns.isFolder] = !isExplicit; |
| 86 |
|
| 87 |
row.SendItemAdded(); |
| 88 |
} |
| 89 |
|
| 90 |
void XDataSelector::fillTree() |
| 91 |
{ |
| 92 |
// Start adding to tree. |
| 93 |
wxutil::VFSTreePopulator populator(_store); |
| 94 |
|
| 95 |
for (XData::StringVectorMap::const_iterator it = _files.begin(); it != _files.end(); ++it) |
| 96 |
{ |
| 97 |
populator.addPath(it->first); |
| 98 |
} |
| 99 |
|
| 100 |
populator.forEachNode(*this); |
| 101 |
|
| 102 |
_store->SortModelFoldersFirst(_columns.name, _columns.isFolder); |
| 103 |
} |
| 104 |
|
| 105 |
void XDataSelector::onSelectionChanged(wxDataViewEvent& ev) |
| 106 |
{ |
| 107 |
wxDataViewItem item = _view->GetSelection(); |
| 108 |
|
| 109 |
if (item.IsOk()) |
| 110 |
{ |
| 111 |
wxutil::TreeModel::Row row(item, *_store); |
| 112 |
|
| 113 |
if (!row[_columns.isFolder].getBool()) |
| 114 |
{ |
| 115 |
_selection = row[_columns.fullName]; |
| 116 |
_editorDialog->updateGuiView(this, "", _selection); |
| 117 |
|
| 118 |
FindWindowById(wxID_OK, this)->Enable(true); |
| 119 |
return; |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
FindWindowById(wxID_OK, this)->Enable(false); |
| 124 |
} |
| 125 |
|
| 126 |
} // namespace ui |