text.cc 13.6 KB
Newer Older
Jan Möbius's avatar
Jan Möbius committed
1 2 3
/*===========================================================================*\
*                                                                            *
*                              OpenFlipper                                   *
Martin Schultz's avatar
Martin Schultz committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
 *           Copyright (c) 2001-2015, RWTH-Aachen University                 *
 *           Department of Computer Graphics and Multimedia                  *
 *                          All rights reserved.                             *
 *                            www.openflipper.org                            *
 *                                                                           *
 *---------------------------------------------------------------------------*
 * This file is part of OpenFlipper.                                         *
 *---------------------------------------------------------------------------*
 *                                                                           *
 * Redistribution and use in source and binary forms, with or without        *
 * modification, are permitted provided that the following conditions        *
 * are met:                                                                  *
 *                                                                           *
 * 1. Redistributions of source code must retain the above copyright notice, *
 *    this list of conditions and the following disclaimer.                  *
 *                                                                           *
 * 2. Redistributions in binary form must reproduce the above copyright      *
 *    notice, this list of conditions and the following disclaimer in the    *
 *    documentation and/or other materials provided with the distribution.   *
 *                                                                           *
 * 3. Neither the name of the copyright holder nor the names of its          *
 *    contributors may be used to endorse or promote products derived from   *
 *    this software without specific prior written permission.               *
 *                                                                           *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS       *
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A           *
 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,  *
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,       *
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR        *
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF    *
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING      *
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS        *
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.              *
Jan Möbius's avatar
Jan Möbius committed
39 40 41 42 43 44 45 46 47 48
*                                                                            *
\*===========================================================================*/

/*===========================================================================*\
*                                                                            *
*   $Revision$                                                       *
*   $LastChangedBy$                                                *
*   $Date$                     *
*                                                                            *
\*===========================================================================*/
Jan Möbius's avatar
 
Jan Möbius committed
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74

//== INCLUDES =================================================================

#include <QPolygonF>
#include <QGraphicsItem>
#include <QFontMetrics>
#include <QPen>
#include <QPainter>

#include "text.hh"

#define BACK_OFFSET 2

//== NAMESPACES ===============================================================
namespace VSI {

//=============================================================================
//
//  CLASS VSI::Text - IMPLEMENTATION
//
//=============================================================================

/// Constructor
Text::Text (QGraphicsItem *_parent) :
  QGraphicsSimpleTextItem (_parent),
  backgroundSet_ (false),
Jan Möbius's avatar
Jan Möbius committed
75 76
  leftOut_(true),
  rightOut_(true),
Jan Möbius's avatar
 
Jan Möbius committed
77 78 79 80 81 82 83 84 85 86 87 88 89
  alignment_ (Qt::AlignLeft),
  hStretch_ (true)
{
  setBrush (Qt::white);
}

//------------------------------------------------------------------------------

/// Constructor
Text::Text (const QString &_text, QGraphicsItem *_parent) :
  QGraphicsSimpleTextItem (_text, _parent),
  text_ (_text),
  backgroundSet_ (false),
Jan Möbius's avatar
Jan Möbius committed
90 91
  leftOut_(true),
  rightOut_(true),
Jan Möbius's avatar
 
Jan Möbius committed
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
  alignment_ (Qt::AlignLeft),
  hStretch_ (true)
{
  setBrush (Qt::white);
}

//------------------------------------------------------------------------------

/// Destructor
Text::~Text ()
{
}

//------------------------------------------------------------------------------

/// Sets the geometry
void Text::setGeometry (const QRectF &_rect)
{
  QFontMetrics fn (font ());

  int width = _rect.width ();

  if (backgroundSet_)
  {
    if (rightOut_)
      width -= _rect.height () / 2;
    else
      width -= BACK_OFFSET * 2;

    if (leftOut_)
      width -= _rect.height () / 2;
    else
      width -= BACK_OFFSET * 2;
  }

  if (fn.boundingRect (text_).width () > width)
  {
    QString nt = text_ + "...";
    while (fn.boundingRect (nt).width () > width && nt.length () > 3)
      nt.remove (nt.length () - 4, 1);
    QGraphicsSimpleTextItem::setText (nt);
  }
  else
    QGraphicsSimpleTextItem::setText (text_);

  QGraphicsSimpleTextItem::setPos (_rect.topLeft ());
  QGraphicsLayoutItem::setGeometry (_rect);
}

//------------------------------------------------------------------------------

// size information for layouting
Dirk Wilden's avatar
Dirk Wilden committed
144
QSizeF Text::sizeHint (Qt::SizeHint _which, const QSizeF &/*_constraint*/) const
Jan Möbius's avatar
 
Jan Möbius committed
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
{
  QFontMetrics fn (font ());
  QSizeF sh;

  int w = 0;
  if (rightOut_)
    w += (fn.height () / 2) + (BACK_OFFSET * 2);
  else
    w += BACK_OFFSET * 2;
  if (leftOut_)
    w += (fn.height () / 2) + (BACK_OFFSET * 2);
  else
    w += BACK_OFFSET * 2;

  QSizeF bOff = QSizeF (w, BACK_OFFSET * 2);

  switch (_which) {
    case Qt::MinimumSize:
        sh = QSizeF(fn.boundingRect ("...").width (), fn.height ());
        if (backgroundSet_)
          sh += bOff;
        break;
    case Qt::PreferredSize:
        sh = QSizeF(fn.boundingRect (text_).width (), fn.height ());
        if (backgroundSet_)
          sh += bOff;
        break;
    case Qt::MaximumSize:
        sh = QSizeF(fn.boundingRect (text_).width (), fn.height ());
        if (backgroundSet_)
          sh += bOff;
        if (hStretch_)
          sh += QSizeF (65535, 0);
        break;
    case Qt::MinimumDescent:
        sh = QSizeF(0, fn.descent ());
    default:
      break;
  }

  return sh;
}

//------------------------------------------------------------------------------

/// Background painting
void Text::paint (QPainter *_painter, const QStyleOptionGraphicsItem *_option, QWidget *_widget)
{
  qreal h = geometry ().size ().height () / 2.0;

  if (backgroundSet_)
  {
    QPainterPath path;

    path.moveTo (geometry ().size ().width () / 2, 0);
    if (leftOut_)
    {
      path.lineTo (h, 0);
      path.arcTo (0, 0, h * 2.0, h * 2.0, 90, 180);
    }
    else
    {
      path.lineTo (-h, 0);
      path.arcTo (-h * 2, 0, h * 2.0, h * 2.0, 90, -180);
    }

    if (rightOut_)
    {
      path.lineTo (geometry ().size ().width () - h, geometry ().size ().height ());
      path.arcTo (geometry ().size ().width () - (h * 2), 0, h * 2.0, h * 2.0, -90, 180);
    }
    else
    {
      path.lineTo (geometry ().size ().width () + h, geometry ().size ().height ());
      path.arcTo (geometry ().size ().width (), 0, h * 2.0, h * 2.0, -90, -180);
    }
    path.lineTo (geometry ().size ().width () / 2, 0);

    _painter->setBrush (backgroundBrush_);
    _painter->setPen (backgroundPen_);
    _painter->drawPath (path);


    QLinearGradient lG;

    lG.setStart (0, h);
    lG.setFinalStop(0, h * 2);
    lG.setColorAt(0, Qt::transparent);
    lG.setColorAt(1, QColor (0, 0, 0, 192));

    _painter->setBrush (lG);
    _painter->setPen (Qt::NoPen);
    _painter->drawPath(path);

    lG.setStart (0, h);
    lG.setFinalStop(0, 0);
    lG.setColorAt(0, Qt::transparent);
    lG.setColorAt(1, QColor (255, 255, 255, 192));

    QPen pen;
    pen.setColor (Qt::transparent);
    pen.setWidthF (4);
    _painter->setBrush (lG);
    _painter->setPen (pen);
    _painter->drawPath(path);

    if (leftOut_)
      _painter->translate (h, BACK_OFFSET);
    else
      _painter->translate (BACK_OFFSET * 2, BACK_OFFSET);
  }

  QFontMetrics fn (font ());
  int trans;
  if (alignment_ == Qt::AlignHCenter)
  {
    trans = geometry ().size ().width ();

    if (backgroundSet_)
    {
      if (rightOut_)
        trans -= h;
      else
        trans -= BACK_OFFSET * 2;
      if (leftOut_)
        trans -= h;
      else
        trans -= BACK_OFFSET * 2;
    }

    _painter->translate ((trans - fn.boundingRect (QGraphicsSimpleTextItem::text ()).width ()) / 2, 0);
  }
  else if (alignment_ == Qt::AlignRight)
  {
    trans = geometry ().size ().width ();

    if (backgroundSet_)
    {
      if (rightOut_)
        trans -= h;
      else
        trans -= BACK_OFFSET * 2;
      if (leftOut_)
        trans -= h;
      else
        trans -= BACK_OFFSET * 2;
    }

    _painter->translate (trans - fn.boundingRect (QGraphicsSimpleTextItem::text ()).width (), 0);
  }
  else
  {

  }

  QGraphicsSimpleTextItem::paint (_painter, _option, _widget);
}

//------------------------------------------------------------------------------

/// Enables background painting
void Text::setBackground(bool _leftOut, bool _rightOut)
{
  backgroundSet_ = true;
Jan Möbius's avatar
Jan Möbius committed
309 310
  leftOut_       = _leftOut;
  rightOut_      = _rightOut;
Jan Möbius's avatar
 
Jan Möbius committed
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
  updateGeometry ();
}

//------------------------------------------------------------------------------

/// Placement of the text in a stretched widget 
void Text::setAlignment(Qt::Alignment _alignment)
{
  if (_alignment & Qt::AlignLeft)
    alignment_ = Qt::AlignLeft;
  else if (_alignment & Qt::AlignHCenter)
    alignment_ = Qt::AlignHCenter;
  else if (_alignment & Qt::AlignRight)
    alignment_ = Qt::AlignRight;
}

//------------------------------------------------------------------------------

/// Should this widget be stretchable in horizontal direction
void Text::setHorizontalStretch(bool _stretch)
{
  hStretch_ = _stretch;
  updateGeometry ();
}

//------------------------------------------------------------------------------

/// Disables backgorund painting
void Text::clearBackground()
{
  backgroundSet_ = false;
  updateGeometry ();
}

//------------------------------------------------------------------------------

/// Bounding rect
QRectF Text::boundingRect() const
{
  QFontMetrics fn (font ());
  int x, w;

  if (!backgroundSet_)
  {
    QRectF rect = QGraphicsSimpleTextItem::boundingRect ();

    if (alignment_ == Qt::AlignHCenter)
      rect.translate ((geometry ().size ().width () - fn.boundingRect (QGraphicsSimpleTextItem::text ()).width ()) / 2, 0);
    else if (alignment_ == Qt::AlignRight)
      rect.translate (geometry ().size ().width () - fn.boundingRect (QGraphicsSimpleTextItem::text ()).width (), 0);

    return rect;
  }

  w = geometry ().size ().width ();
  x = 0;

  if (!leftOut_)
  {
    w += (fn.height () / 2) + (BACK_OFFSET * 2);
    x -= (fn.height () / 2) + (BACK_OFFSET * 2);
  }

  if (!rightOut_)
  {
    w += (fn.height () / 2) + (BACK_OFFSET * 2);
  }

  return QRectF (QPointF (x, 0), QSizeF(w, geometry ().size ().height ()));
}

//------------------------------------------------------------------------------

/// Sets the background brush
void Text::setBackgroundBrush(QBrush _brush)
{
  backgroundBrush_ = _brush;
  update ();
}

//------------------------------------------------------------------------------

/// Sets the background pen
void Text::setBackgroundPen(QPen _pen)
{
  backgroundPen_ = _pen;
  update ();
}

//------------------------------------------------------------------------------

/// Returns the shape for proper repainting/event handling
QPainterPath Text::shape() const
{
  if (!backgroundSet_)
  {
    QPainterPath shape = QGraphicsSimpleTextItem::shape ();

    QFontMetrics fn (font ());

    if (alignment_ == Qt::AlignHCenter || alignment_ == Qt::AlignRight)
    {
      QTransform trans;
      if (alignment_ == Qt::AlignHCenter)
        trans = QTransform::fromTranslate ((geometry ().size ().width () - fn.boundingRect (QGraphicsSimpleTextItem::text ()).width ()) / 2, 0);
      else if (alignment_ == Qt::AlignRight)
        trans = QTransform::fromTranslate (geometry ().size ().width () - fn.boundingRect (QGraphicsSimpleTextItem::text ()).width (), 0);

      QList<QPolygonF> poly = shape.toSubpathPolygons (trans);

      shape = QPainterPath ();

      foreach (const QPolygonF &p, poly)
        shape.addPolygon(p);
    }

    return shape;
  }

  qreal h = geometry ().size ().height () / 2.0;

  QPainterPath path;

  path.moveTo (geometry ().size ().width () / 2, 0);
  if (leftOut_)
  {
    path.lineTo (h, 0);
    path.arcTo (0, 0, h * 2.0, h * 2.0, 90, 180);
  }
  else
  {
    path.lineTo (-h, 0);
    path.arcTo (-h * 2, 0, h * 2.0, h * 2.0, 90, -180);
  }

  if (rightOut_)
  {
    path.lineTo (geometry ().size ().width () - h, geometry ().size ().height ());
    path.arcTo (geometry ().size ().width () - (h * 2), 0, h * 2.0, h * 2.0, -90, 180);
  }
  else
  {
    path.lineTo (geometry ().size ().width () + h, geometry ().size ().height ());
    path.arcTo (geometry ().size ().width (), 0, h * 2.0, h * 2.0, -90, -180);
  }
  path.lineTo (geometry ().size ().width () / 2, 0);

  return path;
}

//------------------------------------------------------------------------------
}