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 @@
#include <QGridLayout>
#include <QLabel>
#include <limits>
#include "radixwidgets/navigationitem.hh"
#include "radixwidgets/navigationmodel.hh"
#include "radixwidgets/navigationwidget.hh"
using namespace radix;
MainWindow::MainWindow(QWidget *parent)
MainWindow::MainWindow(QWidget* parent)
: QMainWindow(parent)
{
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);
}
......@@ -28,7 +36,7 @@ MainWindow::~MainWindow() {}
/******************************** MAIN PROGRAM ********************************/
/******************************************************************************/
int main(int argc, char **argv)
int main(int argc, char** argv)
{
QApplication app(argc, argv);
MainWindow mainWindow;
......
......@@ -48,7 +48,16 @@ void NavigationItem::addChild(NavigationItem *child)
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
{
......
......@@ -23,18 +23,11 @@ NavigationModel::NavigationModel(QObject* parent)
: QAbstractItemModel(parent)
, p(new PImpl(), [](PImpl* impl) { delete impl; })
{
radix_tagged_line("Navigation Model");
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);
p->root = new NavigationItem();
}
QVariant NavigationModel::data(const QModelIndex& index, int role) const
{
radix_tagged_line("data(" << index.row() << ", " << index.column() << ")");
if (!index.isValid()) return QVariant();
NavigationItem* item = static_cast<NavigationItem*>(index.internalPointer());
......@@ -86,7 +79,6 @@ QVariant NavigationModel::headerData(int section, Qt::Orientation orientation,
QModelIndex NavigationModel::index(int row, int column,
const QModelIndex& parent) const
{
radix_tagged_line("index(" << row << "," << column << ")");
if (!hasIndex(row, column, parent)) return QModelIndex();
NavigationItem* parentItem;
......@@ -105,7 +97,6 @@ QModelIndex NavigationModel::index(int row, int column,
QModelIndex NavigationModel::parent(const QModelIndex& index) const
{
radix_tagged_line("parent(" << index.row() << "," << index.column() << ")");
if (!index.isValid()) return QModelIndex();
NavigationItem* childItem =
......@@ -119,8 +110,6 @@ QModelIndex NavigationModel::parent(const QModelIndex& index) const
int NavigationModel::rowCount(const QModelIndex& parent) const
{
radix_tagged_line("rowCount(" << parent.row() << "," << parent.column()
<< ")");
NavigationItem* parentItem;
if (parent.column() > 0) return 0;
......@@ -134,8 +123,6 @@ int NavigationModel::rowCount(const QModelIndex& parent) const
int NavigationModel::columnCount(const QModelIndex& parent) const
{
radix_tagged_line("columnCount(" << parent.row() << "," << parent.column()
<< ")");
if (parent.isValid())
return static_cast<NavigationItem*>(parent.internalPointer())
->columnCount();
......@@ -143,4 +130,15 @@ int NavigationModel::columnCount(const QModelIndex& parent) const
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
......@@ -7,6 +7,9 @@
namespace radix
{
// Forward declaration
class NavigationItem;
class NavigationModel : public QAbstractItemModel
{
Q_OBJECT
......@@ -24,6 +27,8 @@ class NavigationModel : public QAbstractItemModel
QModelIndex parent(const QModelIndex &index) const override;
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
int columnCount(const QModelIndex &parent = QModelIndex()) const override;
NavigationItem *rootItem();
NavigationItem *addItem(QVariant value, NavigationItem *parent);
signals:
void itemChecked(const QModelIndex &index);
......
......@@ -89,6 +89,16 @@ NavigationWidget::NavigationWidget(QWidget* parent)
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()
{
auto vlayout = new QVBoxLayout(this);
......@@ -137,6 +147,8 @@ void NavigationWidget::initMembers()
SLOT(filterModel(QString)));
connect(p->view, SIGNAL(doubleClicked(QModelIndex)), this,
SLOT(itemDoubleClicked(QModelIndex)));
connect(p->view, SIGNAL(customContextMenuRequested(QPoint)), this,
SLOT(contextMenuRequested(QPoint)));
}
void NavigationWidget::filterModel(const QString& pattern)
{
......
......@@ -15,7 +15,9 @@ QT_END_NAMESPACE
namespace radix
{
// Forward declaration
class NavigationItem;
class NavigationModel;
class NavigationItemSortFilterProxyModel : public QSortFilterProxyModel
{
......@@ -46,16 +48,18 @@ class NavigationWidget : public QWidget
void collapse(QStandardItem* item);
void expand(QStandardItem* item);
NavigationModel* navigationModel();
QTreeView* navigationTree() const;
private slots:
void contextMenuRequested(QPoint);
public slots:
void filterModel(const QString& pattern);
void itemDoubleClicked(QModelIndex index);
signals:
void itemActivated(NavigationItem* item);
void itemChecked(const QModelIndex& index);
void itemUnchecked(const QModelIndex& index);
private:
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