1 | /* |
---|
2 | |
---|
3 | Copyright 2012-2014 Adam Reichold |
---|
4 | |
---|
5 | This file is part of qpdfview. |
---|
6 | |
---|
7 | qpdfview is free software: you can redistribute it and/or modify |
---|
8 | it under the terms of the GNU General Public License as published by |
---|
9 | the Free Software Foundation, either version 2 of the License, or |
---|
10 | (at your option) any later version. |
---|
11 | |
---|
12 | qpdfview is distributed in the hope that it will be useful, |
---|
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
15 | GNU General Public License for more details. |
---|
16 | |
---|
17 | You should have received a copy of the GNU General Public License |
---|
18 | along with qpdfview. If not, see <http://www.gnu.org/licenses/>. |
---|
19 | |
---|
20 | */ |
---|
21 | |
---|
22 | #include "recentlyusedmenu.h" |
---|
23 | |
---|
24 | #include <QFileInfo> |
---|
25 | |
---|
26 | namespace qpdfview |
---|
27 | { |
---|
28 | |
---|
29 | RecentlyUsedMenu::RecentlyUsedMenu(const QStringList& absoluteFilePaths, int count, QWidget* parent) : QMenu(parent), |
---|
30 | m_count(count) |
---|
31 | { |
---|
32 | menuAction()->setText(tr("Recently &used")); |
---|
33 | menuAction()->setIcon(QIcon::fromTheme("document-open-recent")); |
---|
34 | menuAction()->setIconVisibleInMenu(true); |
---|
35 | |
---|
36 | m_openActionGroup = new QActionGroup(this); |
---|
37 | connect(m_openActionGroup, SIGNAL(triggered(QAction*)), SLOT(on_open_triggered(QAction*))); |
---|
38 | |
---|
39 | m_separatorAction = addSeparator(); |
---|
40 | |
---|
41 | m_clearListAction = addAction(tr("&Clear list")); |
---|
42 | connect(m_clearListAction, SIGNAL(triggered()), SLOT(on_clearList_triggered())); |
---|
43 | |
---|
44 | foreach(const QString& absoluteFilePath, absoluteFilePaths) |
---|
45 | { |
---|
46 | addOpenAction(QFileInfo(absoluteFilePath)); |
---|
47 | } |
---|
48 | } |
---|
49 | |
---|
50 | void RecentlyUsedMenu::addOpenAction(const QFileInfo& fileInfo) |
---|
51 | { |
---|
52 | foreach(QAction* action, m_openActionGroup->actions()) |
---|
53 | { |
---|
54 | if(action->data().toString() == fileInfo.absoluteFilePath()) |
---|
55 | { |
---|
56 | removeAction(action); |
---|
57 | m_openActionGroup->removeAction(action); |
---|
58 | |
---|
59 | insertAction(actions().first(), action); |
---|
60 | m_openActionGroup->addAction(action); |
---|
61 | |
---|
62 | return; |
---|
63 | } |
---|
64 | } |
---|
65 | |
---|
66 | if(m_openActionGroup->actions().count() >= m_count) |
---|
67 | { |
---|
68 | QAction* first = m_openActionGroup->actions().first(); |
---|
69 | |
---|
70 | removeAction(first); |
---|
71 | m_openActionGroup->removeAction(first); |
---|
72 | |
---|
73 | delete first; |
---|
74 | } |
---|
75 | |
---|
76 | QAction* action = new QAction(fileInfo.completeBaseName(), this); |
---|
77 | action->setToolTip(fileInfo.absoluteFilePath()); |
---|
78 | action->setData(fileInfo.absoluteFilePath()); |
---|
79 | |
---|
80 | insertAction(actions().first(), action); |
---|
81 | m_openActionGroup->addAction(action); |
---|
82 | } |
---|
83 | |
---|
84 | void RecentlyUsedMenu::removeOpenAction(const QString& filePath) |
---|
85 | { |
---|
86 | const QFileInfo fileInfo(filePath); |
---|
87 | |
---|
88 | foreach(QAction* action, m_openActionGroup->actions()) |
---|
89 | { |
---|
90 | if(action->data().toString() == fileInfo.absoluteFilePath()) |
---|
91 | { |
---|
92 | delete action; |
---|
93 | |
---|
94 | break; |
---|
95 | } |
---|
96 | } |
---|
97 | } |
---|
98 | |
---|
99 | QStringList RecentlyUsedMenu::filePaths() const |
---|
100 | { |
---|
101 | QStringList filePaths; |
---|
102 | |
---|
103 | foreach(const QAction* action, m_openActionGroup->actions()) |
---|
104 | { |
---|
105 | filePaths.append(action->data().toString()); |
---|
106 | } |
---|
107 | |
---|
108 | return filePaths; |
---|
109 | } |
---|
110 | |
---|
111 | void RecentlyUsedMenu::on_open_triggered(QAction* action) |
---|
112 | { |
---|
113 | emit openTriggered(action->data().toString()); |
---|
114 | } |
---|
115 | |
---|
116 | void RecentlyUsedMenu::on_clearList_triggered() |
---|
117 | { |
---|
118 | qDeleteAll(m_openActionGroup->actions()); |
---|
119 | } |
---|
120 | |
---|
121 | } // qpdfview |
---|