Commit aa008605 authored by LEFEBVREJP email's avatar LEFEBVREJP email
Browse files

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

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