Engauge Digitizer  2
 All Classes Files Functions Variables Enumerations Enumerator Friends Pages
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  return curvesEntry.curveNameOriginal(); // Default if asserts are disabled
71  }
72 }
73 
74 
75 Qt::ItemFlags CurveNameList::flags (const QModelIndex &index) const
76 {
77  // Only the root item can accept drops, or else dragging one entry onto another
78  // would result in the drop target getting overwritten
79 
80  if (index.isValid ()) {
81 
82  // Not root item
83  return QAbstractTableModel::flags (index) |
84  Qt::ItemIsDragEnabled |
85  Qt::ItemIsEnabled |
86  Qt::ItemIsSelectable |
87  Qt::ItemIsEditable;
88 
89  } else {
90 
91  // Root item
92  return QAbstractTableModel::flags (index) |
93  Qt::ItemIsDropEnabled;
94 
95  }
96 }
97 
99  int count,
100  const QModelIndex &parent)
101 {
102  bool skip = (count != 1 || row < 0 || row > rowCount () || parent.isValid());
103 
104  LOG4CPP_INFO_S ((*mainCat)) << "CurveNameList::insertRows"
105  << " row=" << row
106  << " count=" << count
107  << " isRoot=" << (parent.isValid () ? "no" : "yes")
108  << " skip=" << (skip ? "yes" : "no");
109 
110  if (skip) {
111 
112  // Parent should be root item which is not valid
113  return false;
114  }
115 
116  beginInsertRows (QModelIndex (),
117  row,
118  row + count - 1);
119 
120  CurveNameListEntry emptyCurvesEntry;
121 
122  m_modelCurvesEntries.insert (row,
123  emptyCurvesEntry.toString ());
124 
125  endInsertRows ();
126 
127  return true;
128 }
129 
131  int count,
132  const QModelIndex &parent)
133 {
134  bool skip = (count != 1 || row < 0 || row > rowCount () || parent.isValid());
135 
136  LOG4CPP_DEBUG_S ((*mainCat)) << "CurveNameList::removeRows"
137  << " row=" << row
138  << " count=" << count
139  << " isRoot=" << (parent.isValid () ? "no" : "yes")
140  << " skip=" << (skip ? "yes" : "no");
141 
142  bool success = false;
143 
144  beginRemoveRows (QModelIndex (),
145  row,
146  row + count - 1);
147 
148  m_modelCurvesEntries.removeAt (row);
149 
150  endRemoveRows ();
151 
152  return success;
153 }
154 
155 int CurveNameList::rowCount (const QModelIndex & /* parent */) const
156 {
157  int count = m_modelCurvesEntries.count ();
158 
159  LOG4CPP_DEBUG_S ((*mainCat)) << "CurveNameList::rowCount count=" << count;
160 
161  return count;
162 }
163 
164 bool CurveNameList::setData (const QModelIndex &index,
165  const QVariant &value,
166  int role)
167 {
168  LOG4CPP_INFO_S ((*mainCat)) << "CurveNameList::setData"
169  << " indexRow=" << index.row ()
170  << " value=" << (value.isValid () ? "valid" : "invalid")
171  << " role=" << roleAsString (role).toLatin1 ().data ();
172 
173  bool success = false;
174 
175  int row = index.row ();
176  if (row < m_modelCurvesEntries.count ()) {
177 
178  if (!value.isValid () && (role == Qt::EditRole)) {
179 
180  // Remove the entry
181  m_modelCurvesEntries.removeAt (row);
182 
183  } else {
184 
185  // Modify the entry
186  CurveNameListEntry curvesEntry (m_modelCurvesEntries [row]); // Retrieve entry
187 
188  if (index.column () == 0) {
189  curvesEntry.setCurveNameCurrent (value.toString ());
190  } else if (index.column () == 1) {
191  curvesEntry.setCurveNameOriginal (value.toString ());
192  } else if (index.column () == 2) {
193  curvesEntry.setNumPoints (value.toInt ());
194  } else {
195  ENGAUGE_ASSERT (false);
196  }
197 
198  m_modelCurvesEntries [row] = curvesEntry.toString (); // Save update entry
199  }
200 
201  emit dataChanged (index,
202  index);
203 
204  success = true;
205  }
206 
207  return success;
208 }
209 
210 Qt::DropActions CurveNameList::supportedDropActions () const
211 {
212  return Qt::MoveAction;
213 }
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.