2 Copyright (c) 2007 Paolo Capriotti <p.capriotti@gmail.com>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
10 #include "backgrounddelegate.h"
12 #include <KGlobalSettings>
15 #include "backgroundpackage.h"
17 BackgroundDelegate::BackgroundDelegate(QObject
*listener
,
18 float ratio
, QObject
*parent
)
19 : QAbstractItemDelegate(parent
)
20 , m_listener(listener
)
25 void BackgroundDelegate::paint(QPainter
*painter
,
26 const QStyleOptionViewItem
&option
,
27 const QModelIndex
&index
) const
29 QString title
= index
.model()->data(index
, Qt::DisplayRole
).toString();
30 QString author
= index
.model()->data(index
, AuthorRole
).toString();
31 QPixmap pix
= index
.model()->data(index
, ScreenshotRole
).value
<QPixmap
>();
33 // draw selection outline
34 if (option
.state
& QStyle::State_Selected
) {
35 QPen oldPen
= painter
->pen();
36 painter
->setPen(option
.palette
.color(QPalette::Highlight
));
37 painter
->drawRect(option
.rect
.adjusted(2, 2, -2, -2));
38 painter
->setPen(oldPen
);
42 int maxheight
= Background::SCREENSHOT_HEIGHT
;
43 int maxwidth
= int(maxheight
* m_ratio
);
45 QSize sz
= pix
.size();
46 int x
= MARGIN
+ (maxwidth
- pix
.width()) / 2;
47 int y
= MARGIN
+ (maxheight
- pix
.height()) / 2;
48 QRect imgRect
= QRect(option
.rect
.topLeft(), pix
.size()).translated(x
, y
);
49 painter
->drawPixmap(imgRect
, pix
);
54 QFont font
= painter
->font();
55 font
.setWeight(QFont::Bold
);
56 painter
->setFont(font
);
57 int x
= option
.rect
.left() + MARGIN
* 5 + maxwidth
;
60 option
.rect
.top() + MARGIN
,
61 option
.rect
.width() - x
- MARGIN
* 2,
64 QString authorCaption
;
65 if (!author
.isEmpty()) {
66 authorCaption
= i18nc("Caption to wallpaper preview, %1 author name",
68 text
+= '\n' + authorCaption
;
70 QRect boundingRect
= painter
->boundingRect(
71 textRect
, Qt::AlignVCenter
| Qt::TextWordWrap
, text
);
72 painter
->drawText(boundingRect
, Qt::TextWordWrap
, title
);
73 if (!author
.isEmpty()) {
74 QRect titleRect
= painter
->boundingRect(boundingRect
, Qt::TextWordWrap
, title
);
75 QRect
authorRect(titleRect
.bottomLeft(), textRect
.size());
76 painter
->setFont(KGlobalSettings::smallestReadableFont());
77 painter
->drawText(authorRect
, Qt::TextWordWrap
, authorCaption
);
83 QSize
BackgroundDelegate::sizeHint(const QStyleOptionViewItem
&,
84 const QModelIndex
&) const
86 return QSize(100, Background::SCREENSHOT_HEIGHT
+ MARGIN
* 2);