Vuo  2.3.2
VuoRecentFileMenu.cc
Go to the documentation of this file.
1 
10 #include "VuoRecentFileMenu.hh"
11 #include "VuoEditor.hh"
12 #include "VuoFileUtilities.hh"
13 #include "VuoStringUtilities.hh"
14 
15 const int VuoRecentFileMenu::maxRecentFileCount = 10;
16 
21  QMenu(parent)
22 {
23  setTitle(QApplication::translate("VuoEditorWindow", "Open Recent"));
24  updateRecentFileActions();
25 }
26 
30 void VuoRecentFileMenu::addFile(const QString &filePath)
31 {
32  recentFiles.removeAll(filePath);
33  recentFiles.prepend(filePath);
34 
35  while (recentFiles.size() > maxRecentFileCount)
36  recentFiles.removeLast();
37 
38  updateRecentFileActions();
39 }
40 
46 {
47  QStringList currentlyExistingRecentFiles = QStringList(recentFiles);
48  bool foundMissingFile = false;
49 
50  // Filter out recent files that no longer exist.
51  recentFiles.clear();
52  foreach (QString recentFile, currentlyExistingRecentFiles)
53  {
54  QUrl url(recentFile);
55  QString fileName;
56  bool currentFileMissing = false;
57 
58  // Determine whether the current file in the list is an example composition.
59  bool isExampleComposition = (url.isValid() && url.scheme() == VuoEditor::vuoExampleCompositionScheme);
60 
61  // If not, assume it is a regular composition.
62  if (!isExampleComposition)
63  {
64  fileName = QFileInfo(recentFile).fileName();
65  if (!VuoFileUtilities::fileExists(recentFile.toStdString()))
66  {
67  currentFileMissing = true;
68  foundMissingFile = true;
69  }
70  }
71 
72  if (!currentFileMissing)
73  recentFiles.append(recentFile);
74  }
75 
76  if (foundMissingFile)
77  updateRecentFileActions();
78 }
79 
84 void VuoRecentFileMenu::updateRecentFileActions()
85 {
86  clear();
87  int currentNumRecentFiles = recentFiles.size();
88  setEnabled(currentNumRecentFiles > 0);
89 
90  // List recently opened files.
91  for (int i = 0; i < currentNumRecentFiles; ++i)
92  {
93  QAction *recentFileAction = new QAction(this);
94  QString fileName, fileURL;
95 
96  QUrl url(recentFiles[i]);
97 
98  // Determine whether the current file in the list is an example composition.
99  if (url.isValid() && url.scheme() == VuoEditor::vuoExampleCompositionScheme)
100  {
101  fileName = VuoStringUtilities::substrAfter(url.path().toUtf8().constData(), "/").c_str();
102  fileURL = QFileInfo(recentFiles[i]).filePath();
103  }
104 
105  // If not, assume it is a regular composition.
106  else
107  {
108  fileName = QFileInfo(recentFiles[i]).fileName();
109  fileURL = QString("file://").append(QFileInfo(recentFiles[i]).filePath());
110  }
111 
112  recentFileAction->setText(fileName);
113  recentFileAction->setData(fileURL);
114  recentFileAction->setIcon(QIcon(":/Icons/vuo-composition.png"));
115  if (i == 0)
116  recentFileAction->setShortcut(QKeySequence("Ctrl+Shift+O"));
117  connect(recentFileAction, &QAction::triggered, this, &VuoRecentFileMenu::recentFileActionTriggered);
118  addAction(recentFileAction);
119  }
120 
121  // Add a "Clear Menu" option.
122  if (currentNumRecentFiles > 0)
123  {
124  addSeparator();
125  QAction *clearRecentFileListAction = new QAction(this);
126  clearRecentFileListAction->setText(QApplication::translate("VuoEditorWindow", "Clear Menu"));
127  connect(clearRecentFileListAction, &QAction::triggered, this, &VuoRecentFileMenu::clearRecentFileListActionTriggered);
128  addAction(clearRecentFileListAction);
129  }
130 }
131 
135 void VuoRecentFileMenu::recentFileActionTriggered()
136 {
137  QAction *sender = (QAction *)QObject::sender();
138  emit recentFileSelected(sender->data().toString());
139 }
140 
145 {
146  if (recentFiles.size())
147  {
148  QUrl url(recentFiles[0]);
149  QString formattedURL;
150 
151  // Determine whether the current file in the list is an example composition.
152  if (url.isValid() && url.scheme() == VuoEditor::vuoExampleCompositionScheme)
153  formattedURL = QFileInfo(recentFiles[0]).filePath();
154 
155  // If not, assume it is a regular composition.
156  else
157  formattedURL = QString("file://").append(QFileInfo(recentFiles[0]).filePath());
158 
159  emit recentFileSelected(formattedURL);
160  }
161 }
162 
167 {
168  recentFiles.clear();
169  updateRecentFileActions();
170  emit recentFileListCleared();
171 }
172 
177 {
178  return recentFiles;
179 }
180 
184 void VuoRecentFileMenu::setRecentFiles(QStringList recentFiles)
185 {
186  this->recentFiles = recentFiles;
187  updateRecentFileActions();
188 }