|
| 1 | +/*************************************************************************** |
| 2 | + qgsdetailedlistwidget.cpp - A rich QItemDelegate subclass |
| 3 | + ------------------- |
| 4 | + begin : Sat May 17 2008 |
| 5 | + copyright : (C) 2008 Tim Sutton |
| 6 | + email : tim@linfiniti.com |
| 7 | + ***************************************************************************/ |
| 8 | + |
| 9 | +/*************************************************************************** |
| 10 | + * * |
| 11 | + * This program is free software; you can redistribute it and/or modify * |
| 12 | + * it under the terms of the GNU General Public License as published by * |
| 13 | + * the Free Software Foundation; either version 2 of the License, or * |
| 14 | + * (at your option) any later version. * |
| 15 | + * * |
| 16 | + ***************************************************************************/ |
| 17 | +/* $Id:$ */ |
| 18 | + |
| 19 | +#include "qgsdetaileditemdelegate.h" |
| 20 | +#include "qgsdetaileditemwidget.h" |
| 21 | +#include "qgsdetaileditemdata.h" |
| 22 | +#include <QPainter> |
| 23 | +#include <QFont> |
| 24 | +#include <QFontMetrics> |
| 25 | +#include <QStyleOptionViewItem> |
| 26 | +#include <QModelIndex> |
| 27 | +#include <QCheckBox> |
| 28 | +#include <QLinearGradient> |
| 29 | +QgsDetailedItemDelegate::QgsDetailedItemDelegate(QObject * parent) : |
| 30 | + QAbstractItemDelegate(parent), |
| 31 | + mpWidget(new QgsDetailedItemWidget()), |
| 32 | + mpCheckBox(new QCheckBox()) |
| 33 | + |
| 34 | +{ |
| 35 | + mpCheckBox->resize(16,16); |
| 36 | +} |
| 37 | + |
| 38 | +QgsDetailedItemDelegate::~QgsDetailedItemDelegate() |
| 39 | +{ |
| 40 | + delete mpCheckBox; |
| 41 | + delete mpWidget; |
| 42 | +} |
| 43 | + |
| 44 | +void QgsDetailedItemDelegate::paint(QPainter * thepPainter, |
| 45 | + const QStyleOptionViewItem & theOption, |
| 46 | + const QModelIndex & theIndex) const |
| 47 | +{ |
| 48 | + if (qVariantCanConvert<QgsDetailedItemData>(theIndex.data(Qt::UserRole))) |
| 49 | + { |
| 50 | + QgsDetailedItemData myData = |
| 51 | + qVariantValue<QgsDetailedItemData>(theIndex.data(Qt::UserRole)); |
| 52 | + bool myCheckState = theIndex.model()->data(theIndex, Qt::CheckStateRole).toBool(); |
| 53 | + mpWidget->setChecked(myCheckState); |
| 54 | + mpWidget->setData(myData); |
| 55 | + mpWidget->resize(theOption.rect.width(),mpWidget->height()); |
| 56 | + mpWidget->setAutoFillBackground(false); |
| 57 | + mpWidget->show(); |
| 58 | + mpWidget->repaint(); |
| 59 | + |
| 60 | + if (theOption.state & QStyle::State_Selected) |
| 61 | + { |
| 62 | + QColor myColor1 = theOption.palette.highlight(); |
| 63 | + QColor myColor2 = myColor1; |
| 64 | + myColor2 = myColor2.lighter(); |
| 65 | + QLinearGradient myGradient(QPointF(0,theOption.rect.y()), |
| 66 | + QPointF(0,theOption.rect.y() + mpWidget->height())); |
| 67 | + myGradient.setColorAt(0, myColor1); |
| 68 | + myGradient.setColorAt(0.1, myColor2); |
| 69 | + myGradient.setColorAt(0.5, myColor1); |
| 70 | + myGradient.setColorAt(0.9, myColor2); |
| 71 | + myGradient.setColorAt(1, myColor2); |
| 72 | + thepPainter->fillRect(theOption.rect, QBrush(myGradient)); |
| 73 | + } |
| 74 | + QPixmap myPixmap(mpWidget->size()); |
| 75 | + mpWidget->render(&myPixmap); |
| 76 | + thepPainter->drawPixmap(theOption.rect.x(), |
| 77 | + theOption.rect.y(), |
| 78 | + myPixmap); |
| 79 | + } |
| 80 | + else |
| 81 | + { |
| 82 | + // After painting we need to restore the painter to its original state |
| 83 | + thepPainter->save(); |
| 84 | + // |
| 85 | + // Get the strings and check box properties |
| 86 | + // |
| 87 | + QString myString = theIndex.model()->data(theIndex, Qt::DisplayRole).toString(); |
| 88 | + QString myDetailString = theIndex.model()->data(theIndex, Qt::UserRole).toString(); |
| 89 | + bool myCheckState = theIndex.model()->data(theIndex, Qt::CheckStateRole).toBool(); |
| 90 | + mpCheckBox->setChecked(myCheckState); |
| 91 | + QPixmap myPixmap(mpCheckBox->size()); |
| 92 | + mpCheckBox->render(&myPixmap); //we will draw this onto the widget further down |
| 93 | + |
| 94 | + // |
| 95 | + // Calculate the widget height and other metrics |
| 96 | + // |
| 97 | + QFont myFont = theOption.font; |
| 98 | + QFont myBoldFont = myFont; |
| 99 | + myBoldFont.setBold(true); |
| 100 | + myBoldFont.setPointSize(myFont.pointSize() + 3); |
| 101 | + QFontMetrics myMetrics(myBoldFont); |
| 102 | + int myVerticalSpacer = 3; //spacing between title and description |
| 103 | + int myHorizontalSpacer = 5; //spacing between checkbox / icon and description |
| 104 | + int myTextStartX = theOption.rect.x() + myPixmap.width() + myHorizontalSpacer; |
| 105 | + int myHeight = myMetrics.height() + myVerticalSpacer; |
| 106 | + |
| 107 | + // |
| 108 | + // Draw the item background with a gradient if its highlighted |
| 109 | + // |
| 110 | + if (theOption.state & QStyle::State_Selected) |
| 111 | + { |
| 112 | + QColor myColor1 = theOption.palette.highlight(); |
| 113 | + QColor myColor2 = myColor1; |
| 114 | + myColor2 = myColor2.lighter(); |
| 115 | + int myHeight = myMetrics.height() + myVerticalSpacer; |
| 116 | + QLinearGradient myGradient(QPointF(0,theOption.rect.y()), |
| 117 | + QPointF(0,theOption.rect.y() + myHeight*2)); |
| 118 | + myGradient.setColorAt(0, myColor1); |
| 119 | + myGradient.setColorAt(0.1, myColor2); |
| 120 | + myGradient.setColorAt(0.5, myColor1); |
| 121 | + myGradient.setColorAt(0.9, myColor2); |
| 122 | + myGradient.setColorAt(1, myColor2); |
| 123 | + thepPainter->fillRect(theOption.rect, QBrush(myGradient)); |
| 124 | + } |
| 125 | + |
| 126 | + // |
| 127 | + // Draw the checkbox |
| 128 | + // |
| 129 | + thepPainter->drawPixmap(theOption.rect.x(), |
| 130 | + theOption.rect.y() + mpCheckBox->height(), |
| 131 | + myPixmap); |
| 132 | + |
| 133 | + // |
| 134 | + // Draw the title and description |
| 135 | + // |
| 136 | + thepPainter->setFont(myBoldFont); |
| 137 | + thepPainter->drawText( myTextStartX ,theOption.rect.y() + myHeight, myString); |
| 138 | + thepPainter->setFont(myFont); //return to original font set by client |
| 139 | + thepPainter->drawText( myTextStartX, |
| 140 | + theOption.rect.y() + (myHeight *2) - myVerticalSpacer, |
| 141 | + myDetailString); |
| 142 | + thepPainter->restore(); |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +QSize QgsDetailedItemDelegate::sizeHint( |
| 147 | + const QStyleOptionViewItem & theOption, |
| 148 | + const QModelIndex & theIndex ) const |
| 149 | +{ |
| 150 | + if (qVariantCanConvert<QgsDetailedItemData>(theIndex.data(Qt::UserRole))) |
| 151 | + { |
| 152 | + return QSize(378,mpWidget->height()); |
| 153 | + } |
| 154 | + else // fall back to hand calculated & hand drawn item |
| 155 | + { |
| 156 | + QFont myFont = theOption.font; |
| 157 | + QFont myBoldFont = myFont; |
| 158 | + myBoldFont.setBold(true); |
| 159 | + myBoldFont.setPointSize(myFont.pointSize() + 3); |
| 160 | + QFontMetrics myMetrics(myBoldFont); |
| 161 | + int myVerticalSpacer = 3; //spacing between title and description |
| 162 | + int myHorizontalSpacer = 5; //spacing between checkbox / icon and description |
| 163 | + int myHeight = myMetrics.height() + myVerticalSpacer; |
| 164 | + return QSize(50, |
| 165 | + myHeight *2 + myVerticalSpacer); |
| 166 | + } |
| 167 | +} |
0 commit comments