Skip to content

Commit

Permalink
Merge pull request #190 from friction2d/svg-quick-preview
Browse files Browse the repository at this point in the history
SVG: support quick preview
  • Loading branch information
rodlie authored Jun 16, 2024
2 parents 252ff9e + 103d61e commit b321e02
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 27 deletions.
20 changes: 16 additions & 4 deletions src/app/GUI/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,12 @@ void MainWindow::setupMenuBar()
tr("Save Backup", "MenuBar_File"),
this, &MainWindow::saveBackup);

const auto previewSvgAct = mFileMenu->addAction(QIcon::fromTheme("seq_preview"),
tr("Preview SVG", "MenuBar_File"),
this,[this]{ exportSVG(true); },
QKeySequence(AppSupport::getSettings("shortcuts",
"previewSVG",
"Ctrl+F12").toString()));
const auto exportSvgAct = mFileMenu->addAction(QIcon::fromTheme("seq_preview"),
tr("Export SVG", "MenuBar_File"),
this, &MainWindow::exportSVG,
Expand All @@ -519,7 +525,7 @@ void MainWindow::setupMenuBar()
saveToolBtn->setDefaultAction(mSaveAct);
saveToolMenu->addAction(saveAsAct);
saveToolMenu->addAction(saveBackAct);
saveToolMenu->addAction(exportSvgAct);
//saveToolMenu->addAction(exportSvgAct);
saveToolMenu->addSeparator();

mFileMenu->addSeparator();
Expand Down Expand Up @@ -1146,6 +1152,7 @@ void MainWindow::setupMenuBar()
tr("Render"),
this, &MainWindow::openRendererWindow);

mToolbar->addAction(previewSvgAct);
mToolbar->addAction(exportSvgAct);

setMenuBar(mMenuBar);
Expand Down Expand Up @@ -1813,11 +1820,16 @@ const QString MainWindow::checkBeforeExportSVG()
return result.join("");
}

void MainWindow::exportSVG()
void MainWindow::exportSVG(const bool &preview)
{
const auto dialog = new ExportSvgDialog(this, checkBeforeExportSVG());
dialog->show();
const auto dialog = new ExportSvgDialog(this,
preview ? QString() : checkBeforeExportSVG());
dialog->setAttribute(Qt::WA_DeleteOnClose);
if (!preview) {
dialog->show();
} else {
dialog->showPreview(true /* close when done */);
}
}

void MainWindow::updateLastOpenDir(const QString &path)
Expand Down
2 changes: 1 addition & 1 deletion src/app/GUI/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ class MainWindow : public QMainWindow
void saveFileAs(const bool setPath = true);
void saveBackup();
const QString checkBeforeExportSVG();
void exportSVG();
void exportSVG(const bool &preview = false);
void updateLastOpenDir(const QString &path);
void updateLastSaveDir(const QString &path);
const QString getLastOpenDir();
Expand Down
74 changes: 52 additions & 22 deletions src/ui/dialogs/exportsvgdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,36 @@ ExportSvgDialog::ExportSvgDialog(QWidget* const parent,
mLastFrame->setValue(maxFrame);

mBackground = new QCheckBox(tr("Background"), this);
mBackground->setChecked(true);
mBackground->setChecked(AppSupport::getSettings("exportSVG",
"background",
true).toBool());
mFixedSize = new QCheckBox(tr("Fixed Size"), this);
mFixedSize->setChecked(false);
mFixedSize->setChecked(AppSupport::getSettings("exportSVG",
"fixed",
false).toBool());
mLoop = new QCheckBox(tr("Loop"), this);
mLoop->setChecked(true);
mLoop->setChecked(AppSupport::getSettings("exportSVG",
"loop",
true).toBool());

connect(mBackground, &QCheckBox::stateChanged,
this, [this] {
AppSupport::setSettings("exportSVG",
"background",
mBackground->isChecked());
});
connect(mFixedSize, &QCheckBox::stateChanged,
this, [this] {
AppSupport::setSettings("exportSVG",
"fixed",
mFixedSize->isChecked());
});
connect(mLoop, &QCheckBox::stateChanged,
this, [this] {
AppSupport::setSettings("exportSVG",
"loop",
mLoop->isChecked());
});

twoColLayout->addPair(new QLabel(tr("Scene:")), sceneButton);
twoColLayout->addPair(new QLabel(tr("First Frame:")), mFirstFrame);
Expand Down Expand Up @@ -187,25 +212,8 @@ ExportSvgDialog::ExportSvgDialog(QWidget* const parent,
mPreviewButton->setIcon(QIcon::fromTheme("seq_preview"));
mPreviewButton->setObjectName("SVGPreviewButton");
buttons->addButton(mPreviewButton, QDialogButtonBox::ActionRole);
connect(mPreviewButton, &QPushButton::released, this, [this]() {
if (!mPreviewFile) {
const QString templ = QString::fromUtf8("%1/%2_svg_preview_XXXXXX.html").arg(QDir::tempPath(),
AppSupport::getAppName());
mPreviewFile = qsptr<QTemporaryFile>::create(templ);
mPreviewFile->open();
mPreviewFile->close();
}
const auto task = exportTo(mPreviewFile->fileName(), true);
if (!task) { return; }
QPointer<ExportSvgDialog> ptr = this;
task->addDependent(
{[ptr]() {
if (ptr) {
const auto fileName = ptr->mPreviewFile->fileName();
QDesktopServices::openUrl(QUrl::fromLocalFile(fileName));
}
}, nullptr});
});
connect(mPreviewButton, &QPushButton::released,
this, [this] { showPreview(false); });

const auto settingsWidget = new QWidget(this);
settingsWidget->setLayout(settingsLayout);
Expand All @@ -218,6 +226,28 @@ ExportSvgDialog::ExportSvgDialog(QWidget* const parent,
setLayout(mainLayout);
}

void ExportSvgDialog::showPreview(const bool &closeWhenDone)
{
if (!mPreviewFile) {
const QString templ = QString::fromUtf8("%1/%2_svg_preview_XXXXXX.html").arg(QDir::tempPath(),
AppSupport::getAppName());
mPreviewFile = qsptr<QTemporaryFile>::create(templ);
mPreviewFile->setAutoRemove(false);
mPreviewFile->open();
mPreviewFile->close();
}
const auto fileName = mPreviewFile->fileName();
const auto task = exportTo(fileName, true);
if (!task) {
if (closeWhenDone) { close(); }
return;
}
task->addDependent({[fileName, this, closeWhenDone]() {
QDesktopServices::openUrl(QUrl::fromLocalFile(fileName));
if (closeWhenDone) { close(); }
}, nullptr});
}

ComplexTask* ExportSvgDialog::exportTo(const QString& file,
bool preview)
{
Expand Down
1 change: 1 addition & 0 deletions src/ui/dialogs/exportsvgdialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class UI_EXPORT ExportSvgDialog : public QDialog
public:
ExportSvgDialog(QWidget* const parent = nullptr,
const QString &warnings = QString());
void showPreview(const bool &closeWhenDone = false);

private:
ComplexTask* exportTo(const QString& file,
Expand Down

0 comments on commit b321e02

Please sign in to comment.