Notification panel add time

pull/15/head
reionwong 4 years ago
parent 65d9c097b6
commit 258d558514

@ -1,5 +1,6 @@
set(SRCS main.cpp
application.cpp
datehelper.cpp
screenhelper.cpp
notificationsmodel.cpp
notificationserver.cpp

@ -0,0 +1,34 @@
#include "datehelper.h"
#include <QDateTime>
DateHelper::DateHelper(QObject *parent) : QObject(parent)
{
}
QString DateHelper::friendlyTime(const QDateTime &time)
{
QDateTime now = QDateTime::currentDateTime();
qint64 minutes = qRound64(time.secsTo(now) / 60.0f);
if (minutes < 1)
return tr("Now");
else if (minutes == 1)
return tr("1 minute ago");
else if (minutes < 60)
return tr("%1 minutes ago").arg(minutes);
qint64 hours = qRound64(minutes / 60.0f);
if (hours == 1)
return tr("1 hour ago");
else if (hours < 24)
return tr("%1 hours ago").arg(hours);
qint64 days = qRound64(hours / 24.0f);
if (days == 1)
return tr("1 day ago");
else if (days <= 10)
return tr("%1 days ago").arg(days);
return time.toString(Qt::DefaultLocaleShortDate);
}

@ -0,0 +1,16 @@
#ifndef DATEHELPER_H
#define DATEHELPER_H
#include <QObject>
class DateHelper : public QObject
{
Q_OBJECT
public:
explicit DateHelper(QObject *parent = nullptr);
Q_INVOKABLE static QString friendlyTime(const QDateTime &time);
};
#endif // DATEHELPER_H

@ -18,6 +18,7 @@
*/
#include "historymodel.h"
#include "datehelper.h"
#include <QSettings>
#include <QDataStream>
@ -51,7 +52,9 @@ QVariant HistoryModel::data(const QModelIndex &index, int role) const
case HistoryModel::ImageRole:
return "";
case HistoryModel::CreatedRole:
return notification.created;
return DateHelper::friendlyTime(notification.created);
case HistoryModel::UpdatedRole:
return DateHelper::friendlyTime(notification.updated);
case HistoryModel::BodyRole:
return notification.body;
case HistoryModel::IconNameRole:
@ -147,3 +150,8 @@ void HistoryModel::initDatas()
QDataStream in(&listByteArray, QIODevice::ReadOnly);
in >> m_notifications;
}
void HistoryModel::updateTime()
{
emit layoutChanged();
}

@ -53,6 +53,7 @@ public:
Q_INVOKABLE void save();
void initDatas();
void updateTime();
private:
QVector<Notification> m_notifications;

@ -47,6 +47,7 @@ bool NotificationWindow::eventFilter(QObject *object, QEvent *event)
}
} else if (event->type() == QEvent::Show) {
KWindowSystem::setState(winId(), NET::SkipTaskbar | NET::SkipPager | NET::SkipSwitcher);
HistoryModel::self()->updateTime();
}
return QObject::eventFilter(object, event);

@ -35,7 +35,7 @@ Item {
id: _background
anchors.fill: parent
color: FishUI.Theme.secondBackgroundColor
radius: NotificationDialog.width * 0.02 //FishUI.Theme.bigRadius
radius: NotificationDialog.width * 0.05 //FishUI.Theme.bigRadius
opacity: 0.7
border.width: 1
@ -126,10 +126,6 @@ Item {
visible: _view.count === 0
}
addDisplaced: Transition {
NumberAnimation { properties: "x, y"; duration: 250 }
}
removeDisplaced: Transition {
NumberAnimation { properties: "x, y"; duration: 250 }
}
@ -185,16 +181,25 @@ Item {
rightPadding: FishUI.Units.smallSpacing
}
Label {
text: model.body
visible: text
rightPadding: FishUI.Units.smallSpacing
maximumLineCount: 2
elide: Text.ElideRight
wrapMode: Text.Wrap
Layout.fillWidth: true
// Layout.fillHeight: true
Layout.alignment: Qt.AlignVCenter
RowLayout {
Label {
id: bodyLabel
text: model.body
visible: text
rightPadding: FishUI.Units.smallSpacing
maximumLineCount: 2
elide: Text.ElideRight
wrapMode: Text.Wrap
Layout.fillWidth: true
// Layout.fillHeight: true
Layout.alignment: Qt.AlignVCenter
}
Label {
text: model.created
rightPadding: FishUI.Units.smallSpacing
Layout.alignment: Qt.AlignRight
}
}
Item {

@ -1,6 +1,44 @@
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE TS>
<TS version="2.1">
<context>
<name>DateHelper</name>
<message>
<location filename="../datehelper.cpp" line="15"/>
<source>Now</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../datehelper.cpp" line="17"/>
<source>1 minute ago</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../datehelper.cpp" line="19"/>
<source>%1 minutes ago</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../datehelper.cpp" line="23"/>
<source>1 hour ago</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../datehelper.cpp" line="25"/>
<source>%1 hours ago</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../datehelper.cpp" line="29"/>
<source>1 day ago</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../datehelper.cpp" line="31"/>
<source>%1 days ago</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>NotificationWindow</name>
<message>

@ -1,6 +1,44 @@
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE TS>
<TS version="2.1">
<TS version="2.1" language="zh_CN">
<context>
<name>DateHelper</name>
<message>
<location filename="../datehelper.cpp" line="15"/>
<source>Now</source>
<translation></translation>
</message>
<message>
<location filename="../datehelper.cpp" line="17"/>
<source>1 minute ago</source>
<translation></translation>
</message>
<message>
<location filename="../datehelper.cpp" line="19"/>
<source>%1 minutes ago</source>
<translation>%1</translation>
</message>
<message>
<location filename="../datehelper.cpp" line="23"/>
<source>1 hour ago</source>
<translation></translation>
</message>
<message>
<location filename="../datehelper.cpp" line="25"/>
<source>%1 hours ago</source>
<translation>%1</translation>
</message>
<message>
<location filename="../datehelper.cpp" line="29"/>
<source>1 day ago</source>
<translation></translation>
</message>
<message>
<location filename="../datehelper.cpp" line="31"/>
<source>%1 days ago</source>
<translation>%1</translation>
</message>
</context>
<context>
<name>NotificationWindow</name>
<message>

Loading…
Cancel
Save