Commits:
-
f7e36670
by Charlie Jiang
at 2022-07-03T15:15:01+08:00
[ftinspect] Support infinite panning
Now the `Grid` dynamically resizes (and resizes the outer scene as well) so
one can drag/scroll infinitely on the grid. "Go back to center" is especially
helpful when you get lost.
* src/ftinspect/rendering/grid.hpp, src/ftinspect/rendering/grid.cpp: Add
`updateRect` to dynamically update bounding rect of the Grid and the scene's
rect.
* src/ftinspect/maingui.hpp, src/ftinspect/maingui.cpp: Call
`Grid::updateRect` when necessary. Note that `MainGUI` must manually maintain
the ownership of `gridItem_` pointer to prevent crash.
4 changed files:
Changes:
src/ftinspect/maingui.cpp
... |
... |
@@ -10,6 +10,7 @@ |
10
|
10
|
#include <QFileDialog>
|
11
|
11
|
#include <QMessageBox>
|
12
|
12
|
#include <QSettings>
|
|
13
|
+#include <QScrollBar>
|
13
|
14
|
|
14
|
15
|
#include <freetype/ftdriver.h>
|
15
|
16
|
|
... |
... |
@@ -32,7 +33,8 @@ MainGUI::MainGUI(Engine* engine) |
32
|
33
|
|
33
|
34
|
MainGUI::~MainGUI()
|
34
|
35
|
{
|
35
|
|
- // empty
|
|
36
|
+ delete gridItem_;
|
|
37
|
+ gridItem_ = NULL;
|
36
|
38
|
}
|
37
|
39
|
|
38
|
40
|
|
... |
... |
@@ -429,6 +431,7 @@ MainGUI::zoom() |
429
|
431
|
transform.translate(shift, shift);
|
430
|
432
|
|
431
|
433
|
glyphView_->setTransform(transform);
|
|
434
|
+ updateGrid();
|
432
|
435
|
}
|
433
|
436
|
|
434
|
437
|
|
... |
... |
@@ -440,6 +443,16 @@ MainGUI::backToCenter() |
440
|
443
|
glyphView_->ensureVisible(currentGlyphBitmapItem_);
|
441
|
444
|
else if (currentGlyphPointsItem_)
|
442
|
445
|
glyphView_->ensureVisible(currentGlyphPointsItem_);
|
|
446
|
+
|
|
447
|
+ updateGrid();
|
|
448
|
+}
|
|
449
|
+
|
|
450
|
+
|
|
451
|
+void
|
|
452
|
+MainGUI::updateGrid()
|
|
453
|
+{
|
|
454
|
+ if (gridItem_)
|
|
455
|
+ gridItem_->updateRect();
|
443
|
456
|
}
|
444
|
457
|
|
445
|
458
|
|
... |
... |
@@ -616,7 +629,6 @@ MainGUI::createLayout() |
616
|
629
|
fontNameLabel_ = new QLabel(this);
|
617
|
630
|
|
618
|
631
|
glyphScene_ = new QGraphicsScene(this);
|
619
|
|
- glyphScene_->addItem(new Grid(gridPen_, axisPen_));
|
620
|
632
|
|
621
|
633
|
currentGlyphBitmapItem_ = NULL;
|
622
|
634
|
currentGlyphOutlineItem_ = NULL;
|
... |
... |
@@ -631,6 +643,10 @@ MainGUI::createLayout() |
631
|
643
|
glyphView_->setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
|
632
|
644
|
glyphView_->setScene(glyphScene_);
|
633
|
645
|
|
|
646
|
+ glyphScene_->setSceneRect(-32000, -32000, 64000, 64000);
|
|
647
|
+ gridItem_ = new Grid(glyphView_, gridPen_, axisPen_);
|
|
648
|
+ glyphScene_->addItem(gridItem_);
|
|
649
|
+
|
634
|
650
|
// Don't use QGraphicsTextItem: We want this hint to be anchored at the
|
635
|
651
|
// top-left corner.
|
636
|
652
|
mouseUsageHint_ = new QLabel(tr("Scroll: Grid Up/Down\n"
|
... |
... |
@@ -782,6 +798,10 @@ MainGUI::createConnections() |
782
|
798
|
SLOT(wheelResize(QWheelEvent*)));
|
783
|
799
|
connect(glyphView_, SIGNAL(ctrlWheelEvent(QWheelEvent*)),
|
784
|
800
|
SLOT(wheelZoom(QWheelEvent*)));
|
|
801
|
+ connect(glyphView_->horizontalScrollBar(), &QScrollBar::valueChanged,
|
|
802
|
+ this, &MainGUI::updateGrid);
|
|
803
|
+ connect(glyphView_->verticalScrollBar(), &QScrollBar::valueChanged, this,
|
|
804
|
+ &MainGUI::updateGrid);
|
785
|
805
|
|
786
|
806
|
connect(centerGridButton_, SIGNAL(clicked()),
|
787
|
807
|
SLOT(backToCenter()));
|
src/ftinspect/maingui.hpp
... |
... |
@@ -10,6 +10,7 @@ |
10
|
10
|
#include "rendering/glyphoutline.hpp"
|
11
|
11
|
#include "rendering/glyphpointnumbers.hpp"
|
12
|
12
|
#include "rendering/glyphpoints.hpp"
|
|
13
|
+#include "rendering/grid.hpp"
|
13
|
14
|
#include "widgets/custom_widgets.hpp"
|
14
|
15
|
#include "models/ttsettingscomboboxmodel.hpp"
|
15
|
16
|
#include "panels/settingpanel.hpp"
|
... |
... |
@@ -86,6 +87,7 @@ private slots: |
86
|
87
|
void watchCurrentFont();
|
87
|
88
|
void zoom();
|
88
|
89
|
void backToCenter();
|
|
90
|
+ void updateGrid();
|
89
|
91
|
void wheelZoom(QWheelEvent* event);
|
90
|
92
|
void wheelResize(QWheelEvent* event);
|
91
|
93
|
|
... |
... |
@@ -108,6 +110,7 @@ private: |
108
|
110
|
GlyphPoints *currentGlyphPointsItem_;
|
109
|
111
|
GlyphPointNumbers *currentGlyphPointNumbersItem_;
|
110
|
112
|
GlyphBitmap *currentGlyphBitmapItem_;
|
|
113
|
+ Grid *gridItem_ = NULL;
|
111
|
114
|
QLabel* mouseUsageHint_;
|
112
|
115
|
|
113
|
116
|
QAction *aboutAct_;
|
src/ftinspect/rendering/grid.cpp
... |
... |
@@ -7,25 +7,51 @@ |
7
|
7
|
|
8
|
8
|
#include <QPainter>
|
9
|
9
|
#include <QStyleOptionGraphicsItem>
|
|
10
|
+#include <QGraphicsWidget>
|
|
11
|
+#include <QGraphicsView>
|
10
|
12
|
|
11
|
13
|
|
12
|
|
-Grid::Grid(const QPen& gridP,
|
|
14
|
+Grid::Grid(QGraphicsView* parentView,
|
|
15
|
+ const QPen& gridP,
|
13
|
16
|
const QPen& axisP)
|
14
|
17
|
: gridPen_(gridP),
|
15
|
|
- axisPen_(axisP)
|
|
18
|
+ axisPen_(axisP),
|
|
19
|
+ parentView_(parentView)
|
16
|
20
|
{
|
17
|
21
|
// empty
|
|
22
|
+ updateRect();
|
18
|
23
|
}
|
19
|
24
|
|
20
|
25
|
|
21
|
26
|
QRectF
|
22
|
27
|
Grid::boundingRect() const
|
23
|
28
|
{
|
24
|
|
- // XXX fix size
|
|
29
|
+ return rect_;
|
|
30
|
+}
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+void
|
|
34
|
+Grid::updateRect()
|
|
35
|
+{
|
|
36
|
+ auto viewport = parentView_->mapToScene(parentView_->viewport()->geometry())
|
|
37
|
+ .boundingRect()
|
|
38
|
+ .toRect();
|
|
39
|
+ int minX = std::min(viewport.left() - 10, -100);
|
|
40
|
+ int minY = std::min(viewport.top() - 10, -100);
|
|
41
|
+ int maxX = std::max(viewport.right() + 10, 100);
|
|
42
|
+ int maxY = std::max(viewport.bottom() + 10, 100);
|
|
43
|
+
|
|
44
|
+ auto newSceneRect = QRectF(QPointF(minX - 20, minY - 20),
|
|
45
|
+ QPointF(maxX + 20, maxY + 20));
|
|
46
|
+ if (sceneRect_ != newSceneRect && scene())
|
|
47
|
+ {
|
|
48
|
+ scene()->setSceneRect(newSceneRect);
|
|
49
|
+ sceneRect_ = newSceneRect;
|
|
50
|
+ }
|
25
|
51
|
|
26
|
52
|
// no need to take care of pen width
|
27
|
|
- return QRectF(-100, -100,
|
28
|
|
- 200, 200);
|
|
53
|
+ rect_ = QRectF(QPointF(minX, minY),
|
|
54
|
+ QPointF(maxX, maxY));
|
29
|
55
|
}
|
30
|
56
|
|
31
|
57
|
|
... |
... |
@@ -35,8 +61,14 @@ Grid::boundingRect() const |
35
|
61
|
void
|
36
|
62
|
Grid::paint(QPainter* painter,
|
37
|
63
|
const QStyleOptionGraphicsItem* option,
|
38
|
|
- QWidget*)
|
|
64
|
+ QWidget* widget)
|
39
|
65
|
{
|
|
66
|
+ auto br = boundingRect().toRect();
|
|
67
|
+ int minX = br.left();
|
|
68
|
+ int minY = br.top();
|
|
69
|
+ int maxX = br.right();
|
|
70
|
+ int maxY = br.bottom();
|
|
71
|
+
|
40
|
72
|
const qreal lod = option->levelOfDetailFromTransform(
|
41
|
73
|
painter->worldTransform());
|
42
|
74
|
|
... |
... |
@@ -59,8 +91,8 @@ Grid::paint(QPainter* painter, |
59
|
91
|
else if (lod > 40)
|
60
|
92
|
halfLength = 2;
|
61
|
93
|
|
62
|
|
- for (qreal x = -100; x < 100; x++)
|
63
|
|
- for (qreal y = -100; y < 100; y++)
|
|
94
|
+ for (qreal x = minX; x < maxX; x++)
|
|
95
|
+ for (qreal y = minY; y < maxY; y++)
|
64
|
96
|
{
|
65
|
97
|
painter->drawLine(QLineF(x + 0.5, y + 0.5 - halfLength / lod,
|
66
|
98
|
x + 0.5, y + 0.5 + halfLength / lod));
|
... |
... |
@@ -72,21 +104,20 @@ Grid::paint(QPainter* painter, |
72
|
104
|
// don't draw grid if magnification is too small
|
73
|
105
|
if (lod >= 5)
|
74
|
106
|
{
|
75
|
|
- // XXX fix size
|
76
|
|
- for (int x = -100; x <= 100; x++)
|
77
|
|
- painter->drawLine(x, -100,
|
78
|
|
- x, 100);
|
79
|
|
- for (int y = -100; y <= 100; y++)
|
80
|
|
- painter->drawLine(-100, y,
|
81
|
|
- 100, y);
|
|
107
|
+ for (int x = minX; x <= maxX; x++)
|
|
108
|
+ painter->drawLine(x, minY,
|
|
109
|
+ x, maxY);
|
|
110
|
+ for (int y = minY; y <= maxY; y++)
|
|
111
|
+ painter->drawLine(minX, y,
|
|
112
|
+ maxX, y);
|
82
|
113
|
}
|
83
|
114
|
|
84
|
115
|
painter->setPen(axisPen_);
|
85
|
116
|
|
86
|
|
- painter->drawLine(0, -100,
|
87
|
|
- 0, 100);
|
88
|
|
- painter->drawLine(-100, 0,
|
89
|
|
- 100, 0);
|
|
117
|
+ painter->drawLine(0, minY,
|
|
118
|
+ 0, maxY);
|
|
119
|
+ painter->drawLine(minX, 0,
|
|
120
|
+ maxX, 0);
|
90
|
121
|
}
|
91
|
122
|
|
92
|
123
|
|
src/ftinspect/rendering/grid.hpp
... |
... |
@@ -8,21 +8,27 @@ |
8
|
8
|
#include <QGraphicsItem>
|
9
|
9
|
#include <QPen>
|
10
|
10
|
|
11
|
|
-
|
12
|
11
|
class Grid
|
13
|
12
|
: public QGraphicsItem
|
14
|
13
|
{
|
15
|
14
|
public:
|
16
|
|
- Grid(const QPen& gridPen,
|
|
15
|
+ Grid(QGraphicsView* parentView,
|
|
16
|
+ const QPen& gridPen,
|
17
|
17
|
const QPen& axisPen);
|
18
|
18
|
QRectF boundingRect() const;
|
19
|
19
|
void paint(QPainter* painter,
|
20
|
20
|
const QStyleOptionGraphicsItem* option,
|
21
|
21
|
QWidget* widget);
|
22
|
22
|
|
|
23
|
+ void updateRect(); // there's no signal/slots for QGraphicsItem.
|
|
24
|
+
|
23
|
25
|
private:
|
24
|
26
|
QPen gridPen_;
|
25
|
27
|
QPen axisPen_;
|
|
28
|
+
|
|
29
|
+ QGraphicsView* parentView_;
|
|
30
|
+ QRectF rect_;
|
|
31
|
+ QRectF sceneRect_;
|
26
|
32
|
};
|
27
|
33
|
|
28
|
34
|
|
|