source: terepaima/terepaima-0.4.16/sources/recentlyusedmenu.cpp

desarrollostretch
Last change on this file was 1f4adec, checked in by aosorio <aosorio@…>, 8 years ago

Agregado proyecto base, esto luego del dh_make -f

  • Property mode set to 100644
File size: 3.2 KB
Line 
1/*
2
3Copyright 2012-2014 Adam Reichold
4
5This file is part of qpdfview.
6
7qpdfview is free software: you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation, either version 2 of the License, or
10(at your option) any later version.
11
12qpdfview is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with qpdfview.  If not, see <http://www.gnu.org/licenses/>.
19
20*/
21
22#include "recentlyusedmenu.h"
23
24#include <QFileInfo>
25
26namespace qpdfview
27{
28
29RecentlyUsedMenu::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
50void 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
84void 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
99QStringList 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
111void RecentlyUsedMenu::on_open_triggered(QAction* action)
112{
113    emit openTriggered(action->data().toString());
114}
115
116void RecentlyUsedMenu::on_clearList_triggered()
117{
118    qDeleteAll(m_openActionGroup->actions());
119}
120
121} // qpdfview
Note: See TracBrowser for help on using the repository browser.