Skip to content

Commit c74f99d

Browse files
committed
refactor: extract UBThumbnailTextItem
- UBThumbnailTextItem was part of UBDocumentThumbnailsView - extract to own pair of files to improve code structure
1 parent 483d538 commit c74f99d

6 files changed

Lines changed: 142 additions & 68 deletions

File tree

src/gui/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ target_sources(${PROJECT_NAME} PRIVATE
7575
UBThumbnailArranger.h
7676
UBThumbnailScene.cpp
7777
UBThumbnailScene.h
78+
UBThumbnailTextItem.cpp
79+
UBThumbnailTextItem.h
7880
UBThumbnailsView.cpp
7981
UBThumbnailsView.h
8082
UBDocumentThumbnailsView.cpp

src/gui/UBDocumentThumbnailsView.h

Lines changed: 0 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -137,71 +137,4 @@ class UBDocumentThumbnailsView : public UBThumbnailsView
137137
};
138138

139139

140-
class UBThumbnailTextItem : public QGraphicsTextItem
141-
{
142-
Q_OBJECT
143-
144-
public:
145-
UBThumbnailTextItem()
146-
: QGraphicsTextItem()
147-
{
148-
}
149-
150-
UBThumbnailTextItem(int index)
151-
: QGraphicsTextItem()
152-
, mUnelidedText(toPlainText())
153-
{
154-
setPageNumber(index + 1);
155-
}
156-
157-
UBThumbnailTextItem(const QString& text)
158-
: QGraphicsTextItem(text)
159-
, mUnelidedText(text)
160-
{
161-
setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
162-
}
163-
164-
QRectF boundingRect() const { return QRectF(QPointF(0.0, 0.0), QSize(mWidth, QFontMetricsF(font()).height() + 5));}
165-
166-
void setWidth(qreal pWidth)
167-
{
168-
if (mWidth != pWidth)
169-
{
170-
prepareGeometryChange();
171-
mWidth = pWidth;
172-
computeText();
173-
174-
// center text
175-
setTextWidth(mWidth);
176-
document()->setDefaultTextOption(QTextOption(Qt::AlignCenter));
177-
}
178-
}
179-
180-
qreal width() {return mWidth;}
181-
182-
void setPageNumber(int i)
183-
{
184-
mUnelidedText = tr("Page %0").arg(i);
185-
computeText();
186-
}
187-
188-
void setText(const QString& text)
189-
{
190-
mUnelidedText = text;
191-
computeText();
192-
}
193-
194-
void computeText()
195-
{
196-
QFontMetricsF fm(font());
197-
QString elidedText = fm.elidedText(mUnelidedText, Qt::ElideLeft, mWidth - 2 * document()->documentMargin() - 1);
198-
setPlainText(elidedText);
199-
}
200-
201-
private:
202-
qreal mWidth{0};
203-
QString mUnelidedText{};
204-
};
205-
206-
207140
#endif /* UBDOCUMENTTHUMBNAILSVIEW_H_ */

src/gui/UBThumbnail.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@
2828
#include "board/UBDrawingController.h"
2929
#include "document/UBDocument.h"
3030
#include "domain/UBGraphicsScene.h"
31-
#include "gui/UBDocumentThumbnailsView.h"
3231
#include "gui/UBThumbnailArranger.h"
3332
#include "gui/UBThumbnailScene.h"
33+
#include "gui/UBThumbnailTextItem.h"
3434

3535

3636
constexpr int cSelectionWidth{8};

src/gui/UBThumbnailTextItem.cpp

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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+
}

src/gui/UBThumbnailTextItem.h

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
#pragma once
24+
25+
#include <QGraphicsTextItem>
26+
27+
class UBThumbnailTextItem : public QGraphicsTextItem
28+
{
29+
Q_OBJECT
30+
31+
public:
32+
UBThumbnailTextItem();
33+
UBThumbnailTextItem(int index);
34+
UBThumbnailTextItem(const QString& text);
35+
36+
QRectF boundingRect() const;
37+
38+
void setWidth(qreal pWidth);
39+
qreal width();
40+
41+
void setPageNumber(int i);
42+
void setText(const QString& text);
43+
void computeText();
44+
45+
private:
46+
qreal mWidth{0};
47+
QString mUnelidedText{};
48+
};

src/gui/gui.pri

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ HEADERS += \
22
src/gui/UBThumbnail.h \
33
src/gui/UBThumbnailArranger.h \
44
src/gui/UBThumbnailScene.h \
5+
src/gui/UBThumbnailTextItem.h \
56
src/gui/UBThumbnailsView.h \
67
$$PWD/UBStartupHintsPalette.h \
78
src/gui/UBFloatingPalette.h \
@@ -50,6 +51,7 @@ SOURCES += \
5051
src/gui/UBThumbnail.cpp \
5152
src/gui/UBThumbnailArranger.cpp \
5253
src/gui/UBThumbnailScene.cpp \
54+
src/gui/UBThumbnailTextItem.cpp \
5355
src/gui/UBThumbnailsView.cpp \
5456
$$PWD/UBStartupHintsPalette.cpp \
5557
src/gui/UBFloatingPalette.cpp \

0 commit comments

Comments
 (0)