개정판 1cfeb071
issue #478: 멀티 라인 텍스트의 크기 조정
- OcrResultDialog에 단축키 't' 적용(텍스트 인식)
Change-Id: Id45af40d59979a626f6e8f7c502294f707e5d644
DTI_PID/DTI_PID/Shapes/EngineeringTextItem.py | ||
---|---|---|
140 | 140 |
def center(self): |
141 | 141 |
return self.sceneBoundingRect().center() |
142 | 142 |
|
143 |
def boundingRect(self): |
|
143 |
@property |
|
144 |
def text_size(self): |
|
144 | 145 |
if self.angle == 1.57 or self.angle == 4.71: |
145 | 146 |
return QRectF(0, 0, self.size[1], self.size[0]) |
146 | 147 |
else: |
147 | 148 |
return QRectF(0, 0, self.size[0], self.size[1]) |
148 | 149 |
|
150 |
def boundingRect(self) -> QRectF: |
|
151 |
return self.text_size |
|
152 |
|
|
149 | 153 |
''' |
150 | 154 |
@brief hover event |
151 | 155 |
@authro humkyung |
... | ... | |
187 | 191 |
''' |
188 | 192 |
|
189 | 193 |
def keyPressEvent(self, event): |
194 |
import math |
|
195 |
|
|
190 | 196 |
if Qt.Key_Return == event.key(): |
191 | 197 |
self.edit_text() |
192 | 198 |
elif event.key() == Qt.Key_R: |
... | ... | |
212 | 218 |
modifiers = QApplication.keyboardModifiers() |
213 | 219 |
delta = 10 if modifiers == Qt.ControlModifier else 1 |
214 | 220 |
|
221 |
trans = QTransform() |
|
222 |
trans.rotateRadians(self.angle) |
|
223 |
dx, dy = trans.map(0, -1) |
|
215 | 224 |
self.loc[1] = self.loc[1] - delta |
216 |
self.rotate() |
|
225 |
|
|
226 |
trans = self.transform() |
|
227 |
trans.translate(dx*delta, dy*delta) |
|
228 |
self.setTransform(trans) |
|
217 | 229 |
elif event.key() == Qt.Key_Down: |
218 | 230 |
modifiers = QApplication.keyboardModifiers() |
219 | 231 |
delta = 10 if modifiers == Qt.ControlModifier else 1 |
220 | 232 |
|
233 |
trans = QTransform() |
|
234 |
trans.rotateRadians(self.angle) |
|
235 |
dx, dy = trans.map(0, 1) |
|
221 | 236 |
self.loc[1] = self.loc[1] + delta |
222 |
self.rotate() |
|
237 |
|
|
238 |
trans = self.transform() |
|
239 |
trans.translate(dx * delta, dy * delta) |
|
240 |
self.setTransform(trans) |
|
223 | 241 |
elif event.key() == Qt.Key_Left: |
224 | 242 |
modifiers = QApplication.keyboardModifiers() |
225 | 243 |
delta = 10 if modifiers == Qt.ControlModifier else 1 |
226 | 244 |
|
245 |
trans = QTransform() |
|
246 |
trans.rotateRadians(self.angle) |
|
247 |
dx, dy = trans.map(-1, 0) |
|
227 | 248 |
self.loc[0] = self.loc[0] - delta |
228 |
self.rotate() |
|
249 |
|
|
250 |
trans = self.transform() |
|
251 |
trans.translate(dx * delta, dy * delta) |
|
252 |
self.setTransform(trans) |
|
229 | 253 |
elif event.key() == Qt.Key_Right: |
230 | 254 |
modifiers = QApplication.keyboardModifiers() |
231 | 255 |
delta = 10 if modifiers == Qt.ControlModifier else 1 |
232 | 256 |
|
257 |
trans = QTransform() |
|
258 |
trans.rotateRadians(self.angle) |
|
259 |
dx, dy = trans.map(1, 0) |
|
233 | 260 |
self.loc[0] = self.loc[0] + delta |
234 |
self.rotate() |
|
261 |
|
|
262 |
trans = self.transform() |
|
263 |
trans.translate(dx * delta, dy * delta) |
|
264 |
self.setTransform(trans) |
|
235 | 265 |
|
236 | 266 |
# QGraphicsTextItem.keyPressEvent(self, event) |
237 | 267 |
|
... | ... | |
248 | 278 |
hilightColor = QColor(255, 0, 0, 127) |
249 | 279 |
painter.setBrush(QBrush(hilightColor)) |
250 | 280 |
painter.setPen(self.focuspen) |
251 |
painter.drawRect(self.boundingRect())
|
|
281 |
painter.drawRect(self.text_size)
|
|
252 | 282 |
|
253 | 283 |
''' |
254 | 284 |
@brief override paint(draw connection points) |
... | ... | |
274 | 304 |
if not configs or not int(configs[0].value) != 1: |
275 | 305 |
painter.setPen(Qt.NoPen) |
276 | 306 |
painter.setBrush(Qt.white) |
277 |
painter.drawRect(self.boundingRect())
|
|
307 |
painter.drawRect(self.text_size)
|
|
278 | 308 |
# up to here |
279 | 309 |
|
280 | 310 |
painter.setPen(QPen(color)) |
... | ... | |
282 | 312 |
if self.isSelected(): |
283 | 313 |
self.drawFocusRect(painter) |
284 | 314 |
|
315 |
#painter.drawText(rect, Qt.AlignCenter, self.text()) |
|
285 | 316 |
QGraphicsTextItem.paint(self, painter, options, widget) |
286 | 317 |
|
287 | 318 |
''' |
... | ... | |
291 | 322 |
''' |
292 | 323 |
|
293 | 324 |
def boundingRectOnScene(self): |
294 |
rect = self.boundingRect()
|
|
325 |
rect = self.text_size
|
|
295 | 326 |
rect.moveTo(self.loc[0], self.loc[1]) |
296 | 327 |
return rect |
297 | 328 |
|
... | ... | |
374 | 405 |
|
375 | 406 |
transform = QTransform() |
376 | 407 |
if (1.57 == self.angle) or (4.71 == self.angle): |
377 |
rect = self.boundingRect()
|
|
408 |
rect = self.text_size
|
|
378 | 409 |
sx = width / rect.height() |
379 | 410 |
sy = height / rect.width() |
380 | 411 |
|
... | ... | |
384 | 415 |
transform.rotateRadians(-self.angle) |
385 | 416 |
transform.translate(-rect.width() * 0.5, -rect.height() * 0.5) |
386 | 417 |
elif 3.14 == self.angle: |
387 |
rect = self.boundingRect()
|
|
418 |
rect = self.text_size
|
|
388 | 419 |
sx = width / rect.width() |
389 | 420 |
sy = height / rect.height() |
390 | 421 |
|
... | ... | |
393 | 424 |
transform.rotateRadians(-self.angle) |
394 | 425 |
transform.translate(-width * 0.5, -height * 0.5) |
395 | 426 |
else: |
396 |
rect = self.boundingRect()
|
|
427 |
rect = self.text_size
|
|
397 | 428 |
sx = width / rect.width() |
398 | 429 |
sy = height / rect.height() |
399 | 430 |
|
... | ... | |
404 | 435 |
self.setTransform(transform) |
405 | 436 |
self.update() |
406 | 437 |
|
438 |
def text_width(self, font): |
|
439 |
"""return text width""" |
|
440 |
text_width = 0 |
|
441 |
for _text in self.text().split('\n'): |
|
442 |
""" |
|
443 |
rect = QFontMetricsF(font).tightBoundingRect(_text) |
|
444 |
text_width = max(rect.width(), text_width) |
|
445 |
""" |
|
446 |
text_width = max(QFontMetricsF(font).width(_text), text_width) |
|
447 |
|
|
448 |
return text_width |
|
449 |
|
|
407 | 450 |
def update_shape(self): |
408 | 451 |
"""update text shape""" |
409 | 452 |
|
... | ... | |
438 | 481 |
font = QFont(fontName, width if fontSize == -1 else fontSize) |
439 | 482 |
font.setBold(True) |
440 | 483 |
|
441 |
x_factor = width / QFontMetricsF(font).height() |
|
442 |
y_factor = height / (QFontMetricsF(font).width(self.text()) / line_count)
|
|
484 |
x_factor = width / QFontMetricsF(font).height() #self.text()).height() / line_count)
|
|
485 |
y_factor = height / self.text_width(font)
|
|
443 | 486 |
factor = min(x_factor, y_factor) |
444 | 487 |
font.setPointSizeF(font.pointSizeF() * factor) |
445 |
|
|
446 | 488 |
self.setFont(font) |
447 |
rect = self.boundingRect() |
|
448 |
sx = rect.height() / QFontMetricsF(font).height()
|
|
449 |
text_width = (QFontMetricsF(font).width(self.text()) / line_count)
|
|
450 |
sy = rect.width() / text_width
|
|
489 |
|
|
490 |
rect = self.text_size
|
|
491 |
sx = rect.height() / QFontMetricsF(font).tightBoundingRect(self.text()).height()
|
|
492 |
sy = rect.width() / self.text_width(font)
|
|
451 | 493 |
|
452 | 494 |
transform.translate(x + width * 0.5, y + height * 0.5) |
453 | 495 |
transform.rotateRadians(-self.angle) |
454 |
transform.scale(1, sy) |
|
496 |
#transform.scale(1, sy)
|
|
455 | 497 |
transform.translate(-rect.width() * 0.5, -rect.height() * 0.5) |
456 | 498 |
elif abs(self.angle - 3.14) < allowed_error: |
457 | 499 |
font = QFont(fontName, height if fontSize == -1 else fontSize) |
458 | 500 |
font.setBold(True) |
459 | 501 |
|
460 |
x_factor = width / (QFontMetricsF(font).width(self.text()) / line_count)
|
|
461 |
y_factor = height / QFontMetricsF(font).height() |
|
502 |
x_factor = width / self.text_width(font)
|
|
503 |
y_factor = height / QFontMetricsF(font).height() #self.text()).height() / line_count)
|
|
462 | 504 |
factor = min(x_factor, y_factor) |
463 | 505 |
font.setPointSizeF(font.pointSizeF() * factor) |
464 |
|
|
465 | 506 |
self.setFont(font) |
466 |
rect = self.boundingRect() |
|
467 |
sx = width / (QFontMetricsF(font).width(self.text()) / line_count) |
|
468 |
sy = height / rect.height() |
|
507 |
|
|
508 |
rect = self.text_size |
|
509 |
sx = width / self.text_width(font) |
|
510 |
sy = height / QFontMetricsF(font).tightBoundingRect(self.text()).height() |
|
469 | 511 |
|
470 | 512 |
transform.translate(x, y - round((rect.height() - height) * 0.5)) |
471 |
transform.scale(sx, 1) |
|
513 |
#transform.scale(sx, 1)
|
|
472 | 514 |
transform.rotateRadians(-self.angle) |
473 | 515 |
transform.translate(-width * 0.5, -height * 0.5) |
474 | 516 |
else: |
475 | 517 |
font = QFont(fontName, height if fontSize == -1 else fontSize) |
476 | 518 |
font.setBold(True) |
477 | 519 |
|
478 |
x_factor = width / (QFontMetricsF(font).width(self.text()) / line_count)
|
|
479 |
y_factor = height / QFontMetricsF(font).height() |
|
520 |
x_factor = width / self.text_width(font)
|
|
521 |
y_factor = height / QFontMetricsF(font).height() #.boun(self.text()).height() / line_count)
|
|
480 | 522 |
factor = min(x_factor, y_factor) |
481 | 523 |
font.setPointSizeF(font.pointSizeF() * factor) |
482 |
|
|
483 | 524 |
self.setFont(font) |
484 | 525 |
|
485 |
text_width = (QFontMetricsF(font).width(self.text()) / line_count) |
|
486 |
sx = width / text_width |
|
487 |
sy = height / QFontMetricsF(font).height() |
|
526 |
sx = width / self.text_width(font) |
|
527 |
sy = height / QFontMetricsF(font).tightBoundingRect(self.text()).height() |
|
488 | 528 |
|
489 | 529 |
transform.translate(x, y) |
490 |
#transform.scale(sx, 1) |
|
491 | 530 |
|
492 | 531 |
self.setTransform(transform) |
493 | 532 |
|
내보내기 Unified diff