Details View: display dates as relative Short dates

CCBUG: 340982
This commit is contained in:
Méven Car 2020-12-24 00:11:21 +00:00
parent 9abe299c77
commit f0d03c7dd7
4 changed files with 51 additions and 5 deletions

View 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);
}

View 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>

View 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;

View file

@ -64,6 +64,8 @@ private:
QRadioButton* m_numberOfItems;
QRadioButton* m_sizeOfContents;
QSpinBox* m_recursiveDirectorySizeLimit;
QRadioButton* m_useRelatetiveDates;
QRadioButton* m_useShortDates;
};
#endif