Skip to content

Commit

Permalink
recent projects menu added.
Browse files Browse the repository at this point in the history
  • Loading branch information
ponchio committed Jan 31, 2024
1 parent 968f1b8 commit c5f1923
Show file tree
Hide file tree
Showing 10 changed files with 79 additions and 50 deletions.
3 changes: 1 addition & 2 deletions relightlab/imageframe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ ImageFrame::ImageFrame() {
void ImageFrame::init() {
image_list->init();
image_grid->init();
scene->clear();
showImage(0);
fit();
listMode(); //TODO actually use last used mode used by the user
Expand All @@ -106,8 +107,6 @@ void ImageFrame::showImage(int id) {
QMessageBox::critical(this, "Houston we have a problem!", "Could not load image " + filename);
return;
}
if(imagePixmap)
delete imagePixmap;
imagePixmap = new QGraphicsPixmapItem(QPixmap::fromImage(img));
imagePixmap->setZValue(-1);
scene->addItem(imagePixmap);
Expand Down
29 changes: 29 additions & 0 deletions relightlab/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "tabwidget.h"
#include "homeframe.h"
#include "imageframe.h"
#include "recentprojects.h"

#include <iostream>
using namespace std;
Expand Down Expand Up @@ -47,6 +48,11 @@ void MainWindow::createMenu() {

menuFile->addAction(qRelightApp->action("new_project"));
menuFile->addAction(qRelightApp->action("open_project"));

recentMenu = new QMenu(tr("&Recent Projects"), this);
menuFile->addMenu(recentMenu);
updateRecentProjectsMenu();

menuFile->addSeparator();
menuFile->addAction(qRelightApp->action("save_project"));
menuFile->addAction(qRelightApp->action("save_project_as"));
Expand All @@ -56,6 +62,29 @@ void MainWindow::createMenu() {
setMenuBar(menubar);
}

void MainWindow::updateRecentProjectsMenu() {
recentMenu->clear();

QStringList recents = recentProjects();

int count = 1;
for (const QString& project : recents) {
QAction *action = new QAction(QString::number(count++) + " | " + project, this);
action->setProperty("filename", project);
connect(action, &QAction::triggered, this, &MainWindow::openRecentProject);
recentMenu->addAction(action);
}
}

void MainWindow::openRecentProject() {
QAction *action = qobject_cast<QAction *>(sender());
if (action) {
QString projectFilename = action->property("filename").toString();
if(!qRelightApp->needsSavingProceed())
return;
qRelightApp->openProject(projectFilename);
}
}


void MainWindow::initInterface() {
Expand Down
6 changes: 5 additions & 1 deletion relightlab/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ class MainWindow: public QMainWindow {
Q_OBJECT
public:
MainWindow();
void setupActions();
void createMenu();
void updateRecentProjectsMenu();
void openRecentProject();

void initInterface(); //initialize interface using the current project
void setTabIndex(int index);
void setTabWidget(QWidget *widget);
Expand All @@ -22,6 +24,8 @@ class MainWindow: public QMainWindow {
TabWidget *tabs;
HomeFrame *home_frame;
ImageFrame *image_frame;
private:
QMenu *recentMenu = nullptr;
};

#endif // MAINWINDOW_H
6 changes: 6 additions & 0 deletions relightlab/preferences.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include "preferences.h"

Preferences::Preferences()
{

}
11 changes: 11 additions & 0 deletions relightlab/preferences.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#ifndef PREFERENCES_H
#define PREFERENCES_H


class Preferences
{
public:
Preferences();
};

#endif // PREFERENCES_H
42 changes: 11 additions & 31 deletions relightlab/recentprojects.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,44 +5,24 @@
#include "recentprojects.h"


/*QList<QVariant> list = QSettings().value("Recents", QList<QVariant>()).toList();
std::vector<RecentProject> m_recents;
for(QVariant &v: list) {
auto r = v.toMap();
RecentProject p;
p.filename = r["filename"].toString();
}*/

QVariant RecentProject::toVariant() {
QMap<QString, QVariant> recent;
recent["filename"] = QVariant(filename);
recent["title"] = QVariant(title);
recent["notes"] = QVariant(notes);
return recent;
}

void RecentProject::fromVariant(const QVariant &v) {
QMap<QString, QVariant> recent = v.toMap();
filename = recent["filename"].toString();
title = recent["title"].toString();
notes = recent["notes"].toString();
QStringList recentProjects() {
return QSettings().value("recent-projects", QStringList()).toStringList();
}

static std::vector<RecentProject> getRecentProjects() {
QList<QVariant> recents = QSettings().value("RecentProjects", QList<QVariant>()).toList();

std::vector<RecentProject> recentProjects;
for(const QVariant &v: recents) {
recentProjects.push_back(v);
void addRecentProject(const QString &filename) {
QStringList recents = recentProjects();
int index = recents.indexOf(filename);
if (index != -1) { // String found in the list
recents.removeAt(index); // Remove the string from its current position
}
return recentProjects;
recents.prepend(filename); // Add the string to the front
QSettings().setValue("recent-projects", recents);
}

static void addRecentProject(QString filename, QString title, QString notes) {

void clearRecentProjects() {
QSettings().setValue("recent-projects", QStringList());
}

static void clearRecentProjects() {

}

19 changes: 4 additions & 15 deletions relightlab/recentprojects.h
Original file line number Diff line number Diff line change
@@ -1,24 +1,13 @@
#ifndef RECENTPROJECTS_H
#define RECENTPROJECTS_H

#include <QString>
#include <QStringList>
#include <vector>

class RecentProject {
public:
QString filename;
QString title;
QString notes;

RecentProject() {}
RecentProject(const QVariant &v) { fromVariant(v); }
QVariant toVariant();
void fromVariant(const QVariant &v);
};

static std::vector<RecentProject> getRecentProjects();
static void addRecentProject(QString filename, QString title, QString notes);
static void clearRecentProjects();
QStringList recentProjects();
void addRecentProject(const QString &filename);
void clearRecentProjects();


#endif // RECENTPROJECTS_H
7 changes: 7 additions & 0 deletions relightlab/relightapp.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "relightapp.h"
#include "../relight/processqueue.h"
#include "imageframe.h"
#include "recentprojects.h"

#include <QMessageBox>
#include <QFileDialog>
Expand Down Expand Up @@ -148,7 +149,10 @@ void RelightApp::openProject() {
QString filename = QFileDialog::getOpenFileName(mainwindow, "Select a project", qRelightApp->lastProjectDir(), "*.relight");
if(filename.isNull())
return;
openProject(filename);
}

void RelightApp::openProject(const QString &filename) {
Project project;
try {
project.load(filename);
Expand Down Expand Up @@ -200,6 +204,8 @@ void RelightApp::openProject() {
mainwindow->initInterface();
mainwindow->setTabIndex(1);
mainwindow->image_frame->init();
addRecentProject(filename);
mainwindow->updateRecentProjectsMenu();
}

void RelightApp::saveProject() {
Expand Down Expand Up @@ -247,3 +253,4 @@ QAction *RelightApp::addAction(const QString &id, const QString &label, const QS
connect(a, SIGNAL(triggered()), this, method);
return a;
}

4 changes: 3 additions & 1 deletion relightlab/relightapp.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class RelightApp: public QApplication {
public slots:
void newProject();
void openProject();
void openProject(const QString &filename);
void saveProject();
void saveProjectAs();
void close();
Expand All @@ -45,10 +46,11 @@ public slots:
void setLastProjectDir(QString dir) {
QSettings().setValue("LastProjectDir", dir);
}
bool needsSavingProceed();

private:
QAction *addAction(const QString &id, const QString &label, const QString &icon, const QString &shortcut, const char *method = nullptr);
bool needsSavingProceed();


//keep memory of current project filename for quick saving.
QString project_filename;
Expand Down
2 changes: 2 additions & 0 deletions relightlab/relightlab.pro
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ SOURCES += main.cpp \
../src/white.cpp \
canvas.cpp \
mainwindow.cpp \
preferences.cpp \
recentprojects.cpp \
relightapp.cpp \
tabwidget.cpp \
Expand Down Expand Up @@ -74,6 +75,7 @@ HEADERS += \
canvas.h \
mainwindow.h \
mainwindow.h \
preferences.h \
recentprojects.h \
relightapp.h \
tabwidget.h \
Expand Down

0 comments on commit c5f1923

Please sign in to comment.