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 CurveNameList::CurveNameList()
00017 {
00018 }
00019
00020 int CurveNameList::columnCount (const QModelIndex & ) const
00021 {
00022 return 3;
00023 }
00024
00025 bool CurveNameList::containsCurveNameCurrent (const QString &curveName) const
00026 {
00027 LOG4CPP_INFO_S ((*mainCat)) << "CurveNameList::containsCurveNameCurrent"
00028 << " entryCount=" << m_modelCurvesEntries.count();
00029
00030
00031 QStringList::const_iterator itr;
00032 for (itr = m_modelCurvesEntries.begin (); itr != m_modelCurvesEntries.end (); itr++) {
00033
00034 CurveNameListEntry curvesEntry (*itr);
00035 if (curveName == curvesEntry.curveNameCurrent()) {
00036
00037 return true;
00038 }
00039 }
00040
00041 return false;
00042 }
00043
00044 bool CurveNameList::curveNameIsAcceptable (const QString &curveNameNew,
00045 int row) const
00046 {
00047
00048 bool success = (!curveNameNew.isEmpty ());
00049
00050 if (success) {
00051
00052
00053
00054 for (int row1 = 0; row1 < m_modelCurvesEntries.count(); row1++) {
00055
00056
00057 CurveNameListEntry curvesEntry1 (m_modelCurvesEntries [row1]);
00058 QString curveNameCurrent1 = (row1 == row ?
00059 curveNameNew :
00060 curvesEntry1.curveNameCurrent());
00061
00062 for (int row2 = row1 + 1; row2 < m_modelCurvesEntries.count(); row2++) {
00063
00064
00065 CurveNameListEntry curvesEntry2 (m_modelCurvesEntries [row2]);
00066 QString curveNameCurrent2 = (row2 == row ?
00067 curveNameNew :
00068 curvesEntry2.curveNameCurrent());
00069
00070 if (curveNameCurrent1 == curveNameCurrent2) {
00071
00072
00073 success = false;
00074 break;
00075 }
00076 }
00077 }
00078 }
00079
00080 return success;
00081 }
00082
00083 QVariant CurveNameList::data (const QModelIndex &index,
00084 int role) const
00085 {
00086 LOG4CPP_DEBUG_S ((*mainCat)) << "CurveNameList::data"
00087 << " isRoot=" << (index.isValid () ? "no" : "yes")
00088 << " role=" << roleAsString (role).toLatin1 ().data ();
00089
00090 if (!index.isValid ()) {
00091
00092 return QVariant ();
00093 }
00094
00095 int row = index.row ();
00096 if (row < 0 || row >= m_modelCurvesEntries.count ()) {
00097 return QVariant();
00098 }
00099
00100 if ((role != Qt::DisplayRole) &&
00101 (role != Qt::EditRole)) {
00102 return QVariant();
00103 }
00104
00105 CurveNameListEntry curvesEntry (m_modelCurvesEntries.at (row));
00106
00107 if (index.column () == 0) {
00108 return curvesEntry.curveNameCurrent();
00109 } else if (index.column () == 1) {
00110 return curvesEntry.curveNameOriginal();
00111 } else if (index.column () == 2) {
00112 return curvesEntry.numPoints ();
00113 } else {
00114 ENGAUGE_ASSERT (false);
00115 return curvesEntry.curveNameOriginal();
00116 }
00117 }
00118
00119
00120 Qt::ItemFlags CurveNameList::flags (const QModelIndex &index) const
00121 {
00122
00123
00124
00125 if (index.isValid ()) {
00126
00127
00128 return QAbstractTableModel::flags (index) |
00129 Qt::ItemIsDragEnabled |
00130 Qt::ItemIsEnabled |
00131 Qt::ItemIsSelectable |
00132 Qt::ItemIsEditable;
00133
00134 } else {
00135
00136
00137 return QAbstractTableModel::flags (index) |
00138 Qt::ItemIsDropEnabled;
00139
00140 }
00141 }
00142
00143 bool CurveNameList::insertRows (int row,
00144 int count,
00145 const QModelIndex &parent)
00146 {
00147 bool skip = (count != 1 || row < 0 || row > rowCount () || parent.isValid());
00148
00149 LOG4CPP_INFO_S ((*mainCat)) << "CurveNameList::insertRows"
00150 << " row=" << row
00151 << " count=" << count
00152 << " isRoot=" << (parent.isValid () ? "no" : "yes")
00153 << " skip=" << (skip ? "yes" : "no");
00154
00155 if (skip) {
00156
00157
00158 return false;
00159 }
00160
00161 beginInsertRows (QModelIndex (),
00162 row,
00163 row + count - 1);
00164
00165 CurveNameListEntry emptyCurvesEntry;
00166
00167 m_modelCurvesEntries.insert (row,
00168 emptyCurvesEntry.toString ());
00169
00170 endInsertRows ();
00171
00172 return true;
00173 }
00174
00175 bool CurveNameList::removeRows (int row,
00176 int count,
00177 const QModelIndex &parent)
00178 {
00179 bool skip = (count != 1 || row < 0 || row > rowCount () || parent.isValid());
00180
00181 LOG4CPP_DEBUG_S ((*mainCat)) << "CurveNameList::removeRows"
00182 << " row=" << row
00183 << " count=" << count
00184 << " isRoot=" << (parent.isValid () ? "no" : "yes")
00185 << " skip=" << (skip ? "yes" : "no");
00186
00187 bool success = false;
00188
00189 beginRemoveRows (QModelIndex (),
00190 row,
00191 row + count - 1);
00192
00193 m_modelCurvesEntries.removeAt (row);
00194
00195 endRemoveRows ();
00196
00197 return success;
00198 }
00199
00200 int CurveNameList::rowCount (const QModelIndex & ) const
00201 {
00202 int count = m_modelCurvesEntries.count ();
00203
00204 LOG4CPP_DEBUG_S ((*mainCat)) << "CurveNameList::rowCount count=" << count;
00205
00206 return count;
00207 }
00208
00209 bool CurveNameList::setData (const QModelIndex &index,
00210 const QVariant &value,
00211 int role)
00212 {
00213 LOG4CPP_INFO_S ((*mainCat)) << "CurveNameList::setData"
00214 << " indexRow=" << index.row ()
00215 << " value=" << (value.isValid () ? "valid" : "invalid")
00216 << " role=" << roleAsString (role).toLatin1 ().data ();
00217
00218 bool success = false;
00219
00220 int row = index.row ();
00221 if (row < m_modelCurvesEntries.count ()) {
00222
00223 success = true;
00224
00225 if (!value.isValid () && (role == Qt::EditRole)) {
00226
00227
00228 m_modelCurvesEntries.removeAt (row);
00229
00230 } else {
00231
00232
00233 CurveNameListEntry curvesEntry (m_modelCurvesEntries [row]);
00234
00235 if (index.column () == 0) {
00236 curvesEntry.setCurveNameCurrent (value.toString ());
00237 success = curveNameIsAcceptable (value.toString (),
00238 row);
00239 } else if (index.column () == 1) {
00240 curvesEntry.setCurveNameOriginal (value.toString ());
00241 } else if (index.column () == 2) {
00242 curvesEntry.setNumPoints (value.toInt ());
00243 } else {
00244 ENGAUGE_ASSERT (false);
00245 }
00246
00247 if (success) {
00248 m_modelCurvesEntries [row] = curvesEntry.toString ();
00249 }
00250 }
00251
00252 if (success) {
00253 emit dataChanged (index,
00254 index);
00255 }
00256 }
00257
00258 return success;
00259 }
00260
00261 Qt::DropActions CurveNameList::supportedDropActions () const
00262 {
00263 return Qt::MoveAction;
00264 }