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