From 70c6378a8147efaa2d2098c0f23c630bae4d7891 Mon Sep 17 00:00:00 2001 From: Julia Ritz Date: Sun, 17 Dec 2023 21:22:34 +0100 Subject: [PATCH] fix crash on wrongful click; add naming for directory --- .../context_manager_widget.h | 7 +- .../models/context_table_model.h | 1 + .../context_manager_widget.cpp | 93 +++++++++---------- .../models/context_table_model.cpp | 87 +++++++---------- 4 files changed, 85 insertions(+), 103 deletions(-) diff --git a/plugins/gui/include/gui/context_manager_widget/context_manager_widget.h b/plugins/gui/include/gui/context_manager_widget/context_manager_widget.h index bf0619fa94d..28a5ef8d162 100644 --- a/plugins/gui/include/gui/context_manager_widget/context_manager_widget.h +++ b/plugins/gui/include/gui/context_manager_widget/context_manager_widget.h @@ -178,13 +178,15 @@ namespace hal GraphTabWidget* mTabView; QTreeView* mContextTreeView; - ContextTreeModel* mContextTableModel; + ContextTreeModel* mContextTreeModel; ContextTableProxyModel* mContextTableProxyModel; Searchbar* mSearchbar; QString mDisabledIconStyle; + QAction* mNewDirectoryAction; + QAction* mNewViewAction; QString mNewViewIconPath; QString mNewViewIconStyle; @@ -211,8 +213,9 @@ namespace hal QShortcut* mShortCutDeleteItem; - void handleCreateClicked(const QPoint& point); + void handleCreateClicked(); void handleCreateContextClicked(); + void handleCreateDirectoryClicked(); void handleRenameContextClicked(); void handleDuplicateContextClicked(); void handleDeleteContextClicked(); diff --git a/plugins/gui/include/gui/context_manager_widget/models/context_table_model.h b/plugins/gui/include/gui/context_manager_widget/models/context_table_model.h index aa8eaacfa49..eb9ae3ff69c 100644 --- a/plugins/gui/include/gui/context_manager_widget/models/context_table_model.h +++ b/plugins/gui/include/gui/context_manager_widget/models/context_table_model.h @@ -42,6 +42,7 @@ namespace hal public: ContextDirectory(u32 id, QString name):mId(id), mName(name){} + QString getName() { return mName; } }; diff --git a/plugins/gui/src/context_manager_widget/context_manager_widget.cpp b/plugins/gui/src/context_manager_widget/context_manager_widget.cpp index c13c2f7fa43..64561ec3186 100644 --- a/plugins/gui/src/context_manager_widget/context_manager_widget.cpp +++ b/plugins/gui/src/context_manager_widget/context_manager_widget.cpp @@ -33,11 +33,12 @@ #include "gui/user_action/user_action_compound.h" #include #include +#include namespace hal { ContextManagerWidget::ContextManagerWidget(GraphTabWidget* tab_view, QWidget* parent) - : ContentWidget("Views", parent), mSearchbar(new Searchbar(this)), mNewViewAction(new QAction(this)), mRenameAction(new QAction(this)), mDuplicateAction(new QAction(this)), + : ContentWidget("Views", parent), mSearchbar(new Searchbar(this)), mNewDirectoryAction(new QAction(this)), mNewViewAction(new QAction(this)), mRenameAction(new QAction(this)), mDuplicateAction(new QAction(this)), mDeleteAction(new QAction(this)), mOpenAction(new QAction(this)) { //needed to load the properties @@ -46,13 +47,15 @@ namespace hal mOpenAction->setIcon(gui_utility::getStyledSvgIcon(mOpenIconStyle, mOpenIconPath)); mNewViewAction->setIcon(gui_utility::getStyledSvgIcon(mNewViewIconStyle, mNewViewIconPath)); + mNewDirectoryAction->setIcon(gui_utility::getStyledSvgIcon(mNewViewIconStyle, mNewViewIconPath)); mRenameAction->setIcon(gui_utility::getStyledSvgIcon(mRenameIconStyle, mRenameIconPath)); mDuplicateAction->setIcon(gui_utility::getStyledSvgIcon(mDuplicateIconStyle, mDuplicateIconPath)); mDeleteAction->setIcon(gui_utility::getStyledSvgIcon(mDeleteIconStyle, mDeleteIconPath)); mSearchAction->setIcon(gui_utility::getStyledSvgIcon(mSearchIconStyle, mSearchIconPath)); mOpenAction->setToolTip("Open"); - mNewViewAction->setToolTip("New"); + mNewViewAction->setToolTip("New View"); + mNewDirectoryAction->setToolTip("New Directory"); mRenameAction->setToolTip("Rename"); mDuplicateAction->setToolTip("Duplicate"); mDeleteAction->setToolTip("Delete"); @@ -60,6 +63,7 @@ namespace hal mOpenAction->setText("Open view"); mNewViewAction->setText("Create new view"); + mNewDirectoryAction->setText("Create new Directory"); mRenameAction->setText("Rename view"); mDuplicateAction->setText("Duplicate view"); mDeleteAction->setText("Delete view"); @@ -70,10 +74,10 @@ namespace hal //mDuplicateAction->setEnabled(false); //mDeleteAction->setEnabled(false); - mContextTableModel = gGraphContextManager->getContextTableModel(); + mContextTreeModel = gGraphContextManager->getContextTableModel(); mContextTableProxyModel = new ContextTableProxyModel(this); - mContextTableProxyModel->setSourceModel(mContextTableModel); + mContextTableProxyModel->setSourceModel(mContextTreeModel); mContextTableProxyModel->setSortRole(Qt::UserRole); mContextTreeView = new QTreeView(this); @@ -99,6 +103,7 @@ namespace hal connect(mOpenAction, &QAction::triggered, this, &ContextManagerWidget::handleOpenContextClicked); connect(mNewViewAction, &QAction::triggered, this, &ContextManagerWidget::handleCreateContextClicked); + connect(mNewDirectoryAction, &QAction::triggered, this, &ContextManagerWidget::handleCreateDirectoryClicked); connect(mRenameAction, &QAction::triggered, this, &ContextManagerWidget::handleRenameContextClicked); connect(mDuplicateAction, &QAction::triggered, this, &ContextManagerWidget::handleDuplicateContextClicked); connect(mDeleteAction, &QAction::triggered, this, &ContextManagerWidget::handleDeleteContextClicked); @@ -107,8 +112,8 @@ namespace hal connect(mContextTreeView, &QTableView::customContextMenuRequested, this, &ContextManagerWidget::handleContextMenuRequest); connect(mContextTreeView, &QTableView::doubleClicked, this, &ContextManagerWidget::handleOpenContextClicked); connect(mContextTreeView->selectionModel(), &QItemSelectionModel::selectionChanged, this, &ContextManagerWidget::handleSelectionChanged); - connect(mContextTableModel, &ContextTreeModel::rowsRemoved, this, &ContextManagerWidget::handleDataChanged); - connect(mContextTableModel, &ContextTreeModel::rowsInserted, this, &ContextManagerWidget::handleDataChanged); + connect(mContextTreeModel, &ContextTreeModel::rowsRemoved, this, &ContextManagerWidget::handleDataChanged); + connect(mContextTreeModel, &ContextTreeModel::rowsInserted, this, &ContextManagerWidget::handleDataChanged); connect(mSearchbar, &Searchbar::triggerNewSearch, this, &ContextManagerWidget::updateSearchIcon); connect(mSearchbar, &Searchbar::triggerNewSearch, mContextTableProxyModel, &ContextTableProxyModel::startSearch); @@ -122,45 +127,6 @@ namespace hal connect(qApp, &QApplication::focusChanged, this, &ContextManagerWidget::handleDeleteShortcutOnFocusChanged); } - - void ContextManagerWidget::handleCreateClicked(const QPoint& point) - { - QModelIndex index = mContextTreeView->indexAt(point); - - if (!index.isValid()) - return; - - QMenu context_menu; - - QAction create_context; - QAction create_directory; - - create_context.setText("Create new view"); - create_context.setParent(&context_menu); - - create_directory.setText("Create new directory"); - create_directory.setParent(&context_menu); - - context_menu.addAction(&create_context); - context_menu.addAction(&create_directory); - - QAction* clicked = context_menu.exec(mContextTreeView->viewport()->mapToGlobal(point)); - - if (!clicked) - return; - - if (clicked == &create_context) - { - - } - - if (clicked == &create_directory) - { - - } - - } - void ContextManagerWidget::handleCreateContextClicked() { UserActionCompound* act = new UserActionCompound; @@ -171,9 +137,24 @@ namespace hal act->exec(); } + void ContextManagerWidget::handleCreateDirectoryClicked() + { + bool confirm; + QString newName = QInputDialog::getText(this, "Directory name", "name:", QLineEdit::Normal, "", &confirm); + + if (confirm && !newName.isEmpty()) + { + mContextTreeModel->addDirectory(newName); + } + + } + void ContextManagerWidget::handleOpenContextClicked() { GraphContext* clicked_context = getCurrentContext(); + + if (!clicked_context) return; + mTabView->showContext(clicked_context); } @@ -181,6 +162,8 @@ namespace hal { GraphContext* clicked_context = getCurrentContext(); + if (!clicked_context) return; + QStringList used_context_names; for (const auto& context : gGraphContextManager->getContexts()) used_context_names.append(context->name()); @@ -207,6 +190,9 @@ namespace hal void ContextManagerWidget::handleDuplicateContextClicked() { GraphContext* clicked_context = getCurrentContext(); + + if (!clicked_context) return; + UserActionCompound* act = new UserActionCompound; act->setUseCreatedObject(); act->addAction(new ActionCreateObject(UserActionObjectType::Context,clicked_context->name() + " (Copy)")); @@ -218,7 +204,11 @@ namespace hal { QModelIndex current = mContextTreeView->currentIndex(); if (!current.isValid()) return; + GraphContext* clicked_context = getCurrentContext(); + + if (!clicked_context) return; + ActionDeleteObject* act = new ActionDeleteObject; act->setObject(UserActionObject(clicked_context->id(),UserActionObjectType::Context)); act->exec(); @@ -241,6 +231,14 @@ namespace hal QMenu context_menu; context_menu.addAction(mNewViewAction); + context_menu.addAction(mNewDirectoryAction); + + GraphContext* clicked_context = getCurrentContext(); + + if (!clicked_context) { + context_menu.exec(mContextTreeView->viewport()->mapToGlobal(point)); + return; + } if (clicked_index.isValid()) { @@ -268,7 +266,7 @@ namespace hal void ContextManagerWidget::selectViewContext(GraphContext* context) { - const QModelIndex source_model_index = mContextTableModel->getIndexFromContext(context); + const QModelIndex source_model_index = mContextTreeModel->getIndexFromContext(context); const QModelIndex proxy_model_index = mContextTableProxyModel->mapFromSource(source_model_index); if(proxy_model_index.isValid()) @@ -282,11 +280,12 @@ namespace hal QModelIndex proxy_model_index = mContextTreeView->currentIndex(); QModelIndex source_model_index = mContextTableProxyModel->mapToSource(proxy_model_index); - return mContextTableModel->getContext(source_model_index); + return mContextTreeModel->getContext(source_model_index); } void ContextManagerWidget::setupToolbar(Toolbar* toolbar) { + toolbar->addAction(mNewDirectoryAction); toolbar->addAction(mNewViewAction); toolbar->addAction(mOpenAction); toolbar->addAction(mDuplicateAction); diff --git a/plugins/gui/src/context_manager_widget/models/context_table_model.cpp b/plugins/gui/src/context_manager_widget/models/context_table_model.cpp index 49ded93c26a..c4e5f89b08c 100644 --- a/plugins/gui/src/context_manager_widget/models/context_table_model.cpp +++ b/plugins/gui/src/context_manager_widget/models/context_table_model.cpp @@ -9,29 +9,50 @@ namespace hal ContextTreeItem::ContextTreeItem(GraphContext *context) : BaseTreeItem(), - mContext(context) + mContext(context), + mDirectory(nullptr) { } ContextTreeItem::ContextTreeItem(ContextDirectory *directory) : BaseTreeItem(), - mDirectory(directory) + mDirectory(directory), + mContext(nullptr) { } QVariant ContextTreeItem::getData(int column) const { - switch(column) + if(isDirectory()) { - case 0: + switch(column) { - return mContext->getNameWithDirtyState(); - break; + case 0: + { + return mDirectory->getName(); + break; + } + case 1: + { + return ""; + break; + } } - case 1: + } + if(isContext()) + { + switch(column) { - return mContext->getTimestamp().toString(Qt::SystemLocaleShortDate); - break; + case 0: + { + return mContext->getNameWithDirtyState(); + break; + } + case 1: + { + return mContext->getTimestamp().toString(Qt::SystemLocaleShortDate); + break; + } } } } @@ -78,14 +99,6 @@ namespace hal setHeaderLabels(QStringList() << "View Name" << "Timestamp"); } - /*int ContextTreeModel::rowCount(const QModelIndex& parent) const - { - Q_UNUSED(parent) - - return mContextMap.size(); - }*/ - - QVariant ContextTreeModel::data(const QModelIndex& index, int role) const { if (!index.isValid()) @@ -98,12 +111,14 @@ namespace hal if(role == Qt::DisplayRole) { + switch(index.column()) { case 0: return item->getData(0); break; case 1: return item->getData(1); break; default: return QVariant(); } + } return QVariant(); @@ -201,26 +216,11 @@ namespace hal return getIndexFromItem(mContextMap.find(context)->second); } - /*void ContextTableModel::handleDataChanged() - { - GraphContext* gc = static_cast(sender()); - if (!gc) return; - for (int irow = 0; irow < mContextList.size(); ++irow) - { - if (gc == mContextList.at(irow)) - { - QModelIndex inx0 = index(irow,0); - QModelIndex inx1 = index(irow,1); - Q_EMIT dataChanged(inx0,inx1); - return; - } - } - }*/ - GraphContext* ContextTreeModel::getContext(const QModelIndex& index) const { BaseTreeItem* item = getItemFromIndex(index); + if (static_cast(item)->isDirectory()) return nullptr; GraphContext* context; for (auto &i : mContextMap) { if (i.second == item) { @@ -231,27 +231,6 @@ namespace hal return context; } - /* QModelIndex ContextTableModel::getIndex(const BaseTreeItem* const item) const - { - assert(item); - - QVector row_numbers; - const BaseTreeItem* current_item = item; - - while (current_item != mRootItem) - { - row_numbers.append(static_cast(current_item)->row()); - current_item = current_item->getParent(); - } - - QModelIndex model_index = index(0, 0, QModelIndex()); - - for (QVector::const_reverse_iterator i = row_numbers.crbegin(); i != row_numbers.crend(); ++i) - model_index = index(*i, 0, model_index); - - return model_index; - }*/ - const QVector &ContextTreeModel::list() { mContextList.clear();