plasma: rework calendar applet to make it behave like read-only widget

while navigation trough the calendar (which is still possible) does make
sense selecting a date does not, it is like the clock applet which serves
information purpose for the most part (the digital clock applet has a
feature to copy the current date and time tho)

Signed-off-by: Ivailo Monev <xakepa10@gmail.com>
This commit is contained in:
Ivailo Monev 2024-04-03 15:26:39 +03:00
parent b9dba4de56
commit 61c5251a1e
3 changed files with 103 additions and 98 deletions

View file

@ -1,10 +1,5 @@
project(plasma-applet-calendar) project(plasma-applet-calendar)
include_directories(
# for plasmaclock_export.h
${CMAKE_BINARY_DIR}/libs/plasmaclock
)
set(calendar_SRCS set(calendar_SRCS
calendar.cpp calendar.cpp
) )
@ -12,8 +7,6 @@ set(calendar_SRCS
kde4_add_plugin(plasma_applet_calendar ${calendar_SRCS}) kde4_add_plugin(plasma_applet_calendar ${calendar_SRCS})
target_link_libraries(plasma_applet_calendar target_link_libraries(plasma_applet_calendar
KDE4::plasma KDE4::plasma
KDE4::kio
plasmaclock
) )
install( install(

View file

@ -20,60 +20,102 @@
#include "calendar.h" #include "calendar.h"
#include <QGraphicsLayout> #include <QGraphicsLinearLayout>
#include <QPainter> #include <QPainter>
#include <QTimer>
#include <KDebug>
#include <KSystemTimeZones>
#include <KConfigDialog>
#include <KConfigGroup>
#include <Plasma/Svg>
#include <Plasma/Theme> #include <Plasma/Theme>
#include <Plasma/CalendarWidget>
#include <KCalendarWidget>
#include <KDebug>
// TODO: option for this, what if I want to show the UTC date?
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) CalendarApplet::CalendarApplet(QObject *parent, const QVariantList &args)
: Plasma::PopupApplet(parent, args), : Plasma::PopupApplet(parent, args),
m_calendarWidget(0), m_calendarwidget(nullptr),
m_theme(0) m_theme(nullptr),
m_day(-1)
{ {
KGlobal::locale()->insertCatalog("libplasmaclock");
setAspectRatioMode(Plasma::IgnoreAspectRatio); setAspectRatioMode(Plasma::IgnoreAspectRatio);
setCacheMode(DeviceCoordinateCache); setPopupIcon("view-pim-calendar");
m_dateUpdater = new QTimer(this);
m_dateUpdater->setSingleShot(true);
connect(m_dateUpdater, SIGNAL(timeout()), this, SLOT(updateDate()));
}
CalendarApplet::~CalendarApplet() 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() void CalendarApplet::init()
{ {
setPopupIcon("view-pim-calendar"); slotCheckDate();
m_calendarWidget = new Plasma::Calendar(this); m_dateUpdater->start();
updateDate();
configChanged();
setFocusPolicy(Qt::StrongFocus);
}
void CalendarApplet::focusInEvent(QFocusEvent* event)
{
Q_UNUSED(event);
m_calendarWidget->setFlag(QGraphicsItem::ItemIsFocusable);
m_calendarWidget->setFocus();
}
QGraphicsWidget *CalendarApplet::graphicsWidget()
{
return m_calendarWidget;
} }
void CalendarApplet::constraintsEvent(Plasma::Constraints constraints) void CalendarApplet::constraintsEvent(Plasma::Constraints constraints)
{ {
if ((constraints|Plasma::FormFactorConstraint || constraints|Plasma::SizeConstraint) && if (constraints & Plasma::FormFactorConstraint || constraints & Plasma::SizeConstraint) {
layout()->itemAt(0) != m_calendarWidget) { 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(); paintIcon();
} }
} }
@ -86,14 +128,13 @@ void CalendarApplet::paintIcon()
return; return;
} }
QPixmap icon(iconSize, iconSize);
if (!m_theme) { if (!m_theme) {
m_theme = new Plasma::Svg(this); m_theme = new Plasma::Svg(this);
m_theme->setImagePath("calendar/mini-calendar"); m_theme->setImagePath("calendar/mini-calendar");
m_theme->setContainsMultipleImages(true); m_theme->setContainsMultipleImages(true);
} }
QPixmap icon(iconSize, iconSize);
icon.fill(Qt::transparent); icon.fill(Qt::transparent);
QPainter p(&icon); QPainter p(&icon);
@ -103,32 +144,14 @@ void CalendarApplet::paintIcon()
p.setPen(Plasma::Theme::defaultTheme()->color(Plasma::Theme::ButtonTextColor)); p.setPen(Plasma::Theme::defaultTheme()->color(Plasma::Theme::ButtonTextColor));
font.setPixelSize(icon.height() / 2); font.setPixelSize(icon.height() / 2);
p.setFont(font); p.setFont(font);
p.drawText(icon.rect().adjusted(0, icon.height()/4, 0, 0), Qt::AlignCenter, p.drawText(
QString::number(m_calendarWidget->date().day())); icon.rect().adjusted(0, icon.height() / 4, 0, 0), Qt::AlignCenter,
QString::number(kGetDay())
);
m_theme->resize(); m_theme->resize();
p.end(); p.end();
setPopupIcon(icon); setPopupIcon(icon);
} }
void CalendarApplet::updateDate()
{
QDateTime now = QDateTime::currentDateTime();
static const int secsInDay = 24 * 60 * 60;
const int sinceEpoch = now.toTime_t() + KSystemTimeZones::local().currentOffset();
const int updateIn = (secsInDay) - (sinceEpoch % secsInDay);
if (updateIn > secsInDay - 60) {
// after midnight, we try and update right away again in case of odd clock drifting
// that could cause us to miss (or delay) the date change
m_dateUpdater->setInterval(60 * 1000);
} else if (updateIn < m_dateUpdater->interval()) {
m_dateUpdater->setInterval(updateIn * 1000);
} else {
// update once an hour
m_dateUpdater->setInterval(60 * 60 * 1000);
}
//kDebug() << "updating in" << m_dateUpdater->interval();
m_dateUpdater->start();
paintIcon();
}
#include "moc_calendar.cpp" #include "moc_calendar.cpp"
#include "calendar.moc"

View file

@ -21,44 +21,33 @@
#ifndef CALENDARTEST_H #ifndef CALENDARTEST_H
#define CALENDARTEST_H #define CALENDARTEST_H
#include <Plasma/PopupApplet>
#include <Plasma/Label>
#include <QTimer> #include <QTimer>
#include <Plasma/PopupApplet>
#include <Plasma/Svg>
#include "plasmaclock/calendar.h" class CalendarWidget;
namespace Plasma
{
class Svg;
}
class CalendarApplet : public Plasma::PopupApplet class CalendarApplet : public Plasma::PopupApplet
{ {
Q_OBJECT Q_OBJECT
public: public:
CalendarApplet(QObject *parent, const QVariantList &args); CalendarApplet(QObject *parent, const QVariantList &args);
~CalendarApplet();
void init(); void init() final;
void constraintsEvent(Plasma::Constraints constraints); void constraintsEvent(Plasma::Constraints constraints) final;
QGraphicsWidget *graphicsWidget() final;
void popupEvent(bool show) final;
/** private slots:
* The widget that displays the calendar. void slotCheckDate();
*/
QGraphicsWidget *graphicsWidget();
protected: private:
void focusInEvent(QFocusEvent * event); void paintIcon();
protected slots: CalendarWidget *m_calendarwidget;
void updateDate(); Plasma::Svg *m_theme;
void paintIcon(); QTimer *m_dateUpdater;
int m_day;
private:
Plasma::Calendar *m_calendarWidget;
Plasma::Svg *m_theme;
QTimer *m_dateUpdater;
}; };
K_EXPORT_PLASMA_APPLET(calendar, CalendarApplet) K_EXPORT_PLASMA_APPLET(calendar, CalendarApplet)