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
ZTableModel.h
Go to the documentation of this file.
1 /*
2  * Zuble - A simple QML/Javascript application framework
3  * Copyright (C) 2014 Bob Dinitto
4  *
5  * Filename: ZTableModel.h
6  * Created on: 11/9/2014
7  * Author: bob
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 #ifndef ZTABLEMODEL_H
26 #define ZTABLEMODEL_H
27 
28 
29 //#include <QObject>
30 
31 #include <QAbstractTableModel>
32 #include "ZblDataModel.h"
33 #include "zglobal.h"
34 #include <QJsonObject>
35 #include <QItemSelection>
36 
37 
38 namespace Zbl
39 {
40 
44 typedef QHash<int, QByteArray> ZRoleNames;
45 
46 #if 0
47 
50 //typedef QMap<QString, QVariant> ZRoleNameMap;
51 #endif
52 
57 typedef QList<QVariant> ZDataRow;
58 
63 typedef QMap<int, QList<QVariant> > ZRoleRow;
64 
69 typedef QList<QMap<int, QList<QVariant> > > ZRoleRowList;
70 
75 typedef QList<QMap<int, QVariant> > ZTableRowList;
76 
77 
96 class ZTableModel : public QAbstractTableModel
97 {
98  //typedef QList<QList<QVariant> > tableData;
99 
100  friend class ZblLogManager;
101  //friend class ZblLogWorker;
102 
103  Q_OBJECT
104 public:
105 
106  explicit ZTableModel(QObject *parent = 0);
107 
112  static void registerType();
113 
119  Q_PROPERTY(int userRole READ getUserRole)
120 
121 
125  Q_PROPERTY(int roleCount READ getRoleCount)
126 
132  int getUserRole();
133 
139  int getRoleCount() const;
140 
141 
146  virtual int rowCount(const QModelIndex & parent = QModelIndex()) const;
147 
152  virtual int columnCount(const QModelIndex & parent = QModelIndex()) const;
153 
157  virtual QVariant data(
158  const QModelIndex & index,
159  int role = Qt::DisplayRole) const;
160 
161 
162 
166  virtual QHash<int, QByteArray> roleNames() const;
167 
168 
169 
170  // begin javascript access methods
171 
177  Q_INVOKABLE void dumpModelData() const;
178 
183  Q_INVOKABLE void putValue(int role, int row, const QVariant& value);
184 
189  Q_INVOKABLE void putValue(int role, int row, int column, const QVariant& value);
190 
198  Q_INVOKABLE QVariant getValue(int role, int row);
199 
213  Q_INVOKABLE QVariant getValue(int role, int row, int column);
214 
235  Q_INVOKABLE QVariant getTableColumnRows(
236  QList<int> roles,
237  int startRow,
238  int col,
239  int rowCount);
240 
259  Q_INVOKABLE void putTableColumnRows(
260  QVariant rows,
261  int startRow,
262  int col);
263 
285  Q_INVOKABLE void sendTableColumnRows(
286  QVariant rows,
287  int startRow,
288  int col);
289 
290 
291  Q_INVOKABLE void appendRow(QVariant data);
292 
293  Q_INVOKABLE void appendRows(QVariant data);
294 
295  Q_INVOKABLE void appendRow(ZRoleRow roleRow);
296 
297  Q_INVOKABLE int appendRowIndex(ZRoleRow roleRow);
298 
299  Q_INVOKABLE void appendRows(ZRoleRowList roleRowList);
300 
301  Q_INVOKABLE void prependRow(QVariant data);
302 
303  Q_INVOKABLE void prependRows(QVariant data);
304 
305  Q_INVOKABLE void prependRow(ZRoleRow roleRow);
306 
307  Q_INVOKABLE void prependRows(ZRoleRowList roleRowList);
308 
309  Q_INVOKABLE void removeRow(int row);
310 
311  Q_INVOKABLE void removeRows(int row, int count);
312 
313  Q_INVOKABLE void truncate(int rowCount, bool removeFromFront = false);
314 
315  Q_INVOKABLE bool copyColumn(int fromColumn, int toColumn);
316 
317 
335  Q_INVOKABLE void appendCells(QVariant data, bool truncateModel=false);
336 
347  Q_INVOKABLE void prependCells(QVariant data, bool truncateModel=false);
348 
354  Q_INVOKABLE int modelRowCount() const;
355 
371  Q_INVOKABLE void setColumnCount(int count);
372 
382  Q_INVOKABLE int modelColumnCount() const;
383 
384 
395  Q_INVOKABLE bool addRole(int roleNumber);
396 
408  Q_INVOKABLE bool addRole(int roleNumber, const QString& roleName);
409 
417  Q_INVOKABLE QList<int> roles() const;
418 
425  Q_INVOKABLE QVariantMap roleMap() const;
426 
427 
431  Q_INVOKABLE void clearRoles();
432 
437  Q_INVOKABLE void clearData();
438 
439  //Q_INVOKABLE QVariant findItems(const QString & text, Qt::MatchFlags flags = Qt::MatchExactly, int column = 0) const;
440 
441  //QVariant findItems(const QString & text, Qt::MatchFlags flags = Qt::MatchExactly, int column = 0) const;
442 
443 
460  Q_INVOKABLE int findNextItemRow(
461  int startRow,
462  const QString& text,
463  int column = 0,
464  bool caseSensitive = true,
465  bool forwardDirection = true,
466  bool keySearch = false
467  ) const;
468 
469 
478  Q_INVOKABLE void appendMissingRow(
479  ZRoleRow roleRow,
480  const QString& keyText,
481  int keyColumn = 0);
490  Q_INVOKABLE void appendMissingRow(
491  QVariant roleRow,
492  const QString& keyText,
493  int keyColumn = 0);
494 
525  Q_INVOKABLE bool writeData(QVariant jsonData);
526 
536  Q_INVOKABLE QVariant readData() const;
537 
569  Q_INVOKABLE void putJsonData(QJsonObject data/*, bool putRoles=true*/);
570 
578  Q_INVOKABLE QJsonObject getJsonData() const;
579 
587  Q_INVOKABLE void sync();
588 
589 
590 signals:
591 
598  void tableModified(int dataRole, int rowNumber);
599 
605  void tableModelReset();
606 
607 public slots:
608 
609  void moveModelToThread(QThread* targetThread);
610 
616  void invalidateModel();
617 
618 protected:
619 
620  static void registerLogCategory();
621 
622 
624  QVariant rows,
625  int startRow,
626  int col,
627  bool asynchronous);
628 
640  Q_INVOKABLE void mergeCells(QVariant data, bool front=true, bool truncateModel=true);
641 
642 
643 
644  ZRoleRow coalesceCellRow(const ZDataRow cells,
645  int startIndex,
646  int columnCount,
647  const QList<int> roles,
648  int& nextIndex);
649 
650  ZRoleRowList coalesceCellRows(
651  const ZDataRow cells,
652  int columnCount,
653  const QList<int> roles);
654 
656 
662 
664 
665 };
666 
667 } // Zbl
668 
669 Q_DECLARE_METATYPE(Zbl::ZRoleNames)
670 //Q_DECLARE_METATYPE(Zbl::ZRoleNameMap)
671 Q_DECLARE_METATYPE(Zbl::ZDataRow)
672 Q_DECLARE_METATYPE(Zbl::ZRoleRow)
673 Q_DECLARE_METATYPE(Zbl::ZRoleRowList)
674 //Q_DECLARE_METATYPE(QQmlChangeSet)
675 
676 
677 #endif // ZTABLEMODEL_H
The Zuble Log Manager is a singleton object that controls logging in Zuble applications.
Definition: ZblLogManager.h:70
void tableModified(int dataRole, int rowNumber)
Sent when a table cell has been modified.
Q_INVOKABLE void appendMissingRow(ZRoleRow roleRow, const QString &keyText, int keyColumn=0)
Appends the specified row if no rows contain the specified text in the specified column. Otherwise does nothing.
Q_INVOKABLE void prependCells(QVariant data, bool truncateModel=false)
Asynchronously converts an array of cell data into one or more rows of model data and prepends it to ...
virtual int rowCount(const QModelIndex &parent=QModelIndex()) const
QAbstractTableModel override.
Definition: ZTableModel.cpp:65
Q_INVOKABLE void appendCells(QVariant data, bool truncateModel=false)
Asynchronously converts an array of cell data into one or more rows of model data and appends it to t...
Q_INVOKABLE QVariant readData() const
Returns a JSON object containing a copy of the table data. Data in the table is unaffected. Data is formatted as for ZTableModel::writeData(QVariant jsonData)
#define ZBL_DECLARE_LOGGED_OBJECT
Definition: zglobal.h:94
virtual QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const
QAbstractTableModel override.
Definition: ZTableModel.cpp:93
Q_INVOKABLE void clearData()
Removes all data from the model. Roles and role names remain intact.
int getUserRole()
Returns the value of Qt::UserRole. ie: the first role number available for user defined roles...
Definition: ZTableModel.cpp:58
virtual int columnCount(const QModelIndex &parent=QModelIndex()) const
QAbstractTableModel override.
Definition: ZTableModel.cpp:81
Q_INVOKABLE int appendRowIndex(ZRoleRow roleRow)
void moveModelToThread(QThread *targetThread)
Q_INVOKABLE QJsonObject getJsonData() const
Copies data from the table model into a newly created JSON object and returns it. ...
Q_INVOKABLE void putValue(int role, int row, const QVariant &value)
Sets the value of the specified cell in the data set.
Q_INVOKABLE void prependRow(QVariant data)
int getRoleCount() const
Returns the number of roles defined for this model.
Q_INVOKABLE void truncate(int rowCount, bool removeFromFront=false)
Q_INVOKABLE QVariantMap roleMap() const
Determines which roles are in the data set. This method may block the current thread.
void invalidateModel()
Resets the model.
Q_INVOKABLE void dumpModelData() const
dumps diagnostic information about the model to the debug output.
ZRoleRowList coalesceCellRows(const ZDataRow cells, int columnCount, const QList< int > roles)
Q_INVOKABLE QList< int > roles() const
Determines which roles are in the data set. This method may block the current thread.
Q_INVOKABLE QVariant getValue(int role, int row)
Obtains the value of the specified data cell.
Q_INVOKABLE int findNextItemRow(int startRow, const QString &text, int column=0, bool caseSensitive=true, bool forwardDirection=true, bool keySearch=false) const
Searches for the specified text from startIndex.
int m_defaultRole
The role used for data access methods that don&#39;t specify a role.
Definition: ZTableModel.h:661
Q_INVOKABLE void appendRows(QVariant data)
Definition: ZAndGate.cpp:6
QMap< int, QList< QVariant > > ZRoleRow
Represents a single row (or column for column headers) of data cell values for multiple roles...
Definition: ZTableModel.h:63
static void registerType()
Registers ZTableModel as a QML type.
Definition: ZTableModel.cpp:52
ZTableModel(QObject *parent=0)
Definition: ZTableModel.cpp:41
This two dimensional table model is used to store and manipulate data.
Definition: ZTableModel.h:96
Q_INVOKABLE void clearRoles()
Removes all roles, role names and data from the model.
Q_INVOKABLE void putJsonData(QJsonObject data)
Writes data from the specified JSON object into the data table. Previous contents of the data table a...
QList< QMap< int, QVariant > > ZTableRowList
Represents multiple rows of data cell values for multiple roles for a single column.
Definition: ZTableModel.h:75
int userRole
Returns the value of Qt::UserRole to javascript client code. ie: the first role number available for ...
Definition: ZTableModel.h:119
Q_INVOKABLE bool copyColumn(int fromColumn, int toColumn)
ZRoleRow coalesceCellRow(const ZDataRow cells, int startIndex, int columnCount, const QList< int > roles, int &nextIndex)
QHash< int, QByteArray > ZRoleNames
Maps role numbers to role names as a hash table object.
Definition: ZTableModel.h:44
Q_INVOKABLE bool writeData(QVariant jsonData)
Writes data from the specified JSON object into the data table. Previous data in the table is discard...
Q_INVOKABLE void setColumnCount(int count)
Sets the number of columns the table will contain. This method may block the current thread...
Q_INVOKABLE int modelRowCount() const
Returns the number of rows in the data set. This method may block the current thread.
Q_INVOKABLE QVariant getTableColumnRows(QList< int > roles, int startRow, int col, int rowCount)
Obtain data values for a set of rows from a single model column.
Q_INVOKABLE void putTableColumnRows(QVariant rows, int startRow, int col)
Replaces the current value for a set of rows for a single model column.
Q_INVOKABLE void mergeCells(QVariant data, bool front=true, bool truncateModel=true)
Converts an array of cell data into one or more rows of model data and either appends or prepends it ...
int roleCount
Returns the number of roles defined for this model.
Definition: ZTableModel.h:125
This class is an implementation of an N+1 dimensional data store suitable for use as a generic QAbstr...
Definition: ZblDataModel.h:62
static void registerLogCategory()
Definition: ZTableModel.cpp:47
Q_INVOKABLE bool addRole(int roleNumber)
Adds the specified role to the data model. This method may block the current thread.
Q_INVOKABLE void sync()
Blocks thread until model&#39;s event queue has been processed.
QList< QMap< int, QList< QVariant > > > ZRoleRowList
Represents multiple rows of data cell values for multiple roles for multiple columns.
Definition: ZTableModel.h:69
Q_INVOKABLE void sendTableColumnRows(QVariant rows, int startRow, int col)
Asynchronously replaces the current value for a set of rows for a single model column.
void insertTableColumnRows(QVariant rows, int startRow, int col, bool asynchronous)
Q_INVOKABLE void removeRows(int row, int count)
Q_INVOKABLE void removeRow(int row)
virtual QHash< int, QByteArray > roleNames() const
QAbstractTableModel override.
void tableModelReset()
Sent when the table model has been reset.
QList< QVariant > ZDataRow
Represents a single row (or column for column headers) of data cell values for a single role...
Definition: ZTableModel.h:57
ZblDataModel m_model
Definition: ZTableModel.h:655
Q_INVOKABLE void appendRow(QVariant data)
Q_INVOKABLE void prependRows(QVariant data)
Q_INVOKABLE int modelColumnCount() const
Returns the number of columns in the data set. This method may block the current thread.