CalendarSupport

calprinter.cpp
1/*
2 SPDX-FileCopyrightText: 1998 Preston Brown <pbrown@kde.org>
3 SPDX-FileCopyrightText: 2003-2004 Reinhold Kainhofer <reinhold@kainhofer.com>
4
5 SPDX-License-Identifier: GPL-2.0-or-later WITH Qt-Commercial-exception-1.0
6*/
7
8#include "calprinter.h"
9using namespace Qt::Literals::StringLiterals;
10
11#include "calprintdefaultplugins.h"
12#include "journalprint.h"
13#include "yearprint.h"
14
15#include <KConfigGroup>
16#include <KMessageBox>
17#include <KStandardGuiItem>
18#include <KWindowStateSaver>
19
20#include <QButtonGroup>
21#include <QDialogButtonBox>
22#include <QGridLayout>
23#include <QGroupBox>
24#include <QPrintDialog>
25#include <QPrintPreviewDialog>
26#include <QSplitter>
27#include <QStackedWidget>
28#include <QVBoxLayout>
29
30using namespace CalendarSupport;
31
32CalPrinter::CalPrinter(QWidget *parent, const KCalendarCore::Calendar::Ptr &calendar, bool uniqItem)
33 : QObject(parent)
34 , mParent(parent)
35 , mConfig(new KConfig(QStringLiteral("calendar_printing.rc"), KConfig::SimpleConfig))
36 , mUniqItem(uniqItem)
37{
38 init(calendar);
39}
40
41CalPrinter::~CalPrinter()
42{
43 qDeleteAll(mPrintPlugins);
44 delete mConfig;
45}
46
47void CalPrinter::init(const KCalendarCore::Calendar::Ptr &calendar)
48{
49 mCalendar = calendar;
50
51 qDeleteAll(mPrintPlugins);
52 mPrintPlugins.clear();
53
54 if (!mUniqItem) {
55 mPrintPlugins.prepend(new CalPrintYear());
56 mPrintPlugins.prepend(new CalPrintJournal());
57 mPrintPlugins.prepend(new CalPrintTodos());
58 mPrintPlugins.prepend(new CalPrintMonth());
59 mPrintPlugins.prepend(new CalPrintWeek());
60 mPrintPlugins.prepend(new CalPrintDay());
61 }
62 mPrintPlugins.prepend(new CalPrintIncidence());
63
64 PrintPlugin::List::Iterator it = mPrintPlugins.begin();
65 PrintPlugin::List::Iterator end = mPrintPlugins.end();
66 for (; it != end; ++it) {
67 if (*it) {
68 (*it)->setConfig(mConfig);
69 (*it)->setCalendar(mCalendar);
70 (*it)->doLoadConfig();
71 }
72 }
73}
74
76{
77 for (const auto plugin : std::as_const(mPrintPlugins)) {
78 plugin->setDateRange(fd, td);
79 }
80}
81
82void CalPrinter::print(int type, QDate fd, QDate td, const KCalendarCore::Incidence::List &selectedIncidences, bool preview)
83{
85 const PrintPlugin::List::Iterator end = mPrintPlugins.end();
86 for (it = mPrintPlugins.begin(); it != end; ++it) {
87 (*it)->setSelectedIncidences(selectedIncidences);
88 }
89 QPointer<CalPrintDialog> printDialog = new CalPrintDialog(type, mPrintPlugins, mParent, mUniqItem);
90
91 KConfigGroup grp(mConfig, QStringLiteral("")); // orientation setting isn't in a group
92 printDialog->setOrientation(CalPrinter::ePrintOrientation(grp.readEntry("Orientation", 1)));
93 printDialog->setPreview(preview);
94 setDateRange(fd, td);
95
96 if (printDialog->exec() == QDialog::Accepted) {
97 grp.writeEntry("Orientation", static_cast<int>(printDialog->orientation()));
98
99 // Save all changes in the dialog
100 for (it = mPrintPlugins.begin(); it != mPrintPlugins.end(); ++it) {
101 (*it)->doSaveConfig();
102 }
103 doPrint(printDialog->selectedPlugin(), printDialog->orientation(), preview);
104 }
105 delete printDialog;
106
107 for (it = mPrintPlugins.begin(); it != mPrintPlugins.end(); ++it) {
108 (*it)->setSelectedIncidences(KCalendarCore::Incidence::List());
109 }
110}
111
112void CalPrinter::doPrint(PrintPlugin *selectedStyle, CalPrinter::ePrintOrientation dlgorientation, bool preview)
113{
114 if (!selectedStyle) {
115 KMessageBox::error(mParent, i18nc("@info", "Unable to print, an invalid print style was specified."), i18nc("@title:window", "Printing error"));
116 return;
117 }
118
119 QPrinter printer;
120 switch (dlgorientation) {
121 case eOrientPlugin:
122 printer.setPageOrientation(selectedStyle->defaultOrientation());
123 break;
124 case eOrientPortrait:
126 break;
127 case eOrientLandscape:
129 break;
130 case eOrientPrinter:
131 break;
132 }
133
134 if (preview) {
136 new KWindowStateSaver(printPreview.data(), "CalendarPrintPreviewDialog"_L1);
137 connect(printPreview.data(), &QPrintPreviewDialog::paintRequested, this, [selectedStyle, &printer]() {
138 selectedStyle->doPrint(&printer);
139 });
140 printPreview->exec();
141 delete printPreview;
142 } else {
143 QPointer<QPrintDialog> printDialog = new QPrintDialog(&printer, mParent);
144 if (printDialog->exec() == QDialog::Accepted) {
145 selectedStyle->doPrint(&printer);
146 }
147 delete printDialog;
148 }
149}
150
151void CalPrinter::updateConfig()
152{
153}
154
155CalPrintDialog::CalPrintDialog(int initialPrintType, const PrintPlugin::List &plugins, QWidget *parent, bool uniqItem)
156 : QDialog(parent)
157{
158 setWindowTitle(i18nc("@title:window", "Print"));
160 auto mainLayout = new QVBoxLayout(this);
161 mOkButton = buttonBox->button(QDialogButtonBox::Ok);
162 mOkButton->setDefault(true);
163 mOkButton->setShortcut(Qt::CTRL | Qt::Key_Return);
164 connect(buttonBox, &QDialogButtonBox::accepted, this, &CalPrintDialog::slotOk);
166 setModal(true);
167 auto page = new QWidget(this);
168 auto pageVBoxLayout = new QVBoxLayout(page);
169 pageVBoxLayout->setContentsMargins(0, 0, 0, 0);
170 mainLayout->addWidget(page);
171 mainLayout->addWidget(buttonBox);
172
173 auto splitter = new QSplitter(page);
174 pageVBoxLayout->addWidget(splitter);
175 splitter->setOrientation(Qt::Horizontal);
176 splitter->setChildrenCollapsible(false);
177 auto typeBox = new QGroupBox(i18nc("@title:group", "Print Style"), splitter);
178 QBoxLayout *typeLayout = new QVBoxLayout(typeBox);
179 mTypeGroup = new QButtonGroup(typeBox);
180
181 auto splitterRight = new QWidget(splitter);
182 auto splitterRightLayout = new QGridLayout(splitterRight);
183 splitterRightLayout->setContentsMargins(0, 0, 0, 0);
184 // splitterRightLayout->setMargin( marginHint() );
185 // splitterRightLayout->setSpacing( spacingHint() );
186
187 mConfigArea = new QStackedWidget(splitterRight);
188 splitterRightLayout->addWidget(mConfigArea, 0, 0, 1, 2);
189 auto orientationLabel = new QLabel(i18nc("@label", "Page &orientation:"), splitterRight);
190 orientationLabel->setAlignment(Qt::AlignRight);
191 splitterRightLayout->addWidget(orientationLabel, 1, 0);
192
193 mOrientationSelection = new QComboBox(splitterRight);
194 mOrientationSelection->setToolTip(i18nc("@info:tooltip", "Set the print orientation"));
195 mOrientationSelection->setWhatsThis(i18nc("@info:whatsthis",
196 "Choose if you want your output to be printed in \"portrait\" or "
197 "\"landscape\". You can also default to the orientation best suited to "
198 "the selected style or to your printer's default setting."));
199 mOrientationSelection->addItem(i18nc("@item:inlistbox", "Use Default Orientation of Selected Style"));
200 mOrientationSelection->addItem(i18nc("@item:inlistbox", "Use Printer Default"));
201 mOrientationSelection->addItem(i18nc("@item:inlistbox", "Portrait"));
202 mOrientationSelection->addItem(i18nc("@item:inlistbox", "Landscape"));
203 splitterRightLayout->addWidget(mOrientationSelection, 1, 1);
204
205 // signals and slots connections
206 connect(mTypeGroup, &QButtonGroup::buttonClicked, this, &CalPrintDialog::setPrintType);
207 orientationLabel->setBuddy(mOrientationSelection);
208
209 // First insert the config widgets into the widget stack. This possibly assigns
210 // proper ids (when two plugins have the same sortID), so store them in a map
211 // and use these new IDs to later sort the plugins for the type selection.
212 for (PrintPlugin::List::ConstIterator it = plugins.constBegin(), total = plugins.constEnd(); it != total; ++it) {
213 int newid = mConfigArea->insertWidget((*it)->sortID(), (*it)->configWidget(mConfigArea));
214 mPluginIDs[newid] = (*it);
215 }
216 // Insert all plugins in sorted order; plugins with clashing IDs will be first
218 bool firstButton = true;
219 int id = 0;
220 for (mapit = mPluginIDs.constBegin(); mapit != mPluginIDs.constEnd(); ++mapit) {
221 PrintPlugin *p = mapit.value();
222 auto radioButton = new QRadioButton(p->description());
223 radioButton->setEnabled(p->enabled());
224 radioButton->setToolTip(i18nc("@info:tooltip", "Select the type of print"));
225 radioButton->setWhatsThis(i18nc("@info:whatsthis",
226 "Select one of the following types of prints you want to make. "
227 "You may want to print an individual item, or all the items for a "
228 "specific time range (like a day, week or month), or you may want "
229 "to print your to-do list."));
230 // Check the first available button (to ensure one is selected initially) and then
231 // the button matching the desired print type -- if such is available!
232 if ((firstButton || p->sortID() == initialPrintType) && p->enabled()) {
233 firstButton = false;
234 radioButton->setChecked(true);
235 changePrintType(id);
236 }
237 mTypeGroup->addButton(radioButton, mapit.key());
238 typeLayout->addWidget(radioButton);
239 id++;
240 }
241 if (uniqItem) {
242 typeBox->hide();
243 }
244 typeLayout->insertStretch(-1, 100);
245 setMinimumSize(minimumSizeHint());
246 resize(minimumSizeHint());
247}
248
249CalPrintDialog::~CalPrintDialog() = default;
250
251void CalPrintDialog::setPreview(bool preview)
252{
253 if (preview) {
254 mOkButton->setText(i18nc("@action:button", "&Preview"));
255 } else {
256 mOkButton->setText(KStandardGuiItem::print().text());
257 }
258}
259
260void CalPrintDialog::changePrintType(int i)
261{
262 mConfigArea->setCurrentIndex(i);
263 mConfigArea->currentWidget()->raise();
264 QAbstractButton *btn = mTypeGroup->button(i);
265 if (btn) {
266 btn->setChecked(true);
267 }
268}
269
270void CalPrintDialog::setPrintType(QAbstractButton *button)
271{
272 if (button) {
273 const int i = mTypeGroup->id(button);
274 mConfigArea->setCurrentIndex(i);
275 mConfigArea->currentWidget()->raise();
276 button->setChecked(true);
277 }
278}
279
280void CalPrintDialog::setOrientation(CalPrinter::ePrintOrientation orientation)
281{
282 mOrientation = orientation;
283 mOrientationSelection->setCurrentIndex(mOrientation);
284}
285
286CalPrinter::ePrintOrientation CalPrintDialog::orientation() const
287{
288 return mOrientation;
289}
290
291PrintPlugin *CalPrintDialog::selectedPlugin()
292{
293 int id = mConfigArea->currentIndex();
294 if (mPluginIDs.contains(id)) {
295 return mPluginIDs[id];
296 } else {
297 return nullptr;
298 }
299}
300
301void CalPrintDialog::slotOk()
302{
303 mOrientation = static_cast<CalPrinter::ePrintOrientation>(mOrientationSelection->currentIndex());
304
305 QMap<int, PrintPlugin *>::ConstIterator it = mPluginIDs.constBegin();
306 for (; it != mPluginIDs.constEnd(); ++it) {
307 if (it.value()) {
308 it.value()->readSettingsWidget();
309 }
310 }
311 accept();
312}
313
314#include "moc_calprinter.cpp"
void setDateRange(QDate start, QDate end)
Set date range to be printed.
CalPrinter(QWidget *par, const KCalendarCore::Calendar::Ptr &calendar, bool uniqItem=false)
Base class for Calendar printing classes.
Definition printplugin.h:43
virtual bool enabled() const
Returns true if the plugin should be enabled; false otherwise.
virtual QString description() const =0
Returns short description of print format.
virtual void doPrint(QPrinter *printer)=0
Actually do the printing.
virtual QPageLayout::Orientation defaultOrientation() const
Orientation of printout.
virtual int sortID() const
Returns the sort ID of the plugin.
Definition printplugin.h:95
QString i18nc(const char *context, const char *text, const TYPE &arg...)
void error(QWidget *parent, const QString &text, const QString &title, const KGuiItem &buttonOk, Options options=Notify)
QAction * printPreview(const QObject *recvr, const char *slot, QObject *parent)
KGuiItem print()
const QList< QKeySequence > & end()
void setChecked(bool)
void setText(const QString &text)
QVariant data() const const
void addWidget(QWidget *widget, int stretch, Qt::Alignment alignment)
void insertStretch(int index, int stretch)
QAbstractButton * button(int id) const const
void buttonClicked(QAbstractButton *button)
int id(QAbstractButton *button) const const
void setCurrentIndex(int index)
virtual void accept()
virtual void reject()
iterator begin()
void clear()
const_iterator constBegin() const const
const_iterator constEnd() const const
iterator end()
void prepend(parameter_type value)
ConstIterator
Key key(const T &value, const Key &defaultKey) const const
T value(const Key &key, const T &defaultValue) const const
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
virtual bool setPageOrientation(QPageLayout::Orientation orientation)
void paintRequested(QPrinter *printer)
void setCurrentIndex(int index)
QWidget * currentWidget() const const
AlignRight
Key_Return
Horizontal
void raise()
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:05:28 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.