Skip to content

Commit

Permalink
#5364: ColourSchemeEditor now uses plain wxDataViewListCtrl
Browse files Browse the repository at this point in the history
Simplify the ColourSchemeEditor implementation by moving away from the
heavyweight custom TreeModel/TreeStore and switching to a simple
wxDataViewListCtrl instead.
  • Loading branch information
Matthew Mott committed Dec 17, 2020
1 parent 45a25ed commit 95db3b8
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 87 deletions.
140 changes: 69 additions & 71 deletions radiant/uimanager/colourscheme/ColourSchemeEditor.cpp
Expand Up @@ -15,7 +15,7 @@
#include <wx/clrpicker.h>
#include <wx/stattext.h>

namespace ui
namespace ui
{

namespace
Expand All @@ -24,8 +24,7 @@ namespace
}

ColourSchemeEditor::ColourSchemeEditor() :
DialogBase(_(EDITOR_WINDOW_TITLE)),
_listStore(new wxutil::TreeModel(_columns, true))
DialogBase(_(EDITOR_WINDOW_TITLE))
{
SetSizer(new wxBoxSizer(wxVERTICAL));

Expand All @@ -45,97 +44,96 @@ ColourSchemeEditor::ColourSchemeEditor() :

void ColourSchemeEditor::populateTree()
{
GlobalColourSchemeManager().foreachScheme([&](const std::string& name, colours::IColourScheme&)
{
wxutil::TreeModel::Row row = _listStore->AddItem();

row[_columns.name] = name;

row.SendItemAdded();
});
GlobalColourSchemeManager().foreachScheme(
[&](const std::string& name, colours::IColourScheme&)
{
wxVector<wxVariant> row;
row.push_back(wxVariant(name));
_schemeList->AppendItem(row);
}
);
}

void ColourSchemeEditor::constructWindow()
{
wxBoxSizer* hbox = new wxBoxSizer(wxHORIZONTAL);
wxBoxSizer* hbox = new wxBoxSizer(wxHORIZONTAL);

GetSizer()->Add(hbox, 1, wxEXPAND | wxALL, 12);
GetSizer()->Add(CreateStdDialogButtonSizer(wxOK | wxCANCEL), 0,
wxALIGN_RIGHT | wxLEFT | wxBOTTOM | wxRIGHT, 12);
GetSizer()->Add(hbox, 1, wxEXPAND | wxALL, 12);
GetSizer()->Add(CreateStdDialogButtonSizer(wxOK | wxCANCEL), 0,
wxALIGN_RIGHT | wxLEFT | wxBOTTOM | wxRIGHT, 12);

// Create the treeview and the buttons
wxBoxSizer* treeViewVbox = new wxBoxSizer(wxVERTICAL);
hbox->Add(treeViewVbox, 0, wxEXPAND | wxRIGHT, 6);
// Create the scheme list and the buttons
wxBoxSizer* treeViewVbox = new wxBoxSizer(wxVERTICAL);
hbox->Add(treeViewVbox, 0, wxEXPAND | wxRIGHT, 6);

_treeView = wxutil::TreeView::CreateWithModel(this, _listStore.get(), wxDV_NO_HEADER);
_treeView->SetMinClientSize(wxSize(200, -1));
treeViewVbox->Add(_treeView, 1, wxEXPAND | wxBOTTOM, 6);
_schemeList = new wxDataViewListCtrl(this, wxID_ANY, wxDefaultPosition,
wxDefaultSize, wxDV_NO_HEADER);
_schemeList->SetMinClientSize(wxSize(200, -1));
treeViewVbox->Add(_schemeList, 1, wxEXPAND | wxBOTTOM, 6);

// Create a new column and set its parameters
_treeView->AppendTextColumn(_("Colour"), _columns.name.getColumnIndex(),
wxDATAVIEW_CELL_INERT, wxCOL_WIDTH_AUTOSIZE, wxALIGN_NOT, wxDATAVIEW_COL_SORTABLE);
// Create a text column to show the scheme name
_schemeList->AppendTextColumn(
_("Colour scheme"), wxDATAVIEW_CELL_INERT, wxCOL_WIDTH_AUTOSIZE,
wxALIGN_LEFT, wxDATAVIEW_COL_SORTABLE
);

// Connect the signal AFTER selecting the active scheme
_treeView->Connect(wxEVT_DATAVIEW_SELECTION_CHANGED,
wxDataViewEventHandler(ColourSchemeEditor::callbackSelChanged), NULL, this);
// Connect the signal AFTER selecting the active scheme
_schemeList->Connect(wxEVT_DATAVIEW_SELECTION_CHANGED,
wxDataViewEventHandler(ColourSchemeEditor::callbackSelChanged), NULL, this);

// Treeview buttons
wxBoxSizer* buttonBox = new wxBoxSizer(wxHORIZONTAL);
treeViewVbox->Add(buttonBox, 0, wxEXPAND, 6);
// Treeview buttons
wxBoxSizer* buttonBox = new wxBoxSizer(wxHORIZONTAL);
treeViewVbox->Add(buttonBox, 0, wxEXPAND, 6);

_deleteButton = new wxButton(this, wxID_DELETE, _("Delete"));
wxButton* copyButton = new wxButton(this, wxID_COPY, _("Copy"));
_deleteButton = new wxButton(this, wxID_DELETE, _("Delete"));
wxButton* copyButton = new wxButton(this, wxID_COPY, _("Copy"));

buttonBox->Add(copyButton, 1, wxEXPAND | wxRIGHT, 6);
buttonBox->Add(_deleteButton, 1, wxEXPAND);
buttonBox->Add(copyButton, 1, wxEXPAND | wxRIGHT, 6);
buttonBox->Add(_deleteButton, 1, wxEXPAND);

copyButton->Connect(wxEVT_BUTTON, wxCommandEventHandler(ColourSchemeEditor::callbackCopy), NULL, this);
_deleteButton->Connect(wxEVT_BUTTON, wxCommandEventHandler(ColourSchemeEditor::callbackDelete), NULL, this);
copyButton->Connect(wxEVT_BUTTON, wxCommandEventHandler(ColourSchemeEditor::callbackCopy), NULL, this);
_deleteButton->Connect(wxEVT_BUTTON, wxCommandEventHandler(ColourSchemeEditor::callbackDelete), NULL, this);

// The Box containing the Colour, pack it into the right half of the hbox
_colourFrame = new wxPanel(this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL | wxDOUBLE_BORDER);
hbox->Add(_colourFrame, 1, wxEXPAND);
// The Box containing the Colour, pack it into the right half of the hbox
_colourFrame = new wxPanel(this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL | wxDOUBLE_BORDER);
hbox->Add(_colourFrame, 1, wxEXPAND);
}

void ColourSchemeEditor::selectActiveScheme()
{
wxDataViewItem found = _listStore->FindString(
GlobalColourSchemeManager().getActiveScheme().getName(), _columns.name);

_treeView->Select(found);
// Find a row matching the active colour scheme name
wxString name = GlobalColourSchemeManager().getActiveScheme().getName();
unsigned r = 0;
for ( ; r < _schemeList->GetItemCount(); ++r)
{
wxString rowName = _schemeList->GetTextValue(r, 0);
if (rowName == name)
break;
}

_schemeList->SelectRow(r);
selectionChanged();
}

void ColourSchemeEditor::deleteSchemeFromList()
{
wxDataViewItem item = _treeView->GetSelection();

if (item.IsOk())
{
_listStore->RemoveItem(item);
}
// Delete the selected row
int row = _schemeList->GetSelectedRow();
if (row != wxNOT_FOUND)
_schemeList->DeleteItem(row);

// Select the first scheme
wxDataViewItemArray children;

if (_listStore->GetChildren(_listStore->GetRoot(), children) > 0)
{
_treeView->Select(*children.begin());
selectionChanged();
}
if (_schemeList->GetItemCount() > 0)
_schemeList->SelectRow(0);
}

std::string ColourSchemeEditor::getSelectedScheme()
{
wxDataViewItem item = _treeView->GetSelection();

if (item.IsOk())
{
wxutil::TreeModel::Row row(item, *_listStore);
return row[_columns.name];
}

return "";
int row = _schemeList->GetSelectedRow();
if (row != wxNOT_FOUND)
return _schemeList->GetTextValue(row, 0).ToStdString();
else
return "";
}

wxSizer* ColourSchemeEditor::constructColourSelector(colours::IColourItem& colour, const std::string& name)
Expand All @@ -160,7 +158,7 @@ wxSizer* ColourSchemeEditor::constructColourSelector(colours::IColourItem& colou
{
callbackColorChanged(ev, colour);
});

// Create the description label
wxStaticText* label = new wxStaticText(_colourFrame, wxID_ANY, description);

Expand Down Expand Up @@ -281,9 +279,9 @@ void ColourSchemeEditor::copyScheme()
GlobalColourSchemeManager().setActive(newName);

// Add the new list item to the ListStore
wxutil::TreeModel::Row row = _listStore->AddItem();
row[_columns.name] = newName;
row.SendItemAdded();
wxVector<wxVariant> rowData;
rowData.push_back(wxVariant(newName));
_schemeList->AppendItem(rowData);

// Highlight the copied scheme
selectActiveScheme();
Expand All @@ -306,7 +304,7 @@ void ColourSchemeEditor::callbackColorChanged(wxColourPickerEvent& ev, colours::

// Update the colourItem class
item.getColour().set(
static_cast<double>(colour.Red()) / 255.0,
static_cast<double>(colour.Red()) / 255.0,
static_cast<double>(colour.Green()) / 255.0,
static_cast<double>(colour.Blue()) / 255.0);

Expand All @@ -323,7 +321,7 @@ void ColourSchemeEditor::callbackSelChanged(wxDataViewEvent& ev)
int ColourSchemeEditor::ShowModal()
{
int returnCode = DialogBase::ShowModal();

if (returnCode == wxID_OK)
{
GlobalColourSchemeManager().setActive(getSelectedScheme());
Expand Down
18 changes: 2 additions & 16 deletions radiant/uimanager/colourscheme/ColourSchemeEditor.h
Expand Up @@ -22,22 +22,8 @@ namespace ui
class ColourSchemeEditor :
public wxutil::DialogBase
{
// The treeview and its selection pointer
wxutil::TreeView* _treeView;

struct Columns :
public wxutil::TreeModel::ColumnRecord
{
Columns() :
name(add(wxutil::TreeModel::Column::String))
{}

wxutil::TreeModel::Column name;
};

// The list store containing the list of ColourSchemes
Columns _columns;
wxutil::TreeModel::Ptr _listStore;
// The list of available colour schemes
wxDataViewListCtrl* _schemeList = nullptr;

// The vbox containing the colour buttons and its frame
wxPanel* _colourFrame;
Expand Down

0 comments on commit 95db3b8

Please sign in to comment.