Skip to content

Commit

Permalink
Shortcuts and exporting fractal with content
Browse files Browse the repository at this point in the history
  • Loading branch information
chrizbee committed Sep 2, 2019
1 parent 63f6365 commit df53d92
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 79 deletions.
2 changes: 1 addition & 1 deletion NewtonFractal.pro
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ QT += core gui widgets concurrent
TARGET = NewtonFractal
TEMPLATE = app
CONFIG += c++14 debug_and_release
VERSION = 1.5.2
VERSION = 1.5.3
DEFINES += APP_VERSION=\\\"$$VERSION\\\"
win32:LIBS += -lOpenGL32
unix:LIBS += -lOpenGL
Expand Down
59 changes: 34 additions & 25 deletions src/fractalwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
#include <QMouseEvent>
#include <QHBoxLayout>
#include <QDateTime>
#include <QShortcut>
#include <QSettings>
#include <QPainter>
#include <QAction>
#include <QIcon>
#include <QDebug>

static QPoint mousePosition;
static const QVector<QVector3D> vertices = QVector<QVector3D>() <<
Expand Down Expand Up @@ -43,23 +43,25 @@ FractalWidget::FractalWidget(QWidget *parent) :
centralLayout->addWidget(settingsWidget_);
setLayout(centralLayout);

// Add actions and connect signals
QAction *quit = new QAction(this);
QAction *hide = new QAction(this);
QAction *orbit = new QAction(this);
QAction *position = new QAction(this);
QAction *settings = new QAction(this);
quit->setShortcut(QKeySequence("Ctrl+Q"));
hide->setShortcut(Qt::Key_Escape);
orbit->setShortcut(Qt::Key_F2);
position->setShortcut(Qt::Key_F3);
settings->setShortcut(Qt::Key_F1);
addActions(QList<QAction*>() << quit << hide << orbit << position << settings);
connect(quit, &QAction::triggered, QApplication::instance(), &QCoreApplication::quit);
connect(hide, &QAction::triggered, [this]() { legend_ = !legend_; update(); });
connect(orbit, &QAction::triggered, [this]() { params_->orbitMode = !params_->orbitMode; updateParams(); });
connect(position, &QAction::triggered, [this]() { position_ = !position_; update(); });
connect(settings, &QAction::triggered, settingsWidget_, &SettingsWidget::toggle);
// Add shortcuts and connect them
QShortcut *quit = new QShortcut(QKeySequence("Ctrl+Q"), this);
QShortcut *hide = new QShortcut(QKeySequence(Qt::Key_Escape), this);
QShortcut *orbit = new QShortcut(QKeySequence(Qt::Key_F2), this);
QShortcut *position = new QShortcut(QKeySequence(Qt::Key_F3), this);
QShortcut *settings = new QShortcut(QKeySequence(Qt::Key_F1), this);
QShortcut *resetFractal = new QShortcut(QKeySequence("Ctrl+R"), this);
QShortcut *exportImage = new QShortcut(QKeySequence("Ctrl+S"), this);
QShortcut *exportSettings = new QShortcut(QKeySequence("Ctrl+E"), this);
QShortcut *importSettings = new QShortcut(QKeySequence("Ctrl+I"), this);
connect(quit, &QShortcut::activated, QApplication::instance(), &QCoreApplication::quit);
connect(hide, &QShortcut::activated, [this]() { legend_ = !legend_; update(); });
connect(orbit, &QShortcut::activated, [this]() { params_->orbitMode = !params_->orbitMode; updateParams(); });
connect(position, &QShortcut::activated, [this]() { position_ = !position_; update(); });
connect(settings, &QShortcut::activated, settingsWidget_, &SettingsWidget::toggle);
connect(resetFractal, &QShortcut::activated, settingsWidget_, &SettingsWidget::reset);
connect(exportImage, &QShortcut::activated, settingsWidget_, &SettingsWidget::exportImage);
connect(exportSettings, &QShortcut::activated, settingsWidget_, &SettingsWidget::exportSettings);
connect(importSettings, &QShortcut::activated, settingsWidget_, &SettingsWidget::importSettings);

// Initialize general stuff
setMinimumSize(nf::MSI, nf::MSI);
Expand All @@ -74,9 +76,9 @@ FractalWidget::FractalWidget(QWidget *parent) :
// Connect settingswidget signals
connect(settingsWidget_, &SettingsWidget::paramsChanged, this, &FractalWidget::updateParams);
connect(settingsWidget_, &SettingsWidget::sizeChanged, this, QOverload<const QSize &>::of(&FractalWidget::resize));
connect(settingsWidget_, &SettingsWidget::exportImage, this, &FractalWidget::exportImage);
connect(settingsWidget_, &SettingsWidget::exportRoots, this, &FractalWidget::exportRoots);
connect(settingsWidget_, &SettingsWidget::importRoots, this, &FractalWidget::importRoots);
connect(settingsWidget_, &SettingsWidget::exportImageTo, this, &FractalWidget::exportImageTo);
connect(settingsWidget_, &SettingsWidget::exportSettingsTo, this, &FractalWidget::exportSettingsTo);
connect(settingsWidget_, &SettingsWidget::importSettingsFrom, this, &FractalWidget::importSettingsFrom);
connect(settingsWidget_, &SettingsWidget::reset, this, &FractalWidget::reset);
connect(this, &FractalWidget::rootMoved, settingsWidget_, &SettingsWidget::moveRoot);
connect(this, &FractalWidget::zoomChanged, settingsWidget_, &SettingsWidget::changeZoom);
Expand All @@ -100,19 +102,26 @@ void FractalWidget::updateParams()
renderThread_.render(*params_);
}

void FractalWidget::exportImage(const QString &dir)
void FractalWidget::exportImageTo(const QString &dir)
{
// Close settingsWidget
bool closed = settingsWidget_->isHidden();
settingsWidget_->setHidden(true);

// Export fractal to file
QString filePath = dir + "/fractal_" +
QDateTime::currentDateTime().toString("yyMMdd_HHmmss_") +
QString::number(params_->roots.count()) + "roots_" +
QString::number(params_->size.width()) + "x" + QString::number(params_->size.width()) + ".png";
QFile f(filePath);
f.open(QIODevice::WriteOnly | QIODevice::Truncate);
pixmap_.save(&f, "png");
grab().save(&f, "png");

// Reopen if needed
settingsWidget_->setHidden(closed);
}

void FractalWidget::exportRoots(const QString &dir)
void FractalWidget::exportSettingsTo(const QString &dir)
{
// Export roots to file
quint8 rootCount = params_->roots.count();
Expand Down Expand Up @@ -159,7 +168,7 @@ void FractalWidget::exportRoots(const QString &dir)
ini.endGroup();
}

void FractalWidget::importRoots(const QString &file)
void FractalWidget::importSettingsFrom(const QString &file)
{
// Open ini file
QSettings ini(file, QSettings::IniFormat, this);
Expand Down
6 changes: 3 additions & 3 deletions src/fractalwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ class FractalWidget : public QOpenGLWidget, protected QOpenGLFunctions
public:
FractalWidget(QWidget *parent = nullptr);
void updateParams();
void exportImage(const QString &dir);
void exportRoots(const QString &dir);
void importRoots(const QString &file);
void exportImageTo(const QString &dir);
void exportSettingsTo(const QString &dir);
void importSettingsFrom(const QString &file);
void reset();

public slots:
Expand Down
79 changes: 39 additions & 40 deletions src/settingswidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
#include <QDateTime>
#include <QMenu>
#include <QUrl>
#include <QDebug>

static bool updateParamsAllowed = true;

Expand All @@ -43,9 +42,9 @@ SettingsWidget::SettingsWidget(Parameters *params, QWidget *parent) :
rootMenu_->addAction(QIcon("://resources/icons/mirrory.png"), tr("Mirror on y-axis"), rootMenu_, &QMenu::hide);

// Connect ui signals to slots
connect(ui_->btnExportImage, &QPushButton::clicked, this, &SettingsWidget::on_btnExportImageClicked);
connect(ui_->btnExportSettings, &QPushButton::clicked, this, &SettingsWidget::on_btnExportSettingsClicked);
connect(ui_->btnImportSettings, &QPushButton::clicked, this, &SettingsWidget::on_btnImportSettingsClicked);
connect(ui_->btnExportImage, &QPushButton::clicked, this, &SettingsWidget::exportImage);
connect(ui_->btnExportSettings, &QPushButton::clicked, this, &SettingsWidget::exportSettings);
connect(ui_->btnImportSettings, &QPushButton::clicked, this, &SettingsWidget::importSettings);
connect(ui_->lineSize, &SizeEdit::sizeChanged, this, &SettingsWidget::sizeChanged);
connect(ui_->btnReset, &QPushButton::clicked, this, &SettingsWidget::reset);
connect(ui_->spinScale, QOverload<int>::of(&QSpinBox::valueChanged), this, &SettingsWidget::on_settingsChanged);
Expand Down Expand Up @@ -173,6 +172,42 @@ void SettingsWidget::moveRoot(quint8 index, complex value)
}
}

void SettingsWidget::exportImage()
{
// Export pixmap to file
QSettings settings;
QString dir = settings.value("imagedir", QStandardPaths::standardLocations(QStandardPaths::PicturesLocation)).toString();
dir = QFileDialog::getExistingDirectory(this, tr("Export fractal to"), dir);
if (!dir.isEmpty()) {
settings.setValue("imagedir", dir);
emit exportImageTo(dir);
}
}

void SettingsWidget::exportSettings()
{
// Export roots to file
QSettings settings;
QString dir = settings.value("settingsdir", QStandardPaths::standardLocations(QStandardPaths::DocumentsLocation)).toString();
dir = QFileDialog::getExistingDirectory(this, tr("Export settings to"), dir);
if (!dir.isEmpty()) {
settings.setValue("settingsdir", dir);
emit exportSettingsTo(dir);
}
}

void SettingsWidget::importSettings()
{
// Import roots from file
QSettings settings;
QString dir = settings.value("settingsdir", QStandardPaths::standardLocations(QStandardPaths::DocumentsLocation)).toString();
QString file = QFileDialog::getOpenFileName(this, tr("Import settings"), dir, tr("Ini-File (*.ini)"));
if (!file.isEmpty()) {
settings.setValue("settingsdir", dir);
emit importSettingsFrom(file);
}
}

void SettingsWidget::openRootContextMenu()
{
// Open root context menu
Expand Down Expand Up @@ -240,39 +275,3 @@ void SettingsWidget::on_settingsChanged()
emit paramsChanged();
}
}

void SettingsWidget::on_btnExportImageClicked()
{
// Export pixmap to file
QSettings settings;
QString dir = settings.value("imagedir", QStandardPaths::standardLocations(QStandardPaths::PicturesLocation)).toString();
dir = QFileDialog::getExistingDirectory(this, tr("Export fractal to"), dir);
if (!dir.isEmpty()) {
settings.setValue("imagedir", dir);
emit exportImage(dir);
}
}

void SettingsWidget::on_btnExportSettingsClicked()
{
// Export roots to file
QSettings settings;
QString dir = settings.value("settingsdir", QStandardPaths::standardLocations(QStandardPaths::DocumentsLocation)).toString();
dir = QFileDialog::getExistingDirectory(this, tr("Export settings to"), dir);
if (!dir.isEmpty()) {
settings.setValue("settingsdir", dir);
emit exportRoots(dir);
}
}

void SettingsWidget::on_btnImportSettingsClicked()
{
// Import roots from file
QSettings settings;
QString dir = settings.value("settingsdir", QStandardPaths::standardLocations(QStandardPaths::DocumentsLocation)).toString();
QString file = QFileDialog::getOpenFileName(this, tr("Import settings"), dir, tr("Ini-File (*.ini)"));
if (!file.isEmpty()) {
settings.setValue("settingsdir", dir);
emit importRoots(file);
}
}
12 changes: 6 additions & 6 deletions src/settingswidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ public slots:
void addRoot(complex value = complex(0, 0), QColor color = Qt::black);
void removeRoot(qint8 index = -1);
void moveRoot(quint8 index, complex value);
void exportImage();
void exportSettings();
void importSettings();

private slots:
void openRootContextMenu();
void on_settingsChanged();
void on_btnExportImageClicked();
void on_btnExportSettingsClicked();
void on_btnImportSettingsClicked();

protected:
// Don't pass any events to parent while settings are shown
Expand All @@ -55,9 +55,9 @@ private slots:
signals:
void paramsChanged();
void sizeChanged(QSize size);
void exportImage(const QString &dir);
void exportRoots(const QString &dir);
void importRoots(const QString &file);
void exportImageTo(const QString &dir);
void exportSettingsTo(const QString &dir);
void importSettingsFrom(const QString &file);
void reset();

private:
Expand Down
8 changes: 4 additions & 4 deletions src/settingswidget.ui
Original file line number Diff line number Diff line change
Expand Up @@ -624,7 +624,7 @@
</size>
</property>
<property name="toolTip">
<string>import roots from file</string>
<string>import roots from file [Ctrl+I]</string>
</property>
<property name="text">
<string/>
Expand Down Expand Up @@ -659,7 +659,7 @@
</size>
</property>
<property name="toolTip">
<string>export roots to file</string>
<string>export roots to file [Ctrl+E]</string>
</property>
<property name="text">
<string/>
Expand Down Expand Up @@ -694,7 +694,7 @@
</size>
</property>
<property name="toolTip">
<string>change to default zoom and equidistant roots</string>
<string>change to default zoom and equidistant roots [Ctrl+R]</string>
</property>
<property name="text">
<string/>
Expand Down Expand Up @@ -729,7 +729,7 @@
</size>
</property>
<property name="toolTip">
<string>export fractal as png</string>
<string>export fractal as png [Ctrl+S]</string>
</property>
<property name="text">
<string/>
Expand Down

0 comments on commit df53d92

Please sign in to comment.