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

KDEUI

kcategorydrawer.cpp

Go to the documentation of this file.
00001 
00021 #include "kcategorydrawer.h"
00022 
00023 #include <QPainter>
00024 #include <QStyleOption>
00025 #include <QApplication>
00026 
00027 #include <kiconloader.h>
00028 #include <kcategorizedsortfilterproxymodel.h>
00029 
00030 #define HORIZONTAL_HINT 3
00031 
00032 KCategoryDrawer::KCategoryDrawer()
00033 {
00034 }
00035 
00036 KCategoryDrawer::~KCategoryDrawer()
00037 {
00038 }
00039 
00040 void KCategoryDrawer::drawCategory(const QModelIndex &index,
00041                                    int /*sortRole*/,
00042                                    const QStyleOption &option,
00043                                    QPainter *painter) const
00044 {
00045     const QString category = index.model()->data(index, KCategorizedSortFilterProxyModel::CategoryDisplayRole).toString();
00046 
00047     QColor color;
00048 
00049     if (option.state & QStyle::State_Selected)
00050     {
00051         color = option.palette.color(QPalette::HighlightedText);
00052     }
00053     else
00054     {
00055         color = option.palette.color(QPalette::Text);
00056     }
00057 
00058     painter->save();
00059     painter->setRenderHint(QPainter::Antialiasing);
00060 
00061     QStyleOptionViewItemV4 viewOptions;
00062     viewOptions.rect = option.rect;
00063     viewOptions.palette = option.palette;
00064     viewOptions.direction = option.direction;
00065     viewOptions.state = option.state;
00066     viewOptions.viewItemPosition = QStyleOptionViewItemV4::OnlyOne;
00067     QApplication::style()->drawPrimitive(QStyle::PE_PanelItemViewItem, &viewOptions, painter, 0);
00068 
00069     QFont painterFont = painter->font();
00070     painterFont.setWeight(QFont::Bold);
00071     QFontMetrics metrics(painterFont);
00072     painter->setFont(painterFont);
00073 
00074     QRect lineRect(option.rect.left(),
00075                    option.rect.bottom() - 1,
00076                    option.rect.width(),
00077                    1);
00078 
00079     QLinearGradient gradient(option.rect.topLeft(),
00080                              option.rect.bottomRight());
00081     gradient.setColorAt(option.direction == Qt::LeftToRight ? 0
00082                                                             : 1, color);
00083     gradient.setColorAt(option.direction == Qt::LeftToRight ? 1
00084                                                             : 0, Qt::transparent);
00085 
00086     painter->fillRect(lineRect, gradient);
00087 
00088     painter->setPen(color);
00089 
00090     QRect textRect(option.rect);
00091     textRect.setLeft(textRect.left() + HORIZONTAL_HINT);
00092     textRect.setRight(textRect.right() - HORIZONTAL_HINT);
00093     painter->drawText(textRect, Qt::AlignVCenter | Qt::AlignLeft,
00094     metrics.elidedText(category, Qt::ElideRight, option.rect.width()));
00095 
00096     painter->restore();
00097 }
00098 
00099 int KCategoryDrawer::categoryHeight(const QModelIndex &index, const QStyleOption &option) const
00100 {
00101     Q_UNUSED(index);
00102 
00103     return option.fontMetrics.height() + 4 /* 3 separator; 1 gradient */;
00104 }

KDEUI

Skip menu "KDEUI"
  • Main Page
  • Modules
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • Kross
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs 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