File indexing completed on 2025-01-05 03:52:01

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2003-11-03
0007  * Description : a tool to create calendar.
0008  *
0009  * SPDX-FileCopyrightText: 2003-2005 by Renchi Raju <renchi dot raju at gmail dot com>
0010  * SPDX-FileCopyrightText: 2006      by Tom Albers <tomalbers at kde dot nl>
0011  * SPDX-FileCopyrightText: 2007-2008 by Orgad Shaneh <orgads at gmail dot com>
0012  * SPDX-FileCopyrightText: 2011      by Andi Clemens <andi dot clemens at googlemail dot com>
0013  * SPDX-FileCopyrightText: 2012      by Angelo Naselli <anaselli at linux dot it>
0014  * SPDX-FileCopyrightText: 2012-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
0015  *
0016  * SPDX-License-Identifier: GPL-2.0-or-later
0017  *
0018  * ============================================================ */
0019 
0020 #include "calwizard.h"
0021 
0022 // Qt includes
0023 
0024 #include <QIcon>
0025 #include <QDate>
0026 #include <QPrintDialog>
0027 #include <QPrinter>
0028 #include <QStringList>
0029 #include <QMenu>
0030 #include <QLocale>
0031 #include <QPushButton>
0032 
0033 // KDE includes
0034 
0035 #include <klocalizedstring.h>
0036 
0037 // Local includes
0038 
0039 #include "digikam_config.h"
0040 #include "calintropage.h"
0041 #include "calprinter.h"
0042 #include "calsettings.h"
0043 #include "calsystem.h"
0044 #include "caltemplate.h"
0045 #include "digikam_debug.h"
0046 #include "ui_calevents.h"
0047 #include "ui_calprogress.h"
0048 
0049 namespace DigikamGenericCalendarPlugin
0050 {
0051 
0052 class Q_DECL_HIDDEN CalWizard::Private
0053 {
0054 public:
0055 
0056     explicit Private()
0057       : iface         (nullptr),
0058         introPage     (nullptr),
0059         cSettings     (nullptr),
0060         wTemplate     (nullptr),
0061         wPrintLabel   (nullptr),
0062         wFinish       (nullptr),
0063         wTemplatePage (nullptr),
0064         wPrintPage    (nullptr),
0065         wFinishPage   (nullptr),
0066 
0067 #ifdef HAVE_KCALENDAR
0068 
0069         wEvents       (nullptr),
0070         wEventsPage   (nullptr),
0071 
0072 #endif
0073 
0074         printer       (nullptr),
0075         printThread   (nullptr)
0076     {
0077     }
0078 
0079     DInfoInterface*  iface;
0080     CalIntroPage*    introPage;
0081     CalSettings*     cSettings;
0082     CalTemplate*     wTemplate;
0083     Ui::CalEvents    calEventsUI;
0084     Ui::CalProgress  calProgressUI;
0085 
0086     QLabel*          wPrintLabel;
0087     QWidget*         wFinish;
0088 
0089     DWizardPage*     wTemplatePage;
0090     DWizardPage*     wPrintPage;
0091     DWizardPage*     wFinishPage;
0092 
0093 #ifdef HAVE_KCALENDAR
0094 
0095     QWidget*         wEvents;
0096     DWizardPage*     wEventsPage;
0097 
0098 #endif
0099 
0100     QPrinter*        printer;
0101 
0102     CalPrinter*      printThread;
0103 
0104     QMap<int, QUrl>  months;
0105 };
0106 
0107 CalWizard::CalWizard(QWidget* const parent, DInfoInterface* const iface)
0108     : DWizardDlg(parent, QLatin1String("Calendar Dialog")),
0109       d(new Private)
0110 {
0111     setWindowTitle(i18nc("@title:window", "Create Calendar"));
0112     d->iface         = iface;
0113     d->cSettings     = CalSettings::instance(this);
0114     d->introPage     = new CalIntroPage(this, i18n("Welcome to Calendar Tool"));
0115 
0116     // ---------------------------------------------------------------
0117 
0118     d->wTemplate     = new CalTemplate(d->iface->currentSelectedItems(), this);
0119     d->wTemplatePage = new DWizardPage(this, i18n("Create Template for Calendar"));
0120     d->wTemplatePage->setPageWidget(d->wTemplate);
0121     d->wTemplatePage->setLeftBottomPix(QIcon::fromTheme(QLatin1String("resource-calendar-insert")));
0122 
0123     // ---------------------------------------------------------------
0124 
0125 #ifdef HAVE_KCALENDAR
0126 
0127     d->wEvents     = new QWidget(this);
0128     d->calEventsUI.setupUi(d->wEvents);
0129     d->wEventsPage = new DWizardPage(this, i18n("Choose events to show on the Calendar"));
0130     d->wEventsPage->setPageWidget(d->wEvents);
0131     d->wEventsPage->setLeftBottomPix(QIcon::fromTheme(QLatin1String("text-vcalendar")));
0132 
0133 #endif
0134 
0135     // ---------------------------------------------------------------
0136 
0137     d->wPrintLabel = new QLabel(this);
0138     d->wPrintLabel->setIndent(20);
0139     d->wPrintLabel->setWordWrap(true);
0140     d->wPrintPage  = new DWizardPage(this, i18n("Print Calendar"));
0141     d->wPrintPage->setPageWidget(d->wPrintLabel);
0142     d->wPrintPage->setLeftBottomPix(QIcon::fromTheme(QLatin1String("document-print")));
0143 
0144     // ---------------------------------------------------------------
0145 
0146     d->wFinish     = new QWidget(this);
0147     d->calProgressUI.setupUi(d->wFinish);
0148     d->wFinishPage = new DWizardPage(this, i18n("Printing in Progress"));
0149     d->wFinishPage->setPageWidget(d->wFinish);
0150     d->wFinishPage->setLeftBottomPix(QIcon::fromTheme(QLatin1String("system-run")));
0151 
0152     // ---------------------------------------------------------------
0153 
0154 #ifdef HAVE_KCALENDAR
0155 
0156     d->calEventsUI.ohUrlRequester->setFileDlgFilter(i18nc("@info: open file filters", "Calendar Data File (*.ics)"));
0157     d->calEventsUI.ohUrlRequester->setFileDlgTitle(i18nc("@title:window", "Select Calendar Data File"));
0158     d->calEventsUI.ohUrlRequester->setFileDlgMode(QFileDialog::ExistingFile);
0159 
0160     d->calEventsUI.fhUrlRequester->setFileDlgFilter(i18nc("@info: open file filters", "Calendar Data File (*.ics)"));
0161     d->calEventsUI.fhUrlRequester->setFileDlgTitle(i18nc("@title:window", "Select Calendar Data File"));
0162     d->calEventsUI.fhUrlRequester->setFileDlgMode(QFileDialog::ExistingFile);
0163 
0164 #endif
0165 
0166     // ------------------------------------------
0167 
0168     d->printThread = nullptr;
0169     d->printer     = nullptr;
0170 
0171     connect(this, SIGNAL(currentIdChanged(int)),
0172             this, SLOT(slotPageSelected(int)));
0173 }
0174 
0175 CalWizard::~CalWizard()
0176 {
0177     if (d->printThread)
0178     {
0179         d->printThread->cancel();
0180         d->printThread->wait();
0181         delete d->printThread;
0182     }
0183 
0184     delete d->printer;
0185     delete d;
0186 }
0187 
0188 DInfoInterface* CalWizard::iface() const
0189 {
0190     return d->iface;
0191 }
0192 
0193 void CalWizard::slotPageSelected(int curr)
0194 {
0195     DWizardPage* const current = dynamic_cast<DWizardPage*>(page(curr));
0196 
0197     if (current == d->wPrintPage)
0198     {
0199         d->months.clear();
0200         QUrl        image;
0201         QString     month;
0202         QStringList printList;
0203         QDate       date = CalSystem().date(d->cSettings->year(), 1, 1);
0204 
0205         for (int i = 1 ; i <= CalSystem().monthsInYear(date) ; ++i)
0206         {
0207             month = QLocale().standaloneMonthName(i, QLocale::LongFormat);
0208             image = d->cSettings->image(i);
0209 
0210             if (!image.isEmpty())
0211             {
0212                 d->months.insert(i, image);
0213                 printList.append(month);
0214             }
0215         }
0216 
0217         if (d->months.isEmpty())
0218         {
0219             d->wPrintLabel->setText(QLatin1String("<qt>") +
0220                                   i18n("No valid images selected for months<br/>"
0221                                        "Click Back to select images") + QLatin1String("</qt>"));
0222             d->wFinishPage->setComplete(false);
0223         }
0224         else
0225         {
0226             int year = d->cSettings->year();
0227 
0228             QString extra;
0229 
0230             if ((CalSystem().month(QDate::currentDate()) >= 6    &&
0231                  CalSystem().year(QDate::currentDate()) == year) ||
0232                  CalSystem().year(QDate::currentDate()) > year)
0233             {
0234                 extra = QLatin1String("<br/><br/><b>") +
0235                         i18n("Please note that you are making a "
0236                              "calendar for<br/>the current year or a year in the "
0237                              "past.") + QLatin1String("</b>");
0238             }
0239 
0240             QString year_locale = QLocale().toString(date, QLatin1String("yyyy"));
0241 
0242             d->wPrintLabel->setText(i18n("Click Next to start Printing<br/><br/>"
0243                                        "Following months will be printed for year %1:<br/>", year_locale)
0244                                     + printList.join(QLatin1String(" - ")) + extra);
0245             d->wPrintLabel->setTextFormat(Qt::RichText);
0246 
0247             d->wFinishPage->setComplete(true);
0248         }
0249     }
0250 
0251     else if (current == d->wFinishPage)
0252     {
0253         d->calProgressUI.finishLabel->clear();
0254         d->calProgressUI.currentProgress->reset();
0255         d->calProgressUI.totalProgress->reset();
0256 
0257         button(QWizard::BackButton)->setEnabled(false);
0258         button(QWizard::NextButton)->setEnabled(false);
0259 
0260         // Set printer settings ---------------------------------------
0261 
0262         if (!d->printer)
0263         {
0264             d->printer = new QPrinter(d->cSettings->resolution());
0265         }
0266 
0267         CalParams& params = d->cSettings->params;
0268 
0269         // Orientation
0270 
0271         switch (params.imgPos)
0272         {
0273             case (CalParams::Top):
0274                 d->printer->setPageOrientation(QPageLayout::Portrait);
0275                 break;
0276 
0277             default:
0278                 d->printer->setPageOrientation(QPageLayout::Landscape);
0279                 break;
0280         }
0281 
0282         qCDebug(DIGIKAM_DPLUGIN_GENERIC_LOG) << "printing...";
0283 
0284         // PageSize
0285 
0286         d->printer->setPageSize(QPageSize(params.pageSize));
0287 
0288         QPrintDialog* const printDialog = new QPrintDialog(d->printer, this);
0289         printDialog->setWindowTitle(i18nc("@title:window", "Print Calendar"));
0290 
0291         if (printDialog->exec() == QDialog::Accepted)
0292         {
0293             print();
0294         }
0295         else
0296         {
0297             d->calProgressUI.finishLabel->setText(i18n("Printing Cancelled"));
0298             button(QWizard::BackButton)->setEnabled(true);
0299         }
0300 
0301         delete printDialog;
0302     }
0303 }
0304 
0305 void CalWizard::print()
0306 {
0307     d->calProgressUI.totalProgress->setMaximum(d->months.count());
0308     d->calProgressUI.totalProgress->setValue(0);
0309     d->calProgressUI.totalProgress->progressScheduled(i18n("Create calendar"), false, true);
0310     d->calProgressUI.totalProgress->progressThumbnailChanged(QIcon::fromTheme(QLatin1String("view-calendar")).pixmap(22, 22));
0311 
0312     if (d->printThread)
0313     {
0314         d->printThread->cancel();
0315         d->printThread->wait();
0316         delete d->printThread;
0317     }
0318 
0319     d->cSettings->clearSpecial();
0320 
0321 #ifdef HAVE_KCALENDAR
0322 
0323     d->cSettings->loadSpecial(QUrl::fromLocalFile(d->calEventsUI.ohUrlRequester->lineEdit()->text()), Qt::red);
0324     d->cSettings->loadSpecial(QUrl::fromLocalFile(d->calEventsUI.fhUrlRequester->lineEdit()->text()), Qt::darkGreen);
0325 
0326 #endif
0327 
0328     d->printThread = new CalPrinter(d->printer, d->months, this);
0329 
0330     connect(d->printThread, SIGNAL(pageChanged(int)),
0331             this, SLOT(updatePage(int)));
0332 
0333     connect(d->printThread, SIGNAL(pageChanged(int)),
0334             d->calProgressUI.totalProgress, SLOT(setValue(int)));
0335 
0336     connect(d->printThread, SIGNAL(totalBlocks(int)),
0337             d->calProgressUI.currentProgress, SLOT(setMaximum(int)));
0338 
0339     connect(d->printThread, SIGNAL(blocksFinished(int)),
0340             d->calProgressUI.currentProgress, SLOT(setValue(int)));
0341 
0342     d->calProgressUI.totalProgress->setMaximum(d->months.count());
0343     d->printThread->start();
0344 }
0345 
0346 void CalWizard::updatePage(int page)
0347 {
0348     const int year = d->cSettings->year();
0349     QDate date(year, 1, 1);
0350 
0351     if (page >= d->months.count())
0352     {
0353         printComplete();
0354         return;
0355     }
0356 
0357     int month = d->months.keys().at(page);
0358 
0359     d->calProgressUI.finishLabel->setText(i18n("Printing calendar page for %1 of %2",
0360                                           QLocale().standaloneMonthName(month, QLocale::LongFormat),
0361                                           QLocale().toString(date, QLatin1String("yyyy"))));
0362 }
0363 
0364 void CalWizard::printComplete()
0365 {
0366     d->calProgressUI.totalProgress->progressCompleted();
0367     button(QWizard::BackButton)->setEnabled(true);
0368     button(QWizard::NextButton)->setEnabled(true);
0369     d->calProgressUI.finishLabel->setText(i18n("Printing Complete"));
0370 }
0371 
0372 } // Namespace Digikam
0373 
0374 #include "moc_calwizard.cpp"