Skip to content

Commit

Permalink
Append CustomVariables to the end of AppVariables not project variables.
Browse files Browse the repository at this point in the history
  • Loading branch information
mohsenD98 committed Aug 23, 2024
1 parent 4f31c93 commit ea2684c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 18 deletions.
27 changes: 13 additions & 14 deletions src/core/expressionvariablemodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ bool ExpressionVariableModel::setData( const QModelIndex &index, const QVariant
return QStandardItemModel::setData( index, value, role );
}

void ExpressionVariableModel::addCustomVariable( const QString &varName, const QString &varVal )
void ExpressionVariableModel::addCustomVariable( const QString &varName, const QString &varVal, const int &rowIndex )
{
QStandardItem *nameItem = new QStandardItem( varName );
nameItem->setData( varName, VariableName );
nameItem->setData( varVal, VariableValue );
nameItem->setData( QVariant::fromValue( VariableScope::ApplicationScope ), VariableScopeRole );
nameItem->setData( true, VariableEditable );

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

void ExpressionVariableModel::removeCustomVariable( int row )
Expand Down Expand Up @@ -88,12 +88,20 @@ void ExpressionVariableModel::reloadVariables()
nameItem->setData( varValue, VariableValue );
nameItem->setData( QVariant::fromValue( VariableScope::ApplicationScope ), VariableScopeRole );
nameItem->setData( false, VariableEditable );
nameItem->setEditable( false );

insertRow( rowCount(), QList<QStandardItem *>() << nameItem );
}
}

// Second add readonly project variables
// Second add custom variables
for ( const QString &varName : variableNames )
{
if ( !scope->isReadOnly( varName ) )
{
addCustomVariable( varName, scope->variable( varName ).toString() );
}
}
// Finally add readonly project variables
QVariantMap projectVariables = ExpressionContextUtils::projectVariables( mCurrentProject );
for ( const QString &varName : projectVariables.keys() )
{
Expand All @@ -104,19 +112,10 @@ void ExpressionVariableModel::reloadVariables()
nameItem->setData( varValue, VariableValue );
nameItem->setData( QVariant::fromValue( VariableScope::ProjectScope ), VariableScopeRole );
nameItem->setData( false, VariableEditable );
nameItem->setEditable( false );

insertRow( rowCount(), QList<QStandardItem *>() << nameItem );
}


// Then add custom variables
for ( const QString &varName : variableNames )
{
if ( !scope->isReadOnly( varName ) )
{
addCustomVariable( varName, scope->variable( varName ).toString() );
}
}
}

void ExpressionVariableModel::setName( int row, const QString &name )
Expand Down
2 changes: 1 addition & 1 deletion src/core/expressionvariablemodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ 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 );
Q_INVOKABLE void addCustomVariable( const QString &varName, const QString &varVal, const int &rowIndex = -1 );

Q_INVOKABLE void removeCustomVariable( int row );

Expand Down
12 changes: 9 additions & 3 deletions src/qml/VariableEditor.qml
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,15 @@ ColumnLayout {
text: qsTr("Add a new variable")

onClicked: {
table.model.addCustomVariable("new_variable", "");
table.positionViewAtIndex(table.count - 1, ListView.visible);
table.itemAtIndex(table.count - 1).forceFocusOnVariableName();
let lastEditableIndex = 0;
for (let i = 0; i < table.count; ++i) {
if (table.itemAtIndex(i) && table.itemAtIndex(i).canDelete) {
lastEditableIndex = i;
}
}
table.model.addCustomVariable("new_variable", "", lastEditableIndex + 1);
table.positionViewAtIndex(lastEditableIndex + 1, ListView.Contain);
table.itemAtIndex(lastEditableIndex + 1).forceFocusOnVariableName();
}
}
}

0 comments on commit ea2684c

Please sign in to comment.