Fréchet View  1.6.0
A Tool for Exploring Fréchet Distance Algorithms
filehistory.cpp
Go to the documentation of this file.
1 
2 #include <filehistory.h>
3 
4 #include <QFileInfo>
5 
6 #ifdef __unix__
7 # include<sys/stat.h>
8 #endif
9 
10 using namespace frechet;
11 using namespace app;
12 
13 FileEntry::FileEntry(QString apath, size_t anid, QDateTime adate)
14  : path(apath), id(anid), used(adate)
15 { }
16 
17 bool FileEntry::operator<(const FileEntry &that) const {
18  // sort by Timestamp DESC
19  return used > that.used;
20 }
21 
22 
23 size_t FileHistory::insert(QString path, QDateTime date)
24 {
25  int idx = indexOf(path);
26  if (idx < 0) {
27  files.push_front(FileEntry(path,++next_id,date));
28 
29  createMenuItem(files[0],0);
30  }
31  else {
32  files.move(idx,0);
33  files[0].used = date;
34 
35  moveMenuItem(idx,0);
36  }
37  return files[0].id;
38 
39  // TODO updte menu
40 }
41 
42 
44  : files(), groupName(), next_id(0), menu(NULL)
45 { }
46 
47 void FileHistory::init(QString agroup)
48 {
49  groupName = agroup;
50  menu = new QMenu();
51 }
52 
53 void FileHistory::restore(QSettings& settings)
54 {
55  settings.beginGroup(groupName);
56  next_id = settings.value("next_id").toUInt();
57 
58  QStringList children = settings.childGroups();
59  foreach(QString id, children)
60  {
61  settings.beginGroup(id);
62  QString path = settings.value("path").toString();
63 
64  if (QFileInfo::exists(path))
65  {
66  QDateTime used = settings.value("used").toDateTime();
67  files.push_back(FileEntry(path, id.toUInt(), used));
68  settings.endGroup();
69  }
70  else
71  {
72  settings.endGroup();
73  settings.remove(id);
74  }
75  }
76 
77  settings.endGroup();
78 
79  // sort by Timestamp DESC
80  qSort(files);
81  // create sub-menu
82  for(int idx=0; idx < files.size(); ++idx)
83  createMenuItem(files[idx], idx);
84 }
85 
86 size_t FileHistory::lastId() const
87 {
88  if (files.isEmpty())
89  return 0;
90  else
91  return files[0].id;
92 }
93 
94 QString FileHistory::lastFile() const {
95  if (files.isEmpty())
96  return QString();
97  else
98  return files[0].path;
99 }
100 
101 void FileHistory::attachMenu(QAction* recent)
102 {
103  recent->setEnabled(!files.isEmpty());
104  recent->setMenu(menu);
105 }
106 
107 void FileHistory::beginFile(QSettings &settings, size_t id, bool forWrite)
108 {
109  int idx = indexOf(id);
110  Q_ASSERT(idx >= 0);
111 
112  settings.beginGroup(groupName);
113  if (forWrite) {
114  settings.setValue("next_id",(uint)next_id);
115  }
116 
117  settings.beginGroup(QString::number(files[idx].id));
118 
119  if (forWrite) {
120  settings.setValue("path",files[idx].path);
121  settings.setValue("used",files[idx].used);
122  }
123  // note: path is not stored, as it is subject to change
124 
125  // next, let the appplication store their settings
126 }
127 
128 void FileHistory::endFile(QSettings& settings)
129 {
130  settings.endGroup();
131  settings.endGroup();
132 }
133 
135 {
136  QAction* action = (QAction*) QObject::sender();
137  size_t id = action->data().toUInt();
138  int idx = indexOf(id);
139  if (idx >= 0) {
140  emit open(files[idx].path);
141  }
142 }
143 
144 
145 int FileHistory::indexOf(size_t id)
146 {
147  for(int i=0; i < files.size(); ++i)
148  if (files[i].id == id)
149  return i;
150  return -1;
151 }
152 
153 QAction* FileHistory::createMenuItem(const FileEntry& e, int position)
154 {
155  QFileInfo finfo(e.path);
156  QAction* action = menu->addAction(finfo.fileName());
157  action->setData((uint)e.id);
158 
159  connect(action,SIGNAL(triggered()), this, SLOT(onMenuSelected()));
160 
161  moveMenuItem(menu->actions().size()-1, 0);
162  return action;
163 }
164 
165 void FileHistory::moveMenuItem(int from, int to)
166 {
167  menu->actions().move(from,to);
168 }
169 
170 int FileHistory::indexOf(QString path)
171 {
172  for(int i=0; i < files.size(); ++i)
173  if (files[i].path == path)
174  return i;
175  return -1;
176 }
177 
178 
void onMenuSelected()
called when an item from the Open Recent menu is selected
bool operator<(const FileEntry &that) const
compare files by their last-used date
Definition: filehistory.cpp:17
QString groupName
group identifier if preferences
Definition: filehistory.h:55
void restore(QSettings &settings)
read settings from application preferences
Definition: filehistory.cpp:53
void attachMenu(QAction *recent)
attach sub-menu
global definitions for all algorithms.
int indexOf(QString path)
find file by name
QAction * createMenuItem(const FileEntry &entry, int position)
create a new menu item in File/Open Recent
QString path
absolute path to file
Definition: filehistory.h:16
FileEntry(QString apath, size_t id, QDateTime adate=QDateTime::currentDateTime())
default constructor
Definition: filehistory.cpp:13
size_t next_id
next identifier
Definition: filehistory.h:57
an entry in the recently opened file list
Definition: filehistory.h:12
void open(QString)
raised when a file is selected from the "Open Recent" menu
void beginFile(QSettings &settings, size_t id, bool forWrite=false)
begin reading or updating file specific settings
size_t id
unique identifier issued by FileHistory
Definition: filehistory.h:18
QString lastFile() const
Definition: filehistory.cpp:94
QDateTime used
time when the file was last used
Definition: filehistory.h:20
FileHistory()
empty constructor
Definition: filehistory.cpp:43
void endFile(QSettings &settings)
end updating file specific settings
QMenu * menu
pointer to File/Open Recent menu
Definition: filehistory.h:59
QList< FileEntry > files
list of file IDs; sorted by LRU
Definition: filehistory.h:53
void moveMenuItem(int from, int to)
move a menu item
size_t insert(QString path, QDateTime date=QDateTime::currentDateTime())
insert a new file into the list
Definition: filehistory.cpp:23
void init(QString agroup)
initialize file list and menu
Definition: filehistory.cpp:47