/
LayerControlDialog.cpp
572 lines (448 loc) · 15.4 KB
/
LayerControlDialog.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
#include "LayerControlDialog.h"
#include "i18n.h"
#include "itextstream.h"
#include "ilayer.h"
#include "ui/imainframe.h"
#include "iselection.h"
#include <wx/button.h>
#include <wx/tglbtn.h>
#include <wx/sizer.h>
#include <wx/panel.h>
#include <wx/artprov.h>
#include "wxutil/Button.h"
#include "scene/LayerUsageBreakdown.h"
#include "wxutil/Bitmap.h"
#include "wxutil/EntryAbortedException.h"
#include "wxutil/dataview/IndicatorColumn.h"
#include "wxutil/dataview/TreeView.h"
#include "wxutil/dataview/TreeViewItemStyle.h"
#include "wxutil/dialog/Dialog.h"
#include "wxutil/dialog/MessageBox.h"
namespace ui
{
namespace
{
const std::string RKEY_ROOT = "user/ui/layers/controlDialog/";
const std::string RKEY_WINDOW_STATE = RKEY_ROOT + "window";
}
LayerControlDialog::LayerControlDialog() :
TransientWindow(_("Layers"), GlobalMainFrame().getWxTopLevelWindow(), true),
_layersView(nullptr),
_showAllLayers(nullptr),
_hideAllLayers(nullptr),
_refreshTreeOnIdle(false),
_updateTreeOnIdle(false),
_rescanSelectionOnIdle(false)
{
populateWindow();
Bind(wxEVT_IDLE, [&](wxIdleEvent&) { onIdle(); });
InitialiseWindowPosition(230, 400, RKEY_WINDOW_STATE);
SetMinClientSize(wxSize(230, 200));
}
void LayerControlDialog::populateWindow()
{
_layerStore = new wxutil::TreeModel(_columns);
_layersView = wxutil::TreeView::CreateWithModel(this, _layerStore.get(), wxDV_SINGLE | wxDV_NO_HEADER);
_layersView->AppendToggleColumn(_("Visible"), _columns.visible.getColumnIndex(),
wxDATAVIEW_CELL_ACTIVATABLE, wxCOL_WIDTH_AUTOSIZE, wxALIGN_NOT);
_layersView->AppendColumn(new wxutil::IndicatorColumn(_("Contains Selection"),
_columns.selectionIsPartOfLayer.getColumnIndex()));
_layersView->AppendTextColumn("", _columns.name.getColumnIndex(),
wxDATAVIEW_CELL_INERT, wxCOL_WIDTH_AUTOSIZE, wxALIGN_NOT, wxDATAVIEW_COL_SORTABLE);
_layersView->Bind(wxEVT_DATAVIEW_ITEM_ACTIVATED, &LayerControlDialog::onItemActivated, this);
_layersView->Bind(wxEVT_DATAVIEW_ITEM_VALUE_CHANGED, &LayerControlDialog::onItemToggled, this);
_layersView->Bind(wxEVT_DATAVIEW_SELECTION_CHANGED, &LayerControlDialog::onItemSelected, this);
SetSizer(new wxBoxSizer(wxVERTICAL));
auto overallVBox = new wxBoxSizer(wxVERTICAL);
GetSizer()->Add(overallVBox, 1, wxEXPAND | wxALL, 12);
overallVBox->Add(_layersView, 1, wxEXPAND);
// Add the option buttons ("Create Layer", etc.) to the window
createButtons();
}
void LayerControlDialog::createButtons()
{
// Show all / hide all buttons
auto hideShowBox = new wxBoxSizer(wxHORIZONTAL);
_showAllLayers = new wxButton(this, wxID_ANY, _("Show all"));
_hideAllLayers = new wxButton(this, wxID_ANY, _("Hide all"));
_showAllLayers->Bind(wxEVT_BUTTON, [this](auto& ev) { setVisibilityOfAllLayers(true); });
_hideAllLayers->Bind(wxEVT_BUTTON, [this](auto& ev) { setVisibilityOfAllLayers(false); });
// Create layer button
auto topRow = new wxBoxSizer(wxHORIZONTAL);
auto createButton = new wxButton(this, wxID_ANY, _("New"));
createButton->SetBitmap(wxArtProvider::GetBitmap(wxART_PLUS));
wxutil::button::connectToCommand(createButton, "CreateNewLayerDialog");
_deleteButton = new wxBitmapButton(this, wxID_ANY, wxutil::GetLocalBitmap("delete.png"));
_deleteButton->Bind(wxEVT_BUTTON, &LayerControlDialog::onDeleteLayer, this);
_deleteButton->SetToolTip(_("Delete this layer"));
_renameButton = new wxBitmapButton(this, wxID_ANY, wxutil::GetLocalBitmap("edit.png"));
_renameButton->Bind(wxEVT_BUTTON, &LayerControlDialog::onRenameLayer, this);
_renameButton->SetToolTip(_("Rename this layer"));
topRow->Add(createButton, 1, wxEXPAND | wxRIGHT, 6);
topRow->Add(_renameButton, 0, wxEXPAND | wxRIGHT, 6);
topRow->Add(_deleteButton, 0, wxEXPAND, 6);
hideShowBox->Add(_showAllLayers, 1, wxEXPAND | wxTOP, 6);
hideShowBox->Add(_hideAllLayers, 1, wxEXPAND | wxLEFT | wxTOP, 6);
GetSizer()->Add(topRow, 0, wxEXPAND | wxLEFT | wxRIGHT, 12);
GetSizer()->Add(hideShowBox, 0, wxEXPAND | wxBOTTOM | wxLEFT | wxRIGHT, 12);
}
void LayerControlDialog::clearControls()
{
_layerItemMap.clear();
_layerStore->Clear();
}
void LayerControlDialog::queueRefresh()
{
_refreshTreeOnIdle = true;
_rescanSelectionOnIdle = true;
}
void LayerControlDialog::queueUpdate()
{
_updateTreeOnIdle = true;
_rescanSelectionOnIdle = true;
}
void LayerControlDialog::refresh()
{
_refreshTreeOnIdle = false;
clearControls();
if (!GlobalMapModule().getRoot()) return; // no map present
// Traverse the layers
auto& layerManager = GlobalMapModule().getRoot()->getLayerManager();
auto activeLayerId = layerManager.getActiveLayer();
// Update the show/hide all button sensitiveness
std::size_t numVisible = 0;
std::size_t numHidden = 0;
layerManager.foreachLayer([&](int layerId, const std::string& layerName)
{
// Store the object in a sorted container
auto row = _layerStore->AddItem();
row[_columns.id] = layerId;
row[_columns.name] = layerName;
if (activeLayerId == layerId)
{
row[_columns.name] = wxutil::TreeViewItemStyle::ActiveItemStyle();
}
if (layerManager.layerIsVisible(layerId))
{
row[_columns.visible] = true;
numVisible++;
}
else
{
row[_columns.visible] = false;
numHidden++;
}
row.SendItemAdded();
_layerItemMap.emplace(layerId, row.getItem());
});
updateButtonSensitivity(numVisible, numHidden);
}
void LayerControlDialog::update()
{
_updateTreeOnIdle = false;
if (!GlobalMapModule().getRoot())
{
return;
}
auto& layerManager = GlobalMapModule().getRoot()->getLayerManager();
auto activeLayerId = layerManager.getActiveLayer();
// Update the show/hide all button sensitiveness
std::size_t numVisible = 0;
std::size_t numHidden = 0;
layerManager.foreachLayer([&](int layerId, const std::string& layerName)
{
auto existingItem = _layerItemMap.find(layerId);
if (existingItem == _layerItemMap.end()) return; // tracking error?
wxutil::TreeModel::Row row(existingItem->second, *_layerStore);
row[_columns.name] = layerName;
row[_columns.name] = activeLayerId == layerId ?
wxutil::TreeViewItemStyle::ActiveItemStyle() : wxDataViewItemAttr(); // no style
if (layerManager.layerIsVisible(layerId))
{
row[_columns.visible] = true;
numVisible++;
}
else
{
row[_columns.visible] = false;
numHidden++;
}
row.SendItemChanged();
});
updateButtonSensitivity(numVisible, numHidden);
}
void LayerControlDialog::updateButtonSensitivity(std::size_t numVisible, std::size_t numHidden)
{
_showAllLayers->Enable(numHidden > 0);
_hideAllLayers->Enable(numVisible > 0);
updateItemActionSensitivity();
}
void LayerControlDialog::updateItemActionSensitivity()
{
auto selectedLayerId = getSelectedLayerId();
// Don't allow deleting or renaming the default layer (or -1)
_deleteButton->Enable(selectedLayerId > 0);
_renameButton->Enable(selectedLayerId > 0);
}
void LayerControlDialog::updateLayerUsage()
{
_rescanSelectionOnIdle = false;
// Scan the selection and get the histogram
auto breakDown = scene::LayerUsageBreakdown::CreateFromSelection();
GlobalMapModule().getRoot()->getLayerManager().foreachLayer([&](int layerId, const std::string& layerName)
{
auto existingItem = _layerItemMap.find(layerId);
if (existingItem == _layerItemMap.end()) return; // tracking error?
wxutil::TreeModel::Row row(existingItem->second, *_layerStore);
row[_columns.selectionIsPartOfLayer] = breakDown[layerId] > 0;
row.SendItemChanged();
});
}
void LayerControlDialog::onIdle()
{
if (_refreshTreeOnIdle)
{
refresh();
}
if (_updateTreeOnIdle)
{
update();
}
if (_rescanSelectionOnIdle)
{
updateLayerUsage();
}
}
void LayerControlDialog::ToggleDialog(const cmd::ArgumentList& args)
{
Instance().ToggleVisibility();
}
void LayerControlDialog::onMainFrameConstructed()
{
// Lookup the stored window information in the registry
if (GlobalRegistry().getAttribute(RKEY_WINDOW_STATE, "visible") == "1")
{
// Show dialog
Instance().Show();
}
}
void LayerControlDialog::onMainFrameShuttingDown()
{
rMessage() << "LayerControlDialog shutting down." << std::endl;
// Write the visibility status to the registry
GlobalRegistry().setAttribute(RKEY_WINDOW_STATE, "visible", IsShownOnScreen() ? "1" : "0");
// Hide the window and save its state
if (IsShownOnScreen())
{
Hide();
}
// Destroy the window
SendDestroyEvent();
// Final step: clear the instance pointer
InstancePtr().reset();
}
std::shared_ptr<LayerControlDialog>& LayerControlDialog::InstancePtr()
{
static std::shared_ptr<LayerControlDialog> _instancePtr;
return _instancePtr;
}
LayerControlDialog& LayerControlDialog::Instance()
{
auto& instancePtr = InstancePtr();
if (!instancePtr)
{
// Not yet instantiated, do it now
instancePtr.reset(new LayerControlDialog);
// Pre-destruction cleanup
GlobalMainFrame().signal_MainFrameShuttingDown().connect(
sigc::mem_fun(*instancePtr, &LayerControlDialog::onMainFrameShuttingDown));
}
return *instancePtr;
}
void LayerControlDialog::_preShow()
{
TransientWindow::_preShow();
_selectionChangedSignal = GlobalSelectionSystem().signal_selectionChanged().connect([this](const ISelectable&)
{
_rescanSelectionOnIdle = true;
});
connectToMapRoot();
_mapEventSignal = GlobalMapModule().signal_mapEvent().connect(
sigc::mem_fun(this, &LayerControlDialog::onMapEvent)
);
// Re-populate the dialog
queueRefresh();
}
void LayerControlDialog::_postHide()
{
disconnectFromMapRoot();
_mapEventSignal.disconnect();
_selectionChangedSignal.disconnect();
_refreshTreeOnIdle = false;
_updateTreeOnIdle = false;
_rescanSelectionOnIdle = false;
}
void LayerControlDialog::connectToMapRoot()
{
// Always disconnect first
disconnectFromMapRoot();
if (GlobalMapModule().getRoot())
{
auto& layerSystem = GlobalMapModule().getRoot()->getLayerManager();
// Layer creation/addition/removal triggers a refresh
_layersChangedSignal = layerSystem.signal_layersChanged().connect(
sigc::mem_fun(this, &LayerControlDialog::queueRefresh));
// Visibility change doesn't repopulate the dialog
_layerVisibilityChangedSignal = layerSystem.signal_layerVisibilityChanged().connect(
sigc::mem_fun(this, &LayerControlDialog::queueUpdate));
// Node membership triggers a selection rescan
_nodeLayerMembershipChangedSignal = layerSystem.signal_nodeMembershipChanged().connect([this]()
{
_rescanSelectionOnIdle = true;
});
}
}
void LayerControlDialog::disconnectFromMapRoot()
{
_nodeLayerMembershipChangedSignal.disconnect();
_layersChangedSignal.disconnect();
_layerVisibilityChangedSignal.disconnect();
}
void LayerControlDialog::setVisibilityOfAllLayers(bool visible)
{
if (!GlobalMapModule().getRoot())
{
rError() << "Can't change layer visibility, no map loaded." << std::endl;
return;
}
auto& layerSystem = GlobalMapModule().getRoot()->getLayerManager();
layerSystem.foreachLayer([&](int layerID, const std::string&)
{
layerSystem.setLayerVisibility(layerID, visible);
});
}
void LayerControlDialog::onMapEvent(IMap::MapEvent ev)
{
if (ev == IMap::MapLoaded)
{
// Rebuild the dialog once a map is loaded
connectToMapRoot();
queueRefresh();
}
else if (ev == IMap::MapUnloading)
{
disconnectFromMapRoot();
clearControls();
}
}
int LayerControlDialog::getSelectedLayerId()
{
auto item = _layersView->GetSelection();
if (!item.IsOk()) return -1;
wxutil::TreeModel::Row row(item, *_layerStore);
return row[_columns.id].getInteger();
}
void LayerControlDialog::onItemActivated(wxDataViewEvent& ev)
{
// Don't react to double-clicks on the checkbox
if (ev.GetColumn() == _columns.visible.getColumnIndex())
{
return;
}
if (!GlobalMapModule().getRoot())
{
rError() << "Can't select layer, no map root present" << std::endl;
return;
}
auto layerId = getSelectedLayerId();
// When holding down CTRL the user sets this as active
if (wxGetKeyState(WXK_CONTROL))
{
GlobalMapModule().getRoot()->getLayerManager().setActiveLayer(layerId);
queueRefresh();
return;
}
// By default, we SELECT the layer
// The user can choose to DESELECT the layer when holding down shift
bool selected = !wxGetKeyState(WXK_SHIFT);
// Set the entire layer to selected
GlobalMapModule().getRoot()->getLayerManager().setSelected(layerId, selected);
}
void LayerControlDialog::onItemToggled(wxDataViewEvent& ev)
{
if (!GlobalMapModule().getRoot()) return;
if (ev.GetDataViewColumn() != nullptr &&
static_cast<int>(ev.GetDataViewColumn()->GetModelColumn()) == _columns.visible.getColumnIndex())
{
// Model value in the boolean column has changed, this means the checkbox has been toggled
wxutil::TreeModel::Row row(ev.GetItem(), *_layerStore);
GlobalMapModule().getRoot()->getLayerManager().setLayerVisibility(row[_columns.id].getInteger(), row[_columns.visible].getBool());
}
}
void LayerControlDialog::onItemSelected(wxDataViewEvent& ev)
{
updateItemActionSensitivity();
}
void LayerControlDialog::onRenameLayer(wxCommandEvent& ev)
{
if (!GlobalMapModule().getRoot())
{
rError() << "Can't rename layer, no map root present" << std::endl;
return;
}
auto selectedLayerId = getSelectedLayerId();
auto& layerSystem = GlobalMapModule().getRoot()->getLayerManager();
while (true)
{
// Query the name of the new layer from the user
std::string newLayerName;
try
{
newLayerName = wxutil::Dialog::TextEntryDialog(
_("Rename Layer"),
_("Enter new Layer Name"),
layerSystem.getLayerName(selectedLayerId),
this
);
}
catch (wxutil::EntryAbortedException&)
{
break;
}
// Attempt to rename the layer, this will return -1 if the operation fails
bool success = layerSystem.renameLayer(selectedLayerId, newLayerName);
if (success)
{
// Stop here, the control might already have been destroyed
GlobalMapModule().setModified(true);
return;
}
else
{
// Wrong name, let the user try again
wxutil::Messagebox::ShowError(_("Could not rename layer, please try again."));
continue;
}
}
}
void LayerControlDialog::onDeleteLayer(wxCommandEvent& ev)
{
if (!GlobalMapModule().getRoot())
{
rError() << "Can't delete layer, no map root present" << std::endl;
return;
}
auto selectedLayerId = getSelectedLayerId();
auto& layerSystem = GlobalMapModule().getRoot()->getLayerManager();
// Ask the about the deletion
auto msg = _("Do you really want to delete this layer?");
msg += "\n" + layerSystem.getLayerName(selectedLayerId);
IDialogPtr box = GlobalDialogManager().createMessageBox(
_("Confirm Layer Deletion"), msg, IDialog::MESSAGE_ASK
);
if (box->run() == IDialog::RESULT_YES)
{
GlobalCommandSystem().executeCommand("DeleteLayer", cmd::Argument(selectedLayerId));
}
}
} // namespace ui