diff --git a/src/EditProfileDialog.h b/src/EditProfileDialog.h --- a/src/EditProfileDialog.h +++ b/src/EditProfileDialog.h @@ -91,6 +91,8 @@ // reimplemented void reject() Q_DECL_OVERRIDE; + void apply(); + protected: bool eventFilter(QObject *watched, QEvent *event) Q_DECL_OVERRIDE; @@ -240,6 +242,10 @@ }; void setupCheckBoxes(BooleanOption *options, const Profile::Ptr profile); + // returns false if the profile name is empty or if the name matches + // the name of an already existing profile; otherwise return true. + bool isValidProfileName(); + Ui::EditProfileDialog *_ui; Profile::Ptr _tempProfile; Profile::Ptr _profile; diff --git a/src/EditProfileDialog.cpp b/src/EditProfileDialog.cpp --- a/src/EditProfileDialog.cpp +++ b/src/EditProfileDialog.cpp @@ -83,7 +83,7 @@ connect(mButtonBox->button(QDialogButtonBox::Apply), &QPushButton::clicked, this, - &Konsole::EditProfileDialog::save); + &Konsole::EditProfileDialog::apply); connect(_delayedPreviewTimer, &QTimer::timeout, this, &Konsole::EditProfileDialog::delayedPreviewActivate); @@ -142,21 +142,57 @@ } void EditProfileDialog::accept() +{ + if (!isValidProfileName()) { + return; + } + save(); + unpreviewAll(); + QDialog::accept(); +} + +void EditProfileDialog::apply() +{ + if (!isValidProfileName()) { + return; + } + save(); +} + +bool EditProfileDialog::isValidProfileName() { Q_ASSERT(_profile); Q_ASSERT(_tempProfile); + const QList existingProfiles = ProfileManager::instance()->allProfiles(); + QStringList otherExistingProfileNames; + + foreach(auto existingProfile, existingProfiles) { + if (existingProfile->name() != _profile->name()) { + otherExistingProfileNames.append(existingProfile->name()); + } + } + if ((_tempProfile->isPropertySet(Profile::Name) && _tempProfile->name().isEmpty()) || (_profile->name().isEmpty() && _tempProfile->name().isEmpty())) { KMessageBox::sorry(this, i18n("

Each profile must have a name before it can be saved " "into disk.

")); - return; + // revert the name in the dialog + _ui->profileNameEdit->setText(_profile->name()); + selectProfileName(); + return false; + } else if (!_tempProfile->name().isEmpty() && otherExistingProfileNames.contains(_tempProfile->name())) { + KMessageBox::sorry(this, + i18n("

A profile with this name already exists.

")); + // revert the name in the dialog + _ui->profileNameEdit->setText(_profile->name()); + selectProfileName(); + return false; + } else { + return true; } - save(); - unpreviewAll(); - QDialog::accept(); } QString EditProfileDialog::groupProfileNames(const ProfileGroup::Ptr group, int maxLength) diff --git a/src/ProfileManager.cpp b/src/ProfileManager.cpp --- a/src/ProfileManager.cpp +++ b/src/ProfileManager.cpp @@ -342,6 +342,8 @@ { Q_ASSERT(profile); + const QString origPath = profile->path(); + // never save a profile with empty name into disk! persistent = persistent && !profile->name().isEmpty(); @@ -411,6 +413,25 @@ // it has no file on disk if (persistent && !newProfile->isHidden()) { newProfile->setProperty(Profile::Path, saveProfile(newProfile)); + // if the profile was renamed, after saving the new profile + // delete the the old/redundant profile. + // only do this if origPath is not empty, because it's empty + // when creating a new profile, this works around a bug where + // the newly created profile appears twice in the ProfileSettings + // dialog + if (!origPath.isEmpty() && (newProfile->path() != origPath)) { + // this is needed to include the old profile too + _loadedAllProfiles = false; + const QList availableProfiles = ProfileManager::instance()->allProfiles(); + foreach(auto oldProfile, availableProfiles) { + if (oldProfile->path() == origPath) { + // assign the same shortcut of the old profile to + // the newly renamed profile + setShortcut(newProfile, shortcut(oldProfile)); + deleteProfile(oldProfile); + } + } + } } // notify the world about the change diff --git a/src/settings/ProfileSettings.h b/src/settings/ProfileSettings.h --- a/src/settings/ProfileSettings.h +++ b/src/settings/ProfileSettings.h @@ -87,6 +87,9 @@ void updateItems(const Profile::Ptr); void removeItems(const Profile::Ptr); + // double clicking the profile name opens the edit profile dialog + void doubleClicked(const QModelIndex &index); + private: Profile::Ptr currentProfile() const; QList selectedProfiles() const; diff --git a/src/settings/ProfileSettings.cpp b/src/settings/ProfileSettings.cpp --- a/src/settings/ProfileSettings.cpp +++ b/src/settings/ProfileSettings.cpp @@ -55,6 +55,9 @@ sessionTable->setItemDelegateForColumn(ShortcutColumn, new ShortcutItemDelegate(this)); sessionTable->setEditTriggers(sessionTable->editTriggers() | QAbstractItemView::SelectedClicked); + // double clicking the profile name opens the profile edit dialog + connect(sessionTable, &QTableView::doubleClicked, this, &Konsole::ProfileSettings::doubleClicked); + // populate the table with profiles populateTable(); @@ -90,18 +93,6 @@ QKeySequence sequence = QKeySequence::fromString(item->text()); ProfileManager::instance()->setShortcut(item->data(ShortcutRole).value(), sequence); - } else if (item->column() == ProfileNameColumn) { - QString newName = item->text(); - Profile::Ptr profile = item->data(ProfileKeyRole).value(); - QString oldName = profile->name(); - - if (newName != oldName) { - QHash properties; - properties.insert(Profile::Name, newName); - properties.insert(Profile::UntranslatedName, newName); - - ProfileManager::instance()->changeProfile(profile, properties); - } } } @@ -144,7 +135,9 @@ if (!profile->icon().isEmpty()) items[ProfileNameColumn]->setIcon(QIcon::fromTheme(profile->icon())); items[ProfileNameColumn]->setData(QVariant::fromValue(profile), ProfileKeyRole); - items[ProfileNameColumn]->setToolTip(i18nc("@info:tooltip", "Click to rename profile")); + // only allow renaming the profile from the edit profile dialog + // so as to use ProfileManager::checkProfileName() + items[ProfileNameColumn]->setEditable(false); // Favorite Status const bool isFavorite = ProfileManager::instance()->findFavorites().contains(profile); @@ -161,6 +154,15 @@ items[ShortcutColumn]->setData(QVariant::fromValue(profile), ShortcutRole); items[ShortcutColumn]->setToolTip(i18nc("@info:tooltip", "Double click to change shortcut")); } + +void ProfileSettings::doubleClicked(const QModelIndex &index) +{ + QStandardItem *item = _sessionModel->itemFromIndex(index); + if (item->column() == ProfileNameColumn) { + editSelected(); + } +} + void ProfileSettings::addItems(const Profile::Ptr profile) { if (profile->isHidden())