File: printpreviewdialog.cpp

package info (click to toggle)
ktikz 0.13.1%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 5,136 kB
  • sloc: cpp: 10,491; xml: 701; sh: 131; makefile: 19
file content (114 lines) | stat: -rw-r--r-- 4,877 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/***************************************************************************
 *   Copyright (C) 2011 by Glad Deschrijver                                *
 *     <glad.deschrijver@gmail.com>                                        *
 *                                                                         *
 *   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, see <http://www.gnu.org/licenses/>.  *
 ***************************************************************************/

#include "printpreviewdialog.h"

#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
#include <QtPrintSupport/QPrintPreviewWidget>
#include <QtWidgets/QVBoxLayout>
#else
#include <QtGui/QPrintPreviewWidget>
#include <QtGui/QVBoxLayout>
#endif

#include "action.h"
#include "globallocale.h"
#include "icon.h"
#include "standardaction.h"
#include "toolbar.h"
#include "zoomaction.h"

PrintPreviewDialog::PrintPreviewDialog(QPrinter *printer, QWidget *parent)
	: QDialog(parent)
{
	m_initialized = false;

	QVBoxLayout *mainLayout = new QVBoxLayout;

	m_printPreviewWidget = new QPrintPreviewWidget(printer, this);
	connect(m_printPreviewWidget, SIGNAL(paintRequested(QPrinter*)), this, SIGNAL(paintRequested(QPrinter*)));
	connect(m_printPreviewWidget, SIGNAL(previewChanged()), this, SLOT(updateZoomFactor()));

	ToolBar *toolBar = new ToolBar(QLatin1String("printpreview_toolbar"), this);
	Action *action = new Action(Icon(QLatin1String("zoom-fit-width")), tr("Fit &width"), this, QLatin1String("printpreview_fit_width"));
	connect(action, SIGNAL(triggered()), m_printPreviewWidget, SLOT(fitToWidth()));
	toolBar->addAction(action);
	action = new Action(Icon(QLatin1String("zoom-fit-best")), tr("Fit p&age"), this, QLatin1String("printpreview_fit_page"));
	connect(action, SIGNAL(triggered()), m_printPreviewWidget, SLOT(fitInView()));
	toolBar->addAction(action);
	m_zoomToAction = new ZoomAction(Icon(QLatin1String("zoom-original")), tr("&Zoom"), this, QLatin1String("printpreview_zoom_to"));
	connect(m_zoomToAction, SIGNAL(zoomFactorAdded(qreal)), this, SLOT(setZoomFactor(qreal)));
	toolBar->addAction(m_zoomToAction);
	toolBar->addAction(StandardAction::zoomIn(this, SLOT(zoomIn()), this));
	toolBar->addAction(StandardAction::zoomOut(this, SLOT(zoomOut()), this));
	action = new Action(Icon(QLatin1String("document-print")), tr("&Print"), this, QLatin1String("printpreview_print"));
	connect(action, SIGNAL(triggered()), this, SLOT(print()));
	toolBar->addAction(action);
	action = new Action(Icon(QLatin1String("window-close")), tr("&Close"), this, QLatin1String("printpreview_close"));
	connect(action, SIGNAL(triggered()), this, SLOT(reject()));
	toolBar->addAction(action);

	mainLayout->addWidget(toolBar);
	mainLayout->addWidget(m_printPreviewWidget);
	setLayout(mainLayout);

	m_zoomToAction->setZoomFactor(1.0);
}

void PrintPreviewDialog::setVisible(bool visible)
{
	// this will make the dialog get a decent default size
	if (visible && !m_initialized)
	{
		m_printPreviewWidget->updatePreview();
		m_initialized = true;
	}
	QDialog::setVisible(visible);
}

void PrintPreviewDialog::setZoomFactor(qreal zoomFactor)
{
	m_printPreviewWidget->setZoomFactor(zoomFactor);
}

void PrintPreviewDialog::updateZoomFactor()
{
	disconnect(m_zoomToAction, SIGNAL(zoomFactorAdded(qreal)), this, SLOT(setZoomFactor(qreal)));
	m_zoomToAction->setZoomFactor(m_printPreviewWidget->zoomFactor());
	connect(m_zoomToAction, SIGNAL(zoomFactorAdded(qreal)), this, SLOT(setZoomFactor(qreal)));
}

void PrintPreviewDialog::zoomIn()
{
	const qreal zoomFactor = m_printPreviewWidget->zoomFactor();
	m_zoomToAction->setZoomFactor(zoomFactor + (zoomFactor > 0.99 ?
	                                           (zoomFactor > 1.99 ? 0.5 : 0.2) : 0.1));
}

void PrintPreviewDialog::zoomOut()
{
	const qreal zoomFactor = m_printPreviewWidget->zoomFactor();
	m_zoomToAction->setZoomFactor(zoomFactor - (zoomFactor > 1.01 ?
	                                           (zoomFactor > 2.01 ? 0.5 : 0.2) : 0.1));
}

void PrintPreviewDialog::print()
{
	m_printPreviewWidget->print();
	accept();
}