Implemented DolphinRecentTabsMenu to encapsulate the recent

tabs menu related code from DolphinMainWindow in a new class.

The DolphinRecentTabsMenu remembers the tab configuration if a
tab has been closed.

REVIEW: 118805
This commit is contained in:
Emmanuel Pescosta 2014-06-19 22:04:36 +02:00
parent efa19caa46
commit 24823bbfd1
5 changed files with 180 additions and 104 deletions

View file

@ -171,6 +171,7 @@ set(dolphin_SRCS
dolphinmainwindow.cpp
dolphinviewcontainer.cpp
dolphincontextmenu.cpp
dolphinrecenttabsmenu.cpp
filterbar/filterbar.cpp
main.cpp
panels/information/filemetadataconfigurationdialog.cpp

View file

@ -25,6 +25,7 @@
#include "dolphindockwidget.h"
#include "dolphincontextmenu.h"
#include "dolphinnewfilemenu.h"
#include "dolphinrecenttabsmenu.h"
#include "dolphinviewcontainer.h"
#include "panels/folders/folderspanel.h"
#include "panels/places/placespanel.h"
@ -93,19 +94,6 @@ namespace {
const int CurrentDolphinVersion = 200;
};
/*
* Remembers the tab configuration if a tab has been closed.
* Each closed tab can be restored by the menu
* "Go -> Recently Closed Tabs".
*/
struct ClosedTab
{
KUrl primaryUrl;
KUrl secondaryUrl;
bool isSplit;
};
Q_DECLARE_METATYPE(ClosedTab)
DolphinMainWindow::DolphinMainWindow() :
KXmlGuiWindow(0),
m_newFileMenu(0),
@ -739,35 +727,6 @@ void DolphinMainWindow::slotUndoAvailable(bool available)
}
}
void DolphinMainWindow::restoreClosedTab(QAction* action)
{
if (action->data().toBool()) {
// clear all actions except the "Empty Recently Closed Tabs"
// action and the separator
QList<QAction*> actions = m_recentTabsMenu->menu()->actions();
const int count = actions.size();
for (int i = 2; i < count; ++i) {
m_recentTabsMenu->menu()->removeAction(actions.at(i));
}
} else {
const ClosedTab closedTab = action->data().value<ClosedTab>();
openNewTab(closedTab.primaryUrl);
m_tabBar->setCurrentIndex(m_viewTab.count() - 1);
if (closedTab.isSplit) {
// create secondary view
toggleSplitView();
m_viewTab[m_tabIndex].secondaryView->setUrl(closedTab.secondaryUrl);
}
m_recentTabsMenu->removeAction(action);
}
if (m_recentTabsMenu->menu()->actions().count() == 2) {
m_recentTabsMenu->setEnabled(false);
}
}
void DolphinMainWindow::slotUndoTextChanged(const QString& text)
{
QAction* undoAction = actionCollection()->action(KStandardAction::name(KStandardAction::Undo));
@ -1136,7 +1095,10 @@ void DolphinMainWindow::closeTab(int index)
// previous tab before closing the tab.
m_tabBar->setCurrentIndex((index > 0) ? index - 1 : 1);
}
rememberClosedTab(index);
const KUrl primaryUrl(m_viewTab[index].primaryView->url());
const KUrl secondaryUrl(m_viewTab[index].secondaryView ? m_viewTab[index].secondaryView->url() : KUrl());
emit rememberClosedTab(primaryUrl, secondaryUrl);
// delete tab
m_viewTab[index].primaryView->deleteLater();
@ -1434,6 +1396,18 @@ void DolphinMainWindow::slotPlaceActivated(const KUrl& url)
}
}
void DolphinMainWindow::restoreClosedTab(const KUrl& primaryUrl, const KUrl& secondaryUrl)
{
openNewActivatedTab(primaryUrl);
if (!secondaryUrl.isEmpty() && secondaryUrl.isValid()) {
const int index = m_tabBar->currentIndex();
createSecondaryView(index);
setActiveViewContainer(m_viewTab[index].secondaryView);
m_viewTab[index].secondaryView->setUrl(secondaryUrl);
}
}
void DolphinMainWindow::setActiveViewContainer(DolphinViewContainer* viewContainer)
{
Q_ASSERT(viewContainer);
@ -1582,19 +1556,12 @@ void DolphinMainWindow::setupActions()
backShortcut.setAlternate(Qt::Key_Backspace);
backAction->setShortcut(backShortcut);
m_recentTabsMenu = new KActionMenu(i18n("Recently Closed Tabs"), this);
m_recentTabsMenu->setIcon(KIcon("edit-undo"));
m_recentTabsMenu->setDelayed(false);
actionCollection()->addAction("closed_tabs", m_recentTabsMenu);
connect(m_recentTabsMenu->menu(), SIGNAL(triggered(QAction*)),
this, SLOT(restoreClosedTab(QAction*)));
QAction* action = new QAction(i18n("Empty Recently Closed Tabs"), m_recentTabsMenu);
action->setIcon(KIcon("edit-clear-list"));
action->setData(QVariant::fromValue(true));
m_recentTabsMenu->addAction(action);
m_recentTabsMenu->addSeparator();
m_recentTabsMenu->setEnabled(false);
DolphinRecentTabsMenu* recentTabsMenu = new DolphinRecentTabsMenu(this);
actionCollection()->addAction("closed_tabs", recentTabsMenu);
connect(this, SIGNAL(rememberClosedTab(KUrl,KUrl)),
recentTabsMenu, SLOT(rememberClosedTab(KUrl,KUrl)));
connect(recentTabsMenu, SIGNAL(restoreClosedTab(KUrl,KUrl)),
this, SLOT(restoreClosedTab(KUrl,KUrl)));
KAction* forwardAction = KStandardAction::forward(this, SLOT(goForward()), actionCollection());
connect(forwardAction, SIGNAL(triggered(Qt::MouseButtons,Qt::KeyboardModifiers)), this, SLOT(goForward(Qt::MouseButtons)));
@ -1903,44 +1870,6 @@ bool DolphinMainWindow::addActionToMenu(QAction* action, KMenu* menu)
return true;
}
void DolphinMainWindow::rememberClosedTab(int index)
{
KMenu* tabsMenu = m_recentTabsMenu->menu();
const QString primaryPath = m_viewTab[index].primaryView->url().path();
const QString iconName = KMimeType::iconNameForUrl(primaryPath);
QAction* action = new QAction(squeezedText(primaryPath), tabsMenu);
ClosedTab closedTab;
closedTab.primaryUrl = m_viewTab[index].primaryView->url();
if (m_viewTab[index].secondaryView) {
closedTab.secondaryUrl = m_viewTab[index].secondaryView->url();
closedTab.isSplit = true;
} else {
closedTab.isSplit = false;
}
action->setData(QVariant::fromValue(closedTab));
action->setIcon(KIcon(iconName));
// add the closed tab menu entry after the separator and
// "Empty Recently Closed Tabs" entry
if (tabsMenu->actions().size() == 2) {
tabsMenu->addAction(action);
} else {
tabsMenu->insertAction(tabsMenu->actions().at(2), action);
}
// assure that only up to 8 closed tabs are shown in the menu
if (tabsMenu->actions().size() > 8) {
tabsMenu->removeAction(tabsMenu->actions().last());
}
actionCollection()->action("closed_tabs")->setEnabled(true);
KAcceleratorManager::manage(tabsMenu);
}
void DolphinMainWindow::refreshViews()
{
Q_ASSERT(m_viewTab[m_tabIndex].primaryView);

View file

@ -28,7 +28,6 @@
#include <kio/fileundomanager.h>
#include <ksortablelist.h>
#include <kxmlguiwindow.h>
#include <KActionMenu>
#include <KIcon>
#include <QList>
@ -156,6 +155,11 @@ signals:
*/
void settingsChanged();
/**
* Is emitted when a tab has been closed.
*/
void rememberClosedTab(const KUrl& primaryUrl, const KUrl& secondaryUrl);
protected:
/** @see QWidget::showEvent() */
virtual void showEvent(QShowEvent* event);
@ -192,9 +196,6 @@ private slots:
*/
void slotUndoAvailable(bool available);
/** Invoked when an action in the recent tabs menu is clicked. */
void restoreClosedTab(QAction* action);
/** Sets the text of the 'Undo' menu action to \a text. */
void slotUndoTextChanged(const QString& text);
@ -473,6 +474,12 @@ private slots:
*/
void slotPlaceActivated(const KUrl& url);
/**
* Is called when the user wants to reopen a previously closed \a tab from
* the recent tabs menu.
*/
void restoreClosedTab(const KUrl& primaryUrl, const KUrl& secondaryUrl);
private:
/**
* Activates the given view, which means that
@ -503,11 +510,6 @@ private:
*/
bool addActionToMenu(QAction* action, KMenu* menu);
/**
* Adds the tab[\a index] to the closed tab menu's list of actions.
*/
void rememberClosedTab(int index);
/**
* Connects the signals from the created DolphinView with
* the DolphinViewContainer \a container with the corresponding slots of
@ -572,7 +574,6 @@ private:
};
KNewFileMenu* m_newFileMenu;
KActionMenu* m_recentTabsMenu;
KTabBar* m_tabBar;
DolphinViewContainer* m_activeViewContainer;
QVBoxLayout* m_centralWidgetLayout;

View file

@ -0,0 +1,96 @@
/***************************************************************************
* Copyright (C) 2014 by Emmanuel Pescosta <emmanuelpescosta099@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) any later version. *
* *
* 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, write to the *
* Free Software Foundation, Inc., *
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
***************************************************************************/
#include "dolphinrecenttabsmenu.h"
#include <KLocalizedString>
#include <KAcceleratorManager>
#include <KMimeType>
#include <KMenu>
DolphinRecentTabsMenu::DolphinRecentTabsMenu(QObject* parent) :
KActionMenu(KIcon("edit-undo"), i18n("Recently Closed Tabs"), parent)
{
setDelayed(false);
setEnabled(false);
m_clearListAction = new QAction(i18n("Empty Recently Closed Tabs"), this);
m_clearListAction->setIcon(KIcon("edit-clear-list"));
addAction(m_clearListAction);
addSeparator();
connect(menu(), SIGNAL(triggered(QAction*)),
this, SLOT(handleAction(QAction*)));
}
void DolphinRecentTabsMenu::rememberClosedTab(const KUrl& primaryUrl, const KUrl& secondaryUrl)
{
QAction* action = new QAction(menu());
action->setText(primaryUrl.path());
const QString iconName = KMimeType::iconNameForUrl(primaryUrl);
action->setIcon(KIcon(iconName));
KUrl::List urls;
urls << primaryUrl;
urls << secondaryUrl;
action->setData(QVariant::fromValue(urls));
// Add the closed tab menu entry after the separator and
// "Empty Recently Closed Tabs" entry
if (menu()->actions().size() == 2) {
addAction(action);
} else {
insertAction(menu()->actions().at(2), action);
}
// Assure that only up to 6 closed tabs are shown in the menu.
// 8 because of clear action + separator + 6 closed tabs
if (menu()->actions().size() > 8) {
removeAction(menu()->actions().last());
}
setEnabled(true);
KAcceleratorManager::manage(menu());
}
void DolphinRecentTabsMenu::handleAction(QAction* action)
{
if (action == m_clearListAction) {
// Clear all actions except the "Empty Recently Closed Tabs"
// action and the separator
QList<QAction*> actions = menu()->actions();
const int count = actions.size();
for (int i = 2; i < count; ++i) {
removeAction(actions.at(i));
}
} else {
const KUrl::List urls = action->data().value<KUrl::List>();
if (urls.count() == 2) {
emit restoreClosedTab(urls.first(), urls.last());
}
removeAction(action);
delete action;
action = 0;
}
if (menu()->actions().count() <= 2) {
setEnabled(false);
}
}

View file

@ -0,0 +1,49 @@
/***************************************************************************
* Copyright (C) 2014 by Emmanuel Pescosta <emmanuelpescosta099@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) any later version. *
* *
* 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, write to the *
* Free Software Foundation, Inc., *
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
***************************************************************************/
#ifndef DOLPHIN_RECENT_TABS_MENU_H
#define DOLPHIN_RECENT_TABS_MENU_H
#include <KActionMenu>
#include <KUrl>
class DolphinTabPage;
class QAction;
class DolphinRecentTabsMenu : public KActionMenu
{
Q_OBJECT
public:
explicit DolphinRecentTabsMenu(QObject* parent);
public slots:
void rememberClosedTab(const KUrl& primaryUrl, const KUrl& secondaryUrl);
signals:
void restoreClosedTab(const KUrl& primaryUrl, const KUrl& secondaryUrl);
private slots:
void handleAction(QAction* action);
private:
QAction* m_clearListAction;
};
#endif