tuchulcha  0.10.1
Graphical Workflow Configuration Editor
RecentFileHandler.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2012 Jens-Malte Gottfried
2 
3  This file is part of Tuchulcha.
4 
5  Tuchulcha is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  Tuchulcha 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 Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with Tuchulcha. If not, see <http://www.gnu.org/licenses/>.
17  */
25 #include "RecentFileHandler.h"
26 
27 #include <QAction>
28 #include <QSettings>
29 #include <QFileInfo>
30 #include <QMenu>
31 
32 RecentFileHandler::RecentFileHandler(QObject* pp) : QObject(pp) {
33 }
34 
36 QAction *action = qobject_cast<QAction *>(sender());
37  if (action) {
38  emit openFile(action->data().toString());
39  }
40 }
41 
43  _separatorAct = menu->addSeparator();
44  for (int i = 0; i < _maxRecentFiles; ++i) {
45  _recentFileActs[i] = new QAction(this);
46  _recentFileActs[i]->setIcon(QIcon::fromTheme(
47  "document-open-recent",QIcon(":/icons/document-open-recent.png")));
48  _recentFileActs[i]->setVisible(false);
49  connect(_recentFileActs[i],
50  SIGNAL(triggered()), SLOT(_openRecentFile()));
51  menu->addAction(_recentFileActs[i]);
52  }
54 }
55 
57  QSettings settings;
58  QStringList files = settings.value("recentFileList").toStringList();
59 
60  int numRecentFiles = qMin(files.size(), (int)_maxRecentFiles);
61 
62  for (int i = 0; i < numRecentFiles; ++i) {
63  QString text = QString("&%1 %2")
64  .arg(i + 1).arg(_strippedName(files[i]));
65  _recentFileActs[i]->setText(text);
66  _recentFileActs[i]->setData(files[i]);
67  _recentFileActs[i]->setStatusTip(
68  tr("Open recent file \"%1\"").arg(files[i]));
69  _recentFileActs[i]->setVisible(true);
70  }
71  for (int j = numRecentFiles; j < _maxRecentFiles; ++j)
72  _recentFileActs[j]->setVisible(false);
73 
74  _separatorAct->setVisible(numRecentFiles > 0);
75 }
76 
77 QString RecentFileHandler::_strippedName(QString fullFileName) {
78  return QFileInfo(fullFileName).fileName();
79 }
80 
81 void RecentFileHandler::setCurrentFile(QString fileName) {
82  if (fileName.isEmpty())
83  return;
84 
85  QSettings settings;
86  QStringList files = settings.value("recentFileList").toStringList();
87  QString fname = QFileInfo(fileName).absoluteFilePath();
88  files.removeAll(fname);
89  files.prepend(fname);
90  while (files.size() > _maxRecentFiles)
91  files.removeLast();
92 
93  settings.setValue("recentFileList", files);
94 
96 }
97 
void _updateRecentFileActions()
updated the list of recent files
static QString _strippedName(QString fullFileName)
strip file name
RecentFileHandler declaration.
RecentFileHandler(QObject *parent=0)
default constructor
void openFile(QString fileName)
open file with given name
QAction * _separatorAct
separator above recent files
void registerRecentFileEntries(QMenu *menu)
add entries to the given menu
void setCurrentFile(QString fileName)
insert file as current file to recent file list
void _openRecentFile()
open recent file
QAction * _recentFileActs[_maxRecentFiles]
actions to open recent files