|
| 1 | +/* |
| 2 | + * Copyright (C) 2015-2025 Département de l'Instruction Publique (DIP-SEM) |
| 3 | + * |
| 4 | + * This file is part of OpenBoard. |
| 5 | + * |
| 6 | + * OpenBoard 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, version 3 of the License, |
| 9 | + * with a specific linking exception for the OpenSSL project's |
| 10 | + * "OpenSSL" library (or with modified versions of it that use the |
| 11 | + * same license as the "OpenSSL" library). |
| 12 | + * |
| 13 | + * OpenBoard is distributed in the hope that it will be useful, |
| 14 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | + * GNU General Public License for more details. |
| 17 | + * |
| 18 | + * You should have received a copy of the GNU General Public License |
| 19 | + * along with OpenBoard. If not, see <http://www.gnu.org/licenses/>. |
| 20 | + */ |
| 21 | + |
| 22 | + |
| 23 | +#include "UBThumbnailTextItem.h" |
| 24 | + |
| 25 | +#include <QFontMetricsF> |
| 26 | +#include <QTextDocument> |
| 27 | +#include <QTextOption> |
| 28 | + |
| 29 | +UBThumbnailTextItem::UBThumbnailTextItem() |
| 30 | + : QGraphicsTextItem() |
| 31 | +{ |
| 32 | +} |
| 33 | + |
| 34 | +UBThumbnailTextItem::UBThumbnailTextItem(int index) |
| 35 | + : QGraphicsTextItem() |
| 36 | + , mUnelidedText(toPlainText()) |
| 37 | +{ |
| 38 | + setPageNumber(index + 1); |
| 39 | +} |
| 40 | + |
| 41 | +UBThumbnailTextItem::UBThumbnailTextItem(const QString& text) |
| 42 | + : QGraphicsTextItem(text) |
| 43 | + , mUnelidedText(text) |
| 44 | +{ |
| 45 | + setFlag(QGraphicsItem::ItemSendsGeometryChanges, true); |
| 46 | +} |
| 47 | + |
| 48 | +QRectF UBThumbnailTextItem::boundingRect() const |
| 49 | +{ |
| 50 | + return QRectF(QPointF(0.0, 0.0), QSize(mWidth, QFontMetricsF(font()).height() + 5)); |
| 51 | +} |
| 52 | + |
| 53 | +void UBThumbnailTextItem::setWidth(qreal pWidth) |
| 54 | +{ |
| 55 | + if (mWidth != pWidth) |
| 56 | + { |
| 57 | + prepareGeometryChange(); |
| 58 | + mWidth = pWidth; |
| 59 | + computeText(); |
| 60 | + |
| 61 | + // center text |
| 62 | + setTextWidth(mWidth); |
| 63 | + document()->setDefaultTextOption(QTextOption(Qt::AlignCenter)); |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +qreal UBThumbnailTextItem::width() |
| 68 | +{ |
| 69 | + return mWidth; |
| 70 | +} |
| 71 | + |
| 72 | +void UBThumbnailTextItem::setPageNumber(int i) |
| 73 | +{ |
| 74 | + mUnelidedText = tr("Page %0").arg(i); |
| 75 | + computeText(); |
| 76 | +} |
| 77 | + |
| 78 | +void UBThumbnailTextItem::setText(const QString& text) |
| 79 | +{ |
| 80 | + mUnelidedText = text; |
| 81 | + computeText(); |
| 82 | +} |
| 83 | + |
| 84 | +void UBThumbnailTextItem::computeText() |
| 85 | +{ |
| 86 | + QFontMetricsF fm(font()); |
| 87 | + QString elidedText = fm.elidedText(mUnelidedText, Qt::ElideLeft, mWidth - 2 * document()->documentMargin() - 1); |
| 88 | + setPlainText(elidedText); |
| 89 | +} |
0 commit comments