QElectroTech  0.70
recentfiles.cpp
Go to the documentation of this file.
1 /*
2  Copyright 2006-2019 The QElectroTech Team
3  This file is part of QElectroTech.
4 
5  QElectroTech is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 2 of the License, or
8  (at your option) any later version.
9 
10  QElectroTech is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with QElectroTech. If not, see <http://www.gnu.org/licenses/>.
17 */
18 #include "recentfiles.h"
19 #include "qeticons.h"
20 #include <QMenu>
21 
29 RecentFiles::RecentFiles(const QString &identifier, int size, QObject *parent) :
30  QObject(parent),
31  identifier_(identifier.isEmpty() ? "unnamed" : identifier),
32  size_(size > 0 ? size : 10),
33  menu_(nullptr)
34 {
35  mapper_ = new QSignalMapper(this);
36  connect(mapper_, SIGNAL(mapped(const QString &)), this, SLOT(handleMenuRequest(const QString &)));
37 
39  buildMenu();
40 }
41 
47  delete menu_;
48 }
49 
53 int RecentFiles::size() const {
54  return(size_);
55 }
56 
60 QMenu *RecentFiles::menu() const {
61  return(menu_);
62 }
63 
69  return(files_icon_);
70 }
71 
77 void RecentFiles::setIconForFiles(const QIcon &icon) {
78  files_icon_ = icon;
79  buildMenu();
80 }
81 
86  list_.clear();
87  buildMenu();
88 }
89 
95 }
96 
100 void RecentFiles::handleMenuRequest(const QString &filepath) {
101  emit(fileOpeningRequested(filepath));
102 }
103 
108 void RecentFiles::fileWasOpened(const QString &filepath) {
109  insertFile(filepath);
110  buildMenu();
111 }
112 
118 {
119  //Forget the list of recent files
120  list_.clear();
121 
122  //Get the last opened file from the settings
123  QSettings settings;
124  for (int i = size_ ; i >= 1 ; -- i)
125  {
126  QString key(identifier_ + "-recentfiles/file" + QString::number(i));
127  QString value(settings.value(key, QString()).toString());
128  insertFile(value);
129  }
130 }
131 
135 void RecentFiles::insertFile(const QString &filepath) {
136  // s'assure que le chemin soit exprime avec des separateurs conformes au systeme
137  QString filepath_ns = QDir::toNativeSeparators(filepath);
138 
139  // evite d'inserer un chemin de fichier vide ou en double
140  if (filepath_ns.isEmpty()) return;
141  list_.removeAll(filepath_ns);
142 
143  // insere le chemin de fichier
144  list_.push_front(filepath_ns);
145 
146  // s'assure que l'on ne retient pas plus de fichiers que necessaire
147  while (list_.count() > size_) list_.removeLast();
148 }
149 
155 {
156  QSettings settings;
157  for (int i = 0 ; i < size_ && i < list_.count() ; ++ i)
158  {
159  QString key(identifier_ + "-recentfiles/file" + QString::number(i + 1));
160  settings.setValue(key, list_[i]);
161  }
162 }
163 
168  // reinitialise le menu
169  if (!menu_) {
170  menu_ = new QMenu;
171  } else {
172  menu_ -> clear();
173  }
174 
175  // remplit le menu
176  foreach (QString filepath, list_) {
177  // creee une nouvelle action pour le fichier
178  QAction *action = new QAction(filepath, this);
179  if (!files_icon_.isNull()) {
180  action -> setIcon(files_icon_);
181  }
182  menu_ -> addAction(action);
183 
184  // lie l'action et le mapper
185  mapper_ -> setMapping(action, filepath);
186  connect(action, SIGNAL(triggered()), mapper_, SLOT(map()));
187  }
188 }
void fileWasOpened(const QString &)
QMenu * menu_
Definition: recentfiles.h:65
void handleMenuRequest(const QString &)
void buildMenu()
QIcon files_icon_
Definition: recentfiles.h:67
QIcon iconForFiles() const
Definition: recentfiles.cpp:68
void fileOpeningRequested(const QString &)
~RecentFiles() override
Definition: recentfiles.cpp:46
QList< QString > list_
Definition: recentfiles.h:64
void saveFilesToSettings()
RecentFiles::saveFilesToSettings Write the list of recent files to settings.
void insertFile(const QString &)
QSignalMapper * mapper_
Definition: recentfiles.h:66
int size() const
Definition: recentfiles.cpp:53
QString identifier_
Definition: recentfiles.h:62
RecentFiles(const QString &, int=10, QObject *=nullptr)
Definition: recentfiles.cpp:29
QMenu * menu() const
Definition: recentfiles.cpp:60
void extractFilesFromSettings()
RecentFiles::extractFilesFromSettings Read the list of recent file from settings. ...
void clear()
Definition: recentfiles.cpp:85
void setIconForFiles(const QIcon &)
Definition: recentfiles.cpp:77