]> cloud.milkyroute.net Git - dolphin.git/commitdiff
Details View: display dates as relative Short dates
authorMéven Car <meven29@gmail.com>
Thu, 24 Dec 2020 00:11:21 +0000 (00:11 +0000)
committerMéven Car <meven29@gmail.com>
Thu, 24 Dec 2020 00:11:21 +0000 (00:11 +0000)
CCBUG: 340982

src/kitemviews/kfileitemlistwidget.cpp
src/settings/dolphin_detailsmodesettings.kcfg
src/settings/viewmodes/viewsettingstab.cpp
src/settings/viewmodes/viewsettingstab.h

index 66fcafaf628f6d2d56880131cbf8fc301a5d0203..1b38176cc29c95fbc245e8268e9c1ca1a1f92be0 100644 (file)
@@ -47,10 +47,20 @@ QString KFileItemListWidgetInformant::roleText(const QByteArray& role,
 {
     QString text;
     const QVariant roleValue = values.value(role);
+    QLocale local;
+    KFormat formatter(local);
 
     // Implementation note: In case if more roles require a custom handling
     // use a hash + switch for a linear runtime.
 
+    auto formatDate = [formatter, local](const QDateTime& time) {
+        if (DetailsModeSettings::useShortRelativeDates()) {
+            return formatter.formatRelativeDateTime(time, QLocale::ShortFormat);
+        } else {
+            return local.toString(time, QLocale::ShortFormat);
+        }
+    };
+
     if (role == "size") {
         if (values.value("isDir").toBool()) {
             if (!roleValue.isNull() && roleValue != -1) {
@@ -62,22 +72,25 @@ QString KFileItemListWidgetInformant::roleText(const QByteArray& role,
                 } else {
                     // if we have directory size available
                     const KIO::filesize_t size = roleValue.value<KIO::filesize_t>();
-                    text = KFormat().formatByteSize(size);
+                    text = formatter.formatByteSize(size);
                 }
             }
         } else {
             const KIO::filesize_t size = roleValue.value<KIO::filesize_t>();
-            text = KFormat().formatByteSize(size);
+            text = formatter.formatByteSize(size);
         }
     } else if (role == "modificationtime" || role == "creationtime" || role == "accesstime") {
             bool ok;
             const long long time = roleValue.toLongLong(&ok);
             if (ok && time != -1) {
-                return QLocale().toString(QDateTime::fromSecsSinceEpoch(time), QLocale::ShortFormat);
+                const QDateTime dateTime = QDateTime::fromSecsSinceEpoch(time);
+                text = formatDate(dateTime);
             }
     } else if (role == "deletiontime" || role == "imageDateTime") {
         const QDateTime dateTime = roleValue.toDateTime();
-        text = QLocale().toString(dateTime, QLocale::ShortFormat);
+        if (dateTime.isValid()) {
+            text = formatDate(dateTime);
+        }
     } else {
         text = KStandardItemListWidgetInformant::roleText(role, values);
     }
index 9d05a8ab004e14bfefd4eeedb0e95c146c432ace..c8238f1e827df548c91915c104c0da315c6d1615 100644 (file)
@@ -52,5 +52,9 @@
             <label>Recursive directory size limit</label>
             <default>10</default>
         </entry>
+        <entry name="UseShortRelativeDates" type="Bool">
+            <label>if true we use short relative dates, if not short dates</label>
+            <default>true</default>
+        </entry>
     </group>
 </kcfg>
index cf8cd2810a9974caaa0a919ff3810dc663bd3342..0fd2dca3ac84a83000230a45b11b84705e9707ba 100644 (file)
@@ -14,6 +14,7 @@
 #include "views/zoomlevelinfo.h"
 
 #include <KLocalizedString>
+#include <KFormat>
 
 #include <QApplication>
 #include <QCheckBox>
@@ -34,7 +35,9 @@ ViewSettingsTab::ViewSettingsTab(Mode mode, QWidget* parent) :
     m_widthBox(nullptr),
     m_maxLinesBox(nullptr),
     m_expandableFolders(nullptr),
-    m_recursiveDirectorySizeLimit(nullptr)
+    m_recursiveDirectorySizeLimit(nullptr),
+    m_useRelatetiveDates(nullptr),
+    m_useShortDates(nullptr)
 {
     QFormLayout* topLayout = new QFormLayout(this);
 
@@ -121,6 +124,25 @@ ViewSettingsTab::ViewSettingsTab(Mode mode, QWidget* parent) :
         topLayout->addRow(i18nc("@title:group", "Folder size displays:"), m_numberOfItems);
         topLayout->addRow(QString(), contentsSizeLayout);
 #endif
+
+        QDateTime thirtyMinutesAgo = QDateTime::currentDateTime().addSecs(-30 * 60);
+        QLocale local;
+        KFormat formatter(local);
+
+        m_useRelatetiveDates = new QRadioButton(i18nc(
+            "option:radio as in relative date", "Relative (e.g. '%1')", formatter.formatRelativeDateTime(thirtyMinutesAgo, QLocale::ShortFormat))
+        );
+        m_useShortDates = new QRadioButton(
+            i18nc("option:radio as in absolute date", "Absolute (e.g. '%1')", local.toString(thirtyMinutesAgo, QLocale::ShortFormat))
+        );
+
+        QButtonGroup* dateFormatGroup = new QButtonGroup(this);
+        dateFormatGroup->addButton(m_useRelatetiveDates);
+        dateFormatGroup->addButton(m_useShortDates);
+
+        topLayout->addRow(i18nc("@title:group", "Date style:"), m_useRelatetiveDates);
+        topLayout->addRow(QString(), m_useShortDates);
+
         break;
     }
 
@@ -147,6 +169,8 @@ ViewSettingsTab::ViewSettingsTab(Mode mode, QWidget* parent) :
             m_recursiveDirectorySizeLimit->setEnabled(m_sizeOfContents->isChecked());
         });
 #endif
+        connect(m_useRelatetiveDates, &QRadioButton::toggled, this, &ViewSettingsTab::changed);
+        connect(m_useShortDates, &QRadioButton::toggled, this, &ViewSettingsTab::changed);
         break;
     default:
         break;
@@ -176,6 +200,7 @@ void ViewSettingsTab::applySettings()
         DetailsModeSettings::setDirectorySizeCount(m_numberOfItems->isChecked());
         DetailsModeSettings::setRecursiveDirectorySizeLimit(m_recursiveDirectorySizeLimit->value());
 #endif
+        DetailsModeSettings::setUseShortRelativeDates(m_useRelatetiveDates->isChecked());
         break;
     default:
         break;
@@ -234,6 +259,8 @@ void ViewSettingsTab::loadSettings()
             }
             m_recursiveDirectorySizeLimit->setValue(DetailsModeSettings::recursiveDirectorySizeLimit());
         #endif
+        m_useRelatetiveDates->setChecked(DetailsModeSettings::useShortRelativeDates());
+        m_useShortDates->setChecked(!DetailsModeSettings::useShortRelativeDates());
         break;
     default:
         break;
index 41b40b95e0978aae333d1a7cca62f8303c028938..d5756bb3aa917d595e02f21488664c5a56fe6b1a 100644 (file)
@@ -64,6 +64,8 @@ private:
     QRadioButton* m_numberOfItems;
     QRadioButton* m_sizeOfContents;
     QSpinBox* m_recursiveDirectorySizeLimit;
+    QRadioButton* m_useRelatetiveDates;
+    QRadioButton* m_useShortDates;
 };
 
 #endif