28 CheckableTreeModel::Item::~Item() {
29 for (
auto c : children) {
35 CheckableTreeModel::CheckableTreeModel(
const ptree& tree,
QObject* parent)
38 m_root =
new Item({
nullptr, {}, QString(),
false});
39 m_root->children.reserve(tree.size());
41 function<void(const ptree&, Item*)> work_recursively;
42 (work_recursively = [&](
const ptree& tree, Item*
parent) {
43 for (
auto it = tree.ordered_begin(); it != tree.not_found(); it++) {
45 auto checked = node.second.get<
bool>(
"checked");
46 auto item =
new Item({
parent, {}, QString::fromStdString(node.first), checked});
47 parent->children.push_back(item);
49 auto children_optional = node.second.get_child_optional(
"children");
50 if (children_optional) {
51 auto& children = children_optional.get();
52 item->children.reserve(children.size());
53 work_recursively(children, item);
59 CheckableTreeModel::~CheckableTreeModel() {
68 function<ptree(Item*)> work_recursive;
69 return (work_recursive = [&](Item* root) -> ptree {
71 for (
auto c : root->children) {
73 child_pt.put(
"checked", c->checked);
74 child_pt.put_child(
"children", work_recursive(c));
75 result.put_child(c->data.toStdString(), child_pt);
87 if (!index.isValid()) {
91 auto item =
static_cast<Item*
>(index.internalPointer());
93 if (role == Qt::CheckStateRole) {
94 return item->checked? Qt::Checked : Qt::Unchecked;
97 if (role == Qt::DisplayRole) {
106 if (!index.isValid()) {
110 return Qt::ItemIsEnabled | Qt::ItemIsUserCheckable;
120 if (!hasIndex(row, column, parent)) {
121 return QModelIndex();
125 if (!parent.isValid()) {
126 parent_item = m_root;
128 parent_item =
static_cast<Item*
>(parent.internalPointer());
131 return createIndex(row, column, parent_item->children[row]);
136 if (!index.isValid()) {
137 return QModelIndex();
139 Item* child =
static_cast<Item*
>(index.internalPointer());
140 Item*
parent = child->parent;
141 if (parent !=
nullptr) {
142 Item* grandparent = parent->parent;
143 if (grandparent !=
nullptr) {
144 auto it = find(grandparent->children.begin(), grandparent->children.end(),
parent);
145 return createIndex(it - grandparent->children.begin(), 0,
parent);
148 return QModelIndex();
154 if (!parent.isValid()) {
155 parent_item = m_root;
157 parent_item =
static_cast<Item*
>(parent.internalPointer());
159 return parent_item->children.size();
164 if (!index.isValid()) {
168 Item* item =
static_cast<Item*
>(index.internalPointer());
169 if (role == Qt::CheckStateRole) {
170 if (index.column() == 0) {
171 item->checked = value.toBool();
173 emit dataChanged(index, index);
virtual QVariant headerData(int section, Qt::Orientation o, int role) const
see the online Qt documentation
virtual int columnCount(QModelIndex const &) const
virtual QVariant data(const QModelIndex &index, int role) const
virtual bool setData(QModelIndex const &index, const QVariant &value, int role)
virtual int rowCount(QModelIndex const &parent) const
virtual QModelIndex parent(QModelIndex const &index) const
virtual QModelIndex index(int row, int column, QModelIndex const &parent) const
CheckableTreeModel header.
boost::property_tree::ptree ToPTree()
Get checked/unchecked state of items, represented by a ptree of the form discussed in the constructor...
virtual Qt::ItemFlags flags(const QModelIndex &index) const
see the online Qt documentation
Namespace for generic graphical shell for simulators.