bool CurveNameList::setData (const QModelIndex &index,
                             const QVariant &value,
                             int role)
{
  LOG4CPP_DEBUG_S ((*mainCat)) << "CurveNameList::setData"
                               << " row=" << index.row()
                               << " value=" << value.toString().toLatin1().data()
                               << " role=" << roleAsString (role).toLatin1().data();

  bool success;
  if (role == Qt::EditRole) {

    // Each curve name must be unique
    if (curveNameIsAcceptable (value.toString(),
                               index.row())) {

      // Curve name is fine
      QModelIndex idxOld = QStandardItemModel::index (index.row(), CURVE_NAME_LIST_COLUMN_CURRENT);

      // Old and new curve names
      QString curveCurrentOld = data (idxOld).toString ();
      QString curveCurrentNew = value.toString ();

      // Remove old entry after saving original curve name
      QString curveOriginal;
      if (m_currentCurveToOriginalCurve.contains (curveCurrentOld)) {

        // Remember old original curve name
        curveOriginal = m_currentCurveToOriginalCurve [curveCurrentOld];

        // Remove old entry
        m_currentCurveToOriginalCurve.remove (curveCurrentOld);

        // Add new entry
        m_currentCurveToOriginalCurve [curveCurrentNew] = curveOriginal;
      }

      success = QStandardItemModel::setData (index,
                                             value,
                                             role);
    } else {

      // Curve name is unacceptable
      success = false;

    }
  } else {

    // For non-edits, this method just calls the superclass method
    success = QStandardItemModel::setData (index,
                                           value,
                                           role);
  }

  return success;
}
Beispiel #2
0
bool CurveNameList::setData (const QModelIndex &index,
                             const QVariant &value,
                             int role)
{
  LOG4CPP_INFO_S ((*mainCat)) << "CurveNameList::setData"
                              << " indexRow=" << index.row ()
                              << " indexCol=" << index.column ()
                              << " indexValid=" << (index.isValid() ? "valid" : "invalid")
                              << " valueValid=" << (value.isValid () ? "valid" : "invalid")
                              << " value=" << value.toString().toLatin1().data()
                              << " role=" << roleAsString (role).toLatin1 ().data ();

  bool success = false;

  if (index.isValid()) {

    // Process the new entry
    int row = index.row ();
    if (row < m_modelCurvesEntries.count ()) {

      // Variable 'row' points to an empty entry (created by insertRows) so we populate it here
      success = true;

      QString before = m_modelCurvesEntries.join (PIPE).replace (TAB, SPACE);

      if (!value.isValid () && (role == Qt::EditRole)) {

        // Remove the entry
        m_modelCurvesEntries.removeAt (row);

      } else {

        // Modify the entry
        CurveNameListEntry curvesEntry (m_modelCurvesEntries [row]); // Retrieve entry

        if (index.column () == 0) {

          if (role == Qt::EditRole) {

            // Does new curve name meet the requirements
            if (curveNameIsAcceptable (value.toString (),
                                       row)) {

              curvesEntry.setCurveNameCurrent (value.toString ());
              m_modelCurvesEntries [row] = curvesEntry.toString (); // Save update entry

            } else {

              success = false;
            }

          } else if ((role == Qt::DisplayRole) ||
                     (curveNameIsAcceptable (value.toString(),
                                             row))) {

            // Above we skipped curve name uniqueness check for Qt::DisplayRole since that role is used when dragging
            // curve from one place to another, since for a short time the new curve name will coexist
            // with the old curve name (until the old entry is removed)

            if (rowIsUnpopulated (row)) {
              success = true;
              curvesEntry.setCurveNameCurrent (value.toString ());
              curvesEntry.setNumPoints (0);
              m_modelCurvesEntries [row] = curvesEntry.toString (); // Save update entry
              tryToRemoveOriginalCopy (index,
                                       value,
                                       role);
            } else {
              success = false;
            }
          }
        } else if (index.column () == 1) {
          curvesEntry.setCurveNameOriginal (value.toString ());
          m_modelCurvesEntries [row] = curvesEntry.toString (); // Save update entry
        } else if (index.column () == 2) {
          curvesEntry.setNumPoints (value.toInt ());
          m_modelCurvesEntries [row] = curvesEntry.toString (); // Save update entry
        } else {
          ENGAUGE_ASSERT (false);
        }

        if (success) {
          emit dataChanged (index,
                            index);
        }

        QString after = m_modelCurvesEntries.join (PIPE).replace (TAB, SPACE);

        LOG4CPP_INFO_S ((*mainCat)) << "CurveNameList::setData setting"
                                    << " before=" << before.toLatin1().data()
                                    << " after=" << after.toLatin1().data();

      }
    }
  }

  return success;
}