NamedConfigDataModel.cpp
Go to the documentation of this file.
1 #include <DatabaseUtils/GUI/NamedConfigDataModel.h>
2 
3 using namespace dbutils::daqconfig;
4 
5 namespace dbutils {
6 namespace gui {
7 
8 NamedConfigDataModel::
9 NamedConfigDataModel(QObject *parent) : QAbstractItemModel(parent)
10 {
11 }
12 
15 {
16  QVector<QVariant> rootData;
17  rootData << "Configuration Name";
18  rootData << "DCM Operating Mode";
19  rootData << "Time";
20 
21  if (_rootItem.get() != 0) {
22  QModelIndex dummyIndex;
23  this->removeRows(0, _rootItem->childCount(), dummyIndex);
24  }
25  _rootItem.reset(new TreeItem(rootData));
26 
27  NamedConfigUtils::GlobalNameSpecMap::const_iterator iter;
28  NamedConfigUtils::GlobalNameSpecMap::const_iterator iterEnd =
29  nameSpecMap.end();
30  for (iter = nameSpecMap.begin(); iter != iterEnd; ++iter) {
31  int newChildIndex = _rootItem->childCount();
32  _rootItem->insertChildren(newChildIndex, 1, _rootItem->columnCount());
33  TreeItem* thisItem = _rootItem->child(newChildIndex);
34 
35  thisItem->setData(0, QVariant((*iter).first.getConfigName().c_str()));
36  thisItem->setData(1, QVariant((*iter).first.getDCMOpMode().c_str()));
37  thisItem->setData(2, QVariant(NamedConfigUtils::LATEST_TIME_STRING.c_str()));
38 
39  std::vector<GlobalNameSpec> detailsList = (*iter).second;
40  for (int idx = 0; idx < (int) detailsList.size(); ++idx) {
41  newChildIndex = thisItem->childCount();
42  thisItem->insertChildren(newChildIndex,1,thisItem->columnCount());
43  TreeItem* childItem = thisItem->child(newChildIndex);
44 
45  childItem->setData(0, QVariant(detailsList[idx].getConfigName().c_str()));
46  childItem->setData(1, QVariant(detailsList[idx].getDCMOpMode().c_str()));
47  childItem->setData(2, QVariant(detailsList[idx].getTimestamp().c_str()));
48  }
49  }
50 }
51 
52 int NamedConfigDataModel::columnCount(const QModelIndex & /* parent */) const
53 {
54  return _rootItem->columnCount();
55 }
56 
57 QVariant NamedConfigDataModel::data(const QModelIndex &index, int role) const
58 {
59  if (!index.isValid()) {return QVariant();}
60 
61  if (role != Qt::DisplayRole && role != Qt::EditRole)
62  return QVariant();
63 
64  TreeItem *item = _getItem(index);
65 
66  return item->data(index.column());
67 }
68 
69 QModelIndex NamedConfigDataModel::index(int row, int column,
70  const QModelIndex &parent) const
71 {
72  if (parent.isValid() && parent.column() != 0) {
73  return QModelIndex();
74  }
75 
76  TreeItem *parentItem = _getItem(parent);
77 
78  TreeItem *childItem = parentItem->child(row);
79  if (childItem)
80  return createIndex(row, column, childItem);
81  else
82  return QModelIndex();
83 }
84 
85 QModelIndex NamedConfigDataModel::parent(const QModelIndex &index) const
86 {
87  if (!index.isValid()) {return QModelIndex();}
88 
89  TreeItem *childItem = _getItem(index);
90  TreeItem *parentItem = childItem->parent();
91 
92  if (parentItem == _rootItem.get())
93  return QModelIndex();
94 
95  return createIndex(parentItem->childNumber(), 0, parentItem);
96 }
97 
98 int NamedConfigDataModel::rowCount(const QModelIndex &parent) const
99 {
100  TreeItem *parentItem = _getItem(parent);
101 
102  return parentItem->childCount();
103 }
104 
105 QVariant NamedConfigDataModel::
106 headerData(int section, Qt::Orientation orientation, int role) const
107 {
108  if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
109  return _rootItem->data(section);
110 
111  return QVariant();
112 }
113 
115  Qt::Orientation orientation,
116  const QVariant &value, int role)
117 {
118  if (role != Qt::EditRole || orientation != Qt::Horizontal)
119  return false;
120 
121  bool result = _rootItem->setData(section, value);
122 
123  if (result)
124  emit headerDataChanged(orientation, section, section);
125 
126  return result;
127 }
128 
130 {
131  if (index.isValid()) {
132  TreeItem *item = static_cast<TreeItem*>(index.internalPointer());
133  if (item) return item;
134  }
135  return _rootItem.get();
136 }
137 
138 } // end of namespace gui
139 } // end of namespace dbutils
virtual QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const
bool insertChildren(int position, int count, int columns)
Definition: TreeItem.cpp:94
virtual int columnCount(const QModelIndex &parent=QModelIndex()) const
int childCount() const
Definition: TreeItem.cpp:71
virtual bool setHeaderData(int section, Qt::Orientation orientation, const QVariant &value, int role=Qt::EditRole)
TreeItem * _getItem(const QModelIndex &index) const
bool setData(int column, const QVariant &value)
Definition: TreeItem.cpp:125
std::map< GlobalNameSpec, std::vector< GlobalNameSpec > > GlobalNameSpecMap
virtual void loadData(dbutils::daqconfig::NamedConfigUtils::GlobalNameSpecMap nameSpecMap)
int childNumber() const
Definition: TreeItem.cpp:76
int columnCount() const
Definition: TreeItem.cpp:84
const XML_Char int const XML_Char * value
Definition: expat.h:331
virtual QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const
boost::shared_ptr< TreeItem > _rootItem
TreeItem * parent()
Definition: TreeItem.cpp:108
virtual QModelIndex parent(const QModelIndex &index) const
virtual int rowCount(const QModelIndex &parent=QModelIndex()) const
TreeItem * child(int number)
Definition: TreeItem.cpp:66
QVariant data(int column) const
Definition: TreeItem.cpp:89
virtual QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const