Skip to content

Commit

Permalink
fix crash on wrongful click; add naming for directory
Browse files Browse the repository at this point in the history
  • Loading branch information
ailujezi committed Dec 17, 2023
1 parent b2858e3 commit 70c6378
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 103 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ namespace hal

public:
ContextDirectory(u32 id, QString name):mId(id), mName(name){}
QString getName() { return mName; }

};

Expand Down
93 changes: 46 additions & 47 deletions plugins/gui/src/context_manager_widget/context_manager_widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,12 @@
#include "gui/user_action/user_action_compound.h"
#include <QShortcut>
#include <QApplication>
#include <QInputDialog>

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
Expand All @@ -46,20 +47,23 @@ 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");
mSearchAction->setToolTip("Search");

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");
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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;
Expand All @@ -171,16 +137,33 @@ 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);
}

void ContextManagerWidget::handleRenameContextClicked()
{
GraphContext* clicked_context = getCurrentContext();

if (!clicked_context) return;

QStringList used_context_names;
for (const auto& context : gGraphContextManager->getContexts())
used_context_names.append(context->name());
Expand All @@ -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)"));
Expand All @@ -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();
Expand All @@ -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())
{
Expand Down Expand Up @@ -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())
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
}
Expand Down Expand Up @@ -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())
Expand All @@ -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();
Expand Down Expand Up @@ -201,26 +216,11 @@ namespace hal
return getIndexFromItem(mContextMap.find(context)->second);
}

/*void ContextTableModel::handleDataChanged()
{
GraphContext* gc = static_cast<GraphContext*>(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<ContextTreeItem*>(item)->isDirectory()) return nullptr;
GraphContext* context;
for (auto &i : mContextMap) {
if (i.second == item) {
Expand All @@ -231,27 +231,6 @@ namespace hal
return context;
}

/* QModelIndex ContextTableModel::getIndex(const BaseTreeItem* const item) const
{
assert(item);
QVector<int> row_numbers;
const BaseTreeItem* current_item = item;
while (current_item != mRootItem)
{
row_numbers.append(static_cast<const ContextTreeItem*>(current_item)->row());
current_item = current_item->getParent();
}
QModelIndex model_index = index(0, 0, QModelIndex());
for (QVector<int>::const_reverse_iterator i = row_numbers.crbegin(); i != row_numbers.crend(); ++i)
model_index = index(*i, 0, model_index);
return model_index;
}*/

const QVector<GraphContext *> &ContextTreeModel::list()
{
mContextList.clear();
Expand Down

0 comments on commit 70c6378

Please sign in to comment.