Commit ea2d7440 authored by David Faure's avatar David Faure
Browse files

Port from KFileDialog to QFileDialog.

Summary:
Tricky because of the embedded widget for save options.
Reused the solution found in kuickshow.

Since QFileDialog implements overwrite checking, kill all
the custom code for doing the same.

Test Plan: Save as (new file), save as (existing file)

Reviewers: broulik, andreagenor, tcanabrava

Reviewed By: tcanabrava

Maniphest Tasks: T11576

Differential Revision: https://phabricator.kde.org/D25482
parent bed17657
......@@ -111,13 +111,11 @@ public:
const QUrl &url,
const kpDocumentSaveOptions &saveOptions,
const kpDocumentMetaInfo &metaInfo,
bool overwritePrompt,
bool lossyPrompt,
QWidget *parent);
bool save (bool overwritePrompt = false, bool lossyPrompt = false);
bool save (bool lossyPrompt = false);
bool saveAs (const QUrl &url,
const kpDocumentSaveOptions &saveOptions,
bool overwritePrompt = true,
bool lossyPrompt = true);
......
......@@ -157,6 +157,7 @@ QString kpDocumentSaveOptions::mimeType () const
// public
void kpDocumentSaveOptions::setMimeType (const QString &mimeType)
{
Q_ASSERT(mimeType.isEmpty () || mimeType.contains ('/'));
d->m_mimeType = mimeType;
}
......
......@@ -66,11 +66,10 @@
#include "views/manager/kpViewManager.h"
bool kpDocument::save (bool overwritePrompt, bool lossyPrompt)
bool kpDocument::save (bool lossyPrompt)
{
#if DEBUG_KP_DOCUMENT
qCDebug(kpLogDocument) << "kpDocument::save("
<< "overwritePrompt=" << overwritePrompt
<< ",lossyPrompt=" << lossyPrompt
<< ") url=" << m_url
<< " savedAtLeastOnceBefore=" << savedAtLeastOnceBefore ();
......@@ -92,7 +91,6 @@ bool kpDocument::save (bool overwritePrompt, bool lossyPrompt)
}
return saveAs (m_url, *m_saveOptions,
overwritePrompt,
lossyPrompt);
}
......@@ -303,7 +301,6 @@ bool kpDocument::savePixmapToFile (const QImage &pixmap,
const QUrl &url,
const kpDocumentSaveOptions &saveOptions,
const kpDocumentMetaInfo &metaInfo,
bool overwritePrompt,
bool lossyPrompt,
QWidget *parent)
{
......@@ -313,39 +310,12 @@ bool kpDocument::savePixmapToFile (const QImage &pixmap,
#if DEBUG_KP_DOCUMENT
qCDebug(kpLogDocument) << "kpDocument::savePixmapToFile ("
<< url
<< ",overwritePrompt=" << overwritePrompt
<< ",lossyPrompt=" << lossyPrompt
<< ")";
saveOptions.printDebug (QLatin1String ("\tsaveOptions"));
metaInfo.printDebug (QLatin1String ("\tmetaInfo"));
#endif
if (overwritePrompt)
{
// A probably better solution would be to do file_copy without Overwrite,
// and on "already exists" error, prompt and redo file_copy with Overwrite.
KIO::StatJob *job = KIO::stat (url, KIO::StatJob::DestinationSide/*write*/, 0);
KJobWidgets::setWindow (job, parent);
if (job->exec ()) {
int result = KMessageBox::warningContinueCancel (parent,
i18n ("A document called \"%1\" already exists.\n"
"Do you want to overwrite it?",
kpUrlFormatter::PrettyFilename (url)),
QString(),
KStandardGuiItem::overwrite ());
if (result != KMessageBox::Continue)
{
#if DEBUG_KP_DOCUMENT
qCDebug(kpLogDocument) << "\tuser doesn't want to overwrite";
#endif
return false;
}
}
}
if (lossyPrompt && !lossyPromptContinue (pixmap, saveOptions, parent))
{
#if DEBUG_KP_DOCUMENT
......@@ -482,7 +452,6 @@ bool kpDocument::savePixmapToFile (const QImage &pixmap,
bool kpDocument::saveAs (const QUrl &url,
const kpDocumentSaveOptions &saveOptions,
bool overwritePrompt,
bool lossyPrompt)
{
#if DEBUG_KP_DOCUMENT
......@@ -493,7 +462,6 @@ bool kpDocument::saveAs (const QUrl &url,
if (kpDocument::savePixmapToFile (imageWithSelection (),
url,
saveOptions, *metaInfo (),
overwritePrompt,
lossyPrompt,
d->environ->dialogParent ()))
{
......
......@@ -354,7 +354,6 @@ private:
bool localOnly,
kpDocumentSaveOptions *chosenSaveOptions,
bool isSavingForFirstTime,
bool *allowOverwritePrompt,
bool *allowLossyPrompt);
private slots:
......
......@@ -854,7 +854,7 @@ void kpMainWindow::slotCopyToFile ()
kpDocumentSaveOptions chosenSaveOptions;
bool allowOverwritePrompt, allowLossyPrompt;
bool allowLossyPrompt;
QUrl chosenURL = askForSaveURL (i18nc ("@title:window", "Copy to File"),
d->lastCopyToURL.url (),
imageToSave,
......@@ -864,7 +864,6 @@ void kpMainWindow::slotCopyToFile ()
false/*allow remote files*/,
&chosenSaveOptions,
d->copyToFirstTime,
&allowOverwritePrompt,
&allowLossyPrompt);
if (chosenURL.isEmpty ()) {
......@@ -875,7 +874,6 @@ void kpMainWindow::slotCopyToFile ()
if (!kpDocument::savePixmapToFile (imageToSave,
chosenURL,
chosenSaveOptions, kpDocumentMetaInfo (),
allowOverwritePrompt,
allowLossyPrompt,
this))
{
......
......@@ -56,7 +56,6 @@
#include <KSharedConfig>
#include <kconfiggroup.h>
#include <KPluralHandlingSpinBox>
#include <kfiledialog.h> // kdelibs4support
#include <kmessagebox.h>
#include <krecentfilesaction.h>
#include <kstandardshortcut.h>
......@@ -746,8 +745,7 @@ bool kpMainWindow::save (bool localOnly)
return saveAs (localOnly);
}
if (d->document->save (false/*no overwrite prompt*/,
!d->document->savedAtLeastOnceBefore ()/*lossy prompt*/))
if (d->document->save (!d->document->savedAtLeastOnceBefore ()/*lossy prompt*/))
{
addRecentURL (d->document->url ());
return true;
......@@ -778,7 +776,6 @@ QUrl kpMainWindow::askForSaveURL (const QString &caption,
bool localOnly,
kpDocumentSaveOptions *chosenSaveOptions,
bool isSavingForFirstTime,
bool *allowOverwritePrompt,
bool *allowLossyPrompt)
{
#if DEBUG_KP_MAIN_WINDOW
......@@ -805,10 +802,6 @@ QUrl kpMainWindow::askForSaveURL (const QString &caption,
*chosenSaveOptions = kpDocumentSaveOptions ();
}
if (allowOverwritePrompt) {
*allowOverwritePrompt = true; // play it safe for now
}
if (allowLossyPrompt) {
*allowLossyPrompt = true; // play it safe for now
}
......@@ -889,18 +882,27 @@ QUrl kpMainWindow::askForSaveURL (const QString &caption,
docMetaInfo,
this);
KFileDialog fd (QUrl (startURL), QString(), this,
saveOptionsWidget);
saveOptionsWidget->setVisualParent (&fd);
QFileDialog fd(this);
fd.setAcceptMode (QFileDialog::AcceptSave);
fd.setOption (QFileDialog::DontUseNativeDialog);
fd.setDirectoryUrl (QUrl (startURL));
fd.setWindowTitle (caption);
fd.setOperationMode (KFileDialog::Saving);
fd.setMimeFilter (mimeTypes, fdSaveOptions.mimeType ());
fd.setMimeTypeFilters (mimeTypes);
fd.selectMimeTypeFilter (fdSaveOptions.mimeType ());
if (localOnly) {
fd.setMode (KFile::File | KFile::LocalOnly);
fd.setSupportedSchemes ({QStringLiteral("file")});
}
// insert the checkbox below the filter box
if (QGridLayout* gl = qobject_cast<QGridLayout*>(fd.layout ())) {
gl->addWidget (saveOptionsWidget, gl->rowCount (), 0, 1, gl->columnCount ());
}
saveOptionsWidget->setVisualParent (&fd);
connect (&fd, &KFileDialog::filterChanged,
saveOptionsWidget, &kpDocumentSaveOptionsWidget::setMimeType);
connect (&fd, &QFileDialog::filterSelected,
this, [saveOptionsWidget, &fd]() {
saveOptionsWidget->setMimeType(fd.selectedMimeTypeFilter());
});
if ( fd.exec() == QDialog::Accepted )
{
......@@ -921,17 +923,11 @@ QUrl kpMainWindow::askForSaveURL (const QString &caption,
*chosenSaveOptions = newSaveOptions;
}
bool shouldAllowOverwritePrompt =
(fd.selectedUrl () != QUrl (startURL) ||
newSaveOptions.mimeType () != startSaveOptions.mimeType ());
if (allowOverwritePrompt)
{
*allowOverwritePrompt = shouldAllowOverwritePrompt;
#if DEBUG_KP_MAIN_WINDOW
qCDebug(kpLogMainWindow) << "\tallowOverwritePrompt=" << *allowOverwritePrompt;
#endif
const QList<QUrl> selectedUrls = fd.selectedUrls ();
if (selectedUrls.isEmpty()) { // shouldn't happen
return {};
}
const QUrl selectedUrl = selectedUrls.at(0);
if (allowLossyPrompt)
{
......@@ -940,7 +936,7 @@ QUrl kpMainWindow::askForSaveURL (const QString &caption,
// need to continually warn due to quality change)
*allowLossyPrompt =
(isSavingForFirstTime ||
shouldAllowOverwritePrompt ||
selectedUrl != QUrl (startURL) ||
newSaveOptions.mimeType () != startSaveOptions.mimeType () ||
newSaveOptions.colorDepth () != startSaveOptions.colorDepth () ||
newSaveOptions.dither () != startSaveOptions.dither ());
......@@ -951,9 +947,9 @@ QUrl kpMainWindow::askForSaveURL (const QString &caption,
#if DEBUG_KP_MAIN_WINDOW
qCDebug(kpLogMainWindow) << "\tselectedUrl=" << fd.selectedUrl ();
qCDebug(kpLogMainWindow) << "\tselectedUrl=" << selectedUrl;
#endif
return fd.selectedUrl ();
return selectedUrl;
}
return {};
......@@ -966,7 +962,7 @@ QUrl kpMainWindow::askForSaveURL (const QString &caption,
bool kpMainWindow::saveAs (bool localOnly)
{
kpDocumentSaveOptions chosenSaveOptions;
bool allowOverwritePrompt, allowLossyPrompt;
bool allowLossyPrompt;
QUrl chosenURL = askForSaveURL (i18nc ("@title:window", "Save Image As"),
d->document->url ().url (),
d->document->imageWithSelection (),
......@@ -976,7 +972,6 @@ bool kpMainWindow::saveAs (bool localOnly)
localOnly,
&chosenSaveOptions,
!d->document->savedAtLeastOnceBefore (),
&allowOverwritePrompt,
&allowLossyPrompt);
......@@ -986,7 +981,6 @@ bool kpMainWindow::saveAs (bool localOnly)
if (!d->document->saveAs (chosenURL, chosenSaveOptions,
allowOverwritePrompt,
allowLossyPrompt))
{
return false;
......@@ -1016,7 +1010,7 @@ bool kpMainWindow::slotExport ()
toolEndShape ();
kpDocumentSaveOptions chosenSaveOptions;
bool allowOverwritePrompt, allowLossyPrompt;
bool allowLossyPrompt;
QUrl chosenURL = askForSaveURL (i18nc ("@title:window", "Export"),
d->lastExportURL.url (),
d->document->imageWithSelection (),
......@@ -1026,7 +1020,6 @@ bool kpMainWindow::slotExport ()
false/*allow remote files*/,
&chosenSaveOptions,
d->exportFirstTime,
&allowOverwritePrompt,
&allowLossyPrompt);
......@@ -1037,7 +1030,6 @@ bool kpMainWindow::slotExport ()
if (!kpDocument::savePixmapToFile (d->document->imageWithSelection (),
chosenURL,
chosenSaveOptions, *d->document->metaInfo (),
allowOverwritePrompt,
allowLossyPrompt,
this))
{
......
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