chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2012 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
| 8 | |
| 9 | |
| 10 | #include "SkListWidget.h" |
| 11 | |
| 12 | SkListWidget::SkListWidget(QObject *parent) {} |
| 13 | |
| 14 | SkListWidget::~SkListWidget() {} |
| 15 | |
chudy@google.com | e565de4 | 2012-07-12 14:15:54 +0000 | [diff] [blame] | 16 | void SkListWidget::paint (QPainter *painter, |
| 17 | const QStyleOptionViewItem &option, |
| 18 | const QModelIndex &index) const { |
| 19 | /* We adjust the initial position of the list item so that |
chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 20 | * we don't have overlapping top and bottom borders of concurrent |
chudy@google.com | e565de4 | 2012-07-12 14:15:54 +0000 | [diff] [blame] | 21 | * widget items. */ |
chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 22 | QRect r = option.rect; |
| 23 | r.adjust(-1,-1,1,0); |
| 24 | |
| 25 | QPen linePen(QColor::fromRgb(211,211,211), 1, Qt::SolidLine); |
| 26 | QPen fontPen(QColor::fromRgb(51,51,51), 1, Qt::SolidLine); |
| 27 | QPen fontMarkedPen(Qt::white, 1, Qt::SolidLine); |
| 28 | |
chudy@google.com | e565de4 | 2012-07-12 14:15:54 +0000 | [diff] [blame] | 29 | // If selected |
chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 30 | if(option.state & QStyle::State_Selected){ |
| 31 | QLinearGradient gradientSelected(r.left(),r.top(),r.left(),r.height()+r.top()); |
| 32 | gradientSelected.setColorAt(0.0, QColor::fromRgb(119,213,247)); |
| 33 | gradientSelected.setColorAt(0.9, QColor::fromRgb(27,134,183)); |
| 34 | gradientSelected.setColorAt(1.0, QColor::fromRgb(0,120,174)); |
| 35 | painter->setBrush(gradientSelected); |
| 36 | painter->drawRect(r); |
| 37 | |
| 38 | painter->setPen(linePen); |
| 39 | painter->drawLine(r.topLeft(),r.topRight()); |
| 40 | painter->drawLine(r.topRight(),r.bottomRight()); |
| 41 | painter->drawLine(r.bottomLeft(),r.bottomRight()); |
| 42 | painter->drawLine(r.topLeft(),r.bottomLeft()); |
| 43 | |
| 44 | painter->setPen(fontMarkedPen); |
| 45 | |
| 46 | } else { |
chudy@google.com | e565de4 | 2012-07-12 14:15:54 +0000 | [diff] [blame] | 47 | // Alternating background |
| 48 | painter->setBrush((index.row() % 2) ? Qt::white : QColor(252,252,252)); |
chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 49 | painter->drawRect(r); |
| 50 | |
| 51 | painter->setPen(linePen); |
| 52 | painter->drawLine(r.topLeft(),r.topRight()); |
| 53 | painter->drawLine(r.topRight(),r.bottomRight()); |
| 54 | painter->drawLine(r.bottomLeft(),r.bottomRight()); |
| 55 | painter->drawLine(r.topLeft(),r.bottomLeft()); |
| 56 | |
| 57 | painter->setPen(fontPen); |
| 58 | } |
| 59 | |
chudy@google.com | e565de4 | 2012-07-12 14:15:54 +0000 | [diff] [blame] | 60 | QIcon breakpointIcon = |
| 61 | QIcon(qvariant_cast<QPixmap>(index.data(Qt::DecorationRole))); |
| 62 | QIcon deleteIcon = |
chudy@google.com | 7e4cfbf | 2012-07-17 15:40:51 +0000 | [diff] [blame] | 63 | QIcon(qvariant_cast<QPixmap>(index.data(Qt::UserRole + 2))); |
robertphillips@google.com | 30d35f2 | 2012-11-06 16:45:36 +0000 | [diff] [blame] | 64 | int indent = index.data(Qt::UserRole + 3).toInt(); |
chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 65 | |
chudy@google.com | e565de4 | 2012-07-12 14:15:54 +0000 | [diff] [blame] | 66 | QString drawCommandText = index.data(Qt::DisplayRole).toString(); |
| 67 | QString drawCommandNumber = index.data(Qt::UserRole + 1).toString(); |
chudy@google.com | e565de4 | 2012-07-12 14:15:54 +0000 | [diff] [blame] | 68 | |
| 69 | /* option.rect is a struct that Qt uses as a target to draw into. Following |
| 70 | * the format (x1,y1,x2,y2) x1 and y1 represent where the painter can start |
| 71 | * drawing. x2 and y2 represent where the drawing area has to terminate |
| 72 | * counting from the bottom right corner of each list item styled with this |
| 73 | * widget. A (x1,y1,0,0) rect would mean that the item being drawn would |
| 74 | * be pushed down into that bottom corner. Negative values in the x2,y2 |
| 75 | * spot act as a margin for the bottom and right sides. Positive values in |
| 76 | * x1,y1 act as a margin for the top and left. The target area will not |
| 77 | * affect size of text but will scale icons. */ |
robertphillips@google.com | 30d35f2 | 2012-11-06 16:45:36 +0000 | [diff] [blame] | 78 | static const int kImageSpace = 35; |
| 79 | static const int kCommandNumberSpace = 30; |
chudy@google.com | e565de4 | 2012-07-12 14:15:54 +0000 | [diff] [blame] | 80 | |
| 81 | // Breakpoint Icon |
chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 82 | r = option.rect.adjusted(5, 10, -10, -10); |
chudy@google.com | e565de4 | 2012-07-12 14:15:54 +0000 | [diff] [blame] | 83 | breakpointIcon.paint(painter, r, Qt::AlignVCenter|Qt::AlignLeft); |
chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 84 | |
chudy@google.com | e565de4 | 2012-07-12 14:15:54 +0000 | [diff] [blame] | 85 | // Delete Icon |
| 86 | r = option.rect.adjusted(19, 10, -10, -10); |
| 87 | deleteIcon.paint(painter, r, Qt::AlignVCenter|Qt::AlignLeft); |
chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 88 | |
chudy@google.com | e565de4 | 2012-07-12 14:15:54 +0000 | [diff] [blame] | 89 | // Draw Command |
robertphillips@google.com | 30d35f2 | 2012-11-06 16:45:36 +0000 | [diff] [blame] | 90 | r = option.rect.adjusted(kImageSpace+kCommandNumberSpace+indent, 0, -10, -7); |
chudy@google.com | e565de4 | 2012-07-12 14:15:54 +0000 | [diff] [blame] | 91 | painter->drawText(r.left(), r.top(), r.width(), r.height(), |
robertphillips@google.com | 30d35f2 | 2012-11-06 16:45:36 +0000 | [diff] [blame] | 92 | Qt::AlignBottom|Qt::AlignLeft, drawCommandText, &r); |
chudy@google.com | e565de4 | 2012-07-12 14:15:54 +0000 | [diff] [blame] | 93 | |
| 94 | // Draw Command Number |
robertphillips@google.com | 30d35f2 | 2012-11-06 16:45:36 +0000 | [diff] [blame] | 95 | r = option.rect.adjusted(kImageSpace, 0, -10, -7); |
chudy@google.com | e565de4 | 2012-07-12 14:15:54 +0000 | [diff] [blame] | 96 | painter->drawText(r.left(), r.top(), r.width(), r.height(), |
| 97 | Qt::AlignBottom|Qt::AlignLeft, drawCommandNumber, &r); |
chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 98 | } |
| 99 | |
chudy@google.com | e565de4 | 2012-07-12 14:15:54 +0000 | [diff] [blame] | 100 | QSize SkListWidget::sizeHint ( const QStyleOptionViewItem & option, |
| 101 | const QModelIndex & index ) const{ |
chudy@google.com | 902ebe5 | 2012-06-29 14:21:22 +0000 | [diff] [blame] | 102 | return QSize(200, 30); |
| 103 | } |