| 1 | /* |
| 2 | * Copyright 2014 Mihai Niculescu <[email protected]> |
| 3 | * |
| 4 | * This file is part of EqualX Project (https://launchpad.net/equalx/) |
| 5 | * |
| 6 | * EqualX is free software: you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation, either version 3 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * EqualX is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | */ |
| 19 | |
| 20 | #include <QDebug> |
| 21 | |
| 22 | #include <QApplication> |
| 23 | #include <QDateTime> |
| 24 | #include <QPainter> |
| 25 | #include <QVariant> |
| 26 | |
| 27 | #include "Library/LibraryData.h" |
| 28 | #include "HistoryPanel/HistoryViewItemDelegate.h" |
| 29 | |
| 30 | HistoryViewItemDelegate::HistoryViewItemDelegate(QObject *parent) : |
| 31 | QStyledItemDelegate(parent) |
| 32 | { |
| 33 | widthH = 80; |
| 34 | heightH= 80; |
| 35 | } |
| 36 | |
| 37 | QWidget *HistoryViewItemDelegate::createEditor(QWidget* /* parent*/, const QStyleOptionViewItem& /*&option*/, const QModelIndex& /* index */) const |
| 38 | { |
| 39 | return 0; |
| 40 | } |
| 41 | |
| 42 | void HistoryViewItemDelegate::setEditorData(QWidget* /* editor */, const QModelIndex& /* index */) const |
| 43 | { |
| 44 | } |
| 45 | |
| 46 | void HistoryViewItemDelegate::setModelData(QWidget* /* editor */, QAbstractItemModel* /*model*/, const QModelIndex& /* index */) const |
| 47 | { |
| 48 | } |
| 49 | |
| 50 | void HistoryViewItemDelegate::updateEditorGeometry(QWidget* /* editor */, const QStyleOptionViewItem& /*option*/, const QModelIndex& /* index */) const |
| 51 | { |
| 52 | } |
| 53 | |
| 54 | void HistoryViewItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex& index) const |
| 55 | { |
| 56 | LibraryModelData data = qvariant_cast<LibraryModelData>(index.data(Qt::DisplayRole)); |
| 57 | |
| 58 | painter->save(); |
| 59 | |
| 60 | painter->setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform, true); |
| 61 | painter->setPen(Qt::SolidLine); |
| 62 | |
| 63 | QRect drawArea = option.rect; |
| 64 | |
| 65 | QString titleStr; |
| 66 | QRect textBB(0,0,0,0); |
| 67 | |
| 68 | // Title Dimensions |
| 69 | titleStr = QDateTime::fromMSecsSinceEpoch(data.created).toString(); |
| 70 | |
| 71 | textBB = option.fontMetrics.boundingRect(titleStr); |
| 72 | textBB.setHeight(textBB.height()+6); |
| 73 | QRect titleArea(drawArea.x(), drawArea.y(), drawArea.width(), textBB.height()); |
| 74 | |
| 75 | if (option.state & QStyle::State_Selected) |
| 76 | { |
| 77 | painter->fillRect(drawArea, option.palette.highlight()); |
| 78 | |
| 79 | |
| 80 | |
| 81 | } |
| 82 | else{ |
| 83 | |
| 84 | painter->fillRect(drawArea, option.palette.light()); |
| 85 | |
| 86 | painter->setPen(option.palette.dark().color()); |
| 87 | painter->drawRect(drawArea); |
| 88 | } |
| 89 | |
| 90 | |
| 91 | /** Draw the Equation **/ |
| 92 | QImage eq(data.name); |
| 93 | |
| 94 | int w = eq.width(); |
| 95 | int h = eq.height(); |
| 96 | |
| 97 | float af = (float)w/(float)h; // aspect ratio |
| 98 | int wmax = drawArea.height()*af; // represents the maximum width to draw at maximum possible height (and keep aspect ratio) |
| 99 | |
| 100 | |
| 101 | if(drawArea.width()>=wmax){ |
| 102 | w = qMin(wmax,w); |
| 103 | h = qMin(drawArea.height(),h); |
| 104 | } else { |
| 105 | w = qMin(drawArea.width(),w); |
| 106 | h = qMin(int(w/af),h); |
| 107 | } |
| 108 | |
| 109 | eq = eq.scaled(QSize(w, h), Qt::KeepAspectRatio, Qt::SmoothTransformation); |
| 110 | |
| 111 | int x = (drawArea.width() - w)/2.0 + drawArea.x(); |
| 112 | int y = (drawArea.height() + titleArea.height() -h)/2.0 + drawArea.y(); |
| 113 | |
| 114 | painter->setPen(option.palette.buttonText().color()); |
| 115 | painter->drawImage(x,y, eq); |
| 116 | |
| 117 | /** Draw Titlebar Area **/ |
| 118 | // qDebug() << "painting row: " << data.id << " title:" << data.title << " filepath: " << data.filePath << " Date: " << titleStr << " Size: " << x << " " << y << " " << w << " " << h; |
| 119 | QLinearGradient gradient(titleArea.topLeft(), titleArea.bottomLeft() ); // diagonal gradient from top-left to bottom-right |
| 120 | |
| 121 | if (option.state & QStyle::State_Selected){ |
| 122 | gradient.setColorAt(0, option.palette.highlightedText().color()); |
| 123 | gradient.setColorAt(1, option.palette.highlight().color()); |
| 124 | } |
| 125 | else{ |
| 126 | gradient.setColorAt(0, option.palette.light().color()); |
| 127 | gradient.setColorAt(1, option.palette.dark().color()); |
| 128 | } |
| 129 | |
| 130 | painter->fillRect(titleArea, gradient); |
| 131 | |
| 132 | if (option.state & QStyle::State_Selected){ |
| 133 | painter->setPen(option.palette.highlightedText().color()); |
| 134 | } |
| 135 | else { |
| 136 | painter->setPen(option.palette.windowText().color()); |
| 137 | } |
| 138 | |
| 139 | painter->setFont(option.font); |
| 140 | painter->drawText(titleArea, Qt::AlignCenter, titleStr); |
| 141 | |
| 142 | painter->restore(); |
| 143 | //painter->end(); |
| 144 | } |
| 145 | |
| 146 | QSize HistoryViewItemDelegate::sizeHint(const QStyleOptionViewItem& /*option*/, const QModelIndex& /*index*/) const |
| 147 | { |
| 148 | return QSize(widthH, heightH); |
| 149 | } |