/*************************************************************************** * Copyright 2008 by Davide Bettio * * Copyright 2009 by John Layt * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, 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 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" #include #include #include #include #include #include #include #include #include static int kGetDay() { return QDate::currentDate().day(); } class CalendarWidget : public QGraphicsWidget { Q_OBJECT public: CalendarWidget(QGraphicsWidget *parent); void showToday(); private: QGraphicsLinearLayout* m_layout; Plasma::CalendarWidget* m_plasmacalendar; KCalendarWidget* m_nativewidget; }; CalendarWidget::CalendarWidget(QGraphicsWidget *parent) : QGraphicsWidget(parent), m_layout(nullptr), m_plasmacalendar(nullptr) { m_layout = new QGraphicsLinearLayout(Qt::Horizontal, this); m_plasmacalendar = new Plasma::CalendarWidget(this); m_plasmacalendar->setMinimumSize(QSize(300, 250)); m_plasmacalendar->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); m_nativewidget = m_plasmacalendar->nativeWidget(); // changing the date on the calendar does not make sense m_nativewidget->setSelectionMode(QCalendarWidget::NoSelection); m_nativewidget->setDateEditEnabled(false); m_layout->addItem(m_plasmacalendar); setLayout(m_layout); } void CalendarWidget::showToday() { m_nativewidget->showToday(); } CalendarApplet::CalendarApplet(QObject *parent, const QVariantList &args) : Plasma::PopupApplet(parent, args), m_calendarwidget(nullptr), m_theme(nullptr), m_day(-1) { setAspectRatioMode(Plasma::IgnoreAspectRatio); setPopupIcon("view-pim-calendar"); m_calendarwidget = new CalendarWidget(this); m_dateUpdater = new QTimer(this); m_dateUpdater->setInterval(60000); // 1min connect(m_dateUpdater, SIGNAL(timeout()), this, SLOT(slotCheckDate())); } void CalendarApplet::init() { slotCheckDate(); m_dateUpdater->start(); Plasma::ToolTipManager::self()->registerWidget(this); } void CalendarApplet::constraintsEvent(Plasma::Constraints constraints) { if (constraints & Plasma::FormFactorConstraint || constraints & Plasma::SizeConstraint) { paintIcon(); } } QGraphicsWidget *CalendarApplet::graphicsWidget() { return m_calendarwidget; } void CalendarApplet::popupEvent(const bool show) { if (show) { m_calendarwidget->showToday(); } } void CalendarApplet::slotCheckDate() { const int today = kGetDay(); if (today != m_day) { m_day = today; kDebug() << "updating calendar icon" << m_day << today; paintIcon(); } Plasma::ToolTipContent plasmatooltip; plasmatooltip.setMainText(i18n("Current Date")); const QString calendarstring = KGlobal::locale()->formatDate(QDateTime::currentDateTime().date()); QString calendartooltip; if (KSystemTimeZones::local() != KTimeZone::utc()) { calendartooltip.append(i18n("UTC: %1
", KGlobal::locale()->formatDate(QDateTime::currentDateTimeUtc().date()))); calendartooltip.append(i18n("Local: %1", calendarstring)); } else { calendartooltip.append(i18n("
%1
", calendarstring)); } plasmatooltip.setSubText(calendartooltip); plasmatooltip.setImage(KIcon("office-calendar")); Plasma::ToolTipManager::self()->setContent(this, plasmatooltip); } void CalendarApplet::paintIcon() { const int iconSize = qMin(size().width(), size().height()); if (iconSize <= 0) { return; } if (!m_theme) { m_theme = new Plasma::Svg(this); m_theme->setImagePath("calendar/mini-calendar"); m_theme->setContainsMultipleImages(true); } QPixmap icon(iconSize, iconSize); icon.fill(Qt::transparent); QPainter p(&icon); m_theme->paint(&p, icon.rect(), "mini-calendar"); QFont font = Plasma::Theme::defaultTheme()->font(Plasma::Theme::DefaultFont); p.setPen(Plasma::Theme::defaultTheme()->color(Plasma::Theme::ButtonTextColor)); font.setPixelSize(icon.height() / 2); p.setFont(font); p.drawText( icon.rect().adjusted(0, icon.height() / 4, 0, 0), Qt::AlignCenter, QString::number(kGetDay()) ); m_theme->resize(); p.end(); setPopupIcon(icon); } #include "moc_calendar.cpp" #include "calendar.moc"