Subversion Repositories tpanel

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
390 andreas 1
/*
2
 * Copyright (C) 2023 by Andreas Theofilu <andreas@theosys.at>
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 3 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * This program is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program; if not, write to the Free Software Foundation,
16
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
17
 */
18
#include <QPainter>
19
#include <QString>
20
#include <QStaticText>
21
 
22
#include "tqmarquee.h"
23
#include "terror.h"
24
 
25
TQMarquee::TQMarquee(QWidget* parent)
391 andreas 26
    : QLabel(parent),
390 andreas 27
      mParent(parent)
28
{
29
    DECL_TRACER("TQMarquee::TQMarquee(QWidget* parent)");
30
 
31
    init();
32
}
33
 
391 andreas 34
TQMarquee::TQMarquee(QWidget* parent, int msec, MQ_TYPES type, bool enable)
35
    : QLabel(parent),
390 andreas 36
      mParent(parent),
391 andreas 37
      mScrollEnabled(enable),
395 andreas 38
      mType(type),
39
      mSpeed(msec)
390 andreas 40
{
391 andreas 41
    DECL_TRACER("TQMarquee::TQMarquee(QWidget* parent, int msec, MQ_TYPES type, bool enable)");
390 andreas 42
 
391 andreas 43
    if (mSpeed < 1)
44
        mSpeed = 1;
45
    else if (mSpeed > 10)
46
        mSpeed = 10;
47
 
390 andreas 48
    init();
49
}
50
 
51
TQMarquee::~TQMarquee()
52
{
53
    DECL_TRACER("TQMarquee::~TQMarquee()");
54
}
55
 
56
void TQMarquee::init()
57
{
58
    DECL_TRACER("TQMarquee::init()");
59
 
391 andreas 60
    switch(mType)
61
    {
395 andreas 62
        case MQ_LEFT:   mDirection = Qt::RightToLeft; break;
63
        case MQ_RIGHT:  mDirection = Qt::LeftToRight; break;
390 andreas 64
 
391 andreas 65
        default:
395 andreas 66
            mDirection = Qt::RightToLeft;
391 andreas 67
    }
390 andreas 68
 
391 andreas 69
    connect(&mTimer, &QTimer::timeout, this, &TQMarquee::refreshLabel);
390 andreas 70
    // By default the background image is transparent
71
    mBackgroundImage.fill(Qt::transparent);
72
    setBackgroundColor(mBgColor);
391 andreas 73
    mTimer.start(mSpeed);
390 andreas 74
}
75
 
391 andreas 76
void TQMarquee::setAlignment(Qt::Alignment al)
77
{
78
    DECL_TRACER("TQMarquee::setAlignment(Qt::Alignment al)");
79
 
80
    mAlign = al;
81
    updateCoordinates();
82
    QLabel::setAlignment(al);
83
}
84
 
85
void TQMarquee::setFrame(int left, int top, int right, int bottom)
86
{
87
    DECL_TRACER("TQMarquee::setFrame(int left, int top, int right, int bottom)");
88
 
394 andreas 89
    mFrameLeft = (left > 0 && left < width() ? left : 0);
90
    mFrameTop = (top > 0 && top < height() ? top : 0);
91
    mFrameRight = (right > 0 && right < width() ? right : 0);
92
    mFrameBottom = (bottom > 0 && bottom < height() ? bottom : 0);
93
 
395 andreas 94
    if ((mFrameLeft + mFrameRight) > width())
95
        mFrameLeft = mFrameRight = 0;
394 andreas 96
 
395 andreas 97
    if ((mFrameTop + mFrameBottom) > height())
98
        mFrameTop = mFrameBottom = 0;
394 andreas 99
 
100
    MSG_DEBUG("Frame size: l=" << mFrameLeft << ", r=" << mFrameRight << ", t=" << mFrameTop << ", b=" << mFrameBottom);
391 andreas 101
}
102
 
390 andreas 103
void TQMarquee::setText(const QString& text)
104
{
105
    DECL_TRACER("TQMarquee::setText(const QString& text)");
106
 
107
    mText = text;
391 andreas 108
    refreshLabel();
390 andreas 109
    update();
110
}
111
 
391 andreas 112
void TQMarquee::setSpeed(int msec)
390 andreas 113
{
391 andreas 114
    DECL_TRACER("TQMarquee::setSpeed(int msec)");
390 andreas 115
 
391 andreas 116
    if (msec < 1 || msec > 10)
117
    {
118
        MSG_WARNING("Wrong speed " << msec << "! The speed must be between 1 and 10.");
119
        return;
120
    }
121
 
122
    mSpeed = msec;
123
 
124
    if (mTimer.isActive())
125
        mTimer.start(mSpeed);
126
    else
127
        mTimer.setInterval(mSpeed);
390 andreas 128
}
129
 
391 andreas 130
void TQMarquee::pause()
390 andreas 131
{
391 andreas 132
    DECL_TRACER("TQMarquee::pause()");
390 andreas 133
 
391 andreas 134
    if (mTimer.isActive())
135
        mTimer.stop();
390 andreas 136
}
137
 
391 andreas 138
void TQMarquee::resume()
390 andreas 139
{
391 andreas 140
    DECL_TRACER("TQMarquee::resume()");
390 andreas 141
 
391 andreas 142
    if (!mTimer.isActive())
143
        mTimer.start(mSpeed);
390 andreas 144
}
145
 
146
QColor TQMarquee::backgroundColor()
147
{
148
    DECL_TRACER("TQMarquee::backgroundColor()");
149
 
150
    QPalette::ColorRole colorRole = backgroundRole();
151
    QBrush brush = palette().brush(colorRole);
152
    return brush.color();
153
}
154
 
155
void TQMarquee::setBackgroundColor(QColor& color)
156
{
157
    DECL_TRACER("TQMarquee::setBackgroundColor(QColor& color)");
158
 
159
    QBrush brush;
160
    brush.setColor(color);
161
    QPalette pal;
162
    pal.setBrush(backgroundRole(), brush);
163
    setPalette(pal);
164
}
165
 
391 andreas 166
QPixmap TQMarquee::background()
390 andreas 167
{
168
    DECL_TRACER("TQMarquee::background()");
169
 
170
    return mBackgroundImage;
171
}
172
 
391 andreas 173
void TQMarquee::setBackground(QPixmap& image)
390 andreas 174
{
391 andreas 175
    DECL_TRACER("TQMarquee::setBackground(QPixmap& image)");
390 andreas 176
 
177
    mBackgroundImage = image;
391 andreas 178
    setPixmap(mBackgroundImage);
179
    repaint();
390 andreas 180
}
181
 
391 andreas 182
void TQMarquee::setDirection(MQ_TYPES type)
390 andreas 183
{
391 andreas 184
    DECL_TRACER("TQMarquee::setDirection(MQ_TYPES type)");
390 andreas 185
 
391 andreas 186
    mType = type;
390 andreas 187
 
391 andreas 188
    switch(type)
189
    {
395 andreas 190
        case MQ_LEFT:   mDirection = Qt::RightToLeft; break;
191
        case MQ_RIGHT:  mDirection = Qt::LeftToRight; break;
390 andreas 192
 
391 andreas 193
        default:
395 andreas 194
            mDirection = Qt::RightToLeft;
391 andreas 195
    }
196
 
395 andreas 197
    if (mType == MQ_RIGHT || mType == MQ_PONG)
391 andreas 198
        px = width() - (mTextLength + mFrameLeft + mFrameRight);
395 andreas 199
    else if (mType == MQ_LEFT)
391 andreas 200
        px = mFrameLeft;
393 andreas 201
    else if (mType == MQ_DOWN)
202
        py = height() - (mTextHeight + mFrameTop + mFrameBottom);
203
    else if (mType == MQ_UP)
204
        py = mFrameTop;
391 andreas 205
 
206
    refreshLabel();
390 andreas 207
}
208
 
391 andreas 209
void TQMarquee::refreshLabel()
390 andreas 210
{
391 andreas 211
    DECL_TRACER("TQMarquee::refreshLabel()");
390 andreas 212
 
391 andreas 213
    repaint();
214
    std::this_thread::sleep_for(std::chrono::milliseconds(5));
390 andreas 215
}
216
 
217
void TQMarquee::paintEvent(QPaintEvent*)
218
{
391 andreas 219
//    DECL_TRACER("TQMarquee::paintEvent(QPaintEvent*)");   // This would fill up a logfile, if there is one.
390 andreas 220
 
221
    QPainter p(this);
391 andreas 222
    p.drawPixmap(0, 0, mBackgroundImage);
390 andreas 223
 
391 andreas 224
    if (mScrollEnabled)
390 andreas 225
    {
395 andreas 226
        if(mType == MQ_LEFT)
391 andreas 227
        {
228
            px -= mSpeed;
390 andreas 229
 
391 andreas 230
            if(px <= (-mTextLength))
231
                px = width();
232
        }
395 andreas 233
        else if (mType == MQ_RIGHT)
391 andreas 234
        {
235
            px += mSpeed;
390 andreas 236
 
391 andreas 237
            if(px >= width())
393 andreas 238
                px = -mTextLength;
390 andreas 239
        }
393 andreas 240
        else if (mType == MQ_PONG)
241
        {
242
            if (mDirection == Qt::LeftToRight)
243
            {
244
                px -= mSpeed;
245
                bool changeDirection = false;
390 andreas 246
 
393 andreas 247
                if (mTextLength > width())
248
                    changeDirection = (px <= ((mTextLength - (width() - mFrameLeft - mFrameRight)) * -1));
249
                else
250
                    changeDirection = (px <= mFrameLeft);
251
 
252
                if (changeDirection)
253
                    mDirection = Qt::RightToLeft;
254
            }
255
            else
256
            {
257
                px += mSpeed;
258
                bool changeDirection = false;
259
 
260
                if (mTextLength > width())
261
                    changeDirection = (px >= mFrameLeft);
262
                else
263
                    changeDirection = (px >= (width() - mTextLength - mFrameRight));
264
 
265
                if (changeDirection)
266
                    mDirection = Qt::LeftToRight;
267
            }
268
        }
269
        else if (mType == MQ_DOWN)
270
        {
271
            py += mSpeed;
272
 
273
            if (py >= height())
274
                py = -mTextHeight;
275
        }
276
        else if (mType == MQ_UP)
277
        {
278
            py -= mSpeed;
279
 
280
            if (py <= (-mTextHeight))
281
                py = height();
282
        }
283
 
394 andreas 284
        if (mFrameLeft || mFrameRight || mFrameTop || mFrameBottom)
285
            p.setClipRect(QRect(mFrameLeft, mFrameTop, width() - mFrameLeft - mFrameRight, height() - mFrameBottom - mFrameTop), Qt::ReplaceClip);
393 andreas 286
 
391 andreas 287
        p.drawText(px, py + mFontPointSize, mText);
288
        p.translate(px,0);
390 andreas 289
    }
290
    else
391 andreas 291
        QLabel::setText(mText);
292
}
390 andreas 293
 
391 andreas 294
void TQMarquee::resizeEvent(QResizeEvent* evt)
295
{
296
    DECL_TRACER("TQMarquee::resizeEvent(QResizeEvent* evt)");
390 andreas 297
 
391 andreas 298
    updateCoordinates();
299
    QLabel::resizeEvent(evt);
390 andreas 300
}
301
 
391 andreas 302
void TQMarquee::updateCoordinates()
390 andreas 303
{
391 andreas 304
    DECL_TRACER("TQMarquee::updateCoordinates()");
390 andreas 305
 
391 andreas 306
    switch(mAlign)
390 andreas 307
    {
391 andreas 308
        case Qt::AlignTop:      py = mFrameTop; break;
309
        case Qt::AlignBottom:   py = height() - (mFrameTop + mFrameBottom); break;
310
        case Qt::AlignVCenter:  py = height() / 2; break;
390 andreas 311
    }
312
 
391 andreas 313
    mFontPointSize = font().pointSize() / 2;
314
    mTextLength = fontMetrics().horizontalAdvance(mText);
393 andreas 315
    mTextHeight = fontMetrics().height();
316
 
317
    if (mType == MQ_UP || mType == MQ_DOWN)
318
    {
319
        if (width() < mTextLength)
320
            px = ((mTextLength - width()) / 2) * -1;
321
        else
322
            px = width() - mTextLength / 2;
323
    }
390 andreas 324
}