• 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
  • datatypes
datainformationwithchildren.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, 2012 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 #include "datainformationwithchildren.h"
23 #include "topleveldatainformation.h"
24 #include "../parsers/scriptvalueconverter.h"
25 #include "../script/scriptutils.h"
26 #include "../script/scripthandlerinfo.h"
27 #include "../script/classes/structunionscriptclass.h"
28 #include "../script/scriptlogger.h"
29 
30 #include <KLineEdit>
31 #include <KLocalizedString>
32 #include <QScriptEngine>
33 
34 DataInformation* DataInformationWithChildren::childAt(unsigned int idx) const
35 {
36  if (idx >= (unsigned) mChildren.size())
37  return 0;
38  return mChildren[idx];
39 }
40 
41 bool DataInformationWithChildren::setData(const QVariant&, Okteta::AbstractByteArrayModel*,
42  Okteta::Address, BitCount64, quint8)
43 {
44  Q_ASSERT_X(false, "DataInformationWithChildren::setData()", "this should never be called");
45  return false;
46 }
47 
48 DataInformationWithChildren::~DataInformationWithChildren()
49 {
50  qDeleteAll(mChildren);
51 }
52 
53 DataInformationWithChildren::DataInformationWithChildren(const QString& name,
54  const QVector<DataInformation*>& children, DataInformation* parent)
55  : DataInformation(name, parent), mChildren(children)
56 {
57  for (int i = 0; i < mChildren.size(); ++i)
58  mChildren.at(i)->setParent(this);
59 }
60 
61 DataInformationWithChildren::DataInformationWithChildren(const DataInformationWithChildren& d)
62  : DataInformation(d), mChildren(cloneList(d.mChildren, this))
63 {
64 }
65 
66 QWidget* DataInformationWithChildren::createEditWidget(QWidget* parent) const
67 {
68  Q_ASSERT(false);
69  return new KLineEdit(parent);
70 }
71 
72 QVariant DataInformationWithChildren::dataFromWidget(const QWidget* w) const
73 {
74  Q_UNUSED(w);
75  Q_ASSERT(false);
76  return QVariant();
77 }
78 
79 void DataInformationWithChildren::setWidgetData(QWidget* w) const
80 {
81  Q_ASSERT(false);
82  Q_UNUSED(w)
83 }
84 
85 BitCount32 DataInformationWithChildren::size() const
86 {
87  BitCount32 size = 0;
88  for (unsigned int i = 0; i < childCount(); ++i)
89  {
90  size += childAt(i)->size();
91  }
92  return size;
93 }
94 
95 void DataInformationWithChildren::resetValidationState()
96 {
97  DataInformation::resetValidationState();
98  for (int i = 0; i < mChildren.size(); ++i)
99  {
100  mChildren.at(i)->resetValidationState();
101  }
102 }
103 
104 void DataInformationWithChildren::calculateValidationState()
105 {
106  if (childCount() > 0)
107  {
108  bool hasValidatedChildren = false;
109  bool allChildrenValid = true;
110  for (uint i = 0; i < childCount(); ++i)
111  {
112  DataInformation* child = childAt(i);
113  DataInformationWithChildren* childWithChildren =
114  dynamic_cast<DataInformationWithChildren*>(child);
115  if (childWithChildren)
116  childWithChildren->calculateValidationState();
117  //first make sure the child item validation state has been set
118  if (child->hasBeenValidated())
119  {
120  hasValidatedChildren = true;
121  if (!child->validationSuccessful())
122  {
123  allChildrenValid = false;
124  break; //one is invalid -> whole structure is invalid
125  }
126  }
127  }
128  if (hasValidatedChildren)
129  {
130  mValidationSuccessful = allChildrenValid;
131  }
132  }
133 }
134 
135 void DataInformationWithChildren::setChildren(const QVector<DataInformation*>& newChildren)
136 {
137  //since we are replacing the children and the first few may be different emit
138  //change to length zero and then to new length so that model gets updated correctly
139  uint numChildren = childCount();
140  topLevelDataInformation()->_childCountAboutToChange(this, numChildren, 0);
141  qDeleteAll(mChildren);
142  mChildren.clear();
143  topLevelDataInformation()->_childCountChanged(this, numChildren, 0);
144 
145  const uint count = newChildren.size();
146  topLevelDataInformation()->_childCountAboutToChange(this, 0, count);
147  mChildren = newChildren;
148  for (int i = 0; i < mChildren.size(); ++i)
149  mChildren.at(i)->setParent(this);
150  topLevelDataInformation()->_childCountChanged(this, 0, count);
151 }
152 
153 void DataInformationWithChildren::setChildren(QScriptValue children)
154 {
155  if (children.isNull() || children.isUndefined())
156  {
157  logError() << "attempting to set children to null/undefined.";
158  return;
159  }
160  QVector<DataInformation*> convertedVals =
161  ScriptValueConverter::convertValues(children, topLevelDataInformation()->logger());
162  setChildren(convertedVals);
163 }
164 
165 int DataInformationWithChildren::indexOf(const DataInformation* const data) const
166 {
167  const int size = mChildren.size();
168  for (int i = 0; i < size; ++i)
169  {
170  if (mChildren.at(i) == data)
171  {
172  return i;
173  }
174  }
175  Q_ASSERT(false); //should never reach this
176  return -1;
177 }
178 
179 QVariant DataInformationWithChildren::childData(int row, int column, int role) const
180 {
181  Q_ASSERT(row >= 0 && row < mChildren.size());
182  //just delegate to child
183  return mChildren.at(row)->data(column, role);
184 }
185 
186 void DataInformationWithChildren::appendChild(DataInformation* newChild, bool emitSignal)
187 {
188  if (emitSignal)
189  topLevelDataInformation()->_childCountAboutToChange(this, mChildren.size(), mChildren.size() + 1);
190  newChild->setParent(this);
191  mChildren.append(newChild);
192  if (emitSignal)
193  topLevelDataInformation()->_childCountChanged(this, mChildren.size(), mChildren.size() + 1);
194 }
195 
196 void DataInformationWithChildren::appendChildren(const QVector<DataInformation*>& newChildren, bool emitSignal)
197 {
198  if (newChildren.isEmpty())
199  return;
200  const int added = newChildren.size();
201  if (emitSignal)
202  topLevelDataInformation()->_childCountAboutToChange(this, mChildren.size(), mChildren.size() + added);
203  for (int i = 0; i < newChildren.size(); ++i)
204  newChildren.at(i)->setParent(this);
205  mChildren << newChildren;
206  if (emitSignal)
207  topLevelDataInformation()->_childCountChanged(this, mChildren.size(), mChildren.size() + added);
208 }
209 
210 bool DataInformationWithChildren::replaceChildAt(unsigned int index, DataInformation* newChild)
211 {
212  Q_ASSERT(index < uint(mChildren.size()));
213  Q_CHECK_PTR(newChild);
214  if (index >= uint(mChildren.size()))
215  return false;
216 
217  delete mChildren.at(index);
218  mChildren[index] = newChild;
219  return true;
220 }
221 
222 QScriptClass* DataInformationWithChildren::scriptClass(ScriptHandlerInfo* handlerInfo) const
223 {
224  return handlerInfo->mStructUnionClass.data();
225 }
226 
227 QString DataInformationWithChildren::tooltipString() const
228 {
229  QString valueStr = mWasAbleToRead ? valueString() : eofReachedData(Qt::DisplayRole).toString();
230  if (mHasBeenValidated && !mValidationSuccessful)
231  {
232  QString validationMsg = validationError();
233  if (validationMsg.isEmpty())
234  {
235  validationMsg = i18nc("not all values in this structure"
236  " are as they should be", "Validation failed.");
237  }
238  else
239  {
240  validationMsg = i18nc("not all values in this structure"
241  " are as they should be", "Validation failed: \"%1\"", validationMsg);
242  }
243  return i18np("Name: %2\nValue: %3\n\nType: %4\nSize: %5 (%1 child)\n\n %6",
244  "Name: %2\nValue: %3\n\nType: %4\nSize: %5 (%1 children)\n\n %6",
245  childCount(), name(), valueStr, typeName(), sizeString(), validationMsg);
246  }
247  else
248  {
249  return i18np("Name: %2\nValue: %3\n\nType: %4\nSize: %5 (%1 child)",
250  "Name: %2\nValue: %3\n\nType: %4\nSize: %5 (%1 children)",
251  childCount(), name(), valueStr, typeName(), sizeString());
252  }
253 }
254 
255 QVector<DataInformation*> DataInformationWithChildren::cloneList(const QVector<DataInformation*>& other,
256  DataInformation* parent)
257 {
258  int count = other.count();
259  QVector<DataInformation*> ret;
260  ret.reserve(count);
261  for (int i = 0; i < count; ++i)
262  {
263  DataInformation* dat = other.at(i);
264  DataInformation* newChild = dat->clone();
265  newChild->setParent(parent);
266  ret.append(newChild);
267  }
268  return ret;
269 }
DataInformation::valueString
QString valueString() const
by default just returns an empty QString
Definition: datainformation.h:414
DataInformation
Interface that must be implemented by all datatypes.
Definition: datainformation.h:67
Okteta::Address
qint32 Address
Definition: address.h:34
Okteta::AbstractByteArrayModel
could it be useful to hide the data access behind an iterator? * class KDataBufferIterator { public: ...
Definition: abstractbytearraymodel.h:79
DataInformation::name
QString name() const
Definition: datainformation.h:258
DataInformation::topLevelDataInformation
TopLevelDataInformation * topLevelDataInformation() const
Definition: datainformation.cpp:240
DataInformationWithChildren::childCount
virtual unsigned int childCount() const
Definition: datainformationwithchildren.h:77
QScriptClass
DataInformation::mValidationSuccessful
bool mValidationSuccessful
Definition: datainformation.h:240
TopLevelDataInformation::_childCountAboutToChange
void _childCountAboutToChange(DataInformation *sender, uint oldCount, uint newCount)
Definition: topleveldatainformation.h:184
BitCount64
quint64 BitCount64
Definition: datainformationbase.h:42
TopLevelDataInformation::_childCountChanged
void _childCountChanged(DataInformation *sender, uint oldCount, uint newCount)
Definition: topleveldatainformation.h:192
DataInformationWithChildren::~DataInformationWithChildren
virtual ~DataInformationWithChildren()
Definition: datainformationwithchildren.cpp:48
QWidget
DataInformation::typeName
QString typeName() const
Definition: datainformation.h:406
ScriptValueConverter::convertValues
QVector< DataInformation * > convertValues(const QScriptValue &value, ScriptLogger *logger, DataInformation *parent)
If the value is a list of elements or an object with many elements.
Definition: scriptvalueconverter.cpp:44
DataInformationWithChildren::mChildren
QVector< DataInformation * > mChildren
Definition: datainformationwithchildren.h:32
DataInformationWithChildren::appendChild
void appendChild(DataInformation *child, bool emitSignal=true)
Takes ownership!
Definition: datainformationwithchildren.cpp:186
QVector< DataInformation * >
DataInformation::logger
ScriptLogger * logger() const
Definition: datainformation.cpp:134
BitCount32
quint32 BitCount32
Definition: datainformationbase.h:37
ScriptHandlerInfo::mStructUnionClass
QScopedPointer< StructUnionScriptClass > mStructUnionClass
Definition: scripthandlerinfo.h:53
DataInformationWithChildren::setChildren
void setChildren(const QVector< DataInformation * > &newChildren)
Definition: datainformationwithchildren.cpp:135
DataInformationWithChildren
Definition: datainformationwithchildren.h:29
DataInformationWithChildren::scriptClass
virtual QScriptClass * scriptClass(ScriptHandlerInfo *handlerInfo) const
So that this object can be wrapped by the correct javascript object.
Definition: datainformationwithchildren.cpp:222
DataInformationWithChildren::childAt
virtual DataInformation * childAt(unsigned int index) const
Definition: datainformationwithchildren.cpp:34
DataInformation::sizeString
virtual QString sizeString() const
needs to be virtual for bitfields
Definition: datainformation.cpp:56
DataInformation::mWasAbleToRead
bool mWasAbleToRead
Definition: datainformation.h:243
DataInformationWithChildren::indexOf
virtual int indexOf(const DataInformation *const data) const
Find the index of a DataInformation in this object, needed to calculate the row.
Definition: datainformationwithchildren.cpp:165
DataInformation::setParent
void setParent(DataInformationBase *newParent)
Definition: datainformation.h:303
DataInformationWithChildren::setData
virtual bool setData(const QVariant &value, Okteta::AbstractByteArrayModel *out, Okteta::Address address, BitCount64 bitsRemaining, quint8 bitOffset)
Writes the current data contained in this object to out.
Definition: datainformationwithchildren.cpp:41
DataInformationWithChildren::createEditWidget
virtual QWidget * createEditWidget(QWidget *parent) const
create a QWidget for the QItemDelegate
Definition: datainformationwithchildren.cpp:66
DataInformationWithChildren::childData
virtual QVariant childData(int row, int column, int role) const
Definition: datainformationwithchildren.cpp:179
topleveldatainformation.h
DataInformationWithChildren::tooltipString
virtual QString tooltipString() const
Definition: datainformationwithchildren.cpp:227
DataInformation::child
virtual DataInformation * child(const QString &name) const
Looks for a child of this object with given name.
Definition: datainformation.cpp:228
DataInformationWithChildren::DataInformationWithChildren
DataInformationWithChildren(const DataInformationWithChildren &d)
Definition: datainformationwithchildren.cpp:61
DataInformationWithChildren::appendChildren
void appendChildren(const QVector< DataInformation * > &newChildren, bool emitSignal=true)
Takes ownership of all elements.
Definition: datainformationwithchildren.cpp:196
DataInformationWithChildren::size
virtual BitCount32 size() const
the size in bits of this element
Definition: datainformationwithchildren.cpp:85
DataInformationWithChildren::replaceChildAt
virtual bool replaceChildAt(unsigned int index, DataInformation *newChild)
replaces child at index with newChild.
Definition: datainformationwithchildren.cpp:210
DataInformation::data
virtual QVariant data(int column, int role) const
get the necessary data (for the model)
Definition: datainformation.cpp:156
DataInformation::hasBeenValidated
bool hasBeenValidated() const
Definition: datainformation.h:349
KLineEdit
DataInformationWithChildren::resetValidationState
virtual void resetValidationState()
Definition: datainformationwithchildren.cpp:95
DataInformation::validationError
QString validationError() const
Definition: datainformation.h:384
ScriptHandlerInfo
Definition: scripthandlerinfo.h:39
DataInformation::resetValidationState
virtual void resetValidationState()
Definition: datainformation.cpp:111
DataInformation::validationSuccessful
bool validationSuccessful() const
Definition: datainformation.h:344
datainformationwithchildren.h
DataInformation::clone
virtual DataInformation * clone() const =0
DataInformationWithChildren::dataFromWidget
virtual QVariant dataFromWidget(const QWidget *w) const
get the needed data from the widget
Definition: datainformationwithchildren.cpp:72
DataInformationWithChildren::cloneList
static QVector< DataInformation * > cloneList(const QVector< DataInformation * > &other, DataInformation *parent)
Definition: datainformationwithchildren.cpp:255
DataInformationWithChildren::calculateValidationState
virtual void calculateValidationState()
Definition: datainformationwithchildren.cpp:104
DataInformation::mHasBeenValidated
bool mHasBeenValidated
Definition: datainformation.h:241
DataInformation::size
virtual BitCount32 size() const =0
the size in bits of this element
DataInformation::eofReachedData
static QVariant eofReachedData(int role)
Definition: datainformation.cpp:189
DataInformationWithChildren::setWidgetData
virtual void setWidgetData(QWidget *w) const
initialize the delegate widget with the correct data
Definition: datainformationwithchildren.cpp:79
DataInformation::logError
QDebug logError() const
just a shorthand for logger->error(this)
Definition: datainformation.h:324
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 23:04:08 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