on xenial help sign in
improve datetime and session indicators
commit0f4c4fdaf4d028fac74fd0a0bc432a4a96aab7a8 authorLukáš Tinkl <lukas.tinkl@canonical.com> dateTue 15 Sep 2015, 18:33 (11 years ago) parentfb6cedc
3 files changed+98−1
plugins/Utils/timeformatter.cpp+13−1 plugins/Utils/timeformatter.h+2 qml/Panel/Indicators/MenuItemFactory.qml+83
plugins/Utils/timeformatter.cpp+13−1
@@ -20,6 +20,7 @@
2020
2121#include <gio/gio.h>
2222#include <QDateTime>
23+#include <QTimeZone>
2324
2425struct TimeFormatterPrivate
2526{
@@ -176,13 +177,24 @@ void TimeFormatter::update()
176177 Q_EMIT timeStringChanged(priv->timeString);
177178}
178179
180+QString TimeFormatter::currentTimeInTimezone(const QVariant &tzId) const
181+{
182+ QTimeZone tz(tzId.toByteArray());
183+ if (tz.isValid()) {
184+ const QDateTime now = QDateTime::currentDateTime().toTimeZone(tz);
185+ // return locale-aware string in the form "day, hh:mm", e.g. "Mon 14:30" or "Mon 1:30 pm"
186+ return QStringLiteral("%1 %2").arg(now.toString("ddd")).arg(now.time().toString(Qt::DefaultLocaleShortDate));
187+ }
188+ return QString();
189+}
190+
179191QString TimeFormatter::formatTime() const
180192{
181193 return QDateTime::fromMSecsSinceEpoch(time() / 1000).toString(format());
182194}
183195
184196GDateTimeFormatter::GDateTimeFormatter(QObject* parent)
185−: TimeFormatter("%d-%m-%Y %I:%M%p", parent)
197+ : TimeFormatter("%d-%m-%Y %I:%M%p", parent)
186198{
187199}
188200
plugins/Utils/timeformatter.h+2
@@ -42,6 +42,8 @@ public:
4242
4343 void update();
4444
45+ Q_INVOKABLE QString currentTimeInTimezone(const QVariant &tzId) const;
46+
4547Q_SIGNALS:
4648 void formatChanged(const QString &format);
4749 void timeStringChanged(const QString &timeString);
qml/Panel/Indicators/MenuItemFactory.qml+83
@@ -24,6 +24,7 @@ import QMenuModel 0.1
2424import Utils 0.1 as Utils
2525import Ubuntu.Components.ListItems 0.1 as ListItems
2626import Ubuntu.Components 1.1
27+import "../../Greeter"
2728
2829Item {
2930 id: menuFactory
@@ -63,6 +64,14 @@ Item {
6364 },
6465 "indicator-messages" : {
6566 "com.canonical.indicator.button" : messagesButtonMenu
67+ },
68+ "indicator-datetime" : {
69+ "com.canonical.indicator.calendar": calendarMenu,
70+ "com.canonical.indicator.location": timezoneMenu
71+ },
72+ "indicator-session" : {
73+ "indicator.user-menu-item": userMenuItem,
74+ "indicator.guest-menu-item": userMenuItem
6675 }
6776 }
6877
@@ -73,6 +82,8 @@ Item {
7382 return defaultValue;
7483 }
7584
85+ LightDM {id: lightDM} // Provide backend access
86+
7687 Component {
7788 id: separatorMenu;
7889
@@ -422,6 +433,78 @@ Item {
422433 }
423434 }
424435
436+ Component {
437+ id: userMenuItem
438+
439+ Menus.UserSessionMenu {
440+ objectName: "userSessionMenu"
441+ highlightWhenPressed: false
442+
443+ property QtObject menuData: null
444+ property var menuModel: menuFactory.menuModel
445+ property int menuIndex: -1
446+ //property var extendedData: menuData && menuData.ext || undefined
447+
448+ name: menuData && menuData.label || ""
449+ iconSource: menuData && menuData.icon || ""
450+ // active: true // TODO hook up with lightdm
451+
452+ onTriggered: {
453+ menuModel.activate(menuIndex);
454+ }
455+ }
456+ }
457+
458+ Component {
459+ id: calendarMenu
460+
461+ Menus.CalendarMenu {
462+ objectName: "calendarMenu"
463+ highlightWhenPressed: false
464+ }
465+ }
466+
467+ Component {
468+ id: timezoneMenu
469+
470+ Menus.TimeZoneMenu {
471+ id: tzMenuItem
472+ objectName: "timezoneMenu"
473+
474+ property QtObject menuData: null
475+ property var menuModel: menuFactory.menuModel
476+ property int menuIndex: -1
477+ property var extendedData: menuData && menuData.ext || undefined
478+ readonly property var tz: getExtendedProperty(extendedData, "xCanonicalTimezone", "UTC")
479+ property var tzFormatter: Utils.TimeFormatter {}
480+ property var updateTimer: Timer {
481+ repeat: true
482+ running: true
483+ onTriggered: tzMenuItem.time = tzMenuItem.tzFormatter.currentTimeInTimezone(tzMenuItem.tz)
484+ }
485+
486+ city: menuData && menuData.label || ""
487+ time: tzFormatter.currentTimeInTimezone(tz)
488+ enabled: menuData && menuData.sensitive || false
489+ highlightWhenPressed: false
490+
491+ onMenuModelChanged: {
492+ loadAttributes();
493+ }
494+ onMenuIndexChanged: {
495+ loadAttributes();
496+ }
497+ onTriggered: {
498+ menuModel.activate(menuIndex);
499+ }
500+
501+ function loadAttributes() {
502+ if (!menuModel || menuIndex == -1) return;
503+ menuModel.loadExtendedAttributes(menuIndex, {'x-canonical-timezone': 'string'});
504+ }
505+ }
506+ }
507+
425508 Component {
426509 id: wifiSection;
427510