File indexing completed on 2024-05-05 17:33:58

0001 /*
0002  *   SPDX-FileCopyrightText: 2012 Matthias Fuchs <mat69@gmx.net>
0003  *
0004  *   SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #include "stripselector.h"
0008 #include "comicdata.h"
0009 #include "stripselector_p.h"
0010 
0011 #include <KDatePicker>
0012 #include <KLocalizedString>
0013 #include <QDialog>
0014 #include <QDialogButtonBox>
0015 #include <QInputDialog>
0016 #include <QSpinBox>
0017 
0018 #include <QLabel>
0019 #include <QScopedPointer>
0020 #include <QTimer>
0021 #include <QVBoxLayout>
0022 
0023 // NOTE based on GotoPageDialog KDE/kdegraphics/okular/part.cpp
0024 // BEGIN choose a strip dialog
0025 class ChooseStripNumDialog : public QDialog
0026 {
0027 public:
0028     ChooseStripNumDialog(QWidget *parent, int current, int min, int max)
0029         : QDialog(parent)
0030     {
0031         setWindowTitle(i18nc("@title:window", "Go to Strip"));
0032 
0033         QVBoxLayout *topLayout = new QVBoxLayout(this);
0034         topLayout->setContentsMargins(0, 0, 0, 0);
0035         numInput = new QSpinBox(this);
0036         numInput->setRange(min, max);
0037         numInput->setValue(current);
0038 
0039         QLabel *label = new QLabel(i18nc("@label:spinbox", "&Strip number:"), this);
0040         label->setBuddy(numInput);
0041         topLayout->addWidget(label);
0042         topLayout->addWidget(numInput);
0043         // A little bit extra space
0044         topLayout->addStretch(10);
0045 
0046         QDialogButtonBox *buttonBox = new QDialogButtonBox(this);
0047         buttonBox->setStandardButtons(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
0048         connect(buttonBox, &QDialogButtonBox::accepted, this, &ChooseStripNumDialog::accept);
0049         connect(buttonBox, &QDialogButtonBox::rejected, this, &ChooseStripNumDialog::reject);
0050         topLayout->addWidget(buttonBox);
0051 
0052         numInput->setFocus();
0053     }
0054 
0055     int getStripNumber() const
0056     {
0057         return numInput->value();
0058     }
0059 
0060 protected:
0061     QSpinBox *numInput;
0062 };
0063 // END choose a strip dialog
0064 
0065 StripSelector::StripSelector(QObject *parent)
0066     : QObject(parent)
0067 {
0068 }
0069 
0070 StripSelector::~StripSelector()
0071 {
0072 }
0073 
0074 StripSelector *StripSelectorFactory::create(IdentifierType type)
0075 {
0076     switch (type) {
0077     case IdentifierType::NumberIdentifier:
0078         return new NumberStripSelector();
0079     case IdentifierType::DateIdentifier:
0080         return new DateStripSelector();
0081     case IdentifierType::StringIdentifier:
0082         return new StringStripSelector();
0083     }
0084 
0085     return nullptr;
0086 }
0087 
0088 StringStripSelector::StringStripSelector(QObject *parent)
0089     : StripSelector(parent)
0090 {
0091 }
0092 
0093 StringStripSelector::~StringStripSelector()
0094 {
0095 }
0096 
0097 void StringStripSelector::select(const ComicData &currentStrip)
0098 {
0099     bool ok;
0100     const QString strip = QInputDialog::getText(nullptr,
0101                                                 i18nc("@title:window", "Go to Strip"),
0102                                                 i18nc("@label:textbox", "Strip identifier:"),
0103                                                 QLineEdit::Normal,
0104                                                 currentStrip.current(),
0105                                                 &ok);
0106     if (ok) {
0107         Q_EMIT stripChosen(strip);
0108     }
0109     deleteLater();
0110 }
0111 
0112 NumberStripSelector::NumberStripSelector(QObject *parent)
0113     : StripSelector(parent)
0114 {
0115 }
0116 
0117 NumberStripSelector::~NumberStripSelector()
0118 {
0119 }
0120 
0121 void NumberStripSelector::select(const ComicData &currentStrip)
0122 {
0123     QScopedPointer<ChooseStripNumDialog> pageDialog(
0124         new ChooseStripNumDialog(nullptr, currentStrip.current().toInt(), currentStrip.firstStripNum(), currentStrip.maxStripNum()));
0125     if (pageDialog->exec() == QDialog::Accepted) {
0126         Q_EMIT stripChosen(QString::number(pageDialog->getStripNumber()));
0127     }
0128     deleteLater();
0129 }
0130 
0131 DateStripSelector::DateStripSelector(QObject *parent)
0132     : StripSelector(parent)
0133 {
0134 }
0135 
0136 DateStripSelector::~DateStripSelector()
0137 {
0138 }
0139 
0140 void DateStripSelector::select(const ComicData &currentStrip)
0141 {
0142     mFirstIdentifierSuffix = currentStrip.first();
0143 
0144     KDatePicker *calendar = new KDatePicker;
0145     calendar->setAttribute(Qt::WA_DeleteOnClose); // to have destroyed emitted upon closing
0146     calendar->setMinimumSize(calendar->sizeHint());
0147     calendar->setDate(QDate::fromString(currentStrip.current(), QStringLiteral("yyyy-MM-dd")));
0148 
0149     connect(calendar, &KDatePicker::dateSelected, this, &DateStripSelector::slotChosenDay);
0150     connect(calendar, &KDatePicker::dateEntered, this, &DateStripSelector::slotChosenDay);
0151 
0152     // only delete this if the dialog got closed
0153     connect(calendar, &KDatePicker::destroyed, this, &DateStripSelector::deleteLater);
0154     calendar->show();
0155 }
0156 
0157 void DateStripSelector::slotChosenDay(const QDate &date)
0158 {
0159     if (date <= QDate::currentDate()) {
0160         QDate temp = QDate::fromString(mFirstIdentifierSuffix, QStringLiteral("yyyy-MM-dd"));
0161         // only update if date >= first strip date, or if there is no first
0162         // strip date
0163         if (temp.isValid() || date >= temp) {
0164             Q_EMIT stripChosen(date.toString(QStringLiteral("yyyy-MM-dd")));
0165         }
0166     }
0167 }