• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

Applets

itemdelegate.cpp

Go to the documentation of this file.
00001 /*
00002     Copyright 2007 Robert Knight <robertknight@gmail.com>
00003     Copyright 2007 Kevin Ottens <ervin@kde.org>
00004 
00005     This library is free software; you can redistribute it and/or
00006     modify it under the terms of the GNU Library General Public
00007     License as published by the Free Software Foundation; either
00008     version 2 of the License, or (at your option) any later version.
00009 
00010     This library is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013     Library General Public License for more details.
00014 
00015     You should have received a copy of the GNU Library General Public License
00016     along with this library; see the file COPYING.LIB.  If not, write to
00017     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018     Boston, MA 02110-1301, USA.
00019 */
00020 
00021 // Own
00022 #include "ui/itemdelegate.h"
00023 
00024 // Qt
00025 #include <QApplication>
00026 #include <QFontMetrics>
00027 #include <QIcon>
00028 #include <QModelIndex>
00029 #include <QPainter>
00030 #include <QStyle>
00031 #include <QStyleOptionViewItem>
00032 #include <QStyleOptionProgressBar>
00033 
00034 // KDE
00035 #include <KColorUtils>
00036 #include <KDebug>
00037 #include <KGlobal>
00038 #include <KGlobalSettings>
00039 #include <kcapacitybar.h>
00040 
00041 // plasma
00042 #include <Plasma/Plasma>
00043 
00044 using namespace Kickoff;
00045 
00046 ItemDelegate::ItemDelegate(QObject *parent)
00047         : Plasma::Delegate(parent)
00048 {
00049 }
00050 
00051 void ItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
00052 {
00053     Plasma::Delegate::paint(painter, option, index);
00054 
00055     qreal freeSpace = -1;
00056     qreal usedSpace = -1;
00057     if (!index.data(DiskFreeSpaceRole).isNull()) {
00058         freeSpace = index.data(DiskFreeSpaceRole).value<int>() / 1024.0 / 1024.0;
00059         usedSpace = index.data(DiskUsedSpaceRole).value<int>() / 1024.0 / 1024.0;
00060     }
00061 
00062 
00063     // draw free space information (for drive icons)
00064     if (usedSpace >= 0) {
00065         painter->save();
00066 
00067         QRect emptyRect = rectAfterTitle(option, index);
00068 
00069         QSize barSize = QSize(qMin(emptyRect.width(), option.rect.width() / 3), emptyRect.height());
00070 
00071         if (barSize.width() > 0) {
00072             // if the item view is gradually resized smaller or larger, make the bar fade out/in
00073             // as enough space for it becomes available
00074             if (barSize.width() < 20.0) {
00075                 painter->setOpacity(barSize.width() / 20.0);
00076             }
00077 
00078             QRect spaceRect = QStyle::alignedRect(option.direction,
00079                                                   Qt::AlignRight, barSize, emptyRect);
00080 
00081             if (!(option.state & (QStyle::State_Selected | QStyle::State_MouseOver | QStyle::State_HasFocus))) {
00082                 painter->setOpacity(painter->opacity() / 2.5);
00083             } else {
00084             }
00085 
00086             KCapacityBar capacityBar(KCapacityBar::DrawTextInline);
00087             capacityBar.setValue((usedSpace / (freeSpace + usedSpace))*100);
00088             capacityBar.drawCapacityBar(painter, spaceRect);
00089 
00090             // -- Removed the free space text because it added too much 'visual noise' to the item
00091             //
00092             // some precision is lost here, but it is acceptible given that the disk-free bar
00093             // is only shown as a guide
00094             // QString freeSpaceString = KGlobal::locale()->formatByteSize(freeSpace*1024*1024*1024);
00095             // painter->drawText(spaceRect,Qt::AlignCenter,i18n("%1 free",freeSpaceString));
00096         }
00097 
00098         painter->restore();
00099     }
00100 
00101 }
00102 
00103 bool ItemDelegate::isVisible(const QModelIndex& index) const
00104 {
00105     Q_ASSERT(index.isValid());
00106 
00107     if (index.model()->hasChildren(index)) {
00108         int childCount = index.model()->rowCount(index);
00109         for (int i = 0; i < childCount; ++i) {
00110             QModelIndex child = index.model()->index(i, 0, index);
00111             if (!child.data(UrlRole).isNull()) {
00112                 return true;
00113             }
00114         }
00115         return false;
00116     }
00117 
00118     return !index.data(UrlRole).isNull();
00119 }
00120 

Applets

Skip menu "Applets"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members

API Reference

Skip menu "API Reference"
  • KWin
  •   KWin Libraries
  • Libraries
  •   libkworkspace
  •   libsolidcontrol
  •   libtaskmanager
  • Plasma
  •   Animators
  •   Applets
  •   Engines
  • Solid Modules
Generated for API Reference by doxygen 1.5.7
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal