Adaptively adjust label size (#678)

* Adjust label font size based on image size

* Adjust the upper boundary of the painted label

* Set font size based on both image width & height
This commit is contained in:
chenghsiung 2021-01-02 03:54:27 +08:00 committed by GitHub
parent 0a581ef7e9
commit 5d6557f994
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 8 additions and 4 deletions

View File

@ -1141,6 +1141,7 @@ class MainWindow(QMainWindow, WindowMixin):
def paintCanvas(self): def paintCanvas(self):
assert not self.image.isNull(), "cannot paint null image" assert not self.image.isNull(), "cannot paint null image"
self.canvas.scale = 0.01 * self.zoomWidget.value() self.canvas.scale = 0.01 * self.zoomWidget.value()
self.canvas.labelFontSize = int(0.02 * max(self.image.width(), self.image.height()))
self.canvas.adjustSize() self.canvas.adjustSize()
self.canvas.update() self.canvas.update()

View File

@ -47,6 +47,7 @@ class Canvas(QWidget):
self.prevPoint = QPointF() self.prevPoint = QPointF()
self.offsets = QPointF(), QPointF() self.offsets = QPointF(), QPointF()
self.scale = 1.0 self.scale = 1.0
self.labelFontSize = 8
self.pixmap = QPixmap() self.pixmap = QPixmap()
self.visible = {} self.visible = {}
self._hideBackround = False self._hideBackround = False
@ -478,6 +479,7 @@ class Canvas(QWidget):
p.drawPixmap(0, 0, self.pixmap) p.drawPixmap(0, 0, self.pixmap)
Shape.scale = self.scale Shape.scale = self.scale
Shape.labelFontSize = self.labelFontSize
for shape in self.shapes: for shape in self.shapes:
if (shape.selected or not self._hideBackround) and self.isVisible(shape): if (shape.selected or not self._hideBackround) and self.isVisible(shape):
shape.fill = shape.selected or shape == self.hShape shape.fill = shape.selected or shape == self.hShape

View File

@ -18,7 +18,6 @@ DEFAULT_SELECT_LINE_COLOR = QColor(255, 255, 255)
DEFAULT_SELECT_FILL_COLOR = QColor(0, 128, 255, 155) DEFAULT_SELECT_FILL_COLOR = QColor(0, 128, 255, 155)
DEFAULT_VERTEX_FILL_COLOR = QColor(0, 255, 0, 255) DEFAULT_VERTEX_FILL_COLOR = QColor(0, 255, 0, 255)
DEFAULT_HVERTEX_FILL_COLOR = QColor(255, 0, 0) DEFAULT_HVERTEX_FILL_COLOR = QColor(255, 0, 0)
MIN_Y_LABEL = 10
class Shape(object): class Shape(object):
@ -37,6 +36,7 @@ class Shape(object):
point_type = P_ROUND point_type = P_ROUND
point_size = 8 point_size = 8
scale = 1.0 scale = 1.0
labelFontSize = 8
def __init__(self, label=None, line_color=None, difficult=False, paintLabel=False): def __init__(self, label=None, line_color=None, difficult=False, paintLabel=False):
self.label = label self.label = label
@ -115,18 +115,19 @@ class Shape(object):
if self.paintLabel: if self.paintLabel:
min_x = sys.maxsize min_x = sys.maxsize
min_y = sys.maxsize min_y = sys.maxsize
min_y_label = int(1.25 * self.labelFontSize)
for point in self.points: for point in self.points:
min_x = min(min_x, point.x()) min_x = min(min_x, point.x())
min_y = min(min_y, point.y()) min_y = min(min_y, point.y())
if min_x != sys.maxsize and min_y != sys.maxsize: if min_x != sys.maxsize and min_y != sys.maxsize:
font = QFont() font = QFont()
font.setPointSize(8) font.setPointSize(self.labelFontSize)
font.setBold(True) font.setBold(True)
painter.setFont(font) painter.setFont(font)
if(self.label == None): if(self.label == None):
self.label = "" self.label = ""
if(min_y < MIN_Y_LABEL): if(min_y < min_y_label):
min_y += MIN_Y_LABEL min_y += min_y_label
painter.drawText(min_x, min_y, self.label) painter.drawText(min_x, min_y, self.label)
if self.fill: if self.fill: