Skip to content

Commit 40a0016

Browse files
committed
Add rename action to context menu of the the model tree
Closes #233
1 parent 15e3134 commit 40a0016

File tree

5 files changed

+37
-26
lines changed

5 files changed

+37
-26
lines changed

gui/source/pre/MainWindow.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,11 +156,11 @@ MainWindow::MainWindow()
156156

157157
// Load geometry and state
158158
UserSettings settings;
159-
restoreState(settings.value("MainWindow/state").toByteArray());
160-
restoreGeometry(settings.value("MainWindow/geometry").toByteArray());
159+
restoreState(settings.value("MainWindow/state").toByteArray()); // TODO: Reenable
160+
restoreGeometry(settings.value("MainWindow/geometry").toByteArray()); // TODO: Reenable
161161

162162
// Load unit settings
163-
Quantities::loadFromSettings(settings); // TODO: Move to mainModel
163+
Quantities::loadFromSettings(settings); // TODO: Move to mainModel?
164164
}
165165

166166
// Attempts to load the given file

gui/source/pre/views/docks/EditDock.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
EditDock::EditDock(MainModel* viewModel)
2121
: placeholder(new QTableView()) // Show an empty table view by default
2222
{
23-
setObjectName("EditDock"); // Required to save state of main window
23+
setObjectName("EditDock2"); // Required to save state of main window // TODO: Remove 2 after one release cycle?
2424
setFeatures(QDockWidget::NoDockWidgetFeatures);
2525
setWindowTitle("Properties");
2626

gui/source/pre/views/docks/PlotDock.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ PlotDock::PlotDock(MainModel* model) {
2323
placeholder = new PlaceholderLabel();
2424

2525
this->setWindowTitle("Graph");
26-
this->setObjectName("PlotView"); // Required to save state of main window
26+
this->setObjectName("PlotDock2"); // Required to save state of main window // TODO: Remove 2 after one release cycle?
2727
this->setFeatures(QDockWidget::NoDockWidgetFeatures);
2828
this->setWidget(placeholder);
2929

gui/source/pre/views/docks/TreeDock.cpp

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,16 @@
1313

1414
#include <QDebug>
1515

16-
//viewModel->getMainTreeModel(), viewModel->getModelTreeSelectionModel()
16+
//model->getMainTreeModel(), model->getModelTreeSelectionModel()
1717

1818
TreeDock::TreeDock(MainModel* mainModel)
19-
: viewModel(mainModel->getMainTreeModel()),
19+
: model(mainModel->getMainTreeModel()),
2020
tree(new QTreeView()),
2121
menuAddMaterial(createMaterialMenu()),
2222
menuAddLayer(createLayerMenu()),
2323
menuAddSegment(createSegmentMenu())
2424
{
25-
this->setObjectName("PlotView"); // Required to save state of main window
25+
this->setObjectName("TreeDock2"); // Required to save state of main window // TODO: Remove 2 after one release cycle?
2626
this->setFeatures(QDockWidget::NoDockWidgetFeatures);
2727
this->setWindowTitle("Model");
2828
this->setWidget(tree);
@@ -36,19 +36,27 @@ TreeDock::TreeDock(MainModel* mainModel)
3636
actionRemove->setShortcutContext(Qt::WidgetShortcut);
3737
QObject::connect(actionRemove, &QAction::triggered, this, [=, this] {
3838
QModelIndexList selection = selectionModel->selectedIndexes();
39-
viewModel->removeIndexes(selection);
39+
model->removeIndexes(selection);
4040
});
4141

4242
actionMoveUp = new QAction(QIcon(":/icons/list-move-up.svg"), "Up", tree);
4343
QObject::connect(actionMoveUp, &QAction::triggered, this, [=, this] {
4444
QModelIndexList selection = selectionModel->selectedIndexes();
45-
viewModel->moveIndexesUp(selection);
45+
model->moveIndexesUp(selection);
4646
});
4747

4848
actionMoveDown = new QAction(QIcon(":/icons/list-move-down.svg"), "Down", tree);
4949
QObject::connect(actionMoveDown, &QAction::triggered, this, [=, this] {
5050
QModelIndexList selection = selectionModel->selectedIndexes();
51-
viewModel->moveIndexesDown(selection);
51+
model->moveIndexesDown(selection);
52+
});
53+
54+
actionRename = new QAction("Rename...", tree);
55+
QObject::connect(actionRename, &QAction::triggered, this, [=, this] {
56+
QModelIndex index = tree->currentIndex();
57+
if(index.isValid()) {
58+
tree->edit(index);
59+
}
5260
});
5361

5462
buttonAdd = new QToolButton();
@@ -74,15 +82,15 @@ TreeDock::TreeDock(MainModel* mainModel)
7482
hbox->addWidget(buttonUp);
7583
hbox->addWidget(buttonDown);
7684

77-
tree->setModel(viewModel);
85+
tree->setModel(model);
7886
tree->setSelectionModel(selectionModel);
7987
tree->setSelectionMode(QAbstractItemView::ExtendedSelection);
8088
tree->setContextMenuPolicy(Qt::ActionsContextMenu);
8189
tree->setLayout(hbox);
8290
tree->setHeaderHidden(true);
8391

8492
// Update the button states if either the model data/layout or the item selection changed
85-
QObject::connect(viewModel, &MainTreeModel::contentModified, this, &TreeDock::updateActions);
93+
QObject::connect(model, &MainTreeModel::contentModified, this, &TreeDock::updateActions);
8694
QObject::connect(selectionModel, &QItemSelectionModel::selectionChanged, this, &TreeDock::updateActions);
8795

8896
updateActions();
@@ -93,10 +101,10 @@ QMenu* TreeDock::createMaterialMenu() {
93101
menu->addAction(QIcon(":/icons/model-material.svg"), "New Material", this, [=, this]{
94102
QModelIndex index = tree->selectionModel()->currentIndex();
95103
if(index.internalId() == ItemType::TOPLEVEL) {
96-
viewModel->appendMaterial(); // If the top level item is selected, add the new material at the end
104+
model->appendMaterial(); // If the top level item is selected, add the new material at the end
97105
}
98106
else {
99-
viewModel->insertMaterial(index.row() + 1); // If a material is selected, insert the new material below
107+
model->insertMaterial(index.row() + 1); // If a material is selected, insert the new material below
100108
}
101109
});
102110

@@ -108,10 +116,10 @@ QMenu* TreeDock::createLayerMenu() {
108116
menu->addAction(QIcon(":/icons/model-layer.svg"), "New Layer", this, [=, this]{
109117
QModelIndex index = tree->selectionModel()->currentIndex();
110118
if(index.internalId() == ItemType::TOPLEVEL) {
111-
viewModel->appendLayer(); // If the top level item is selected, add the new layer at the end
119+
model->appendLayer(); // If the top level item is selected, add the new layer at the end
112120
}
113121
else {
114-
viewModel->insertLayer(index.row() + 1); // If a layer is selected, insert the new layer below
122+
model->insertLayer(index.row() + 1); // If a layer is selected, insert the new layer below
115123
}
116124
});
117125

@@ -122,10 +130,10 @@ QMenu* TreeDock::createSegmentMenu() {
122130
auto add_segment_of_type = [=, this](SegmentType type) {
123131
QModelIndex index = tree->selectionModel()->currentIndex();
124132
if(index.internalId() == ItemType::TOPLEVEL) {
125-
viewModel->appendSegment(type); // If the top level item is selected, add the new segment at the end
133+
model->appendSegment(type); // If the top level item is selected, add the new segment at the end
126134
}
127135
else {
128-
viewModel->insertSegment(index.row() + 1, type); // If a layer is selected, insert the new segment below
136+
model->insertSegment(index.row() + 1, type); // If a layer is selected, insert the new segment below
129137
}
130138
};
131139

@@ -149,17 +157,17 @@ void TreeDock::updateActions() {
149157

150158
// If adding something is possible, the add button gets assigned the respective menu with the selection of things to add
151159
// and the tree view gets assigned the same menu actions in order to show them in its context menu in a flattened way.
152-
if(viewModel->canInsertMaterial(selection)) {
160+
if(model->canInsertMaterial(selection)) {
153161
tree->addActions(menuAddMaterial->actions());
154162
buttonAdd->setMenu(menuAddMaterial);
155163
buttonAdd->setEnabled(true);
156164
}
157-
else if(viewModel->canInsertLayer(selection)) {
165+
else if(model->canInsertLayer(selection)) {
158166
tree->addActions(menuAddLayer->actions());
159167
buttonAdd->setMenu(menuAddLayer);
160168
buttonAdd->setEnabled(true);
161169
}
162-
else if(viewModel->canInsertSegment(selection)) {
170+
else if(model->canInsertSegment(selection)) {
163171
tree->addActions(menuAddSegment->actions());
164172
buttonAdd->setMenu(menuAddSegment);
165173
buttonAdd->setEnabled(true);
@@ -170,9 +178,10 @@ void TreeDock::updateActions() {
170178
}
171179

172180
// Other actions just get enabled/disbled based on the selection, which also affects all associated buttons and menu items
173-
actionRemove->setEnabled(viewModel->canRemoveIndexes(selection));
174-
actionMoveUp->setEnabled(viewModel->canMoveIndexesUp(selection));
175-
actionMoveDown->setEnabled(viewModel->canMoveIndexesDown(selection));
181+
actionMoveUp->setEnabled(model->canMoveIndexesUp(selection));
182+
actionMoveDown->setEnabled(model->canMoveIndexesDown(selection));
183+
actionRename->setEnabled(selection.size() == 1 && (selection[0].flags() & Qt::ItemIsEditable));
184+
actionRemove->setEnabled(model->canRemoveIndexes(selection));
176185

177186
// Add back standard actions of the tree view
178187

@@ -185,6 +194,7 @@ void TreeDock::updateActions() {
185194
tree->addAction(sep1);
186195
tree->addAction(actionMoveUp);
187196
tree->addAction(actionMoveDown);
197+
tree->addAction(actionRename);
188198
tree->addAction(sep2);
189199
tree->addAction(actionRemove);
190200
}

gui/source/pre/views/docks/TreeDock.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,14 @@ class TreeDock: public QDockWidget {
1414
TreeDock(MainModel* mainModel);
1515

1616
private:
17-
MainTreeModel* viewModel;
17+
MainTreeModel* model;
1818
QTreeView* tree;
1919

2020
QToolButton* buttonAdd;
2121
QAction* actionRemove;
2222
QAction* actionMoveUp;
2323
QAction* actionMoveDown;
24+
QAction* actionRename;
2425

2526
QMenu* menuAddMaterial;
2627
QMenu* menuAddLayer;

0 commit comments

Comments
 (0)