Skip to content

Commit

Permalink
Refactore addCustomVariable and removeCustomVariable.
Browse files Browse the repository at this point in the history
  • Loading branch information
mohsenD98 committed Aug 24, 2024
1 parent 2bdadca commit facb371
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 23 deletions.
42 changes: 32 additions & 10 deletions src/core/expressionvariablemodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,42 @@ bool ExpressionVariableModel::setData( const QModelIndex &index, const QVariant
return QStandardItemModel::setData( index, value, role );
}

void ExpressionVariableModel::addCustomVariable( const QString &varName, const QString &varVal, const int &rowIndex )
int ExpressionVariableModel::addVariable( VariableScope scope, const QString &name, const QString &value )
{
QStandardItem *nameItem = new QStandardItem( varName );
nameItem->setData( varName, VariableName );
nameItem->setData( varVal, VariableValue );
nameItem->setData( QVariant::fromValue( VariableScope::ApplicationScope ), VariableScopeRole );
nameItem->setData( true, VariableEditable );
int lastVariableInScope = rowCount();
for ( int i = 0; i < rowCount(); ++i )
{
if ( item( i )->data( VariableScopeRole ).value<VariableScope>() == scope )
{
lastVariableInScope = i;
}
}

QStandardItem *nameItem = new QStandardItem( name );
nameItem->setData( name, VariableName );
nameItem->setData( value, VariableValue );
nameItem->setData( QVariant::fromValue( scope ), VariableScopeRole );
nameItem->setData( scope == VariableScope::ApplicationScope, VariableEditable );

insertRow( lastVariableInScope + 1, QList<QStandardItem *>() << nameItem );

insertRow( rowIndex == -1 ? rowCount() : rowIndex, QList<QStandardItem *>() << nameItem );
return lastVariableInScope + 1;
}

void ExpressionVariableModel::removeCustomVariable( int row )
void ExpressionVariableModel::removeVariable( VariableScope scope, const QString &name )
{
removeRow( row );
for ( int i = 0; i < rowCount(); ++i )
{
QStandardItem *rowItem = item( i );
QString variableName = rowItem->data( VariableName ).toString();
VariableScope variableScope = rowItem->data( VariableScopeRole ).value<VariableScope>();

if ( variableName == name && variableScope == scope )
{
removeRow( i );
return;
}
}
}

void ExpressionVariableModel::save()
Expand Down Expand Up @@ -98,7 +120,7 @@ void ExpressionVariableModel::reloadVariables()
{
if ( !scope->isReadOnly( varName ) )
{
addCustomVariable( varName, scope->variable( varName ).toString() );
addVariable( VariableScope::ApplicationScope, varName, scope->variable( varName ).toString() );
}
}
// Finally add readonly project variables
Expand Down
4 changes: 2 additions & 2 deletions src/core/expressionvariablemodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ class ExpressionVariableModel : public QStandardItemModel

bool setData( const QModelIndex &index, const QVariant &value, int role ) override;

Q_INVOKABLE void addCustomVariable( const QString &varName, const QString &varVal, const int &rowIndex = -1 );
Q_INVOKABLE int addVariable( VariableScope scope, const QString &name, const QString &value );

Q_INVOKABLE void removeCustomVariable( int row );
Q_INVOKABLE void removeVariable( VariableScope scope, const QString &name );

Q_INVOKABLE void save();

Expand Down
16 changes: 5 additions & 11 deletions src/qml/VariableEditor.qml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ ColumnLayout {

property var itemRow: index
property bool canDelete: VariableEditable
property var variableType: VariableScope

function forceFocusOnVariableName() {
variableNameText.forceActiveFocus();
Expand Down Expand Up @@ -143,7 +142,7 @@ ColumnLayout {
bgcolor: "transparent"

onClicked: {
table.model.removeCustomVariable(index);
table.model.removeVariable(VariableScope, variableNameText.text);
}
}
}
Expand All @@ -157,15 +156,10 @@ ColumnLayout {
text: qsTr("Add a new variable")

onClicked: {
let lastAppVariableIndex = 0;
for (let i = 0; i < table.count; ++i) {
if (table.itemAtIndex(i) && table.itemAtIndex(i).variableType === 0) {
lastAppVariableIndex = i;
}
}
table.model.addCustomVariable("new_variable", "", lastAppVariableIndex + 1);
table.positionViewAtIndex(lastAppVariableIndex + 1, ListView.Contain);
table.itemAtIndex(lastAppVariableIndex + 1).forceFocusOnVariableName();
let applicationScope = 0;
let insertionPosition = table.model.addVariable(applicationScope, "new_variable", "");
table.positionViewAtIndex(insertionPosition, ListView.Contain);
table.itemAtIndex(insertionPosition).forceFocusOnVariableName();
}
}
}

0 comments on commit facb371

Please sign in to comment.