[kde-doc-english] [trojita] src/Gui: GUI: use RAII for the one-at-time layout instead of manual slot setup and other stuff

Jan Kundrát jkt at flaska.net
Fri May 31 12:35:07 UTC 2013


Git commit 7c30b4ea1848cb836352ee8034dbf4f3d29a4650 by Jan Kundrát.
Committed on 30/05/2013 at 23:24.
Pushed by jkt into branch 'master'.

GUI: use RAII for the one-at-time layout instead of manual slot setup and other stuff

M  +4    -2    src/Gui/Gui.pro
A  +96   -0    src/Gui/OnePanelAtTimeWidget.cpp     [License: GPL (v2/3)]
A  +63   -0    src/Gui/OnePanelAtTimeWidget.h     [License: GPL (v2/3)]
M  +2    -56   src/Gui/Window.cpp
M  +0    -3    src/Gui/Window.h

http://commits.kde.org/trojita/7c30b4ea1848cb836352ee8034dbf4f3d29a4650

diff --git a/src/Gui/Gui.pro b/src/Gui/Gui.pro
index 13bd104..4b6ca50 100644
--- a/src/Gui/Gui.pro
+++ b/src/Gui/Gui.pro
@@ -56,7 +56,8 @@ SOURCES += \
     ProgressPopUp.cpp \
     OverlayWidget.cpp \
     EnvelopeView.cpp \
-    ReplaceCharValidator.cpp
+    ReplaceCharValidator.cpp \
+    OnePanelAtTimeWidget.cpp
 HEADERS += \
     ../Imap/Model/ModelTest/modeltest.h \
     ComposeWidget.h \
@@ -98,7 +99,8 @@ HEADERS += \
     ProgressPopUp.h \
     OverlayWidget.h \
     EnvelopeView.h \
-    ReplaceCharValidator.h
+    ReplaceCharValidator.h \
+    OnePanelAtTimeWidget.h
 FORMS += CreateMailboxDialog.ui \
     ComposeWidget.ui \
     SettingsImapPage.ui \
diff --git a/src/Gui/OnePanelAtTimeWidget.cpp b/src/Gui/OnePanelAtTimeWidget.cpp
new file mode 100644
index 0000000..b6c0ca7
--- /dev/null
+++ b/src/Gui/OnePanelAtTimeWidget.cpp
@@ -0,0 +1,96 @@
+/* Copyright (C) 2006 - 2013 Jan Kundrát <jkt at flaska.net>
+
+   This file is part of the Trojita Qt IMAP e-mail client,
+   http://trojita.flaska.net/
+
+   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 "OnePanelAtTimeWidget.h"
+#include <QAction>
+#include <QMainWindow>
+#include <QToolBar>
+#include "CompleteMessageWidget.h"
+#include "MailBoxTreeView.h"
+#include "MessageListWidget.h"
+#include "MsgListView.h"
+
+namespace Gui {
+
+OnePanelAtTimeWidget::OnePanelAtTimeWidget(QMainWindow *mainWindow, MailBoxTreeView *mboxTree, MessageListWidget *msgListWidget,
+                                           CompleteMessageWidget *messageWidget, QToolBar *toolbar, QAction *actionGoBack):
+    QStackedWidget(mainWindow), m_mainWindow(mainWindow), m_mboxTree(mboxTree), m_msgListWidget(msgListWidget),
+    m_messageWidget(messageWidget), m_toolbar(toolbar), m_actionGoBack(actionGoBack)
+{
+    addWidget(m_mboxTree);
+    addWidget(m_msgListWidget);
+    addWidget(m_messageWidget);
+    setCurrentWidget(m_mboxTree);
+
+    connect(m_msgListWidget->tree, SIGNAL(clicked(QModelIndex)), this, SLOT(slotOneAtTimeGoDeeper()));
+    connect(m_msgListWidget->tree, SIGNAL(activated(QModelIndex)), this, SLOT(slotOneAtTimeGoDeeper()));
+    connect(m_mboxTree, SIGNAL(clicked(QModelIndex)), this, SLOT(slotOneAtTimeGoDeeper()));
+    connect(m_mboxTree, SIGNAL(activated(QModelIndex)), this, SLOT(slotOneAtTimeGoDeeper()));
+    connect(m_actionGoBack, SIGNAL(triggered()), this, SLOT(slotOneAtTimeGoBack()));
+
+    // The list view is configured to auto-emit activated(QModelIndex) after a short while when the user has navigated
+    // to an index through keyboard. Of course, this doesn't play terribly well with this layout.
+    m_msgListWidget->tree->setAutoActivateAfterKeyNavigation(false);
+
+    m_toolbar->addAction(m_actionGoBack);
+}
+
+OnePanelAtTimeWidget::~OnePanelAtTimeWidget()
+{
+    while (count()) {
+        QWidget *w = widget(0);
+        removeWidget(w);
+        w->show();
+    }
+    m_msgListWidget->tree->setAutoActivateAfterKeyNavigation(false);
+
+    m_toolbar->removeAction(m_actionGoBack);
+
+    // The size of the widgets is still wrong. Let's fix this.
+    if (m_mainWindow->isMaximized()) {
+        m_mainWindow->showNormal();
+        m_mainWindow->showMaximized();
+    } else {
+        m_mainWindow->resize(m_mainWindow->sizeHint());
+    }
+}
+
+void OnePanelAtTimeWidget::slotOneAtTimeGoBack()
+{
+    if (currentIndex() > 0)
+        setCurrentIndex(currentIndex() - 1);
+
+    m_actionGoBack->setEnabled(currentIndex() > 0);
+}
+
+void OnePanelAtTimeWidget::slotOneAtTimeGoDeeper()
+{
+    // Careful here: some of the events are, unfortunately, emitted twice (one for clicked(), another time for activated())
+    if (sender() == m_msgListWidget->tree)
+        setCurrentIndex(indexOf(m_msgListWidget) + 1);
+    else if (sender() == m_mboxTree)
+        setCurrentIndex(indexOf(static_cast<QWidget*>(sender())) + 1);
+
+    m_actionGoBack->setEnabled(currentIndex() > 0);
+}
+
+}
diff --git a/src/Gui/OnePanelAtTimeWidget.h b/src/Gui/OnePanelAtTimeWidget.h
new file mode 100644
index 0000000..47ed96b
--- /dev/null
+++ b/src/Gui/OnePanelAtTimeWidget.h
@@ -0,0 +1,63 @@
+/* Copyright (C) 2006 - 2013 Jan Kundrát <jkt at flaska.net>
+
+   This file is part of the Trojita Qt IMAP e-mail client,
+   http://trojita.flaska.net/
+
+   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 GUI_ONEPANELATTIME_H
+#define GUI_ONEPANELATTIME_H
+
+#include <QStackedWidget>
+
+class QAction;
+class QMainWindow;
+class QToolBar;
+
+namespace Gui
+{
+
+class CompleteMessageWidget;
+class MailBoxTreeView;
+class MessageListWidget;
+
+/** @short Implementation of the "show one panel at a time" mode */
+class OnePanelAtTimeWidget: public QStackedWidget
+{
+    Q_OBJECT
+public:
+    OnePanelAtTimeWidget(QMainWindow *mainWindow, MailBoxTreeView *mboxTree, MessageListWidget *msgListWidget,
+                         CompleteMessageWidget *messageWidget, QToolBar *toolbar, QAction* toolbarActions);
+    virtual ~OnePanelAtTimeWidget();
+
+public slots:
+    void slotOneAtTimeGoBack();
+    void slotOneAtTimeGoDeeper();
+
+private:
+    QMainWindow *m_mainWindow;
+    MailBoxTreeView *m_mboxTree;
+    MessageListWidget *m_msgListWidget;
+    CompleteMessageWidget *m_messageWidget;
+
+    QToolBar *m_toolbar;
+    QAction *m_actionGoBack;
+};
+
+}
+
+#endif // GUI_ONEPANELATTIME_H
diff --git a/src/Gui/Window.cpp b/src/Gui/Window.cpp
index aec57b5..c4fd429 100644
--- a/src/Gui/Window.cpp
+++ b/src/Gui/Window.cpp
@@ -70,6 +70,7 @@
 #include "MessageView.h"
 #include "MessageSourceWidget.h"
 #include "MsgListView.h"
+#include "OnePanelAtTimeWidget.h"
 #include "PasswordDialog.h"
 #include "ProtocolLoggerWidget.h"
 #include "SettingsDialog.h"
@@ -275,7 +276,6 @@ void MainWindow::createActions()
     m_oneAtTimeGoBack = new QAction(loadIcon(QLatin1String("go-previous")), tr("Navigate Back"), this);
     m_oneAtTimeGoBack->setShortcut(QKeySequence::Back);
     m_oneAtTimeGoBack->setEnabled(false);
-    connect(m_oneAtTimeGoBack, SIGNAL(triggered()), this, SLOT(slotOneAtTimeGoBack()));
 
     composeMail = ShortcutHandler::instance()->createAction("action_compose_mail", this, SLOT(slotComposeMail()), this);
     m_editDraft = ShortcutHandler::instance()->createAction("action_compose_draft", this, SLOT(slotEditDraft()), this);
@@ -2091,7 +2091,6 @@ void MainWindow::slotLayoutCompact()
     setMinimumWidth(MINIMUM_WIDTH_NORMAL);
 
     delete m_mainStack;
-    undoLayoutOneAtTimeCraziness();
 
     m_layoutMode = LAYOUT_COMPACT;
     QSettings().setValue(Common::SettingsNames::guiMainWindowLayout, Common::SettingsNames::guiMainWindowLayoutCompact);
@@ -2123,7 +2122,6 @@ void MainWindow::slotLayoutWide()
 
     delete m_mainStack;
     delete m_mainVSplitter;
-    undoLayoutOneAtTimeCraziness();
 
     m_layoutMode = LAYOUT_WIDE;
     QSettings().setValue(Common::SettingsNames::guiMainWindowLayout, Common::SettingsNames::guiMainWindowLayoutWide);
@@ -2136,70 +2134,18 @@ void MainWindow::slotLayoutOneAtTime()
     if (m_mainStack)
         return;
 
-    m_mainStack = new QStackedWidget();
-    m_mainStack->addWidget(mboxTree);
-    m_mainStack->addWidget(msgListWidget);
-    m_mainStack->addWidget(m_messageWidget);
-    m_mainStack->setCurrentWidget(mboxTree);
+    m_mainStack = new OnePanelAtTimeWidget(this, mboxTree, msgListWidget, m_messageWidget, m_mainToolbar, m_oneAtTimeGoBack);
     setCentralWidget(m_mainStack);
     setMinimumWidth(MINIMUM_WIDTH_NORMAL);
 
     delete m_mainHSplitter;
     delete m_mainVSplitter;
 
-    // The list view is configured to auto-emit activated(QModelIndex) after a short while when the user has navigated
-    // to an index through keyboard. Of course, this doesn't play terribly well with this layout.
-    msgListWidget->tree->setAutoActivateAfterKeyNavigation(false);
-
-    connect(msgListWidget->tree, SIGNAL(clicked(QModelIndex)), this, SLOT(slotOneAtTimeGoDeeper()));
-    connect(msgListWidget->tree, SIGNAL(activated(QModelIndex)), this, SLOT(slotOneAtTimeGoDeeper()));
-    connect(mboxTree, SIGNAL(clicked(QModelIndex)), this, SLOT(slotOneAtTimeGoDeeper()));
-    connect(mboxTree, SIGNAL(activated(QModelIndex)), this, SLOT(slotOneAtTimeGoDeeper()));
-    m_mainToolbar->addAction(m_oneAtTimeGoBack);
-
     m_layoutMode = LAYOUT_ONE_AT_TIME;
     QSettings().setValue(Common::SettingsNames::guiMainWindowLayout, Common::SettingsNames::guiMainWindowLayoutOneAtTime);
     applySizesAndState();
 }
 
-/** @short Undo whatever crazy modification were required for the "one at a time" layout madness */
-void MainWindow::undoLayoutOneAtTimeCraziness()
-{
-    disconnect(msgListWidget->tree, SIGNAL(clicked(QModelIndex)), this, SLOT(slotOneAtTimeGoDeeper()));
-    disconnect(msgListWidget->tree, SIGNAL(activated(QModelIndex)), this, SLOT(slotOneAtTimeGoDeeper()));
-    disconnect(mboxTree, SIGNAL(clicked(QModelIndex)), this, SLOT(slotOneAtTimeGoDeeper()));
-    disconnect(mboxTree, SIGNAL(activated(QModelIndex)), this, SLOT(slotOneAtTimeGoDeeper()));
-    m_mainToolbar->removeAction(m_oneAtTimeGoBack);
-    msgListWidget->tree->setAutoActivateAfterKeyNavigation(true);
-
-    // The size of the widgets is still wrong. Let's fix this.
-    if (isMaximized()) {
-        showNormal();
-        showMaximized();
-    } else {
-        resize(sizeHint());
-    }
-}
-
-void MainWindow::slotOneAtTimeGoBack()
-{
-    if (m_mainStack->currentIndex() > 0)
-        m_mainStack->setCurrentIndex(m_mainStack->currentIndex() - 1);
-
-    m_oneAtTimeGoBack->setEnabled(m_mainStack->currentIndex() > 0);
-}
-
-void MainWindow::slotOneAtTimeGoDeeper()
-{
-    // Careful here: some of the events are, unfortunately, emitted twice (one for clicked(), another time for activated())
-    if (sender() == msgListWidget->tree)
-        m_mainStack->setCurrentIndex(m_mainStack->indexOf(msgListWidget) + 1);
-    else if (sender() == mboxTree)
-        m_mainStack->setCurrentIndex(m_mainStack->indexOf(static_cast<QWidget*>(sender())) + 1);
-
-    m_oneAtTimeGoBack->setEnabled(m_mainStack->currentIndex() > 0);
-}
-
 Imap::Mailbox::Model *MainWindow::imapModel() const
 {
     return model;
diff --git a/src/Gui/Window.h b/src/Gui/Window.h
index 5f30b0f..698cc00 100644
--- a/src/Gui/Window.h
+++ b/src/Gui/Window.h
@@ -174,8 +174,6 @@ private slots:
     void slotLayoutCompact();
     void slotLayoutWide();
     void slotLayoutOneAtTime();
-    void slotOneAtTimeGoBack();
-    void slotOneAtTimeGoDeeper();
     void saveSizesAndState(const LayoutMode oldMode);
     void saveSizesAndState();
 
@@ -205,7 +203,6 @@ private:
     void migrateSettings();
     void applySizesAndState();
     QString settingsKeyForLayout(const LayoutMode layout);
-    void undoLayoutOneAtTimeCraziness();
 
     void recoverDrafts();
     void createSysTray();



More information about the kde-doc-english mailing list