• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdevelop API Reference
  • KDE Home
  • Contact Us
 

kdevplatform/debugger

  • sources
  • kfour-appscomplete
  • kdevelop
  • kdevplatform
  • debugger
  • util
treemodel.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of KDevelop
3  *
4  * Copyright 2008 Vladimir Prus <[email protected]>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation; either version 2 of the
9  * License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public
17  * License along with this program; if not, write to the
18  * Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  */
21 
22 #include "treemodel.h"
23 
24 #include <iostream>
25 
26 #include "treeitem.h"
27 
28 using namespace KDevelop;
29 
30 class KDevelop::TreeModelPrivate
31 {
32 public:
33  explicit TreeModelPrivate(const QVector<QString>& headers)
34  : headers(headers)
35  {
36  }
37 
38  QVector<QString> headers;
39  TreeItem* root = nullptr;
40 };
41 
42 TreeModel::TreeModel(const QVector<QString>& headers,
43  QObject *parent)
44  : QAbstractItemModel(parent)
45  , d_ptr(new TreeModelPrivate(headers))
46 {
47 }
48 
49 void TreeModel::setRootItem(TreeItem *item)
50 {
51  Q_D(TreeModel);
52 
53  d->root = item;
54  d->root->fetchMoreChildren();
55 }
56 
57 TreeModel::~TreeModel()
58 {
59  Q_D(TreeModel);
60 
61  delete d->root;
62 }
63 
64 int TreeModel::columnCount(const QModelIndex &parent) const
65 {
66  Q_D(const TreeModel);
67 
68  Q_UNUSED(parent);
69  return d->headers.size();
70 }
71 
72 QVariant TreeModel::data(const QModelIndex &index, int role) const
73 {
74  if (!index.isValid())
75  return QVariant();
76 
77  auto *item = static_cast<TreeItem*>(index.internalPointer());
78  if (role == ItemRole)
79  return QVariant::fromValue(item);
80 
81  return item->data(index.column(), role);
82 }
83 
84 Qt::ItemFlags TreeModel::flags(const QModelIndex &index) const
85 {
86  if (!index.isValid())
87  return Qt::NoItemFlags;
88 
89  return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
90 }
91 
92 QVariant TreeModel::headerData(int section, Qt::Orientation orientation,
93  int role) const
94 {
95  Q_D(const TreeModel);
96 
97  if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
98  return d->headers.value(section);
99 
100  return QVariant();
101 }
102 
103 QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent)
104  const
105 {
106  Q_D(const TreeModel);
107 
108  if (!hasIndex(row, column, parent))
109  return QModelIndex();
110 
111  TreeItem *parentItem;
112 
113  if (!parent.isValid())
114  parentItem = d->root;
115  else
116  parentItem = static_cast<TreeItem*>(parent.internalPointer());
117 
118  TreeItem *childItem = parentItem->child(row);
119  if (childItem)
120  return createIndex(row, column, childItem);
121  else
122  return createIndex(row, column, nullptr);
123 }
124 
125 QModelIndex TreeModel::parent(const QModelIndex &index) const
126 {
127  Q_D(const TreeModel);
128 
129  if (!index.isValid())
130  return QModelIndex();
131 
132  auto *childItem = static_cast<TreeItem*>(index.internalPointer());
133  TreeItem *parentItem = childItem->parent();
134 
135  if (parentItem == d->root)
136  return QModelIndex();
137 
138  return createIndex(parentItem->row(), 0, parentItem);
139 }
140 
141 int TreeModel::rowCount(const QModelIndex &parent) const
142 {
143  Q_D(const TreeModel);
144 
145  TreeItem *parentItem;
146  if (parent.column() > 0)
147  return 0;
148 
149  if (!parent.isValid())
150  parentItem = d->root;
151  else
152  parentItem = static_cast<TreeItem*>(parent.internalPointer());
153 
154  if(parentItem)
155  return parentItem->childCount();
156  else
157  return 0;
158 }
159 
160 TreeItem* TreeModel::itemForIndex(const QModelIndex& index) const
161 {
162  Q_D(const TreeModel);
163 
164  if (!index.isValid())
165  return d->root;
166  else
167  return static_cast<TreeItem*>(index.internalPointer());
168 }
169 
170 QModelIndex TreeModel::indexForItem(TreeItem *item, int column) const
171 {
172  if (item->parent() == nullptr)
173  return QModelIndex();
174 
175  if (TreeItem* parent = item->parent())
176  {
177  /* FIXME: we might store row directly in item. */
178  int row = parent->childItems.indexOf(item);
179  Q_ASSERT(row != -1);
180 
181  return createIndex(row, column, item);
182  }
183  else
184  {
185  return QModelIndex();
186  }
187 }
188 
189 void TreeModel::expanded(const QModelIndex &index)
190 {
191  TreeItem* item = itemForIndex(index);
192  QObject::connect(item, &TreeItem::allChildrenFetched, this, &TreeModel::itemChildrenReady);
193  if (item->hasMore() && item->childCount() == 1)
194  item->fetchMoreChildren();
195  else
196  emit itemChildrenReady();
197  item->setExpanded(true);
198 }
199 
200 void TreeModel::collapsed(const QModelIndex &index)
201 {
202  TreeItem* item = itemForIndex(index);
203  item->setExpanded(false);
204 }
205 
206 void TreeModel::clicked(const QModelIndex &index)
207 {
208  TreeItem* item = itemForIndex(index);
209  item->clicked();
210 }
211 
212 bool TreeModel::setData(const QModelIndex& index, const QVariant& value,
213  int role)
214 {
215  /* FIXME: CheckStateRole is dirty. Should we pass the role to
216  the item? */
217  if (index.isValid()
218  && (role == Qt::EditRole || role == Qt::CheckStateRole))
219  {
220 
221  auto *item = static_cast<TreeItem*>(index.internalPointer());
222  item->setColumn(index.column(), value);
223  return true;
224  }
225  return false;
226 }
227 
228 KDevelop::TreeItem* KDevelop::TreeModel::root() const
229 {
230  Q_D(const TreeModel);
231 
232  return d->root;
233 }
234 
235 
KDevelop::TreeItem::hasMore
bool hasMore() const
Definition: treeitem.h:108
KDevelop::TreeModel::data
QVariant data(const QModelIndex &index, int role) const override
Definition: treemodel.cpp:72
QVariant::fromValue
QVariant fromValue(const T &value)
QModelIndex::internalPointer
void * internalPointer() const
KDevelop::TreeModel::headerData
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const override
Definition: treemodel.cpp:92
treeitem.h
KDevelop::TreeModel::flags
Qt::ItemFlags flags(const QModelIndex &index) const override
Definition: treemodel.cpp:84
QModelIndex::column
int column() const
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QAbstractItemModel::hasIndex
bool hasIndex(int row, int column, const QModelIndex &parent) const
KDevelop::TreeModel
Definition: treemodel.h:38
KDevelop::TreeModel::columnCount
int columnCount(const QModelIndex &parent=QModelIndex()) const override
Definition: treemodel.cpp:64
QAbstractItemModel::createIndex
QModelIndex createIndex(int row, int column, void *ptr) const
KDevelop::TreeItem::setExpanded
void setExpanded(bool b)
Definition: treeitem.cpp:256
KDevelop::TreeModel::itemForIndex
TreeItem * itemForIndex(const QModelIndex &index) const
Definition: treemodel.cpp:160
KDevelop::TreeItem::parent
TreeItem * parent()
Definition: treeitem.cpp:172
QObject
KDevelop::TreeModel::ItemRole
Definition: treemodel.h:55
KDevelop::TreeItem
Definition: treeitem.h:37
KDevelop::TreeModel::setData
bool setData(const QModelIndex &index, const QVariant &value, int role) override
Definition: treemodel.cpp:212
KDevelop::TreeModel::root
TreeItem * root() const
Definition: treemodel.cpp:228
KDevelop::TreeModel::expanded
void expanded(const QModelIndex &index)
Definition: treemodel.cpp:189
KDevelop::TreeModel::indexForItem
QModelIndex indexForItem(TreeItem *item, int column) const
Definition: treemodel.cpp:170
KDevelop::TreeItem::allChildrenFetched
void allChildrenFetched()
QModelIndex::isValid
bool isValid() const
KDevelop::TreeItem::fetchMoreChildren
virtual void fetchMoreChildren()=0
Fetches more children, and adds them by calling appendChild.
KDevelop::TreeModel::setRootItem
void setRootItem(TreeItem *item)
Definition: treemodel.cpp:49
KDevelop::TreeModel::rowCount
int rowCount(const QModelIndex &parent=QModelIndex()) const override
Definition: treemodel.cpp:141
KDevelop::TreeModel::TreeModel
TreeModel(const QVector< QString > &headers, QObject *parent=nullptr)
Definition: treemodel.cpp:42
KDevelop::TreeModel::collapsed
void collapsed(const QModelIndex &index)
Definition: treemodel.cpp:200
KDevelop::TreeModel::itemChildrenReady
void itemChildrenReady()
KDevelop::TreeModel::clicked
void clicked(const QModelIndex &index)
Definition: treemodel.cpp:206
KDevelop::TreeModel::~TreeModel
~TreeModel() override
Definition: treemodel.cpp:57
KDevelop
The variables widget is passive, and is invoked by the rest of the code via two main Q_SLOTS:
Definition: breakpoint.h:34
QModelIndex
Qt::ItemFlags
typedef ItemFlags
QVector< QString >
QVariant
KDevelop::TreeItem::childCount
int childCount() const
Definition: treeitem.cpp:153
KDevelop::TreeItem::child
TreeItem * child(int row)
Definition: treeitem.cpp:142
KDevelop::TreeModel::index
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
Definition: treemodel.cpp:103
KDevelop::TreeItem::clicked
virtual void clicked()
Definition: treeitem.h:111
treemodel.h
QObject::parent
QObject * parent() const
QAbstractItemModel
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Mon Jan 25 2021 23:36:19 by doxygen 1.8.16 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kdevplatform/debugger

Skip menu "kdevplatform/debugger"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdevelop API Reference

Skip menu "kdevelop API Reference"
  • kdevplatform
  •   debugger
  •   documentation
  •   interfaces
  •   language
  •     assistant
  •     backgroundparser
  •     checks
  •     classmodel
  •     codecompletion
  •     codegen
  •     duchain
  •     editor
  •     highlighting
  •     interfaces
  •     util
  •   outputview
  •   project
  •   serialization
  •   shell
  •   sublime
  •   tests
  •   util
  •   vcs

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal