... |
... |
@@ -107,14 +107,17 @@ GlyphBitmapWidget::~GlyphBitmapWidget() |
107
|
107
|
|
108
|
108
|
void
|
109
|
109
|
GlyphBitmapWidget::updateImage(QImage* image,
|
110
|
|
- QRect rect)
|
|
110
|
+ QRect rect,
|
|
111
|
+ QRect placeholderRect)
|
111
|
112
|
{
|
112
|
|
- rect.moveTop(0);
|
113
|
|
- rect.moveLeft(0);
|
114
|
|
-
|
115
|
113
|
delete bitmapItem_;
|
116
|
114
|
auto* copied = new QImage(image->copy());
|
117
|
|
- bitmapItem_ = new GlyphBitmap(copied, rect);
|
|
115
|
+
|
|
116
|
+ rect_ = rect;
|
|
117
|
+ placeholderRect_ = placeholderRect;
|
|
118
|
+ auto zeroedRect = rect; // `GlyphBitmap` doesn't play well with offset
|
|
119
|
+ zeroedRect.moveTopLeft({ 0, 0 });
|
|
120
|
+ bitmapItem_ = new GlyphBitmap(copied, zeroedRect);
|
118
|
121
|
|
119
|
122
|
repaint();
|
120
|
123
|
}
|
... |
... |
@@ -135,24 +138,63 @@ GlyphBitmapWidget::paintEvent(QPaintEvent* event) |
135
|
138
|
if (!bitmapItem_)
|
136
|
139
|
return;
|
137
|
140
|
auto s = size();
|
138
|
|
- auto br = bitmapItem_->boundingRect();
|
139
|
|
- double xScale = s.width() / br.width();
|
140
|
|
- double yScale = s.height() / br.height();
|
|
141
|
+
|
|
142
|
+ auto br = QRect(QPoint(std::min(rect_.left(), placeholderRect_.left()),
|
|
143
|
+ std::min(rect_.top(), placeholderRect_.top())),
|
|
144
|
+ QPoint(std::max(rect_.right(), placeholderRect_.right()),
|
|
145
|
+ std::max(rect_.bottom(), placeholderRect_.bottom())));
|
|
146
|
+
|
|
147
|
+ double xScale = 0.9 * s.width() / br.width();
|
|
148
|
+ double yScale = 0.9 * s.height() / br.height();
|
|
149
|
+ auto margin = br.width() * 0.05;
|
141
|
150
|
auto scale = std::min(xScale, yScale);
|
142
|
151
|
|
143
|
152
|
QPainter painter(this);
|
144
|
153
|
painter.fillRect(rect(), Qt::white);
|
|
154
|
+ painter.save(); // push before scaling
|
145
|
155
|
painter.scale(scale, scale);
|
|
156
|
+ painter.translate(-br.topLeft() + QPointF(margin, margin));
|
|
157
|
+
|
|
158
|
+ double scaledLineWidth = 4 / scale;
|
|
159
|
+ double scaledLineWidthHalf = scaledLineWidth / 2;
|
|
160
|
+ painter.setPen(QPen(Qt::blue, scaledLineWidth));
|
|
161
|
+ // Blue line: Ink box
|
|
162
|
+ painter.drawRect(QRectF(rect_).adjusted(-scaledLineWidthHalf,
|
|
163
|
+ -scaledLineWidthHalf,
|
|
164
|
+ scaledLineWidthHalf,
|
|
165
|
+ scaledLineWidthHalf));
|
|
166
|
+
|
|
167
|
+ painter.save(); // push before translating
|
|
168
|
+ painter.translate(rect_.topLeft());
|
146
|
169
|
|
147
|
170
|
QStyleOptionGraphicsItem ogi;
|
148
|
171
|
ogi.exposedRect = br;
|
149
|
172
|
bitmapItem_->paint(&painter, &ogi, this);
|
150
|
173
|
|
151
|
|
- double scaledLineWidth = 4 / scale;
|
152
|
|
- painter.setPen(QPen(Qt::black, scaledLineWidth));
|
|
174
|
+ painter.restore(); // undo translating.
|
|
175
|
+
|
153
|
176
|
scaledLineWidth /= 2;
|
154
|
|
- painter.drawRect(br.adjusted(scaledLineWidth, scaledLineWidth,
|
155
|
|
- -scaledLineWidth, -scaledLineWidth));
|
|
177
|
+ scaledLineWidthHalf /= 2;
|
|
178
|
+ // Light gray line: EM box
|
|
179
|
+ painter.setPen(QPen(Qt::lightGray, scaledLineWidth));
|
|
180
|
+ painter.drawRect(QRectF(placeholderRect_).adjusted(-scaledLineWidthHalf,
|
|
181
|
+ -scaledLineWidthHalf,
|
|
182
|
+ scaledLineWidthHalf,
|
|
183
|
+ scaledLineWidthHalf));
|
|
184
|
+
|
|
185
|
+ auto tfForAxis = painter.transform().inverted();
|
|
186
|
+ painter.setPen(QPen(Qt::black, scaledLineWidth / 2));
|
|
187
|
+ // Thin black line: xy-axis
|
|
188
|
+ painter.drawLine(QPointF(tfForAxis.map(QPointF(0, 0)).x(), 0),
|
|
189
|
+ QPointF(tfForAxis.map(QPointF(s.width(), 0)).x(), 0));
|
|
190
|
+ painter.drawLine(QPointF(0, tfForAxis.map(QPointF(0, 0)).y()),
|
|
191
|
+ QPointF(0, tfForAxis.map(QPointF(0, s.height())).y()));
|
|
192
|
+
|
|
193
|
+ painter.restore(); // undo scaling.
|
|
194
|
+
|
|
195
|
+ // main border
|
|
196
|
+ painter.setPen(QPen(Qt::black, 4));
|
|
197
|
+ painter.drawRect(rect().adjusted(2, 2, -2, -2));
|
156
|
198
|
}
|
157
|
199
|
|
158
|
200
|
|