/
ImageFileSelector.cpp
99 lines (72 loc) · 2.48 KB
/
ImageFileSelector.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include "ImageFileSelector.h"
#include "ImageFilePopulator.h"
namespace ui
{
namespace
{
const char* const WINDOW_TITLE = N_("Select Image File");
}
ImageFileSelector::ImageFileSelector(wxWindow* parent, wxTextCtrl* targetControl) :
DialogBase(_(WINDOW_TITLE), parent),
_okButton(nullptr),
_targetControl(targetControl)
{
SetSizer(new wxBoxSizer(wxVERTICAL));
_treeView = new wxutil::ResourceTreeView(this, _columns, wxDV_NO_HEADER);
_treeView->AppendIconTextColumn(_("File"), _columns.iconAndName.getColumnIndex(),
wxDATAVIEW_CELL_INERT, wxCOL_WIDTH_AUTOSIZE, wxALIGN_NOT, wxDATAVIEW_COL_SORTABLE);
_treeView->AddSearchColumn(_columns.iconAndName);
_treeView->SetExpandTopLevelItemsAfterPopulation(true);
_treeView->Bind(wxEVT_DATAVIEW_SELECTION_CHANGED, &ImageFileSelector::onTreeSelectionChanged, this);
auto dialogButtons = CreateStdDialogButtonSizer(wxOK | wxCANCEL);
_okButton = dialogButtons->GetAffirmativeButton();
_okButton->Disable();
GetSizer()->Add(_treeView, 1, wxALL|wxEXPAND, 12);
GetSizer()->Add(dialogButtons, 0, wxALIGN_RIGHT | wxBOTTOM | wxRIGHT, 12);
FitToScreen(0.5f, 0.7f);
}
int ImageFileSelector::ShowModal(const std::string& preselectItem)
{
_treeView->Populate(std::make_shared<ImageFilePopulator>(_columns));
if (!preselectItem.empty())
{
_treeView->SetSelectedFullname(preselectItem);
}
_previousValue = _targetControl->GetValue().ToStdString();
auto result = DialogBase::ShowModal();
if (result != wxID_OK)
{
// Restore the previous value on cancel
_targetControl->SetValue(_previousValue);
}
return result;
}
int ImageFileSelector::ShowModal()
{
return ShowModal(std::string());
}
std::string ImageFileSelector::GetSelectedImageFilePath()
{
auto item = _treeView->GetSelection();
if (!item.IsOk()) return std::string();
wxutil::TreeModel::Row row(item, *_treeView->GetModel());
if (row[_columns.isFolder].getBool()) return std::string();
return row[_columns.fullName];
}
void ImageFileSelector::onTreeSelectionChanged(wxDataViewEvent& ev)
{
auto item = _treeView->GetSelection();
if (!item.IsOk())
{
_okButton->Disable();
return;
}
wxutil::TreeModel::Row row(item, *_treeView->GetModel());
bool isFolder = row[_columns.isFolder].getBool();
_okButton->Enable(!isFolder);
if (!isFolder)
{
_targetControl->SetValue(row[_columns.fullName].getString());
}
}
}