ConfigDataTreeModel.h
Go to the documentation of this file.
1 /****************************************************************************
2  **
3  ** 03-Feb-2011, KAB: This file was copied from treemodel.h in the
4  ** Editable Tree Model Example from Nokia (copyright notice below).
5  ** See http://doc.qt.nokia.com/4.6/itemviews-editabletreemodel.html.
6  ** It has been modified to add local customizations.
7  **
8  **
9  ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
10  ** All rights reserved.
11  ** Contact: Nokia Corporation (qt-info@nokia.com)
12  **
13  ** This file is part of the examples of the Qt Toolkit.
14  **
15  ** $QT_BEGIN_LICENSE:LGPL$
16  ** Commercial Usage
17  ** Licensees holding valid Qt Commercial licenses may use this file in
18  ** accordance with the Qt Commercial License Agreement provided with the
19  ** Software or, alternatively, in accordance with the terms contained in
20  ** a written agreement between you and Nokia.
21  **
22  ** GNU Lesser General Public License Usage
23  ** Alternatively, this file may be used under the terms of the GNU Lesser
24  ** General Public License version 2.1 as published by the Free Software
25  ** Foundation and appearing in the file LICENSE.LGPL included in the
26  ** packaging of this file. Please review the following information to
27  ** ensure the GNU Lesser General Public License version 2.1 requirements
28  ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
29  **
30  ** In addition, as a special exception, Nokia gives you certain additional
31  ** rights. These rights are described in the Nokia Qt LGPL Exception
32  ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
33  **
34  ** GNU General Public License Usage
35  ** Alternatively, this file may be used under the terms of the GNU
36  ** General Public License version 3.0 as published by the Free Software
37  ** Foundation and appearing in the file LICENSE.GPL included in the
38  ** packaging of this file. Please review the following information to
39  ** ensure the GNU General Public License version 3.0 requirements will be
40  ** met: http://www.gnu.org/copyleft/gpl.html.
41  **
42  ** If you have questions regarding the use of this file, please contact
43  ** Nokia at qt-info@nokia.com.
44  ** $QT_END_LICENSE$
45  **
46  ****************************************************************************/
47 
48 #ifndef DBUTILS_GUI_CONFIGDATATREEMODEL_H
49 #define DBUTILS_GUI_CONFIGDATATREEMODEL_H
50 
51 #include <DatabaseUtils/GUI/TreeItem.h>
52 #include <DatabaseUtils/DAQConfig/ConfigDataTree.h>
53 #include <QtCore/QAbstractItemModel>
54 #include <QtCore/QModelIndex>
55 #include <QtCore/QVariant>
56 #include <boost/shared_ptr.hpp>
57 
58 namespace dbutils {
59 namespace gui {
60 
61 class ConfigDataTreeModel : public QAbstractItemModel
62 {
63  Q_OBJECT
64 
65  public:
66 
67  // ************************************************************
68  // constructor/destructor
69  // ************************************************************
70  ConfigDataTreeModel(QObject *parent = 0);
71  virtual ~ConfigDataTreeModel() {}
72 
73  // ************************************************************
74  // the following methods do *not* have implementations in
75  // QAbstractItemModel, so they need to be implemented here
76  // (or in subclasses)
77  // ************************************************************
78  virtual int columnCount(const QModelIndex &parent = QModelIndex()) const;
79  virtual QVariant data(const QModelIndex &index,
80  int role = Qt::DisplayRole) const;
81  virtual QModelIndex index(int row, int column,
82  const QModelIndex &parent = QModelIndex()) const;
83  virtual QModelIndex parent(const QModelIndex &index) const;
84  virtual int rowCount(const QModelIndex &parent = QModelIndex()) const;
85 
86  // ************************************************************
87  // the following methods *do* have implementations in QAbstractItemModel,
88  // but want to customize the implementation
89  // ************************************************************
90  virtual Qt::ItemFlags flags(const QModelIndex &index) const;
91  virtual QVariant headerData(int section, Qt::Orientation orientation,
92  int role = Qt::DisplayRole) const;
93  virtual bool insertRows(int position, int rows,
94  const QModelIndex &parent = QModelIndex());
95  virtual bool removeRows(int position, int rows,
96  const QModelIndex &parent = QModelIndex());
97  virtual bool setData(const QModelIndex &index, const QVariant &value,
98  int role = Qt::EditRole);
99  virtual bool setHeaderData(int section, Qt::Orientation orientation,
100  const QVariant &value, int role = Qt::EditRole);
101 
102  // ************************************************************
103  // the following methods are unique to this class and its children
104  // ************************************************************
105  QList<QString> allowsInsertBefore(const QModelIndex &index) const;
106  QList<QString> allowsInsertAfter(const QModelIndex &index) const;
107  QList<QString> allowsChildInsert(const QModelIndex &index) const;
108 
109  virtual bool loadData(boost::shared_ptr<daqconfig::ConfigDataTree> dataTree);
110  virtual bool hasChanges() const;
111  virtual bool saveChanges(const int64_t& subsystemConfigId);
112 
113  bool addChildRow(QModelIndex index, QString selectorColumnText,
114  int position = 0);
115  bool addRowBefore(const QModelIndex& index,
116  QString selectorColumnText);
117  bool addRowAfter(const QModelIndex& index,
118  QString selectorColumnText);
119 
120  bool selectorColumnNameForIndex(const QModelIndex& index,
121  std::string& selectorColumnName) const;
122  bool selectorColumnNameForChild(const QModelIndex& index,
123  std::string& selectorColumnName) const;
124  bool dataColumnNameForIndex(const QModelIndex& index,
125  std::string& dataColumnName) const;
126  bool dataColumnTypeForIndex(const QModelIndex& index,
127  std::string& dataColumnType) const;
128 
129  bool hasSelectors() const {return _selectorColumnNames.size() > 0;}
130  bool hasTreeStructure() const {return this->hasSelectors();}
131 
132  signals:
133 
134  void aChangeWasMade();
135  void dataWasSaved();
136 
137  private:
138 
139  // ************************************************************
140  // private methods
141  // ************************************************************
142  void _setupModelData();
143  int _populateSubTree(TreeItem* parent, int level,
144  std::vector<std::string> selectionList);
145  TreeItem *_getItem(const QModelIndex &index) const;
146  bool _getSelectorColumnName(const QModelIndex& index,
147  std::string& selectorColumnName,
148  int startingOffset = 0) const;
149  int _getParentCount(const QModelIndex& index) const;
150  void _getModifiedItems(TreeItem* baseItem,
151  std::vector<TreeItem*>& list) const;
152 
153  // ************************************************************
154  // private data
155  // ************************************************************
156  boost::shared_ptr<daqconfig::ConfigDataTree> _dataTree;
157  boost::shared_ptr<TreeItem> _rootItem;
158  std::vector<std::string> _selectorColumnNames;
159  std::vector<std::string> _dataColumnNames;
160  std::vector<std::string> _dataColumnTypes;
161  std::vector<std::string> _keysForDefaultValues;
162 
163  boost::shared_ptr<nova::database::Table> _dbTablePtr;
164 };
165 
166 } // end of namespace gui
167 } // end of namespace dbutils
168 
169 #endif
TreeItem * _getItem(const QModelIndex &index) const
bool addChildRow(QModelIndex index, QString selectorColumnText, int position=0)
QList< QString > allowsInsertBefore(const QModelIndex &index) const
bool dataColumnTypeForIndex(const QModelIndex &index, std::string &dataColumnType) const
bool addRowBefore(const QModelIndex &index, QString selectorColumnText)
virtual QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const
virtual bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole)
QList< QString > allowsChildInsert(const QModelIndex &index) const
bool dataColumnNameForIndex(const QModelIndex &index, std::string &dataColumnName) const
int _populateSubTree(TreeItem *parent, int level, std::vector< std::string > selectionList)
std::vector< std::string > _keysForDefaultValues
std::vector< std::string > _dataColumnNames
QList< QString > allowsInsertAfter(const QModelIndex &index) const
std::vector< std::string > _dataColumnTypes
boost::shared_ptr< daqconfig::ConfigDataTree > _dataTree
virtual int rowCount(const QModelIndex &parent=QModelIndex()) const
const XML_Char int const XML_Char * value
Definition: expat.h:331
virtual bool saveChanges(const int64_t &subsystemConfigId)
bool _getSelectorColumnName(const QModelIndex &index, std::string &selectorColumnName, int startingOffset=0) const
bool addRowAfter(const QModelIndex &index, QString selectorColumnText)
virtual Qt::ItemFlags flags(const QModelIndex &index) const
virtual bool loadData(boost::shared_ptr< daqconfig::ConfigDataTree > dataTree)
virtual bool removeRows(int position, int rows, const QModelIndex &parent=QModelIndex())
virtual QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const
virtual QModelIndex parent(const QModelIndex &index) const
virtual QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const
boost::shared_ptr< nova::database::Table > _dbTablePtr
virtual bool insertRows(int position, int rows, const QModelIndex &parent=QModelIndex())
virtual int columnCount(const QModelIndex &parent=QModelIndex()) const
std::vector< std::string > _selectorColumnNames
virtual bool setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role=Qt::EditRole)
void _getModifiedItems(TreeItem *baseItem, std::vector< TreeItem * > &list) const
int _getParentCount(const QModelIndex &index) const
boost::shared_ptr< TreeItem > _rootItem
bool selectorColumnNameForChild(const QModelIndex &index, std::string &selectorColumnName) const
bool selectorColumnNameForIndex(const QModelIndex &index, std::string &selectorColumnName) const
enum BeamMode string