Commit 7769905d authored by Anthony Fieroni's avatar Anthony Fieroni

Provide a dialog helper to handle its destruction

Differential Revision: https://phabricator.kde.org/D9731Signed-off-by: Anthony Fieroni's avatarAnthony Fieroni <bvbfan@abv.bg>
parent 338ffeee
......@@ -20,6 +20,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Cambridge, MA 02110-1301, USA
*/
// Self
#include "fileoperations.h"
#include "dialogguard.h"
// Qt
#include <QMenu>
......@@ -52,54 +53,54 @@ static void copyMoveOrLink(Operation operation, const QList<QUrl>& urlList, QWid
Q_ASSERT(!urlList.isEmpty());
const int numberOfImages = urlList.count();
QFileDialog dialog(parent->nativeParentWidget(), QString());
dialog.setAcceptMode(QFileDialog::AcceptSave);
DialogGuard<QFileDialog> dialog(parent->nativeParentWidget(), QString());
dialog->setAcceptMode(QFileDialog::AcceptSave);
// Figure out what the window title and buttons should say,
// depending on the operation and how many images are selected
switch (operation) {
case COPY:
if (numberOfImages == 1) {
dialog.setWindowTitle(i18nc("@title:window %1 file name", "Copy %1", urlList.constFirst().fileName()));
dialog->setWindowTitle(i18nc("@title:window %1 file name", "Copy %1", urlList.constFirst().fileName()));
} else {
dialog.setWindowTitle(i18ncp("@title:window %1 number of images", "Copy %1 image", "Copy %1 images", numberOfImages));
dialog->setWindowTitle(i18ncp("@title:window %1 number of images", "Copy %1 image", "Copy %1 images", numberOfImages));
}
dialog.setLabelText(QFileDialog::DialogLabel::Accept, i18nc("@action:button", "Copy"));
dialog->setLabelText(QFileDialog::DialogLabel::Accept, i18nc("@action:button", "Copy"));
break;
case MOVE:
if (numberOfImages == 1) {
dialog.setWindowTitle(i18nc("@title:window %1 file name", "Move %1", urlList.constFirst().fileName()));
dialog->setWindowTitle(i18nc("@title:window %1 file name", "Move %1", urlList.constFirst().fileName()));
} else {
dialog.setWindowTitle(i18ncp("@title:window %1 number of images", "Move %1 image", "Move %1 images", numberOfImages));
dialog->setWindowTitle(i18ncp("@title:window %1 number of images", "Move %1 image", "Move %1 images", numberOfImages));
}
dialog.setLabelText(QFileDialog::DialogLabel::Accept, i18nc("@action:button", "Move"));
dialog->setLabelText(QFileDialog::DialogLabel::Accept, i18nc("@action:button", "Move"));
break;
case LINK:
if (numberOfImages == 1) {
dialog.setWindowTitle(i18nc("@title:window %1 file name", "Link %1", urlList.constFirst().fileName()));
dialog->setWindowTitle(i18nc("@title:window %1 file name", "Link %1", urlList.constFirst().fileName()));
} else {
dialog.setWindowTitle(i18ncp("@title:window %1 number of images", "Link %1 image", "Link %1 images", numberOfImages));
dialog->setWindowTitle(i18ncp("@title:window %1 number of images", "Link %1 image", "Link %1 images", numberOfImages));
}
dialog.setLabelText(QFileDialog::DialogLabel::Accept, i18nc("@action:button", "Link"));
dialog->setLabelText(QFileDialog::DialogLabel::Accept, i18nc("@action:button", "Link"));
break;
default:
Q_ASSERT(0);
}
if (numberOfImages == 1) {
dialog.setFileMode(QFileDialog::AnyFile);
dialog.selectUrl(urlList.constFirst());
dialog->setFileMode(QFileDialog::AnyFile);
dialog->selectUrl(urlList.constFirst());
} else {
dialog.setFileMode(QFileDialog::Directory);
dialog.setOption(QFileDialog::ShowDirsOnly, true);
dialog->setFileMode(QFileDialog::Directory);
dialog->setOption(QFileDialog::ShowDirsOnly, true);
}
dialog.setDirectoryUrl(contextManager->targetDirUrl());
if (!dialog.exec()) {
dialog->setDirectoryUrl(contextManager->targetDirUrl());
if (!dialog->exec()) {
return;
}
QUrl destUrl = dialog.selectedUrls().first();
QUrl destUrl = dialog->selectedUrls().first();
KIO::CopyJob* job = 0;
switch (operation) {
......@@ -222,7 +223,7 @@ void showMenuForDroppedUrls(QWidget* parent, const QList<QUrl>& urlList, const Q
void rename(const QUrl &oldUrl, QWidget* parent)
{
QString name = QInputDialog::getText(parent,
QString name = QInputDialog::getText(parent,
i18nc("@title:window", "Rename") /* caption */,
xi18n("Rename <filename>%1</filename> to:", oldUrl.fileName()) /* label */,
QLineEdit::Normal, oldUrl.fileName() /* value */
......
......@@ -20,6 +20,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
// Self
#include "fileopscontextmanageritem.h"
#include "dialogguard.h"
// Qt
#include <QAction>
......@@ -389,16 +390,16 @@ void FileOpsContextManagerItem::openWith(QAction* action)
GV_RETURN_IF_FAIL(ok);
if (idx == -1) {
// Other Application...
KOpenWithDialog dlg(list, mGroup);
if (!dlg.exec()) {
DialogGuard<KOpenWithDialog> dlg(list, mGroup);
if (!dlg->exec()) {
return;
}
service = dlg.service();
service = dlg->service();
if (!service) {
// User entered a custom command
Q_ASSERT(!dlg.text().isEmpty());
KRun::run(dlg.text(), list, mGroup);
Q_ASSERT(!dlg->text().isEmpty());
KRun::run(dlg->text(), list, mGroup);
return;
}
} else {
......
......@@ -20,6 +20,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Cambridge, MA 02110-1301, USA
*/
// Self
#include "gvcore.h"
#include "dialogguard.h"
// Qt
#include <QApplication>
......@@ -68,26 +69,26 @@ struct GvCorePrivate
bool showSaveAsDialog(const QUrl &url, QUrl* outUrl, QByteArray* format)
{
QFileDialog dialog(mMainWindow);
dialog.setAcceptMode(QFileDialog::AcceptSave);
dialog.setWindowTitle(i18nc("@title:window", "Save Image"));
dialog.selectUrl(url);
DialogGuard<QFileDialog> dialog(mMainWindow);
dialog->setAcceptMode(QFileDialog::AcceptSave);
dialog->setWindowTitle(i18nc("@title:window", "Save Image"));
dialog->selectUrl(url);
QStringList supportedMimetypes;
for (const QByteArray &mimeName : QImageWriter::supportedMimeTypes()) {
supportedMimetypes.append(QString::fromLocal8Bit(mimeName));
}
dialog.setMimeTypeFilters(supportedMimetypes);
dialog.selectMimeTypeFilter(MimeTypeUtils::urlMimeType(url));
dialog->setMimeTypeFilters(supportedMimetypes);
dialog->selectMimeTypeFilter(MimeTypeUtils::urlMimeType(url));
// Show dialog
do {
if (!dialog.exec()) {
if (!dialog->exec()) {
return false;
}
QList<QUrl> files = dialog.selectedUrls();
QList<QUrl> files = dialog->selectedUrls();
if (files.isEmpty()) {
return false;
}
......@@ -106,7 +107,7 @@ struct GvCorePrivate
);
} while (true);
*outUrl = dialog.selectedUrls().first();
*outUrl = dialog->selectedUrls().first();
return true;
}
......
......@@ -20,6 +20,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
// Self
#include "imageopscontextmanageritem.h"
#include "dialogguard.h"
// Qt
#include <QApplication>
......@@ -236,12 +237,12 @@ void ImageOpsContextManagerItem::resizeImage()
}
Document::Ptr doc = DocumentFactory::instance()->load(contextManager()->currentUrl());
doc->startLoadingFullImage();
ResizeImageDialog dialog(d->mMainWindow);
dialog.setOriginalSize(doc->size());
if (!dialog.exec()) {
DialogGuard<ResizeImageDialog> dialog(d->mMainWindow);
dialog->setOriginalSize(doc->size());
if (!dialog->exec()) {
return;
}
ResizeImageOperation* op = new ResizeImageOperation(dialog.size());
ResizeImageOperation* op = new ResizeImageOperation(dialog->size());
applyImageOperation(op);
}
......
......@@ -19,6 +19,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "mainwindow.h"
#include <config-gwenview.h>
#include "dialogguard.h"
// Qt
#include <QApplication>
......@@ -1337,18 +1338,18 @@ void MainWindow::openFile()
{
QUrl dirUrl = d->mContextManager->currentDirUrl();
QFileDialog dialog(this);
dialog.selectUrl(dirUrl);
dialog.setWindowTitle(i18nc("@title:window", "Open Image"));
DialogGuard<QFileDialog> dialog(this);
dialog->selectUrl(dirUrl);
dialog->setWindowTitle(i18nc("@title:window", "Open Image"));
const QStringList mimeFilter = MimeTypeUtils::imageMimeTypes();
dialog.setMimeTypeFilters(mimeFilter);
dialog.setAcceptMode(QFileDialog::AcceptOpen);
if (!dialog.exec()) {
dialog->setMimeTypeFilters(mimeFilter);
dialog->setAcceptMode(QFileDialog::AcceptOpen);
if (!dialog->exec()) {
return;
}
if (!dialog.selectedUrls().isEmpty()) {
openUrl(dialog.selectedUrls().first());
if (!dialog->selectedUrls().isEmpty()) {
openUrl(dialog->selectedUrls().first());
}
}
......@@ -1446,9 +1447,9 @@ bool MainWindow::queryClose()
void MainWindow::showConfigDialog()
{
ConfigDialog dialog(this);
connect(&dialog, SIGNAL(settingsChanged(QString)), SLOT(loadConfig()));
dialog.exec();
DialogGuard<ConfigDialog> dialog(this);
connect(dialog.data(), SIGNAL(settingsChanged(QString)), SLOT(loadConfig()));
dialog->exec();
}
void MainWindow::toggleMenuBar()
......
......@@ -20,6 +20,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Cambridge, MA 02110-1301, USA
*/
// Self
#include "thumbnailpage.h"
#include "dialogguard.h"
// Qt
#include <QDebug>
......@@ -346,8 +347,8 @@ void ThumbnailPage::updateImportButtons()
void ThumbnailPage::showConfigDialog()
{
ImporterConfigDialog dialog(this);
dialog.exec();
DialogGuard<ImporterConfigDialog> dialog(this);
dialog->exec();
}
/**
......
/*
Gwenview: an image viewer
Copyright 2018 Anthony Fieroni <bvbfan@abv.bg>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Cambridge, MA 02110-1301, USA.
*/
#ifndef DIALOGGUARD_H
#define DIALOGGUARD_H
// std
#include <utility>
// Qt
#include <QPointer>
template<class T>
class DialogGuard
{
QPointer<T> m_dialog;
public:
template<class ...Args>
DialogGuard(Args&& ...args)
{
m_dialog = new T(std::forward<Args>(args)...);
}
~DialogGuard()
{
delete m_dialog;
}
T* data() const
{
return m_dialog.data();
}
T* operator->() const
{
return m_dialog.data();
}
operator bool() const
{
return !m_dialog.isNull();
}
};
#endif // DIALOGGUARD_H
......@@ -20,6 +20,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
// Self
#include "printhelper.h"
#include "dialogguard.h"
// STD
#include <memory>
......@@ -122,7 +123,7 @@ void PrintHelper::print(Document::Ptr doc)
PrintOptionsPage* optionsPage = new PrintOptionsPage(doc->size());
optionsPage->loadConfig();
QScopedPointer<QPrintDialog> dialog(new QPrintDialog(&printer, d->mParent));
DialogGuard<QPrintDialog> dialog(&printer, d->mParent);
#if defined (Q_OS_UNIX) && !defined(Q_OS_DARWIN)
dialog->setOptionTabs(QList<QWidget*>() << optionsPage);
#else
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment