]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinrecenttabsmenu.cpp
Move search and filter bar close buttons to the right
[dolphin.git] / src / dolphinrecenttabsmenu.cpp
1 /***************************************************************************
2 * Copyright (C) 2014 by Emmanuel Pescosta <emmanuelpescosta099@gmail.com> *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18 ***************************************************************************/
19
20 #include "dolphinrecenttabsmenu.h"
21
22 #include <KAcceleratorManager>
23 #include <KLocalizedString>
24 #include <kio/global.h>
25
26 #include <QMenu>
27
28 DolphinRecentTabsMenu::DolphinRecentTabsMenu(QObject* parent) :
29 KActionMenu(QIcon::fromTheme(QStringLiteral("edit-undo")), i18n("Recently Closed Tabs"), parent)
30 {
31 setDelayed(false);
32 setEnabled(false);
33
34 m_clearListAction = new QAction(i18n("Empty Recently Closed Tabs"), this);
35 m_clearListAction->setIcon(QIcon::fromTheme(QStringLiteral("edit-clear-list")));
36 addAction(m_clearListAction);
37
38 addSeparator();
39
40 connect(menu(), &QMenu::triggered,
41 this, &DolphinRecentTabsMenu::handleAction);
42 }
43
44 void DolphinRecentTabsMenu::rememberClosedTab(const QUrl& url, const QByteArray& state)
45 {
46 QAction* action = new QAction(menu());
47 action->setText(url.path());
48 action->setData(state);
49 const QString iconName = KIO::iconNameForUrl(url);
50 action->setIcon(QIcon::fromTheme(iconName));
51
52 // Add the closed tab menu entry after the separator and
53 // "Empty Recently Closed Tabs" entry
54 if (menu()->actions().size() == 2) {
55 addAction(action);
56 } else {
57 insertAction(menu()->actions().at(2), action);
58 }
59 emit closedTabsCountChanged(menu()->actions().size() - 2);
60 // Assure that only up to 6 closed tabs are shown in the menu.
61 // 8 because of clear action + separator + 6 closed tabs
62 if (menu()->actions().size() > 8) {
63 removeAction(menu()->actions().last());
64 }
65 setEnabled(true);
66 KAcceleratorManager::manage(menu());
67 }
68
69 void DolphinRecentTabsMenu::undoCloseTab()
70 {
71 Q_ASSERT(menu()->actions().size() > 2);
72 handleAction(menu()->actions().at(2));
73 }
74
75 void DolphinRecentTabsMenu::handleAction(QAction* action)
76 {
77 if (action == m_clearListAction) {
78 // Clear all actions except the "Empty Recently Closed Tabs"
79 // action and the separator
80 QList<QAction*> actions = menu()->actions();
81 const int count = actions.size();
82 for (int i = 2; i < count; ++i) {
83 removeAction(actions.at(i));
84 }
85 emit closedTabsCountChanged(0);
86 } else {
87 const QByteArray state = action->data().toByteArray();
88 removeAction(action);
89 delete action;
90 action = nullptr;
91 emit restoreClosedTab(state);
92 emit closedTabsCountChanged(menu()->actions().size() - 2);
93 }
94
95 if (menu()->actions().count() <= 2) {
96 setEnabled(false);
97 }
98 }