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

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2003-11-03
0007  * Description : template selection for calendar.
0008  *
0009  * SPDX-FileCopyrightText: 2003-2005 by Renchi Raju <renchi dot raju at gmail dot com>
0010  * SPDX-FileCopyrightText: 2007-2008 by Orgad Shaneh <orgads at gmail dot com>
0011  * SPDX-FileCopyrightText: 2011      by Andi Clemens <andi dot clemens at googlemail dot com>
0012  * SPDX-FileCopyrightText: 2012      by Angelo Naselli <anaselli at linux dot it>
0013  * SPDX-FileCopyrightText: 2012-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
0014  *
0015  * SPDX-License-Identifier: GPL-2.0-or-later
0016  *
0017  * ============================================================ */
0018 
0019 #include "caltemplate.h"
0020 
0021 // Qt includes
0022 
0023 #include <QButtonGroup>
0024 
0025 // Local includes
0026 
0027 #include "calsettings.h"
0028 #include "calsystem.h"
0029 #include "calmonthwidget.h"
0030 #include "calpainter.h"
0031 #include "digikam_debug.h"
0032 
0033 namespace DigikamGenericCalendarPlugin
0034 {
0035 
0036 class Q_DECL_HIDDEN CalTemplate::Private
0037 {
0038 public:
0039 
0040     explicit Private()
0041       : MAX_MONTHS(13)
0042     {
0043     }
0044 
0045     const int                MAX_MONTHS;
0046     Ui::CalTemplate          ui;
0047     QVector<CalMonthWidget*> wVector;
0048 };
0049 
0050 CalTemplate::CalTemplate(const QList<QUrl>& urlList, QWidget* const parent)
0051     : QWidget(parent),
0052       d(new Private)
0053 {
0054     d->ui.setupUi(this);
0055 
0056     CalSettings* const settings = CalSettings::instance();
0057 
0058     // set initial settings
0059 
0060     settings->setPaperSize(d->ui.paperSizeCombo->currentText());
0061     settings->setDrawLines(d->ui.drawLinesCheckBox->isChecked());
0062     settings->setRatio(d->ui.ratioSlider->value());
0063     settings->setFont(d->ui.fontCombo->currentText());
0064     settings->setResolution(d->ui.resolutionCombo->currentText());
0065 
0066     d->ui.calendarWidget->recreate();
0067 
0068     connect(d->ui.yearSpin, SIGNAL(valueChanged(int)),
0069             this, SLOT(yearChanged(int)));
0070 
0071     int currentYear   = CalSystem().year(QDate::currentDate());
0072 
0073     QDate date        = CalSystem().date(currentYear, 1, 1);
0074     int months        = CalSystem().monthsInYear(date);
0075 
0076     // span the monthWidgets over 2 rows. inRow should usually be 6 or 7 (for 12 or 13 months)
0077 
0078     int inRow         = (months / 2) + ((months % 2) != 0);
0079     CalMonthWidget* w = nullptr;
0080 
0081     for (int i = 0; i < d->MAX_MONTHS; ++i)
0082     {
0083         w = new CalMonthWidget(d->ui.monthBox, i + 1);
0084 
0085         connect(w, SIGNAL(monthSelected(int)),
0086                 this, SLOT(monthChanged(int)));
0087 
0088         if (i < urlList.count())
0089         {
0090             w->setImage(urlList[i]);
0091         }
0092 
0093         if (i < months)
0094         {
0095             d->ui.monthBoxLayout->addWidget(w, i / inRow, i % inRow);
0096         }
0097         else
0098         {
0099             w->hide();
0100         }
0101 
0102         d->wVector.insert(i, w);
0103     }
0104 
0105     d->ui.yearSpin->setRange(CalSystem().year(CalSystem().earliestValidDate()) + 1,
0106                              CalSystem().year(CalSystem().latestValidDate()) - 1);
0107     d->ui.yearSpin->setValue(currentYear);
0108 
0109     QButtonGroup* const btnGrp = new QButtonGroup(d->ui.imagePosButtonGroup);
0110     btnGrp->addButton(d->ui.topRadio,   CalParams::Top);
0111     btnGrp->addButton(d->ui.leftRadio,  CalParams::Left);
0112     btnGrp->addButton(d->ui.rightRadio, CalParams::Right);
0113     btnGrp->setExclusive(true);
0114 
0115     connect(d->ui.paperSizeCombo, SIGNAL(currentTextChanged(QString)),
0116             settings, SLOT(setPaperSize(QString)));
0117 
0118     connect(d->ui.resolutionCombo, SIGNAL(currentTextChanged(QString)),
0119             settings, SLOT(setResolution(QString)));
0120 
0121 #if (QT_VERSION >= QT_VERSION_CHECK(5, 15, 0))
0122 
0123     connect(btnGrp, SIGNAL(idClicked(int)),
0124             settings, SLOT(setImagePos(int)));
0125 
0126 #else
0127 
0128     connect(btnGrp, SIGNAL(buttonClicked(int)),
0129             settings, SLOT(setImagePos(int)));
0130 
0131 #endif
0132 
0133     connect(d->ui.drawLinesCheckBox, SIGNAL(toggled(bool)),
0134             settings, SLOT(setDrawLines(bool)));
0135 
0136     connect(d->ui.ratioSlider, SIGNAL(valueChanged(int)),
0137             settings, SLOT(setRatio(int)));
0138 
0139     connect(d->ui.fontCombo, SIGNAL(currentTextChanged(QString)),
0140             settings, SLOT(setFont(QString)));
0141 
0142     connect(settings, SIGNAL(settingsChanged()),
0143             d->ui.calendarWidget, SLOT(recreate()));
0144 }
0145 
0146 CalTemplate::~CalTemplate()
0147 {
0148     delete d;
0149 }
0150 
0151 void CalTemplate::monthChanged(int m)
0152 {
0153   d->ui.calendarWidget->setCurrent(m);
0154 }
0155 
0156 void CalTemplate::yearChanged(int year)
0157 {
0158     int months;
0159     QDate date = CalSystem().date(year, 1, 1);
0160     QDate oldD = CalSystem().date(CalSettings::instance()->year(), 1, 1);
0161     months     = CalSystem().monthsInYear(date);
0162 
0163     if ((CalSystem().monthsInYear(oldD) != months) && !d->wVector.isEmpty())
0164     {
0165         int i;
0166 
0167         // hide the last months that are not present on the current year
0168 
0169         for (i = months ; (i < CalSystem().monthsInYear(oldD)) && (i < d->wVector.count()) ; ++i)
0170         {
0171             d->wVector.at(i)->hide();
0172         }
0173 
0174         // span the monthWidgets over 2 rows. inRow should usually be 6 or 7 (for 12 or 13 months)
0175 
0176         int inRow = (months / 2) + ((months % 2) != 0);
0177 
0178         // remove all the monthWidgets, then readd the needed ones
0179 
0180         for (i = 0 ; i < CalSystem().monthsInYear(oldD) ; ++i)
0181         {
0182             d->ui.monthBoxLayout->removeWidget(d->wVector.at(i));
0183         }
0184 
0185         for (i = 0 ; (i < months) && (i < d->wVector.count()) ; ++i)
0186         {
0187             d->ui.monthBoxLayout->addWidget(d->wVector.at(i), i / inRow, i % inRow);
0188 
0189             if (d->wVector.at(i)->isHidden())
0190             {
0191                 d->wVector.at(i)->show();
0192             }
0193 
0194             d->wVector.at(i)->update();
0195         }
0196     }
0197 
0198     CalSettings::instance()->setYear(year);
0199 }
0200 
0201 } // Namespace Digikam
0202 
0203 #include "moc_caltemplate.cpp"