Skip to content

Commit

Permalink
Wobbly: add list of recently opened files
Browse files Browse the repository at this point in the history
  • Loading branch information
cantabile committed Jan 26, 2016
1 parent 1269d08 commit 7c42627
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 1 deletion.
68 changes: 67 additions & 1 deletion src/wobbly/WobblyWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,43 @@ WobblyWindow::WobblyWindow()
}


void WobblyWindow::addRecentFile(const QString &path) {
int index = -1;
auto actions = recent_menu->actions();
for (int i = 0; i < actions.size(); i++) {
if (actions[i]->text().endsWith(path)) {
index = i;
break;
}
}

if (index == 0) {
return;
} else if (index > 0) {
recent_menu->removeAction(actions[index]);
recent_menu->insertAction(actions[0], actions[index]);
} else {
QAction *recent = new QAction(QStringLiteral("&0. %1").arg(path), this);
connect(recent, &QAction::triggered, recent_menu_signal_mapper, static_cast<void (QSignalMapper::*)()>(&QSignalMapper::map));
recent_menu_signal_mapper->setMapping(recent, path);

recent_menu->insertAction(actions.size() ? actions[0] : 0, recent);

if (actions.size() == 10)
recent_menu->removeAction(actions[9]);
}

actions = recent_menu->actions();
for (int i = 0; i < actions.size(); i++) {
QString text = actions[i]->text();
text[1] = QChar('0' + i);
actions[i]->setText(text);

settings.setValue(QStringLiteral("user_interface/recent%1").arg(i), text.mid(4));
}
}


void WobblyWindow::readSettings() {
if (settings.contains("user_interface/state"))
restoreState(settings.value("user_interface/state").toByteArray());
Expand Down Expand Up @@ -115,6 +152,15 @@ void WobblyWindow::readSettings() {
settings_shortcuts_table->setItem(i, 2, item);
}
settings_shortcuts_table->resizeColumnsToContents();

QStringList recent;
for (int i = 9; i >= 0; i--) {
QString settings_key = QStringLiteral("user_interface/recent%1").arg(i);
if (settings.contains(settings_key))
recent.push_back(settings.value(settings_key).toString());
}
for (int i = 0; i < recent.size(); i++)
addRecentFile(recent[i]);
}


Expand Down Expand Up @@ -231,19 +277,33 @@ void WobblyWindow::createMenu() {
{ "Save screenshot", &WobblyWindow::saveScreenshot },
{ "Import from project", &WobblyWindow::importFromProject },
{ nullptr, nullptr },
{ "&Recently opened", nullptr },
{ nullptr, nullptr },
{ "&Quit", &WobblyWindow::quit }
};

for (size_t i = 0; i < project_menu.size(); i++) {
if (project_menu[i].name) {
if (project_menu[i].name && project_menu[i].func) {
QAction *action = new QAction(project_menu[i].name, this);
connect(action, &QAction::triggered, this, project_menu[i].func);
p->addAction(action);
} else if (project_menu[i].name && !project_menu[i].func) {
// Not very nicely done.
recent_menu = p->addMenu(project_menu[i].name);
} else {
p->addSeparator();
}
}

recent_menu_signal_mapper = new QSignalMapper(this);

connect(recent_menu_signal_mapper, static_cast<void (QSignalMapper::*)(const QString &)>(&QSignalMapper::mapped), [this] (const QString &path) {
if (path.endsWith(".json"))
realOpenProject(path);
else
realOpenVideo(path);
});


tools_menu = bar->addMenu("&Tools");

Expand Down Expand Up @@ -3007,6 +3067,8 @@ void WobblyWindow::realOpenProject(const QString &path) {
delete project;
project = tmp;

addRecentFile(path);

current_frame = project->getLastVisitedFrame();

initialiseUIFromProject();
Expand Down Expand Up @@ -3087,6 +3149,8 @@ void WobblyWindow::realOpenVideo(const QString &path) {
vsscript_clearOutput(vsscript, 1);

evaluateMainDisplayScript();

addRecentFile(path);
} catch(WobblyException &e) {
errorPopup(e.what());

Expand Down Expand Up @@ -3123,6 +3187,8 @@ void WobblyWindow::realSaveProject(const QString &path) {
video_path.clear();

updateWindowTitle();

addRecentFile(path);
}


Expand Down
7 changes: 7 additions & 0 deletions src/wobbly/WobblyWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ SOFTWARE.
#include <QListWidget>
#include <QMainWindow>
#include <QSettings>
#include <QSignalMapper>
#include <QSlider>
#include <QSpinBox>
#include <QStringListModel>
Expand All @@ -57,6 +58,8 @@ class WobblyWindow : public QMainWindow {

QMenu *tools_menu;

QMenu *recent_menu;



// Widgets.
Expand Down Expand Up @@ -153,6 +156,8 @@ class WobblyWindow : public QMainWindow {

QString window_title;

QSignalMapper *recent_menu_signal_mapper;


// Other stuff.

Expand Down Expand Up @@ -215,6 +220,8 @@ class WobblyWindow : public QMainWindow {
void drawColorBars();
void createUI();

void addRecentFile(const QString &path);

void readSettings();
void writeSettings();

Expand Down

0 comments on commit 7c42627

Please sign in to comment.