File indexing completed on 2024-04-28 04:18:50

0001 // vim: set tabstop=4 shiftwidth=4 expandtab:
0002 /*
0003 Gwenview: an image viewer
0004 Copyright 2008 Aurélien Gâteau <agateau@kde.org>
0005 
0006 This program is free software; you can redistribute it and/or
0007 modify it under the terms of the GNU General Public License
0008 as published by the Free Software Foundation; either version 2
0009 of the License, or (at your option) any later version.
0010 
0011 This program is distributed in the hope that it will be useful,
0012 but WITHOUT ANY WARRANTY; without even the implied warranty of
0013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0014 GNU General Public License for more details.
0015 
0016 You should have received a copy of the GNU General Public License
0017 along with this program; if not, write to the Free Software
0018 Foundation, Inc., 51 Franklin Street, Fifth Floor, Cambridge, MA 02110-1301, USA.
0019 
0020 */
0021 // Self
0022 #include "datewidget.h"
0023 
0024 // Qt
0025 #include <QDate>
0026 #include <QGuiApplication>
0027 #include <QHBoxLayout>
0028 #include <QLocale>
0029 
0030 // KF
0031 #include <KDatePicker>
0032 
0033 // Local
0034 #include <lib/statusbartoolbutton.h>
0035 
0036 namespace Gwenview
0037 {
0038 struct DateWidgetPrivate {
0039     DateWidget *q = nullptr;
0040 
0041     QDate mDate;
0042     KDatePicker *mDatePicker = nullptr;
0043     StatusBarToolButton *mPreviousButton = nullptr;
0044     StatusBarToolButton *mDateButton = nullptr;
0045     StatusBarToolButton *mNextButton = nullptr;
0046 
0047     void setupDatePicker()
0048     {
0049         mDatePicker = new KDatePicker;
0050         /* Use Qt::Tool instead of Qt::Window so that the bubble does not appear in the task bar */
0051         // mDatePicker->setWindowFlags(Qt::Tool | Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);
0052         mDatePicker->setWindowFlags(Qt::Popup);
0053         mDatePicker->hide();
0054         mDatePicker->setFrameStyle(QFrame::StyledPanel | QFrame::Raised);
0055 
0056         QObject::connect(mDatePicker, &KDatePicker::dateEntered, q, &DateWidget::slotDatePickerModified);
0057         QObject::connect(mDatePicker, &KDatePicker::dateSelected, q, &DateWidget::slotDatePickerModified);
0058     }
0059 
0060     void updateButton()
0061     {
0062         mDateButton->setText(QLocale().toString(mDate, QLocale::ShortFormat));
0063     }
0064 
0065     void adjustDate(int delta)
0066     {
0067         mDate = mDate.addDays(delta);
0068         updateButton();
0069         Q_EMIT q->dateChanged(mDate);
0070     }
0071 };
0072 
0073 DateWidget::DateWidget(QWidget *parent)
0074     : QWidget(parent)
0075     , d(new DateWidgetPrivate)
0076 {
0077     d->q = this;
0078 
0079     d->setupDatePicker();
0080 
0081     d->mPreviousButton = new StatusBarToolButton;
0082     d->mPreviousButton->setGroupPosition(StatusBarToolButton::GroupLeft);
0083     d->mPreviousButton->setIcon(
0084         QIcon::fromTheme(QGuiApplication::layoutDirection() == Qt::LeftToRight ? QStringLiteral("go-previous") : QStringLiteral("go-previous-symbolic-rtl")));
0085     connect(d->mPreviousButton, &StatusBarToolButton::clicked, this, &DateWidget::goToPrevious);
0086 
0087     d->mDateButton = new StatusBarToolButton;
0088     d->mDateButton->setGroupPosition(StatusBarToolButton::GroupCenter);
0089     connect(d->mDateButton, &StatusBarToolButton::clicked, this, &DateWidget::showDatePicker);
0090 
0091     d->mNextButton = new StatusBarToolButton;
0092     d->mNextButton->setGroupPosition(StatusBarToolButton::GroupRight);
0093     d->mNextButton->setIcon(
0094         QIcon::fromTheme(QGuiApplication::layoutDirection() == Qt::LeftToRight ? QStringLiteral("go-next") : QStringLiteral("go-next-symbolic-rtl")));
0095     connect(d->mNextButton, &StatusBarToolButton::clicked, this, &DateWidget::goToNext);
0096 
0097     auto layout = new QHBoxLayout(this);
0098     layout->setContentsMargins(0, 0, 0, 0);
0099     layout->setSpacing(0);
0100     layout->addWidget(d->mPreviousButton);
0101     layout->addWidget(d->mDateButton);
0102     layout->addWidget(d->mNextButton);
0103 }
0104 
0105 DateWidget::~DateWidget()
0106 {
0107     delete d->mDatePicker;
0108     delete d;
0109 }
0110 
0111 QDate DateWidget::date() const
0112 {
0113     return d->mDate;
0114 }
0115 
0116 void DateWidget::showDatePicker()
0117 {
0118     d->mDatePicker->setDate(d->mDate);
0119     d->mDatePicker->adjustSize();
0120     const QPoint pos = mapToGlobal(QPoint(0, -d->mDatePicker->height()));
0121     d->mDatePicker->move(pos);
0122     d->mDatePicker->show();
0123 }
0124 
0125 void DateWidget::slotDatePickerModified(const QDate &date)
0126 {
0127     d->mDatePicker->hide();
0128     d->mDate = date;
0129     Q_EMIT dateChanged(date);
0130 
0131     d->updateButton();
0132 }
0133 
0134 void DateWidget::goToPrevious()
0135 {
0136     d->adjustDate(-1);
0137 }
0138 
0139 void DateWidget::goToNext()
0140 {
0141     d->adjustDate(1);
0142 }
0143 
0144 } // namespace
0145 
0146 #include "moc_datewidget.cpp"