calprinter.cpp 11.1 KB
Newer Older
1
/*
2
3
  SPDX-FileCopyrightText: 1998 Preston Brown <pbrown@kde.org>
  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
9
10
11
12
13
14
15
16
*/

#include "calprinter.h"
#include "calprintdefaultplugins.h"
#include "journalprint.h"
#include "yearprint.h"

#include <KMessageBox>
#include <KStandardGuiItem>
#include <QVBoxLayout>

Laurent Montel's avatar
Laurent Montel committed
17
#include <KConfigGroup>
18
#include <QButtonGroup>
Laurent Montel's avatar
Laurent Montel committed
19
#include <QDialogButtonBox>
20
21
22
23
24
25
26
#include <QGridLayout>
#include <QGroupBox>
#include <QPrintDialog>
#include <QPrintPreviewDialog>
#include <QSplitter>
#include <QStackedWidget>

Laurent Montel's avatar
Laurent Montel committed
27
28
#include <PimCommon/KPimPrintPreviewDialog>

29
30
using namespace CalendarSupport;

Laurent Montel's avatar
Laurent Montel committed
31
32
CalPrinter::CalPrinter(QWidget *parent, const Akonadi::ETMCalendar::Ptr &calendar, bool uniqItem)
    : QObject(parent)
Laurent Montel's avatar
Laurent Montel committed
33
34
    , mParent(parent)
    , mConfig(new KConfig(QStringLiteral("calendar_printing.rc"), KConfig::SimpleConfig))
Laurent Montel's avatar
Laurent Montel committed
35
    , mUniqItem(uniqItem)
36
37
38
39
40
41
42
{

    init(calendar);
}

CalPrinter::~CalPrinter()
{
Laurent Montel's avatar
Laurent Montel committed
43
    qDeleteAll(mPrintPlugins);
44
45
46
47
48
49
50
    delete mConfig;
}

void CalPrinter::init(const Akonadi::ETMCalendar::Ptr &calendar)
{
    mCalendar = calendar;

Laurent Montel's avatar
Laurent Montel committed
51
    qDeleteAll(mPrintPlugins);
52
53
54
55
56
57
58
59
60
61
62
63
64
    mPrintPlugins.clear();

    if (!mUniqItem) {
        mPrintPlugins.prepend(new CalPrintYear());
        mPrintPlugins.prepend(new CalPrintJournal());
        mPrintPlugins.prepend(new CalPrintTodos());
        mPrintPlugins.prepend(new CalPrintMonth());
        mPrintPlugins.prepend(new CalPrintWeek());
        mPrintPlugins.prepend(new CalPrintDay());
    }
    mPrintPlugins.prepend(new CalPrintIncidence());

    PrintPlugin::List::Iterator it = mPrintPlugins.begin();
Laurent Montel's avatar
Laurent Montel committed
65
66
    PrintPlugin::List::Iterator end = mPrintPlugins.end();
    for (; it != end; ++it) {
67
68
69
70
71
72
73
74
        if (*it) {
            (*it)->setConfig(mConfig);
            (*it)->setCalendar(mCalendar);
            (*it)->doLoadConfig();
        }
    }
}

Laurent Montel's avatar
Laurent Montel committed
75
void CalPrinter::setDateRange(QDate fd, QDate td)
76
{
Laurent Montel's avatar
Laurent Montel committed
77
    for (const auto plugin : std::as_const(mPrintPlugins)) {
Glen Ditchfield's avatar
Glen Ditchfield committed
78
        plugin->setDateRange(fd, td);
79
80
81
    }
}

Laurent Montel's avatar
Laurent Montel committed
82
void CalPrinter::print(int type, QDate fd, QDate td, const KCalendarCore::Incidence::List &selectedIncidences, bool preview)
83
84
{
    PrintPlugin::List::Iterator it;
Laurent Montel's avatar
Laurent Montel committed
85
86
    const PrintPlugin::List::Iterator end = mPrintPlugins.end();
    for (it = mPrintPlugins.begin(); it != end; ++it) {
87
88
        (*it)->setSelectedIncidences(selectedIncidences);
    }
Laurent Montel's avatar
Laurent Montel committed
89
    QPointer<CalPrintDialog> printDialog = new CalPrintDialog(type, mPrintPlugins, mParent, mUniqItem);
90

Laurent Montel's avatar
Laurent Montel committed
91
    KConfigGroup grp(mConfig, ""); // orientation setting isn't in a group
92
93
94
95
96
    printDialog->setOrientation(CalPrinter::ePrintOrientation(grp.readEntry("Orientation", 1)));
    printDialog->setPreview(preview);
    setDateRange(fd, td);

    if (printDialog->exec() == QDialog::Accepted) {
Laurent Montel's avatar
Laurent Montel committed
97
        grp.writeEntry("Orientation", static_cast<int>(printDialog->orientation()));
98
99
100
101
102
103
104
105
106
107

        // Save all changes in the dialog
        for (it = mPrintPlugins.begin(); it != mPrintPlugins.end(); ++it) {
            (*it)->doSaveConfig();
        }
        doPrint(printDialog->selectedPlugin(), printDialog->orientation(), preview);
    }
    delete printDialog;

    for (it = mPrintPlugins.begin(); it != mPrintPlugins.end(); ++it) {
108
        (*it)->setSelectedIncidences(KCalendarCore::Incidence::List());
109
110
111
    }
}

Laurent Montel's avatar
Laurent Montel committed
112
void CalPrinter::doPrint(PrintPlugin *selectedStyle, CalPrinter::ePrintOrientation dlgorientation, bool preview)
113
114
{
    if (!selectedStyle) {
Laurent Montel's avatar
Laurent Montel committed
115
        KMessageBox::error(mParent, i18nc("@info", "Unable to print, an invalid print style was specified."), i18nc("@title:window", "Printing error"));
116
117
118
119
120
121
        return;
    }

    QPrinter printer;
    switch (dlgorientation) {
    case eOrientPlugin:
122
        printer.setPageOrientation(selectedStyle->defaultOrientation());
123
124
        break;
    case eOrientPortrait:
125
        printer.setPageOrientation(QPageLayout::Portrait);
126
127
        break;
    case eOrientLandscape:
128
        printer.setPageOrientation(QPageLayout::Landscape);
129
130
131
132
133
134
        break;
    case eOrientPrinter:
        break;
    }

    if (preview) {
Laurent Montel's avatar
Laurent Montel committed
135
136
        QPointer<PimCommon::KPimPrintPreviewDialog> printPreview = new PimCommon::KPimPrintPreviewDialog(&printer);
        connect(printPreview.data(), &QPrintPreviewDialog::paintRequested, this, [selectedStyle, &printer]() {
Laurent Montel's avatar
Laurent Montel committed
137
            selectedStyle->doPrint(&printer);
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
        });
        printPreview->exec();
        delete printPreview;
    } else {
        QPointer<QPrintDialog> printDialog = new QPrintDialog(&printer, mParent);
        if (printDialog->exec() == QDialog::Accepted) {
            selectedStyle->doPrint(&printer);
        }
        delete printDialog;
    }
}

void CalPrinter::updateConfig()
{
}

Laurent Montel's avatar
Laurent Montel committed
154
CalPrintDialog::CalPrintDialog(int initialPrintType, const PrintPlugin::List &plugins, QWidget *parent, bool uniqItem)
155
156
157
    : QDialog(parent)
{
    setWindowTitle(i18nc("@title:window", "Print"));
Laurent Montel's avatar
Laurent Montel committed
158
    auto buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, this);
159
    auto mainLayout = new QVBoxLayout(this);
160
161
162
163
164
165
    mOkButton = buttonBox->button(QDialogButtonBox::Ok);
    mOkButton->setDefault(true);
    mOkButton->setShortcut(Qt::CTRL | Qt::Key_Return);
    connect(buttonBox, &QDialogButtonBox::accepted, this, &CalPrintDialog::slotOk);
    connect(buttonBox, &QDialogButtonBox::rejected, this, &CalPrintDialog::reject);
    setModal(true);
Laurent Montel's avatar
Laurent Montel committed
166
    auto page = new QWidget(this);
167
    auto pageVBoxLayout = new QVBoxLayout(page);
168
    pageVBoxLayout->setContentsMargins(0, 0, 0, 0);
169
170
171
    mainLayout->addWidget(page);
    mainLayout->addWidget(buttonBox);

172
    auto splitter = new QSplitter(page);
173
174
175
    pageVBoxLayout->addWidget(splitter);
    splitter->setOrientation(Qt::Horizontal);
    splitter->setChildrenCollapsible(false);
Laurent Montel's avatar
Laurent Montel committed
176
    auto typeBox = new QGroupBox(i18nc("@title:group", "Print Style"), splitter);
177
178
179
    QBoxLayout *typeLayout = new QVBoxLayout(typeBox);
    mTypeGroup = new QButtonGroup(typeBox);

Laurent Montel's avatar
Laurent Montel committed
180
    auto splitterRight = new QWidget(splitter);
181
    auto splitterRightLayout = new QGridLayout(splitterRight);
182
    splitterRightLayout->setContentsMargins(0, 0, 0, 0);
Laurent Montel's avatar
Laurent Montel committed
183
184
    // splitterRightLayout->setMargin( marginHint() );
    // splitterRightLayout->setSpacing( spacingHint() );
185
186
187

    mConfigArea = new QStackedWidget(splitterRight);
    splitterRightLayout->addWidget(mConfigArea, 0, 0, 1, 2);
Laurent Montel's avatar
Laurent Montel committed
188
    auto orientationLabel = new QLabel(i18nc("@label", "Page &orientation:"), splitterRight);
189
190
191
    orientationLabel->setAlignment(Qt::AlignRight);
    splitterRightLayout->addWidget(orientationLabel, 1, 0);

192
    mOrientationSelection = new QComboBox(splitterRight);
Laurent Montel's avatar
Laurent Montel committed
193
194
195
196
197
198
199
200
201
    mOrientationSelection->setToolTip(i18nc("@info:tooltip", "Set the print orientation"));
    mOrientationSelection->setWhatsThis(i18nc("@info:whatsthis",
                                              "Choose if you want your output to be printed in \"portrait\" or "
                                              "\"landscape\". You can also default to the orientation best suited to "
                                              "the selected style or to your printer's default setting."));
    mOrientationSelection->addItem(i18nc("@item:inlistbox", "Use Default Orientation of Selected Style"));
    mOrientationSelection->addItem(i18nc("@item:inlistbox", "Use Printer Default"));
    mOrientationSelection->addItem(i18nc("@item:inlistbox", "Portrait"));
    mOrientationSelection->addItem(i18nc("@item:inlistbox", "Landscape"));
202
203
204
    splitterRightLayout->addWidget(mOrientationSelection, 1, 1);

    // signals and slots connections
205
    connect(mTypeGroup, &QButtonGroup::buttonClicked, this, &CalPrintDialog::setPrintType);
206
207
208
209
210
    orientationLabel->setBuddy(mOrientationSelection);

    // First insert the config widgets into the widget stack. This possibly assigns
    // proper ids (when two plugins have the same sortID), so store them in a map
    // and use these new IDs to later sort the plugins for the type selection.
Laurent Montel's avatar
Laurent Montel committed
211
    for (PrintPlugin::List::ConstIterator it = plugins.constBegin(), total = plugins.constEnd(); it != total; ++it) {
212
213
214
215
216
217
218
219
220
        int newid = mConfigArea->insertWidget((*it)->sortID(), (*it)->configWidget(mConfigArea));
        mPluginIDs[newid] = (*it);
    }
    // Insert all plugins in sorted order; plugins with clashing IDs will be first
    QMap<int, PrintPlugin *>::ConstIterator mapit;
    bool firstButton = true;
    int id = 0;
    for (mapit = mPluginIDs.constBegin(); mapit != mPluginIDs.constEnd(); ++mapit) {
        PrintPlugin *p = mapit.value();
Laurent Montel's avatar
Laurent Montel committed
221
        auto radioButton = new QRadioButton(p->description());
222
        radioButton->setEnabled(p->enabled());
Laurent Montel's avatar
Laurent Montel committed
223
224
225
226
227
228
        radioButton->setToolTip(i18nc("@info:tooltip", "Select the type of print"));
        radioButton->setWhatsThis(i18nc("@info:whatsthis",
                                        "Select one of the following types of prints you want to make. "
                                        "You may want to print an individual item, or all the items for a "
                                        "specific time range (like a day, week or month), or you may want "
                                        "to print your to-do list."));
229
230
231
232
233
        // Check the first available button (to ensure one is selected initially) and then
        // the button matching the desired print type -- if such is available!
        if ((firstButton || p->sortID() == initialPrintType) && p->enabled()) {
            firstButton = false;
            radioButton->setChecked(true);
234
            changePrintType(id);
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
        }
        mTypeGroup->addButton(radioButton, mapit.key());
        typeLayout->addWidget(radioButton);
        id++;
    }
    if (uniqItem) {
        typeBox->hide();
    }
    typeLayout->insertStretch(-1, 100);
    setMinimumSize(minimumSizeHint());
    resize(minimumSizeHint());
}

CalPrintDialog::~CalPrintDialog()
{
}

void CalPrintDialog::setPreview(bool preview)
{
    if (preview) {
        mOkButton->setText(i18nc("@action:button", "&Preview"));
    } else {
        mOkButton->setText(KStandardGuiItem::print().text());
    }
}

261
void CalPrintDialog::changePrintType(int i)
262
263
264
265
266
267
268
269
270
{
    mConfigArea->setCurrentIndex(i);
    mConfigArea->currentWidget()->raise();
    QAbstractButton *btn = mTypeGroup->button(i);
    if (btn) {
        btn->setChecked(true);
    }
}

271
272
273
274
275
276
277
278
279
280
void CalPrintDialog::setPrintType(QAbstractButton *button)
{
    if (button) {
        const int i = mTypeGroup->id(button);
        mConfigArea->setCurrentIndex(i);
        mConfigArea->currentWidget()->raise();
        button->setChecked(true);
    }
}

281
282
283
284
285
286
void CalPrintDialog::setOrientation(CalPrinter::ePrintOrientation orientation)
{
    mOrientation = orientation;
    mOrientationSelection->setCurrentIndex(mOrientation);
}

Laurent Montel's avatar
Laurent Montel committed
287
288
289
290
291
CalPrinter::ePrintOrientation CalPrintDialog::orientation() const
{
    return mOrientation;
}

292
293
294
295
296
297
PrintPlugin *CalPrintDialog::selectedPlugin()
{
    int id = mConfigArea->currentIndex();
    if (mPluginIDs.contains(id)) {
        return mPluginIDs[id];
    } else {
Laurent Montel's avatar
Laurent Montel committed
298
        return nullptr;
299
300
301
302
303
    }
}

void CalPrintDialog::slotOk()
{
Laurent Montel's avatar
Laurent Montel committed
304
    mOrientation = static_cast<CalPrinter::ePrintOrientation>(mOrientationSelection->currentIndex());
305
306
307
308
309
310
311
312
313

    QMap<int, PrintPlugin *>::ConstIterator it = mPluginIDs.constBegin();
    for (; it != mPluginIDs.constEnd(); ++it) {
        if (it.value()) {
            it.value()->readSettingsWidget();
        }
    }
    accept();
}