File indexing completed on 2024-05-12 04:02:23

0001 /* -*- C++ -*-
0002     This file implements the SMIVItemDelegate class.
0003 
0004     SPDX-FileCopyrightText: 2005 Mirko Boehm <mirko@kde.org>
0005 
0006     SPDX-License-Identifier: LGPL-2.0-or-later
0007 
0008     License: LGPL with the following explicit clarification:
0009         This code may be linked against any version of the Qt toolkit
0010         from Trolltech, Norway.
0011 
0012     $Id: SMIVItemDelegate.cpp 30 2005-08-16 16:16:04Z mirko $
0013 */
0014 
0015 #include <QModelIndex>
0016 #include <QPainter>
0017 
0018 #include "Image.h"
0019 #include "ItemDelegate.h"
0020 #include "Model.h"
0021 
0022 const int ItemDelegate::FrameWidth = 2;
0023 const int ItemDelegate::TextMargin = 6;
0024 const int ItemDelegate::Margin = 3;
0025 
0026 ItemDelegate::ItemDelegate(QObject *parent)
0027     : QItemDelegate(parent)
0028 {
0029 }
0030 
0031 void ItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
0032 {
0033     const bool itemIsSelected = option.showDecorationSelected && (option.state & QStyle::State_Selected);
0034     const Image *image = index.data(Model::Role_ImageRole).value<const Image *>();
0035     // calculate some constants:
0036     const int y0 = option.rect.top();
0037     const int x0 = option.rect.left();
0038     const int width = option.rect.width();
0039     // const int height = option.rect.height();
0040 
0041     painter->save();
0042 
0043     // draw the background color, depending on focus:
0044     if (itemIsSelected) {
0045         QPalette::ColorGroup cg = option.state & QStyle::State_Enabled ? QPalette::Normal : QPalette::Disabled;
0046         painter->fillRect(option.rect, option.palette.brush(cg, QPalette::Highlight));
0047     }
0048 
0049     // draw the image frame:
0050     painter->setPen(Qt::blue);
0051     painter->setBrush(Qt::white);
0052     painter->drawRect(x0 + FrameWidth + Margin, y0 + FrameWidth + Margin, Image::ThumbWidth + 1, Image::ThumbHeight + 1);
0053 
0054     // draw the image:
0055     if (image->progress().first >= Image::Step_LoadImage) {
0056         const QImage &thumb = image->thumbNail();
0057         QPoint orig = QPoint(x0 + FrameWidth + Margin + 1, y0 + FrameWidth + Margin + 1);
0058         painter->drawImage(orig, thumb, QRect(0, qMax(0, (thumb.height() - Image::ThumbHeight) / 2), Image::ThumbWidth, Image::ThumbHeight));
0059     }
0060 
0061     // render the text next to the image:
0062     painter->setPen(Qt::black);
0063     QFontMetrics font1Metrics(option.font);
0064     int textx0 = x0 + FrameWidth + Margin + Image::ThumbWidth + TextMargin;
0065     QRect text1Rect = QRect(textx0, y0 + TextMargin, width - TextMargin - textx0, font1Metrics.lineSpacing());
0066     painter->drawText(text1Rect, image->description());
0067     if (itemIsSelected) {
0068         painter->setPen(Qt::white);
0069     } else {
0070         painter->setPen(Qt::darkGray);
0071     }
0072     QFont font2 = option.font;
0073     font2.setPointSize((int)(0.9 * option.font.pointSize()));
0074     painter->setFont(font2);
0075     QFontMetrics font2Metrics(font2);
0076     QRect text2Rect = text1Rect.adjusted(0, font1Metrics.lineSpacing(), 0, font2Metrics.lineSpacing());
0077     painter->drawText(text2Rect, image->details());
0078     QRect text3Rect = text2Rect.adjusted(0, font2Metrics.lineSpacing(), 0, font2Metrics.lineSpacing());
0079     painter->drawText(text3Rect, image->details2());
0080 
0081     painter->restore();
0082 }
0083 
0084 QSize ItemDelegate::sizeHint(const QStyleOptionViewItem &, const QModelIndex &) const
0085 {
0086     static const int Width = Image::ThumbWidth + 2 * FrameWidth + 2 * Margin + 2;
0087     static const int Height = Image::ThumbHeight + 2 * FrameWidth + 2 * Margin + 2;
0088     return QSize(Width, Height);
0089 }
0090 
0091 #include "moc_ItemDelegate.cpp"