Changeset View
Changeset View
Standalone View
Standalone View
src/settings/ProfileSettings.cpp
Show All 13 Lines | 1 | /* | |||
---|---|---|---|---|---|
14 | You should have received a copy of the GNU General Public License | 14 | You should have received a copy of the GNU General Public License | ||
15 | along with this program; if not, write to the Free Software | 15 | along with this program; if not, write to the Free Software | ||
16 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | 16 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | ||
17 | 02110-1301 USA. | 17 | 02110-1301 USA. | ||
18 | */ | 18 | */ | ||
19 | 19 | | |||
20 | // Own | 20 | // Own | ||
21 | #include "ProfileSettings.h" | 21 | #include "ProfileSettings.h" | ||
22 | #include <QPainter> | ||||
22 | 23 | | |||
23 | // Qt | 24 | // Qt | ||
24 | #include <QFileInfo> | 25 | #include <QFileInfo> | ||
25 | #include <QStandardPaths> | 26 | #include <QStandardPaths> | ||
26 | #include <QStandardItem> | 27 | #include <QStandardItem> | ||
27 | 28 | | |||
28 | // KDE | 29 | // KDE | ||
29 | #include <KKeySequenceWidget> | 30 | #include <KKeySequenceWidget> | ||
Show All 12 Lines | |||||
42 | using namespace Konsole; | 43 | using namespace Konsole; | ||
43 | 44 | | |||
44 | ProfileSettings::ProfileSettings(QWidget* aParent) | 45 | ProfileSettings::ProfileSettings(QWidget* aParent) | ||
45 | : QWidget(aParent) | 46 | : QWidget(aParent) | ||
46 | , _sessionModel(new QStandardItemModel(this)) | 47 | , _sessionModel(new QStandardItemModel(this)) | ||
47 | { | 48 | { | ||
48 | setupUi(this); | 49 | setupUi(this); | ||
49 | 50 | | |||
50 | // hide vertical header | 51 | profilesList->setItemDelegateForColumn(ShortcutColumn, new ShortcutItemDelegate(this)); | ||
51 | sessionTable->verticalHeader()->hide(); | | |||
52 | sessionTable->setShowGrid(false); | | |||
53 | | ||||
54 | sessionTable->setItemDelegateForColumn(FavoriteStatusColumn, new FavoriteItemDelegate(this)); | | |||
55 | sessionTable->setItemDelegateForColumn(ShortcutColumn, new ShortcutItemDelegate(this)); | | |||
56 | sessionTable->setEditTriggers(sessionTable->editTriggers() | QAbstractItemView::SelectedClicked); | | |||
57 | 52 | | |||
58 | // double clicking the profile name opens the profile edit dialog | 53 | // double clicking the profile name opens the profile edit dialog | ||
59 | connect(sessionTable, &QTableView::doubleClicked, this, &Konsole::ProfileSettings::doubleClicked); | 54 | connect(profilesList, &QAbstractItemView::doubleClicked, this, &Konsole::ProfileSettings::doubleClicked); | ||
60 | 55 | | |||
61 | // populate the table with profiles | 56 | // populate the table with profiles | ||
62 | populateTable(); | 57 | populateTable(); | ||
63 | 58 | | |||
64 | // listen for changes to profiles | 59 | // listen for changes to profiles | ||
65 | connect(ProfileManager::instance(), &Konsole::ProfileManager::profileAdded, this, &Konsole::ProfileSettings::addItems); | 60 | connect(ProfileManager::instance(), &Konsole::ProfileManager::profileAdded, this, &Konsole::ProfileSettings::addItems); | ||
66 | connect(ProfileManager::instance(), &Konsole::ProfileManager::profileRemoved, this, &Konsole::ProfileSettings::removeItems); | 61 | connect(ProfileManager::instance(), &Konsole::ProfileManager::profileRemoved, this, &Konsole::ProfileSettings::removeItems); | ||
67 | connect(ProfileManager::instance(), &Konsole::ProfileManager::profileChanged, this, &Konsole::ProfileSettings::updateItems); | 62 | connect(ProfileManager::instance(), &Konsole::ProfileManager::profileChanged, this, &Konsole::ProfileSettings::updateItems); | ||
68 | connect(ProfileManager::instance() , &Konsole::ProfileManager::favoriteStatusChanged, this, &Konsole::ProfileSettings::updateFavoriteStatus); | 63 | connect(ProfileManager::instance(), &Konsole::ProfileManager::favoriteStatusChanged, this, &Konsole::ProfileSettings::updateFavoriteStatus); | ||
69 | 64 | | |||
70 | // resize the session table to the full width of the table | | |||
71 | sessionTable->horizontalHeader()->setHighlightSections(false); | | |||
72 | sessionTable->horizontalHeader()->setStretchLastSection(true); | | |||
73 | sessionTable->resizeColumnsToContents(); | | |||
74 | | ||||
75 | // setup buttons | 65 | // setup buttons | ||
76 | connect(newProfileButton, &QPushButton::clicked, this, &Konsole::ProfileSettings::createProfile); | 66 | connect(newProfileButton, &QPushButton::clicked, this, &Konsole::ProfileSettings::createProfile); | ||
77 | connect(editProfileButton, &QPushButton::clicked, this, &Konsole::ProfileSettings::editSelected); | 67 | connect(editProfileButton, &QPushButton::clicked, this, &Konsole::ProfileSettings::editSelected); | ||
78 | connect(deleteProfileButton, &QPushButton::clicked, this, &Konsole::ProfileSettings::deleteSelected); | 68 | connect(deleteProfileButton, &QPushButton::clicked, this, &Konsole::ProfileSettings::deleteSelected); | ||
79 | connect(setAsDefaultButton, &QPushButton::clicked, this, &Konsole::ProfileSettings::setSelectedAsDefault); | 69 | connect(setAsDefaultButton, &QPushButton::clicked, this, &Konsole::ProfileSettings::setSelectedAsDefault); | ||
80 | } | 70 | } | ||
81 | 71 | | |||
82 | ProfileSettings::~ProfileSettings() = default; | 72 | ProfileSettings::~ProfileSettings() = default; | ||
83 | 73 | | |||
84 | void ProfileSettings::slotAccepted() | 74 | void ProfileSettings::slotAccepted() | ||
85 | { | 75 | { | ||
86 | ProfileManager::instance()->saveSettings(); | 76 | ProfileManager::instance()->saveSettings(); | ||
87 | deleteLater(); | 77 | deleteLater(); | ||
88 | } | 78 | } | ||
89 | 79 | | |||
90 | void ProfileSettings::itemDataChanged(QStandardItem* item) | 80 | void ProfileSettings::itemDataChanged(QStandardItem* item) | ||
91 | { | 81 | { | ||
92 | if (item->column() == ShortcutColumn) { | 82 | if (item->column() == ShortcutColumn) { | ||
93 | QKeySequence sequence = QKeySequence::fromString(item->text()); | 83 | QKeySequence sequence = QKeySequence::fromString(item->text()); | ||
94 | ProfileManager::instance()->setShortcut(item->data(ShortcutRole).value<Profile::Ptr>(), | 84 | ProfileManager::instance()->setShortcut(item->data(ShortcutRole).value<Profile::Ptr>(), | ||
95 | sequence); | 85 | sequence); | ||
86 | } else if (item->column() == ProfileNameColumn) { | ||||
87 | ProfileManager::instance()->setFavorite(item->data(ProfileKeyRole).value<Profile::Ptr>(), | ||||
88 | item->checkState() == Qt::Checked); | ||||
96 | } | 89 | } | ||
97 | } | 90 | } | ||
98 | 91 | | |||
99 | int ProfileSettings::rowForProfile(const Profile::Ptr &profile) const | 92 | int ProfileSettings::rowForProfile(const Profile::Ptr &profile) const | ||
100 | { | 93 | { | ||
101 | const int rowCount = _sessionModel->rowCount(); | 94 | const int rowCount = _sessionModel->rowCount(); | ||
102 | for (int i = 0; i < rowCount; i++) { | 95 | for (int i = 0; i < rowCount; i++) { | ||
103 | if (_sessionModel->item(i, ProfileNameColumn)->data(ProfileKeyRole) | 96 | if (_sessionModel->item(i, ProfileNameColumn)->data(ProfileKeyRole) | ||
Show All 15 Lines | |||||
119 | void ProfileSettings::updateItems(const Profile::Ptr &profile) | 112 | void ProfileSettings::updateItems(const Profile::Ptr &profile) | ||
120 | { | 113 | { | ||
121 | const int row = rowForProfile(profile); | 114 | const int row = rowForProfile(profile); | ||
122 | if (row < 0) { | 115 | if (row < 0) { | ||
123 | return; | 116 | return; | ||
124 | } | 117 | } | ||
125 | 118 | | |||
126 | const auto items = QList<QStandardItem*> { | 119 | const auto items = QList<QStandardItem*> { | ||
127 | _sessionModel->item(row, ProfileNameColumn), | | |||
128 | _sessionModel->item(row, FavoriteStatusColumn), | 120 | _sessionModel->item(row, FavoriteStatusColumn), | ||
121 | _sessionModel->item(row, ProfileNameColumn), | ||||
129 | _sessionModel->item(row, ShortcutColumn) | 122 | _sessionModel->item(row, ShortcutColumn) | ||
130 | }; | 123 | }; | ||
131 | updateItemsForProfile(profile, items); | 124 | updateItemsForProfile(profile, items); | ||
132 | } | 125 | } | ||
133 | void ProfileSettings::updateItemsForProfile(const Profile::Ptr &profile, const QList<QStandardItem*>& items) const | 126 | void ProfileSettings::updateItemsForProfile(const Profile::Ptr &profile, const QList<QStandardItem*>& items) const | ||
134 | { | 127 | { | ||
128 | // "Enabled" checkbox | ||||
129 | const auto isEnabled = ProfileManager::instance()->findFavorites().contains(profile); | ||||
130 | items[FavoriteStatusColumn]->setCheckState(isEnabled ? Qt::Checked : Qt::Unchecked); | ||||
131 | items[FavoriteStatusColumn]->setCheckable(true); | ||||
132 | items[FavoriteStatusColumn]->setToolTip( | ||||
133 | i18nc("@info:tooltip List item's checkbox for making item (profile) visible in a menu", | ||||
134 | "Show profile in menu")); | ||||
135 | | ||||
135 | // Profile Name | 136 | // Profile Name | ||
136 | items[ProfileNameColumn]->setText(profile->name()); | 137 | items[ProfileNameColumn]->setText(profile->name()); | ||
137 | if (!profile->icon().isEmpty()) { | 138 | if (!profile->icon().isEmpty()) { | ||
138 | items[ProfileNameColumn]->setIcon(QIcon::fromTheme(profile->icon())); | 139 | items[ProfileNameColumn]->setIcon(QIcon::fromTheme(profile->icon())); | ||
139 | } | 140 | } | ||
140 | items[ProfileNameColumn]->setData(QVariant::fromValue(profile), ProfileKeyRole); | 141 | items[ProfileNameColumn]->setData(QVariant::fromValue(profile), ProfileKeyRole); | ||
141 | // only allow renaming the profile from the edit profile dialog | 142 | // only allow renaming the profile from the edit profile dialog | ||
142 | // so as to use ProfileManager::checkProfileName() | 143 | // so as to use ProfileManager::checkProfileName() | ||
143 | items[ProfileNameColumn]->setEditable(false); | 144 | items[ProfileNameColumn]->setEditable(false); | ||
144 | 145 | | |||
145 | // Favorite Status | | |||
146 | const auto isFavorite = ProfileManager::instance()->findFavorites().contains(profile); | | |||
147 | const auto icon = isFavorite ? QIcon::fromTheme(QStringLiteral("dialog-ok-apply")) : QIcon(); | | |||
148 | items[FavoriteStatusColumn]->setData(icon, Qt::DecorationRole); | | |||
149 | items[FavoriteStatusColumn]->setData(QVariant::fromValue(profile), ProfileKeyRole); | | |||
150 | items[FavoriteStatusColumn]->setToolTip(i18nc("@info:tooltip", "Click to toggle status")); | | |||
151 | | ||||
152 | // Shortcut | 146 | // Shortcut | ||
153 | const auto shortcut = ProfileManager::instance()->shortcut(profile).toString(); | 147 | const auto shortcut = ProfileManager::instance()->shortcut(profile).toString(); | ||
154 | items[ShortcutColumn]->setText(shortcut); | 148 | items[ShortcutColumn]->setText(shortcut); | ||
155 | items[ShortcutColumn]->setData(QVariant::fromValue(profile), ShortcutRole); | 149 | items[ShortcutColumn]->setData(QVariant::fromValue(profile), ShortcutRole); | ||
156 | items[ShortcutColumn]->setToolTip(i18nc("@info:tooltip", "Double click to change shortcut")); | 150 | items[ShortcutColumn]->setToolTip(i18nc("@info:tooltip", "Double click to change shortcut")); | ||
157 | } | 151 | } | ||
158 | 152 | | |||
159 | void ProfileSettings::doubleClicked(const QModelIndex &index) | 153 | void ProfileSettings::doubleClicked(const QModelIndex &index) | ||
Show All 17 Lines | 168 | const auto items = QList<QStandardItem*> { | |||
177 | new QStandardItem() | 171 | new QStandardItem() | ||
178 | }; | 172 | }; | ||
179 | 173 | | |||
180 | updateItemsForProfile(profile, items); | 174 | updateItemsForProfile(profile, items); | ||
181 | _sessionModel->appendRow(items); | 175 | _sessionModel->appendRow(items); | ||
182 | } | 176 | } | ||
183 | void ProfileSettings::populateTable() | 177 | void ProfileSettings::populateTable() | ||
184 | { | 178 | { | ||
185 | Q_ASSERT(!sessionTable->model()); | 179 | Q_ASSERT(!profilesList->model()); | ||
186 | 180 | | |||
187 | sessionTable->setModel(_sessionModel); | 181 | profilesList->setModel(_sessionModel); | ||
188 | 182 | | |||
189 | _sessionModel->clear(); | 183 | _sessionModel->clear(); | ||
190 | // setup session table | 184 | // setup session table | ||
191 | _sessionModel->setHorizontalHeaderLabels({i18nc("@title:column Profile label", "Name"), | 185 | _sessionModel->setHorizontalHeaderLabels({ | ||
192 | i18nc("@title:column Display profile in file menu", "Show"), | 186 | QString(), // set using header item below | ||
193 | i18nc("@title:column Profile shortcut text", "Shortcut")}); | 187 | i18nc("@title:column Profile name", "Name"), | ||
188 | i18nc("@title:column Profile keyboard shortcut", "Shortcut") | ||||
189 | }); | ||||
190 | auto *favoriteColumnHeaderItem = new QStandardItem(); | ||||
191 | favoriteColumnHeaderItem->setIcon(QIcon::fromTheme(QStringLiteral("visibility"))); | ||||
192 | favoriteColumnHeaderItem->setToolTip( | ||||
193 | i18nc("@info:tooltip List item's checkbox for making item (profile) visible in a menu", | ||||
194 | "Show profile in menu")); | ||||
195 | _sessionModel->setHorizontalHeaderItem(FavoriteStatusColumn, favoriteColumnHeaderItem); | ||||
196 | | ||||
197 | // Calculate favorite column width. resizeColumnToContents() | ||||
198 | // is not used because it takes distance between checkbox and | ||||
199 | // text into account, but there is no text and it looks weird. | ||||
200 | const int headerMargin = style()->pixelMetric(QStyle::PM_HeaderMargin, nullptr, | ||||
201 | profilesList->header()); | ||||
202 | const int iconWidth = style()->pixelMetric(QStyle::PM_SmallIconSize, nullptr, | ||||
203 | profilesList->header()); | ||||
204 | const int favoriteHeaderWidth = headerMargin * 2 + iconWidth; | ||||
205 | QStyleOptionViewItem opt; | ||||
206 | opt.features = QStyleOptionViewItem::HasCheckIndicator | QStyleOptionViewItem::HasDecoration; | ||||
207 | const QRect checkBoxRect = style()->subElementRect(QStyle::SE_ItemViewItemCheckIndicator, | ||||
208 | &opt, profilesList); | ||||
209 | // When right edge is at x < 0 it is assumed the checkbox is | ||||
210 | // placed on the right item's side and the margin between right | ||||
211 | // checkbox edge and right item edge should be used. | ||||
212 | const int checkBoxMargin = checkBoxRect.right() >= 0 ? checkBoxRect.x() | ||||
213 | : 0 - checkBoxRect.right(); | ||||
214 | const int favoriteItemWidth = checkBoxMargin * 2 + checkBoxRect.width(); | ||||
215 | | ||||
216 | profilesList->setColumnWidth(FavoriteStatusColumn, | ||||
217 | qMax(favoriteHeaderWidth, favoriteItemWidth)); | ||||
218 | profilesList->header()->setSectionResizeMode(FavoriteStatusColumn, | ||||
219 | QHeaderView::ResizeMode::Fixed); | ||||
194 | 220 | | |||
195 | QList<Profile::Ptr> profiles = ProfileManager::instance()->allProfiles(); | 221 | QList<Profile::Ptr> profiles = ProfileManager::instance()->allProfiles(); | ||
196 | ProfileManager::instance()->sortProfiles(profiles); | 222 | ProfileManager::instance()->sortProfiles(profiles); | ||
197 | 223 | | |||
198 | foreach(const Profile::Ptr& profile, profiles) { | 224 | foreach(const Profile::Ptr& profile, profiles) { | ||
199 | addItems(profile); | 225 | addItems(profile); | ||
200 | } | 226 | } | ||
201 | updateDefaultItem(); | 227 | updateDefaultItem(); | ||
228 | profilesList->resizeColumnToContents(ProfileNameColumn); | ||||
202 | 229 | | |||
203 | connect(_sessionModel, &QStandardItemModel::itemChanged, this, &Konsole::ProfileSettings::itemDataChanged); | 230 | connect(_sessionModel, &QStandardItemModel::itemChanged, this, &Konsole::ProfileSettings::itemDataChanged); | ||
204 | 231 | | |||
205 | // listen for changes in the table selection and update the state of the form's buttons | 232 | // listen for changes in the table selection and update the state of the form's buttons | ||
206 | // accordingly. | 233 | // accordingly. | ||
207 | // | 234 | // | ||
208 | // it appears that the selection model is changed when the model itself is replaced, | 235 | // it appears that the selection model is changed when the model itself is replaced, | ||
209 | // so the signals need to be reconnected each time the model is updated. | 236 | // so the signals need to be reconnected each time the model is updated. | ||
210 | connect(sessionTable->selectionModel(), &QItemSelectionModel::selectionChanged, this, &Konsole::ProfileSettings::tableSelectionChanged); | 237 | connect(profilesList->selectionModel(), &QItemSelectionModel::selectionChanged, this, &Konsole::ProfileSettings::tableSelectionChanged); | ||
211 | | ||||
212 | sessionTable->selectRow(0); | | |||
213 | } | 238 | } | ||
214 | void ProfileSettings::updateDefaultItem() | 239 | void ProfileSettings::updateDefaultItem() | ||
215 | { | 240 | { | ||
216 | Profile::Ptr defaultProfile = ProfileManager::instance()->defaultProfile(); | 241 | Profile::Ptr defaultProfile = ProfileManager::instance()->defaultProfile(); | ||
217 | 242 | | |||
243 | const QString defaultItemSuffix = i18nc("Default list item's name suffix (with separator)", " (default)"); | ||||
244 | | ||||
218 | const int rowCount = _sessionModel->rowCount(); | 245 | const int rowCount = _sessionModel->rowCount(); | ||
219 | for (int i = 0; i < rowCount; i++) { | 246 | for (int i = 0; i < rowCount; i++) { | ||
220 | QStandardItem* item = _sessionModel->item(i); | 247 | QStandardItem* item = _sessionModel->item(i, ProfileNameColumn); | ||
221 | QFont itemFont = item->font(); | 248 | QFont itemFont = item->font(); | ||
249 | auto profile = item->data().value<Profile::Ptr>(); | ||||
250 | const bool isDefault = (defaultProfile == profile); | ||||
251 | const QString cleanItemName = profile != nullptr ? profile->name() : QString(); | ||||
222 | 252 | | |||
223 | bool isDefault = (defaultProfile == item->data().value<Profile::Ptr>()); | 253 | if (isDefault) { | ||
224 | 254 | itemFont.setItalic(true); | |||
225 | if (isDefault && !itemFont.bold()) { | | |||
226 | QIcon icon(KIconLoader::global()->loadIcon(defaultProfile->icon(), KIconLoader::Small, 0, KIconLoader::DefaultState, QStringList(QStringLiteral("emblem-favorite")))); | | |||
227 | item->setIcon(icon); | | |||
228 | itemFont.setBold(true); | | |||
229 | item->setFont(itemFont); | 255 | item->setFont(itemFont); | ||
230 | } else if (!isDefault && itemFont.bold()) { | 256 | item->setText(cleanItemName + defaultItemSuffix); | ||
231 | QModelIndex index = _sessionModel->index(i, ProfileNameColumn); | 257 | } else if (!isDefault) { | ||
232 | Profile::Ptr profile = index.data(ProfileSettings::ProfileKeyRole).value<Profile::Ptr>(); | 258 | // FIXME: use default font | ||
233 | if (profile != nullptr) { | 259 | itemFont.setItalic(false); | ||
234 | item->setIcon(QIcon::fromTheme(profile->icon())); | | |||
235 | } | | |||
236 | itemFont.setBold(false); | | |||
237 | item->setFont(itemFont); | 260 | item->setFont(itemFont); | ||
261 | item->setText(cleanItemName); | ||||
238 | } | 262 | } | ||
239 | } | 263 | } | ||
240 | } | 264 | } | ||
241 | void ProfileSettings::tableSelectionChanged(const QItemSelection&) | 265 | void ProfileSettings::tableSelectionChanged(const QItemSelection&) | ||
242 | { | 266 | { | ||
243 | const int selectedRows = sessionTable->selectionModel()->selectedRows().count(); | | |||
244 | const ProfileManager* manager = ProfileManager::instance(); | 267 | const ProfileManager* manager = ProfileManager::instance(); | ||
245 | const bool isNotDefault = (selectedRows > 0) && currentProfile() != manager->defaultProfile(); | 268 | bool isNotDefault = true; | ||
246 | const bool isDeletable = (selectedRows > 1) || | 269 | bool isDeletable = true; | ||
247 | (selectedRows == 1 && isProfileDeletable(currentProfile())); | | |||
248 | 270 | | |||
249 | newProfileButton->setEnabled(selectedRows < 2); | 271 | const auto profiles = selectedProfiles(); | ||
272 | for (const auto &profile: profiles) { | ||||
273 | isNotDefault = isNotDefault && (profile != manager->defaultProfile()); | ||||
274 | isDeletable = isDeletable && isProfileDeletable(profile); | ||||
275 | } | ||||
276 | | ||||
277 | newProfileButton->setEnabled(profiles.count() < 2); | ||||
250 | // FIXME: At some point editing 2+ profiles no longer works | 278 | // FIXME: At some point editing 2+ profiles no longer works | ||
251 | editProfileButton->setEnabled(selectedRows == 1); | 279 | editProfileButton->setEnabled(profiles.count() == 1); | ||
252 | // do not allow the default session type to be removed | 280 | // do not allow the default session type to be removed | ||
253 | deleteProfileButton->setEnabled(isDeletable && isNotDefault); | 281 | deleteProfileButton->setEnabled(isDeletable && isNotDefault && (profiles.count() > 0)); | ||
254 | setAsDefaultButton->setEnabled(isNotDefault && (selectedRows < 2)); | 282 | setAsDefaultButton->setEnabled(isNotDefault && (profiles.count() == 1)); | ||
255 | } | 283 | } | ||
256 | void ProfileSettings::deleteSelected() | 284 | void ProfileSettings::deleteSelected() | ||
257 | { | 285 | { | ||
258 | foreach(const Profile::Ptr & profile, selectedProfiles()) { | 286 | foreach(const Profile::Ptr & profile, selectedProfiles()) { | ||
259 | if (profile != ProfileManager::instance()->defaultProfile()) { | 287 | if (profile != ProfileManager::instance()->defaultProfile()) { | ||
260 | ProfileManager::instance()->deleteProfile(profile); | 288 | ProfileManager::instance()->deleteProfile(profile); | ||
261 | } | 289 | } | ||
262 | } | 290 | } | ||
263 | } | 291 | } | ||
264 | void ProfileSettings::setSelectedAsDefault() | 292 | void ProfileSettings::setSelectedAsDefault() | ||
265 | { | 293 | { | ||
266 | ProfileManager::instance()->setDefaultProfile(currentProfile()); | 294 | ProfileManager::instance()->setDefaultProfile(currentProfile()); | ||
267 | // do not allow the new default session type to be removed | 295 | // do not allow the new default session type to be removed | ||
268 | deleteProfileButton->setEnabled(false); | 296 | deleteProfileButton->setEnabled(false); | ||
269 | setAsDefaultButton->setEnabled(false); | 297 | setAsDefaultButton->setEnabled(false); | ||
270 | 298 | | |||
271 | // update font of new default item | 299 | // update font of new default item | ||
272 | updateDefaultItem(); | 300 | updateDefaultItem(); | ||
273 | } | 301 | } | ||
274 | 302 | | |||
275 | void ProfileSettings::moveUpSelected() | | |||
276 | { | | |||
277 | Q_ASSERT(_sessionModel); | | |||
278 | | ||||
279 | const int rowIndex = sessionTable->currentIndex().row(); | | |||
280 | const QList<QStandardItem*>items = _sessionModel->takeRow(rowIndex); | | |||
281 | _sessionModel->insertRow(rowIndex - 1, items); | | |||
282 | sessionTable->selectRow(rowIndex - 1); | | |||
283 | } | | |||
284 | | ||||
285 | void ProfileSettings::moveDownSelected() | | |||
286 | { | | |||
287 | Q_ASSERT(_sessionModel); | | |||
288 | | ||||
289 | const int rowIndex = sessionTable->currentIndex().row(); | | |||
290 | const QList<QStandardItem*>items = _sessionModel->takeRow(rowIndex); | | |||
291 | _sessionModel->insertRow(rowIndex + 1, items); | | |||
292 | sessionTable->selectRow(rowIndex + 1); | | |||
293 | } | | |||
294 | | ||||
295 | void ProfileSettings::createProfile() | 303 | void ProfileSettings::createProfile() | ||
296 | { | 304 | { | ||
297 | // setup a temporary profile which is a clone of the selected profile | 305 | // setup a temporary profile which is a clone of the selected profile | ||
298 | // or the default if no profile is selected | 306 | // or the default if no profile is selected | ||
299 | Profile::Ptr sourceProfile = currentProfile() ? currentProfile() : ProfileManager::instance()->defaultProfile(); | 307 | Profile::Ptr sourceProfile = currentProfile() ? currentProfile() : ProfileManager::instance()->defaultProfile(); | ||
300 | 308 | | |||
301 | Q_ASSERT(sourceProfile); | 309 | Q_ASSERT(sourceProfile); | ||
302 | 310 | | |||
303 | auto newProfile = Profile::Ptr(new Profile(ProfileManager::instance()->fallbackProfile())); | 311 | auto newProfile = Profile::Ptr(new Profile(ProfileManager::instance()->fallbackProfile())); | ||
304 | newProfile->clone(sourceProfile, true); | 312 | newProfile->clone(sourceProfile, true); | ||
313 | // TODO: add number suffix when the name is taken | ||||
305 | newProfile->setProperty(Profile::Name, i18nc("@item This will be used as part of the file name", "New Profile")); | 314 | newProfile->setProperty(Profile::Name, i18nc("@item This will be used as part of the file name", "New Profile")); | ||
306 | newProfile->setProperty(Profile::UntranslatedName, QStringLiteral("New Profile")); | 315 | newProfile->setProperty(Profile::UntranslatedName, QStringLiteral("New Profile")); | ||
307 | newProfile->setProperty(Profile::MenuIndex, QStringLiteral("0")); | 316 | newProfile->setProperty(Profile::MenuIndex, QStringLiteral("0")); | ||
308 | 317 | | |||
309 | // Consider https://blogs.kde.org/2009/03/26/how-crash-almost-every-qtkde-application-and-how-fix-it-0 before changing the below | 318 | // Consider https://blogs.kde.org/2009/03/26/how-crash-almost-every-qtkde-application-and-how-fix-it-0 before changing the below | ||
310 | QPointer<EditProfileDialog> dialog = new EditProfileDialog(this); | 319 | QPointer<EditProfileDialog> dialog = new EditProfileDialog(this); | ||
311 | dialog.data()->setProfile(newProfile); | 320 | dialog.data()->setProfile(newProfile); | ||
312 | dialog.data()->selectProfileName(); | 321 | dialog.data()->selectProfileName(); | ||
Show All 33 Lines | 331 | { | |||
346 | group->updateValues(); | 355 | group->updateValues(); | ||
347 | 356 | | |||
348 | dialog.setProfile(Profile::Ptr(group)); | 357 | dialog.setProfile(Profile::Ptr(group)); | ||
349 | dialog.exec(); | 358 | dialog.exec(); | ||
350 | } | 359 | } | ||
351 | QList<Profile::Ptr> ProfileSettings::selectedProfiles() const | 360 | QList<Profile::Ptr> ProfileSettings::selectedProfiles() const | ||
352 | { | 361 | { | ||
353 | QList<Profile::Ptr> list; | 362 | QList<Profile::Ptr> list; | ||
354 | QItemSelectionModel* selection = sessionTable->selectionModel(); | 363 | QItemSelectionModel* selection = profilesList->selectionModel(); | ||
355 | if (selection == nullptr) { | 364 | if (selection == nullptr) { | ||
356 | return list; | 365 | return list; | ||
357 | } | 366 | } | ||
358 | 367 | | |||
359 | foreach(const QModelIndex & index, selection->selectedIndexes()) { | 368 | foreach(const QModelIndex & index, selection->selectedIndexes()) { | ||
360 | if (index.column() == ProfileNameColumn) { | 369 | if (index.column() == ProfileNameColumn) { | ||
361 | list << index.data(ProfileKeyRole).value<Profile::Ptr>(); | 370 | list << index.data(ProfileKeyRole).value<Profile::Ptr>(); | ||
362 | } | 371 | } | ||
363 | } | 372 | } | ||
364 | 373 | | |||
365 | return list; | 374 | return list; | ||
366 | } | 375 | } | ||
367 | Profile::Ptr ProfileSettings::currentProfile() const | 376 | Profile::Ptr ProfileSettings::currentProfile() const | ||
368 | { | 377 | { | ||
369 | QItemSelectionModel* selection = sessionTable->selectionModel(); | 378 | QItemSelectionModel* selection = profilesList->selectionModel(); | ||
370 | 379 | | |||
371 | if ((selection == nullptr) || selection->selectedRows().count() != 1) { | 380 | if ((selection == nullptr) || selection->selectedRows().count() != 1) { | ||
372 | return Profile::Ptr(); | 381 | return Profile::Ptr(); | ||
373 | } | 382 | } | ||
374 | 383 | | |||
375 | return selection-> | 384 | return selection-> | ||
376 | selectedIndexes().first().data(ProfileKeyRole).value<Profile::Ptr>(); | 385 | selectedIndexes().at(ProfileNameColumn).data(ProfileKeyRole).value<Profile::Ptr>(); | ||
377 | } | 386 | } | ||
378 | bool ProfileSettings::isProfileDeletable(Profile::Ptr profile) const | 387 | bool ProfileSettings::isProfileDeletable(Profile::Ptr profile) const | ||
379 | { | 388 | { | ||
380 | if (!profile) { | 389 | if (!profile) { | ||
381 | return false; | 390 | return false; | ||
382 | } | 391 | } | ||
383 | 392 | | |||
384 | const QFileInfo fileInfo(profile->path()); | 393 | const QFileInfo fileInfo(profile->path()); | ||
385 | if (!fileInfo.exists()) { | 394 | if (!fileInfo.exists()) { | ||
386 | return false; | 395 | return false; | ||
387 | } | 396 | } | ||
388 | 397 | | |||
389 | const QFileInfo dirInfo(fileInfo.path()); | 398 | const QFileInfo dirInfo(fileInfo.path()); | ||
390 | return dirInfo.isWritable(); | 399 | return dirInfo.isWritable(); | ||
391 | } | 400 | } | ||
392 | void ProfileSettings::updateFavoriteStatus(const Profile::Ptr &profile, bool favorite) | 401 | void ProfileSettings::updateFavoriteStatus(const Profile::Ptr &profile, bool favorite) | ||
393 | { | 402 | { | ||
394 | Q_ASSERT(_sessionModel); | 403 | Q_ASSERT(_sessionModel); | ||
395 | 404 | | |||
396 | const int rowCount = _sessionModel->rowCount(); | 405 | const int rowCount = _sessionModel->rowCount(); | ||
397 | for (int i = 0; i < rowCount; i++) { | 406 | for (int i = 0; i < rowCount; i++) { | ||
398 | QModelIndex index = _sessionModel->index(i, FavoriteStatusColumn); | 407 | auto *item = _sessionModel->item(i, ProfileNameColumn); | ||
399 | if (index.data(ProfileKeyRole).value<Profile::Ptr>() == profile) { | 408 | if (item->data(ProfileKeyRole).value<Profile::Ptr>() == profile) { | ||
400 | // FIXME: On desktops without this icon, it is impossible to | 409 | auto *favoriteItem = _sessionModel->item(i, FavoriteStatusColumn); | ||
401 | // determine if a profile is a favorite in this dialog. | 410 | favoriteItem->setCheckState(favorite ? Qt::Checked : Qt::Unchecked); | ||
402 | // Consider changing to using QStandardItem::setCheckable | 411 | break; | ||
403 | const QIcon icon = favorite ? QIcon::fromTheme(QStringLiteral("dialog-ok-apply")) : QIcon(); | | |||
404 | _sessionModel->setData(index, icon, Qt::DecorationRole); | | |||
405 | } | 412 | } | ||
406 | } | 413 | } | ||
407 | } | 414 | } | ||
408 | void ProfileSettings::setShortcutEditorVisible(bool visible) | 415 | void ProfileSettings::setShortcutEditorVisible(bool visible) | ||
409 | { | 416 | { | ||
410 | sessionTable->setColumnHidden(ShortcutColumn, !visible); | 417 | profilesList->setColumnHidden(ShortcutColumn, !visible); | ||
411 | } | 418 | } | ||
412 | void StyledBackgroundPainter::drawBackground(QPainter* painter, const QStyleOptionViewItem& option, | 419 | void StyledBackgroundPainter::drawBackground(QPainter* painter, const QStyleOptionViewItem& option, | ||
413 | const QModelIndex&) | 420 | const QModelIndex&) | ||
414 | { | 421 | { | ||
415 | const auto* opt = qstyleoption_cast<const QStyleOptionViewItem*>(&option); | 422 | const auto* opt = qstyleoption_cast<const QStyleOptionViewItem*>(&option); | ||
416 | const QWidget* widget = opt != nullptr ? opt->widget : nullptr; | 423 | const QWidget* widget = opt != nullptr ? opt->widget : nullptr; | ||
417 | 424 | | |||
418 | QStyle* style = widget != nullptr ? widget->style() : QApplication::style(); | 425 | QStyle* style = widget != nullptr ? widget->style() : QApplication::style(); | ||
419 | 426 | | |||
420 | style->drawPrimitive(QStyle::PE_PanelItemViewItem, &option, painter, widget); | 427 | style->drawPrimitive(QStyle::PE_PanelItemViewItem, &option, painter, widget); | ||
421 | } | 428 | } | ||
422 | 429 | | |||
423 | // This adds a checkmark and the appropriate background in the "Show" | | |||
424 | // column of the Manage Profiles->Profiles page. | | |||
425 | FavoriteItemDelegate::FavoriteItemDelegate(QObject* aParent) | | |||
426 | : QStyledItemDelegate(aParent) | | |||
427 | { | | |||
428 | } | | |||
429 | void FavoriteItemDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const | | |||
430 | { | | |||
431 | // See implementation of QStyledItemDelegate::paint() | | |||
432 | QStyleOptionViewItem opt = option; | | |||
433 | initStyleOption(&opt, index); | | |||
434 | | ||||
435 | StyledBackgroundPainter::drawBackground(painter, opt, index); | | |||
436 | | ||||
437 | int margin = (opt.rect.height() - opt.decorationSize.height()) / 2; | | |||
438 | margin++; | | |||
439 | | ||||
440 | opt.rect.setTop(opt.rect.top() + margin); | | |||
441 | opt.rect.setBottom(opt.rect.bottom() - margin); | | |||
442 | | ||||
443 | QIcon icon = index.data(Qt::DecorationRole).value<QIcon>(); | | |||
444 | icon.paint(painter, opt.rect, Qt::AlignCenter); | | |||
445 | } | | |||
446 | | ||||
447 | bool FavoriteItemDelegate::editorEvent(QEvent* aEvent, QAbstractItemModel*, | | |||
448 | const QStyleOptionViewItem&, const QModelIndex& index) | | |||
449 | { | | |||
450 | if (aEvent->type() == QEvent::MouseButtonPress || | | |||
451 | aEvent->type() == QEvent::KeyPress || | | |||
452 | aEvent->type() == QEvent::MouseButtonDblClick) { | | |||
453 | Profile::Ptr profile = index.data(ProfileSettings::ProfileKeyRole).value<Profile::Ptr>(); | | |||
454 | const bool isFavorite = ProfileManager::instance()->findFavorites().contains(profile); | | |||
455 | | ||||
456 | ProfileManager::instance()->setFavorite(profile, !isFavorite); | | |||
457 | } | | |||
458 | | ||||
459 | return true; | | |||
460 | } | | |||
461 | ShortcutItemDelegate::ShortcutItemDelegate(QObject* aParent) | 430 | ShortcutItemDelegate::ShortcutItemDelegate(QObject* aParent) | ||
462 | : QStyledItemDelegate(aParent), | 431 | : QStyledItemDelegate(aParent), | ||
463 | _modifiedEditors(QSet<QWidget *>()), | 432 | _modifiedEditors(QSet<QWidget *>()), | ||
464 | _itemsBeingEdited(QSet<QModelIndex>()) | 433 | _itemsBeingEdited(QSet<QModelIndex>()) | ||
465 | { | 434 | { | ||
466 | } | 435 | } | ||
467 | void ShortcutItemDelegate::editorModified(const QKeySequence& keys) | 436 | void ShortcutItemDelegate::editorModified(const QKeySequence& keys) | ||
468 | { | 437 | { | ||
▲ Show 20 Lines • Show All 46 Lines • Show Last 20 Lines |