plasma: luna applet optimization

Signed-off-by: Ivailo Monev <xakepa10@gmail.com>
This commit is contained in:
Ivailo Monev 2024-04-11 12:05:06 +03:00
parent 97a58ac306
commit 750acd115a
4 changed files with 20 additions and 13 deletions

View file

@ -95,14 +95,14 @@ CalendarApplet::CalendarApplet(QObject *parent, const QVariantList &args)
m_timer = new QTimer(this);
// 3sec to account for localtime changes for example
m_timer->setInterval(3000);
connect(m_timer, SIGNAL(timeout()), this, SLOT(slotCheckDate()));
connect(m_timer, SIGNAL(timeout()), this, SLOT(slotTimeout()));
}
void CalendarApplet::init()
{
slotCheckDate();
m_timer->start();
Plasma::ToolTipManager::self()->registerWidget(this);
slotTimeout();
m_timer->start();
}
QGraphicsWidget *CalendarApplet::graphicsWidget()
@ -137,7 +137,7 @@ void CalendarApplet::popupEvent(bool show)
}
}
void CalendarApplet::slotCheckDate()
void CalendarApplet::slotTimeout()
{
const int today = kGetDay();
if (today != m_day) {
@ -145,7 +145,7 @@ void CalendarApplet::slotCheckDate()
kDebug() << "updating calendar icon" << m_day << today;
paintIcon();
}
// affected by locale changes so always updated
Plasma::ToolTipContent plasmatooltip;
plasmatooltip.setMainText(i18n("Current Date"));
const QString calendarstring = KGlobal::locale()->formatDate(QDate::currentDate());

View file

@ -43,7 +43,7 @@ protected:
void popupEvent(bool show) final;
private slots:
void slotCheckDate();
void slotTimeout();
void slotConfigAccepted();
private:

View file

@ -43,9 +43,8 @@ static int kLunaPhase()
return roundmoonphase;
}
static QString kLunaPhaseString()
static QString kLunaPhaseString(const int phase)
{
const int phase = kLunaPhase();
switch (phase) {
case 0: {
return i18n("New Moon");
@ -104,7 +103,8 @@ static QString kLunaPhaseString()
LunaApplet::LunaApplet(QObject *parent, const QVariantList &args)
: Plasma::Applet(parent, args),
m_svg(nullptr),
m_timer(nullptr)
m_timer(nullptr),
m_phase(0)
{
KGlobal::locale()->insertCatalog("plasma_applet_luna");
setAspectRatioMode(Plasma::AspectRatioMode::Square);
@ -122,8 +122,9 @@ LunaApplet::LunaApplet(QObject *parent, const QVariantList &args)
void LunaApplet::init()
{
m_timer->start();
Plasma::ToolTipManager::self()->registerWidget(this);
slotTimeout();
m_timer->start();
}
void LunaApplet::paintInterface(QPainter *painter,
@ -135,7 +136,7 @@ void LunaApplet::paintInterface(QPainter *painter,
painter->setRenderHint(QPainter::SmoothPixmapTransform);
painter->setRenderHint(QPainter::Antialiasing);
m_svg->paint(painter, contentsRect, QString::number(kLunaPhase()));
m_svg->paint(painter, contentsRect, QString::number(m_phase));
}
void LunaApplet::constraintsEvent(Plasma::Constraints constraints)
@ -161,9 +162,14 @@ void LunaApplet::constraintsEvent(Plasma::Constraints constraints)
void LunaApplet::slotTimeout()
{
update();
const int phase = kLunaPhase();
if (phase != m_phase) {
m_phase = phase;
update();
}
// affected by locale changes so always updated
Plasma::ToolTipContent plasmatooltip;
plasmatooltip.setMainText(kLunaPhaseString());
plasmatooltip.setMainText(kLunaPhaseString(m_phase));
Plasma::ToolTipManager::self()->setContent(this, plasmatooltip);
}

View file

@ -45,6 +45,7 @@ private Q_SLOTS:
private:
Plasma::Svg* m_svg;
QTimer* m_timer;
int m_phase;
};
K_EXPORT_PLASMA_APPLET(luna, LunaApplet)