From ea2684c18da89af7c41fbfcc403466823a6c0a06 Mon Sep 17 00:00:00 2001 From: Mohsen Date: Fri, 23 Aug 2024 13:26:35 +0330 Subject: [PATCH] Append CustomVariables to the end of AppVariables not project variables. --- src/core/expressionvariablemodel.cpp | 27 +++++++++++++-------------- src/core/expressionvariablemodel.h | 2 +- src/qml/VariableEditor.qml | 12 +++++++++--- 3 files changed, 23 insertions(+), 18 deletions(-) diff --git a/src/core/expressionvariablemodel.cpp b/src/core/expressionvariablemodel.cpp index 5e9f4d5170..244b6e52ca 100644 --- a/src/core/expressionvariablemodel.cpp +++ b/src/core/expressionvariablemodel.cpp @@ -35,7 +35,7 @@ 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 ); @@ -43,7 +43,7 @@ void ExpressionVariableModel::addCustomVariable( const QString &varName, const Q nameItem->setData( QVariant::fromValue( VariableScope::ApplicationScope ), VariableScopeRole ); nameItem->setData( true, VariableEditable ); - insertRow( rowCount(), QList() << nameItem ); + insertRow( rowIndex == -1 ? rowCount() : rowIndex, QList() << nameItem ); } void ExpressionVariableModel::removeCustomVariable( int row ) @@ -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() << 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() ) { @@ -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() << 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 ) diff --git a/src/core/expressionvariablemodel.h b/src/core/expressionvariablemodel.h index b8daf1f662..56be49dd26 100644 --- a/src/core/expressionvariablemodel.h +++ b/src/core/expressionvariablemodel.h @@ -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 ); diff --git a/src/qml/VariableEditor.qml b/src/qml/VariableEditor.qml index fc48df4afe..354bfb4b69 100644 --- a/src/qml/VariableEditor.qml +++ b/src/qml/VariableEditor.qml @@ -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(); } } }