WARNING: USE THIS SOFTWARE AT YOUR OWN RISK! THIS IS EXPERIMENTAL SOFTWARE NOT INTENDED FOR PRODUCTION USE! Zuble is currently an early stage prototype. As such Zuble is minimally tested and inherently unstable. It is provided for experimental, development, and demonstration purposes only. Zuble QML Types   |  Zuble C++ Classes   |  Zuble Overview
Zuble  0.1
Zuble Framework C++/QML extension API
ZblDataModel.cpp
Go to the documentation of this file.
1 /*
2  * Zuble - A run-time system for QML/Javascript applications
3  * Copyright (C) 2015 Bob Dinitto
4  *
5  * Filename: ZblDataModel.cpp
6  * Created on: 3/22/2015
7  * Author: Bob Dinitto
8  *
9  * Zuble is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
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, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  *
23  */
24 
25 #include "ZblDataModel.h"
26 #include "ZblTableHeaders.h"
27 #include "ZblException.h"
28 
29 namespace Zbl
30 {
31 
33  m_headers(NULL),
34  m_columnNames(NULL),
35  m_roleNames(NULL),
36  m_rootCell(new ZblTableCell())
37 
38 {
39 }
40 
42 {
43  if(m_headers)
44  delete m_headers;
45 
46  if(m_columnNames)
47  delete m_columnNames;
48 
49  if(m_roleNames)
50  delete m_roleNames;
51 
52  if(m_rootCell)
53  delete m_rootCell;
54 
55 }
56 
58 {
59  if(cell)
60  cell->data().setColumnCount(count);
61  else
62  rootCell().data().setColumnCount(count);
63 }
64 
66 {
67  if(cell)
68  return cell->constData().columnCount();
69  else
70  return constRootCell().constData().columnCount();
71 }
72 
73 int ZblDataModel::rowCount(const ZblTableCell *cell) const
74 {
75  if(cell)
76  return cell->constData().rowCount();
77  else
78  return constRootCell().constData().rowCount();
79 }
80 
82  int role,
83  int row,
84  int col,
85  const ZblTableCell* cell) const
86 {
87  if(cell)
88  return cell->constData().getValue(role, row, col);
89  else
90  return constRootCell().constData().getValue(role, row, col);
91 }
92 
94  int role,
95  int row,
96  int col,
97  const QVariant& value,
98  ZblTableCell* cell)
99 {
100  try
101  {
102  if(cell)
103  cell->data().putValue(role, row, col, value);
104  else
105  rootCell().data().putValue(role, row, col, value);
106  }
107  catch(ZblException x)
108  {
109  QString msg = QString("ZblDataModel::setData - exception: %1").arg(x.message());
110  qDebug("%s", msg.toUtf8().constData());
111 
112  }
113 
114 }
115 
117  QList<int> roles,
118  int startRow,
119  int col,
120  int rowCount,
121  ZblTableCell *cell) const
122 {
123  try
124  {
125  if(cell)
126  return cell->constData().getTableColumnRows(roles, startRow, col, rowCount);
127  else
128  return constRootCell().constData().getTableColumnRows(roles, startRow, col, rowCount);
129  }
130  catch(ZblException x)
131  {
132  QString msg = QString("ZblDataModel::getTableColumnRows - exception: %1").arg(x.message());
133  qDebug("%s", msg.toUtf8().constData());
134  }
135 }
136 
138  QVariant rows,
139  int startRow,
140  int col,
141  ZblTableCell *cell)
142 {
143  try
144  {
145  if(cell)
146  cell->data().putTableColumnRows(rows, startRow, col);
147  else
148  rootCell().data().putTableColumnRows(rows, startRow, col);
149  }
150  catch(ZblException x)
151  {
152  QString msg = QString("ZblDataModel::setData - exception: %1").arg(x.message());
153  qDebug("%s", msg.toUtf8().constData());
154 
155  }
156 
157 }
158 
159 
160 
161 
162 void ZblDataModel::appendRow(QMap<int, QList<QVariant> > data,
163  ZblTableCell* cell)
164 
165 {
166  try
167  {
168  if(cell)
169  cell->data().appendRow(data);
170  else
172  }
173  catch(ZblException x)
174  {
175  QString msg = QString("ZblDataModel::appendRow - exception: %1").arg(x.message());
176  qDebug("%s", msg.toUtf8().constData());
177 
178  }
179 }
180 
181 void ZblDataModel::appendRows(QList<QMap<int, QList<QVariant> > > data,
182  ZblTableCell* cell)
183 
184 {
185  if(cell)
186  cell->data().appendRows(data);
187  else
189 }
190 
191 void ZblDataModel::prependRow(QMap<int, QList<QVariant> > data,
192  ZblTableCell* cell)
193 
194 {
195  try
196  {
197  if(cell)
198  cell->data().prependRow(data);
199  else
201  }
202  catch(ZblException x)
203  {
204  QString msg = QString("ZblDataModel::prependRow - exception: %1").arg(x.message());
205  qDebug("%s", msg.toUtf8().constData());
206 
207  }
208 }
209 
210 void ZblDataModel::prependRows(QList<QMap<int, QList<QVariant> > > data,
211  ZblTableCell* cell)
212 
213 {
214  if(cell)
215  cell->data().prependRows(data);
216  else
218 }
219 
220 void ZblDataModel::removeRows(int row, int count,
221  ZblTableCell* cell)
222 {
223  if(cell)
224  cell->data().removeRows(row, count);
225  else
226  rootCell().data().removeRows(row, count);
227 }
228 
229 bool ZblDataModel::addRole(int roleNumber, ZblTableCell* cell)
230 {
231  if(cell)
232  return cell->data().addRole(roleNumber);
233  else
234  return rootCell().data().addRole(roleNumber);
235 }
236 
237 bool ZblDataModel::addRole(int roleNumber,
238  const QByteArray& roleName,
239  ZblTableCell* cell)
240 {
241  if(!m_roleNames)
242  m_roleNames = new zRoleNames();
243  else if(m_roleNames->contains(roleNumber))
244  return false;
245 
246  m_roleNames->insert(roleNumber, roleName);
247 
248  if(cell)
249  return cell->data().addRole(roleNumber);
250  else
251  return rootCell().data().addRole(roleNumber);
252 }
253 
254 
255 
256 bool ZblDataModel::hasRole(int role, const ZblTableCell* cell) const
257 {
258  if(cell)
259  return cell->constData().hasRole(role);
260  else
261  return constRootCell().constData().hasRole(role);
262 }
263 
264 QList<int> ZblDataModel::roles(const ZblTableCell* cell) const
265 {
266  if(cell)
267  return cell->constData().roles();
268  else
269  return constRootCell().constData().roles();
270 }
271 
272 int ZblDataModel::roleCount(const ZblTableCell* cell) const
273 {
274  if(cell)
275  return cell->constData().roleCount();
276  else
277  return constRootCell().constData().roleCount();
278 }
279 
281 {
282  if(cell)
283  return cell->constData().dumpData();
284  else
285  return constRootCell().constData().dumpData();
286 }
287 
288 bool ZblDataModel::copyColumn(int fromColumn, int toColumn, ZblTableCell* cell)
289 {
290  if(cell)
291  return cell->data().copyColumn(fromColumn, toColumn);
292  else
293  return rootCell().data().copyColumn(fromColumn, toColumn);
294 }
295 
296 
298 {
299  if(!m_headers)
300  m_headers = new ZblTableHeaders();
301 
302  return *m_headers;
303 }
304 
306 {
307  if(!m_columnNames)
308  m_columnNames = &rootCell();
309 
310  return *m_columnNames;
311 }
312 
314 {
315  if(!m_roleNames)
316  return zRoleNames();
317 
318  return *m_roleNames;
319 }
320 
322 {
323  if(cell)
324  return cell->data().clearData();
325  else
326  return rootCell().data().clearData();
327 }
328 
330 {
331  if(m_headers)
332  {
333  delete m_headers;
334  m_headers = NULL;
335  }
336 
337  if(m_columnNames)
338  {
339  delete m_columnNames;
340  m_columnNames = NULL;
341  }
342 
343  if(m_roleNames)
344  {
345  delete m_roleNames;
346  m_roleNames = NULL;
347  }
348 
349  if(m_rootCell)
350  {
351  delete m_rootCell;
352  m_rootCell = new ZblTableCell();
353  }
354 
355 }
356 
357 
358 
359 #if 0
360 void ZblDataModel::setRoleNames(const QHash<int, QByteArray> roleNames)
361 {
362  if(!m_roleNames)
363  m_roleNames = new zRoleNames(roleNames);
364  else
366 }
367 #endif
368 
370 {
371  return *m_rootCell;
372 }
373 
375 {
376  return *m_rootCell;
377 }
378 
380  int startRow,
381  const QString& text,
382  int column,
383  bool caseSensitive,
384  bool forwardDirection,
385  bool keySearch,
386  const ZblTableCell* cell) const
387 {
388  if(cell)
389  return cell->constData().findNextItemRow(
390  startRow, text, column, caseSensitive, forwardDirection, keySearch);
391  else
393  startRow, text, column, caseSensitive, forwardDirection, keySearch);
394 }
395 
396 
397 } // Zbl
ZblTableCell * m_columnNames
The cell containing column names if column names have been set. TBD: do we need column names...
Definition: ZblDataModel.h:268
void prependRows(zRoleRowList data)
Prepends a list of rows of role values to the table data.
void dumpModelData(const ZblTableCell *cell=NULL) const
Prints diagnostic information about the state of the contained data to debug output.
bool copyColumn(int fromColumn, int toColumn, ZblTableCell *cell=NULL)
Copies data between data model columns for all rows and roles in the model.
int roleCount() const
Determines the current number of roles in the data table.
int findNextItemRow(int startRow, const QString &text, int column=0, bool caseSensitive=true, bool forwardDirection=true, bool keySearch=false, const ZblTableCell *cell=NULL) const
Searches for the specified text from startIndex.
void putTableColumnRows(QVariant rows, int startRow, int col)
Replaces the current value for a set of rows for a single model column.
QVariant getTableColumnRows(QList< int > roles, int startRow, int col, int rowCount) const
Obtain data values for a set of rows from a single model column.
bool hasRole(int role, const ZblTableCell *cell=NULL) const
Determines if a specified role already exists in the data table.
void setColumnCount(int count, ZblTableCell *cell=NULL)
Sets the number of columns in the data table. This method fails if the data table contains rows...
void prependRows(QList< QMap< int, QList< QVariant > > > data, ZblTableCell *cell=NULL)
ZblTableHeaders & headers()
void dumpData() const
Prints diagnostic information about the state of the contained data to debug output.
zRoleNames * m_roleNames
A hash of role value/name pairs.
Definition: ZblDataModel.h:274
void prependRow(zRoleRow data)
Prepends a row of role values to the table data.
void clearData(ZblTableCell *cell=NULL)
Removes all data rows from the specified table cell.
QHash< int, QByteArray > zRoleNames
Definition: ZblDataModel.h:66
bool addRole(int role)
Adds the specified role to the data table. The table must not contain data rows when calling this met...
ZblTableCell & colNamesCell()
void removeRows(int row, int count)
Removes one or more rows from the table.
void setData(int role, int row, int col, const QVariant &value, ZblTableCell *cell=NULL)
int rowCount(const ZblTableCell *cell=NULL) const
const QString & message() const
ZblTableData & data()
void prependRow(QMap< int, QList< QVariant > > data, ZblTableCell *cell=NULL)
const ZblTableCell & constRootCell() const
int findNextItemRow(int startRow, const QString &text, int column=0, bool caseSensitive=true, bool forwardDirection=true, bool keySearch=false) const
int columnCount() const
Determines the number of columns in the data table.
bool hasRole(int role) const
Determines if a specified role already exists in the data table.
Definition: ZAndGate.cpp:6
ZblTableHeaders * m_headers
This object contains row and column header values for the model.
Definition: ZblDataModel.h:260
void clearData()
Removes all rows from the data table.
QList< int > roles() const
Determines which roles are in the data set.
A table cell object contains the data for its child cells and maintains the cell&#39;s positional relatio...
Definition: ZblTableCell.h:47
bool copyColumn(int fromColumn, int toColumn)
QVariant getValue(int role, int row, int col) const
Obtains the value of the specified cell and role. This is a blocking call.
const ZblTableData & constData() const
void appendRow(zRoleRow data)
Appends a row of role values to the table data.
void putTableColumnRows(QVariant rows, int startRow, int col, ZblTableCell *cell=NULL)
void clearRoles()
Removes all information from the model and returns the model to it&#39;s uninitialized state...
ZblTableCell * m_rootCell
The model&#39;s root cell contains the top level items. The root cell has no parent.
Definition: ZblDataModel.h:281
void setColumnCount(int count)
Sets the number of columns in the data table. This method fails if the data table contains rows...
ZblTableCell & rootCell()
void appendRows(QList< QMap< int, QList< QVariant > > > data, ZblTableCell *cell=NULL)
void putValue(int role, int row, int col, const QVariant value)
Replaces the current value of a cell&#39;s role. This is a blocking call. with the specified value...
int roleCount(const ZblTableCell *cell=NULL) const
Obtains the number of roles contained by the specified cell.
Zuble&#39;s Qt Exception Object.
Definition: ZblException.h:45
bool addRole(int roleNumber, ZblTableCell *cell=NULL)
Adds the specified role to the data table.
QList< int > roles(const ZblTableCell *cell=NULL) const
Determines which roles are in the data set.
void appendRows(zRoleRowList data)
Appends a list of rows of role values to the table data.
void appendRow(QMap< int, QList< QVariant > > data, ZblTableCell *cell=NULL)
zRoleNames roleNames() const
Obtains a hash that maps role numbers to role names.
void removeRows(int row, int count, ZblTableCell *cell=NULL)
Removes the specified data rows from the model.
int columnCount(const ZblTableCell *cell=NULL) const
int rowCount() const
Determines the number of rows in the data table.
QVariant getTableColumnRows(QList< int > roles, int startRow, int col, int rowCount, ZblTableCell *cell=NULL) const
Contains the data for data model column and row headers.
QVariant data(int role, int row, int col, const ZblTableCell *cell=NULL) const