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

okteta

  • sources
  • kde-4.12
  • kdesdk
  • okteta
  • kasten
  • controllers
  • view
  • structures
structtreemodel.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of the Okteta Kasten Framework, made within the KDE community.
3  *
4  * Copyright 2009, 2010, 2011 Alex Richardson <alex.richardson@gmx.de>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) version 3, or any
10  * later version accepted by the membership of KDE e.V. (or its
11  * successor approved by the membership of KDE e.V.), which shall
12  * act as a proxy defined in Section 6 of version 3 of the license.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library. If not, see <http://www.gnu.org/licenses/>.
21  */
22 
23 #include "structtreemodel.h"
24 #include "structtool.h"
25 #include "datatypes/datainformationwithchildren.h"
26 #include "datatypes/topleveldatainformation.h"
27 #include "datatypes/array/arraydatainformation.h"
28 
29 #include <QFont>
30 
31 #include <KDebug>
32 
33 namespace Kasten2
34 {
35 StructTreeModel::StructTreeModel(StructTool* tool, QObject *parent) :
36  QAbstractItemModel(parent), mTool(tool), mLastSender(0), mLastStartIndex(0), mLastEndIndex(0)
37 {
38  connect(mTool, SIGNAL(dataChanged(int,void*)), this, SLOT(onToolDataChange(int,void*)));
39  connect(mTool, SIGNAL(dataCleared()), this, SLOT(onToolDataClear()));
40  connect(mTool, SIGNAL(childrenAboutToBeInserted(DataInformation*,uint,uint)),
41  this, SLOT(onChildrenAboutToBeInserted(DataInformation*,uint,uint)));
42  connect(mTool, SIGNAL(childrenAboutToBeRemoved(DataInformation*,uint,uint)),
43  this, SLOT(onChildrenAboutToBeRemoved(DataInformation*,uint,uint)));
44  connect(mTool, SIGNAL(childrenInserted(const DataInformation*,uint,uint)),
45  this, SLOT(onChildrenInserted(const DataInformation*,uint,uint)));
46  connect(mTool, SIGNAL(childrenRemoved(const DataInformation*,uint,uint)),
47  this, SLOT(onChildrenRemoved(const DataInformation*,uint,uint)));
48 
49 }
50 
51 StructTreeModel::~StructTreeModel()
52 {
53 }
54 
55 void StructTreeModel::onChildrenRemoved(const DataInformation* sender, uint startIndex,
56  uint endIndex)
57 {
58  Q_ASSERT(sender == mLastSender);
59  Q_ASSERT(startIndex== mLastStartIndex);
60  Q_ASSERT(endIndex == mLastEndIndex);
61  Q_UNUSED(sender)
62  Q_UNUSED(startIndex)
63  Q_UNUSED(endIndex)
64  emit endRemoveRows();
65 }
66 
67 void StructTreeModel::onChildrenInserted(const DataInformation* sender, uint startIndex,
68  uint endIndex)
69 {
70  Q_ASSERT(sender == mLastSender);
71  Q_ASSERT(startIndex== mLastStartIndex);
72  Q_ASSERT(endIndex == mLastEndIndex);
73  Q_UNUSED(sender)
74  Q_UNUSED(startIndex)
75  Q_UNUSED(endIndex)
76  emit endInsertRows();
77 }
78 
79 void StructTreeModel::onChildrenAboutToBeRemoved(DataInformation* sender, uint startIndex,
80  uint endIndex)
81 {
82  //kDebug() << "data information" << sender->fullObjectPath() << ": removing "
83  // "children from index" << startIndex << "to" << endIndex;
84  QModelIndex idx = findItemInModel(sender);
85  Q_ASSERT(idx.isValid());
86  mLastSender = sender;
87  mLastStartIndex = startIndex;
88  mLastEndIndex = endIndex;
89  emit beginRemoveRows(idx, startIndex, endIndex);
90 }
91 
92 void StructTreeModel::onChildrenAboutToBeInserted(DataInformation* sender, uint startIndex,
93  uint endIndex)
94 {
95  //kDebug() << "data information" << sender->fullObjectPath() << ": inserting "
96  // "children from index" << startIndex << "to" << endIndex;
97  QModelIndex idx = findItemInModel(sender);
98  Q_ASSERT(idx.isValid());
99  mLastSender = sender;
100  mLastStartIndex = startIndex;
101  mLastEndIndex = endIndex;
102  emit beginInsertRows(idx, startIndex, endIndex);
103 }
104 
105 int StructTreeModel::columnCount(const QModelIndex& parent) const
106 {
107  Q_UNUSED(parent)
108  return DataInformation::COLUMN_COUNT;
109 }
110 
111 QVariant StructTreeModel::data(const QModelIndex& index, int role) const
112 {
113  if (!index.isValid())
114  return QVariant();
115 
116  DataInformation* item = static_cast<DataInformation*> (index.internalPointer());
117  const int column = index.column();
118  if (role == Qt::FontRole)
119  {
120  if (column == 0 && item->parent()->isTopLevel())
121  {
122  // TODO: ideally here we would not take the default application font
123  // (as given by QFont()) but the default of the view
124  QFont font;
125  font.setBold(true);
126  return font;
127  }
128  else
129  return QVariant();
130  }
131  if (item->parent()->isArray())
132  {
133  ArrayDataInformation* array = item->parent()->asArray();
134  return array->childData(index.row(), column, role);
135  }
136  return item->data(column, role);
137 }
138 
139 bool StructTreeModel::setData(const QModelIndex& index, const QVariant& value,
140  int role)
141 {
142  if (!index.isValid())
143  return false;
144 
145  if (!index.internalPointer())
146  {
147  kDebug() << "item == NULL";
148  return false;
149  }
150 
151  DataInformation* item = static_cast<DataInformation*> (index.internalPointer());
152  bool change = mTool->setData(value, role, item, index.row());
153  if (change)
154  {
155  emit dataChanged(index, index);
156  }
157  return change;
158 }
159 
160 Qt::ItemFlags StructTreeModel::flags(const QModelIndex& index) const
161 {
162  if (!index.isValid())
163  return 0;
164  DataInformation* item = static_cast<DataInformation*> (index.internalPointer());
165  return item->flags(index.column(), mTool->isFileLoaded());
166 }
167 
168 QVariant StructTreeModel::headerData(int section, Qt::Orientation orientation,
169  int role) const
170 {
171  if (orientation == Qt::Horizontal)
172  {
173  return mTool->headerData(section, role);
174  }
175  return QVariant();
176 }
177 
178 QModelIndex StructTreeModel::index(int row, int column, const QModelIndex &parent) const
179 {
180  if (!hasIndex(row, column, parent))
181  return QModelIndex();
182 
183  DataInformation* childItem = NULL;
184 
185  if (!parent.isValid())
186  childItem = mTool->childAt(row);
187  else
188  {
189  if (parent.column() != 0)
190  return QModelIndex();
191  DataInformation* parentItem = static_cast<DataInformation*> (parent.internalPointer());
192  childItem = parentItem->childAt(row);
193  }
194  if (childItem)
195  {
196  return createIndex(row, column, childItem);
197  }
198  else
199  return QModelIndex();
200 }
201 
202 QModelIndex StructTreeModel::parent(const QModelIndex& index) const
203 {
204  if (!index.isValid())
205  return QModelIndex();
206 
207  DataInformation* childItem = static_cast<DataInformation*> (index.internalPointer());
208 
209  DataInformationBase* parentObj = childItem->parent();
210 
211  if (!parentObj || parentObj->isTopLevel())
212  return QModelIndex();
213 
214  // not null, not topleveldatainformation-> must be datainformation
215  DataInformation* parent = parentObj->asDataInformation();
216  return createIndex(parent->row(), 0, parent);
217 }
218 
219 int StructTreeModel::rowCount(const QModelIndex& parent) const
220 {
221  if (!parent.isValid())
222  return mTool->childCount();
223  if (parent.column() != 0)
224  return 0;
225  DataInformation* parentItem =
226  static_cast<DataInformation*> (parent.internalPointer());
227  if (!parentItem)
228  {
229  kDebug() << "parentItem is NULL";
230  return mTool->childCount();
231  }
232  return parentItem->childCount();
233 }
234 
235 bool StructTreeModel::hasChildren(const QModelIndex& parent) const
236 {
237  if (!parent.isValid())
238  return mTool->childCount() > 0;
239  DataInformation* parentItem =
240  static_cast<DataInformation*> (parent.internalPointer());
241  if (!parentItem)
242  return false;
243  else
244  return parentItem->childCount() > 0;
245 }
246 
247 QModelIndex StructTreeModel::findItemInModel(DataInformationBase* data) const
248 {
249  Q_CHECK_PTR(data);
250  if (!data || data->isTopLevel())
251  return QModelIndex(); //invalid object
252  return createIndex(data->asDataInformation()->row(), 0, data);
253 }
254 
255 void StructTreeModel::onToolDataChange(int row, void* data)
256 {
257  emit dataChanged(createIndex(row, 0, data), createIndex(row, 2, data));
258 }
259 
260 void StructTreeModel::onToolDataClear()
261 {
262  emit reset();
263 }
264 
265 
266 }
Kasten2::StructTreeModel::setData
bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole)
Definition: structtreemodel.cpp:139
DataInformation
Interface that must be implemented by all datatypes.
Definition: datainformation.h:67
Kasten2::StructTool::headerData
QVariant headerData(int column, int role)
Definition: structtool.cpp:210
Kasten2::StructTreeModel::onChildrenAboutToBeRemoved
void onChildrenAboutToBeRemoved(DataInformation *sender, uint startIndex, uint endIndex)
Definition: structtreemodel.cpp:79
Kasten2::StructTreeModel::rowCount
int rowCount(const QModelIndex &parent=QModelIndex()) const
Definition: structtreemodel.cpp:219
DataInformation::childCount
virtual unsigned int childCount() const =0
Definition: datainformation.h:278
Kasten2::StructTreeModel::onChildrenInserted
void onChildrenInserted(const DataInformation *sender, uint startIndex, uint endIndex)
Definition: structtreemodel.cpp:67
Kasten2::StructTreeModel::~StructTreeModel
virtual ~StructTreeModel()
Definition: structtreemodel.cpp:51
Kasten2::StructTreeModel::onChildrenRemoved
void onChildrenRemoved(const DataInformation *sender, uint startIndex, uint endIndex)
Definition: structtreemodel.cpp:55
Kasten2::StructTreeModel::onToolDataChange
void onToolDataChange(int row, void *data)
Definition: structtreemodel.cpp:255
Kasten2::StructTool::childAt
DataInformation * childAt(int idx) const
Definition: structtool.cpp:234
ArrayDataInformation::childData
virtual QVariant childData(int row, int column, int role) const
the data of child at index row.
Definition: arraydatainformation.cpp:121
QObject
DataInformationBase::asArray
ArrayDataInformation * asArray()
DataInformation::flags
virtual Qt::ItemFlags flags(int column, bool fileLoaded=true) const
Definition: datainformation.h:251
Kasten2::StructTreeModel::headerData
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const
Definition: structtreemodel.cpp:168
ArrayDataInformation
Definition: arraydatainformation.h:36
Kasten2::StructTreeModel::columnCount
int columnCount(const QModelIndex &parent=QModelIndex()) const
Definition: structtreemodel.cpp:105
DataInformation::childAt
virtual DataInformation * childAt(unsigned int) const =0
Definition: datainformation.h:268
Kasten2::StructTreeModel::parent
QModelIndex parent(const QModelIndex &index) const
Definition: structtreemodel.cpp:202
Kasten2::StructTool::setData
bool setData(const QVariant &value, int role, DataInformation *item, uint row)
Definition: structtool.cpp:166
Kasten2::StructTreeModel::index
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const
Definition: structtreemodel.cpp:178
Kasten2::StructTreeModel::onChildrenAboutToBeInserted
void onChildrenAboutToBeInserted(DataInformation *sender, uint startIndex, uint endIndex)
Definition: structtreemodel.cpp:92
Kasten2::StructTreeModel::StructTreeModel
StructTreeModel(StructTool *tool, QObject *parent=NULL)
Definition: structtreemodel.cpp:35
topleveldatainformation.h
DataInformationBase::isTopLevel
virtual bool isTopLevel() const =0
QAbstractItemModel
Kasten2::StructTreeModel::onToolDataClear
void onToolDataClear()
Definition: structtreemodel.cpp:260
structtreemodel.h
structtool.h
DataInformation::data
virtual QVariant data(int column, int role) const
get the necessary data (for the model)
Definition: datainformation.cpp:156
DataInformationBase::asDataInformation
DataInformation * asDataInformation()
Definition: datainformationbase.h:107
Kasten2::StructTreeModel::flags
Qt::ItemFlags flags(const QModelIndex &index) const
Definition: structtreemodel.cpp:160
arraydatainformation.h
Kasten2::StructTool::childCount
int childCount() const
Definition: structtool.cpp:229
DataInformation::parent
DataInformationBase * parent() const
Definition: datainformation.h:309
datainformationwithchildren.h
DataInformationBase
Definition: datainformationbase.h:44
Kasten2::StructTool::isFileLoaded
bool isFileLoaded() const
Definition: structtool.cpp:371
DataInformation::COLUMN_COUNT
Definition: datainformation.h:84
DataInformation::row
int row() const
Definition: datainformation.cpp:249
Kasten2::StructTool
Definition: structtool.h:49
DataInformationBase::isArray
virtual bool isArray() const
Definition: datainformationbase.cpp:35
Kasten2::StructTreeModel::hasChildren
virtual bool hasChildren(const QModelIndex &parent=QModelIndex()) const
Definition: structtreemodel.cpp:235
Kasten2::StructTreeModel::data
QVariant data(const QModelIndex &index, int role) const
Definition: structtreemodel.cpp:111
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 23:04:09 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

okteta

Skip menu "okteta"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdesdk API Reference

Skip menu "kdesdk API Reference"
  • kapptemplate
  • kcachegrind
  • kompare
  • lokalize
  • okteta
  • umbrello
  •   umbrello

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