]> Shamusworld >> Repos - architektonas/blob - src/widgets/qg_recentfiles.cpp
Initial import
[architektonas] / src / widgets / qg_recentfiles.cpp
1 // qg_recentfiles.cpp
2 //
3 // Part of the Architektonas Project
4 // Originally part of QCad Community Edition by Andrew Mustun
5 // Extensively rewritten and refactored by James L. Hammons
6 // (C) 2010 Underground Software
7 //
8 // JLH = James L. Hammons <jlhamm@acm.org>
9 //
10 // Who  When        What
11 // ---  ----------  -----------------------------------------------------------
12 // JLH  05/11/2010  Added this text. :-)
13 //
14
15 #include "qg_recentfiles.h"
16
17 #include "qc_applicationwindow.h"
18 #include "rs_debug.h"
19
20 /**
21  * Constructor
22  * @param number Number of files that can be stored in the list at maximum
23  */
24 QG_RecentFiles::QG_RecentFiles(QWidget * parent, QMenu * menu, int num/*= 9*/):
25         QObject(), number(num)
26 {
27         // We make a separator that we can control the visibility of...
28         separator = menu->addSeparator();
29         separator->setVisible(false);
30
31         // We create all the actions up front so we don't have to mess with them
32         // later...
33         for(int i=0; i<number; i++)
34         {
35                 QAction * action = new QAction(parent);
36                 action->setVisible(false);
37                 menu->addAction(action);
38                 connect(action, SIGNAL(triggered()), parent, SLOT(slotFileOpenRecent()));
39                 actionList.append(action);
40         }
41 }
42
43 /**
44  * Destructor
45  */
46 QG_RecentFiles::~QG_RecentFiles()
47 {
48         while (!actionList.isEmpty())
49                 delete actionList.takeFirst();
50 }
51
52 /**
53  * Adds a file to the list of recently loaded files if
54  * it's not already in the list.
55  */
56 void QG_RecentFiles::add(const QString & filename)
57 {
58         RS_DEBUG->print("QG_RecentFiles::add");
59
60         // Is the file already in the list? Bail out if so...
61         if (files.indexOf(filename) != -1)
62                 return;
63
64         files.append(filename);
65
66         // Keep the list under our limit
67         if ((int)files.count() > number)
68                 files.pop_front();
69
70         RS_DEBUG->print("QG_RecentFiles::add: OK");
71 }
72
73 /**
74  * @return complete path and name of the file stored in the
75  * list at index i
76  */
77 QString QG_RecentFiles::get(int i)
78 {
79         if (i < (int)files.count())
80                 return files[i];
81
82         return QString("");
83 }
84
85 /** @return number of files currently stored in the list */
86 int QG_RecentFiles::count(void)
87 {
88         return files.count();
89 }
90
91 /** @return number of files that can be stored in the list at maximum */
92 int QG_RecentFiles::Maximum(void)
93 {
94         return number;
95 }
96
97 void QG_RecentFiles::UpdateGUI(void)
98 {
99         int numRecentFiles = count();
100
101         for(int i=0; i<numRecentFiles; i++)
102         {
103 //              QString text = tr("&%1 %2").arg(i + 1).arg(strippedName(files[i]));
104                 QString text = tr("&%1 %2").arg(i + 1).arg(files[i]);
105                 actionList[i]->setText(text);
106                 actionList[i]->setData(files[i]);
107                 actionList[i]->setVisible(true);
108         }
109
110         for(int j=numRecentFiles; j<Maximum(); j++)
111                 actionList[j]->setVisible(false);
112
113         separator->setVisible(numRecentFiles > 0);
114 }