Commit b9490705 authored by Lefebvre, Jordan's avatar Lefebvre, Jordan
Browse files

Reworked navigationmodel for ease of use with adding children. Still need action integration.

parent aa008605
Pipeline #14513 passed with stages
in 7 minutes and 33 seconds
...@@ -10,15 +10,23 @@ ...@@ -10,15 +10,23 @@
#include <QGridLayout> #include <QGridLayout>
#include <QLabel> #include <QLabel>
#include <limits> #include <limits>
#include "radixwidgets/navigationitem.hh"
#include "radixwidgets/navigationmodel.hh"
#include "radixwidgets/navigationwidget.hh" #include "radixwidgets/navigationwidget.hh"
using namespace radix;
MainWindow::MainWindow(QWidget *parent) MainWindow::MainWindow(QWidget* parent)
: QMainWindow(parent) : QMainWindow(parent)
{ {
setGeometry(400, 250, 542, 390); setGeometry(400, 250, 542, 390);
mWidget = new radix::NavigationWidget(this); mWidget = new NavigationWidget(this);
NavigationModel* model = mWidget->navigationModel();
NavigationItem* root = model->rootItem();
auto hello = model->addItem("Hello", root);
auto world = model->addItem("World", root);
auto hidden_beneath = model->addItem("Hidden beneath", world);
setCentralWidget(mWidget); setCentralWidget(mWidget);
} }
...@@ -28,7 +36,7 @@ MainWindow::~MainWindow() {} ...@@ -28,7 +36,7 @@ MainWindow::~MainWindow() {}
/******************************** MAIN PROGRAM ********************************/ /******************************** MAIN PROGRAM ********************************/
/******************************************************************************/ /******************************************************************************/
int main(int argc, char **argv) int main(int argc, char** argv)
{ {
QApplication app(argc, argv); QApplication app(argc, argv);
MainWindow mainWindow; MainWindow mainWindow;
......
...@@ -48,7 +48,16 @@ void NavigationItem::addChild(NavigationItem *child) ...@@ -48,7 +48,16 @@ void NavigationItem::addChild(NavigationItem *child)
int NavigationItem::childCount() const { return p->children.size(); } int NavigationItem::childCount() const { return p->children.size(); }
int NavigationItem::columnCount() const { return p->data.size(); } int NavigationItem::columnCount() const
{
// recursively find the largest number of columns from children
int count = p->data.size();
for (int ci = 0; ci < p->children.size(); ++ci)
{
count = std::max(count, p->children[ci]->columnCount());
}
return count;
}
QVariant NavigationItem::data(int column) const QVariant NavigationItem::data(int column) const
{ {
......
...@@ -23,18 +23,11 @@ NavigationModel::NavigationModel(QObject* parent) ...@@ -23,18 +23,11 @@ NavigationModel::NavigationModel(QObject* parent)
: QAbstractItemModel(parent) : QAbstractItemModel(parent)
, p(new PImpl(), [](PImpl* impl) { delete impl; }) , p(new PImpl(), [](PImpl* impl) { delete impl; })
{ {
radix_tagged_line("Navigation Model"); p->root = new NavigationItem();
p->root = new NavigationItem({"Header 1", "Header 2"});
p->root->addChild(new NavigationItem("Hello", p->root));
NavigationItem* world = new NavigationItem("World", p->root);
p->root->addChild(world);
NavigationItem* child = new NavigationItem("My name is edward", world);
world->addChild(child);
} }
QVariant NavigationModel::data(const QModelIndex& index, int role) const QVariant NavigationModel::data(const QModelIndex& index, int role) const
{ {
radix_tagged_line("data(" << index.row() << ", " << index.column() << ")");
if (!index.isValid()) return QVariant(); if (!index.isValid()) return QVariant();
NavigationItem* item = static_cast<NavigationItem*>(index.internalPointer()); NavigationItem* item = static_cast<NavigationItem*>(index.internalPointer());
...@@ -86,7 +79,6 @@ QVariant NavigationModel::headerData(int section, Qt::Orientation orientation, ...@@ -86,7 +79,6 @@ QVariant NavigationModel::headerData(int section, Qt::Orientation orientation,
QModelIndex NavigationModel::index(int row, int column, QModelIndex NavigationModel::index(int row, int column,
const QModelIndex& parent) const const QModelIndex& parent) const
{ {
radix_tagged_line("index(" << row << "," << column << ")");
if (!hasIndex(row, column, parent)) return QModelIndex(); if (!hasIndex(row, column, parent)) return QModelIndex();
NavigationItem* parentItem; NavigationItem* parentItem;
...@@ -105,7 +97,6 @@ QModelIndex NavigationModel::index(int row, int column, ...@@ -105,7 +97,6 @@ QModelIndex NavigationModel::index(int row, int column,
QModelIndex NavigationModel::parent(const QModelIndex& index) const QModelIndex NavigationModel::parent(const QModelIndex& index) const
{ {
radix_tagged_line("parent(" << index.row() << "," << index.column() << ")");
if (!index.isValid()) return QModelIndex(); if (!index.isValid()) return QModelIndex();
NavigationItem* childItem = NavigationItem* childItem =
...@@ -119,8 +110,6 @@ QModelIndex NavigationModel::parent(const QModelIndex& index) const ...@@ -119,8 +110,6 @@ QModelIndex NavigationModel::parent(const QModelIndex& index) const
int NavigationModel::rowCount(const QModelIndex& parent) const int NavigationModel::rowCount(const QModelIndex& parent) const
{ {
radix_tagged_line("rowCount(" << parent.row() << "," << parent.column()
<< ")");
NavigationItem* parentItem; NavigationItem* parentItem;
if (parent.column() > 0) return 0; if (parent.column() > 0) return 0;
...@@ -134,8 +123,6 @@ int NavigationModel::rowCount(const QModelIndex& parent) const ...@@ -134,8 +123,6 @@ int NavigationModel::rowCount(const QModelIndex& parent) const
int NavigationModel::columnCount(const QModelIndex& parent) const int NavigationModel::columnCount(const QModelIndex& parent) const
{ {
radix_tagged_line("columnCount(" << parent.row() << "," << parent.column()
<< ")");
if (parent.isValid()) if (parent.isValid())
return static_cast<NavigationItem*>(parent.internalPointer()) return static_cast<NavigationItem*>(parent.internalPointer())
->columnCount(); ->columnCount();
...@@ -143,4 +130,15 @@ int NavigationModel::columnCount(const QModelIndex& parent) const ...@@ -143,4 +130,15 @@ int NavigationModel::columnCount(const QModelIndex& parent) const
return p->root->columnCount(); return p->root->columnCount();
} }
NavigationItem* NavigationModel::rootItem() { return p->root; }
NavigationItem* NavigationModel::addItem(QVariant value, NavigationItem* parent)
{
beginResetModel();
NavigationItem* newItem = new NavigationItem(value, parent);
parent->addChild(newItem);
endResetModel();
return newItem;
}
} // namespace radix } // namespace radix
...@@ -7,6 +7,9 @@ ...@@ -7,6 +7,9 @@
namespace radix namespace radix
{ {
// Forward declaration
class NavigationItem;
class NavigationModel : public QAbstractItemModel class NavigationModel : public QAbstractItemModel
{ {
Q_OBJECT Q_OBJECT
...@@ -24,6 +27,8 @@ class NavigationModel : public QAbstractItemModel ...@@ -24,6 +27,8 @@ class NavigationModel : public QAbstractItemModel
QModelIndex parent(const QModelIndex &index) const override; QModelIndex parent(const QModelIndex &index) const override;
int rowCount(const QModelIndex &parent = QModelIndex()) const override; int rowCount(const QModelIndex &parent = QModelIndex()) const override;
int columnCount(const QModelIndex &parent = QModelIndex()) const override; int columnCount(const QModelIndex &parent = QModelIndex()) const override;
NavigationItem *rootItem();
NavigationItem *addItem(QVariant value, NavigationItem *parent);
signals: signals:
void itemChecked(const QModelIndex &index); void itemChecked(const QModelIndex &index);
......
...@@ -89,6 +89,16 @@ NavigationWidget::NavigationWidget(QWidget* parent) ...@@ -89,6 +89,16 @@ NavigationWidget::NavigationWidget(QWidget* parent)
initLayout(); initLayout();
} }
NavigationModel* NavigationWidget::navigationModel() { return p->model; }
void NavigationWidget::contextMenuRequested(QPoint point)
{
radix_tagged_line("contextMenuRequested()");
QModelIndex index = p->view->indexAt(point);
QMenu m;
m.exec(p->view->viewport()->mapToGlobal(point));
}
void NavigationWidget::initLayout() void NavigationWidget::initLayout()
{ {
auto vlayout = new QVBoxLayout(this); auto vlayout = new QVBoxLayout(this);
...@@ -137,6 +147,8 @@ void NavigationWidget::initMembers() ...@@ -137,6 +147,8 @@ void NavigationWidget::initMembers()
SLOT(filterModel(QString))); SLOT(filterModel(QString)));
connect(p->view, SIGNAL(doubleClicked(QModelIndex)), this, connect(p->view, SIGNAL(doubleClicked(QModelIndex)), this,
SLOT(itemDoubleClicked(QModelIndex))); SLOT(itemDoubleClicked(QModelIndex)));
connect(p->view, SIGNAL(customContextMenuRequested(QPoint)), this,
SLOT(contextMenuRequested(QPoint)));
} }
void NavigationWidget::filterModel(const QString& pattern) void NavigationWidget::filterModel(const QString& pattern)
{ {
......
...@@ -15,7 +15,9 @@ QT_END_NAMESPACE ...@@ -15,7 +15,9 @@ QT_END_NAMESPACE
namespace radix namespace radix
{ {
// Forward declaration
class NavigationItem; class NavigationItem;
class NavigationModel;
class NavigationItemSortFilterProxyModel : public QSortFilterProxyModel class NavigationItemSortFilterProxyModel : public QSortFilterProxyModel
{ {
...@@ -46,16 +48,18 @@ class NavigationWidget : public QWidget ...@@ -46,16 +48,18 @@ class NavigationWidget : public QWidget
void collapse(QStandardItem* item); void collapse(QStandardItem* item);
void expand(QStandardItem* item); void expand(QStandardItem* item);
NavigationModel* navigationModel();
QTreeView* navigationTree() const; QTreeView* navigationTree() const;
private slots:
void contextMenuRequested(QPoint);
public slots: public slots:
void filterModel(const QString& pattern); void filterModel(const QString& pattern);
void itemDoubleClicked(QModelIndex index); void itemDoubleClicked(QModelIndex index);
signals: signals:
void itemChecked(const QModelIndex& index);
void itemActivated(NavigationItem* item); void itemUnchecked(const QModelIndex& index);
private: private:
void initLayout(); void initLayout();
......
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