Engauge Digitizer 2
Loading...
Searching...
No Matches
CurveNameList.cpp
1/******************************************************************************************************
2 * (C) 2014 markummitchell@github.com. This file is part of Engauge Digitizer, which is released *
3 * under GNU General Public License version 2 (GPLv2) or (at your option) any later version. See file *
4 * LICENSE or go to gnu.org/licenses for details. Distribution requires prior written permission. *
5 ******************************************************************************************************/
6
7#include "CurveNameListEntry.h"
8#include "CurveNameList.h"
9#include "DocumentSerialize.h"
10#include "EngaugeAssert.h"
11#include "Logger.h"
12#include "QtToString.h"
13#include <QVariant>
14#include <QXmlStreamWriter>
15
19
20int CurveNameList::columnCount (const QModelIndex & /* parent */) const
21{
22 return 3;
23}
24
25bool CurveNameList::containsCurveNameCurrent (const QString &curveName) const
26{
27 LOG4CPP_INFO_S ((*mainCat)) << "CurveNameList::containsCurveNameCurrent"
28 << " entryCount=" << m_modelCurvesEntries.count();
29
30 // Search for curve with matching name
31 QStringList::const_iterator itr;
32 for (itr = m_modelCurvesEntries.begin (); itr != m_modelCurvesEntries.end (); itr++) {
33
34 CurveNameListEntry curvesEntry (*itr);
35 if (curveName == curvesEntry.curveNameCurrent()) {
36
37 return true;
38 }
39 }
40
41 return false;
42}
43
44bool CurveNameList::curveNameIsAcceptable (const QString &curveNameNew,
45 int row) const
46{
47 // First test is to verify curve name is not empty
48 bool success = (!curveNameNew.isEmpty ());
49
50 if (success) {
51
52 // First test was passed. Second test is to check for duplication
53
54 for (int row1 = 0; row1 < m_modelCurvesEntries.count(); row1++) {
55
56 // Use table entry except for the one row that gets overridden
57 CurveNameListEntry curvesEntry1 (m_modelCurvesEntries [row1]); // Retrieve entry
58 QString curveNameCurrent1 = (row1 == row ?
59 curveNameNew :
60 curvesEntry1.curveNameCurrent());
61
62 for (int row2 = row1 + 1; row2 < m_modelCurvesEntries.count(); row2++) {
63
64 // Use table entry except for the one row that gets overridden
65 CurveNameListEntry curvesEntry2 (m_modelCurvesEntries [row2]); // Retrieve entry
66 QString curveNameCurrent2 = (row2 == row ?
67 curveNameNew :
68 curvesEntry2.curveNameCurrent());
69
70 if (curveNameCurrent1 == curveNameCurrent2) {
71
72 // Duplicate!
73 success = false;
74 break;
75 }
76 }
77 }
78 }
79
80 return success;
81}
82
83QVariant CurveNameList::data (const QModelIndex &index,
84 int role) const
85{
86 LOG4CPP_DEBUG_S ((*mainCat)) << "CurveNameList::data"
87 << " isRoot=" << (index.isValid () ? "no" : "yes")
88 << " role=" << roleAsString (role).toLatin1 ().data ();
89
90 if (!index.isValid ()) {
91 // Root item
92 return QVariant ();
93 }
94
95 int row = index.row ();
96 if (row < 0 || row >= m_modelCurvesEntries.count ()) {
97 return QVariant();
98 }
99
100 if ((role != Qt::DisplayRole) &&
101 (role != Qt::EditRole)) {
102 return QVariant();
103 }
104
105 CurveNameListEntry curvesEntry (m_modelCurvesEntries.at (row));
106
107 if (index.column () == 0) {
108 return curvesEntry.curveNameCurrent();
109 } else if (index.column () == 1) {
110 return curvesEntry.curveNameOriginal();
111 } else if (index.column () == 2) {
112 return curvesEntry.numPoints ();
113 } else {
114 ENGAUGE_ASSERT (false);
115 return curvesEntry.curveNameOriginal(); // Default if asserts are disabled
116 }
117}
118
119
120Qt::ItemFlags CurveNameList::flags (const QModelIndex &index) const
121{
122 // Only the root item can accept drops, or else dragging one entry onto another
123 // would result in the drop target getting overwritten
124
125 if (index.isValid ()) {
126
127 // Not root item
128 return QAbstractTableModel::flags (index) |
129 Qt::ItemIsDragEnabled |
130 Qt::ItemIsEnabled |
131 Qt::ItemIsSelectable |
132 Qt::ItemIsEditable;
133
134 } else {
135
136 // Root item
137 return QAbstractTableModel::flags (index) |
138 Qt::ItemIsDropEnabled;
139
140 }
141}
142
144 int count,
145 const QModelIndex &parent)
146{
147 bool skip = (count != 1 || row < 0 || row > rowCount () || parent.isValid());
148
149 LOG4CPP_INFO_S ((*mainCat)) << "CurveNameList::insertRows"
150 << " row=" << row
151 << " count=" << count
152 << " isRoot=" << (parent.isValid () ? "no" : "yes")
153 << " skip=" << (skip ? "yes" : "no");
154
155 if (skip) {
156
157 // Parent should be root item which is not valid
158 return false;
159 }
160
161 beginInsertRows (QModelIndex (),
162 row,
163 row + count - 1);
164
165 CurveNameListEntry emptyCurvesEntry;
166
167 m_modelCurvesEntries.insert (row,
168 emptyCurvesEntry.toString ());
169
170 endInsertRows ();
171
172 return true;
173}
174
176 int count,
177 const QModelIndex &parent)
178{
179 bool skip = (count != 1 || row < 0 || row > rowCount () || parent.isValid());
180
181 LOG4CPP_DEBUG_S ((*mainCat)) << "CurveNameList::removeRows"
182 << " row=" << row
183 << " count=" << count
184 << " isRoot=" << (parent.isValid () ? "no" : "yes")
185 << " skip=" << (skip ? "yes" : "no");
186
187 bool success = false;
188
189 beginRemoveRows (QModelIndex (),
190 row,
191 row + count - 1);
192
193 m_modelCurvesEntries.removeAt (row);
194
195 endRemoveRows ();
196
197 return success;
198}
199
200int CurveNameList::rowCount (const QModelIndex & /* parent */) const
201{
202 int count = m_modelCurvesEntries.count ();
203
204 LOG4CPP_DEBUG_S ((*mainCat)) << "CurveNameList::rowCount count=" << count;
205
206 return count;
207}
208
209bool CurveNameList::setData (const QModelIndex &index,
210 const QVariant &value,
211 int role)
212{
213 LOG4CPP_INFO_S ((*mainCat)) << "CurveNameList::setData"
214 << " indexRow=" << index.row ()
215 << " value=" << (value.isValid () ? "valid" : "invalid")
216 << " role=" << roleAsString (role).toLatin1 ().data ();
217
218 bool success = false;
219
220 int row = index.row ();
221 if (row < m_modelCurvesEntries.count ()) {
222
223 success = true; // This method will be successful except in the rare case when a curve name is a duplicate
224
225 if (!value.isValid () && (role == Qt::EditRole)) {
226
227 // Remove the entry
228 m_modelCurvesEntries.removeAt (row);
229
230 } else {
231
232 // Modify the entry
233 CurveNameListEntry curvesEntry (m_modelCurvesEntries [row]); // Retrieve entry
234
235 if (index.column () == 0) {
236 curvesEntry.setCurveNameCurrent (value.toString ());
237 success = curveNameIsAcceptable (value.toString (),
238 row);
239 } else if (index.column () == 1) {
240 curvesEntry.setCurveNameOriginal (value.toString ());
241 } else if (index.column () == 2) {
242 curvesEntry.setNumPoints (value.toInt ());
243 } else {
244 ENGAUGE_ASSERT (false);
245 }
246
247 if (success) {
248 m_modelCurvesEntries [row] = curvesEntry.toString (); // Save update entry
249 }
250 }
251
252 if (success) {
253 emit dataChanged (index,
254 index);
255 }
256 }
257
258 return success;
259}
260
262{
263 return Qt::MoveAction;
264}
Utility class for converting the QVariant in CurveNameList to/from the curve names as QStrings,...
QString curveNameOriginal() const
Original curve name in document. Empty if there was no original curve.
void setNumPoints(int numPoints)
Set method for point count.
QString toString() const
QString for creating QVariant.
void setCurveNameCurrent(const QString &curveNameCurrent)
Set method for current curve name.
QString curveNameCurrent() const
Curve name displayed in DlgSettingsCurveAddRemove.
int numPoints() const
Number of points in curve.
void setCurveNameOriginal(const QString &curveNameOriginal)
Set method for original curve name.
virtual QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const
Retrieve data from model.
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.
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.
virtual bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole)
Store one curve name data.
virtual Qt::ItemFlags flags(const QModelIndex &index) const
Override normal flags with additional editing flags.
virtual bool removeRows(int row, int count, const QModelIndex &parent)
Remove one row.
CurveNameList()
Default constructor.
virtual Qt::DropActions supportedDropActions() const
Allow dragging for reordering.