delay a few things on startup, such as setting the visibility mode, which ensures...
[personal-kdebase.git] / apps / dolphin / src / dolphincategorydrawer.cpp
blob881743377c9a9cb6eb68164094c72586e8aa853f
1 /**
2 * This file is part of the KDE project
3 * Copyright (C) 2007 Rafael Fernández López <ereslibre@kde.org>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB. If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
21 #include "dolphincategorydrawer.h"
23 #include <config-nepomuk.h>
25 #include <QPainter>
26 #include <QFile>
27 #include <QDir>
28 #include <QApplication>
29 #include <QStyleOption>
31 #ifdef HAVE_NEPOMUK
32 #include <nepomuk/kratingpainter.h>
33 #endif
35 #include <kiconloader.h>
36 #include <kcategorizedsortfilterproxymodel.h>
37 #include <qimageblitz.h>
38 #include <kuser.h>
40 #include "dolphinview.h"
41 #include "dolphinmodel.h"
43 #define HORIZONTAL_HINT 3
45 DolphinCategoryDrawer::DolphinCategoryDrawer()
49 DolphinCategoryDrawer::~DolphinCategoryDrawer()
53 void DolphinCategoryDrawer::drawCategory(const QModelIndex &index, int sortRole,
54 const QStyleOption &option, QPainter *painter) const
56 Q_UNUSED(sortRole);
58 QRect starRect = option.rect;
60 int iconSize = KIconLoader::global()->currentSize(KIconLoader::Small);
61 QVariant categoryVariant = index.model()->data(index, KCategorizedSortFilterProxyModel::CategoryDisplayRole);
63 if (!categoryVariant.isValid())
65 return;
68 const QString category = categoryVariant.toString();
70 QColor color;
72 if (option.state & QStyle::State_Selected)
74 color = option.palette.color(QPalette::HighlightedText);
76 else
78 color = option.palette.color(QPalette::Text);
81 painter->save();
82 painter->setRenderHint(QPainter::Antialiasing);
84 QStyleOptionButton opt;
86 opt.rect = option.rect;
87 opt.rect.setLeft(opt.rect.left() + HORIZONTAL_HINT);
88 opt.rect.setRight(opt.rect.right() - HORIZONTAL_HINT);
89 opt.palette = option.palette;
90 opt.direction = option.direction;
91 opt.text = category;
93 QStyleOptionViewItemV4 viewOptions;
94 viewOptions.rect = option.rect;
95 viewOptions.palette = option.palette;
96 viewOptions.direction = option.direction;
97 viewOptions.state = option.state;
98 viewOptions.viewItemPosition = QStyleOptionViewItemV4::OnlyOne;
99 QApplication::style()->drawPrimitive(QStyle::PE_PanelItemViewItem, &viewOptions, painter, 0);
101 QFont painterFont = painter->font();
102 painterFont.setWeight(QFont::Bold);
103 QFontMetrics metrics(painterFont);
104 painter->setFont(painterFont);
106 QPainterPath path;
107 path.addRect(option.rect.left(),
108 option.rect.bottom() - 1,
109 option.rect.width(),
112 QLinearGradient gradient(option.rect.topLeft(),
113 option.rect.bottomRight());
114 gradient.setColorAt(option.direction == Qt::LeftToRight ? 0
115 : 1, color);
116 gradient.setColorAt(option.direction == Qt::LeftToRight ? 1
117 : 0, Qt::transparent);
119 painter->setBrush(gradient);
120 painter->fillPath(path, gradient);
122 if (option.direction == Qt::LeftToRight)
124 opt.rect.setLeft(opt.rect.left());
125 starRect.setLeft(starRect.left());
126 starRect.setRight(starRect.right());
128 else
130 opt.rect.setRight(opt.rect.width());
131 starRect.setLeft(starRect.width());
132 starRect.setRight(starRect.width());
135 bool paintIcon = true;
136 bool paintText = true;
138 QPixmap icon;
139 switch (index.column()) {
140 case KDirModel::Name:
141 paintIcon = false;
142 break;
144 case KDirModel::Size:
145 paintIcon = false;
146 break;
148 case KDirModel::ModifiedTime:
149 paintIcon = false;
150 break;
152 case KDirModel::Permissions:
153 paintIcon = false; // TODO: let's think about how to represent permissions
154 break;
156 case KDirModel::Owner: {
157 opt.rect.setTop(option.rect.bottom() - (iconSize / 4));
158 KUser user(category);
159 QString faceIconPath = user.faceIconPath();
161 if (!faceIconPath.isEmpty())
163 icon = QPixmap::fromImage(QImage(faceIconPath).scaledToHeight(option.fontMetrics.height(), Qt::SmoothTransformation));
165 else
167 icon = KIconLoader::global()->loadIcon("user-identity", KIconLoader::NoGroup, option.fontMetrics.height());
170 opt.rect.setTop(opt.rect.top() - icon.height());
172 break;
175 case KDirModel::Group:
176 paintIcon = false;
177 break;
179 case KDirModel::Type: {
180 opt.rect.setTop(option.rect.bottom() - (iconSize / 4));
181 const KCategorizedSortFilterProxyModel *proxyModel = static_cast<const KCategorizedSortFilterProxyModel*>(index.model());
182 const DolphinModel *model = static_cast<const DolphinModel*>(proxyModel->sourceModel());
183 KFileItem item = model->itemForIndex(proxyModel->mapToSource(index));
184 // This is the only way of getting the icon right. Others will fail on corner
185 // cases like the item representing this group has been set a different icon,
186 // so the group icon drawn is that one particularly. This way assures the drawn
187 // icon is the one of the mimetype of the group itself. (ereslibre)
188 icon = KIconLoader::global()->loadMimeTypeIcon(item.mimeTypePtr()->iconName(),
189 KIconLoader::NoGroup, option.fontMetrics.height());
191 opt.rect.setTop(opt.rect.top() - icon.height());
193 break;
196 #ifdef HAVE_NEPOMUK
197 case DolphinModel::Rating: {
198 paintText = false;
199 paintIcon = false;
201 painter->setLayoutDirection( option.direction );
202 QRect ratingRect( option.rect );
203 ratingRect.setTop(option.rect.top() + (option.rect.height() / 2) - (iconSize / 2));
204 ratingRect.setHeight( iconSize );
205 KRatingPainter::paintRating( painter, ratingRect, Qt::AlignLeft, category.toInt() );
206 break;
209 case DolphinModel::Tags:
210 paintIcon = false;
211 break;
212 #endif
215 if (paintIcon) {
216 painter->drawPixmap(QRect(option.direction == Qt::LeftToRight ? opt.rect.left()
217 : opt.rect.right() - icon.width() + (iconSize / 4), opt.rect.top(), icon.width(), icon.height()), icon);
219 if (option.direction == Qt::LeftToRight)
221 opt.rect.setLeft(opt.rect.left() + icon.width() + (iconSize / 4));
223 else
225 opt.rect.setRight(opt.rect.right() + (iconSize / 4));
229 if (paintText) {
230 opt.rect.setTop(option.rect.top() + (iconSize / 4));
231 opt.rect.setBottom(opt.rect.bottom() - 1);
232 painter->setPen(color);
234 QRect textRect = opt.rect;
236 if (option.direction == Qt::RightToLeft)
238 textRect.setWidth(textRect.width() - (paintIcon ? icon.width() + (iconSize / 4)
239 : -(iconSize / 4)));
242 painter->drawText(textRect, Qt::AlignVCenter | Qt::AlignLeft,
243 metrics.elidedText(category, Qt::ElideRight, textRect.width()));
246 painter->restore();
249 int DolphinCategoryDrawer::categoryHeight(const QModelIndex &index, const QStyleOption &option) const
251 int iconSize = KIconLoader::global()->currentSize(KIconLoader::Small);
252 int heightWithoutIcon = option.fontMetrics.height() + (iconSize / 4) * 2 + 1; /* 1 pixel-width gradient */
253 bool paintIcon;
255 switch (index.column()) {
256 case KDirModel::Owner:
257 case KDirModel::Type:
258 paintIcon = true;
259 break;
260 default:
261 paintIcon = false;
264 if (paintIcon)
265 return qMax(heightWithoutIcon, iconSize + (iconSize / 4) * 2 + 1) /* 1 pixel-width gradient */;
267 return heightWithoutIcon;