[kde-doc-english] [trojita] src: GUI: create a fancier password dialog which also uses LineEdit

Jan Kundrát jkt at flaska.net
Wed Mar 20 00:39:46 UTC 2013


Git commit f668c9fb5eafd02018ba9589a994612de5b70c38 by Jan Kundrát, on behalf of Glad Deschrijver.
Committed on 20/03/2013 at 00:34.
Pushed by jkt into branch 'master'.

GUI: create a fancier password dialog which also uses LineEdit

The dialog uses dialog-password.png (again this icon  comes from KDE Oxygen).

M  +6    -3    src/Gui/Gui.pro
A  +70   -0    src/Gui/PasswordDialog.cpp     [License: GPL (v2/3)]
A  +52   -0    src/Gui/PasswordDialog.h     [License: GPL (v2/3)]
A  +152  -0    src/Gui/PasswordDialog.ui
M  +5    -5    src/Gui/Window.cpp
M  +1    -0    src/icons.qrc
A  +-    --    src/icons/dialog-password.png

http://commits.kde.org/trojita/f668c9fb5eafd02018ba9589a994612de5b70c38

diff --git a/src/Gui/Gui.pro b/src/Gui/Gui.pro
index 8d8c285..cc4b541 100644
--- a/src/Gui/Gui.pro
+++ b/src/Gui/Gui.pro
@@ -54,7 +54,8 @@ SOURCES += \
     ShortcutHandler/ShortcutConfigDialog.cpp \
     ShortcutHandler/ShortcutConfigWidget.cpp \
     ShortcutHandler/ShortcutHandler.cpp \
-    LineEdit.cpp
+    LineEdit.cpp \
+    PasswordDialog.cpp
 HEADERS += \
     ../Imap/Model/ModelTest/modeltest.h \
     ComposeWidget.h \
@@ -94,7 +95,8 @@ HEADERS += \
     ShortcutHandler/ShortcutConfigDialog.h \
     ShortcutHandler/ShortcutConfigWidget.h \
     ShortcutHandler/ShortcutHandler.h \
-    LineEdit.h
+    LineEdit.h \
+    PasswordDialog.h
 FORMS += CreateMailboxDialog.ui \
     ComposeWidget.ui \
     SettingsImapPage.ui \
@@ -102,7 +104,8 @@ FORMS += CreateMailboxDialog.ui \
     SettingsOutgoingPage.ui \
     SettingsGeneralPage.ui \
     EditIdentity.ui \
-    ShortcutHandler/ShortcutConfigWidget.ui
+    ShortcutHandler/ShortcutConfigWidget.ui \
+    PasswordDialog.ui
 
 RESOURCES += ../icons.qrc
 
diff --git a/src/Gui/PasswordDialog.cpp b/src/Gui/PasswordDialog.cpp
new file mode 100644
index 0000000..cf1fad2
--- /dev/null
+++ b/src/Gui/PasswordDialog.cpp
@@ -0,0 +1,70 @@
+/*
+   Copyright (C) 2013 by Glad Deschrijver <glad.deschrijver at gmail.com>
+
+   This program is free software; you can redistribute it and/or
+   modify it under the terms of the GNU General Public License as
+   published by the Free Software Foundation; either version 2 of
+   the License or (at your option) version 3 or any later version
+   accepted by the membership of KDE e.V. (or its successor approved
+   by the membership of KDE e.V.), which shall act as a proxy
+   defined in Section 14 of version 3 of the license.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#include "PasswordDialog.h"
+
+#include "IconLoader.h"
+
+namespace Gui
+{
+
+PasswordDialog::PasswordDialog(QWidget *parent)
+    : QDialog(parent)
+{
+    ui.setupUi(this);
+    setModal(true);
+    ui.iconLabel->setPixmap(loadIcon("dialog-password").pixmap(64, 64));
+}
+
+PasswordDialog::~PasswordDialog()
+{
+}
+
+void PasswordDialog::showEvent(QShowEvent *event)
+{
+    ui.passwordLineEdit->setFocus();
+    QDialog::showEvent(event);
+}
+
+/***************************************************************************/
+
+QString PasswordDialog::password() const
+{
+    return ui.passwordLineEdit->text();
+}
+
+QString PasswordDialog::getPassword(QWidget *parent, const QString &windowTitle, const QString &title, const QString &description, QLineEdit::EchoMode echo, const QString &password, bool *ok)
+{
+    PasswordDialog dialog(parent);
+    dialog.setWindowTitle(windowTitle);
+    dialog.ui.titleLabel->setText(title);
+    dialog.ui.descriptionLabel->setText(description);
+    dialog.ui.passwordLineEdit->setEchoMode(echo);
+    dialog.ui.passwordLineEdit->setText(password);
+
+    int ret = dialog.exec();
+    if (ok)
+        *ok = !!ret;
+    if (ret)
+        return dialog.ui.passwordLineEdit->text();
+    return QString();
+}
+
+} // namespace Gui
diff --git a/src/Gui/PasswordDialog.h b/src/Gui/PasswordDialog.h
new file mode 100644
index 0000000..d5794c1
--- /dev/null
+++ b/src/Gui/PasswordDialog.h
@@ -0,0 +1,52 @@
+/*
+   Copyright (C) 2013 by Glad Deschrijver <glad.deschrijver at gmail.com>
+
+   This program is free software; you can redistribute it and/or
+   modify it under the terms of the GNU General Public License as
+   published by the Free Software Foundation; either version 2 of
+   the License or (at your option) version 3 or any later version
+   accepted by the membership of KDE e.V. (or its successor approved
+   by the membership of KDE e.V.), which shall act as a proxy
+   defined in Section 14 of version 3 of the license.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#ifndef PASSWORDDIALOG_H
+#define PASSWORDDIALOG_H
+
+#include <QDialog>
+#include "ui_PasswordDialog.h"
+
+class QAction;
+class QKeySequence;
+
+namespace Gui
+{
+
+class PasswordDialog : public QDialog
+{
+    Q_OBJECT
+
+public:
+    explicit PasswordDialog(QWidget *parent = 0);
+    ~PasswordDialog();
+
+    QString password() const;
+    static QString getPassword(QWidget *parent, const QString &windowTitle, const QString &title, const QString &description, QLineEdit::EchoMode echo = QLineEdit::Normal, const QString &password = 0, bool *ok = 0);
+
+protected:
+    void showEvent(QShowEvent *event);
+
+    Ui::PasswordDialog ui;
+};
+
+} // namespace Gui
+
+#endif // PASSWORDDIALOG_H
diff --git a/src/Gui/PasswordDialog.ui b/src/Gui/PasswordDialog.ui
new file mode 100644
index 0000000..4feec20
--- /dev/null
+++ b/src/Gui/PasswordDialog.ui
@@ -0,0 +1,152 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<ui version="4.0">
+ <class>PasswordDialog</class>
+ <widget class="QDialog" name="PasswordDialog">
+  <property name="geometry">
+   <rect>
+    <x>0</x>
+    <y>0</y>
+    <width>400</width>
+    <height>130</height>
+   </rect>
+  </property>
+  <property name="windowTitle">
+   <string>Authentication</string>
+  </property>
+  <layout class="QVBoxLayout" name="verticalLayout">
+   <item>
+    <layout class="QGridLayout" name="gridLayout">
+     <property name="horizontalSpacing">
+      <number>12</number>
+     </property>
+     <property name="verticalSpacing">
+      <number>6</number>
+     </property>
+     <item row="0" column="0" rowspan="3">
+      <widget class="QLabel" name="iconLabel">
+       <property name="text">
+        <string/>
+       </property>
+       <property name="pixmap">
+        <pixmap resource="../icons.qrc">:/icons/dialog-password.png</pixmap>
+       </property>
+      </widget>
+     </item>
+     <item row="0" column="1">
+      <widget class="QLabel" name="titleLabel">
+       <property name="font">
+        <font>
+         <weight>75</weight>
+         <bold>true</bold>
+        </font>
+       </property>
+       <property name="text">
+        <string><h2>Authentication required</h2></string>
+       </property>
+      </widget>
+     </item>
+     <item row="1" column="1">
+      <widget class="QLabel" name="descriptionLabel">
+       <property name="sizePolicy">
+        <sizepolicy hsizetype="Preferred" vsizetype="MinimumExpanding">
+         <horstretch>0</horstretch>
+         <verstretch>0</verstretch>
+        </sizepolicy>
+       </property>
+       <property name="text">
+        <string><p>Please provide password for %1 on %2.</p></string>
+       </property>
+       <property name="wordWrap">
+        <bool>true</bool>
+       </property>
+      </widget>
+     </item>
+     <item row="2" column="1">
+      <layout class="QHBoxLayout" name="horizontalLayout">
+       <item>
+        <widget class="QLabel" name="passwordLabel">
+         <property name="text">
+          <string>&Password:</string>
+         </property>
+         <property name="buddy">
+          <cstring>passwordLineEdit</cstring>
+         </property>
+        </widget>
+       </item>
+       <item>
+        <widget class="LineEdit" name="passwordLineEdit"/>
+       </item>
+      </layout>
+     </item>
+    </layout>
+   </item>
+   <item>
+    <spacer name="verticalSpacer">
+     <property name="orientation">
+      <enum>Qt::Vertical</enum>
+     </property>
+     <property name="sizeHint" stdset="0">
+      <size>
+       <width>10</width>
+       <height>0</height>
+      </size>
+     </property>
+    </spacer>
+   </item>
+   <item>
+    <widget class="QDialogButtonBox" name="buttonBox">
+     <property name="orientation">
+      <enum>Qt::Horizontal</enum>
+     </property>
+     <property name="standardButtons">
+      <set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
+     </property>
+    </widget>
+   </item>
+  </layout>
+ </widget>
+ <customwidgets>
+  <customwidget>
+   <class>LineEdit</class>
+   <extends>QLineEdit</extends>
+   <header>LineEdit.h</header>
+  </customwidget>
+ </customwidgets>
+ <resources>
+  <include location="../icons.qrc"/>
+ </resources>
+ <connections>
+  <connection>
+   <sender>buttonBox</sender>
+   <signal>accepted()</signal>
+   <receiver>PasswordDialog</receiver>
+   <slot>accept()</slot>
+   <hints>
+    <hint type="sourcelabel">
+     <x>248</x>
+     <y>254</y>
+    </hint>
+    <hint type="destinationlabel">
+     <x>157</x>
+     <y>274</y>
+    </hint>
+   </hints>
+  </connection>
+  <connection>
+   <sender>buttonBox</sender>
+   <signal>rejected()</signal>
+   <receiver>PasswordDialog</receiver>
+   <slot>reject()</slot>
+   <hints>
+    <hint type="sourcelabel">
+     <x>316</x>
+     <y>260</y>
+    </hint>
+    <hint type="destinationlabel">
+     <x>286</x>
+     <y>274</y>
+    </hint>
+   </hints>
+  </connection>
+ </connections>
+</ui>
diff --git a/src/Gui/Window.cpp b/src/Gui/Window.cpp
index 0a1ae1f..f009d78 100644
--- a/src/Gui/Window.cpp
+++ b/src/Gui/Window.cpp
@@ -30,7 +30,6 @@
 #include <QDockWidget>
 #include <QFileDialog>
 #include <QHeaderView>
-#include <QInputDialog>
 #include <QItemSelectionModel>
 #include <QMenuBar>
 #include <QMessageBox>
@@ -67,6 +66,7 @@
 #include "MessageView.h"
 #include "MessageSourceWidget.h"
 #include "MsgListView.h"
+#include "PasswordDialog.h"
 #include "ProtocolLoggerWidget.h"
 #include "SettingsDialog.h"
 #include "SimplePartWidget.h"
@@ -916,10 +916,10 @@ void MainWindow::authenticationRequested()
     QString pass = s.value(Common::SettingsNames::imapPassKey).toString();
     if (m_ignoreStoredPassword || pass.isEmpty()) {
         bool ok;
-        pass = QInputDialog::getText(this, tr("IMAP Password"),
-                                     tr("Please provide password for %1 on %2:").arg(
-                                         user, QSettings().value(Common::SettingsNames::imapHostKey).toString()),
-                                     QLineEdit::Password, QString(), &ok);
+        pass = PasswordDialog::getPassword(this, tr("IMAP Password"), tr("<h3>Authentication required</h3>"),
+                                           tr("<p>Please provide password for %1 on %2.</p>").arg(
+                                               user, QSettings().value(Common::SettingsNames::imapHostKey).toString()),
+                                           QLineEdit::Password, QString(), &ok);
         if (ok) {
             model->setImapUser(user);
             model->setImapPassword(pass);
diff --git a/src/icons.qrc b/src/icons.qrc
index 8d1c5d0..5a03752 100644
--- a/src/icons.qrc
+++ b/src/icons.qrc
@@ -38,5 +38,6 @@
         <file>icons/mail-reply-all.png</file>
         <file>icons/mail-reply-list.png</file>
         <file>icons/mail-reply-sender.png</file>
+        <file>icons/dialog-password.png</file>
     </qresource>
 </RCC>
diff --git a/src/icons/dialog-password.png b/src/icons/dialog-password.png
new file mode 100644
index 0000000..5f801af
Binary files /dev/null and b/src/icons/dialog-password.png differ



More information about the kde-doc-english mailing list