/
SkinChooser.cpp
274 lines (210 loc) · 6.93 KB
/
SkinChooser.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#include "SkinChooser.h"
#include "i18n.h"
#include "iuimanager.h"
#include "imainframe.h"
#include "itextstream.h"
#include "modelskin.h"
#include <wx/artprov.h>
#include <wx/sizer.h>
#include <wx/splitter.h>
#include "wxutil/TreeView.h"
#include "wxutil/VFSTreePopulator.h"
namespace ui
{
/* CONSTANTS */
namespace
{
const char* FOLDER_ICON = "folder16.png";
const char* SKIN_ICON = "skin16.png";
const char* const WINDOW_TITLE = N_("Choose Skin");
}
// Constructor
SkinChooser::SkinChooser() :
DialogBase(_(WINDOW_TITLE)),
_treeStore(new wxutil::TreeModel(_columns)),
_treeView(nullptr),
_lastSkin("")
{
FitToScreen(0.6f, 0.6f);
populateWindow();
}
SkinChooser& SkinChooser::Instance()
{
SkinChooserPtr& instancePtr = InstancePtr();
if (!instancePtr)
{
// Not yet instantiated, do it now
instancePtr.reset(new SkinChooser);
// Pre-destruction cleanup
GlobalMainFrame().signal_MainFrameShuttingDown().connect(
sigc::mem_fun(*instancePtr, &SkinChooser::onMainFrameShuttingDown)
);
}
return *instancePtr;
}
SkinChooserPtr& SkinChooser::InstancePtr()
{
static SkinChooserPtr _instancePtr;
return _instancePtr;
}
void SkinChooser::populateWindow()
{
SetSizer(new wxBoxSizer(wxVERTICAL));
wxBoxSizer* vbox = new wxBoxSizer(wxVERTICAL);
GetSizer()->Add(vbox, 1, wxEXPAND | wxALL, 12);
wxSplitterWindow* splitter = new wxSplitterWindow(this, wxID_ANY, wxDefaultPosition,
wxDefaultSize, wxSP_3D | wxSP_LIVE_UPDATE);
splitter->SetMinimumPaneSize(10); // disallow unsplitting
// Create the tree view
_treeView = wxutil::TreeView::CreateWithModel(splitter, _treeStore, wxDV_NO_HEADER);
_treeView->SetMinClientSize(wxSize(GetSize().GetWidth() / 5, -1));
// Single column to display the skin name
_treeView->AppendIconTextColumn(_("Skin"), _columns.displayName.getColumnIndex(),
wxDATAVIEW_CELL_INERT, wxCOL_WIDTH_AUTOSIZE, wxALIGN_NOT);
_treeView->AddSearchColumn(_columns.displayName);
// Connect up selection changed callback
_treeView->Bind(wxEVT_DATAVIEW_SELECTION_CHANGED, &SkinChooser::_onSelChanged, this);
// Preview
_preview.reset(new wxutil::ModelPreview(splitter));
_preview->getWidget()->SetMinClientSize(wxSize(GetSize().GetWidth() / 3, -1));
// Pack treeview and preview
splitter->SplitVertically(_treeView, _preview->getWidget());
FitToScreen(0.6f, 0.8f);
// Set the default size of the window
splitter->SetSashPosition(static_cast<int>(GetSize().GetWidth() * 0.3f));
// Overall vbox for treeview/preview and buttons
vbox->Add(splitter, 1, wxEXPAND);
vbox->Add(CreateStdDialogButtonSizer(wxOK | wxCANCEL), 0, wxALIGN_RIGHT | wxTOP, 12);
}
int SkinChooser::ShowModal()
{
populateSkins();
// Display the model in the window title
SetTitle(std::string(_(WINDOW_TITLE)) + ": " + _model);
setSelectedSkin(_prevSkin);
int returnCode = DialogBase::ShowModal();
if (returnCode == wxID_OK)
{
// Get the selected skin
_lastSkin = getSelectedSkin();
}
else
{
// Revert to previous skin on everything other than OK
_lastSkin = _prevSkin;
}
_preview->setModel(""); // release model
return returnCode;
}
// Populate the list of skins
void SkinChooser::populateSkins()
{
// Create the treestore
wxutil::TreeModel::Ptr treeStore(new wxutil::TreeModel(_columns));
// Clear the treestore
treeStore->Clear();
wxIcon folderIcon;
folderIcon.CopyFromBitmap(
wxArtProvider::GetBitmap(GlobalUIManager().ArtIdPrefix() + FOLDER_ICON));
wxIcon skinIcon;
skinIcon.CopyFromBitmap(
wxArtProvider::GetBitmap(GlobalUIManager().ArtIdPrefix() + SKIN_ICON));
// Add the "Matching skins" toplevel node
wxutil::TreeModel::Row matchingSkins = treeStore->AddItem();
matchingSkins[_columns.displayName] = wxVariant(wxDataViewIconText(_("Matching skins"), folderIcon));
matchingSkins[_columns.fullName] = "";
matchingSkins[_columns.isFolder] = true;
// Get the skins for the associated model, and add them as matching skins
const StringList& matchList = GlobalModelSkinCache().getSkinsForModel(_model);
for (const auto& skin : matchList)
{
wxutil::TreeModel::Row skinRow = treeStore->AddItem(matchingSkins.getItem());
skinRow[_columns.displayName] = wxVariant(wxDataViewIconText(skin, skinIcon));
skinRow[_columns.fullName] = skin;
skinRow[_columns.isFolder] = false;
}
// Add "All skins" toplevel node
wxutil::TreeModel::Row allSkins = treeStore->AddItem();
allSkins[_columns.displayName] = wxVariant(wxDataViewIconText(_("All skins"), folderIcon));
allSkins[_columns.fullName] = "";
allSkins[_columns.isFolder] = true;
// Get the list of skins for the model
const StringList& skins = GlobalModelSkinCache().getAllSkins();
// Create a TreePopulator for the tree store and pass in each of the
// skin names.
wxutil::VFSTreePopulator pop(treeStore, allSkins.getItem());
for (const auto& skin : skins)
{
pop.addPath(skin, [&] (wxutil::TreeModel::Row& row,
const std::string& leafName, bool isFolder)
{
// Get the display path, everything after rightmost slash
std::string displayPath = leafName.substr(leafName.rfind("/") + 1);
row[_columns.displayName] = wxVariant(wxDataViewIconText(displayPath,
!isFolder ? skinIcon : folderIcon));
row[_columns.fullName] = leafName;
row[_columns.isFolder] = isFolder;
});
}
treeStore->SortModelFoldersFirst(_columns.displayName, _columns.isFolder);
_treeView->AssociateModel(treeStore.get());
_treeStore = treeStore;
// Make sure the "matching skins" item is expanded
_treeView->Expand(matchingSkins.getItem());
}
std::string SkinChooser::getSelectedSkin()
{
// Get the selected skin
wxDataViewItem item = _treeView->GetSelection();
if (item.IsOk())
{
wxutil::TreeModel::Row row(item, *_treeStore);
return row[_columns.fullName];
}
else
{
return "";
}
}
void SkinChooser::setSelectedSkin(const std::string& skin)
{
wxDataViewItem item = _treeStore->FindString(skin, _columns.fullName);
if (item.IsOk())
{
_treeView->Select(item);
}
else
{
_treeView->UnselectAll();
}
handleSelectionChange();
}
// Static method to display singleton instance and choose a skin
std::string SkinChooser::chooseSkin(const std::string& model,
const std::string& prev)
{
Instance()._model = model;
Instance()._prevSkin = prev;
Instance().ShowModal();
Instance().Hide();
return Instance()._lastSkin;
}
void SkinChooser::onMainFrameShuttingDown()
{
rMessage() << "SkinChooser shutting down." << std::endl;
_preview.reset();
// Destroy the window
SendDestroyEvent();
InstancePtr().reset();
}
void SkinChooser::handleSelectionChange()
{
// Set the model preview to show the model with the selected skin
_preview->setModel(_model);
_preview->setSkin(getSelectedSkin());
}
void SkinChooser::_onSelChanged(wxDataViewEvent& ev)
{
handleSelectionChange();
}
} // namespace