From 0ac1591f4727a5770dd25ae1decc9a141993862c Mon Sep 17 00:00:00 2001 From: Ken Moore Date: Wed, 16 May 2018 03:54:25 -0400 Subject: Get the Clock plugin all finished up. Now the text should format properly, and we have a single-instanced calendar menu popup for all clock plugins now. --- .../src-desktop/src-widgets/plugins/CalendarMenu.h | 40 ++++++++++++++++++++ .../src-desktop/src-widgets/plugins/ClockPlugin.h | 44 ++++++++++++++++------ .../src-desktop/src-widgets/plugins/plugins.pri | 1 + 3 files changed, 73 insertions(+), 12 deletions(-) create mode 100644 src-qt5/core/lumina-desktop-unified/src-desktop/src-widgets/plugins/CalendarMenu.h (limited to 'src-qt5/core/lumina-desktop-unified/src-desktop/src-widgets/plugins') diff --git a/src-qt5/core/lumina-desktop-unified/src-desktop/src-widgets/plugins/CalendarMenu.h b/src-qt5/core/lumina-desktop-unified/src-desktop/src-widgets/plugins/CalendarMenu.h new file mode 100644 index 00000000..6bf3fefe --- /dev/null +++ b/src-qt5/core/lumina-desktop-unified/src-desktop/src-widgets/plugins/CalendarMenu.h @@ -0,0 +1,40 @@ +//=========================================== +// Lumina-desktop source code +// Copyright (c) 2018, Ken Moore +// Available under the 3-clause BSD license +// See the LICENSE file for full details +//=========================================== +// Simple menu popup with a calendar in it +//=========================================== +#ifndef _LUMINA_DESKTOP_WIDGET_MENU_CALENDAR_H +#define _LUMINA_DESKTOP_WIDGET_MENU_CALENDAR_H + +#include +#include +#include +#include + +class CalendarMenu : public QMenu { + Q_OBJECT +private: + QCalendarWidget *cal; + QWidgetAction *WA; +public: + CalendarMenu(){ + cal = new QCalendarWidget(); + WA = new QWidgetAction(this); + WA->setDefaultWidget(cal); + this->addAction(WA); + connect(this, SIGNAL(aboutToShow()), this, SLOT(resetDate()) ); + } + ~CalendarMenu(){ + cal->deleteLater(); + } + +private slots: + void resetDate(){ + cal->setSelectedDate(QDate::currentDate()); + } + +}; +#endif diff --git a/src-qt5/core/lumina-desktop-unified/src-desktop/src-widgets/plugins/ClockPlugin.h b/src-qt5/core/lumina-desktop-unified/src-desktop/src-widgets/plugins/ClockPlugin.h index 7e7b06c7..7bf48cc2 100644 --- a/src-qt5/core/lumina-desktop-unified/src-desktop/src-widgets/plugins/ClockPlugin.h +++ b/src-qt5/core/lumina-desktop-unified/src-desktop/src-widgets/plugins/ClockPlugin.h @@ -9,6 +9,7 @@ #include #include +#include class ClockPlugin : public PluginButton{ Q_OBJECT @@ -24,20 +25,32 @@ private slots: //qDebug() << "Clock: Update Time:" << textRows << text << text.split(" "); if(textRows>1){ QStringList textL = text.split(" "); //natural breaks + int max = button->width() + (isVertical ? -1 : 200); //button->height() for vertical panels once text rotation is worked out while(textL.length()!=textRows){ if(textL.length() > textRows){ //Need to get a bit more complicated - join the shorter sections together - //Look for an am/pm section and combine that with the previous one - int index = textL.lastIndexOf(QRegExp("(AM|am|PM|pm)")); - //qDebug() << "Got AM/PM index:" << index; - if(index>0){ textL[index-1] = textL[index-1]+" "+textL[index]; textL.removeAt(index); } - else{ - //TO-DO - break; + //Find the smallest section and merge it into the previous one + //Note: This is horribly inefficient, but due to the inherently short length of textL it is acceptable + int min = -1; int index = -1; + for(int i=1; ifontMetrics().width(textL[i-1]+" "+textL[i]); + int tmp = button->fontMetrics().width(textL[i]); + if( max>combined && (min<0 || tmp0){ textL[index-1] = textL[index-1]+" "+textL[index]; textL.removeAt(index); } + else{ break; } }else{ - //Need to get a lot more complicated - need to break up sections based on widget width - // TO-DO + //Need to get a lot more complicated - need to break up sections mid-word based on widget width + for(int i=0; ifontMetrics().width(textL[i]); + if(tmp>max){ + //qDebug() << "CLOCK:" << i << tmp << max << textL[i]; + QString tmps = button->fontMetrics().elidedText(textL[i], Qt::ElideRight, max).section("...",0,-2); //remove the three dots at end + QString rem = textL[i].remove(tmps); + textL[i] = tmps; textL.insert(i+1, rem); + } + } + //Now go ahead and break the main loop - text all fits width-wise now break; } } //end of loop @@ -50,10 +63,16 @@ private slots: public: ClockPlugin(QWidget *parent, QString id, bool panelplug) : PluginButton(parent, id, panelplug){ connect(RootDesktopObject::instance(), SIGNAL(currentTimeChanged()), this, SLOT(updateTime()) ); - /*QFont tmp = button->font(); + QFont tmp = button->font(); tmp.setBold(true); - button->setFont( tmp );*/ + button->setFont( tmp ); textRows = 1; + //Setup the popup menu + static QMenu *calMenu = 0; + if(calMenu == 0){ + calMenu = new CalendarMenu(); + } + button->setMenu(calMenu); QTimer::singleShot(0, this, SLOT(updateTime()) ); } ~ClockPlugin(){ } @@ -63,11 +82,12 @@ protected: Plugin::resizeEvent(ev); //Re-calculate the text sizing for display int tmp = button->height() / button->fontMetrics().height(); + if(isVertical && isPanelPlugin){ tmp+=5; } //non-rotated text - need more columns //qDebug() << "Got height/font ratio:" << tmp; if(tmp!=textRows){ textRows = tmp; if(textRows<1){ textRows = 1; } - QTimer::singleShot(0, this, SLOT(updateTime()) ); + if(button->isVisible() && !button->text().isEmpty()){ QTimer::singleShot(0, this, SLOT(updateTime()) ); } } } }; diff --git a/src-qt5/core/lumina-desktop-unified/src-desktop/src-widgets/plugins/plugins.pri b/src-qt5/core/lumina-desktop-unified/src-desktop/src-widgets/plugins/plugins.pri index d7082141..8931d413 100644 --- a/src-qt5/core/lumina-desktop-unified/src-desktop/src-widgets/plugins/plugins.pri +++ b/src-qt5/core/lumina-desktop-unified/src-desktop/src-widgets/plugins/plugins.pri @@ -4,4 +4,5 @@ INCLUDEPATH *= $${PWD} #SOURCES *= HEADERS *= $${PWD}/ClockPlugin.h \ + $${PWD}/CalendarMenu.h \ $${PWD}/SpacerPlugin.h -- cgit