Engauge Digitizer  2
CurveNameList.cpp
1 #include "CurveNameListEntry.h"
2 #include "CurveNameList.h"
3 #include "DocumentSerialize.h"
4 #include "EngaugeAssert.h"
5 #include "Logger.h"
6 #include "QtToString.h"
7 #include <QVariant>
8 #include <QXmlStreamWriter>
9 
11 {
12 }
13 
14 int CurveNameList::columnCount (const QModelIndex & /* parent */) const
15 {
16  return 3;
17 }
18 
19 bool CurveNameList::containsCurveNameCurrent (const QString &curveName) const
20 {
21  LOG4CPP_INFO_S ((*mainCat)) << "CurveNameList::containsCurveNameCurrent"
22  << " entryCount=" << m_modelCurvesEntries.count();
23 
24  // Search for curve with matching name
25  QStringList::const_iterator itr;
26  for (itr = m_modelCurvesEntries.begin (); itr != m_modelCurvesEntries.end (); itr++) {
27 
28  CurveNameListEntry curvesEntry (*itr);
29  if (curveName == curvesEntry.curveNameCurrent()) {
30 
31  return true;
32  }
33  }
34 
35  return false;
36 }
37 
38 QVariant CurveNameList::data (const QModelIndex &index,
39  int role) const
40 {
41  LOG4CPP_DEBUG_S ((*mainCat)) << "CurveNameList::data"
42  << " isRoot=" << (index.isValid () ? "no" : "yes")
43  << " role=" << roleAsString (role).toLatin1 ().data ();
44 
45  if (!index.isValid ()) {
46  // Root item
47  return QVariant ();
48  }
49 
50  int row = index.row ();
51  if (row < 0 || row >= m_modelCurvesEntries.count ()) {
52  return QVariant();
53  }
54 
55  if ((role != Qt::DisplayRole) &&
56  (role != Qt::EditRole)) {
57  return QVariant();
58  }
59 
60  CurveNameListEntry curvesEntry (m_modelCurvesEntries.at (row));
61 
62  if (index.column () == 0) {
63  return curvesEntry.curveNameCurrent();
64  } else if (index.column () == 1) {
65  return curvesEntry.curveNameOriginal();
66  } else if (index.column () == 2) {
67  return curvesEntry.numPoints ();
68  } else {
69  ENGAUGE_ASSERT (false);
70  }
71 }
72 
73 
74 Qt::ItemFlags CurveNameList::flags (const QModelIndex &index) const
75 {
76  // Only the root item can accept drops, or else dragging one entry onto another
77  // would result in the drop target getting overwritten
78 
79  if (index.isValid ()) {
80 
81  // Not root item
82  return QAbstractTableModel::flags (index) |
83  Qt::ItemIsDragEnabled |
84  Qt::ItemIsEnabled |
85  Qt::ItemIsSelectable |
86  Qt::ItemIsEditable;
87 
88  } else {
89 
90  // Root item
91  return QAbstractTableModel::flags (index) |
92  Qt::ItemIsDropEnabled;
93 
94  }
95 }
96 
98  int count,
99  const QModelIndex &parent)
100 {
101  bool skip = (count != 1 || row < 0 || row > rowCount () || parent.isValid());
102 
103  LOG4CPP_INFO_S ((*mainCat)) << "CurveNameList::insertRows"
104  << " row=" << row
105  << " count=" << count
106  << " isRoot=" << (parent.isValid () ? "no" : "yes")
107  << " skip=" << (skip ? "yes" : "no");
108 
109  if (skip) {
110 
111  // Parent should be root item which is not valid
112  return false;
113  }
114 
115  beginInsertRows (QModelIndex (),
116  row,
117  row + count - 1);
118 
119  CurveNameListEntry emptyCurvesEntry;
120 
121  m_modelCurvesEntries.insert (row,
122  emptyCurvesEntry.toString ());
123 
124  endInsertRows ();
125 
126  return true;
127 }
128 
130  int count,
131  const QModelIndex &parent)
132 {
133  bool skip = (count != 1 || row < 0 || row > rowCount () || parent.isValid());
134 
135  LOG4CPP_DEBUG_S ((*mainCat)) << "CurveNameList::removeRows"
136  << " row=" << row
137  << " count=" << count
138  << " isRoot=" << (parent.isValid () ? "no" : "yes")
139  << " skip=" << (skip ? "yes" : "no");
140 
141  bool success = false;
142 
143  beginRemoveRows (QModelIndex (),
144  row,
145  row + count - 1);
146 
147  m_modelCurvesEntries.removeAt (row);
148 
149  endRemoveRows ();
150 
151  return success;
152 }
153 
154 int CurveNameList::rowCount (const QModelIndex & /* parent */) const
155 {
156  int count = m_modelCurvesEntries.count ();
157 
158  LOG4CPP_DEBUG_S ((*mainCat)) << "CurveNameList::rowCount count=" << count;
159 
160  return count;
161 }
162 
163 bool CurveNameList::setData (const QModelIndex &index,
164  const QVariant &value,
165  int role)
166 {
167  LOG4CPP_INFO_S ((*mainCat)) << "CurveNameList::setData"
168  << " indexRow=" << index.row ()
169  << " value=" << (value.isValid () ? "valid" : "invalid")
170  << " role=" << roleAsString (role).toLatin1 ().data ();
171 
172  bool success = false;
173 
174  int row = index.row ();
175  if (row < m_modelCurvesEntries.count ()) {
176 
177  if (!value.isValid () && (role == Qt::EditRole)) {
178 
179  // Remove the entry
180  m_modelCurvesEntries.removeAt (row);
181 
182  } else {
183 
184  // Modify the entry
185  CurveNameListEntry curvesEntry (m_modelCurvesEntries [row]); // Retrieve entry
186 
187  if (index.column () == 0) {
188  curvesEntry.setCurveNameCurrent (value.toString ());
189  } else if (index.column () == 1) {
190  curvesEntry.setCurveNameOriginal (value.toString ());
191  } else if (index.column () == 2) {
192  curvesEntry.setNumPoints (value.toInt ());
193  } else {
194  ENGAUGE_ASSERT (false);
195  }
196 
197  m_modelCurvesEntries [row] = curvesEntry.toString (); // Save update entry
198  }
199 
200  emit dataChanged (index,
201  index);
202 
203  success = true;
204  }
205 
206  return success;
207 }
208 
209 Qt::DropActions CurveNameList::supportedDropActions () const
210 {
211  return Qt::MoveAction;
212 }
bool containsCurveNameCurrent(const QString &curveName) const
Return true if specified curve name is already in the list.
virtual bool insertRows(int row, int count, const QModelIndex &parent=QModelIndex())
Insert one row.
Utility class for converting the QVariant in CurveNameList to/from the curve names as QStrings...
virtual Qt::DropActions supportedDropActions() const
Allow dragging for reordering.
void setCurveNameCurrent(const QString &curveNameCurrent)
Set method for current curve name.
virtual Qt::ItemFlags flags(const QModelIndex &index) const
Override normal flags with additional editing flags.
QString toString() const
QString for creating QVariant.
virtual bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole)
Store one curve name data.
virtual QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const
Retrieve data from model.
virtual bool removeRows(int row, int count, const QModelIndex &parent)
Remove one row.
void setCurveNameOriginal(const QString &curveNameOriginal)
Set method for original curve name.
virtual int rowCount(const QModelIndex &parent=QModelIndex()) const
One row per curve name.
virtual int columnCount(const QModelIndex &parent=QModelIndex()) const
Columns are current curve name in first column, and original curve name in second column...
QString curveNameCurrent() const
Curve name displayed in DlgSettingsCurveAddRemove.
CurveNameList()
Default constructor.
void setNumPoints(int numPoints)
Set method for point count.