File: tickerboard.cpp

package info (click to toggle)
tipp10 3.3.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,984 kB
  • sloc: cpp: 8,343; xml: 70; ansic: 60; makefile: 11
file content (316 lines) | stat: -rw-r--r-- 9,650 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
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
309
310
311
312
313
314
315
316
/*
Copyright (c) 2006-2009, Tom Thielicke IT Solutions

SPDX-License-Identifier: GPL-2.0-only
*/

/****************************************************************
**
** Implementation of the TickerBoard class
** File name: tickerboard.h
**
****************************************************************/

#include <QCoreApplication>
#include <QFontMetrics>
#include <QPainter>
#include <QRgb>
#include <QSettings>

#include "def/defines.h"
#include "errormessage.h"
#include "tickerboard.h"

TickerBoard::TickerBoard(QWidget* parent)
    : QWidget(parent)
    , startFlag(false)
    , txtCurrentLesson("")
    , txtCompleteLesson("")
    , counterCurrentLesson(0)
    , counterCompleteLesson(0)
    , lengthCurrentLesson(0)
    , lengthCompleteLesson(0)
    , counterRow(0)
    , widthCurrentLesson(0)
    , widthSelection(0)
    , lessonOffset(0)
    , scrollOffset(0)
    , scrollCounter(0)
    , txtPause("")
    , colorSelection(colorCursor)
{
    tickerFont = QFont(t10::font_standard, t10::font_size_ticker);
    tickerFont.setStyleStrategy(QFont::PreferAntialias);
    QFontMetrics fm(tickerFont);

    if (!background.load(":/img/tickerbg.png")) {
        // Error message
        ErrorMessage* errorMessage = new ErrorMessage(this);
        errorMessage->showMessage(Error::ticker_pic,
            ErrorMessage::Type::Warning, ErrorMessage::Cancel::Operation);
    }

    readSettings();

    connect(&timer, &QTimer::timeout, this, &TickerBoard::progress);

    setFixedSize(610, 65);

    setAttribute(Qt::WA_NoSystemBackground);

    // It is very important to set a strong focus on this widget
    // because it has always to receive the QKeyEvent event!!!
    setFocusPolicy(Qt::StrongFocus);
}

void TickerBoard::keyPressEvent(QKeyEvent* event)
{
    // A key was pressed, read the typed text
    QString typed = event->text();
    if (typed == "") {
        // Return if no text was typed
        // i.e. if only a modifier was pressed
        return;
    }
    // Emit the pressed QChar
    emit keyPressed(typed[0]);
}

void TickerBoard::startTicker(bool wasPaused)
{
    if ((lengthCompleteLesson = txtCompleteLesson.length()) > 0) {
        QFontMetrics fm(tickerFont);
        // Check current text and position
        widthSelection
            = fm.horizontalAdvance(txtCurrentLesson.at(counterCurrentLesson));
        widthCurrentLesson = fm.horizontalAdvance(txtCurrentLesson);
        newChar = txtCurrentLesson[counterCurrentLesson];

        if (tickerSpeed == 50) {
            scrollOffset = 290;
            scroll(-290, 0, QRect(10, 15, 590, 35)); // contentsRect());
        }

        startFlag = true;
        repaint();
        if (!wasPaused) {
            emit charChanged(newChar);
        }
    }
}

void TickerBoard::pauseTicker(QString txt)
{
    startFlag = false;
    txtPause = txt;
    update();
}

void TickerBoard::setTicker(QString txt)
{
    txtCompleteLesson = txt;
    splitLesson();
    checkUpdateRequired();
}

void TickerBoard::extendTicker(QString txt, QString seperator)
{
    txtCompleteLesson.append(seperator + txt);
    splitLesson();
    checkUpdateRequired();
}

void TickerBoard::getNewChar()
{
    colorSelection = colorCursor;
    if (startFlag) {
        changeChar();
    }
}

void TickerBoard::changeChar()
{
    counterCurrentLesson++;
    counterCompleteLesson++;
    if (counterCompleteLesson >= lengthCompleteLesson) {
        // No more text available
        txtPause = tr("Dictation Finished");
        startFlag = false;
        update();
        emit isReady();
        emit charChanged(' ');
        return;
    }
    if (counterCurrentLesson >= lengthCurrentLesson) {
        // Take a new text row
        counterCurrentLesson = 0;
        counterRow++;
        if (tickerSpeed != 50) {
            scrollOffset = 0;
        } else {
            scrollOffset = 290;
            scroll(-290, 0, QRect(10, 15, 590, 35)); // contentsRect());
        }
        splitLesson();
    }
    QFontMetrics fm(tickerFont);
    // txtSelectionWidth = fm.width(txtLesson[txtCounter]);
    lessonOffset = fm.horizontalAdvance(txtCurrentLesson, counterCurrentLesson);
    widthSelection
        = fm.horizontalAdvance(txtCurrentLesson.at(counterCurrentLesson));
    newChar = txtCurrentLesson[counterCurrentLesson];
    repaint();
    emit charChanged(newChar);
    checkUpdateRequired();
}

void TickerBoard::checkUpdateRequired()
{
    // Check whether a text update is required
    if ((lengthCompleteLesson - counterCompleteLesson)
        < t10::num_token_until_refresh) {
        // Text update required
        emit updateRequired();
    }
}

void TickerBoard::setErrorSelection()
{
    colorSelection = QColor(249, 126, 0);
    repaint();
}

void TickerBoard::clearErrorSelection()
{
    colorSelection = colorCursor;
    repaint();
}

void TickerBoard::setSpeed(int speed)
{
    // tickerSpeed = speed;
    if (timer.isActive() && tickerCurrentSpeed != speed) {
        tickerCurrentSpeed = speed;
        timer.setInterval(speed);
    }
}

void TickerBoard::splitLesson()
{
    QFontMetrics fm(tickerFont);
    lengthCompleteLesson = txtCompleteLesson.length();
    // Split lection into sentences with line break sign at the end
    // (split case sensitive and skip empty parts)
    txtLessonSplited = txtCompleteLesson.split(
        t10::token_new_line, Qt::SkipEmptyParts, Qt::CaseSensitive);
    txtCurrentLesson = txtLessonSplited.at(counterRow) + t10::token_new_line;
    lengthCurrentLesson = txtCurrentLesson.length();
    widthCurrentLesson = fm.horizontalAdvance(txtCurrentLesson);
    repaint();
}

void TickerBoard::paintEvent([[maybe_unused]] QPaintEvent* event)
{
    QPainter painter(this);
    painter.setPen(Qt::NoPen);
    painter.setBrush(colorBg);
    painter.drawRect(10, 15, 590, 35);
    if (startFlag) {
        // Draw current ticker position
        int x = 290;
        painter.setFont(tickerFont);
        painter.setBrush(colorSelection);
        painter.drawRect(
            10 + x - scrollOffset + lessonOffset, 15, widthSelection, 35);
        painter.setPen(colorFont);
        painter.drawText(10 + x - scrollOffset, 15, widthCurrentLesson, 35,
            Qt::AlignVCenter, txtCurrentLesson);
    } else {
        // Draw pause or start text
        QFont tmpfont = QFont(t10::font_standard, t10::font_size_ticker_pause);
        tmpfont.setStyleStrategy(QFont::PreferAntialias);
        painter.setFont(tmpfont);
        painter.setPen(colorFont);
        painter.drawText(
            10, 15, 590, 35, Qt::AlignCenter | Qt::AlignVCenter, txtPause);
    }
    painter.drawPixmap(0, 0, background);
}

void TickerBoard::progress()
{
    if (startFlag) {
        if (tickerSpeed != 50) {
            if (scrollOffset < lessonOffset) {

                // Move ticker 1 pixel to left
                scrollOffset++;
                scroll(-1, 0, QRect(10, 15, 590, 35)); // contentsRect());

                if ((lessonOffset - scrollOffset) <= 30) {
                    setSpeed(tickerSpeed);
                }
                if ((lessonOffset - scrollOffset) > 30
                    && (lessonOffset - scrollOffset) <= 60) {
                    setSpeed(tickerSpeed - (3 * (tickerSpeed / 10)));
                }
                if ((lessonOffset - scrollOffset) > 60
                    && (lessonOffset - scrollOffset) <= 90) {
                    setSpeed(tickerSpeed - (5 * (tickerSpeed / 10)));
                }
                if ((lessonOffset - scrollOffset)
                    > 90) { // && (lessonOffset - scrollOffset) <= 120) {
                    setSpeed(tickerSpeed - (8 * (tickerSpeed / 10)));
                }
                /*if ((lessonOffset - scrollOffset) > 120) {
                        setSpeed(1);
                }*/
            }
            // If the user types faster than the ticker, move ticker faster
            // after 160 pixels overage (because the user must see at least the
            // next word)
            if ((lessonOffset - scrollOffset) > 200) {
                scrollOffset += (lessonOffset - scrollOffset) - 200;
                scroll(-((lessonOffset - scrollOffset) - 200), 0,
                    QRect(10, 15, 590, 35)); // contentsRect());
            }
        } else {
            // If the user types faster than the ticker, move ticker faster
            // after 160 pixels overage (because the user must see at least the
            // next word)
            if ((lessonOffset - scrollOffset) > 280) {
                scrollOffset += 570;
                scroll(-570, 0, QRect(10, 15, 590, 35)); // contentsRect());
            }
        }
    }
}

void TickerBoard::readSettings()
{
#if APP_PORTABLE
    QSettings settings(
        QCoreApplication::applicationDirPath() + "/portable/settings.ini",
        QSettings::IniFormat);
#else
    QSettings settings;
#endif
    settings.beginGroup("settings");
    colorFont = QColor(
        settings.value("ticker_font_color", t10::ticker_color_font).toString());
    colorBg = QColor(
        settings.value("ticker_bg_color", t10::ticker_color_bg).toString());
    colorCursor
        = QColor(settings.value("ticker_cursor_color", t10::ticker_color_cursor)
                     .toString());
    colorSelection = colorCursor;
    tickerFont.fromString(
        settings.value("ticker_font", tickerFont.toString()).toString());
    setSpeed(tickerSpeed = 50
            - (settings.value("ticker_speed", t10::tickerspeed_standard).toInt()
                * 10));
    // tickerSpeed = 38 + (settings.value("ticker_speed",
    //	t10::tickerspeed_standard).toInt() * 10);
    // setSpeed(1);
    settings.endGroup();
}