• Skip to content
  • Skip to link menu
KDE 4.1 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 
00040 // plasma
00041 #include <plasma/plasma.h>
00042 
00043 using namespace Kickoff;
00044 
00045 ItemDelegate::ItemDelegate(QObject *parent)
00046     : Plasma::Delegate(parent)
00047 {
00048 }
00049 
00050 void ItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
00051 {
00052   Plasma::Delegate::paint(painter, option, index);
00053 
00054     qreal freeSpace = -1;
00055     qreal usedSpace = -1;
00056     if (!index.data(DiskFreeSpaceRole).isNull()) {
00057         freeSpace = index.data(DiskFreeSpaceRole).value<int>()/1024.0/1024.0;
00058         usedSpace = index.data(DiskUsedSpaceRole).value<int>()/1024.0/1024.0;
00059     }
00060 
00061 
00062     // draw free space information (for drive icons)
00063     if (usedSpace >= 0) {
00064         painter->save();
00065 
00066         QRect emptyRect = rectAfterTitle(option, index);
00067 
00068         QSize barSize = QSize(qMin(emptyRect.width(), option.rect.width()/3), emptyRect.height());
00069 
00070         if (barSize.width() > 0) {
00071             // if the item view is gradually resized smaller or larger, make the bar fade out/in
00072             // as enough space for it becomes available
00073             if (barSize.width() < 20.0) {
00074                 painter->setOpacity(barSize.width()/20.0);
00075             }
00076 
00077             QRectF spaceRect = QStyle::alignedRect(option.direction,
00078                                                    Qt::AlignRight, barSize, emptyRect);
00079 
00080             QStyleOptionProgressBar optionPBar;
00081 
00082             if (!(option.state & (QStyle::State_Selected|QStyle::State_MouseOver|QStyle::State_HasFocus))) {
00083                 painter->setOpacity(painter->opacity()/2.5);
00084                 optionPBar.state = QStyle::State_None;
00085             } else {
00086                 optionPBar.state = QStyle::State_Enabled;
00087             }
00088 
00089             optionPBar.rect = spaceRect.toRect();
00090             optionPBar.minimum = 0;
00091             optionPBar.maximum = 100;
00092             optionPBar.progress = (usedSpace / (freeSpace + usedSpace))*100;
00093 
00094             QApplication::style()->drawControl(QStyle::CE_ProgressBar, &optionPBar, painter);
00095 
00096             // -- Removed the free space text because it added too much 'visual noise' to the item
00097             //
00098                 // some precision is lost here, but it is acceptible given that the disk-free bar
00099                 // is only shown as a guide
00100                 // QString freeSpaceString = KGlobal::locale()->formatByteSize(freeSpace*1024*1024*1024);
00101                 // painter->drawText(spaceRect,Qt::AlignCenter,i18n("%1 free",freeSpaceString));
00102         }
00103 
00104         painter->restore();
00105     }
00106 
00107 }
00108 
00109 bool ItemDelegate::isVisible(const QModelIndex& index) const
00110 {
00111     Q_ASSERT(index.isValid());
00112 
00113     if (index.model()->hasChildren(index)) {
00114         int childCount = index.model()->rowCount(index);
00115         for (int i=0; i<childCount; ++i) {
00116             QModelIndex child = index.model()->index(i, 0, index);
00117             if (!child.data(UrlRole).isNull()) {
00118                 return true;
00119             }
00120         }
00121         return false;
00122     }
00123 
00124     return !index.data(UrlRole).isNull();
00125 }
00126 

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
  •   libplasma
  •   libsolidcontrol
  •   libtaskmanager
  • Plasma
  •   Animators
  •   Applets
  •   Engines
  • Solid Modules
Generated for API Reference by doxygen 1.5.4
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