1
/********************************************************************
2
KWin - the KDE window manager
3
This file is part of the KDE project.
5
Copyright (C) 2009 Martin Gräßlin <kde@martin-graesslin.com>
7
This program is free software; you can redistribute it and/or modify
8
it under the terms of the GNU General Public License as published by
9
the Free Software Foundation; either version 2 of the License, or
10
(at your option) any later version.
12
This program is distributed in the hope that it will be useful,
13
but WITHOUT ANY WARRANTY; without even the implied warranty of
14
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
GNU General Public License for more details.
17
You should have received a copy of the GNU General Public License
18
along with this program. If not, see <http://www.gnu.org/licenses/>.
19
*********************************************************************/
21
#include "desktopitemdelegate.h"
23
#include "clientitemdelegate.h"
24
#include "clientmodel.h"
25
#include "desktopmodel.h"
26
#include "tabboxconfig.h"
30
#include <KGlobalSettings>
32
#include <KIconEffect>
33
#include <KIconLoader>
34
#include <Plasma/FrameSvg>
35
#include <Plasma/Theme>
42
DesktopItemDelegate::DesktopItemDelegate(QObject* parent)
43
: QAbstractItemDelegate(parent)
45
m_frame = new Plasma::FrameSvg(this);
46
m_frame->setImagePath("widgets/viewitem");
47
m_frame->setElementPrefix("hover");
48
m_frame->setCacheAllRenderedFrames(true);
49
m_frame->setEnabledBorders(Plasma::FrameSvg::AllBorders);
50
m_clientDelegate = new ClientItemDelegate(this);
53
DesktopItemDelegate::~DesktopItemDelegate()
57
void DesktopItemDelegate::setConfig(const KWin::TabBox::ItemLayoutConfig& config)
62
void DesktopItemDelegate::setLayouts(QMap< QString, ItemLayoutConfig >& layouts)
67
QSize DesktopItemDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const
75
for (int i = 0; i < m_config.count(); i++) {
76
QSizeF row = rowSize(index, i);
77
width = qMax<qreal>(width, row.width());
78
height += row.height();
80
qreal left, top, right, bottom;
81
m_frame->getMargins(left, top, right, bottom);
83
// find icon elements which have a row span
84
for (int i = 0; i < m_config.count(); i++) {
85
ItemLayoutConfigRow row = m_config.row(i);
86
for (int j = 0; j < row.count(); j++) {
87
ItemLayoutConfigRowElement element = row.element(j);
88
if (element.type() == ItemLayoutConfigRowElement::ElementIcon && element.isRowSpan())
89
height = qMax<qreal>(height, element.iconSize().height());
92
return QSize(width + left + right, height + top + bottom);
95
void DesktopItemDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
99
qreal left, top, right, bottom;
100
m_frame->getMargins(left, top, right, bottom);
102
qreal y = option.rect.top() + top;
103
for (int i = 0; i < m_config.count(); i++) {
104
qreal rowHeight = rowSize(index, i).height();
105
qreal x = option.rect.left() + left ;
106
ItemLayoutConfigRow row = m_config.row(i);
107
for (int j = 0; j < row.count(); j++) {
108
ItemLayoutConfigRowElement element = row.element(j);
109
switch(element.type()) {
110
case ItemLayoutConfigRowElement::ElementDesktopName: {
111
x += paintTextElement(painter, option, element, x, y, rowHeight,
112
index.model()->data(index, Qt::DisplayRole).toString());
115
case ItemLayoutConfigRowElement::ElementIcon: {
116
qreal rectWidth = (qreal)option.rect.width();
117
qreal maxWidth = qMin<qreal>(element.width(), rectWidth);
118
if (element.isStretch())
119
maxWidth = qMax<qreal>(maxWidth, option.rect.left() + option.rect.width() - x);
120
qreal iconX = x + maxWidth * 0.5 - element.iconSize().width() * 0.5;
121
qreal iconY = y + rowHeight * 0.5 - element.iconSize().height() * 0.5;
122
if (element.isRowSpan())
123
iconY = option.rect.top() + option.rect.height() * 0.5 - element.iconSize().height() * 0.5;
124
QRectF iconRect = QRectF(iconX, iconY, element.iconSize().width(), element.iconSize().height());
128
KIcon icon("user-desktop");
129
QPixmap iconPixmap = icon.pixmap(QSize(64, 64));
130
if (m_config.isHighlightSelectedIcons() && option.state & QStyle::State_Selected) {
131
KIconEffect *effect = KIconLoader::global()->iconEffect();
132
iconPixmap = effect->apply(iconPixmap, KIconLoader::Desktop, KIconLoader::ActiveState);
134
if (m_config.isGrayscaleDeselectedIcons() && !(option.state & QStyle::State_Selected)) {
135
KIconEffect *effect = KIconLoader::global()->iconEffect();
136
iconPixmap = effect->apply(iconPixmap, KIconLoader::Desktop, KIconLoader::DisabledState);
138
QRectF sourceRect = QRectF(0.0, 0.0, iconPixmap.width(), iconPixmap.height());
139
painter->drawPixmap(iconRect, iconPixmap, sourceRect);
141
x += element.width();
144
case ItemLayoutConfigRowElement::ElementEmpty:
145
x += element.width();
147
case ItemLayoutConfigRowElement::ElementClientList: {
148
m_clientDelegate->setConfig(m_layouts[ element.clientListLayoutName()]);
149
ClientModel* clientModel = static_cast< ClientModel* >(
150
index.model()->data(index, DesktopModel::ClientModelRole).value< void* >());
151
TabBoxClientList clients = clientModel->clientList();
154
qreal itemWidth = element.width();
155
if (element.isStretch())
156
itemWidth = option.rect.x() + option.rect.width() - x;
157
foreach (TabBoxClient * client, clients) {
160
QModelIndex clientIndex = clientModel->index(client);
161
QStyleOptionViewItem itemOption = option;
162
itemOption.state = QStyle::State_Item;
163
QSizeF itemSize = m_clientDelegate->sizeHint(itemOption, clientIndex);
164
// TabBoxHandler gives the same config to the client models as used here
165
// so row and column do not represent the actual used layout for this list
166
switch(element.clientListLayoutMode()) {
167
case TabBoxConfig::HorizontalLayout:
168
itemOption.rect = QRect(partX + x, y, itemWidth - partX, rowHeight);
169
partX += qMin(itemWidth - partX, itemSize.width());
171
case TabBoxConfig::VerticalLayout:
172
itemOption.rect = QRect(x, y + partY, itemWidth, itemSize.height());
173
partY += itemSize.height();
175
case TabBoxConfig::HorizontalVerticalLayout:
181
if ((partX > itemWidth) || (partX + x > option.rect.x() + option.rect.width())) {
185
if ((partY > rowHeight) || (y + partY > option.rect.y() + option.rect.height())) {
189
m_clientDelegate->paint(painter, itemOption, clientIndex);
202
qreal DesktopItemDelegate::paintTextElement(QPainter* painter, const QStyleOptionViewItem& option,
203
const ItemLayoutConfigRowElement& element,
204
const qreal& x, const qreal& y, const qreal& rowHeight, QString text) const
207
QFont font = KGlobalSettings::generalFont();
208
if (element.isSmallTextSize())
209
font = KGlobalSettings::smallestReadableFont();
210
font.setBold(element.isBold());
211
font.setItalic(element.isItalic());
212
text = element.prefix() + text + element.suffix();
213
painter->setFont(font);
214
painter->setPen(Plasma::Theme::defaultTheme()->color(Plasma::Theme::TextColor));
215
qreal width = element.width();
216
if (element.isStretch()) {
217
qreal left, top, right, bottom;
218
m_frame->getMargins(left, top, right, bottom);
219
width = option.rect.left() + option.rect.width() - x - right;
221
text = QFontMetricsF(font).elidedText(text, Qt::ElideRight, width);
222
QRectF rect = QRectF(x, y, width, rowHeight);
223
painter->drawText(rect, element.alignment() | Qt::TextSingleLine, text);
228
QSizeF DesktopItemDelegate::rowSize(const QModelIndex& index, int row) const
230
ItemLayoutConfigRow currentRow = m_config.row(row);
231
qreal rowWidth = 0.0;
232
qreal rowHeight = 0.0;
233
for (int j = 0; j < currentRow.count(); j++) {
234
ItemLayoutConfigRowElement element = currentRow.element(j);
235
switch(element.type()) {
236
case ItemLayoutConfigRowElement::ElementDesktopName: {
237
QSizeF size = textElementSizeHint(index, element,
238
index.model()->data(index, Qt::DisplayRole).toString());
239
rowWidth += size.width();
240
rowHeight = qMax<qreal>(rowHeight, size.height());
243
case ItemLayoutConfigRowElement::ElementIcon:
244
rowWidth += qMax<qreal>(element.iconSize().width(), element.width());
245
if (!element.isRowSpan())
246
rowHeight = qMax<qreal>(rowHeight, element.iconSize().height());
248
case ItemLayoutConfigRowElement::ElementEmpty:
249
rowWidth += element.width();
251
case ItemLayoutConfigRowElement::ElementClientList: {
252
m_clientDelegate->setConfig(m_layouts[ element.clientListLayoutName()]);
253
ClientModel* clientModel = static_cast< ClientModel* >(
254
index.model()->data(index, DesktopModel::ClientModelRole).value< void* >());
255
TabBoxClientList clients = clientModel->clientList();
256
qreal elementWidth = 0.0;
257
qreal elementHeight = 0.0;
258
foreach (TabBoxClient * client, clients) {
261
QModelIndex clientIndex = clientModel->index(client);
262
QStyleOptionViewItem itemOption = QStyleOptionViewItem();
263
QSizeF itemSize = m_clientDelegate->sizeHint(itemOption, clientIndex);
264
switch(element.clientListLayoutMode()) {
265
case TabBoxConfig::HorizontalLayout:
266
elementWidth += itemSize.width();
267
elementHeight = qMax<qreal>(elementHeight, itemSize.height());
269
case TabBoxConfig::VerticalLayout:
270
elementWidth = qMax<qreal>(elementWidth, itemSize.width());
271
elementHeight += itemSize.height();
273
case TabBoxConfig::HorizontalVerticalLayout:
280
if (element.isStretch())
281
rowWidth += elementWidth;
283
rowWidth += qMin(elementWidth, element.width());
284
rowHeight = qMax<qreal>(rowHeight, elementHeight);
291
return QSizeF(rowWidth, rowHeight);
294
QSizeF DesktopItemDelegate::textElementSizeHint(const QModelIndex& index, const ItemLayoutConfigRowElement& element, QString text) const
297
QFont font = KGlobalSettings::generalFont();
298
if (element.isSmallTextSize())
299
font = KGlobalSettings::smallestReadableFont();
300
font.setBold(element.isBold());
301
font.setItalic(element.isItalic());
302
text = element.prefix() + text + element.suffix();
303
QFontMetricsF fm(font);
304
qreal width = element.width();
305
if (element.isStretch())
306
width = fm.width(text);
307
qreal height = fm.boundingRect(text).height();
308
return QSizeF(width, height);
311
} // namespace Tabbox