Debian Codegraph @

src/BookmarksPanel/BookmarksViewItemDelegate.cpp

raw symbols SCIP
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 "BookmarksPanel/BookmarksViewItemDelegate.h"
29
30 BookmarksViewItemDelegate::BookmarksViewItemDelegate(QObject *parent) :
31 QStyledItemDelegate(parent)
32 {
33 widthH = 80;
34 heightH= 80;
35 }
36
37 QWidget *BookmarksViewItemDelegate::createEditor(QWidget* /* parent*/, const QStyleOptionViewItem& /*&option*/, const QModelIndex& /* index */) const
38 {
39 return 0;
40 }
41
42 void BookmarksViewItemDelegate::setEditorData(QWidget* /* editor */, const QModelIndex& /* index */) const
43 {
44 }
45
46 void BookmarksViewItemDelegate::setModelData(QWidget* /* editor */, QAbstractItemModel* /*model*/, const QModelIndex& /* index */) const
47 {
48 }
49
50 void BookmarksViewItemDelegate::updateEditorGeometry(QWidget* /* editor */, const QStyleOptionViewItem& /*option*/, const QModelIndex& /* index */) const
51 {
52 }
53
54 void BookmarksViewItemDelegate::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 if (option.state & QStyle::State_Selected){
66 painter->fillRect(drawArea, option.palette.highlight());
67 }
68
69 if(data.isBookmark()){
70 QImage eq(data.name);
71
72 int w = eq.width();
73 int h = eq.height();
74
75 float af = (float)w/(float)h; // aspect ratio
76 int wmax = drawArea.height()*af; // represents the maximum width to draw at maximum possible height (and keep aspect ratio)
77
78
79 if(drawArea.width()>=wmax){
80 w = qMin(wmax,w);
81 h = qMin(drawArea.height(),h);
82 } else {
83 w = qMin(drawArea.width(),w);
84 h = qMin(int(w/af),h);
85 }
86
87 int x = drawArea.x();
88 int y = (drawArea.height()- h)/2.0 + drawArea.y();
89
90 eq = eq.scaled(QSize(w, h), Qt::KeepAspectRatio, Qt::SmoothTransformation);
91
92 painter->setPen(option.palette.buttonText().color());
93 painter->drawImage(x,y, eq);
94 }
95
96 if(data.isFolder()){
97
98 QIcon icon= QIcon::fromTheme("folder", QIcon("://resources/icons/bookmarks/folder.png"));
99
100 QRect textBB(option.fontMetrics.boundingRect(data.name)); // text bounding box
101 int titleH = textBB.height();
102
103 QRect titleArea(drawArea.x()+titleH+3, drawArea.y()+3, drawArea.width()-3,titleH+3);
104
105 if (option.state & QStyle::State_Selected){
106 painter->setPen(option.palette.highlightedText().color());
107 }
108 else {
109 painter->setPen(option.palette.windowText().color());
110 }
111
112 painter->setFont(option.font);
113 painter->drawText(titleArea, Qt::AlignLeft, data.name);
114 icon.paint(painter, drawArea.x(),drawArea.y(), titleH, titleH);
115 }
116 painter->restore();
117 }
118
119 QSize BookmarksViewItemDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const
120 {
121 LibraryModelData data = qvariant_cast<LibraryModelData>(index.data());
122
123 if(data.isBookmark()){
124 LibraryModelData data = qvariant_cast<LibraryModelData>(index.data(Qt::DisplayRole));
125 QPixmap pix(data.name);
126
127 return QSize(qMin(widthH, pix.size().width()), heightH);
128 }
129
130 QRect r = option.fontMetrics.boundingRect(data.name);
131
132 return QSize(r.width(),r.height()+6);
133
134 }