File: bubble.cpp

package info (click to toggle)
boats 201307-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,424 kB
  • ctags: 1,486
  • sloc: cpp: 10,139; makefile: 13; xml: 10
file content (174 lines) | stat: -rw-r--r-- 5,473 bytes parent folder | download | duplicates (2)
1
2
3
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
39
40
41
42
43
44
45
46
47
48
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
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
144
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
//
// C++ Implementation: BubbleGraphicsItem
//
// Description:
//
//
// Author: Thibaut GRIDEL <tgridel@free.fr>
//
// Copyright (c) 2008-2010 Thibaut GRIDEL
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
//
//
#include <iostream>

#include <QPainter>
#include <QGraphicsSceneMouseEvent>
#include <QGraphicsScene>

#include "bubble.h"

#include "commontypes.h"
#include "situationmodel.h"
#include "trackmodel.h"
#include "boatmodel.h"

#include "undocommands.h"

extern int debugLevel;

BubbleGraphicsItem::BubbleGraphicsItem(PositionModel *model, QGraphicsItem *parent)
        : QGraphicsTextItem(parent),
        m_model(model),
        m_tail(new QGraphicsPolygonItem(parent)) {
    setFlag(QGraphicsItem::ItemIsMovable);
    setFlag(QGraphicsItem::ItemIsSelectable);

    m_tail->setZValue(0);
    m_tail->setBrush(Qt::white);

    updateText(model->text());
    setPosition(model->textPosition());

    connect(document(), SIGNAL(contentsChanged()),
            this, SLOT(setText()));


    if (m_model->inherits("BoatModel")) {
        BoatModel *boat = static_cast<BoatModel*> (m_model);
        connect(boat, SIGNAL(headingChanged(qreal)),
                this, SLOT(setTail()));
    }
    connect(model, SIGNAL(textChanged(QString)),
            this, SLOT(updateText(QString)));
    connect(model, SIGNAL(textPositionChanged(QPointF)),
            this, SLOT(setPosition(QPointF)));
}


BubbleGraphicsItem::~BubbleGraphicsItem() {}

/// update view from model changes
void BubbleGraphicsItem::updateText(QString value) {
    setVisible(!value.isEmpty());
    if (document()->toPlainText() != value)
        setPlainText(value);
    setTail();
}

/// set model from control changes
void BubbleGraphicsItem::setText() {
    if (m_model->situation()) {
        if (document()->toPlainText() != m_model->text()) {
            m_model->situation()->undoStack()->push(new SetTextUndoCommand(m_model, document()->toPlainText()));
            setVisible(!document()->toPlainText().isEmpty());
            setTail();
        }
    }
}

/// update view from model changes
void BubbleGraphicsItem::setPosition(QPointF position) {
    setPos(position);
    setTail();
    update();
}

void BubbleGraphicsItem::setTail() {
    double a = 0;
    if (BoatModel *boat = dynamic_cast<BoatModel*> (m_model)) {
        a = -boat->heading() * M_PI / 180;
        // set the transform to have horizontal text
        setTransform(QTransform().rotateRadians(a));
    }

    //centerx,y is half the size of the text
    double centerx = boundingRect().width()/2;
    double centery = boundingRect().height()/2;
    // x,y is the center of the text
    double x = pos().x() + centerx*cos(a) - centery*sin(a);
    double y = pos().y() + centerx*sin(a) + centery*cos(a);
    // t is a factor for the width of the tail
    double t = boundingRect().height()/5;
    // q is the angle of the tail
    double q = atan2(y, x);
    QPolygonF polygon;
    polygon << QPointF(x,y) << QPointF(x - t*sin(q), y + t*cos(q))
            << QPointF(x/2, y/2) << QPointF(x + t*sin(q), y - t*cos(q));
    m_tail->setPolygon(polygon);
}

QRectF BubbleGraphicsItem::boundingRect() const {
    return QGraphicsTextItem::boundingRect().adjusted(-4, -4, 4, 4);
}

void BubbleGraphicsItem::paint(QPainter *painter,
                               const QStyleOptionGraphicsItem *option,
                               QWidget *widget) {
    QPainterPath path;
    path.addRoundedRect(QGraphicsTextItem::boundingRect().adjusted(-2, -2, 2, 2),
                        15.0, 15.0, Qt::RelativeSize);
    painter->save();
    painter->setBrush(Qt::white);
    painter->drawPath(path);
    painter->restore();
    QGraphicsTextItem::paint(painter, option, widget);
}

void BubbleGraphicsItem::setVisible(bool visible) {
    QGraphicsItem::setVisible(visible);
    m_tail->setVisible(visible);
}


void BubbleGraphicsItem::mousePressEvent(QGraphicsSceneMouseEvent *event) {
    m_clickTime.start();
    QGraphicsTextItem::mousePressEvent(event);
    m_fromPosition = m_model->textPosition();
}

void BubbleGraphicsItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
    QGraphicsTextItem::mouseMoveEvent(event);
    setTail();
}

void BubbleGraphicsItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
    bool click = (m_clickTime.elapsed() < 250);
    if (click) {
        setTextInteractionFlags(Qt::TextEditorInteraction);
        setFocus(Qt::MouseFocusReason);
    } else {
        setTextInteractionFlags(Qt::NoTextInteraction);
    }
    QGraphicsTextItem::mouseReleaseEvent(event);
    if (pos() != m_fromPosition) {
        m_model->situation()->undoStack()->push(new MoveTextUndoCommand(m_model, pos() - m_fromPosition));
    }
}

void BubbleGraphicsItem::focusOutEvent(QFocusEvent *event) {
    setTextInteractionFlags(Qt::NoTextInteraction);
    QGraphicsTextItem::focusOutEvent(event);
}