コード例 #1
0
QRectF
dmz::QtCanvasObjectText::_outline_rect () const {

   QRectF rect;

   if (_text.length ()) {

      const int Padding (8);
      QFontMetricsF metrics = qApp->font ();

      rect.setSize (metrics.size (0, _text));

      if (_drawBackground) {

         rect.adjust (-Padding, -Padding, Padding, Padding);
      }

      if (_alignment == Qt::AlignLeft) {

         rect.translate (0, -rect.center ().y ());
      }
      else if (_alignment == Qt::AlignRight) {

         rect.translate (-rect.width (), -rect.center ().y ());
      }
      else {

         rect.translate (-rect.center ());
      }
   }

   return rect;
}
コード例 #2
0
QSizeF AdjustColumnRowManipulator::textSize(const QString& text, const Style& style) const
{
    QSizeF size;
    DummyWidget dummyWiget;
    const QFontMetricsF fontMetrics(style.font(), &dummyWiget);

    // Set size to correct values according to
    // if the text is horizontal, vertical or rotated.
    if (!style.verticalText() && !style.angle()) {
        // Horizontal text.

        size = fontMetrics.size(0, text);
        double offsetFont = 0.0;
        if ((style.valign() == Style::Bottom) && style.underline())
            offsetFont = fontMetrics.underlinePos() + 1;

        size.setHeight((fontMetrics.ascent() + fontMetrics.descent() + offsetFont)
                       *(text.count('\n') + 1));
    } else if (style.angle() != 0) {
        // Rotated text.

        const double height = fontMetrics.ascent() + fontMetrics.descent();
        const double width  = fontMetrics.width(text);
        size.setHeight(height * ::cos(style.angle() * M_PI / 180)
                       + qAbs(width * ::sin(style.angle() * M_PI / 180)));
        size.setWidth(qAbs(height * ::sin(style.angle() * M_PI / 180))
                      + width * ::cos(style.angle() * M_PI / 180));
    } else {
        // Vertical text.

        qreal width = 0.0;
        for (int i = 0; i < text.length(); i++)
            width = qMax(width, fontMetrics.width(text.at(i)));

        size.setWidth(width);
        size.setHeight((fontMetrics.ascent() + fontMetrics.descent())
                       * text.length());
    }
    return size;
}