1/*
2 Copyright (c) 2007 Paolo Capriotti <p.capriotti@gmail.com>
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8*/
9
10#include "message.h"
11#include <QFontMetrics>
12#include <QPainter>
13#include <QTextDocument>
14#include <QTextOption>
15
16Message::Message(const QString& text, const QFont& font, int maxwidth)
17: QGraphicsTextItem(text)
18, m_opacity(1.0)
19, m_velocity(0.0, 0.0)
20{
21 setFont(font);
22 setDefaultTextColor(Qt::black);
23 setAcceptsHoverEvents(false);
24 document()->setTextWidth(maxwidth);
25 QTextOption opts = document()->defaultTextOption();
26 opts.setAlignment(Qt::AlignHCenter);
27 document()->setDefaultTextOption(opts);
28
29 // translate so that the origin is the center
30 translate(-document()->size().width() / 2,
31 -document()->size().height() / 2);
32}
33
34void Message::setOpacityF(qreal opacity)
35{
36 QGraphicsItem::setOpacity(opacity);
37}
38
39qreal Message::opacityF() const
40{
41 return QGraphicsItem::opacity();
42}
43
44void Message::setVelocity(const QPointF& vel)
45{
46 m_velocity = vel;
47}
48
49QPointF Message::velocity() const
50{
51 return m_velocity;
52}
53
54void Message::setPosition(const QPointF& pos)
55{
56 QGraphicsTextItem::setPos(pos);
57}
58
59QPointF Message::position() const
60{
61 return QGraphicsTextItem::pos();
62}
63
64int Message::height() const {
65 return document()->size().height();
66}
67