From: Olivier Matz Date: Sun, 7 Jun 2015 21:36:29 +0000 (+0200) Subject: outline text with black X-Git-Url: http://git.droids-corp.org/?a=commitdiff_plain;ds=inline;p=fpv.git outline text with black --- diff --git a/qtosd/qtosd.py b/qtosd/qtosd.py index 449f243..9ed4ff9 100644 --- a/qtosd/qtosd.py +++ b/qtosd/qtosd.py @@ -221,6 +221,26 @@ class OSDWidget(QWidget): else: return self.adjdev.width() / 3.5 + def drawText(self, painter, center, s): + r = self.getCenterRadius() + painter.save() + pen = QPen(Qt.white, r / 150., Qt.SolidLine) + brush = QBrush(Qt.white) + painter.setBrush(brush) + pen.setColor(Qt.black); + painter.setPen(pen) + font = QFont("Meiryo UI", 0, QFont.Bold) + font.setPointSizeF(self.FONT_SIZE * r) + painter.setFont(font) + path = QPainterPath() + for l in s.split("\n"): + path.addText(center, font, l) + center += QPoint(0, self.FONT_SIZE * r * 1.5) + bounding = path.boundingRect() + path.translate(-bounding.width() / 2., bounding.height() / 2.) + painter.drawPath(path) + painter.restore() + def drawHorizonRound(self, painter): """Draw the horizon for round widget: the sky in blue, the ground in marron.""" @@ -312,9 +332,6 @@ class OSDWidget(QWidget): # set font and pen pen = QPen(Qt.white, r / 100., Qt.SolidLine) painter.setPen(pen) - font = QFont("Meiryo UI", 0, QFont.Bold) - font.setPointSizeF(self.FONT_SIZE * r) - painter.setFont(font) # round to nearest angle that is a multiple of step a = self.pitch / self.PITCH_REFLINES_STEP_ANGLE @@ -347,32 +364,16 @@ class OSDWidget(QWidget): if disp_val < -90.: disp_val = -180. - disp_val disp_val = str(int(disp_val)) - metrics = painter.fontMetrics() - sz = metrics.size(Qt.TextSingleLine, disp_val) - lefttxt = QRect(QPoint(0, 0), sz) - lefttxt.moveCenter(pt1 - QPoint(0.2 * r, 0)) - # XXX - #pen.setWidth(1); - #brush = QBrush(Qt.white) - #painter.setBrush(brush) - #pen.setColor(Qt.black); - #painter.setPen(pen) - #path = QPainterPath() - #path.addText(lefttxt.center(), font, disp_val) - #painter.drawPath(path) - painter.drawText(lefttxt, Qt.TextSingleLine, disp_val) + lefttxt_pt = pt1 - QPoint(0.2 * r, 0) + self.drawText(painter, lefttxt_pt, disp_val) # flip the right text painter.save() painter.translate(pt2 + QPoint(0.2 * r, 0)) painter.rotate(180.) painter.translate(-pt2 - QPoint(0.2 * r, 0)) - righttxt = QRect(QPoint(0, 0), sz) - righttxt.moveCenter(pt2 + QPoint(0.2 * r, 0)) - #path = QPainterPath() - #path.addText(righttxt.center(), font, disp_val) - #painter.drawPath(path) - painter.drawText(righttxt, Qt.TextSingleLine, disp_val) + righttxt_pt = pt2 + QPoint(0.2 * r, 0) + self.drawText(painter, righttxt_pt, disp_val) painter.restore() painter.restore() @@ -392,18 +393,11 @@ class OSDWidget(QWidget): # then draw the text if disp_text == True: pt_txt = path.pointAtPercent(0.2) - font = QFont("Meiryo UI", 0, QFont.Bold) - font.setPointSizeF(self.FONT_SIZE * r) - painter.setFont(font) disp_val = deg if disp_val > 90: disp_val = 180. - disp_val disp_val = str(int(disp_val)) - metrics = painter.fontMetrics() - sz = metrics.size(Qt.TextSingleLine, disp_val) - txt = QRect(QPoint(0, 0), sz) - txt.moveCenter(pt_txt.toPoint()) - painter.drawText(txt, Qt.TextSingleLine, disp_val) + self.drawText(painter, pt_txt, disp_val) def drawRollGraduation(self, painter): """Draw the roll graduations.""" @@ -444,9 +438,6 @@ class OSDWidget(QWidget): r = self.getCenterRadius() pen = QPen(Qt.red, r / 100., Qt.SolidLine) painter.setPen(pen) - font = QFont("Meiryo UI", 0, QFont.Bold) - font.setPointSizeF(self.FONT_SIZE * r) - painter.setFont(font) if self.mode == "round": y_txt = center.y() + r * 0.6 y1 = center.y() + r * 0.7 @@ -488,20 +479,13 @@ class OSDWidget(QWidget): if disp_text == False: continue disp_val = ["N", "E", "S", "W"][(int(a)/90)%4] - metrics = painter.fontMetrics() - sz = metrics.size(Qt.TextSingleLine, disp_val) - txt = QRect(QPoint(0, 0), sz) - txt.moveCenter(pt_txt) - painter.drawText(txt, Qt.TextSingleLine, disp_val) + self.drawText(painter, pt_txt, disp_val) def drawSpeed(self, painter): center = self.adjdev.center() r = self.getCenterRadius() pen = QPen(Qt.red, r / 100., Qt.SolidLine) painter.setPen(pen) - font = QFont("Meiryo UI", 0, QFont.Bold) - font.setPointSizeF(self.FONT_SIZE * r) - painter.setFont(font) x1 = center.x() - 1.5 * r x2 = center.x() - 1.6 * r pt1 = QPoint(center.x() - 1.45 * r, center.y()) @@ -534,20 +518,13 @@ class OSDWidget(QWidget): if disp_text == False: continue disp_val = str(int(s)) - metrics = painter.fontMetrics() - sz = metrics.size(Qt.TextSingleLine, disp_val) - txt = QRect(QPoint(0, 0), sz) - txt.moveCenter(pt_txt) - painter.drawText(txt, Qt.TextSingleLine, disp_val) + self.drawText(painter, pt_txt, disp_val) def drawAlt(self, painter): center = self.adjdev.center() r = self.getCenterRadius() pen = QPen(Qt.red, r / 100., Qt.SolidLine) painter.setPen(pen) - font = QFont("Meiryo UI", 0, QFont.Bold) - font.setPointSizeF(self.FONT_SIZE * r) - painter.setFont(font) x1 = center.x() + 1.5 * r x2 = center.x() + 1.6 * r pt1 = QPoint(center.x() + 1.45 * r, center.y()) @@ -580,11 +557,7 @@ class OSDWidget(QWidget): if disp_text == False: continue disp_val = str(int(a)) - metrics = painter.fontMetrics() - sz = metrics.size(Qt.TextSingleLine, disp_val) - txt = QRect(QPoint(0, 0), sz) - txt.moveCenter(pt_txt) - painter.drawText(txt, Qt.TextSingleLine, disp_val) + self.drawText(painter, pt_txt, disp_val) def drawReturnToHome(self, painter): center = self.adjdev.center() @@ -613,23 +586,14 @@ class OSDWidget(QWidget): r = self.getCenterRadius() pen = QPen(Qt.white, r / 100., Qt.SolidLine) painter.setPen(pen) - font = QFont("Meiryo UI", 0, QFont.Bold) - font.setPointSizeF(self.FONT_SIZE * r) - metrics = painter.fontMetrics() - sz = metrics.size(Qt.AlignLeft, self.left_txt) - txt = QRect(QPoint(0, 0), sz) pt_txt = QPoint(center.x() + dev_r * -0.95, center.y() + dev_r * -0.95) - txt.moveTopLeft(pt_txt) - painter.drawText(txt, Qt.AlignLeft, self.left_txt) + self.drawText(painter, pt_txt, self.right_txt) - sz = metrics.size(Qt.AlignRight, self.right_txt) - txt = QRect(QPoint(0, 0), sz) pt_txt = QPoint(center.x() + dev_r * 0.95, center.y() + dev_r * -0.95) - txt.moveTopRight(pt_txt) - painter.drawText(txt, Qt.AlignRight, self.right_txt) + self.drawText(painter, pt_txt, self.right_txt) def setPitch(self, pitch): """set the pitch in degrees, between -180 and 180"""