diff --git a/src/ExportManager.h b/src/ExportManager.h --- a/src/ExportManager.h +++ b/src/ExportManager.h @@ -67,6 +67,7 @@ QString windowTitle() const; ImageGrabber::GrabMode grabMode() const; void setGrabMode(const ImageGrabber::GrabMode &grabMode); + QString formatFilename(const QString &nameTemplate); static const QMap filenamePlaceholders; diff --git a/src/ExportManager.cpp b/src/ExportManager.cpp --- a/src/ExportManager.cpp +++ b/src/ExportManager.cpp @@ -157,9 +157,14 @@ } QString ExportManager::makeAutosaveFilename() +{ + return formatFilename(SpectacleConfig::instance()->autoSaveFilenameFormat()); +} + +QString ExportManager::formatFilename(const QString &nameTemplate) { const QDateTime timestamp = mPixmapTimestamp; - QString baseName = SpectacleConfig::instance()->autoSaveFilenameFormat(); + QString baseName = nameTemplate; const QString baseDir = defaultSaveLocation(); QString title; diff --git a/src/Gui/SettingsDialog/SaveOptionsPage.h b/src/Gui/SettingsDialog/SaveOptionsPage.h --- a/src/Gui/SettingsDialog/SaveOptionsPage.h +++ b/src/Gui/SettingsDialog/SaveOptionsPage.h @@ -28,6 +28,7 @@ class KUrlRequester; class QCheckBox; class QSlider; +class QLabel; class SaveOptionsPage : public SettingsPage { @@ -48,11 +49,14 @@ private: + void updateFilenamePreview(); + QLineEdit *mSaveNameFormat; KUrlRequester *mUrlRequester; QComboBox *mSaveImageFormat; QCheckBox *mCopyPathToClipboard; QSlider *mQualitySlider; + QLabel *mPreviewLabel; }; #endif // SAVEOPTIONSPAGE_H diff --git a/src/Gui/SettingsDialog/SaveOptionsPage.cpp b/src/Gui/SettingsDialog/SaveOptionsPage.cpp --- a/src/Gui/SettingsDialog/SaveOptionsPage.cpp +++ b/src/Gui/SettingsDialog/SaveOptionsPage.cpp @@ -89,9 +89,6 @@ mainLayout->addItem(new QSpacerItem(0, 18, QSizePolicy::Fixed, QSizePolicy::Fixed)); - // filename chooser and instructional text - QVBoxLayout *saveNameLayout = new QVBoxLayout; - // filename chooser text field QHBoxLayout *saveFieldLayout = new QHBoxLayout; mSaveNameFormat = new QLineEdit; @@ -108,6 +105,7 @@ } } }); + connect(mSaveNameFormat, &QLineEdit::textChanged,this, &SaveOptionsPage::updateFilenamePreview); mSaveNameFormat->setPlaceholderText(QStringLiteral("%d")); saveFieldLayout->addWidget(mSaveNameFormat); @@ -120,9 +118,12 @@ return items; }()); connect(mSaveImageFormat, &QComboBox::currentTextChanged, this, &SaveOptionsPage::markDirty); + connect(mSaveImageFormat, &QComboBox::currentTextChanged, this, &SaveOptionsPage::updateFilenamePreview); saveFieldLayout->addWidget(mSaveImageFormat); - saveNameLayout->addLayout(saveFieldLayout); + mainLayout->addRow(i18n("Filename:"), saveFieldLayout); + mPreviewLabel = new QLabel(this); + mainLayout->addRow(i18nc("Preview of the user configured filename", "Preview:"), mPreviewLabel); // now the save filename format layout QString helpText = i18n( "You can use the following placeholders in the filename, which will be replaced " @@ -141,8 +142,7 @@ connect(fmtHelpText, &QLabel::linkActivated, [this](const QString& placeholder) { mSaveNameFormat->insert(placeholder); }); - saveNameLayout->addWidget(fmtHelpText); - mainLayout->addRow(i18n("Filename:"), saveNameLayout); + mainLayout->addWidget(fmtHelpText); // read in the data resetChanges(); @@ -198,3 +198,23 @@ mChangesMade = false; } + +void SaveOptionsPage::updateFilenamePreview() +{ + ExportManager *exportManager = ExportManager::instance(); + exportManager->setWindowTitle(QStringLiteral("Spectacle")); + using GrabMode = ImageGrabber::GrabMode; + GrabMode oldMode = exportManager->grabMode(); + /* If the grabMode is not one of those below we need to change it to have the placeholder + * replaced by the window title */ + bool changeAndRestoreGrabMode = !(oldMode == GrabMode::ActiveWindow + || oldMode == GrabMode::TransientWithParent || oldMode == GrabMode::WindowUnderCursor); + if (changeAndRestoreGrabMode) { + exportManager->setGrabMode(GrabMode::ActiveWindow); + } + const QString filename = exportManager->formatFilename(mSaveNameFormat->text()); + mPreviewLabel->setText(xi18nc("@info", "%1.%2", filename, mSaveImageFormat->currentText().toLower())); + if (changeAndRestoreGrabMode) { + exportManager->setGrabMode(oldMode); + } +}