Commit 46b56ecf authored by Ahmad Samir's avatar Ahmad Samir Committed by Kurt Hindenburg
Browse files

Use a KMessageWidget for profile-name related errors in the Edit dialog

A KMessageWidget is nicer than a modal dialog, and now we also
automatically switch to the General page when the user tries to save
the settings while there is an issue with the profile name.
parent e805f086
......@@ -106,10 +106,10 @@ EditProfileDialog::EditProfileDialog(QWidget *parent)
auto *generalPageWidget = new QWidget(this);
_generalUi = new Ui::EditProfileGeneralPage();
_generalUi->setupUi(generalPageWidget);
auto *generalPageItem = addPage(generalPageWidget, generalPageName);
generalPageItem->setHeader(generalPageName);
generalPageItem->setIcon(QIcon::fromTheme(QStringLiteral("utilities-terminal")));
_pages[generalPageItem] = Page(&EditProfileDialog::setupGeneralPage);
_generalPageItem = addPage(generalPageWidget, generalPageName);
_generalPageItem->setHeader(generalPageName);
_generalPageItem->setIcon(QIcon::fromTheme(QStringLiteral("utilities-terminal")));
_pages[_generalPageItem] = Page(&EditProfileDialog::setupGeneralPage);
// Tabs page
......@@ -262,7 +262,7 @@ void EditProfileDialog::accept()
// if the Apply button is disabled then no settings were changed
// or the changes have already been saved by apply()
if (_buttonBox->button(QDialogButtonBox::Apply)->isEnabled()) {
if (!isValidProfileName()) {
if (!isProfileNameValid()) {
return;
}
save();
......@@ -274,29 +274,36 @@ void EditProfileDialog::accept()
void EditProfileDialog::apply()
{
if (!isValidProfileName()) {
if (!isProfileNameValid()) {
return;
}
save();
}
bool EditProfileDialog::isValidProfileName()
void EditProfileDialog::setMessageGeneralPage(const QString &msg)
{
_generalUi->generalPageMessageWidget->setText(msg);
_generalUi->generalPageMessageWidget->setMessageType(KMessageWidget::Error);
setCurrentPage(_generalPageItem);
_generalUi->generalPageMessageWidget->animatedShow();
}
bool EditProfileDialog::isProfileNameValid()
{
Q_ASSERT(_profile);
Q_ASSERT(_tempProfile);
// check whether the user has enough permissions to save the profile
QFileInfo fileInfo(_profile->path());
if (fileInfo.exists() && !fileInfo.isWritable()) {
if (!_tempProfile->isPropertySet(Profile::Name)
|| (_tempProfile->name() == _profile->name())) {
KMessageBox::sorry(this,
i18n("<p>Konsole does not have permission to save this profile to:<br /> \"%1\"</p>"
"<p>To be able to save settings you can either change the permissions "
"of the profile configuration file or change the profile name to save "
"the settings to a new profile.</p>", _profile->path()));
return false;
}
if (fileInfo.exists() && !fileInfo.isWritable()
&& (!_tempProfile->isPropertySet(Profile::Name) || _tempProfile->name() == _profile->name())) {
setMessageGeneralPage(
xi18nc("@info",
"Insufficient permissions to save settings to: <filename>%1</filename>.<nl/>"
"Either change the permissions of that file or set a different name to save "
"the settings to a new profile.",
_profile->path()));
return false;
}
const QList<Profile::Ptr> existingProfiles = ProfileManager::instance()->allProfiles();
......@@ -308,26 +315,28 @@ bool EditProfileDialog::isValidProfileName()
}
}
if ((_tempProfile->isPropertySet(Profile::Name)
&& _tempProfile->name().isEmpty())
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>"));
// revert the name in the dialog
setMessageGeneralPage(i18nc("@info",
"Profile Name was empty; please set a name to be able to save settings."));
// Revert the name in the dialog
_generalUi->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
}
if (!_tempProfile->name().isEmpty() && otherExistingProfileNames.contains(_tempProfile->name())) {
setMessageGeneralPage(i18nc("@info",
"A profile with this name, '%1', already exists.",
_generalUi->profileNameEdit->text()));
// Revert the name in the dialog
_generalUi->profileNameEdit->setText(_profile->name());
selectProfileName();
return false;
} else {
return true;
}
// Valid name
return true;
}
QString EditProfileDialog::groupProfileNames(const ProfileGroup::Ptr &group, int maxLength)
......@@ -426,6 +435,10 @@ void Konsole::EditProfileDialog::selectProfileName()
void EditProfileDialog::setupGeneralPage(const Profile::Ptr &profile)
{
_generalUi->generalPageMessageWidget->setVisible(false);
_generalUi->generalPageMessageWidget->setWordWrap(true);
_generalUi->generalPageMessageWidget->setCloseButtonVisible(true);
// basic profile options
{
ProfileGroup::Ptr group = profile->asGroup();
......
......@@ -241,6 +241,8 @@ private:
void setupAdvancedPage(const Profile::Ptr &profile);
void setupMousePage(const Profile::Ptr &profile);
void setMessageGeneralPage(const QString &msg);
int maxSpinBoxWidth(const KPluralHandlingSpinBox *spinBox, const KLocalizedString &suffix);
// Returns the name of the colorScheme used in the current profile
......@@ -307,7 +309,7 @@ private:
// - the name matches the name of an already existing profile
// - the existing profile config file is read-only
// otherwise returns true.
bool isValidProfileName();
bool isProfileNameValid();
Ui::EditProfileGeneralPage *_generalUi;
Ui::EditProfileTabsPage *_tabsUi;
......@@ -329,6 +331,7 @@ private:
};
QMap<KPageWidgetItem *, Page> _pages;
KPageWidgetItem *_generalPageItem = nullptr;
Profile::Ptr _tempProfile;
Profile::Ptr _profile;
......
......@@ -31,7 +31,11 @@
<property name="spacing">
<number>6</number>
</property>
<item row="0" column="0" alignment="Qt::AlignRight">
<item row="0" column="0" colspan="3">
<widget class="KMessageWidget" name="generalPageMessageWidget">
</widget>
</item>
<item row="1" column="0" alignment="Qt::AlignRight">
<widget class="QPushButton" name="iconSelectButton">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
......@@ -65,7 +69,7 @@
</property>
</widget>
</item>
<item row="0" column="1">
<item row="1" column="1">
<widget class="QLineEdit" name="profileNameEdit">
<property name="toolTip">
<string>A descriptive name for the profile</string>
......@@ -75,7 +79,7 @@
</property>
</widget>
</item>
<item row="1" column="0" alignment="Qt::AlignRight">
<item row="2" column="0" alignment="Qt::AlignRight">
<widget class="QLabel" name="label_3">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Preferred">
......@@ -94,7 +98,7 @@
</property>
</widget>
</item>
<item row="1" column="1">
<item row="2" column="1">
<widget class="QLineEdit" name="commandEdit">
<property name="toolTip">
<string>The command to execute when new terminal sessions are created using this profile</string>
......@@ -107,7 +111,7 @@
</property>
</widget>
</item>
<item row="2" column="0" alignment="Qt::AlignRight">
<item row="3" column="0" alignment="Qt::AlignRight">
<widget class="QLabel" name="label_9">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Preferred">
......@@ -126,7 +130,7 @@
</property>
</widget>
</item>
<item row="2" column="1">
<item row="3" column="1">
<widget class="QLineEdit" name="initialDirEdit">
<property name="toolTip">
<string>The initial working directory for new terminal sessions using this profile</string>
......@@ -139,7 +143,7 @@
</property>
</widget>
</item>
<item row="2" column="2">
<item row="3" column="2">
<widget class="QToolButton" name="dirSelectButton">
<property name="toolTip">
<string>Choose the initial directory</string>
......@@ -149,14 +153,14 @@
</property>
</widget>
</item>
<item row="3" column="1" colspan="2">
<item row="4" column="1" colspan="2">
<widget class="QCheckBox" name="startInSameDirButton">
<property name="text">
<string>Start in same directory as current session</string>
</property>
</widget>
</item>
<item row="4" column="0" alignment="Qt::AlignRight">
<item row="5" column="0" alignment="Qt::AlignRight">
<widget class="QLabel" name="label_4">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Preferred">
......@@ -175,7 +179,7 @@
</property>
</widget>
</item>
<item row="4" column="1" colspan="2">
<item row="5" column="1" colspan="2">
<layout class="QHBoxLayout">
<property name="spacing">
<number>0</number>
......@@ -205,7 +209,7 @@
</item>
</layout>
</item>
<item row="5" column="1">
<item row="6" column="1">
<spacer>
<property name="orientation">
<enum>Qt::Vertical</enum>
......@@ -221,7 +225,7 @@
</property>
</spacer>
</item>
<item row="6" column="0" alignment="Qt::AlignRight">
<item row="7" column="0" alignment="Qt::AlignRight">
<widget class="QLabel" name="label_5">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Preferred">
......@@ -237,7 +241,7 @@
</property>
</widget>
</item>
<item row="6" column="1" colspan="2">
<item row="7" column="1" colspan="2">
<layout class="QHBoxLayout">
<property name="spacing">
<number>0</number>
......@@ -296,7 +300,7 @@
</item>
</layout>
</item>
<item row="7" column="1" colspan="2">
<item row="8" column="1" colspan="2">
<widget class="QLabel" name="useCurrentWindowSizeNote">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Preferred">
......@@ -315,7 +319,7 @@
</property>
</widget>
</item>
<item row="8" column="0" alignment="Qt::AlignRight">
<item row="9" column="0" alignment="Qt::AlignRight">
<widget class="QLabel" name="terminalBellLabel">
<property name="text">
<string>Terminal bell mode:</string>
......@@ -325,7 +329,7 @@
</property>
</widget>
</item>
<item row="8" column="1" colspan="1" alignment="Qt::AlignLeft">
<item row="9" column="1" colspan="1" alignment="Qt::AlignLeft">
<layout class="QHBoxLayout" name="terminalBellLayout">
<item>
<widget class="QComboBox" name="terminalBellCombo">
......@@ -359,6 +363,11 @@
<extends>QSpinBox</extends>
<header>KPluralHandlingSpinBox</header>
</customwidget>
<customwidget>
<class>KMessageWidget</class>
<extends>QFrame</extends>
<header>kmessagewidget.h</header>
</customwidget>
</customwidgets>
<tabstops>
<tabstop>environmentEditButton</tabstop>
......
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