Review Request: Patch to add free space indicator into device notifier plasmoid

Alessandro Diaferia alediaferia at gmail.com
Tue Aug 18 13:44:50 CEST 2009


2009/8/17 Sebastian Kügler <sebas at kde.org>

>
> -----------------------------------------------------------
> This is an automatically generated e-mail. To reply, visit:
> http://reviewboard.kde.org/r/632/#review2065
> -----------------------------------------------------------
>
>
> I've tested the patch on current trunk, about to commit it, but I don't see
> any progress bar showing up. The patch probably needs updating.
>
> - Sebastian
>
>
> On 2009-05-12 14:28:58, mck182 wrote:
> >
> > -----------------------------------------------------------
> > This is an automatically generated e-mail. To reply, visit:
> > http://reviewboard.kde.org/r/632/
> > -----------------------------------------------------------
> >
> > (Updated 2009-05-12 14:28:58)
> >
> >
> > Review request for Plasma.
> >
> >
> > Summary
> > -------
> >
> > This patch adds a KCapacityBar into the device notifier plasmoid to show
> the device's free space.
> >
> >
> > Diffs
> > -----
> >
> >
> trunk/KDE/kdebase/workspace/plasma/applets/devicenotifier/CMakeLists.txt
> 965209
> >
> trunk/KDE/kdebase/workspace/plasma/applets/devicenotifier/devicenotifier.cpp
> 965209
> >
> trunk/KDE/kdebase/workspace/plasma/applets/devicenotifier/devicespaceinfodelegate.h
> PRE-CREATION
> >
> trunk/KDE/kdebase/workspace/plasma/applets/devicenotifier/devicespaceinfodelegate.cpp
> PRE-CREATION
> >
> trunk/KDE/kdebase/workspace/plasma/applets/devicenotifier/notifierdialog.h
> 965209
> >
> trunk/KDE/kdebase/workspace/plasma/applets/devicenotifier/notifierdialog.cpp
> 965209
> >
> trunk/KDE/kdebase/workspace/plasma/applets/devicenotifier/notifierview.cpp
> 965209
> >
> > Diff: http://reviewboard.kde.org/r/632/diff
> >
> >
> > Testing
> > -------
> >
> >
> > Thanks,
> >
> > mck182
> >
> >
>
> _______________________________________________
> Plasma-devel mailing list
> Plasma-devel at kde.org
> https://mail.kde.org/mailman/listinfo/plasma-devel
>

I've updated the patch and now it correctly shows the capacity bar. There's
only a little issue: as soon as the device gets "mounted" the engine does
not return a valid "Free Space" data. This is probably a problem with the
engine or, worse, with solid.. Will check it out though..



-- 
Alessandro Diaferia
KDE Developer
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.kde.org/pipermail/plasma-devel/attachments/20090818/490fbef7/attachment-0001.htm 
-------------- next part --------------
Index: devicenotifier.cpp
===================================================================
--- devicenotifier.cpp	(revision 1012848)
+++ devicenotifier.cpp	(working copy)
@@ -180,6 +180,10 @@ void DeviceNotifier::dataUpdated(const Q
 
         //data from soliddevice engine
     } else if (data["Device Types"].toStringList().contains("Storage Access")) {
+        QList<QVariant> freeSpaceData;
+        freeSpaceData << QVariant(0) << QVariant(0);
+        m_dialog->setDeviceData(source, QVariant(freeSpaceData), NotifierDialog::DeviceFreeSpaceRole);
+
         //kDebug() << "DeviceNotifier::solidDeviceEngine updated" << source;
         if (data["Accessible"].toBool() == true) {
             m_dialog->setUnMount(true,source);
@@ -188,6 +192,12 @@ void DeviceNotifier::dataUpdated(const Q
             QStringList overlays;
             overlays << "emblem-mounted";
             m_dialog->setDeviceData(source, KIcon(m_dialog->getDeviceData(source,NotifierDialog::IconNameRole).toString(), NULL, overlays), Qt::DecorationRole);
+ 
+            if (data["Free Space"].isValid()) {
+                QList<QVariant> freeSpaceData;
+                freeSpaceData << data["Size"] << data["Free Space"];
+                m_dialog->setDeviceData(source, QVariant(freeSpaceData), NotifierDialog::DeviceFreeSpaceRole);
+            }
         } else if (data["Device Types"].toStringList().contains("OpticalDisc")) {
             //Unmounted optical drive
             m_dialog->setDeviceData(source, KIcon("media-eject"), Qt::DecorationRole);
Index: notifierdialog.cpp
===================================================================
--- notifierdialog.cpp	(revision 1012848)
+++ notifierdialog.cpp	(working copy)
@@ -51,6 +51,7 @@
 //own
 #include "notifierview.h"
 #include "devicenotifier.h"
+#include "devicespaceinfodelegate.h"
 
 using namespace Notifier;
 using namespace Plasma;
@@ -273,7 +274,9 @@ void NotifierDialog::buildDialog()
     m_notifierView->setMinimumSize(150,300);
     m_notifierView->setFocusPolicy(Qt::NoFocus);
 
-    Plasma::Delegate *delegate = new Delegate(this);
+
+    DeviceSpaceInfoDelegate *delegate = new DeviceSpaceInfoDelegate(this);
+    //Plasma::Delegate *delegate = new Delegate(this);
     //map the roles of m_hotplugModel into the standard Plasma::Delegate roles
     delegate->setRoleMapping(Plasma::Delegate::SubTitleRole, ActionRole);
     delegate->setRoleMapping(Plasma::Delegate::ColumnTypeRole, ScopeRole);
Index: devicespaceinfodelegate.cpp
===================================================================
--- devicespaceinfodelegate.cpp	(revision 0)
+++ devicespaceinfodelegate.cpp	(revision 0)
@@ -0,0 +1,73 @@
+/*  Copyright 2009 by Martin Klapetek <martin.klapetek at gmail.com>
+
+    This library is free software; you can redistribute it and/or
+    modify it under the terms of the GNU Library General Public
+    License as published by the Free Software Foundation; either
+    version 2 of the License, or (at your option) any later version.
+
+    This library 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
+    Library General Public License for more details.
+
+    You should have received a copy of the GNU Library General Public License
+    along with this library; see the file COPYING.LIB.  If not, write to
+    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+    Boston, MA 02110-1301, USA.
+*/
+#include "devicespaceinfodelegate.h"
+
+#include <kcapacitybar.h>
+#include <KLocale>
+#include <KGlobal>
+#include <KDebug>
+
+DeviceSpaceInfoDelegate::DeviceSpaceInfoDelegate(QObject* parent)
+    : Plasma::Delegate(parent),
+    m_capacityBar(0)
+{
+    m_capacityBar = new KCapacityBar(KCapacityBar::DrawTextInline);
+    m_capacityBar->setMaximumWidth(200);
+    m_capacityBar->setMinimumWidth(200);
+}
+
+DeviceSpaceInfoDelegate::~DeviceSpaceInfoDelegate()
+{
+    delete m_capacityBar;
+}
+
+void DeviceSpaceInfoDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
+{
+    painter->save();
+    Plasma::Delegate::paint(painter, option, index);
+
+    QVariantList sizes = index.data(Qt::UserRole + 7).value<QVariantList>(); // DeviceFreeSpaceRole =  Qt::UserRole + 7
+    qulonglong size = 0;
+    if (sizes.count() == 2) {
+        kDebug() << sizes[0].typeName();
+	kDebug() << sizes[1].typeName();
+        size = qulonglong(sizes[0].toULongLong());
+	kDebug() << size;
+        qulonglong freeSpace = qulonglong(sizes[1].toULongLong());
+        qulonglong usedSpace = size - freeSpace;
+
+	m_capacityBar->setText(i18nc("@info:status Free disk space", "%1  free", KGlobal::locale()->formatByteSize(freeSpace)));
+        m_capacityBar->setUpdatesEnabled(false);
+        m_capacityBar->setValue(size > 0 ? (usedSpace * 100) / size : 0);
+        m_capacityBar->setUpdatesEnabled(true);
+    }
+    if (size) {
+        m_capacityBar->drawCapacityBar(painter, QRect(QPoint(option.rect.x()+48, Plasma::Delegate::rectAfterTitle(option, index).bottom()-5),  QSize(120, 14)));
+    }
+    painter->restore();
+}
+
+QSize DeviceSpaceInfoDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const
+{
+    Q_UNUSED(option)
+    Q_UNUSED(index)
+
+    QSize size = Plasma::Delegate::sizeHint(option, index);
+    size.setHeight(size.height()+m_capacityBar->barHeight()+14); //+14 is for kcapacitybar padding
+    return size;
+}
Index: notifierdialog.h
===================================================================
--- notifierdialog.h	(revision 1012848)
+++ notifierdialog.h	(working copy)
@@ -69,9 +69,10 @@ namespace Notifier
               ActionRole = Qt::UserRole + 3,
               IconNameRole = Qt::UserRole + 4,
               ScopeRole = Qt::UserRole + 5,
-              SubTitleMandatoryRole = Qt::UserRole + 6
+              SubTitleMandatoryRole = Qt::UserRole + 6,
+              DeviceFreeSpaceRole =  Qt::UserRole + 7
           };
-  
+
           /**
           * Constructor of the dialog
           * @param notifier the notifier attached to this dialog
Index: devicespaceinfodelegate.h
===================================================================
--- devicespaceinfodelegate.h	(revision 0)
+++ devicespaceinfodelegate.h	(revision 0)
@@ -0,0 +1,45 @@
+/* Copyright 2009 by Martin Klapetek <martin.klapetek at gmail.com>
+
+   This library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Library General Public
+   License version 2 as published by the Free Software Foundation.
+
+   This library 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
+   Library General Public License for more details.
+
+   You should have received a copy of the GNU Library General Public License
+   along with this library; see the file COPYING.LIB.  If not, write to
+   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+   Boston, MA 02110-1301, USA.
+*/
+
+#ifndef DEVICESPACEINFODELEGATE_H
+#define DEVICESPACEINFODELEGATE_H
+
+#include <plasma/delegate.h>
+
+#include <QStyleOptionViewItem>
+#include <QModelIndex>
+
+#include <QStandardItemModel>
+#include <QPainter>
+
+
+class KCapacityBar;
+
+class DeviceSpaceInfoDelegate : public Plasma::Delegate
+{
+    Q_OBJECT
+public:
+    DeviceSpaceInfoDelegate(QObject *parent = 0);
+    virtual ~DeviceSpaceInfoDelegate();
+    virtual void paint(QPainter *painter, const QStyleOptionViewItem & option, const QModelIndex & index) const;
+protected:
+    virtual QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex & index) const;
+private:
+    KCapacityBar *m_capacityBar;
+};
+
+#endif // DEVICESPACEINFODELEGATE_H
Index: notifierview.cpp
===================================================================
--- notifierview.cpp	(revision 1012848)
+++ notifierview.cpp	(working copy)
@@ -18,6 +18,8 @@
 */
 
 #include "notifierview.h"
+#include "devicespaceinfodelegate.h"
+#include "notifierdialog.h"
 
 // Qt
 
@@ -280,7 +282,10 @@ void NotifierView::paintItem(QPainter &p
     if (index == currentIndex()) {
         option.state |= QStyle::State_HasFocus;
     }
-
+    QList<QVariant> freeSpaceData = index.data(NotifierDialog::DeviceFreeSpaceRole).toList();
+    if (freeSpaceData.count() > 0) {
+        ((DeviceSpaceInfoDelegate*)itemDelegate(index))->setData(freeSpaceData[0], freeSpaceData[1]);
+    }
     itemDelegate(index)->paint(&painter,option,index);
 }
 
Index: CMakeLists.txt
===================================================================
--- CMakeLists.txt	(revision 1012848)
+++ CMakeLists.txt	(working copy)
@@ -3,7 +3,8 @@ project(plasma-devicenotifier)
 set(devicenotifier_SRCS
     devicenotifier.cpp
     notifierdialog.cpp
-    notifierview.cpp)
+    notifierview.cpp
+    devicespaceinfodelegate.cpp)
 
 kde4_add_plugin(plasma_applet_devicenotifier ${devicenotifier_SRCS})
 target_link_libraries(plasma_applet_devicenotifier ${KDE4_PLASMA_LIBS} ${KDE4_KIO_LIBS} ${KDE4_SOLID_LIBS})


More information about the Plasma-devel mailing list