//ChangeCommentCommand ChangeCommentCommand::ChangeCommentCommand(const QString &value, int row, ScriptModel *model, ScriptProxyModel *proxyModel) : QUndoCommand(), mModel(model), mProxyModel(proxyModel), mNew(value), mRow(row) { ActionTools::ActionInstance *actionInstance = mModel->mScript->actionAt(mRow); if(!actionInstance) return; mOld = actionInstance->comment(); setText(QObject::tr("Change the comment from %1 to %2").arg(mOld).arg(mNew)); }
bool ScriptModel::setData(const QModelIndex &index, const QVariant &value, int role) { if(!index.isValid()) return false; if(role == ActionIdRole) { mScript->setAction(index.row(), mActionFactory->newActionInstance(value.toString())); emit dataChanged(index, index); emit scriptEdited(); return true; } ActionTools::ActionInstance *actionInstance = mScript->actionAt(index.row()); if(!actionInstance) return false; if(role == ActionDataRole) { actionInstance->copyActionDataFrom(value.value<ActionTools::ActionInstance>()); emit dataChanged(index, index); emit scriptEdited(); return true; } if(index.column() == ColumnLabel) { switch(role) { case Qt::CheckStateRole: mUndoStack->push(new ChangeEnabledCommand(QList<int>() << index.row(), value.toBool(), this)); emit scriptEdited(); return true; case Qt::EditRole: QString lineNumber(QString("%1").arg(index.row() + 1, 3, 10, QChar('0'))); QString labelString(value.toString()); QString finalValue; if(!labelString.isEmpty() && lineNumber != labelString) { int labelLine = mScript->labelLine(labelString); if(labelLine == -1 || labelLine == index.row()) finalValue = labelString; } if(labelString == actionInstance->label() || labelString == lineNumber) return true; mUndoStack->push(new ChangeLabelCommand(finalValue, index.row(), this)); emit scriptEdited(); return true; } } else if(index.column() == ColumnComment) { if(value.toString() == actionInstance->comment()) return true; mUndoStack->push(new ChangeCommentCommand(value.toString(), index.row(), this)); emit scriptEdited(); return true; } return false; }
QVariant ScriptModel::data(const QModelIndex &index, int role) const { if(!index.isValid()) return QVariant(); ActionTools::ActionInstance *actionInstance = mScript->actionAt(index.row()); if(!actionInstance) return QVariant(); switch(role) { case ActionDataRole: return QVariant::fromValue(*actionInstance); case ActionIdRole: return actionInstance->definition()->id(); case Qt::BackgroundRole: { const QColor &color = actionInstance->color(); if(color.isValid()) return QBrush(color); return QBrush(); } case Qt::FontRole: { if(!actionInstance->definition()->worksUnderThisOS()) { QFont font = QApplication::font(); font.setItalic(true); return font; } return QFont(); } case Qt::ForegroundRole: { const QColor &color = actionInstance->color(); if(color.isValid()) { if(color.lightness() < 128) return QBrush(Qt::white); else return QBrush(Qt::black); } else { const QPalette &palette = QApplication::palette(); if(!actionInstance->isEnabled()) return QBrush(palette.color(QPalette::Disabled, QPalette::WindowText)); return QBrush(); } } } switch(index.column()) { case ColumnLabel: switch(role) { case Qt::CheckStateRole: return QVariant(actionInstance->isEnabled() ? Qt::Checked : Qt::Unchecked); case Qt::DisplayRole: { QString labelString = actionInstance->label(); if(!labelString.isNull() && !labelString.isEmpty()) return labelString; return QString("%1").arg(index.row() + 1, 3, 10, QChar('0')); } case Qt::EditRole: return actionInstance->label(); } break; case ColumnActionName: switch(role) { case Qt::ToolTipRole: return tr("Double-clic to edit the action"); case Qt::DisplayRole: return actionInstance->definition()->name(); case Qt::DecorationRole: return QIcon(actionInstance->definition()->icon()); case Qt::TextAlignmentRole: return Qt::AlignCenter; } break; case ColumnComment: switch(role) { case Qt::DisplayRole: case Qt::EditRole: return actionInstance->comment(); } break; } return QVariant(); }