Commit 652e48cf authored by Kurt Hindenburg's avatar Kurt Hindenburg

Improvements to the Manage Profiles dialog

 - Double-clicking on name opens Edit Profile dialog
 - Only allow renaming from the Edit Profile dialog
 - Upon renaming of a profile, change shortcut to new profile name
 - Upon renaming of a profile, delete old named profile file.
 - Do not allow renaming of a profile if that name already exists.

Patch by ahmadsamir; I made a few changes (name/const).

Differential Revision: https://phabricator.kde.org/D9272
parent cc97a5e4
......@@ -83,7 +83,7 @@ EditProfileDialog::EditProfileDialog(QWidget *aParent) :
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::reject()
}
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<Profile::Ptr> 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("<p>Each profile must have a name before it can be saved "
"into disk.</p>"));
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("<p>A profile with this name already exists.</p>"));
// 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)
......
......@@ -91,6 +91,8 @@ public Q_SLOTS:
// reimplemented
void reject() Q_DECL_OVERRIDE;
void apply();
protected:
bool eventFilter(QObject *watched, QEvent *event) Q_DECL_OVERRIDE;
......@@ -240,6 +242,10 @@ private:
};
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;
......
......@@ -342,6 +342,8 @@ void ProfileManager::changeProfile(Profile::Ptr profile,
{
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 @@ void ProfileManager::changeProfile(Profile::Ptr profile,
// 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<Profile::Ptr> 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
......
......@@ -55,6 +55,9 @@ ProfileSettings::ProfileSettings(QWidget* aParent)
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 @@ void ProfileSettings::itemDataChanged(QStandardItem* item)
QKeySequence sequence = QKeySequence::fromString(item->text());
ProfileManager::instance()->setShortcut(item->data(ShortcutRole).value<Profile::Ptr>(),
sequence);
} else if (item->column() == ProfileNameColumn) {
QString newName = item->text();
Profile::Ptr profile = item->data(ProfileKeyRole).value<Profile::Ptr>();
QString oldName = profile->name();
if (newName != oldName) {
QHash<Profile::Property, QVariant> properties;
properties.insert(Profile::Name, newName);
properties.insert(Profile::UntranslatedName, newName);
ProfileManager::instance()->changeProfile(profile, properties);
}
}
}
......@@ -144,7 +135,9 @@ void ProfileSettings::updateItemsForProfile(const Profile::Ptr profile, QList<QS
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 @@ void ProfileSettings::updateItemsForProfile(const Profile::Ptr profile, QList<QS
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())
......
......@@ -87,6 +87,9 @@ private Q_SLOTS:
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<Profile::Ptr> selectedProfiles() const;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment