[1f4adec] | 1 | /* |
---|
| 2 | |
---|
| 3 | Copyright 2014 Adam Reichold |
---|
| 4 | |
---|
| 5 | This file is part of qpdfview. |
---|
| 6 | |
---|
| 7 | qpdfview is free software: you can redistribute it and/or modify |
---|
| 8 | it under the terms of the GNU General Public License as published by |
---|
| 9 | the Free Software Foundation, either version 2 of the License, or |
---|
| 10 | (at your option) any later version. |
---|
| 11 | |
---|
| 12 | qpdfview is distributed in the hope that it will be useful, |
---|
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 15 | GNU General Public License for more details. |
---|
| 16 | |
---|
| 17 | You should have received a copy of the GNU General Public License |
---|
| 18 | along with qpdfview. If not, see <http://www.gnu.org/licenses/>. |
---|
| 19 | |
---|
| 20 | */ |
---|
| 21 | |
---|
| 22 | #include "thumbnailitem.h" |
---|
| 23 | |
---|
| 24 | #include <QGraphicsSceneMouseEvent> |
---|
| 25 | #include <qmath.h> |
---|
| 26 | #include <QPainter> |
---|
| 27 | #include <QWidget> |
---|
| 28 | |
---|
| 29 | namespace qpdfview |
---|
| 30 | { |
---|
| 31 | |
---|
| 32 | ThumbnailItem::ThumbnailItem(Model::Page* page, const QString& text, int index, QGraphicsItem* parent) : PageItem(page, index, PageItem::ThumbnailMode, parent), |
---|
| 33 | m_text(text), |
---|
| 34 | m_isHighlighted(false) |
---|
| 35 | { |
---|
| 36 | setAcceptHoverEvents(false); |
---|
| 37 | } |
---|
| 38 | |
---|
| 39 | QRectF ThumbnailItem::boundingRect() const |
---|
| 40 | { |
---|
| 41 | return PageItem::boundingRect().adjusted(0.0, 0.0, 0.0, textHeight()); |
---|
| 42 | } |
---|
| 43 | |
---|
| 44 | void ThumbnailItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget) |
---|
| 45 | { |
---|
| 46 | const QRectF boundingRect = PageItem::boundingRect(); |
---|
| 47 | |
---|
| 48 | |
---|
| 49 | painter->save(); |
---|
| 50 | |
---|
| 51 | painter->setClipping(true); |
---|
| 52 | painter->setClipRect(boundingRect); |
---|
| 53 | |
---|
| 54 | PageItem::paint(painter, option, widget); |
---|
| 55 | |
---|
| 56 | painter->restore(); |
---|
| 57 | |
---|
| 58 | |
---|
| 59 | #if QT_VERSION >= QT_VERSION_CHECK(4,7,0) |
---|
| 60 | |
---|
| 61 | const QSizeF textSize = m_text.size(); |
---|
| 62 | |
---|
| 63 | QPointF pos = boundingRect.bottomLeft(); |
---|
| 64 | pos.rx() += 0.5 * (boundingRect.width() - textSize.width()); |
---|
| 65 | pos.ry() += 0.5 * textSize.height(); |
---|
| 66 | |
---|
| 67 | painter->drawStaticText(pos, m_text); |
---|
| 68 | |
---|
| 69 | #else |
---|
| 70 | |
---|
| 71 | const QFontMetrics fontMetrics = QFontMetrics(QFont()); |
---|
| 72 | |
---|
| 73 | QPointF pos = boundingRect.bottomLeft(); |
---|
| 74 | pos.rx() += 0.5 * (boundingRect.width() - fontMetrics.width(m_text)); |
---|
| 75 | pos.ry() += fontMetrics.height(); |
---|
| 76 | |
---|
| 77 | painter->drawText(pos, m_text); |
---|
| 78 | |
---|
| 79 | #endif // QT_VERSION |
---|
| 80 | |
---|
| 81 | if(m_isHighlighted) |
---|
| 82 | { |
---|
| 83 | painter->save(); |
---|
| 84 | |
---|
| 85 | painter->setCompositionMode(QPainter::CompositionMode_Multiply); |
---|
| 86 | painter->fillRect(boundingRect, widget->palette().highlight()); |
---|
| 87 | |
---|
| 88 | painter->restore(); |
---|
| 89 | } |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | qreal ThumbnailItem::textHeight() const |
---|
| 93 | { |
---|
| 94 | #if QT_VERSION >= QT_VERSION_CHECK(4,7,0) |
---|
| 95 | |
---|
| 96 | return 2.0 * m_text.size().height(); |
---|
| 97 | |
---|
| 98 | #else |
---|
| 99 | |
---|
| 100 | return 2.0 * QFontMetrics(QFont()).height(); |
---|
| 101 | |
---|
| 102 | #endif // QT_VERSION |
---|
| 103 | } |
---|
| 104 | |
---|
| 105 | void ThumbnailItem::setHighlighted(bool highlighted) |
---|
| 106 | { |
---|
| 107 | if(m_isHighlighted != highlighted) |
---|
| 108 | { |
---|
| 109 | m_isHighlighted = highlighted; |
---|
| 110 | |
---|
| 111 | update(); |
---|
| 112 | } |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | void ThumbnailItem::mousePressEvent(QGraphicsSceneMouseEvent* event) |
---|
| 116 | { |
---|
| 117 | if(event->modifiers() == Qt::NoModifier |
---|
| 118 | && (event->button() == Qt::LeftButton || event->button() == Qt::MidButton)) |
---|
| 119 | { |
---|
| 120 | emit linkClicked(event->button() == Qt::MidButton, index() + 1); |
---|
| 121 | |
---|
| 122 | event->accept(); |
---|
| 123 | return; |
---|
| 124 | } |
---|
| 125 | |
---|
| 126 | event->ignore(); |
---|
| 127 | } |
---|
| 128 | |
---|
| 129 | void ThumbnailItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent*) |
---|
| 130 | { |
---|
| 131 | } |
---|
| 132 | |
---|
| 133 | void ThumbnailItem::mouseMoveEvent(QGraphicsSceneMouseEvent*) |
---|
| 134 | { |
---|
| 135 | } |
---|
| 136 | |
---|
| 137 | void ThumbnailItem::mouseReleaseEvent(QGraphicsSceneMouseEvent*) |
---|
| 138 | { |
---|
| 139 | } |
---|
| 140 | |
---|
| 141 | void ThumbnailItem::contextMenuEvent(QGraphicsSceneContextMenuEvent*) |
---|
| 142 | { |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | void ThumbnailItem::loadInteractiveElements() |
---|
| 146 | { |
---|
| 147 | const qreal width = size().width() / 72.0 * 25.4; |
---|
| 148 | const qreal height = size().height() / 72.0 * 25.4; |
---|
| 149 | |
---|
| 150 | const qreal longEdge = qMax(width, height); |
---|
| 151 | const qreal shortEdge = qMin(width, height); |
---|
| 152 | |
---|
| 153 | QString paperSize; |
---|
| 154 | |
---|
| 155 | if(qAbs(longEdge - 279.4) <= 1.0 && qAbs(shortEdge - 215.9) <= 1.0) |
---|
| 156 | { |
---|
| 157 | paperSize = QLatin1String(" (Letter)"); |
---|
| 158 | } |
---|
| 159 | else |
---|
| 160 | { |
---|
| 161 | qreal longEdgeA = 1189.0; |
---|
| 162 | qreal shortEdgeA = 841.0; |
---|
| 163 | |
---|
| 164 | qreal longEdgeB = 1414.0; |
---|
| 165 | qreal shortEdgeB = 1000.0; |
---|
| 166 | |
---|
| 167 | for(int i = 0; i <= 10; ++i) |
---|
| 168 | { |
---|
| 169 | if(qAbs(longEdge - longEdgeA) <= 1.0 && qAbs(shortEdge - shortEdgeA) <= 1.0) |
---|
| 170 | { |
---|
| 171 | paperSize = QString(" (A%1)").arg(i); |
---|
| 172 | break; |
---|
| 173 | } |
---|
| 174 | else if(qAbs(longEdge - longEdgeB) <= 1.0 && qAbs(shortEdge - shortEdgeB) <= 1.0) |
---|
| 175 | { |
---|
| 176 | paperSize = QString(" (B%1)").arg(i); |
---|
| 177 | break; |
---|
| 178 | } |
---|
| 179 | |
---|
| 180 | longEdgeA = shortEdgeA; |
---|
| 181 | shortEdgeA /= qSqrt(2.0); |
---|
| 182 | |
---|
| 183 | longEdgeB = shortEdgeB; |
---|
| 184 | shortEdgeB /= qSqrt(2.0); |
---|
| 185 | } |
---|
| 186 | } |
---|
| 187 | |
---|
| 188 | setToolTip(QString("%1 mm x %2 mm%3").arg(width, 0, 'f', 1).arg(height, 0, 'f', 1).arg(paperSize)); |
---|
| 189 | } |
---|
| 190 | |
---|
| 191 | } // qpdfview |
---|