2014-11-13 19:30:51 +02:00
|
|
|
/*
|
|
|
|
* Copyright 2008,2010 Davide Bettio <davide.bettio@kdemail.net>
|
|
|
|
* Copyright 2009 John Layt <john@layt.net>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Library General Public License as
|
|
|
|
* published by the Free Software Foundation; either version 2, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this program; if not, write to the
|
|
|
|
* Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "calendar.h"
|
|
|
|
|
|
|
|
//Qt
|
2015-08-12 13:11:16 +03:00
|
|
|
#include <QtCore/qdatetime.h>
|
2014-11-13 19:30:51 +02:00
|
|
|
#include <QtCore/QTimer>
|
|
|
|
#include <QtGui/QApplication>
|
2015-08-12 13:11:16 +03:00
|
|
|
#include <QtGui/qgraphicssceneevent.h>
|
2014-11-13 19:30:51 +02:00
|
|
|
#include <QtGui/QGraphicsGridLayout>
|
|
|
|
#include <QtGui/QGraphicsLinearLayout>
|
|
|
|
#include <QtGui/QGraphicsProxyWidget>
|
|
|
|
#include <QtGui/QGraphicsView>
|
|
|
|
#include <QtGui/QLabel>
|
|
|
|
#include <QtGui/QMenu>
|
|
|
|
#include <QtGui/QSpinBox>
|
|
|
|
#include <QtGui/QToolButton>
|
|
|
|
#include <QtGui/QDesktopWidget>
|
|
|
|
|
|
|
|
//KDECore
|
|
|
|
#include <KCalendarSystem>
|
2023-07-23 11:36:30 +03:00
|
|
|
#include <KCalendarWidget>
|
2014-11-13 19:30:51 +02:00
|
|
|
#include <KDebug>
|
|
|
|
#include <KGlobal>
|
|
|
|
#include <KIcon>
|
|
|
|
#include <KLineEdit>
|
|
|
|
#include <KLocale>
|
|
|
|
#include <KConfigDialog>
|
|
|
|
#include <KConfigGroup>
|
2023-07-23 20:14:28 +03:00
|
|
|
#include <KNotification>
|
2014-11-13 19:30:51 +02:00
|
|
|
|
|
|
|
//Plasma
|
|
|
|
#include <Plasma/Label>
|
|
|
|
#include <Plasma/LineEdit>
|
|
|
|
#include <Plasma/Separator>
|
|
|
|
#include <Plasma/SpinBox>
|
|
|
|
#include <Plasma/TextBrowser>
|
|
|
|
#include <Plasma/ToolButton>
|
2023-07-23 11:36:30 +03:00
|
|
|
#include <Plasma/CalendarWidget>
|
2014-11-13 19:30:51 +02:00
|
|
|
#include <Plasma/DataEngine>
|
|
|
|
|
|
|
|
#include "wheelytoolbutton.h"
|
2023-07-23 11:36:30 +03:00
|
|
|
#include "ui_calendarConfig.h"
|
2014-11-13 19:30:51 +02:00
|
|
|
|
|
|
|
namespace Plasma
|
|
|
|
{
|
|
|
|
|
|
|
|
static const int s_yearWidgetIndex = 3;
|
|
|
|
|
|
|
|
class CalendarPrivate
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
CalendarPrivate(Calendar *calendar)
|
|
|
|
: q(calendar),
|
2023-07-23 11:36:30 +03:00
|
|
|
calendarWidget(nullptr),
|
|
|
|
layout(nullptr),
|
|
|
|
calendar(nullptr),
|
|
|
|
currentDate(QDate::currentDate()),
|
|
|
|
automaticUpdates(true)
|
2014-11-13 19:30:51 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void init(const QDate &date = QDate());
|
|
|
|
void popupMonthsMenu();
|
|
|
|
void updateSize();
|
|
|
|
|
|
|
|
Calendar *q;
|
2023-07-23 11:36:30 +03:00
|
|
|
Plasma::CalendarWidget *calendarWidget;
|
2014-11-13 19:30:51 +02:00
|
|
|
QGraphicsLinearLayout *layout;
|
2023-07-23 11:36:30 +03:00
|
|
|
const KCalendarSystem *calendar;
|
|
|
|
QDate currentDate;
|
|
|
|
bool automaticUpdates;
|
|
|
|
Ui::calendarConfig calendarConfigUi;
|
2014-11-13 19:30:51 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
Calendar::Calendar(const QDate &date, QGraphicsWidget *parent)
|
|
|
|
: QGraphicsWidget(parent),
|
|
|
|
d(new CalendarPrivate(this))
|
|
|
|
{
|
|
|
|
d->init(date);
|
|
|
|
}
|
|
|
|
|
|
|
|
Calendar::Calendar(QGraphicsWidget *parent)
|
|
|
|
: QGraphicsWidget(parent),
|
|
|
|
d(new CalendarPrivate(this))
|
|
|
|
{
|
|
|
|
d->init();
|
|
|
|
}
|
|
|
|
|
|
|
|
Calendar::~Calendar()
|
|
|
|
{
|
|
|
|
delete d;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CalendarPrivate::init(const QDate &initialDate)
|
|
|
|
{
|
|
|
|
q->setCacheMode(QGraphicsItem::DeviceCoordinateCache);
|
|
|
|
|
|
|
|
layout = new QGraphicsLinearLayout(Qt::Horizontal, q);
|
2023-07-23 11:36:30 +03:00
|
|
|
|
|
|
|
calendarWidget = new CalendarWidget(q);
|
|
|
|
calendarWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
|
|
|
|
QObject::connect(calendarWidget, SIGNAL(clicked(QDate)), q, SLOT(dateUpdated()));
|
|
|
|
QObject::connect(calendarWidget, SIGNAL(activated(QDate)), q, SLOT(dateUpdated()));
|
|
|
|
|
|
|
|
layout->addItem(calendarWidget);
|
2014-11-13 19:30:51 +02:00
|
|
|
|
|
|
|
q->setDate(initialDate);
|
|
|
|
updateSize();
|
|
|
|
}
|
|
|
|
|
2023-07-23 11:36:30 +03:00
|
|
|
void Calendar::showEvent(QShowEvent * event)
|
2014-11-13 19:30:51 +02:00
|
|
|
{
|
2023-07-23 11:36:30 +03:00
|
|
|
if (d->automaticUpdates) {
|
|
|
|
d->currentDate = QDate::currentDate();
|
|
|
|
}
|
|
|
|
QGraphicsWidget::showEvent(event);
|
2014-11-13 19:30:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Calendar::setCalendar(int newCalendarType)
|
|
|
|
{
|
2023-07-23 11:36:30 +03:00
|
|
|
d->calendar = KCalendarSystem::create(static_cast<KLocale::CalendarSystem>(newCalendarType));
|
2014-11-13 19:30:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Calendar::setCalendar(const KCalendarSystem *newCalendar)
|
|
|
|
{
|
2023-07-23 11:36:30 +03:00
|
|
|
d->calendar = newCalendar;
|
|
|
|
d->calendarWidget->nativeWidget()->setCalendar(newCalendar);
|
2014-11-13 19:30:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const KCalendarSystem *Calendar::calendar() const
|
|
|
|
{
|
2023-07-23 11:36:30 +03:00
|
|
|
if (d->calendar) {
|
|
|
|
return d->calendar;
|
|
|
|
}
|
|
|
|
return KGlobal::locale()->calendar();
|
2014-11-13 19:30:51 +02:00
|
|
|
}
|
|
|
|
|
2023-07-23 11:36:30 +03:00
|
|
|
void Calendar::setAutomaticUpdateEnabled(bool automatic)
|
2014-11-13 19:30:51 +02:00
|
|
|
{
|
2023-07-23 11:36:30 +03:00
|
|
|
d->automaticUpdates = automatic;
|
2014-11-13 19:30:51 +02:00
|
|
|
}
|
2023-07-23 11:36:30 +03:00
|
|
|
|
|
|
|
bool Calendar::isAutomaticUpdateEnabled() const
|
2014-11-13 19:30:51 +02:00
|
|
|
{
|
2023-07-23 11:36:30 +03:00
|
|
|
return d->automaticUpdates;
|
2014-11-13 19:30:51 +02:00
|
|
|
}
|
|
|
|
|
2023-07-23 11:36:30 +03:00
|
|
|
void Calendar::setDate(const QDate &toDate)
|
2014-11-13 19:30:51 +02:00
|
|
|
{
|
2023-07-23 11:36:30 +03:00
|
|
|
// New date must be valid in the current calendar system
|
|
|
|
if (!calendar()->isValid(toDate)) {
|
2023-07-23 20:14:28 +03:00
|
|
|
if (!toDate.isNull()) {
|
|
|
|
KNotification::beep();
|
|
|
|
}
|
2023-07-23 11:36:30 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-07-24 20:32:32 +03:00
|
|
|
// NOTE: this method is called by Plasma::ClockApplet::popupEvent() on show so updating as if
|
|
|
|
// show event ocurred
|
|
|
|
if (d->automaticUpdates) {
|
|
|
|
d->currentDate = QDate::currentDate();
|
2023-07-23 11:36:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
d->calendarWidget->setSelectedDate(toDate);
|
2014-11-13 19:30:51 +02:00
|
|
|
}
|
|
|
|
|
2023-07-23 11:36:30 +03:00
|
|
|
QDate Calendar::date() const
|
2014-11-13 19:30:51 +02:00
|
|
|
{
|
2023-07-23 11:36:30 +03:00
|
|
|
return d->calendarWidget->selectedDate();
|
2014-11-13 19:30:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Calendar::setCurrentDate(const QDate &date)
|
|
|
|
{
|
2023-07-23 11:36:30 +03:00
|
|
|
d->currentDate = date;
|
2014-11-13 19:30:51 +02:00
|
|
|
}
|
|
|
|
|
2023-07-23 11:36:30 +03:00
|
|
|
QDate Calendar::currentDate() const
|
2014-11-13 19:30:51 +02:00
|
|
|
{
|
2023-07-23 11:36:30 +03:00
|
|
|
return d->currentDate;
|
2014-11-13 19:30:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Calendar::writeConfiguration(KConfigGroup cg)
|
|
|
|
{
|
2023-07-23 11:36:30 +03:00
|
|
|
const int calendarType = (d->calendar ? d->calendar->calendarSystem() : -1);
|
|
|
|
cg.writeEntry("calendarType", calendarType);
|
2014-11-13 19:30:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Calendar::createConfigurationInterface(KConfigDialog *parent)
|
|
|
|
{
|
2023-07-23 11:36:30 +03:00
|
|
|
QWidget *calendarConfigWidget = new QWidget();
|
|
|
|
d->calendarConfigUi.setupUi(calendarConfigWidget);
|
|
|
|
parent->addPage(calendarConfigWidget, i18n("Calendar"), "view-pim-calendar");
|
|
|
|
|
|
|
|
const QList<KLocale::CalendarSystem> calendars = KCalendarSystem::calendarSystemsList();
|
|
|
|
d->calendarConfigUi.calendarComboBox->addItem( i18n("Local"), QVariant( -1 ) );
|
|
|
|
for (int i = 0; i < calendars.count(); ++i) {
|
|
|
|
d->calendarConfigUi.calendarComboBox->addItem( KCalendarSystem::calendarLabel( calendars.at(i) ), QVariant( calendars.at(i) ) );
|
|
|
|
}
|
|
|
|
const int calendarType = (d->calendar ? d->calendar->calendarSystem() : -1);
|
|
|
|
d->calendarConfigUi.calendarComboBox->setCurrentIndex(d->calendarConfigUi.calendarComboBox->findData( QVariant( calendarType ) ) );
|
|
|
|
|
|
|
|
connect(d->calendarConfigUi.calendarComboBox, SIGNAL(activated(int)), parent, SLOT(settingsModified()));
|
2014-11-13 19:30:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Calendar::configAccepted(KConfigGroup cg)
|
|
|
|
{
|
2023-07-23 11:36:30 +03:00
|
|
|
setCalendar(d->calendarConfigUi.calendarComboBox->itemData(d->calendarConfigUi.calendarComboBox->currentIndex()).toInt());
|
|
|
|
writeConfiguration(cg);
|
2014-11-13 19:30:51 +02:00
|
|
|
applyConfiguration(cg);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CalendarPrivate::updateSize()
|
|
|
|
{
|
2023-07-24 00:46:18 +03:00
|
|
|
QSize minSize = QSize(300, 250);
|
|
|
|
QSize prefSize = calendarWidget ? calendarWidget->size().toSize() : QSize(300, 250);
|
2014-11-13 19:30:51 +02:00
|
|
|
|
|
|
|
q->setMinimumSize(minSize);
|
|
|
|
q->setPreferredSize(prefSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Calendar::applyConfiguration(KConfigGroup cg)
|
|
|
|
{
|
2023-07-23 11:36:30 +03:00
|
|
|
setCalendar(cg.readEntry("calendarType", -1));
|
2014-11-13 19:30:51 +02:00
|
|
|
d->updateSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Calendar::dateUpdated()
|
|
|
|
{
|
|
|
|
emit dateChanged(date());
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-02-27 09:28:46 +00:00
|
|
|
#include "moc_calendar.cpp"
|