aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/scxmleditor/plugin_interface/textitem.cpp
blob: 3dd86f33e6d7e5477e0ad196892722bce5f4d7cb (plain)
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
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0

#include "textitem.h"
#include <QCursor>
#include <QGraphicsScene>
#include <QGraphicsSceneMouseEvent>
#include <QPainter>
#include <QTextCursor>
#include <QTextDocument>
#include <QTextOption>
#include <qmath.h>

using namespace ScxmlEditor::PluginInterface;

TextItem::TextItem(QGraphicsItem *parent)
    : QGraphicsTextItem(parent)
{
    init();
}

TextItem::TextItem(const QString &id, QGraphicsItem *parent)
    : QGraphicsTextItem(id, parent)
{
    init();
}

void TextItem::init()
{
    setTextInteractionFlags(Qt::TextEditorInteraction);
    setFlag(ItemIsSelectable, true);
    setFlag(ItemIsFocusable, true);

    QTextOption options;
    options.setWrapMode(QTextOption::NoWrap);
    options.setAlignment(Qt::AlignCenter);
    document()->setDefaultTextOption(options);

    connect(document(), &QTextDocument::contentsChanged, this, &TextItem::checkText);

    QFont f = font();
    f.setPixelSize(13);
    setFont(f);
}

void TextItem::setItalic(bool ital)
{
    QFont f = font();
    f.setItalic(ital);
    setFont(f);
}

void TextItem::checkText()
{
    if (document()->textWidth() <= 40)
        document()->setTextWidth(40);
    else
        document()->setTextWidth(-1);

    emit textChanged();
}

void TextItem::setText(const QString &t)
{
    QSignalBlocker blocker(this);
    setPlainText(t);
}

void TextItem::focusInEvent(QFocusEvent *event)
{
    QGraphicsTextItem::focusInEvent(event);
    emit selected(true);
}

void TextItem::focusOutEvent(QFocusEvent *event)
{
    emit textReady(toPlainText());
    QGraphicsTextItem::focusOutEvent(event);
}

void TextItem::keyPressEvent(QKeyEvent *event)
{
    switch (event->key()) {
    case Qt::Key_Escape:
    case Qt::Key_Enter:
    case Qt::Key_Return:
    case Qt::Key_Tab:
        event->accept();
        clearFocus();
        return;
    default:
        break;
    }

    QGraphicsTextItem::keyPressEvent(event);
}

bool TextItem::needIgnore(const QPointF sPos) const
{
    // If we found QuickTransition-item or CornerGrabber at this point, we must ignore mouse press here
    // So we can press QuickTransition/CornerGrabber item although there is transition lines front of these items
    const QList<QGraphicsItem *> items = scene()->items(sPos);
    for (QGraphicsItem *item : items) {
        if (item->type() == QuickTransitionType || (item->type() == CornerGrabberType && item->parentItem() != this))
            return true;
    }

    return false;
}

void TextItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
    if (needIgnore(event->scenePos())) {
        event->ignore();
        return;
    }

    QGraphicsTextItem::mousePressEvent(event);
    setFocus();
}

void TextItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
    if (needIgnore(event->scenePos())) {
        event->ignore();
        return;
    }

    QGraphicsTextItem::mouseReleaseEvent(event);
    setFocus();
}

void TextItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
{
    if (needIgnore(event->scenePos())) {
        event->ignore();
        return;
    }

    setFocus();
    QGraphicsTextItem::mouseDoubleClickEvent(event);
    emit selected(true);
}

void TextItem::hoverEnterEvent(QGraphicsSceneHoverEvent *e)
{
    if (needIgnore(e->scenePos())) {
        e->ignore();
        return;
    }

    setCursor(Qt::IBeamCursor);
    QGraphicsTextItem::hoverEnterEvent(e);
}

void TextItem::hoverMoveEvent(QGraphicsSceneHoverEvent *e)
{
    if (needIgnore(e->scenePos())) {
        setCursor(Qt::ArrowCursor);
        e->ignore();
        return;
    }

    setCursor(Qt::IBeamCursor);
    QGraphicsTextItem::hoverEnterEvent(e);
}

void TextItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *e)
{
    setCursor(Qt::ArrowCursor);
    QGraphicsTextItem::hoverLeaveEvent(e);
}