Skip to content

Commit

Permalink
refactoring actions into relightapp.
Browse files Browse the repository at this point in the history
  • Loading branch information
ponchio committed Jan 24, 2024
1 parent ec4ff3d commit 30e4b72
Show file tree
Hide file tree
Showing 14 changed files with 236 additions and 148 deletions.
28 changes: 0 additions & 28 deletions relightlab/actions.cpp

This file was deleted.

17 changes: 0 additions & 17 deletions relightlab/actions.h

This file was deleted.

10 changes: 6 additions & 4 deletions relightlab/homeframe.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#include "homeframe.h"
#include <QVBoxLayout>
#include <QLabel>
#include <QPushButton>
#include <QTextBrowser>
#include <QFile>
#include "actions.h"
#include <QAction>

#include "homeframe.h"
#include "relightapp.h"

void setDefaultAction(QPushButton *button, QAction *action) {
QObject::connect(button, SIGNAL(clicked(bool)), action, SLOT(trigger()));
Expand All @@ -30,11 +32,11 @@ HomeFrame::HomeFrame() {
leftColumnLayout->addWidget(titleLabel);

QPushButton *new_project = new QPushButton(this);
setDefaultAction(new_project, Action::new_project);
setDefaultAction(new_project, qRelightApp->action("new_project"));
leftColumnLayout->addWidget(new_project);

QPushButton *open_project = new QPushButton(this);
setDefaultAction(open_project, Action::open_project);
setDefaultAction(open_project, qRelightApp->action("open_project"));
leftColumnLayout->addWidget(open_project);

QLabel *recentLabel = new QLabel("Recent projects:");
Expand Down
14 changes: 14 additions & 0 deletions relightlab/imagestab.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QToolBar>

#include "imagestab.h"
#include "relightapp.h"

ImagesTab::ImagesTab() {

QVBoxLayout *container = new QVBoxLayout(this);

QToolBar *toolbar = new QToolBar(this);
toolbar->addAction(qRelightApp->action("fullscreen"));
}
11 changes: 11 additions & 0 deletions relightlab/imagestab.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#ifndef IMAGESTAB_H
#define IMAGESTAB_H

#include <QFrame>

class ImagesTab: public QFrame {
public:
ImagesTab();
};

#endif // IMAGESTAB_H
99 changes: 75 additions & 24 deletions relightlab/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@
#include "tabwidget.h"
#include "homeframe.h"
#include "imageframe.h"
#include "actions.h"

#include <iostream>
using namespace std;

MainWindow::MainWindow() {
Action::initialize();
setupActions();
createMenu();

Expand All @@ -38,45 +36,86 @@ void MainWindow::createMenu() {
menuFile->setTitle("File");
menubar->addAction(menuFile->menuAction());

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

menuFile->addSeparator();
menuFile->addSeparator();
menuFile->addAction(Action::exit);
menuFile->addAction(qRelightApp->action("exit"));

setMenuBar(menubar);
}

void MainWindow::setupActions() {
connect(Action::new_project, SIGNAL(triggered(bool)), this, SLOT(newProject()));
connect(Action::open_project, SIGNAL(triggered(bool)), this, SLOT(openProject()));
connect(Action::close_project, SIGNAL(triggered(bool)), this, SLOT(closeProject()));
connect(qRelightApp->action("new_project"), SIGNAL(triggered(bool)), this, SLOT(newProject()));
connect(qRelightApp->action("open_project"), SIGNAL(triggered(bool)), this, SLOT(openProject()));
connect(qRelightApp->action("close_project"), SIGNAL(triggered(bool)), this, SLOT(closeProject()));

connect(Action::exit, SIGNAL(triggered(bool)), this, SLOT(close()));
connect(qRelightApp->action("exit"), SIGNAL(triggered(bool)), this, SLOT(close()));

}

void MainWindow::clear() {
void MainWindow::newProject() {
if(!needsSavingProceed())
return;

}
QString dir = QFileDialog::getExistingDirectory(this, "Choose picture folder", qRelightApp->lastProjectDir());
if(dir.isNull()) return;

void MainWindow::newProject() {
cout << "New Project" << endl;
Action::close_project->setEnabled(true);

Project project;
project.setDir(QDir(dir));
bool ok = project.scanDir();
if(!project.size()) {
QMessageBox::critical(this, "Houston we have a problem!", "Could not find images in directory: " + project.dir.path());
return;
}

if(!ok) {
//check if we can rotate a few images.
bool canrotate = false;
for(Image &image: project.images) {
if(image.size == project.imgsize)
continue;

if(image.isRotated(project.imgsize))
canrotate = true;
}
if(canrotate) {
int answer = QMessageBox::question(this, "Some images are rotated.", "Do you wish to uniform image rotation?", QMessageBox::Yes, QMessageBox::No);
if(answer != QMessageBox::No)
project.rotateImages();
} else
QMessageBox::critical(this, "Resolution problem", "Not all of the images in the folder have the same resolution,\nyou might need to fix this problem manually.");
}

qRelightApp->project() = project;

initInterface();

/* TODO: move this into the lights tab
QStringList img_ext;
img_ext << "*.lp";
QStringList lps = QDir(dir).entryList(img_ext);
if(lps.size() > 0) {
int answer = QMessageBox::question(this, "Found an .lp file: " + lps[0], "Do you wish to load " + lps[0] + "?", QMessageBox::Yes, QMessageBox::No);
if(answer != QMessageBox::No)
loadLP(lps[0]);
}
*/

qRelightApp->action("close_project")->setEnabled(true);
tabs->setCurrentIndex(1);
}

void MainWindow::openProject() {
QString lastDir = QSettings().value("LastDir", QDir::homePath()).toString();

QString filename = QFileDialog::getOpenFileName(this, "Select a project", lastDir, "*.relight");
if(!needsSavingProceed())
return;
QString filename = QFileDialog::getOpenFileName(this, "Select a project", qRelightApp->lastProjectDir(), "*.relight");
if(filename.isNull())
return;

clear();

Project project;
try {
project.load(filename);
Expand Down Expand Up @@ -121,17 +160,29 @@ void MainWindow::openProject() {
}
}

clear();
qRelightApp->project() = project;
project_filename = filename; //project.dir.relativeFilePath(filename);
qRelightApp->setLastProjectDir(project.dir.path());

QSettings().setValue("LastDir", project.dir.path());
initInterface();
qRelightApp->action("close_project")->setEnabled(true);

Action::close_project->setEnabled(true);
tabs->setCurrentIndex(1);
}

void MainWindow::closeProject() {

cout << "close Project" << endl;
Action::close_project->setEnabled(false);
qRelightApp->action("close_project")->setEnabled(false);
}

void MainWindow::initInterface() {

}

bool MainWindow::needsSavingProceed() {
if(!qRelightApp->project().needs_saving);
return true;
auto answer = QMessageBox::question(this, "Current project is unsaved", "Do you want to proceed without saving?");
return answer == QMessageBox::Yes;
}
4 changes: 3 additions & 1 deletion relightlab/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class MainWindow: public QMainWindow {
MainWindow();
void setupActions();
void createMenu();
void clear();
void initInterface(); //initialize interface using the current project

public slots:
void newProject();
Expand All @@ -22,6 +22,8 @@ public slots:
private:
TabWidget *tabs;
QString project_filename; //last filename project opened or saved filename.

bool needsSavingProceed(); //return false if userd oesn't want to proceed
};

#endif // MAINWINDOW_H
8 changes: 7 additions & 1 deletion relightlab/recentprojects.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <QSettings>
#include <QVariant>
#include <QMap>
#include <QList>
#include "recentprojects.h"


Expand All @@ -20,15 +21,20 @@ QVariant RecentProject::toVariant() {
return recent;
}

void RecentProject::fromVariant(QVariant &v) {
void RecentProject::fromVariant(const QVariant &v) {
QMap<QString, QVariant> recent = v.toMap();
filename = recent["filename"].toString();
title = recent["title"].toString();
notes = recent["notes"].toString();
}

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);
}
}
static void addRecentProject(QString filename, QString title, QString notes) {

Expand Down
4 changes: 3 additions & 1 deletion relightlab/recentprojects.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ class RecentProject {
QString title;
QString notes;

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

static std::vector<RecentProject> getRecentProjects();
Expand Down
28 changes: 28 additions & 0 deletions relightlab/relightapp.cpp
Original file line number Diff line number Diff line change
@@ -1,2 +1,30 @@
#include "relightapp.h"

#include <QAction>


RelightApp::RelightApp(int &argc, char **argv): QApplication(argc, argv) {
QTemporaryDir tmp;
if(!tmp.isValid()) {
QMessageBox::critical(nullptr, "Temporary folder is needed", "Could not create a temporary file for the scripts.\nSelect a folder in File->Preferences");
}

QFile style(":/css/style.txt");
style.open(QFile::ReadOnly);
setStyleSheet(style.readAll());

ProcessQueue &queue = ProcessQueue::instance();
queue.start();


addAction("new_project", "New project...", "Ctrl+N");
addAction("open_project", "Open project...", "Ctrl+O");
addAction("close_project", "Close project", "Ctrl-W");
addAction("exit", "Exit", "Alt+F4");
}

void RelightApp::addAction(const QString &id, const QString &label, const QString &shortcut) {
QAction *a = new QAction(label);
a->setShortcut(shortcut);
actions[id] = a;
}
Loading

0 comments on commit 30e4b72

Please sign in to comment.