00001
00002
00003
00004
00005
00006
00007 #include "CurveNameList.h"
00008 #include "DocumentSerialize.h"
00009 #include "EngaugeAssert.h"
00010 #include "Logger.h"
00011 #include <qdebug.h>
00012 #include <QHash>
00013 #include <QTextStream>
00014 #include "QtToString.h"
00015 #include <QVariant>
00016 #include <QXmlStreamWriter>
00017
00018 CurveNameList::CurveNameList() :
00019 QStandardItemModel()
00020 {
00021 }
00022
00023 int CurveNameList::columnCount (const QModelIndex & ) const
00024 {
00025 return NUMBER_CURVE_NAME_LIST_COLUMNS;
00026 }
00027
00028 bool CurveNameList::containsCurveNameCurrent (const QString &curveName) const
00029 {
00030 LOG4CPP_INFO_S ((*mainCat)) << "CurveNameList::containsCurveNameCurrent";
00031
00032
00033 CurrentCurveToOriginalCurve::const_iterator itr;
00034 for (itr = m_currentCurveToOriginalCurve.begin (); itr != m_currentCurveToOriginalCurve.end (); ++itr) {
00035
00036 if (itr.key () == curveName) {
00037 return true;
00038 }
00039 }
00040
00041 return false;
00042 }
00043
00044 QString CurveNameList::currentCurvesAsString () const
00045 {
00046 LOG4CPP_INFO_S ((*mainCat)) << "CurveNameList::currentCurvesAsString";
00047
00048 QString out;
00049 QTextStream str (&out);
00050
00051 for (int row = 0; row < rowCount (); row++) {
00052
00053 QString curveCurrent = data (index (row, CURVE_NAME_LIST_COLUMN_CURRENT)).toString ();
00054 QString curveOriginal;
00055 unsigned int points = 0;
00056 if (m_currentCurveToOriginalCurve.contains (curveCurrent)) {
00057 curveOriginal = m_currentCurveToOriginalCurve [curveCurrent];
00058 if (m_originalCurveToPointCount.contains (curveOriginal)) {
00059
00060 points = m_originalCurveToPointCount [curveOriginal];
00061 }
00062 }
00063
00064 str << "\n current=" << curveCurrent.toLatin1().data()
00065 << " original=" << curveOriginal
00066 << " points=" << points;
00067 }
00068
00069 return out;
00070 }
00071
00072 QString CurveNameList::currentCurveToOriginalCurve (const QString ¤tCurve) const
00073 {
00074 return m_currentCurveToOriginalCurve [currentCurve];
00075 }
00076
00077 unsigned int CurveNameList::currentCurveToPointCount (const QString ¤tCurve) const
00078 {
00079 QString originalCurve = m_currentCurveToOriginalCurve [currentCurve];
00080
00081 return m_originalCurveToPointCount [originalCurve];
00082 }
00083
00084 bool CurveNameList::curveNameIsAcceptable (const QString &curveNameNew,
00085 int row) const
00086 {
00087
00088 bool success = (!curveNameNew.isEmpty ());
00089
00090 if (success) {
00091
00092
00093 for (int row1 = 0; row1 < rowCount(); row1++) {
00094
00095
00096 QModelIndex index1 = index (row1, CURVE_NAME_LIST_COLUMN_CURRENT);
00097 QString curveNameCurrent1 = (row1 == row ?
00098 curveNameNew :
00099 data (index1).toString ());
00100
00101 for (int row2 = row1 + 1; row2 < rowCount(); row2++) {
00102
00103
00104 QModelIndex index2 = index (row2, CURVE_NAME_LIST_COLUMN_CURRENT);
00105 QString curveNameCurrent2 = (row2 == row ?
00106 curveNameNew :
00107 data (index2).toString ());
00108
00109 if (curveNameCurrent1 == curveNameCurrent2) {
00110
00111
00112 success = false;
00113 break;
00114 }
00115 }
00116 }
00117 }
00118
00119 return success;
00120 }
00121
00122 Qt::ItemFlags CurveNameList::flags (const QModelIndex &index) const
00123 {
00124 if (index.isValid ()) {
00125
00126
00127
00128 return (QStandardItemModel::flags (index) |
00129 Qt::ItemIsDragEnabled |
00130 Qt::ItemIsEnabled |
00131 Qt::ItemIsSelectable |
00132 Qt::ItemIsEditable) & ~Qt::ItemIsDropEnabled;
00133
00134 } else {
00135
00136
00137 return QStandardItemModel::flags (index) |
00138 Qt::ItemIsDropEnabled;
00139
00140 }
00141 }
00142
00143 void CurveNameList::insertRow (int row,
00144 const QString &curveCurrent,
00145 const QString &curveOriginal,
00146 unsigned int pointCount)
00147 {
00148 LOG4CPP_DEBUG_S ((*mainCat)) << "CurveNameList::insertRow"
00149 << " row=" <<row
00150 << " curveCurrent=" << curveCurrent.toLatin1().data()
00151 << " curveOriginal=" << curveOriginal.toLatin1().data()
00152 << " points=" << pointCount;
00153
00154 QStandardItem *item = new QStandardItem (curveCurrent);
00155 QStandardItemModel::insertRow (row, item);
00156
00157
00158 beginInsertRows (QModelIndex (),
00159 row,
00160 row);
00161
00162 m_currentCurveToOriginalCurve [curveCurrent] = curveOriginal;
00163 m_originalCurveToPointCount [curveOriginal] = pointCount;
00164
00165 endInsertRows ();
00166 }
00167
00168 QStandardItem *CurveNameList::item(int row, int column) const
00169 {
00170 LOG4CPP_DEBUG_S ((*mainCat)) << "CurveNameList::item"
00171 << " row=" << row;
00172
00173 ENGAUGE_ASSERT (row < rowCount ());
00174
00175 return QStandardItemModel::item (row, column);
00176 }
00177
00178 unsigned int CurveNameList::numPointsForSelectedCurves (const QList<unsigned int> &rowsSelected) const
00179 {
00180 int numPoints = 0;
00181 for (int i = 0; i < rowsSelected.count(); i++) {
00182 int row = rowsSelected [i];
00183
00184 QModelIndex idx = index (row, CURVE_NAME_LIST_COLUMN_CURRENT);
00185 QString currentCurve = data (idx).toString ();
00186 if (m_currentCurveToOriginalCurve.contains (currentCurve)) {
00187
00188 QString originalCurve = m_currentCurveToOriginalCurve [currentCurve];
00189 if (m_originalCurveToPointCount.contains (originalCurve)) {
00190
00191 numPoints += m_originalCurveToPointCount [originalCurve];
00192 }
00193 }
00194 }
00195
00196 return numPoints;
00197 }
00198
00199 bool CurveNameList::removeRows (int row,
00200 int count,
00201 const QModelIndex &parent)
00202 {
00203
00204
00205 bool skip = (count != 1 || row < 0 || row > rowCount () || parent.isValid());
00206
00207 QString before, after;
00208 if (!skip) {
00209
00210 before = currentCurvesAsString ();
00211
00212
00213
00214 beginRemoveRows (QModelIndex (),
00215 row,
00216 row + count - 1);
00217
00218
00219 for (int rowRemove = row; rowRemove < row + count; rowRemove++) {
00220 QStandardItemModel::removeRows (row,
00221 count,
00222 parent);
00223 }
00224
00225 endRemoveRows ();
00226
00227 after = currentCurvesAsString ();
00228 }
00229
00230 LOG4CPP_DEBUG_S ((*mainCat)) << "CurveNameList::removeRows"
00231 << " row=" << row
00232 << " count=" << count
00233 << " isRoot=" << (parent.isValid () ? "no" : "yes")
00234 << " skip=" << (skip ? "yes" : "no")
00235 << " before=" << before.toLatin1().data()
00236 << " after=" << after.toLatin1().data();
00237 return true;
00238 }
00239
00240 void CurveNameList::reset()
00241 {
00242 LOG4CPP_INFO_S ((*mainCat)) << "CurveNameList::reset";
00243
00244 clear();
00245 m_currentCurveToOriginalCurve.clear();
00246 m_originalCurveToPointCount.clear();
00247 }
00248
00249 int CurveNameList::rowCount (const QModelIndex & ) const
00250 {
00251 int count = QStandardItemModel::rowCount ();
00252
00253
00254
00255
00256 return count;
00257 }
00258
00259 bool CurveNameList::setData (const QModelIndex &index,
00260 const QVariant &value,
00261 int role)
00262 {
00263 LOG4CPP_DEBUG_S ((*mainCat)) << "CurveNameList::setData"
00264 << " row=" << index.row()
00265 << " value=" << value.toString().toLatin1().data()
00266 << " role=" << roleAsString (role).toLatin1().data();
00267
00268 bool success;
00269 if (role == Qt::EditRole) {
00270
00271
00272 if (curveNameIsAcceptable (value.toString(),
00273 index.row())) {
00274
00275
00276 QModelIndex idxOld = QStandardItemModel::index (index.row(), CURVE_NAME_LIST_COLUMN_CURRENT);
00277
00278
00279 QString curveCurrentOld = data (idxOld).toString ();
00280 QString curveCurrentNew = value.toString ();
00281
00282
00283 QString curveOriginal;
00284 if (m_currentCurveToOriginalCurve.contains (curveCurrentOld)) {
00285
00286
00287 curveOriginal = m_currentCurveToOriginalCurve [curveCurrentOld];
00288
00289
00290 m_currentCurveToOriginalCurve.remove (curveCurrentOld);
00291
00292
00293 m_currentCurveToOriginalCurve [curveCurrentNew] = curveOriginal;
00294 }
00295
00296 success = QStandardItemModel::setData (index,
00297 value,
00298 role);
00299 } else {
00300
00301
00302 success = false;
00303
00304 }
00305 } else {
00306
00307
00308 success = QStandardItemModel::setData (index,
00309 value,
00310 role);
00311 }
00312
00313 return success;
00314 }
00315
00316 void CurveNameList::setItem(int row,
00317 int column,
00318 QStandardItem *item)
00319 {
00320
00321
00322 ENGAUGE_ASSERT (column == CURVE_NAME_LIST_COLUMN_CURRENT);
00323
00324 QString before = currentCurvesAsString ();
00325
00326 QStandardItemModel::setItem (row,
00327 column,
00328 item);
00329
00330 QString after = currentCurvesAsString ();
00331
00332 LOG4CPP_DEBUG_S ((*mainCat)) << "CurveNameList::setItem"
00333 << " row=" << row
00334 << " before=" << before.toLatin1().data()
00335 << " after=" << after.toLatin1().data();
00336 }
00337
00338 Qt::DropActions CurveNameList::supportedDropActions () const
00339 {
00340 return Qt::MoveAction;
00341 }