Commit 6c4221b5 authored by Lefebvre, Jordan's avatar Lefebvre, Jordan
Browse files

Adding NavigationActionManager to coordinate NavigationItem's context menu actions.

parent b9490705
Pipeline #14525 passed with stages
in 7 minutes and 30 seconds
......@@ -45,12 +45,14 @@ ELSE()
SET(HEADERS
${HEADERS}
numberpadwidget.hh
navigationactionmanager.hh
navigationmodel.hh
navigationwidget.hh
)
SET(SOURCES
${SOURCES}
numberpadwidget.cc
navigationactionmanager.cc
navigationitem.cc
navigationmodel.cc
navigationwidget.cc
......
......@@ -6,10 +6,14 @@
*/
#include "radixnavigationwidget.hh"
#include <iostream>
#include <QApplication>
#include <QGridLayout>
#include <QLabel>
#include <limits>
#include "radixwidgets/navigationactionmanager.hh"
#include "radixwidgets/navigationitem.hh"
#include "radixwidgets/navigationmodel.hh"
#include "radixwidgets/navigationwidget.hh"
......@@ -27,6 +31,17 @@ MainWindow::MainWindow(QWidget* parent)
auto hello = model->addItem("Hello", root);
auto world = model->addItem("World", root);
auto hidden_beneath = model->addItem("Hidden beneath", world);
NavigationActionManager* actions = mWidget->navigationActionManager();
actions->registerAction(hello, "Hello Function 1", []() {
std::cout << "Hello function 1 executed" << std::endl;
});
actions->registerAction(hidden_beneath, "Hidden Function", []() {
std::cout << "Hidden function executed" << std::endl;
});
actions->registerAction(hidden_beneath, "Hidden Function 2", []() {
std::cout << "Hidden function2 executed" << std::endl;
});
setCentralWidget(mWidget);
}
......
#include "radixwidgets/navigationactionmanager.hh"
#include <QHash>
#include <QMenu>
#include "radixbug/bug.hh"
namespace radix
{
class NavigationActionManager::PImpl
{
public:
QHash<NavigationItem *, QList<std::pair<QString, std::function<void()>>>>
item_functions;
~PImpl();
};
NavigationActionManager::PImpl::~PImpl() {}
NavigationActionManager::NavigationActionManager(QObject *parent)
: p(new PImpl(), [](PImpl *impl) { delete impl; })
{
}
void NavigationActionManager::registerAction(NavigationItem *item, QString text,
std::function<void()> functor)
{
auto item_it = p->item_functions.find(item);
if (item_it == p->item_functions.end())
{
// create a new reference
p->item_functions.insert(item, {std::make_pair(text, functor)});
}
else
{
item_it.value().append(std::make_pair(text, functor));
}
}
void NavigationActionManager::itemActions(NavigationItem *item,
QMenu *menu) const
{
//
// Get the list of functors
auto functor_it = p->item_functions.find(item);
if (functor_it == p->item_functions.end()) return;
auto &list = functor_it.value();
for (int i = 0; i < list.length(); ++i)
{
menu->addAction(list[i].first, list[i].second);
}
}
} // namespace radix
#ifndef RADIX_RADIXWIDGETS_NAVIGATIONACTIONMANAGER_HH_
#define RADIX_RADIXWIDGETS_NAVIGATIONACTIONMANAGER_HH_
#include <QAction>
#include <QList>
#include <QObject>
#include <functional>
#include <memory>
#include "radixwidgets/navigationitem.hh"
namespace radix
{
class NavigationActionManager : public QObject
{
Q_OBJECT
public:
NavigationActionManager(QObject *parent = nullptr);
void registerAction(NavigationItem *item, QString text,
std::function<void()> functor);
void itemActions(NavigationItem *item, QMenu *menu) const;
private:
class PImpl;
std::unique_ptr<PImpl, void (*)(PImpl *)> p;
};
} // namespace radix
#endif /** RADIX_RADIXWIDGETS_NAVIGATIONACTIONMANAGER_HH_ */
......@@ -12,14 +12,11 @@ class NavigationItem::PImpl
QList<NavigationItem *> children;
QList<QVariant> data;
bool checked = false;
int type = 0;
~PImpl();
};
NavigationItem::PImpl::~PImpl()
{
qDeleteAll(children);
if (parent) delete parent;
}
NavigationItem::PImpl::~PImpl() { qDeleteAll(children); }
NavigationItem::NavigationItem(QVariant data, NavigationItem *parentItem)
: p(new PImpl(), [](PImpl *impl) { delete impl; })
......@@ -41,6 +38,10 @@ NavigationItem::NavigationItem(NavigationItem *parentItem)
p->parent = parentItem;
}
int NavigationItem::type() const { return p->type; }
void NavigationItem::setType(int type) { p->type = type; }
void NavigationItem::addChild(NavigationItem *child)
{
p->children.append(child);
......
......@@ -13,6 +13,8 @@ class NavigationItem
NavigationItem(QList<QVariant> data, NavigationItem *parentItem = nullptr);
NavigationItem(NavigationItem *parentItem = nullptr);
int type() const;
void setType(int type);
void addChild(NavigationItem *child);
NavigationItem *child(int row);
int childCount() const;
......
#include "radixwidgets/navigationwidget.hh"
#include "radixwidgets/navigationactionmanager.hh"
#include "radixwidgets/navigationitem.hh"
#include "radixwidgets/navigationmodel.hh"
......@@ -77,6 +78,7 @@ class NavigationWidget::PImpl
public:
QLineEdit* filter;
NavigationModel* model;
NavigationActionManager* action_manager;
NavigationItemSortFilterProxyModel* proxy;
QTreeView* view;
};
......@@ -91,11 +93,20 @@ NavigationWidget::NavigationWidget(QWidget* parent)
NavigationModel* NavigationWidget::navigationModel() { return p->model; }
NavigationActionManager* NavigationWidget::navigationActionManager()
{
return p->action_manager;
}
void NavigationWidget::contextMenuRequested(QPoint point)
{
radix_tagged_line("contextMenuRequested()");
QModelIndex index = p->view->indexAt(point);
QModelIndex pindex = p->view->indexAt(point);
QModelIndex index = p->proxy->mapToSource(pindex);
NavigationItem* item = static_cast<NavigationItem*>(index.internalPointer());
QMenu m;
p->action_manager->itemActions(item, &m);
m.exec(p->view->viewport()->mapToGlobal(point));
}
......@@ -110,10 +121,11 @@ void NavigationWidget::initLayout()
void NavigationWidget::initMembers()
{
// allocate
p->filter = new QLineEdit();
p->model = new NavigationModel();
p->proxy = new NavigationItemSortFilterProxyModel();
p->view = new QTreeView();
p->filter = new QLineEdit(this);
p->model = new NavigationModel(this);
p->proxy = new NavigationItemSortFilterProxyModel(this);
p->view = new QTreeView(this);
p->action_manager = new NavigationActionManager(this);
// init properties
p->filter->setPlaceholderText("Filter");
......@@ -145,8 +157,6 @@ void NavigationWidget::initMembers()
// connect signals/slots
connect(p->filter, SIGNAL(textChanged(QString)), this,
SLOT(filterModel(QString)));
connect(p->view, SIGNAL(doubleClicked(QModelIndex)), this,
SLOT(itemDoubleClicked(QModelIndex)));
connect(p->view, SIGNAL(customContextMenuRequested(QPoint)), this,
SLOT(contextMenuRequested(QPoint)));
}
......@@ -175,10 +185,4 @@ void NavigationWidget::filterModel(const QString& pattern)
}
}
void NavigationWidget::itemDoubleClicked(QModelIndex index)
{
radix_tagged_line("itemDoubleClicked()");
const auto& mapped = p->proxy->mapToSource(index);
radix_tagged_line("\t[" << mapped.row() << "," << mapped.column() << "]");
}
} // namespace radix
......@@ -18,6 +18,7 @@ namespace radix
// Forward declaration
class NavigationItem;
class NavigationModel;
class NavigationActionManager;
class NavigationItemSortFilterProxyModel : public QSortFilterProxyModel
{
......@@ -49,15 +50,17 @@ class NavigationWidget : public QWidget
void expand(QStandardItem* item);
NavigationModel* navigationModel();
NavigationActionManager* navigationActionManager();
QTreeView* navigationTree() const;
private slots:
void contextMenuRequested(QPoint);
public slots:
void filterModel(const QString& pattern);
void itemDoubleClicked(QModelIndex index);
signals:
void itemDoubleClicked(const QModelIndex& index);
void itemClicked(const QModelIndex& index);
void itemChecked(const QModelIndex& index);
void itemUnchecked(const QModelIndex& index);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment