File: overlay.cpp

package info (click to toggle)
supercollider 1%3A3.13.0%2Brepack-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 80,292 kB
  • sloc: cpp: 476,363; lisp: 84,680; ansic: 77,685; sh: 25,509; python: 7,909; makefile: 3,440; perl: 1,964; javascript: 974; xml: 826; java: 677; yacc: 314; lex: 175; objc: 152; ruby: 136
file content (204 lines) | stat: -rw-r--r-- 6,328 bytes parent folder | download | duplicates (4)
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
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
/*
    SuperCollider Qt IDE
    Copyright (c) 2012 Jakob Leben & Tim Blechmann
    http://www.audiosynth.com

    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 2 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, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
*/

#include "overlay.hpp"
#include "editor.hpp"
#include "sc_editor.hpp"
#include "../../core/main.hpp"
#include "../../core/settings/theme.hpp"

#include <QTextDocument>
#include <QTextLayout>
#include <QTextBlock>
#include <QPainter>
#include <QPropertyAnimation>
#include <QtCore/qmath.h>
#include <QDebug>

namespace ScIDE {

OverlayAnimator::OverlayAnimator(GenericCodeEditor* editor, QObject* parent):
    QObject(parent),
    mEditor(editor),
    mBackgroundAnimation(this, "backgroundColor") {}

QColor OverlayAnimator::backgroundColor() const { return mEditor->mOverlay->backgroundBrush().color(); }

void OverlayAnimator::setBackgroundColor(const QColor& color) { mEditor->mOverlay->setBackgroundBrush(color); }

void OverlayAnimator::setActiveAppearance(bool active) {
    QColor color = mEditor->palette().color(QPalette::Base);
    if (color.lightness() >= 128)
        color = color.darker(60);
    else
        color = color.lighter(50);

    if (active)
        color.setAlpha(0);
    else
        color.setAlpha(mEditor->inactiveFadeAlpha());

    mBackgroundAnimation.stop();

    if (mEditor->isVisible()) {
        mBackgroundAnimation.setDuration(500);
        mBackgroundAnimation.setEasingCurve(QEasingCurve::OutCubic);
        mBackgroundAnimation.setStartValue(backgroundColor());
        mBackgroundAnimation.setEndValue(color);
        mBackgroundAnimation.start();
    } else {
        setBackgroundColor(color);
    }
}

void ScCodeEditor::blinkCode(const QTextCursor& c) {
    if (!c.document() || !c.hasSelection())
        return;

    Settings::Manager* settings = Main::settings();
    QTextCharFormat evalCodeTextFormat = settings->getThemeVal("evaluatedCode");

    QTextDocument* doc = c.document();

    int startPos = c.selectionStart();
    int endPos = c.selectionEnd();
    QTextBlock startBlock = doc->findBlock(startPos);
    QTextBlock endBlock = doc->findBlock(endPos);
    startPos -= startBlock.position();
    endPos -= endBlock.position();

    // Get the bounds of visible blocks within the cursor's selection:

    QTextBlock block = firstVisibleBlock();
    int idx = block.blockNumber();
    int sidx = startBlock.blockNumber();

    QTextBlock firstBlock, lastBlock;
    firstBlock = lastBlock = block;

    QRectF geom = blockBoundingGeometry(block).translated(contentOffset());
    qreal top = geom.top();
    qreal bottom = top;
    qreal width = 0;

    while (block.isValid() && bottom < viewport()->rect().height()) {
        if (block.isVisible()) {
            QTextLayout* l = block.layout();
            QRectF r = l->boundingRect();
            bottom += r.height();
            if (idx < sidx) {
                // Block not within the selection. Will skip it.
                top = bottom;
            } else {
                // Block within the selection.
                width = qMax(width, l->maximumWidth() + r.left());
            }
        }

        if (block == endBlock)
            break;

        block = block.next();
        ++idx;
        if (top == bottom)
            firstBlock = block;
    }

    lastBlock = block;

    if (bottom == top) {
        // qDebug("no visible block.");
        return;
    }

    // Construct a pixmap to render the code on:

    QPixmap pix(QSize(qCeil(width), qCeil(bottom - top)));
    pix.fill(QColor(0, 0, 0, 0));

    // Render the visible blocks:

    QPainter painter(&pix);
    QVector<QTextLayout::FormatRange> selections;
    block = firstBlock;
    int y = 0;
    while (block.isValid()) {
        if (block.isVisible()) {
            QRectF blockRect = block.layout()->boundingRect();

            // Use extra char formatting to hide code outside of selection
            // and modify the appearance of selected code:

            QTextLayout::FormatRange range;
            selections.clear();

            int start = 0;
            if (block == startBlock) {
                range.start = 0;
                range.length = startPos;
                range.format.setForeground(QColor(0, 0, 0, 0));
                range.format.setBackground(Qt::NoBrush);
                selections.append(range);
                start = startPos;
            }

            range.start = start;
            range.length = (block == endBlock ? endPos : block.length() - 1) - range.start;
            range.format = evalCodeTextFormat;
            selections.append(range);

            if (block == endBlock) {
                range.start = range.start + range.length;
                range.length = block.length() - 1 - range.start;
                range.format.setForeground(QColor(0, 0, 0, 0));
                range.format.setBackground(Qt::NoBrush);
                selections.append(range);
            }

            block.layout()->draw(&painter, QPointF(0, y), selections);

            y += blockRect.height();
        }

        if (block == lastBlock)
            break;

        block = block.next();
    }

    // Create an overlay item to display the pixmap, and animate it:

    CodeFragmentOverlay* item = new CodeFragmentOverlay();
    item->setPixmap(pix);
    item->setPos(geom.left(), top);

    mOverlay->addItem(item);

    QPropertyAnimation* anim = new QPropertyAnimation(item, "opacity", item);
    anim->setDuration(mBlinkDuration);
    anim->setStartValue(1.0);
    anim->setEndValue(0.0);
    anim->setEasingCurve(QEasingCurve::InCubic);
    anim->start();

    connect(anim, SIGNAL(finished()), item, SLOT(deleteLater()));
}

} // namespace ScIDE