Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Editable project variables. #5589

Merged
merged 6 commits into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 9 additions & 22 deletions src/core/expressionvariablemodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,26 +35,27 @@ bool ExpressionVariableModel::setData( const QModelIndex &index, const QVariant
return QStandardItemModel::setData( index, value, role );
}

int ExpressionVariableModel::addVariable( VariableScope scope, const QString &name, const QString &value )
int ExpressionVariableModel::addVariable( VariableScope scope, const QString &name, const QString &value, bool editable )
mohsenD98 marked this conversation as resolved.
Show resolved Hide resolved
mohsenD98 marked this conversation as resolved.
Show resolved Hide resolved
mohsenD98 marked this conversation as resolved.
Show resolved Hide resolved
{
int lastVariableInScope = rowCount();
for ( int i = 0; i < rowCount(); ++i )
{
if ( item( i )->data( VariableScopeRole ).value<VariableScope>() == scope )
{
lastVariableInScope = i;
lastVariableInScope = i + 1;
}
}

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 );
nameItem->setData( editable, VariableEditable );
nameItem->setEditable( editable );

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

return lastVariableInScope + 1;
return lastVariableInScope;
}

void ExpressionVariableModel::removeVariable( VariableScope scope, const QString &name )
Expand Down Expand Up @@ -101,42 +102,28 @@ void ExpressionVariableModel::reloadVariables()
{
if ( scope->isReadOnly( varName ) )
{
QStandardItem *nameItem = new QStandardItem( varName );
QVariant varValue = scope->variable( varName );
if ( QString::compare( varValue.toString(), QStringLiteral( "Not available" ) ) == 0 )
varValue = QVariant( QT_TR_NOOP( "Not Available" ) );

nameItem->setData( varName, VariableName );
nameItem->setData( varValue, VariableValue );
nameItem->setData( QVariant::fromValue( VariableScope::ApplicationScope ), VariableScopeRole );
nameItem->setData( false, VariableEditable );
nameItem->setEditable( false );

insertRow( rowCount(), QList<QStandardItem *>() << nameItem );
addVariable( VariableScope::ApplicationScope, varName, varValue.toString(), false );
}
}
// Second add custom variables
for ( const QString &varName : variableNames )
{
if ( !scope->isReadOnly( varName ) )
{
addVariable( VariableScope::ApplicationScope, varName, scope->variable( varName ).toString() );
addVariable( VariableScope::ApplicationScope, varName, scope->variable( varName ).toString(), true );
}
}
// Finally add readonly project variables
QVariantMap projectVariables = ExpressionContextUtils::projectVariables( mCurrentProject );
for ( const QString &varName : projectVariables.keys() )
{
QStandardItem *nameItem = new QStandardItem( varName );
QVariant varValue = projectVariables.value( varName ).toString();

nameItem->setData( varName, VariableName );
nameItem->setData( varValue, VariableValue );
nameItem->setData( QVariant::fromValue( VariableScope::ProjectScope ), VariableScopeRole );
nameItem->setData( false, VariableEditable );
nameItem->setEditable( false );

insertRow( rowCount(), QList<QStandardItem *>() << nameItem );
addVariable( VariableScope::ProjectScope, varName, varValue.toString(), false );
}
}

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 int addVariable( VariableScope scope, const QString &name, const QString &value );
Q_INVOKABLE int addVariable( VariableScope scope, const QString &name, const QString &value, bool editable );

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

Expand Down
4 changes: 2 additions & 2 deletions src/qml/VariableEditor.qml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ ColumnLayout {
color: "transparent"

property var itemRow: index
property bool canDelete: VariableEditable
property bool canDelete: VariableEditable && VariableScope == 0 // application scope
nirvn marked this conversation as resolved.
Show resolved Hide resolved

function forceFocusOnVariableName() {
variableNameText.forceActiveFocus();
Expand Down Expand Up @@ -157,7 +157,7 @@ ColumnLayout {

onClicked: {
let applicationScope = 0;
let insertionPosition = table.model.addVariable(applicationScope, "new_variable", "");
let insertionPosition = table.model.addVariable(applicationScope, "new_variable", "", true);
table.positionViewAtIndex(insertionPosition, ListView.Contain);
table.itemAtIndex(insertionPosition).forceFocusOnVariableName();
}
Expand Down
Loading