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

WIP: Adding checkbox state for selection/deselection of items.

parent 2e845f6c
......@@ -11,6 +11,7 @@ class NavigationItem::PImpl
NavigationItem *parent = nullptr;
QList<NavigationItem *> children;
QList<QVariant> data;
bool checked = false;
~PImpl();
};
......@@ -75,6 +76,10 @@ int NavigationItem::row() const
NavigationItem *NavigationItem::parentItem() { return p->parent; }
bool NavigationItem::isChecked() const { return p->checked; }
void NavigationItem::setChecked(bool checked) { p->checked = checked; }
NavigationItem *NavigationItem::child(int row)
{
return p->children.value(row);
......
......@@ -21,6 +21,8 @@ class NavigationItem
void setData(int column, QVariant value);
int row() const;
NavigationItem *parentItem();
bool isChecked() const;
void setChecked(bool checked);
private:
class PImpl;
......
......@@ -37,16 +37,30 @@ QVariant NavigationModel::data(const QModelIndex& index, int role) const
radix_tagged_line("data(" << index.row() << ", " << index.column() << ")");
if (!index.isValid()) return QVariant();
if (role != Qt::DisplayRole) 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());
}
bool NavigationModel::setData(const QModelIndex& index, const QVariant& value,
int role)
{
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);
return true;
}
return false;
}
......@@ -54,7 +68,10 @@ Qt::ItemFlags NavigationModel::flags(const QModelIndex& index) const
{
if (!index.isValid()) return QAbstractItemModel::flags(index);
return QAbstractItemModel::flags(index);
Qt::ItemFlags flags = Qt::ItemIsEnabled | Qt::ItemIsSelectable;
if (index.column() == 0) flags |= Qt::ItemIsUserCheckable;
return flags;
}
QVariant NavigationModel::headerData(int section, Qt::Orientation orientation,
......
......@@ -25,6 +25,10 @@ class NavigationModel : public QAbstractItemModel
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
int columnCount(const QModelIndex &parent = QModelIndex()) const override;
signals:
void itemChecked(const QModelIndex &index);
void itemUnChecked(const QModelIndex &index);
private:
class PImpl;
std::unique_ptr<PImpl, void (*)(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