00001
00002
00003
00004
00005
00006
00007 #include "CurveNameListEntry.h"
00008 #include "CurveNameList.h"
00009 #include "DocumentSerialize.h"
00010 #include "EngaugeAssert.h"
00011 #include "Logger.h"
00012 #include "QtToString.h"
00013 #include <QVariant>
00014 #include <QXmlStreamWriter>
00015
00016 const QString PIPE ("|");
00017 const QString SPACE (" ");
00018 const QString TAB ("\t");
00019
00020 CurveNameList::CurveNameList()
00021 {
00022 }
00023
00024 int CurveNameList::columnCount (const QModelIndex & ) const
00025 {
00026 return 3;
00027 }
00028
00029 bool CurveNameList::containsCurveNameCurrent (const QString &curveName) const
00030 {
00031 LOG4CPP_INFO_S ((*mainCat)) << "CurveNameList::containsCurveNameCurrent"
00032 << " entryCount=" << m_modelCurvesEntries.count();
00033
00034
00035 QStringList::const_iterator itr;
00036 for (itr = m_modelCurvesEntries.begin (); itr != m_modelCurvesEntries.end (); itr++) {
00037
00038 CurveNameListEntry curvesEntry (*itr);
00039 if (curveName == curvesEntry.curveNameCurrent()) {
00040
00041 return true;
00042 }
00043 }
00044
00045 return false;
00046 }
00047
00048 bool CurveNameList::curveNameIsAcceptable (const QString &curveNameNew,
00049 int row) const
00050 {
00051
00052 bool success = (!curveNameNew.isEmpty ());
00053
00054 if (success) {
00055
00056
00057 for (int row1 = 0; row1 < m_modelCurvesEntries.count(); row1++) {
00058
00059
00060 CurveNameListEntry curvesEntry1 (m_modelCurvesEntries [row1]);
00061 QString curveNameCurrent1 = (row1 == row ?
00062 curveNameNew :
00063 curvesEntry1.curveNameCurrent());
00064
00065 for (int row2 = row1 + 1; row2 < m_modelCurvesEntries.count(); row2++) {
00066
00067
00068 CurveNameListEntry curvesEntry2 (m_modelCurvesEntries [row2]);
00069 QString curveNameCurrent2 = (row2 == row ?
00070 curveNameNew :
00071 curvesEntry2.curveNameCurrent());
00072
00073 if (curveNameCurrent1 == curveNameCurrent2) {
00074
00075
00076 success = false;
00077 break;
00078 }
00079 }
00080 }
00081 }
00082
00083 return success;
00084 }
00085
00086 QVariant CurveNameList::data (const QModelIndex &index,
00087 int role) const
00088 {
00089 LOG4CPP_DEBUG_S ((*mainCat)) << "CurveNameList::data"
00090 << " isRoot=" << (index.isValid () ? "no" : "yes")
00091 << " role=" << roleAsString (role).toLatin1 ().data ();
00092
00093 if (!index.isValid ()) {
00094
00095 return QVariant ();
00096 }
00097
00098 int row = index.row ();
00099 if (row < 0 || row >= m_modelCurvesEntries.count ()) {
00100 return QVariant();
00101 }
00102
00103 if ((role != Qt::DisplayRole) &&
00104 (role != Qt::EditRole)) {
00105 return QVariant();
00106 }
00107
00108 CurveNameListEntry curvesEntry (m_modelCurvesEntries.at (row));
00109
00110 if (index.column () == 0) {
00111 return curvesEntry.curveNameCurrent();
00112 } else if (index.column () == 1) {
00113 return curvesEntry.curveNameOriginal();
00114 } else if (index.column () == 2) {
00115 return curvesEntry.numPoints ();
00116 } else {
00117 ENGAUGE_ASSERT (false);
00118 return curvesEntry.curveNameOriginal();
00119 }
00120 }
00121
00122
00123 Qt::ItemFlags CurveNameList::flags (const QModelIndex &index) const
00124 {
00125 if (index.isValid ()) {
00126
00127
00128
00129 return QAbstractTableModel::flags (index) |
00130 Qt::ItemIsDragEnabled |
00131 Qt::ItemIsEnabled |
00132 Qt::ItemIsSelectable |
00133 Qt::ItemIsEditable;
00134
00135 } else {
00136
00137
00138 return QAbstractTableModel::flags (index) |
00139 Qt::ItemIsDropEnabled;
00140
00141 }
00142 }
00143
00144 QModelIndex CurveNameList::indexForValue (const QModelIndex &indexToSkip,
00145 const QVariant &value) const
00146 {
00147 LOG4CPP_INFO_S ((*mainCat)) << "CurveNameList::indexForValue";
00148
00149 for (int row = 0; row < rowCount(); row++) {
00150
00151 QModelIndex indexSearch = index (row, 0);
00152
00153 if (indexToSkip != indexSearch) {
00154
00155 if (data (indexSearch) == value) {
00156
00157 return indexSearch;
00158
00159 }
00160 }
00161 }
00162
00163 QModelIndex invalid;
00164 return invalid;
00165 }
00166
00167 bool CurveNameList::insertRows (int row,
00168 int count,
00169 const QModelIndex &parent)
00170 {
00171 bool skip = (count != 1 || row < 0 || row > rowCount () || parent.isValid());
00172
00173 LOG4CPP_INFO_S ((*mainCat)) << "CurveNameList::insertRows"
00174 << " row=" << row
00175 << " count=" << count
00176 << " parentRow=" << parent.row()
00177 << " parentCol=" << parent.column()
00178 << " isRoot=" << (parent.isValid () ? "no" : "yes")
00179 << " skip=" << (skip ? "yes" : "no");
00180
00181 if (skip) {
00182
00183
00184
00185
00186
00187
00188 return false;
00189 }
00190
00191 QString before = m_modelCurvesEntries.join (PIPE).replace (TAB, SPACE);
00192
00193 beginInsertRows (QModelIndex (),
00194 row,
00195 row + count - 1);
00196
00197 CurveNameListEntry emptyCurvesEntry;
00198
00199 m_modelCurvesEntries.insert (row,
00200 emptyCurvesEntry.toString ());
00201
00202 endInsertRows ();
00203
00204 QString after = m_modelCurvesEntries.join (PIPE).replace (TAB, SPACE);
00205
00206 LOG4CPP_INFO_S ((*mainCat)) << "CurveNameList::insertRows"
00207 << " before=" << before.toLatin1().data()
00208 << " after=" << after.toLatin1().data();
00209
00210 return true;
00211 }
00212
00213 bool CurveNameList::removeRows (int row,
00214 int count,
00215 const QModelIndex &parent)
00216 {
00217 bool skip = (count != 1 || row < 0 || row > rowCount () || parent.isValid());
00218
00219 LOG4CPP_DEBUG_S ((*mainCat)) << "CurveNameList::removeRows"
00220 << " row=" << row
00221 << " count=" << count
00222 << " isRoot=" << (parent.isValid () ? "no" : "yes")
00223 << " skip=" << (skip ? "yes" : "no");
00224
00225 bool success = false;
00226
00227 beginRemoveRows (QModelIndex (),
00228 row,
00229 row + count - 1);
00230
00231 m_modelCurvesEntries.removeAt (row);
00232
00233 endRemoveRows ();
00234
00235 return success;
00236 }
00237
00238 int CurveNameList::rowCount (const QModelIndex & ) const
00239 {
00240 int count = m_modelCurvesEntries.count ();
00241
00242 LOG4CPP_DEBUG_S ((*mainCat)) << "CurveNameList::rowCount count=" << count;
00243
00244 return count;
00245 }
00246
00247 bool CurveNameList::rowIsUnpopulated (int row) const
00248 {
00249
00250
00251
00252
00253 QString fields = m_modelCurvesEntries.at (row);
00254 CurveNameListEntry entryAtRow (fields);
00255 return entryAtRow.entryHasNotBeenPopulated ();
00256 }
00257
00258 bool CurveNameList::setData (const QModelIndex &index,
00259 const QVariant &value,
00260 int role)
00261 {
00262 LOG4CPP_INFO_S ((*mainCat)) << "CurveNameList::setData"
00263 << " indexRow=" << index.row ()
00264 << " indexCol=" << index.column ()
00265 << " indexValid=" << (index.isValid() ? "valid" : "invalid")
00266 << " valueValid=" << (value.isValid () ? "valid" : "invalid")
00267 << " value=" << value.toString().toLatin1().data()
00268 << " role=" << roleAsString (role).toLatin1 ().data ();
00269
00270 bool success = false;
00271
00272 if (index.isValid()) {
00273
00274
00275 int row = index.row ();
00276 if (row < m_modelCurvesEntries.count ()) {
00277
00278
00279 success = true;
00280
00281 QString before = m_modelCurvesEntries.join (PIPE).replace (TAB, SPACE);
00282
00283 if (!value.isValid () && (role == Qt::EditRole)) {
00284
00285
00286 m_modelCurvesEntries.removeAt (row);
00287
00288 } else {
00289
00290
00291 CurveNameListEntry curvesEntry (m_modelCurvesEntries [row]);
00292
00293 if (index.column () == 0) {
00294
00295 if (role == Qt::EditRole) {
00296
00297
00298 if (curveNameIsAcceptable (value.toString (),
00299 row)) {
00300
00301 curvesEntry.setCurveNameCurrent (value.toString ());
00302 m_modelCurvesEntries [row] = curvesEntry.toString ();
00303
00304 } else {
00305
00306 success = false;
00307 }
00308
00309 } else if ((role == Qt::DisplayRole) ||
00310 (curveNameIsAcceptable (value.toString(),
00311 row))) {
00312
00313
00314
00315
00316
00317 if (rowIsUnpopulated (row)) {
00318 success = true;
00319 curvesEntry.setCurveNameCurrent (value.toString ());
00320 curvesEntry.setNumPoints (0);
00321 m_modelCurvesEntries [row] = curvesEntry.toString ();
00322 tryToRemoveOriginalCopy (index,
00323 value,
00324 role);
00325 } else {
00326 success = false;
00327 }
00328 }
00329 } else if (index.column () == 1) {
00330 curvesEntry.setCurveNameOriginal (value.toString ());
00331 m_modelCurvesEntries [row] = curvesEntry.toString ();
00332 } else if (index.column () == 2) {
00333 curvesEntry.setNumPoints (value.toInt ());
00334 m_modelCurvesEntries [row] = curvesEntry.toString ();
00335 } else {
00336 ENGAUGE_ASSERT (false);
00337 }
00338
00339 if (success) {
00340 emit dataChanged (index,
00341 index);
00342 }
00343
00344 QString after = m_modelCurvesEntries.join (PIPE).replace (TAB, SPACE);
00345
00346 LOG4CPP_INFO_S ((*mainCat)) << "CurveNameList::setData setting"
00347 << " before=" << before.toLatin1().data()
00348 << " after=" << after.toLatin1().data();
00349
00350 }
00351 }
00352 }
00353
00354 return success;
00355 }
00356
00357 Qt::DropActions CurveNameList::supportedDropActions () const
00358 {
00359 return Qt::MoveAction;
00360 }
00361
00362 void CurveNameList::tryToRemoveOriginalCopy (const QModelIndex &index,
00363 const QVariant &value,
00364 int role)
00365 {
00366
00367 if (index.column () == 0 && role == Qt::DisplayRole) {
00368 QModelIndex indexToRemove = indexForValue (index,
00369 value);
00370 if (indexToRemove.isValid()) {
00371
00372 QString before = m_modelCurvesEntries.join (PIPE).replace (TAB, SPACE);
00373
00374 beginRemoveRows (QModelIndex (),
00375 indexToRemove.row(),
00376 indexToRemove.row());
00377 m_modelCurvesEntries.removeAt (indexToRemove.row ());
00378 endRemoveRows ();
00379
00380 emit dataChanged (indexToRemove,
00381 indexToRemove);
00382
00383 QString after = m_modelCurvesEntries.join (PIPE).replace (TAB, SPACE);
00384
00385 LOG4CPP_INFO_S ((*mainCat)) << "CurveNameList::setData removed"
00386 << " indexRow=" << indexToRemove.row ()
00387 << " indexCol=" << indexToRemove.column ()
00388 << " before=" << before.toLatin1().data()
00389 << " after=" << after.toLatin1().data();
00390 }
00391 }
00392 }