Commit a6c80184 authored by Alexander Volkov's avatar Alexander Volkov
Browse files

Add "Print Preview" action

BUG: 236056
parent f9a205ac
<?xml version="1.0"?>
<!DOCTYPE gui SYSTEM "kpartgui.dtd">
<gui name="gwenview" version="63">
<gui name="gwenview" version="64">
<MenuBar>
<Menu name="file" >
......@@ -9,6 +9,7 @@
<Separator/>
<Action name="reload"/>
<Action name="file_print"/>
<Action name="file_print_preview"/>
<Separator/>
<ActionList name="file_action_list"/>
<Separator/>
......
......@@ -392,6 +392,7 @@ struct MainWindow::Private {
}
file->addAction("file_open_recent", mFileOpenRecentAction);
file->addAction(KStandardAction::Print, q, SLOT(print()));
file->addAction(KStandardAction::PrintPreview, q, SLOT(printPreview()));
file->addAction(KStandardAction::Quit, qApp, SLOT(closeAllWindows()));
QAction *action = file->addAction("reload", q, SLOT(reload()));
......@@ -568,6 +569,7 @@ struct MainWindow::Private {
menu->addAction(actionCollection->action(KStandardAction::name(KStandardAction::Save)));
menu->addAction(actionCollection->action(KStandardAction::name(KStandardAction::SaveAs)));
menu->addAction(actionCollection->action(KStandardAction::name(KStandardAction::Print)));
menu->addAction(actionCollection->action(KStandardAction::name(KStandardAction::PrintPreview)));
menu->addSeparator();
menu->addAction(actionCollection->action(KStandardAction::name(KStandardAction::Copy)));
menu->addAction(actionCollection->action(QStringLiteral("file_trash")));
......@@ -793,6 +795,7 @@ struct MainWindow::Private {
actionCollection->action("file_save")->setEnabled(canSave && isModified);
actionCollection->action("file_save_as")->setEnabled(canSave);
actionCollection->action("file_print")->setEnabled(isRasterImage);
actionCollection->action("file_print_preview")->setEnabled(isRasterImage);
#ifdef KF5Purpose_FOUND
if (url.isEmpty()) {
......@@ -919,6 +922,22 @@ struct MainWindow::Private {
assignThumbnailProviderToThumbnailView(mStartMainPage->recentFoldersView());
}
}
enum class ShowPreview { Yes, No };
void print(ShowPreview showPreview)
{
if (!mContextManager->currentUrlIsRasterImage()) {
return;
}
Document::Ptr doc = DocumentFactory::instance()->load(mContextManager->currentUrl());
PrintHelper printHelper(q);
if (showPreview == ShowPreview::Yes) {
printHelper.printPreview(doc);
} else {
printHelper.print(doc);
}
}
};
MainWindow::MainWindow()
......@@ -1766,13 +1785,12 @@ void MainWindow::saveConfig()
void MainWindow::print()
{
if (!d->mContextManager->currentUrlIsRasterImage()) {
return;
}
d->print(Private::ShowPreview::No);
}
Document::Ptr doc = DocumentFactory::instance()->load(d->mContextManager->currentUrl());
PrintHelper printHelper(this);
printHelper.print(doc);
void MainWindow::printPreview()
{
d->print(Private::ShowPreview::Yes);
}
void MainWindow::preloadNextUrl()
......
......@@ -124,6 +124,7 @@ private Q_SLOTS:
void showConfigDialog();
void loadConfig();
void print();
void printPreview();
void preloadNextUrl();
......
......@@ -29,6 +29,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#include <QCheckBox>
#include <QPainter>
#include <QPrintDialog>
#include <QPrintPreviewDialog>
#include <QPrinter>
#include <QUrl>
......@@ -98,6 +99,52 @@ struct PrintHelperPrivate {
return QPoint(posX, posY);
}
void setupPrinter(QPrinter *printer, Document::Ptr doc)
{
doc->waitUntilLoaded();
printer->setDocName(doc->url().fileName());
const auto docSize = doc->size();
printer->setPageOrientation(docSize.width() > docSize.height() ? QPageLayout::Landscape
: QPageLayout::Portrait);
}
void print(QPrinter *printer, Document::Ptr doc, bool showPrinterSetupDialog)
{
auto *optionsPage = new PrintOptionsPage(doc->size());
optionsPage->loadConfig();
QScopedPointer<QPrintDialog> dialog;
if (showPrinterSetupDialog) {
dialog.reset(new QPrintDialog(printer, mParent));
#if defined(Q_OS_UNIX) && !defined(Q_OS_DARWIN)
dialog->setOptionTabs(QList<QWidget *>() << optionsPage);
#else
optionsPage->setParent(dialog.data());
#endif
dialog->setWindowTitle(i18n("Print Image"));
bool wantToPrint = dialog->exec();
optionsPage->saveConfig();
if (!wantToPrint) {
return;
}
}
QPainter painter(printer);
QRect rect = painter.viewport();
QSize size = adjustSize(optionsPage, doc, printer->resolution(), rect.size());
QPoint pos = adjustPosition(optionsPage, size, rect.size());
painter.setViewport(pos.x(), pos.y(), size.width(), size.height());
if (!dialog) {
delete optionsPage;
}
QImage image = doc->image();
painter.setWindow(image.rect());
painter.drawImage(0, 0, image);
}
};
PrintHelper::PrintHelper(QWidget *parent)
......@@ -113,40 +160,20 @@ PrintHelper::~PrintHelper()
void PrintHelper::print(Document::Ptr doc)
{
doc->waitUntilLoaded();
QPrinter printer;
printer.setDocName(doc->url().fileName());
const auto docSize = doc->size();
printer.setPageOrientation(docSize.width() > docSize.height() ? QPageLayout::Landscape
: QPageLayout::Portrait);
auto *optionsPage = new PrintOptionsPage(docSize);
optionsPage->loadConfig();
DialogGuard<QPrintDialog> dialog(&printer, d->mParent);
#if defined(Q_OS_UNIX) && !defined(Q_OS_DARWIN)
dialog->setOptionTabs(QList<QWidget *>() << optionsPage);
#else
optionsPage->setParent(dialog.data());
#endif
dialog->setWindowTitle(i18n("Print Image"));
bool wantToPrint = dialog->exec();
optionsPage->saveConfig();
if (!wantToPrint) {
return;
}
QPainter painter(&printer);
QRect rect = painter.viewport();
QSize size = d->adjustSize(optionsPage, doc, printer.resolution(), rect.size());
QPoint pos = d->adjustPosition(optionsPage, size, rect.size());
painter.setViewport(pos.x(), pos.y(), size.width(), size.height());
d->setupPrinter(&printer, doc);
d->print(&printer, doc, true);
}
QImage image = doc->image();
painter.setWindow(image.rect());
painter.drawImage(0, 0, image);
void PrintHelper::printPreview(Document::Ptr doc)
{
QPrinter printer;
d->setupPrinter(&printer, doc);
QPrintPreviewDialog dlg(&printer, d->mParent);
QObject::connect(&dlg, &QPrintPreviewDialog::paintRequested, [this, doc](QPrinter *printer) {
d->print(printer, doc, false);
});
dlg.exec();
}
} // namespace
......@@ -41,7 +41,8 @@ public:
explicit PrintHelper(QWidget *parent);
~PrintHelper();
void print(Document::Ptr);
void print(Document::Ptr doc);
void printPreview(Document::Ptr doc);
private:
PrintHelperPrivate *const d;
......
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