Skip to content

Commit

Permalink
Make ContextTableModel migrate to a BaseTreeModel
Browse files Browse the repository at this point in the history
  • Loading branch information
ailujezi committed Dec 11, 2023
1 parent 58f4315 commit b2858e3
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
#include <QTableView>
#include <QPushButton>
#include <QMenu>
#include <QTreeView>



namespace hal
Expand Down Expand Up @@ -84,7 +86,7 @@ namespace hal
ContextManagerWidget(GraphTabWidget* tab_view, QWidget* parent = nullptr);

/**
* Selects the given context if possible (if it is indeed in the widget's ContextTableModel).
* Selects the given context if possible (if it is indeed in the widget's ContextTreeModel).
*
* @param context - The context to select.
*/
Expand Down Expand Up @@ -175,7 +177,7 @@ namespace hal
private:
GraphTabWidget* mTabView;

QTableView* mContextTableView;
QTreeView* mContextTreeView;
ContextTreeModel* mContextTableModel;
ContextTableProxyModel* mContextTableProxyModel;

Expand Down Expand Up @@ -209,6 +211,7 @@ namespace hal

QShortcut* mShortCutDeleteItem;

void handleCreateClicked(const QPoint& point);
void handleCreateContextClicked();
void handleRenameContextClicked();
void handleDuplicateContextClicked();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,15 @@ namespace hal
public:

ContextTreeItem(GraphContext* context);
ContextTreeItem(ContextDirectory* directory);
QVariant getData(int column) const override;
void setData(QList<QVariant> data) override;
void setDataAtIndex(int index, QVariant& data) override;
void appendData(QVariant data) override;
int getColumnCount() const override;
int row() const;
bool isDirectory() const;
bool isContext() const;
};

/**
Expand Down Expand Up @@ -91,6 +93,14 @@ namespace hal
QVariant data(const QModelIndex& inddex, int role = Qt::DisplayRole) const override;
///@}

/**
* Adds a directory to the model.
*
* @param name - The name to the directory.
* @param parent - The Parent of the directory.
*/
void addDirectory(QString name, BaseTreeItem* parent = nullptr);

/**
* Adds a given GraphContext to the model.
*
Expand Down Expand Up @@ -120,15 +130,7 @@ namespace hal
* @param context - The context to convert.
* @return The resulting index.
*/
QModelIndex getIndex(GraphContext* context) const;

/**
* Returns the index where the specified ContextTreeitem can be found.
*
* @param item - The ContextTreeitem to search for in the item model
* @returns the model index of the specified ContextTreeitem
*/
//QModelIndex getIndex(const BaseTreeItem * const item) const;
QModelIndex getIndexFromContext(GraphContext* context) const;

/**
* Resets the model (removes all GraphContext%s).
Expand All @@ -140,12 +142,14 @@ namespace hal
*
* @return A vector of all GraphContext%s.
*/
const QVector<GraphContext*>& list() const;
const QVector<GraphContext*>& list();
private Q_SLOTS:
void handleDataChanged();

private:
ContextTreeItem *mCurrentDirectory;
std::map<GraphContext *, ContextTreeItem *> mContextMap;
QVector<GraphContext*> mContextList;
u32 mMinDirectoryId;
};
} // namespace hal
85 changes: 62 additions & 23 deletions plugins/gui/src/context_manager_widget/context_manager_widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,37 +76,37 @@ namespace hal
mContextTableProxyModel->setSourceModel(mContextTableModel);
mContextTableProxyModel->setSortRole(Qt::UserRole);

mContextTableView = new QTableView(this);
mContextTableView->setModel(mContextTableProxyModel);
mContextTableView->setSortingEnabled(true);
mContextTableView->setSelectionBehavior(QAbstractItemView::SelectRows);
mContextTableView->setSelectionMode(QAbstractItemView::SingleSelection); // ERROR ???
mContextTableView->setContextMenuPolicy(Qt::ContextMenuPolicy::CustomContextMenu);
mContextTableView->sortByColumn(1, Qt::SortOrder::DescendingOrder);
mContextTableView->verticalHeader()->hide();
mContextTableView->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
mContextTreeView = new QTreeView(this);
mContextTreeView->setModel(mContextTableProxyModel);
mContextTreeView->setSortingEnabled(true);
mContextTreeView->setSelectionBehavior(QAbstractItemView::SelectRows);
mContextTreeView->setSelectionMode(QAbstractItemView::SingleSelection); // ERROR ???
mContextTreeView->setContextMenuPolicy(Qt::ContextMenuPolicy::CustomContextMenu);
mContextTreeView->sortByColumn(1, Qt::SortOrder::DescendingOrder);
//mContextTreeView->verticalHeader()->hide();
mContextTreeView->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

mContextTableView->horizontalHeader()->setSectionResizeMode(0, QHeaderView::Stretch);
mContextTableView->horizontalHeader()->setSectionResizeMode(1, QHeaderView::ResizeToContents);
//mContextTreeView->horizontalHeader()->setSectionResizeMode(0, QHeaderView::Stretch);
//mContextTreeView->horizontalHeader()->setSectionResizeMode(1, QHeaderView::ResizeToContents);


mContentLayout->addWidget(mContextTableView);
mContentLayout->addWidget(mContextTreeView);
mContentLayout->addWidget(mSearchbar);

mSearchbar->hide();
mSearchbar->setColumnNames(mContextTableProxyModel->getColumnNames());
enableSearchbar(mContextTableProxyModel->rowCount() > 0);

connect(mOpenAction, &QAction::triggered, this, &ContextManagerWidget::handleOpenContextClicked);
connect(mOpenAction, &QAction::triggered, this, &ContextManagerWidget::handleOpenContextClicked);
connect(mNewViewAction, &QAction::triggered, this, &ContextManagerWidget::handleCreateContextClicked);
connect(mRenameAction, &QAction::triggered, this, &ContextManagerWidget::handleRenameContextClicked);
connect(mDuplicateAction, &QAction::triggered, this, &ContextManagerWidget::handleDuplicateContextClicked);
connect(mDeleteAction, &QAction::triggered, this, &ContextManagerWidget::handleDeleteContextClicked);
connect(mSearchAction, &QAction::triggered, this, &ContextManagerWidget::toggleSearchbar);

connect(mContextTableView, &QTableView::customContextMenuRequested, this, &ContextManagerWidget::handleContextMenuRequest);
connect(mContextTableView, &QTableView::doubleClicked, this, &ContextManagerWidget::handleOpenContextClicked);
connect(mContextTableView->selectionModel(), &QItemSelectionModel::selectionChanged, this, &ContextManagerWidget::handleSelectionChanged);
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);

Expand All @@ -122,6 +122,45 @@ 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 Down Expand Up @@ -177,7 +216,7 @@ namespace hal

void ContextManagerWidget::handleDeleteContextClicked()
{
QModelIndex current = mContextTableView->currentIndex();
QModelIndex current = mContextTreeView->currentIndex();
if (!current.isValid()) return;
GraphContext* clicked_context = getCurrentContext();
ActionDeleteObject* act = new ActionDeleteObject;
Expand All @@ -197,7 +236,7 @@ namespace hal

void ContextManagerWidget::handleContextMenuRequest(const QPoint& point)
{
const QModelIndex clicked_index = mContextTableView->indexAt(point);
const QModelIndex clicked_index = mContextTreeView->indexAt(point);

QMenu context_menu;

Expand All @@ -211,7 +250,7 @@ namespace hal
context_menu.addAction(mDeleteAction);
}

context_menu.exec(mContextTableView->viewport()->mapToGlobal(point));
context_menu.exec(mContextTreeView->viewport()->mapToGlobal(point));
}

void ContextManagerWidget::handleDataChanged()
Expand All @@ -229,18 +268,18 @@ namespace hal

void ContextManagerWidget::selectViewContext(GraphContext* context)
{
const QModelIndex source_model_index = mContextTableModel->getIndex(context);
const QModelIndex source_model_index = mContextTableModel->getIndexFromContext(context);
const QModelIndex proxy_model_index = mContextTableProxyModel->mapFromSource(source_model_index);

if(proxy_model_index.isValid())
mContextTableView->setCurrentIndex(proxy_model_index);
mContextTreeView->setCurrentIndex(proxy_model_index);
else
mContextTableView->clearSelection();
mContextTreeView->clearSelection();
}

GraphContext* ContextManagerWidget::getCurrentContext()
{
QModelIndex proxy_model_index = mContextTableView->currentIndex();
QModelIndex proxy_model_index = mContextTreeView->currentIndex();
QModelIndex source_model_index = mContextTableProxyModel->mapToSource(proxy_model_index);

return mContextTableModel->getContext(source_model_index);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#include "gui/gui_globals.h"

#include <limits>

namespace hal
{

Expand All @@ -11,6 +13,12 @@ namespace hal
{
}

ContextTreeItem::ContextTreeItem(ContextDirectory *directory) :
BaseTreeItem(),
mDirectory(directory)
{
}

QVariant ContextTreeItem::getData(int column) const
{
switch(column)
Expand Down Expand Up @@ -60,7 +68,12 @@ namespace hal
return mDirectory != nullptr;
}

ContextTreeModel::ContextTreeModel(QObject* parent) : BaseTreeModel(parent), mCurrentDirectory(nullptr)
bool ContextTreeItem::isContext() const
{
return mContext != nullptr;
}

ContextTreeModel::ContextTreeModel(QObject* parent) : BaseTreeModel(parent), mCurrentDirectory(nullptr), mMinDirectoryId(std::numeric_limits<u32>::max())
{
setHeaderLabels(QStringList() << "View Name" << "Timestamp");
}
Expand Down Expand Up @@ -96,6 +109,34 @@ namespace hal
return QVariant();
}

void ContextTreeModel::addDirectory(QString name, BaseTreeItem *parent)
{
ContextDirectory* directory = new ContextDirectory(--mMinDirectoryId, name);

ContextTreeItem* item = new ContextTreeItem(directory);

if (parent)
item->setParent(parent);
else if(mCurrentDirectory)
item->setParent(mCurrentDirectory);
else
item->setParent(mRootItem);


QModelIndex index = getIndexFromItem(item->getParent());

int row = item->getParent()->getChildCount();
beginInsertRows(index, row, row);
item->getParent()->appendChild(item);
endInsertRows();


//connect(context,&GraphContext::dataChanged,this,&ContextTableModel::handleDataChanged);
/*connect(context, &GraphContext::dataChanged, this, [item, this]() {
Q_EMIT dataChanged(getIndexFromItem(item), getIndexFromItem(item));
});*/
}

void ContextTreeModel::clear()
{
beginResetModel();
Expand All @@ -118,11 +159,11 @@ namespace hal
item->setParent(mRootItem);


QModelIndex index = getIndexFromItem(parent);
QModelIndex index = getIndexFromItem(item->getParent());

int row = parent->getChildCount();
int row = item->getParent()->getChildCount();
beginInsertRows(index, row, row);
parent->appendChild(item);
item->getParent()->appendChild(item);
endInsertRows();

mContextMap.insert({context, item});
Expand Down Expand Up @@ -151,8 +192,13 @@ namespace hal
delete item;

std::map<GraphContext *,ContextTreeItem *>::iterator it;
it = mContextMap.find (context);
mContextMap.erase (it);
it = mContextMap.find(context);
mContextMap.erase(it);
}

QModelIndex ContextTreeModel::getIndexFromContext(GraphContext *context) const
{
return getIndexFromItem(mContextMap.find(context)->second);
}

/*void ContextTableModel::handleDataChanged()
Expand All @@ -169,12 +215,21 @@ namespace hal
return;
}
}
}
}*/

GraphContext* ContextTableModel::getContext(const QModelIndex& index) const
GraphContext* ContextTreeModel::getContext(const QModelIndex& index) const
{
return (mContextList)[index.row()];
}*/
BaseTreeItem* item = getItemFromIndex(index);

GraphContext* context;
for (auto &i : mContextMap) {
if (i.second == item) {
context = i.first;
break;
}
}
return context;
}

/* QModelIndex ContextTableModel::getIndex(const BaseTreeItem* const item) const
{
Expand All @@ -197,12 +252,12 @@ namespace hal
return model_index;
}*/

const QVector<GraphContext *> &ContextTreeModel::list() const
const QVector<GraphContext *> &ContextTreeModel::list()
{
QVector<GraphContext *> key;
mContextList.clear();
for (auto it = mContextMap.begin(); it != mContextMap.end(); ++it) {
key.push_back(it->first);
mContextList.push_back(it->first);
}
return key;
return mContextList;
}
}

0 comments on commit b2858e3

Please sign in to comment.