Commit 582e91f5 authored by Lefebvre, Jordan's avatar Lefebvre, Jordan
Browse files

Merge branch 'nav-model' into 'master'

radixwidgets Navigation model updates

See merge request !106
parents 3b365a23 7c4eca41
Pipeline #144945 canceled with stages
......@@ -26,6 +26,11 @@ NavigationActionManager::PImpl::~PImpl() {}
NavigationActionManager::~NavigationActionManager() { delete p; }
void NavigationActionManager::clearActions(NavigationItem *item)
{
p->item_functions.remove(item);
}
void NavigationActionManager::registerAction(NavigationItem *item, QString text,
std::function<void()> functor)
{
......
......@@ -19,6 +19,7 @@ class RADIX_PUBLIC NavigationActionManager : public QObject
NavigationActionManager(QObject *parent = nullptr);
~NavigationActionManager();
void clearActions(NavigationItem *item);
void registerAction(NavigationItem *item, QString text,
std::function<void()> functor);
void itemActions(NavigationItem *item, QMenu *menu) const;
......
......@@ -11,6 +11,7 @@ class NavigationItem::PImpl
NavigationItem *parent = nullptr;
QList<NavigationItem *> children;
QList<QVariant> data;
QList<QString> tooltips;
bool checked = false;
int type = 0;
~PImpl();
......@@ -62,20 +63,39 @@ int NavigationItem::columnCount() const
return count;
}
QVariant NavigationItem::data(int column) const
QVariant NavigationItem::data(int column, int role) const
{
if (column >= p->data.size()) return QVariant();
return p->data.value(column);
if (Qt::DisplayRole == role)
{
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();
for (int i = size; i <= column; ++i)
if (Qt::DisplayRole == role)
{
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
......@@ -98,4 +118,9 @@ NavigationItem *NavigationItem::child(int row)
return p->children.value(row);
}
bool NavigationItem::disown(NavigationItem *child)
{
return p->children.removeOne(child);
}
} // namespace radix
......@@ -19,10 +19,11 @@ class RADIX_PUBLIC NavigationItem
void setType(int type);
void addChild(NavigationItem *child);
NavigationItem *child(int row);
bool disown(NavigationItem *child);
int childCount() const;
int columnCount() const;
QVariant data(int column) const;
void setData(int column, QVariant value);
QVariant data(int column, int role = Qt::DisplayRole) const;
void setData(int column, QVariant value, int role = Qt::DisplayRole);
int row() const;
NavigationItem *parentItem();
bool isChecked() const;
......
......@@ -32,30 +32,29 @@ QVariant NavigationModel::data(const QModelIndex& index, int role) const
{
if (!index.isValid()) return QVariant();
NavigationItem* item = static_cast<NavigationItem*>(index.internalPointer());
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());
NavigationItem* item = toItem(index);
return item->data(index.column(), role);
}
bool NavigationModel::setData(const QModelIndex& index, const QVariant& value,
int role)
{
NavigationItem* item = toItem(index);
if (Qt::CheckStateRole == role)
{
NavigationItem* item =
static_cast<NavigationItem*>(index.internalPointer());
item->setChecked(value.toBool());
// signal selection/deselection
if (item->isChecked())
emit itemChecked(index);
else
emit itemUnChecked(index);
emit dataChanged(index, index, {Qt::CheckStateRole});
return true;
}
else if (Qt::ToolTipRole == role)
{
item->setData(index.column(), value, role);
}
return false;
}
......@@ -81,46 +80,34 @@ QVariant NavigationModel::headerData(int section, Qt::Orientation orientation,
QModelIndex NavigationModel::index(int row, int column,
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())
parentItem = p->root;
else
parentItem = static_cast<NavigationItem*>(parent.internalPointer());
if (!parentItem) return QModelIndex();
NavigationItem* childItem = parentItem->child(row);
if (childItem)
return createIndex(row, column, childItem);
else
return QModelIndex();
if (childItem) return createIndex(row, column, childItem);
return QModelIndex();
}
QModelIndex NavigationModel::parent(const QModelIndex& index) const
{
if (!index.isValid()) return QModelIndex();
NavigationItem* childItem =
static_cast<NavigationItem*>(index.internalPointer());
NavigationItem* parentItem = childItem->parentItem();
NavigationItem* childItem = toItem(index);
NavigationItem* parentItem = childItem ? childItem->parentItem() : nullptr;
if (parentItem == p->root) return QModelIndex();
if (parentItem == p->root || !parentItem) return QModelIndex();
return createIndex(parentItem->row(), 0, parentItem);
}
int NavigationModel::rowCount(const QModelIndex& parent) const
{
NavigationItem* parentItem;
if (parent.column() > 0) return 0;
if (!parent.isValid())
parentItem = p->root;
else
parentItem = static_cast<NavigationItem*>(parent.internalPointer());
NavigationItem* parentItem = toItem(parent);
return parentItem->childCount();
return parentItem ? parentItem->childCount() : 0;
}
int NavigationModel::columnCount(const QModelIndex& parent) const
......@@ -132,6 +119,54 @@ int NavigationModel::columnCount(const QModelIndex& parent) const
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::addItem(QVariant value, NavigationItem* parent)
......@@ -143,4 +178,28 @@ NavigationItem* NavigationModel::addItem(QVariant value, NavigationItem* parent)
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
......@@ -29,14 +29,20 @@ class RADIX_PUBLIC 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;
bool removeItem(NavigationItem *item);
bool removeRows(int row, int count,
const QModelIndex &index = QModelIndex()) override;
NavigationItem *rootItem();
NavigationItem *addItem(QVariant value, NavigationItem *parent);
NavigationItem *toItem(const QModelIndex &index) const;
void clear();
signals:
void itemChecked(const QModelIndex &index);
void itemUnChecked(const QModelIndex &index);
private:
bool removeChild(NavigationItem *parent, NavigationItem *item);
class PImpl;
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