Commit 7c4eca41 authored by Lefebvre, Jordan's avatar Lefebvre, Jordan
Browse files

Navigation model updates

parent 3b365a23
...@@ -26,6 +26,11 @@ NavigationActionManager::PImpl::~PImpl() {} ...@@ -26,6 +26,11 @@ NavigationActionManager::PImpl::~PImpl() {}
NavigationActionManager::~NavigationActionManager() { delete p; } NavigationActionManager::~NavigationActionManager() { delete p; }
void NavigationActionManager::clearActions(NavigationItem *item)
{
p->item_functions.remove(item);
}
void NavigationActionManager::registerAction(NavigationItem *item, QString text, void NavigationActionManager::registerAction(NavigationItem *item, QString text,
std::function<void()> functor) std::function<void()> functor)
{ {
......
...@@ -19,6 +19,7 @@ class RADIX_PUBLIC NavigationActionManager : public QObject ...@@ -19,6 +19,7 @@ class RADIX_PUBLIC NavigationActionManager : public QObject
NavigationActionManager(QObject *parent = nullptr); NavigationActionManager(QObject *parent = nullptr);
~NavigationActionManager(); ~NavigationActionManager();
void clearActions(NavigationItem *item);
void registerAction(NavigationItem *item, QString text, void registerAction(NavigationItem *item, QString text,
std::function<void()> functor); std::function<void()> functor);
void itemActions(NavigationItem *item, QMenu *menu) const; void itemActions(NavigationItem *item, QMenu *menu) const;
......
...@@ -11,6 +11,7 @@ class NavigationItem::PImpl ...@@ -11,6 +11,7 @@ class NavigationItem::PImpl
NavigationItem *parent = nullptr; NavigationItem *parent = nullptr;
QList<NavigationItem *> children; QList<NavigationItem *> children;
QList<QVariant> data; QList<QVariant> data;
QList<QString> tooltips;
bool checked = false; bool checked = false;
int type = 0; int type = 0;
~PImpl(); ~PImpl();
...@@ -62,20 +63,39 @@ int NavigationItem::columnCount() const ...@@ -62,20 +63,39 @@ int NavigationItem::columnCount() const
return count; return count;
} }
QVariant NavigationItem::data(int column) const QVariant NavigationItem::data(int column, int role) const
{ {
if (column >= p->data.size()) return QVariant(); if (Qt::DisplayRole == role)
return p->data.value(column); {
if (column >= p->data.size()) return QVariant();
return p->data.value(column);
}
else if (Qt::ToolTipRole == role)
{
if (column < p->tooltips.size()) return p->tooltips.value(column);
}
else if (Qt::CheckStateRole == role)
{
return static_cast<int>(isChecked() ? Qt::Checked : Qt::Unchecked);
}
return QVariant();
} }
void NavigationItem::setData(int column, QVariant value) void NavigationItem::setData(int column, QVariant value, int role)
{ {
int size = p->data.size(); if (Qt::DisplayRole == role)
for (int i = size; i <= column; ++i)
{ {
p->data.append(QVariant()); int size = p->data.size();
for (int i = size; i <= column; ++i)
{
p->data.append(QVariant());
}
p->data[column] = value;
}
else if (Qt::ToolTipRole == role)
{
p->tooltips.insert(column, value.toString());
} }
p->data[column] = value;
} }
int NavigationItem::row() const int NavigationItem::row() const
...@@ -98,4 +118,9 @@ NavigationItem *NavigationItem::child(int row) ...@@ -98,4 +118,9 @@ NavigationItem *NavigationItem::child(int row)
return p->children.value(row); return p->children.value(row);
} }
bool NavigationItem::disown(NavigationItem *child)
{
return p->children.removeOne(child);
}
} // namespace radix } // namespace radix
...@@ -19,10 +19,11 @@ class RADIX_PUBLIC NavigationItem ...@@ -19,10 +19,11 @@ class RADIX_PUBLIC NavigationItem
void setType(int type); void setType(int type);
void addChild(NavigationItem *child); void addChild(NavigationItem *child);
NavigationItem *child(int row); NavigationItem *child(int row);
bool disown(NavigationItem *child);
int childCount() const; int childCount() const;
int columnCount() const; int columnCount() const;
QVariant data(int column) const; QVariant data(int column, int role = Qt::DisplayRole) const;
void setData(int column, QVariant value); void setData(int column, QVariant value, int role = Qt::DisplayRole);
int row() const; int row() const;
NavigationItem *parentItem(); NavigationItem *parentItem();
bool isChecked() const; bool isChecked() const;
......
...@@ -32,30 +32,29 @@ QVariant NavigationModel::data(const QModelIndex& index, int role) const ...@@ -32,30 +32,29 @@ QVariant NavigationModel::data(const QModelIndex& index, int role) const
{ {
if (!index.isValid()) return QVariant(); if (!index.isValid()) return QVariant();
NavigationItem* item = static_cast<NavigationItem*>(index.internalPointer()); NavigationItem* item = toItem(index);
return item->data(index.column(), role);
if (Qt::CheckStateRole == role && index.column() == 0)
return static_cast<int>(item->isChecked() ? Qt::Checked : Qt::Unchecked);
if (role != Qt::DisplayRole) return QVariant();
return item->data(index.column());
} }
bool NavigationModel::setData(const QModelIndex& index, const QVariant& value, bool NavigationModel::setData(const QModelIndex& index, const QVariant& value,
int role) int role)
{ {
NavigationItem* item = toItem(index);
if (Qt::CheckStateRole == role) if (Qt::CheckStateRole == role)
{ {
NavigationItem* item =
static_cast<NavigationItem*>(index.internalPointer());
item->setChecked(value.toBool()); item->setChecked(value.toBool());
// signal selection/deselection // signal selection/deselection
if (item->isChecked()) if (item->isChecked())
emit itemChecked(index); emit itemChecked(index);
else else
emit itemUnChecked(index); emit itemUnChecked(index);
emit dataChanged(index, index, {Qt::CheckStateRole});
return true; return true;
} }
else if (Qt::ToolTipRole == role)
{
item->setData(index.column(), value, role);
}
return false; return false;
} }
...@@ -81,46 +80,34 @@ QVariant NavigationModel::headerData(int section, Qt::Orientation orientation, ...@@ -81,46 +80,34 @@ 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
{ {
if (!hasIndex(row, column, parent)) return QModelIndex(); if (parent.isValid() && parent.column() != 0) return QModelIndex();
NavigationItem* parentItem; NavigationItem* parentItem = toItem(parent);
if (!parent.isValid()) if (!parentItem) return QModelIndex();
parentItem = p->root;
else
parentItem = static_cast<NavigationItem*>(parent.internalPointer());
NavigationItem* childItem = parentItem->child(row); NavigationItem* childItem = parentItem->child(row);
if (childItem) if (childItem) return createIndex(row, column, childItem);
return createIndex(row, column, childItem); return QModelIndex();
else
return QModelIndex();
} }
QModelIndex NavigationModel::parent(const QModelIndex& index) const QModelIndex NavigationModel::parent(const QModelIndex& index) const
{ {
if (!index.isValid()) return QModelIndex(); if (!index.isValid()) return QModelIndex();
NavigationItem* childItem = NavigationItem* childItem = toItem(index);
static_cast<NavigationItem*>(index.internalPointer()); NavigationItem* parentItem = childItem ? childItem->parentItem() : nullptr;
NavigationItem* parentItem = childItem->parentItem();
if (parentItem == p->root) return QModelIndex(); if (parentItem == p->root || !parentItem) return QModelIndex();
return createIndex(parentItem->row(), 0, parentItem); return createIndex(parentItem->row(), 0, parentItem);
} }
int NavigationModel::rowCount(const QModelIndex& parent) const int NavigationModel::rowCount(const QModelIndex& parent) const
{ {
NavigationItem* parentItem; NavigationItem* parentItem = toItem(parent);
if (parent.column() > 0) return 0;
if (!parent.isValid())
parentItem = p->root;
else
parentItem = static_cast<NavigationItem*>(parent.internalPointer());
return parentItem->childCount(); return parentItem ? parentItem->childCount() : 0;
} }
int NavigationModel::columnCount(const QModelIndex& parent) const int NavigationModel::columnCount(const QModelIndex& parent) const
...@@ -132,6 +119,54 @@ int NavigationModel::columnCount(const QModelIndex& parent) const ...@@ -132,6 +119,54 @@ int NavigationModel::columnCount(const QModelIndex& parent) const
return p->root->columnCount(); return p->root->columnCount();
} }
bool NavigationModel::removeChild(NavigationItem* parent, NavigationItem* item)
{
if (parent == nullptr) return false;
for (int ri = 0; ri < parent->childCount(); ++ri)
{
NavigationItem* child = parent->child(ri);
if (item == child)
{
QModelIndex qIndex = createIndex(ri, 0, child);
return removeRow(ri, qIndex);
}
else
{
radix_tagged_line("Recursing down parent(" << parent << ") child("
<< child << ")");
if (removeChild(child, item)) return true;
}
}
return false;
}
bool NavigationModel::removeItem(NavigationItem* item)
{
return removeChild(p->root, item);
}
bool NavigationModel::removeRows(int row, int count, const QModelIndex& index)
{
QVector<NavigationItem*> deleteList;
beginRemoveRows(index.parent(), row, row + count - 1);
NavigationItem* parentItem = toItem(index.parent());
for (int ci = 0; ci < count; ++ci)
{
NavigationItem* child = parentItem->child(row);
radix_tagged_line("Remove child(" << child << ") from parent ("
<< parentItem << ") at row(" << row
<< ")");
parentItem->disown(child);
deleteList.push_back(child);
}
endRemoveRows();
qDeleteAll(deleteList);
return true;
}
NavigationItem* NavigationModel::rootItem() { return p->root; } NavigationItem* NavigationModel::rootItem() { return p->root; }
NavigationItem* NavigationModel::addItem(QVariant value, NavigationItem* parent) NavigationItem* NavigationModel::addItem(QVariant value, NavigationItem* parent)
...@@ -143,4 +178,28 @@ NavigationItem* NavigationModel::addItem(QVariant value, NavigationItem* parent) ...@@ -143,4 +178,28 @@ NavigationItem* NavigationModel::addItem(QVariant value, NavigationItem* parent)
return newItem; return newItem;
} }
NavigationItem* NavigationModel::toItem(const QModelIndex& index) const
{
if (index.isValid())
{
NavigationItem* item = nullptr;
item = static_cast<NavigationItem*>(index.internalPointer());
if (item) return item;
}
return p->root;
}
void NavigationModel::clear()
{
int numKids = p->root->childCount();
for (int ki = 0; ki < numKids; ++ki)
{
NavigationItem* kid = p->root->child(0);
if (p->root->disown(kid))
{
delete kid;
}
}
}
} // namespace radix } // namespace radix
...@@ -29,14 +29,20 @@ class RADIX_PUBLIC NavigationModel : public QAbstractItemModel ...@@ -29,14 +29,20 @@ class RADIX_PUBLIC 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;
bool removeItem(NavigationItem *item);
bool removeRows(int row, int count,
const QModelIndex &index = QModelIndex()) override;
NavigationItem *rootItem(); NavigationItem *rootItem();
NavigationItem *addItem(QVariant value, NavigationItem *parent); NavigationItem *addItem(QVariant value, NavigationItem *parent);
NavigationItem *toItem(const QModelIndex &index) const;
void clear();
signals: signals:
void itemChecked(const QModelIndex &index); void itemChecked(const QModelIndex &index);
void itemUnChecked(const QModelIndex &index); void itemUnChecked(const QModelIndex &index);
private: private:
bool removeChild(NavigationItem *parent, NavigationItem *item);
class PImpl; class PImpl;
PImpl *p; PImpl *p;
}; };
......
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