[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    [plasma-workspace] applets/digital-clock/package/contents: [digital-clock] Add a simple option to us
From:       Martin Klapetek <mklapetek () kde ! org>
Date:       2015-08-05 12:25:37
Message-ID: E1ZMxlJ-0000gZ-A7 () scm ! kde ! org
[Download RAW message or body]

Git commit 1fc66aabb2e597fb4b2432a80d4acaa41f305197 by Martin Klapetek.
Committed on 05/08/2015 at 12:25.
Pushed by mklapetek into branch 'master'.

[digital-clock] Add a simple option to use 24h-clock format

In my opinion this is just a temporary workaround and not a very good
one (generally, code-wise it's awesome :P). There should really be a
global config option for that but as we now rely on QLocale and QLocale
is...bad at this, here's at least this small help, which will at least
change the clock format on the panel.

Imo we could really really use something like KLocale again.

REVIEW: 124453
BUG: 345378
FIXED-IN: 5.4.0

M  +3    -0    applets/digital-clock/package/contents/config/main.xml
M  +24   -8    applets/digital-clock/package/contents/ui/DigitalClock.qml
M  +6    -1    applets/digital-clock/package/contents/ui/configAppearance.qml

http://commits.kde.org/plasma-workspace/1fc66aabb2e597fb4b2432a80d4acaa41f305197

diff --git a/applets/digital-clock/package/contents/config/main.xml \
b/applets/digital-clock/package/contents/config/main.xml index 8b0872b..bdf8121 \
                100644
--- a/applets/digital-clock/package/contents/config/main.xml
+++ b/applets/digital-clock/package/contents/config/main.xml
@@ -42,6 +42,9 @@
     <entry name="showWeekNumbers" type="Bool">
       <default>false</default>
     </entry>
+    <entry name="use24hFormat" type="UInt">
+        <default>1</default>
+    </entry>
   </group>
 
 </kcfg>
diff --git a/applets/digital-clock/package/contents/ui/DigitalClock.qml \
b/applets/digital-clock/package/contents/ui/DigitalClock.qml index 696903a..868a0b5 \
                100644
--- a/applets/digital-clock/package/contents/ui/DigitalClock.qml
+++ b/applets/digital-clock/package/contents/ui/DigitalClock.qml
@@ -45,6 +45,7 @@ Item {
 
     property string lastSelectedTimezone: \
                plasmoid.configuration.lastSelectedTimezone
     property bool displayTimezoneAsCode: \
plasmoid.configuration.displayTimezoneAsCode +    property int use24hFormat: \
plasmoid.configuration.use24hFormat  
     property string lastDate: ""
     property int tzOffset
@@ -67,6 +68,7 @@ Item {
     onShowSecondsChanged:          { \
                timeFormatCorrection(Qt.locale().timeFormat(Locale.ShortFormat)) }
     onShowLocalTimezoneChanged:    { \
                timeFormatCorrection(Qt.locale().timeFormat(Locale.ShortFormat)) }
     onShowDateChanged:             { \
timeFormatCorrection(Qt.locale().timeFormat(Locale.ShortFormat)) } +    \
onUse24hFormatChanged:         { \
timeFormatCorrection(Qt.locale().timeFormat(Locale.ShortFormat)) }  
     states: [
         State {
@@ -465,16 +467,30 @@ Item {
     // It can happen that Qt uses the 'C' locale (it's a fallback) and that locale
     // has always ":ss" part in ShortFormat, so we need to remove it.
     function timeFormatCorrection(timeFormatString) {
+        var regexp = /(hh*)(.+)(mm)/i
+        var match = regexp.exec(timeFormatString);
+
+        var hours = match[1];
+        var delimiter = match[2];
+        var minutes = match[3]
+        var seconds = "ss";
+        var amPm = "AP";
+        var uses24hFormatByDefault = timeFormatString.toLowerCase().indexOf("ap") == \
-1; +
+        // because QLocale is incredibly stupid and does not convert 12h/24h clock \
format +        // when uppercase H is used for hours, needs to be h or hh, so \
toLowerCase() +        var result = hours.toLowerCase() + delimiter + minutes;
+
         if (main.showSeconds && timeFormatString.indexOf('s') == -1) {
-            timeFormatString = timeFormatString.replace(/^(hh*)(.+)(mm)(.*?)/i,
-                                                        function(match, firstPart, \
                delimiter, secondPart, rest, offset, original) {
-                return firstPart + delimiter + secondPart + delimiter + "ss" + rest
-            });
-        } else if (!main.showSeconds && timeFormatString.indexOf('s') != -1) {
-            timeFormatString = timeFormatString.replace(/.ss?/i, "");
+            result += delimiter + seconds;
+        }
+
+        // add "AM/PM" either if the setting is the default and locale uses it OR if \
the user unchecked "use 24h format" +        if ((main.use24hFormat == \
Qt.PartiallyChecked && !uses24hFormatByDefault) || main.use24hFormat == Qt.Unchecked) \
{ +            result += " " + amPm;
         }
 
-        main.timeFormat = timeFormatString;
+        main.timeFormat = result;
         setupLabels();
     }
 
@@ -586,7 +602,7 @@ Item {
         }
 
         if (doCorrections) {
-            timeFormatCorrection(main.timeFormat);
+            timeFormatCorrection(Qt.locale().timeFormat(Locale.ShortFormat));
         }
     }
 
diff --git a/applets/digital-clock/package/contents/ui/configAppearance.qml \
b/applets/digital-clock/package/contents/ui/configAppearance.qml index \
                cc1acbe..37df4f4 100644
--- a/applets/digital-clock/package/contents/ui/configAppearance.qml
+++ b/applets/digital-clock/package/contents/ui/configAppearance.qml
@@ -39,7 +39,7 @@ Item {
 
     property alias cfg_showDate: showDate.checked
     property string cfg_dateFormat: "shortDate"
-
+    property alias cfg_use24hFormat: use24hFormat.checkedState
 
     QtLayouts.ColumnLayout {
         QtControls.GroupBox {
@@ -81,6 +81,11 @@ Item {
                 }
 
                 QtControls.CheckBox {
+                    id: use24hFormat
+                    text: i18nc("Checkbox label; means 24h clock format, without \
am/pm", "Use 24-hour Clock") +                }
+
+                QtControls.CheckBox {
                     id: showLocalTimezone
                     text: i18n("Show local time zone")
                 }


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic