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

Plasma

backgrounddelegate.cpp

Go to the documentation of this file.
00001 /*
00002   Copyright (c) 2007 Paolo Capriotti <p.capriotti@gmail.com>
00003 
00004   This program is free software; you can redistribute it and/or modify
00005   it under the terms of the GNU General Public License as published by
00006   the Free Software Foundation; either version 2 of the License, or
00007   (at your option) any later version.
00008 */
00009 
00010 #include "backgrounddelegate.h"
00011 
00012 #include <QApplication>
00013 #include <QPen>
00014 #include <QPainter>
00015 
00016 #include <KGlobalSettings>
00017 #include <KLocalizedString>
00018 
00019 BackgroundDelegate::BackgroundDelegate(QObject *listener, float ratio, QObject *parent)
00020     : QAbstractItemDelegate(parent), 
00021       m_listener(listener),
00022       m_ratio(ratio)
00023 {
00024 }
00025 
00026 void BackgroundDelegate::paint(QPainter *painter,
00027                                const QStyleOptionViewItem &option,
00028                                const QModelIndex &index) const
00029 {
00030     QString title = index.model()->data(index, Qt::DisplayRole).toString();
00031     QString author = index.model()->data(index, AuthorRole).toString();
00032     QString resolution = index.model()->data(index, ResolutionRole).toString();
00033     QPixmap pix = index.model()->data(index, ScreenshotRole).value<QPixmap>();
00034 
00035     // highlight selected item
00036     QApplication::style()->drawControl(QStyle::CE_ItemViewItem, &option, painter);
00037 
00038     // draw pixmap
00039     int maxheight = SCREENSHOT_SIZE;
00040     int maxwidth = int(maxheight * m_ratio);
00041     if (!pix.isNull()) {
00042         QSize sz = pix.size();
00043         int x = MARGIN + (maxwidth - pix.width()) / 2;
00044         int y = MARGIN + (maxheight - pix.height()) / 2;
00045         QRect imgRect = QRect(option.rect.topLeft(), pix.size()).translated(x, y);
00046         painter->drawPixmap(imgRect, pix);
00047     }
00048 
00049     // draw text
00050     painter->save();
00051     QFont font = painter->font();
00052     font.setWeight(QFont::Bold);
00053     painter->setFont(font);
00054     if (option.state & QStyle::State_Selected) {
00055         painter->setPen(QApplication::palette().brush(QPalette::HighlightedText).color());
00056     }else{
00057         painter->setPen(QApplication::palette().brush(QPalette::Text).color());
00058     }
00059     int x = option.rect.left() + MARGIN * 2 + maxwidth;
00060 
00061     QRect textRect(x,
00062                    option.rect.top() + MARGIN,
00063                    option.rect.width() - x - MARGIN,
00064                    maxheight);
00065     QString text = title;
00066     QString authorCaption;
00067     if (!author.isEmpty()) {
00068         authorCaption = i18nc("Caption to wallpaper preview, %1 author name",
00069                               "by %1", author);
00070         text += '\n' + authorCaption;
00071     }
00072 
00073     QRect boundingRect = painter->boundingRect(
00074         textRect, Qt::AlignVCenter | Qt::TextWordWrap, text) & option.rect;
00075     painter->drawText(boundingRect, Qt::TextWordWrap, title);
00076     QRect titleRect = painter->boundingRect(boundingRect, Qt::TextWordWrap, title);
00077     QPoint lastText(titleRect.bottomLeft());
00078 
00079     if (!author.isEmpty()) {
00080         QRect authorRect = QRect(lastText, textRect.size()) & option.rect;
00081 
00082         if (!authorRect.isEmpty()) {
00083             painter->setFont(KGlobalSettings::smallestReadableFont());
00084             painter->drawText(authorRect, Qt::TextWordWrap, authorCaption);
00085             lastText = painter->boundingRect(authorRect, Qt::TextWordWrap, authorCaption).bottomLeft();
00086         }
00087     }
00088 
00089     if (!resolution.isEmpty()) {
00090         QRect resolutionRect = QRect(lastText, textRect.size()) & option.rect;
00091 
00092         if (!resolutionRect.isEmpty()) {
00093             painter->setFont(KGlobalSettings::smallestReadableFont());
00094             painter->drawText(resolutionRect, Qt::TextWordWrap, resolution);
00095         }
00096     }
00097 
00098     painter->restore();
00099 }
00100 
00101 QSize BackgroundDelegate::sizeHint(const QStyleOptionViewItem &option,
00102                                    const QModelIndex &index) const
00103 {
00104     const QString title = index.model()->data(index, Qt::DisplayRole).toString();
00105     const int maxwidth = int(SCREENSHOT_SIZE * m_ratio);
00106     QFont font = option.font;
00107     font.setWeight(QFont::Bold);
00108     QFontMetrics fm(font);
00109     //kDebug() << QSize(maxwidth + qBound(100, fm.width(title), 500), Background::SCREENSHOT_SIZE + MARGIN * 2);
00110     return QSize(maxwidth + qBound(100, fm.width(title), 500), SCREENSHOT_SIZE + MARGIN * 2);
00111 }
00112 

Plasma

Skip menu "Plasma"
  • 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
  •   libsolidcontrol
  •   libtaskmanager
  • Plasma
  •     Animators
  •     Applets
  •     Engines
  • Solid Modules
  • System Settings
  •   SystemSettingsView
Generated for API Reference by doxygen 1.5.9-20090814
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