[Pkg-owncloud-commits] [owncloud-client] 241/484: ActivityWidget: Created a delegate for prettier display of the activities

Sandro Knauß hefee-guest at moszumanska.debian.org
Wed Dec 16 00:37:47 UTC 2015


This is an automated email from the git hooks/post-receive script.

hefee-guest pushed a commit to branch master
in repository owncloud-client.

commit cb4fba76587e82fc3063442a1c0567bccc1ae161
Author: Klaas Freitag <freitag at owncloud.com>
Date:   Tue Nov 3 17:54:37 2015 +0100

    ActivityWidget: Created a delegate for prettier display of the activities
---
 src/gui/CMakeLists.txt           |   1 +
 src/gui/activityitemdelegate.cpp | 126 +++++++++++++++++++++++++++++++++++++++
 src/gui/activityitemdelegate.h   |  45 ++++++++++++++
 src/gui/activitywidget.cpp       |  51 +++++++++++++++-
 src/gui/activitywidget.h         |   1 +
 5 files changed, 221 insertions(+), 3 deletions(-)

diff --git a/src/gui/CMakeLists.txt b/src/gui/CMakeLists.txt
index 7e0dc99..6fe16fb 100644
--- a/src/gui/CMakeLists.txt
+++ b/src/gui/CMakeLists.txt
@@ -57,6 +57,7 @@ set(client_SRCS
     owncloudsetupwizard.cpp
     protocolwidget.cpp
     activitywidget.cpp
+    activityitemdelegate.cpp
     selectivesyncdialog.cpp
     settingsdialog.cpp
     sharedialog.cpp
diff --git a/src/gui/activityitemdelegate.cpp b/src/gui/activityitemdelegate.cpp
new file mode 100644
index 0000000..a81d3b5
--- /dev/null
+++ b/src/gui/activityitemdelegate.cpp
@@ -0,0 +1,126 @@
+/*
+ * Copyright (C) by Klaas Freitag <freitag at owncloud.com>
+ * Copyright (C) by Olivier Goffart <ogoffart at woboq.com>
+ *
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * for more details.
+ */
+
+#include "activityitemdelegate.h"
+#include "folderstatusmodel.h"
+#include "folderman.h"
+#include "accountstate.h"
+#include "utility.h"
+#include <theme.h>
+#include <account.h>
+
+#include <QFileIconProvider>
+#include <QPainter>
+#include <QApplication>
+
+namespace OCC {
+
+QSize ActivityItemDelegate::sizeHint(const QStyleOptionViewItem & option ,
+                                     const QModelIndex & index) const
+{
+    QFont font = option.font;
+
+    QFontMetrics fm(font);
+    int iconHeight = qRound(fm.height() / 5.0 * 8.0);
+    int margin = fm.height()/4;
+
+    // TODO: set a different height for the day-line
+
+    // calc height
+
+    int h = iconHeight;          // normal text height
+    h += 2*margin;               // two times margin
+
+    return QSize( 0, h);
+}
+
+void ActivityItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
+                                 const QModelIndex &index) const
+{
+    QStyledItemDelegate::paint(painter,option,index);
+
+    QFont font = option.font;
+
+    QFontMetrics fm( font );
+    int margin = fm.height()/4;
+
+    // awesome to detect the timeline entry
+    // if (index.data(Timeline).toBool()) {
+    //        return;
+    // }
+
+    painter->save();
+
+    QIcon actionIcon      = qvariant_cast<QIcon>(index.data(ActionIconRole));
+    QIcon userIcon        = qvariant_cast<QIcon>(index.data(UserIconRole));
+    QString actionText    = qvariant_cast<QString>(index.data(ActionTextRole));
+    QString pathText      = qvariant_cast<QString>(index.data(PathRole));
+    QString remoteLink    = qvariant_cast<QString>(index.data(LinkRole));
+    QString timeText      = qvariant_cast<QString>(index.data(PointInTimeRole));
+    QString accountRole   = qvariant_cast<QString>(index.data(AccountRole));
+
+    QRect actionIconRect = option.rect;
+    QRect userIconRect   = option.rect;
+
+    int iconHeight = qRound(fm.height() / 5.0 * 8.0);
+    int iconWidth = iconHeight;
+
+    actionIconRect.setLeft( option.rect.left() + margin );
+    actionIconRect.setWidth( iconWidth );
+    actionIconRect.setHeight( iconHeight );
+    actionIconRect.setTop( actionIconRect.top() + margin ); // (iconRect.height()-iconsize.height())/2);
+
+    userIconRect.setLeft( actionIconRect.right() + margin );
+    userIconRect.setWidth( iconWidth );
+    userIconRect.setHeight( iconHeight );
+    userIconRect.setTop( actionIconRect.top() ); // (iconRect.height()-iconsize.height())/2);
+
+    int textTopOffset = qRound( (iconHeight - fm.height())/ 2.0 );
+    // time rect
+    QRect timeBox;
+    int timeBoxWidth = fm.boundingRect(QLatin1String("a few minutes ago")).width(); // FIXME.
+    timeBox.setTop( actionIconRect.top()+textTopOffset);
+    timeBox.setLeft( option.rect.right() - timeBoxWidth- margin );
+    timeBox.setWidth( timeBoxWidth);
+    timeBox.setHeight( fm.height() );
+
+    QRect actionTextBox = timeBox;
+    actionTextBox.setLeft( userIconRect.right()+margin );
+    actionTextBox.setRight( timeBox.left()-margin );
+
+    /* === start drawing === */
+    QPixmap pm = actionIcon.pixmap(iconWidth, iconHeight, QIcon::Normal);
+    painter->drawPixmap(QPoint(actionIconRect.left(), actionIconRect.top()), pm);
+
+    pm = userIcon.pixmap(iconWidth, iconHeight, QIcon::Normal);
+    painter->drawPixmap(QPoint(userIconRect.left(), userIconRect.top()), pm);
+
+    const QString elidedAction = fm.elidedText(actionText, Qt::ElideRight, actionTextBox.width());
+    painter->drawText(actionTextBox, elidedAction);
+
+    const QString elidedTime = fm.elidedText(timeText, Qt::ElideRight, timeBox.width());
+    painter->drawText(timeBox, elidedTime);
+    painter->restore();
+
+}
+
+bool ActivityItemDelegate::editorEvent ( QEvent * event, QAbstractItemModel * model,
+                                         const QStyleOptionViewItem & option, const QModelIndex & index )
+{
+    return QStyledItemDelegate::editorEvent(event, model, option, index);
+}
+
+} // namespace OCC
diff --git a/src/gui/activityitemdelegate.h b/src/gui/activityitemdelegate.h
new file mode 100644
index 0000000..521eb06
--- /dev/null
+++ b/src/gui/activityitemdelegate.h
@@ -0,0 +1,45 @@
+/*
+ * Copyright (C) by Klaas Freitag <freitag at kde.org>
+ * Copyright (C) by Olivier Goffart <ogoffart at woboq.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * for more details.
+ */
+
+#pragma once
+#include <QStyledItemDelegate>
+
+namespace OCC {
+
+/**
+ * @brief The ActivityItemDelegate class
+ * @ingroup gui
+ */
+class ActivityItemDelegate : public QStyledItemDelegate
+{
+    Q_OBJECT
+public:
+
+    enum datarole { ActionIconRole = Qt::UserRole + 1,
+                    UserIconRole,
+                    AccountRole,
+                    ActionTextRole,
+                    PathRole,
+                    LinkRole,
+                    PointInTimeRole };
+    void paint( QPainter*, const QStyleOptionViewItem&, const QModelIndex& ) const Q_DECL_OVERRIDE;
+    QSize sizeHint( const QStyleOptionViewItem&, const QModelIndex& ) const Q_DECL_OVERRIDE;
+    bool editorEvent( QEvent* event, QAbstractItemModel* model, const QStyleOptionViewItem& option,
+                      const QModelIndex& index ) Q_DECL_OVERRIDE;
+
+};
+
+} // namespace OCC
+
diff --git a/src/gui/activitywidget.cpp b/src/gui/activitywidget.cpp
index ada65d0..b70662d 100644
--- a/src/gui/activitywidget.cpp
+++ b/src/gui/activitywidget.cpp
@@ -30,6 +30,7 @@
 #include "account.h"
 #include "accountstate.h"
 #include "accountmanager.h"
+#include "activityitemdelegate.h"
 
 #include "ui_activitywidget.h"
 
@@ -61,6 +62,8 @@ QVariant ActivityListModel::data(const QModelIndex &index, int role) const
     if (!index.isValid())
         return QVariant();
 
+    a = _finalList.at(index.row());
+
     if (role == Qt::EditRole)
         return QVariant();
 
@@ -68,18 +71,57 @@ QVariant ActivityListModel::data(const QModelIndex &index, int role) const
     case Qt::ToolTipRole:
         return QVariant();
     case Qt::DisplayRole:
-        a = _finalList.at(index.row());
-        return tr("Account %1 at %2: %3").arg(a._accName).arg(a._dateTime.toString(Qt::SystemLocaleShortDate)).arg(a._subject);
+        // return tr("Account %1 at %2: %3").arg(a._accName).arg(a._dateTime.toString(Qt::SystemLocaleShortDate)).arg(a._subject);
         break;
     case Qt::DecorationRole:
-        // return QFileIconProvider().icon(QFileIconProvider::Folder);
         return QVariant();
         break;
+    case ActivityItemDelegate::ActionIconRole:
+        return QVariant(); // FIXME once the action can be quantified, display on Icon
+        break;
+    case ActivityItemDelegate::UserIconRole:
+        return QIcon(QLatin1String(":/client/resources/account.png"));
+        break;
+    case ActivityItemDelegate::ActionTextRole:
+        return a._subject;
+        break;
+    case ActivityItemDelegate::PathRole:
+            return a._file;
+        break;
+    case ActivityItemDelegate::LinkRole:
+            return a._link;
+        break;
+    case ActivityItemDelegate::AccountRole:
+            return a._accName;
+        break;
+    case ActivityItemDelegate::PointInTimeRole:
+        return timeSpanFromNow(a._dateTime);
+
     }
     return QVariant();
 
 }
 
+QString ActivityListModel::timeSpanFromNow(const QDateTime& dt) const
+{
+    QDateTime now = QDateTime::currentDateTime();
+
+    if( dt.daysTo(now)>0 ) {
+        return tr("%1 day(s) ago").arg(dt.daysTo(now));
+    } else {
+        qint64 secs = dt.secsTo(now);
+
+        if( floor(secs / 3600.0) > 0 ) {
+            int hours = floor(secs/3600.0);
+            return( tr("%1 hour(s) ago").arg(hours));
+        } else {
+            int minutes = qRound(secs/60.0);
+            return( tr("%1 minute(s) ago").arg(minutes));
+        }
+    }
+    return tr("Some time ago");
+}
+
 int ActivityListModel::rowCount(const QModelIndex&) const
 {
     return _finalList.count();
@@ -215,6 +257,9 @@ ActivityWidget::ActivityWidget(QWidget *parent) :
 #endif
 
     _model = new ActivityListModel(this);
+    ActivityItemDelegate *delegate = new ActivityItemDelegate;
+    delegate->setParent(this);
+    _ui->_activityList->setItemDelegate(delegate);
     _ui->_activityList->setModel(_model);
 
     _copyBtn = _ui->_dialogButtonBox->addButton(tr("Copy"), QDialogButtonBox::ActionRole);
diff --git a/src/gui/activitywidget.h b/src/gui/activitywidget.h
index 266b137..6f6db93 100644
--- a/src/gui/activitywidget.h
+++ b/src/gui/activitywidget.h
@@ -97,6 +97,7 @@ private slots:
 private:
     void startFetchJob(AccountStatePtr s);
     void combineActivityLists();
+    QString timeSpanFromNow(const QDateTime& dt) const;
 
     QMap<AccountStatePtr, ActivityList> _activityLists;
     ActivityList _finalList;

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-owncloud/owncloud-client.git



More information about the Pkg-owncloud-commits mailing list