Changeset View
Changeset View
Standalone View
Standalone View
shell/settings/environmentwidget.cpp
1 | /* This file is part of KDevelop | 1 | /* This file is part of KDevelop | ||
---|---|---|---|---|---|
2 | Copyright 2006 Adam Treat <treat@kde.org> | 2 | Copyright 2006 Adam Treat <treat@kde.org> | ||
3 | Copyright 2007 Dukju Ahn <dukjuahn@gmail.com> | 3 | Copyright 2007 Dukju Ahn <dukjuahn@gmail.com> | ||
4 | Copyright 2008 Andreas Pakuat <apaku@gmx.de> | 4 | Copyright 2008 Andreas Pakuat <apaku@gmx.de> | ||
5 | Copyright 2017 Friedrich W. H. Kossebau <kossebau@kde.org> | ||||
5 | 6 | | |||
6 | This library is free software; you can redistribute it and/or | 7 | This library is free software; you can redistribute it and/or | ||
7 | modify it under the terms of the GNU Library General Public | 8 | modify it under the terms of the GNU Library General Public | ||
8 | License as published by the Free Software Foundation; either | 9 | License as published by the Free Software Foundation; either | ||
9 | version 2 of the License, or (at your option) any later version. | 10 | version 2 of the License, or (at your option) any later version. | ||
10 | 11 | | |||
11 | This library is distributed in the hope that it will be useful, | 12 | This library is distributed in the hope that it will be useful, | ||
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
Show All 9 Lines | |||||
22 | #include "environmentwidget.h" | 23 | #include "environmentwidget.h" | ||
23 | 24 | | |||
24 | #include <QDialog> | 25 | #include <QDialog> | ||
25 | #include <QDialogButtonBox> | 26 | #include <QDialogButtonBox> | ||
26 | #include <QHeaderView> | 27 | #include <QHeaderView> | ||
27 | #include <QPushButton> | 28 | #include <QPushButton> | ||
28 | #include <QSortFilterProxyModel> | 29 | #include <QSortFilterProxyModel> | ||
29 | #include <QTextEdit> | 30 | #include <QTextEdit> | ||
31 | #include <QLineEdit> | ||||
30 | #include <QVBoxLayout> | 32 | #include <QVBoxLayout> | ||
33 | #include <QHBoxLayout> | ||||
34 | #include <QValidator> | ||||
31 | 35 | | |||
32 | #include <KLocalizedString> | 36 | #include <KLocalizedString> | ||
33 | 37 | | |||
34 | #include "environmentgroupmodel.h" | 38 | #include "environmentprofilelistmodel.h" | ||
39 | #include "environmentprofilemodel.h" | ||||
35 | #include "placeholderitemproxymodel.h" | 40 | #include "placeholderitemproxymodel.h" | ||
36 | #include "../debug.h" | 41 | #include "../debug.h" | ||
37 | 42 | | |||
38 | namespace KDevelop | 43 | using namespace KDevelop; | ||
44 | | ||||
45 | | ||||
46 | class ProfileNameValidator : public QValidator | ||||
39 | { | 47 | { | ||
48 | Q_OBJECT | ||||
40 | 49 | | |||
41 | EnvironmentWidget::EnvironmentWidget( QWidget *parent ) | 50 | public: | ||
42 | : QWidget( parent ), groupModel( new EnvironmentGroupModel() ), proxyModel( new QSortFilterProxyModel() ) | 51 | explicit ProfileNameValidator(EnvironmentProfileListModel* environmentProfileListModel, QObject* parent = nullptr); | ||
52 | QValidator::State validate(QString& input, int& pos) const override; | ||||
53 | | ||||
54 | private: | ||||
55 | const EnvironmentProfileListModel* const m_environmentProfileListModel; | ||||
56 | }; | ||||
57 | | ||||
58 | ProfileNameValidator::ProfileNameValidator(EnvironmentProfileListModel* environmentProfileListModel, | ||||
59 | QObject* parent) | ||||
60 | : QValidator(parent) | ||||
61 | , m_environmentProfileListModel(environmentProfileListModel) | ||||
62 | { | ||||
63 | } | ||||
64 | | ||||
65 | QValidator::State ProfileNameValidator::validate(QString& input, int& pos) const | ||||
43 | { | 66 | { | ||
67 | Q_UNUSED(pos); | ||||
68 | | ||||
69 | if (input.isEmpty()) { | ||||
70 | return QValidator::Intermediate; | ||||
71 | } | ||||
72 | if (m_environmentProfileListModel->hasProfile(input)) { | ||||
73 | return QValidator::Intermediate; | ||||
74 | } | ||||
75 | return QValidator::Acceptable; | ||||
76 | } | ||||
77 | | ||||
78 | | ||||
44 | 79 | | |||
80 | EnvironmentWidget::EnvironmentWidget( QWidget *parent ) | ||||
81 | : QWidget(parent) | ||||
82 | , m_environmentProfileListModel(new EnvironmentProfileListModel(this)) | ||||
83 | , m_environmentProfileModel(new EnvironmentProfileModel(m_environmentProfileListModel, this)) | ||||
84 | , m_proxyModel(new QSortFilterProxyModel(this)) | ||||
85 | { | ||||
45 | // setup ui | 86 | // setup ui | ||
46 | ui.setupUi( this ); | 87 | ui.setupUi( this ); | ||
47 | ui.variableTable->verticalHeader()->hide(); | | |||
48 | 88 | | |||
49 | proxyModel->setSourceModel( groupModel ); | 89 | ui.profileSelect->setModel(m_environmentProfileListModel); | ||
90 | m_proxyModel->setSourceModel(m_environmentProfileModel); | ||||
50 | 91 | | |||
51 | PlaceholderItemProxyModel* topProxyModel = new PlaceholderItemProxyModel(this); | 92 | PlaceholderItemProxyModel* topProxyModel = new PlaceholderItemProxyModel(this); | ||
52 | topProxyModel->setSourceModel(proxyModel); | 93 | topProxyModel->setSourceModel(m_proxyModel); | ||
53 | topProxyModel->setColumnHint(0, i18n("Enter variable ...")); | 94 | topProxyModel->setColumnHint(0, i18n("Enter variable...")); | ||
54 | connect(topProxyModel, &PlaceholderItemProxyModel::dataInserted, this, &EnvironmentWidget::handleVariableInserted); | 95 | connect(topProxyModel, &PlaceholderItemProxyModel::dataInserted, this, &EnvironmentWidget::onVariableInserted); | ||
55 | 96 | | |||
56 | ui.variableTable->setModel( topProxyModel ); | 97 | ui.variableTable->setModel( topProxyModel ); | ||
57 | ui.variableTable->horizontalHeader()->setSectionResizeMode( 0, QHeaderView::ResizeToContents ); | 98 | ui.variableTable->horizontalHeader()->setSectionResizeMode( 0, QHeaderView::ResizeToContents ); | ||
58 | ui.variableTable->horizontalHeader()->setSectionResizeMode( 1, QHeaderView::Stretch ); | 99 | ui.variableTable->horizontalHeader()->setSectionResizeMode( 1, QHeaderView::Stretch ); | ||
59 | ui.addgrpBtn->setIcon(QIcon::fromTheme(QStringLiteral("list-add"))); | 100 | ui.removeVariableButton->setShortcut(Qt::Key_Delete); | ||
60 | ui.clonegrpBtn->setIcon(QIcon::fromTheme(QStringLiteral("edit-clone"))); | | |||
61 | ui.removegrpBtn->setIcon(QIcon::fromTheme(QStringLiteral("list-remove"))); | | |||
62 | ui.deleteButton->setIcon(QIcon::fromTheme(QStringLiteral("list-remove"))); | | |||
63 | ui.deleteButton->setShortcut(Qt::Key_Delete); | | |||
64 | ui.batchModeEditButton->setIcon(QIcon::fromTheme(QStringLiteral("format-list-unordered"))); | | |||
65 | 101 | | |||
66 | connect( ui.deleteButton, &QPushButton::clicked, | 102 | connect(ui.removeVariableButton, &QPushButton::clicked, | ||
67 | this, &EnvironmentWidget::deleteButtonClicked ); | 103 | this, &EnvironmentWidget::removeSelectedVariables); | ||
68 | connect( ui.batchModeEditButton, &QPushButton::clicked, | 104 | connect(ui.batchModeEditButton, &QPushButton::clicked, | ||
69 | this, &EnvironmentWidget::batchModeEditButtonClicked ); | 105 | this, &EnvironmentWidget::batchModeEditButtonClicked); | ||
70 | 106 | | |||
71 | connect( ui.clonegrpBtn, &QPushButton::clicked, this, &EnvironmentWidget::cloneGroupClicked ); | 107 | connect(ui.cloneProfileButton, &QPushButton::clicked, this, &EnvironmentWidget::cloneSelectedProfile); | ||
72 | connect( ui.addgrpBtn, &QPushButton::clicked, this, &EnvironmentWidget::addGroupClicked ); | 108 | connect(ui.addProfileButton, &QPushButton::clicked, this, &EnvironmentWidget::addProfile); | ||
73 | connect( ui.addgrpBtn, &QPushButton::clicked, this, &EnvironmentWidget::changed ); | 109 | connect(ui.removeProfileButton, &QPushButton::clicked, this, &EnvironmentWidget::removeSelectedProfile); | ||
74 | connect( ui.removegrpBtn, &QPushButton::clicked, this, &EnvironmentWidget::removeGroupClicked ); | 110 | connect(ui.setAsDefaultProfileButton, &QPushButton::clicked, this, &EnvironmentWidget::setSelectedProfileAsDefault); | ||
75 | connect( ui.removegrpBtn, &QPushButton::clicked, this, &EnvironmentWidget::changed ); | 111 | connect(ui.profileSelect, static_cast<void(KComboBox::*)(int)>(&KComboBox::currentIndexChanged), | ||
76 | connect( ui.setAsDefaultBtn, &QPushButton::clicked, this, &EnvironmentWidget::setAsDefault ); | 112 | this, &EnvironmentWidget::onSelectedProfileChanged); | ||
77 | connect( ui.setAsDefaultBtn, &QPushButton::clicked, this, &EnvironmentWidget::changed ); | 113 | | ||
78 | connect( ui.activeCombo, static_cast<void(KComboBox::*)(int)>(&KComboBox::currentIndexChanged), | 114 | connect(m_environmentProfileListModel, &EnvironmentProfileListModel::defaultProfileChanged, | ||
79 | this, &EnvironmentWidget::activeGroupChanged ); | 115 | this, &EnvironmentWidget::onDefaultProfileChanged); | ||
80 | connect( ui.activeCombo, &KComboBox::editTextChanged, this, &EnvironmentWidget::enableButtons); | 116 | connect(m_environmentProfileListModel, &EnvironmentProfileListModel::rowsInserted, | ||
81 | connect( groupModel, &EnvironmentGroupModel::dataChanged, this, &EnvironmentWidget::changed ); | 117 | this, &EnvironmentWidget::changed); | ||
82 | connect( groupModel, &EnvironmentGroupModel::rowsRemoved, this, &EnvironmentWidget::changed ); | 118 | connect(m_environmentProfileListModel, &EnvironmentProfileListModel::rowsRemoved, | ||
83 | connect( groupModel, &EnvironmentGroupModel::rowsInserted, this, &EnvironmentWidget::changed ); | 119 | this, &EnvironmentWidget::changed); | ||
84 | connect( groupModel, &EnvironmentGroupModel::rowsRemoved, this, &EnvironmentWidget::enableDeleteButton ); | 120 | connect(m_environmentProfileListModel, &EnvironmentProfileListModel::defaultProfileChanged, | ||
85 | connect( groupModel, &EnvironmentGroupModel::rowsInserted, this, &EnvironmentWidget::enableDeleteButton ); | 121 | this, &EnvironmentWidget::changed); | ||
86 | connect( groupModel, &EnvironmentGroupModel::modelReset, this, &EnvironmentWidget::enableDeleteButton ); | 122 | | ||
123 | connect(ui.variableTable->selectionModel(), &QItemSelectionModel::selectionChanged, | ||||
124 | this, &EnvironmentWidget::updateDeleteVariableButton); | ||||
125 | connect(m_environmentProfileModel, &EnvironmentProfileModel::rowsInserted, | ||||
126 | this, &EnvironmentWidget::updateDeleteVariableButton); | ||||
127 | connect(m_environmentProfileModel, &EnvironmentProfileModel::rowsRemoved, | ||||
128 | this, &EnvironmentWidget::updateDeleteVariableButton); | ||||
129 | connect(m_environmentProfileModel, &EnvironmentProfileModel::modelReset, | ||||
130 | this, &EnvironmentWidget::updateDeleteVariableButton); | ||||
131 | connect(m_environmentProfileModel, &EnvironmentProfileModel::dataChanged, | ||||
132 | this, &EnvironmentWidget::changed); | ||||
133 | connect(m_environmentProfileModel, &EnvironmentProfileModel::rowsInserted, | ||||
134 | this, &EnvironmentWidget::changed); | ||||
135 | connect(m_environmentProfileModel, &EnvironmentProfileModel::rowsRemoved, | ||||
136 | this, &EnvironmentWidget::changed); | ||||
87 | } | 137 | } | ||
88 | 138 | | |||
89 | void EnvironmentWidget::setActiveGroup( const QString& group ) | 139 | void EnvironmentWidget::selectProfile(const QString& profileName) | ||
90 | { | 140 | { | ||
91 | ui.activeCombo->setCurrentItem(group); | 141 | const int profileIndex = m_environmentProfileListModel->profileIndex(profileName); | ||
142 | if (profileIndex < 0) { | ||||
143 | return; | ||||
144 | } | ||||
145 | ui.profileSelect->setCurrentIndex(profileIndex); | ||||
92 | } | 146 | } | ||
93 | 147 | | |||
94 | void EnvironmentWidget::enableDeleteButton() | 148 | void EnvironmentWidget::updateDeleteVariableButton() | ||
95 | { | 149 | { | ||
96 | ui.deleteButton->setEnabled( groupModel->rowCount() > 0 ); | 150 | const auto selectedRows = ui.variableTable->selectionModel()->selectedRows(); | ||
151 | ui.removeVariableButton->setEnabled(!selectedRows.isEmpty()); | ||||
97 | } | 152 | } | ||
98 | 153 | | |||
99 | void EnvironmentWidget::setAsDefault() | 154 | void EnvironmentWidget::setSelectedProfileAsDefault() | ||
100 | { | 155 | { | ||
101 | groupModel->changeDefaultGroup( ui.activeCombo->currentText() ); | 156 | const int selectedIndex = ui.profileSelect->currentIndex(); | ||
102 | enableButtons( ui.activeCombo->currentText() ); | 157 | m_environmentProfileListModel->setDefaultProfile(selectedIndex); | ||
103 | emit changed(); | | |||
104 | } | 158 | } | ||
105 | 159 | | |||
106 | void EnvironmentWidget::loadSettings( KConfig* config ) | 160 | void EnvironmentWidget::loadSettings( KConfig* config ) | ||
107 | { | 161 | { | ||
108 | qCDebug(SHELL) << "Loading groups from config"; | 162 | qCDebug(SHELL) << "Loading profiles from config"; | ||
109 | groupModel->loadFromConfig( config ); | 163 | m_environmentProfileListModel->loadFromConfig(config); | ||
110 | 164 | | |||
111 | ui.activeCombo->clear(); | 165 | const int defaultProfileIndex = m_environmentProfileListModel->defaultProfileIndex(); | ||
112 | 166 | ui.profileSelect->setCurrentIndex(defaultProfileIndex); | |||
113 | QStringList groupList = groupModel->groups(); | | |||
114 | qCDebug(SHELL) << "Grouplist:" << groupList << "default group:" << groupModel->defaultGroup(); | | |||
115 | ui.activeCombo->addItems( groupList ); | | |||
116 | int idx = ui.activeCombo->findText( groupModel->defaultGroup() ); | | |||
117 | ui.activeCombo->setCurrentIndex( idx ); | | |||
118 | } | 167 | } | ||
119 | 168 | | |||
120 | void EnvironmentWidget::saveSettings( KConfig* config ) | 169 | void EnvironmentWidget::saveSettings( KConfig* config ) | ||
121 | { | 170 | { | ||
122 | groupModel->saveToConfig( config ); | 171 | m_environmentProfileListModel->saveToConfig(config); | ||
123 | } | 172 | } | ||
124 | 173 | | |||
125 | void EnvironmentWidget::defaults( KConfig* config ) | 174 | void EnvironmentWidget::defaults( KConfig* config ) | ||
126 | { | 175 | { | ||
127 | loadSettings( config ); | 176 | loadSettings( config ); | ||
128 | } | 177 | } | ||
129 | 178 | | |||
130 | void EnvironmentWidget::deleteButtonClicked() | 179 | QString EnvironmentWidget::askNewProfileName(const QString& defaultName) | ||
180 | { | ||||
181 | QDialog dialog(this); | ||||
182 | dialog.setWindowTitle(i18n("Enter Name of New Environment Profile")); | ||||
183 | | ||||
184 | QVBoxLayout *layout = new QVBoxLayout(&dialog); | ||||
185 | | ||||
186 | auto editLayout = new QHBoxLayout; | ||||
187 | | ||||
188 | auto label = new QLabel(i18n("Name:")); | ||||
189 | editLayout->addWidget(label); | ||||
190 | auto edit = new QLineEdit; | ||||
191 | editLayout->addWidget(edit); | ||||
192 | layout->addLayout(editLayout); | ||||
193 | | ||||
194 | auto buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel); | ||||
195 | auto okButton = buttonBox->button(QDialogButtonBox::Ok); | ||||
196 | okButton->setEnabled(false); | ||||
197 | okButton->setDefault(true); | ||||
198 | dialog.connect(buttonBox, &QDialogButtonBox::accepted, &dialog, &QDialog::accept); | ||||
199 | dialog.connect(buttonBox, &QDialogButtonBox::rejected, &dialog, &QDialog::reject); | ||||
200 | layout->addWidget(buttonBox); | ||||
201 | | ||||
202 | auto validator = new ProfileNameValidator(m_environmentProfileListModel, &dialog); | ||||
203 | connect(edit, &QLineEdit::textChanged, validator, [validator, okButton](const QString& text) { | ||||
204 | int pos; | ||||
205 | QString t(text); | ||||
206 | const bool isValidProfileName = (validator->validate(t, pos) == QValidator::Acceptable); | ||||
207 | okButton->setEnabled(isValidProfileName); | ||||
208 | }); | ||||
209 | | ||||
210 | edit->setText(defaultName); | ||||
211 | edit->selectAll(); | ||||
212 | | ||||
213 | if (dialog.exec() != QDialog::Accepted) { | ||||
214 | return {}; | ||||
215 | } | ||||
216 | | ||||
217 | return edit->text(); | ||||
218 | } | ||||
219 | | ||||
220 | void EnvironmentWidget::removeSelectedVariables() | ||||
131 | { | 221 | { | ||
132 | QModelIndexList selected = ui.variableTable->selectionModel()->selectedRows(); | 222 | const auto selectedRows = ui.variableTable->selectionModel()->selectedRows(); | ||
133 | if( selected.isEmpty() ) | 223 | if (selectedRows.isEmpty()) { | ||
134 | return; | 224 | return; | ||
225 | } | ||||
135 | 226 | | |||
136 | QStringList variables; | 227 | QStringList variables; | ||
137 | foreach( const QModelIndex &idx, selected ) | 228 | for (const auto& idx : selectedRows) { | ||
138 | { | 229 | const QString variable = idx.data(EnvironmentProfileModel::VariableRole).toString(); | ||
139 | const QString variable = idx.data(EnvironmentGroupModel::VariableRole).toString(); | | |||
140 | variables << variable; | 230 | variables << variable; | ||
141 | } | 231 | } | ||
142 | 232 | | |||
143 | groupModel->removeVariables(variables); | 233 | m_environmentProfileModel->removeVariables(variables); | ||
144 | } | 234 | } | ||
145 | 235 | | |||
146 | void EnvironmentWidget::handleVariableInserted(int /*column*/, const QVariant& value) | 236 | void EnvironmentWidget::onVariableInserted(int column, const QVariant& value) | ||
147 | { | 237 | { | ||
148 | groupModel->addVariable(value.toString(), QString()); | 238 | Q_UNUSED(column); | ||
239 | m_environmentProfileModel->addVariable(value.toString(), QString()); | ||||
149 | } | 240 | } | ||
150 | 241 | | |||
151 | void EnvironmentWidget::batchModeEditButtonClicked() | 242 | void EnvironmentWidget::batchModeEditButtonClicked() | ||
152 | { | 243 | { | ||
153 | QDialog dialog(this); | 244 | QDialog dialog(this); | ||
154 | dialog.setWindowTitle( i18n( "Batch Edit Mode" ) ); | 245 | dialog.setWindowTitle( i18n( "Batch Edit Mode" ) ); | ||
155 | 246 | | |||
156 | QVBoxLayout *layout = new QVBoxLayout(&dialog); | 247 | QVBoxLayout *layout = new QVBoxLayout(&dialog); | ||
157 | 248 | | |||
158 | QTextEdit *edit = new QTextEdit; | 249 | QTextEdit *edit = new QTextEdit; | ||
159 | edit->setPlaceholderText(QStringLiteral("VARIABLE1=VALUE1\nVARIABLE2=VALUE2")); | 250 | edit->setPlaceholderText(QStringLiteral("VARIABLE1=VALUE1\nVARIABLE2=VALUE2")); | ||
160 | QString text; | 251 | QString text; | ||
161 | for (int i = 0; i < proxyModel->rowCount(); ++i) { | 252 | for (int i = 0; i < m_proxyModel->rowCount(); ++i) { | ||
162 | const auto variable = proxyModel->index(i, EnvironmentGroupModel::VariableColumn).data().toString(); | 253 | const auto variable = m_proxyModel->index(i, EnvironmentProfileModel::VariableColumn).data().toString(); | ||
163 | const auto value = proxyModel->index(i, EnvironmentGroupModel::ValueColumn).data().toString(); | 254 | const auto value = m_proxyModel->index(i, EnvironmentProfileModel::ValueColumn).data().toString(); | ||
164 | text.append(QStringLiteral("%1=%2\n").arg(variable, value)); | 255 | text.append(QStringLiteral("%1=%2\n").arg(variable, value)); | ||
165 | } | 256 | } | ||
166 | edit->setText(text); | 257 | edit->setText(text); | ||
167 | layout->addWidget( edit ); | 258 | layout->addWidget( edit ); | ||
168 | 259 | | |||
169 | auto buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok|QDialogButtonBox::Cancel); | 260 | auto buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok|QDialogButtonBox::Cancel); | ||
170 | auto okButton = buttonBox->button(QDialogButtonBox::Ok); | 261 | auto okButton = buttonBox->button(QDialogButtonBox::Ok); | ||
171 | okButton->setDefault(true); | 262 | okButton->setDefault(true); | ||
172 | okButton->setShortcut(Qt::CTRL | Qt::Key_Return); | 263 | okButton->setShortcut(Qt::CTRL | Qt::Key_Return); | ||
173 | dialog.connect(buttonBox, &QDialogButtonBox::accepted, &dialog, &QDialog::accept); | 264 | dialog.connect(buttonBox, &QDialogButtonBox::accepted, &dialog, &QDialog::accept); | ||
174 | dialog.connect(buttonBox, &QDialogButtonBox::rejected, &dialog, &QDialog::reject); | 265 | dialog.connect(buttonBox, &QDialogButtonBox::rejected, &dialog, &QDialog::reject); | ||
175 | layout->addWidget(buttonBox); | 266 | layout->addWidget(buttonBox); | ||
176 | 267 | | |||
177 | dialog.resize(600, 400); | 268 | dialog.resize(600, 400); | ||
178 | 269 | | |||
179 | if ( dialog.exec() != QDialog::Accepted ) { | 270 | if ( dialog.exec() != QDialog::Accepted ) { | ||
180 | return; | 271 | return; | ||
181 | } | 272 | } | ||
182 | 273 | | |||
183 | groupModel->loadEnvironmentFromString(edit->toPlainText()); | 274 | m_environmentProfileModel->setVariablesFromString(edit->toPlainText()); | ||
184 | } | 275 | } | ||
185 | 276 | | |||
186 | void EnvironmentWidget::addGroupClicked() | 277 | void EnvironmentWidget::addProfile() | ||
187 | { | 278 | { | ||
188 | QString curText = ui.activeCombo->currentText(); | 279 | const auto profileName = askNewProfileName(QString()); | ||
189 | if( groupModel->groups().contains( curText ) ) | 280 | if (profileName.isEmpty()) { | ||
190 | { | 281 | return; | ||
191 | return; // same group name cannot be added twice. | | |||
192 | } | 282 | } | ||
193 | ui.activeCombo->addItem( curText ); | 283 | | ||
194 | ui.activeCombo->setCurrentItem( curText ); | 284 | const int profileIndex = m_environmentProfileListModel->addProfile(profileName); | ||
285 | | ||||
286 | ui.profileSelect->setCurrentIndex(profileIndex); | ||||
287 | ui.variableTable->setFocus(Qt::OtherFocusReason); | ||||
195 | } | 288 | } | ||
196 | 289 | | |||
197 | void EnvironmentWidget::cloneGroupClicked() | 290 | void EnvironmentWidget::cloneSelectedProfile() | ||
198 | { | 291 | { | ||
199 | QString newGroup = ui.activeCombo->currentText(); | 292 | const int currentIndex = ui.profileSelect->currentIndex(); | ||
200 | if( !groupModel->cloneCurrentGroup( newGroup ) ) { | 293 | const auto currentProfileName = m_environmentProfileListModel->profileName(currentIndex); | ||
201 | const KLocalizedString newGroupTemplate = | 294 | // pass original name as starting name, as the user might want to enter a variant of it | ||
202 | ki18nc("a copy of the existing environment was created", "%1 (Cloned %2)").subs(newGroup); | 295 | const auto profileName = askNewProfileName(currentProfileName); | ||
203 | for (int id = 1; ; ++id) { | 296 | if (profileName.isEmpty()) { | ||
204 | newGroup = newGroupTemplate.subs(id).toString(); | 297 | return; | ||
205 | if (groupModel->cloneCurrentGroup(newGroup)) { | | |||
206 | break; | | |||
207 | } | | |||
208 | } | | |||
209 | } | 298 | } | ||
210 | ui.activeCombo->addItem( newGroup ); | 299 | | ||
211 | ui.activeCombo->setCurrentItem( newGroup ); | 300 | const int profileIndex = m_environmentProfileListModel->cloneProfile(profileName, currentProfileName); | ||
301 | | ||||
302 | ui.profileSelect->setCurrentIndex(profileIndex); | ||||
303 | ui.variableTable->setFocus(Qt::OtherFocusReason); | ||||
212 | } | 304 | } | ||
213 | 305 | | |||
214 | void EnvironmentWidget::removeGroupClicked() | 306 | void EnvironmentWidget::removeSelectedProfile() | ||
215 | { | | |||
216 | int idx = ui.activeCombo->currentIndex(); | | |||
217 | if( idx < 0 || ui.activeCombo->count() == 1 ) | | |||
218 | { | 307 | { | ||
308 | if (ui.profileSelect->count() <= 1) { | ||||
219 | return; | 309 | return; | ||
220 | } | 310 | } | ||
221 | 311 | | |||
222 | QString curText = ui.activeCombo->currentText(); | 312 | const int selectedProfileIndex = ui.profileSelect->currentIndex(); | ||
223 | groupModel->removeGroup( curText ); | 313 | | ||
224 | ui.activeCombo->removeItem( idx ); | 314 | m_environmentProfileListModel->removeProfile(selectedProfileIndex); | ||
225 | ui.activeCombo->setCurrentItem( groupModel->defaultGroup() ); | 315 | | ||
316 | const int defaultProfileIndex = m_environmentProfileListModel->defaultProfileIndex(); | ||||
317 | ui.profileSelect->setCurrentIndex(defaultProfileIndex); | ||||
226 | } | 318 | } | ||
227 | 319 | | |||
228 | void EnvironmentWidget::activeGroupChanged( int /*idx*/ ) | 320 | void EnvironmentWidget::onDefaultProfileChanged(int defaultProfileIndex) | ||
229 | { | 321 | { | ||
230 | groupModel->setCurrentGroup( ui.activeCombo->currentText() ); | 322 | const int selectedProfileIndex = ui.profileSelect->currentIndex(); | ||
231 | enableButtons( ui.activeCombo->currentText() ); | 323 | const bool isDefaultProfile = (defaultProfileIndex == selectedProfileIndex); | ||
324 | | ||||
325 | ui.removeProfileButton->setEnabled(ui.profileSelect->count() > 1 && !isDefaultProfile); | ||||
326 | ui.setAsDefaultProfileButton->setEnabled(!isDefaultProfile); | ||||
232 | } | 327 | } | ||
233 | 328 | | |||
234 | void EnvironmentWidget::enableButtons( const QString& txt ) | 329 | void EnvironmentWidget::onSelectedProfileChanged(int selectedProfileIndex) | ||
235 | { | 330 | { | ||
236 | ui.addgrpBtn->setEnabled( !groupModel->groups().contains( txt ) ); | 331 | const auto selectedProfileName = m_environmentProfileListModel->profileName(selectedProfileIndex); | ||
237 | ui.removegrpBtn->setEnabled( ( groupModel->groups().contains( txt ) && groupModel->defaultGroup() != txt ) ); | 332 | m_environmentProfileModel->setCurrentProfile(selectedProfileName); | ||
238 | ui.setAsDefaultBtn->setEnabled( ( groupModel->groups().contains( txt ) && groupModel->defaultGroup() != txt ) ); | | |||
239 | } | | |||
240 | 333 | | |||
334 | const bool isDefaultProfile = (m_environmentProfileListModel->defaultProfileIndex() == selectedProfileIndex); | ||||
241 | 335 | | |||
336 | ui.removeProfileButton->setEnabled(ui.profileSelect->count() > 1 && !isDefaultProfile); | ||||
337 | ui.setAsDefaultProfileButton->setEnabled(!isDefaultProfile); | ||||
242 | } | 338 | } | ||
243 | 339 | | |||
244 | #include "moc_environmentwidget.cpp" | 340 | #include "environmentwidget.moc" |