Subversion Repositories tpanel

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
188 andreas 1
/*
439 andreas 2
 * Copyright (C) 2023, 2024 by Andreas Theofilu <andreas@theosys.at>
188 andreas 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
 
19
#include <QLineEdit>
192 andreas 20
#include <QTextEdit>
189 andreas 21
#include <QLabel>
419 andreas 22
#include <QHBoxLayout>
189 andreas 23
#include <QEvent>
24
#include <QKeyEvent>
212 andreas 25
#include <QApplication>
421 andreas 26
#include <QPainter>
264 andreas 27
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
192 andreas 28
#include <QAnyStringView>
211 andreas 29
#endif
303 andreas 30
 
31
#include "tqeditline.h"
419 andreas 32
#include "tqsingleline.h"
33
#include "tqmultiline.h"
303 andreas 34
#include "terror.h"
35
 
188 andreas 36
using std::string;
37
 
192 andreas 38
TQEditLine::TQEditLine(QWidget *widget, bool multiline)
421 andreas 39
    : QWidget(widget),
40
      mMultiline(multiline)
188 andreas 41
{
192 andreas 42
    DECL_TRACER("TQEditLine::TQEditLine(QWidget *widget, bool multiline)");
188 andreas 43
 
44
    init();
45
}
46
 
192 andreas 47
TQEditLine::TQEditLine(string &text, QWidget *widget, bool multiline)
421 andreas 48
    : QWidget(widget),
49
      mText(text),
192 andreas 50
      mMultiline(multiline)
188 andreas 51
 
52
{
192 andreas 53
    DECL_TRACER("TQEditLine::TQEditLine(string &text, QWidget *widget, bool multiline)");
188 andreas 54
 
55
    init();
56
}
57
 
58
TQEditLine::~TQEditLine()
59
{
60
    DECL_TRACER("TQEditLine::~TQEditLine()");
191 andreas 61
 
303 andreas 62
    if (mMultiline && mTextArea)
310 andreas 63
    {
64
        disconnect(mTextArea, &TQMultiLine::textChanged, this, &TQEditLine::onTextAreaChanged);
65
        disconnect(mTextArea, &TQMultiLine::focusChanged, this, &TQEditLine::onFocusChanged);
66
        disconnect(mTextArea, &TQMultiLine::keyPressed, this, &TQEditLine::onKeyPressed);
67
    }
303 andreas 68
    else if (!mMultiline && mEdit)
309 andreas 69
    {
310 andreas 70
        disconnect(mEdit, &TQSingleLine::textChanged, this, &TQEditLine::onTextChanged);
71
        disconnect(mEdit, &TQSingleLine::cursorPositionChanged, this, &TQEditLine::onCursorPositionChangedS);
72
        disconnect(mEdit, &TQSingleLine::editingFinished, this, &TQEditLine::onEditingFinished);
73
        disconnect(mEdit, &TQSingleLine::focusChanged, this, &TQEditLine::onFocusChanged);
74
        disconnect(mEdit, &TQSingleLine::keyPressed, this, &TQEditLine::onKeyPressed);
309 andreas 75
    }
323 andreas 76
 
419 andreas 77
    if (mLayout)
78
        delete mLayout;
188 andreas 79
}
80
 
81
void TQEditLine::init()
82
{
83
    DECL_TRACER("TQEditLine::init()");
84
 
425 andreas 85
    setAutoFillBackground(false);
86
 
419 andreas 87
    if (!mLayout)
88
    {
89
        mLayout = new QHBoxLayout(this);
90
        mLayout->setSpacing(0);
91
        mLayout->setContentsMargins(0, 0, 0, 0);
192 andreas 92
 
419 andreas 93
        if (mMultiline)
94
            mTextArea = new TQMultiLine;
95
        else
96
            mEdit = new TQSingleLine;
188 andreas 97
 
419 andreas 98
        QPalette pal(palette());
189 andreas 99
 
419 andreas 100
        pal.setColor(QPalette::Window, Qt::transparent);
425 andreas 101
        pal.setColor(QPalette::Base, Qt::transparent);
419 andreas 102
        pal.setColor(QPalette::WindowText, Qt::black);
103
        pal.setColor(QPalette::Text, Qt::black);
188 andreas 104
 
419 andreas 105
        if (!mText.empty())
106
        {
107
            if (mMultiline)
425 andreas 108
                mTextArea->setText(QString::fromStdString(mText));
419 andreas 109
            else
425 andreas 110
                mEdit->setText(QString::fromStdString(mText));
419 andreas 111
        }
189 andreas 112
 
419 andreas 113
        if (mMultiline)
114
        {
115
            mTextArea->setPalette(pal);
192 andreas 116
 
419 andreas 117
            QWidget::connect(mTextArea, &TQMultiLine::textChanged, this, &TQEditLine::onTextAreaChanged);
118
            QWidget::connect(mTextArea, &TQMultiLine::focusChanged, this, &TQEditLine::onFocusChanged);
119
            QWidget::connect(mTextArea, &TQMultiLine::keyPressed, this, &TQEditLine::onKeyPressed);
120
            mLayout->addWidget(mTextArea);
121
        }
122
        else
123
        {
124
            mEdit->setPalette(pal);
125
 
126
            QWidget::connect(mEdit, &TQSingleLine::textChanged, this, &TQEditLine::onTextChanged);
127
            QWidget::connect(mEdit, &TQSingleLine::cursorPositionChanged, this, &TQEditLine::onCursorPositionChangedS);
128
            QWidget::connect(mEdit, &TQSingleLine::editingFinished, this, &TQEditLine::onEditingFinished);
129
            QWidget::connect(mEdit, &TQSingleLine::focusChanged, this, &TQEditLine::onFocusChanged);
130
            QWidget::connect(mEdit, &TQSingleLine::keyPressed, this, &TQEditLine::onKeyPressed);
131
            mLayout->addWidget(mEdit);
132
        }
133
 
134
        QWidget::setLayout(mLayout);
188 andreas 135
    }
136
}
137
 
138
void TQEditLine::setText(string &text)
139
{
140
    DECL_TRACER("TQEditLine::setText(string &text)");
141
 
142
    mText = text;
437 andreas 143
    MSG_DEBUG("Setting text: " << text);
188 andreas 144
 
192 andreas 145
    if (mMultiline && mTextArea)
425 andreas 146
        mTextArea->setText(QString::fromStdString(text));
192 andreas 147
    else if (!mMultiline && mEdit)
425 andreas 148
        mEdit->setText(QString::fromStdString(text));
303 andreas 149
 
150
    mChanged = false;
188 andreas 151
}
152
 
439 andreas 153
void TQEditLine::setPlaceholderText(string& text)
154
{
155
    DECL_TRACER("TQEditLine::setPlaceholderText(string& text)");
156
 
157
    if (mMultiline && mTextArea)
158
        mTextArea->setPlaceholderText(QString::fromStdString(text));
159
    else if (!mMultiline && mEdit)
160
        mEdit->setPlaceholderText(QString::fromStdString(text));
161
}
162
 
266 andreas 163
void TQEditLine::setObjectName(const string& name)
164
{
165
    DECL_TRACER("TQEditLine::setObjectName(const string& name)");
166
 
419 andreas 167
    if (name.empty())
266 andreas 168
        return;
169
 
425 andreas 170
    QWidget::setObjectName(QString::fromStdString(name));
303 andreas 171
    QString editName("Edit#");
425 andreas 172
    editName.append(QString::fromStdString(name));
303 andreas 173
 
266 andreas 174
    if (mMultiline && mTextArea)
303 andreas 175
        mTextArea->setObjectName(editName);
266 andreas 176
    else if (!mMultiline && mEdit)
303 andreas 177
        mEdit->setObjectName(editName);
419 andreas 178
 
179
    if (mLayout)
425 andreas 180
        mLayout->setObjectName(QString("Layout#%1").arg(QString::fromStdString(name)));
266 andreas 181
}
182
 
192 andreas 183
void TQEditLine::setPasswordChar(uint c)
184
{
303 andreas 185
    DECL_TRACER("TQEditLine::setPasswordChar(uint c)");
192 andreas 186
 
187
    if (!mMultiline && mEdit && c)
188
    {
189
        char style[256];
190
        snprintf(style, sizeof(style), "lineedit-password-character: %d", c);
191
        mEdit->setStyleSheet(style);
192
        mEdit->setEchoMode(QLineEdit::Password);
193
    }
194
}
195
 
188 andreas 196
void TQEditLine::setFixedSize(int w, int h)
197
{
198
    DECL_TRACER("TQEditLine::setFixedSize(int w, int h)");
199
 
419 andreas 200
    if (mLayout && w > 0 && h > 0)
188 andreas 201
    {
191 andreas 202
        QWidget::setFixedSize(w, h);
192 andreas 203
 
204
        if (mMultiline)
419 andreas 205
            mTextArea->setFixedSize(w - 1 - mPadLeft - mPadRight, h - 1 - mPadTop - mPadBottom);
192 andreas 206
        else
419 andreas 207
            mEdit->setFixedSize(w - 1 - mPadLeft - mPadRight, h - 1 - mPadTop - mPadBottom);
192 andreas 208
 
191 andreas 209
        mWidth = w;
210
        mHeight = h;
188 andreas 211
    }
212
}
213
 
214
void TQEditLine::setFont(QFont &font)
215
{
216
    DECL_TRACER("TQEditLine::setFont(QFont &font)");
217
 
303 andreas 218
    if (!mMultiline && mEdit)
189 andreas 219
        mEdit->setFont(font);
303 andreas 220
    else if (mMultiline && mTextArea)
221
        mTextArea->setFont(font);
188 andreas 222
}
223
 
224
void TQEditLine::setPalette(QPalette &pal)
225
{
226
    DECL_TRACER("TQEditLine::setPalette(QPalette &pal)");
227
 
191 andreas 228
    QWidget::setPalette(pal);
310 andreas 229
}
230
 
231
void TQEditLine::setTextColor(QColor col)
232
{
233
    DECL_TRACER("TQEditLine::setTextColor(QColor col)");
234
 
235
    QPalette pal;
236
 
237
    if (!mMultiline && mEdit)
238
        pal = mEdit->palette();
239
    else if (mMultiline && mTextArea)
240
        pal = mTextArea->palette();
241
    else
242
        return;
243
 
244
    pal.setColor(QPalette::WindowText, col);
245
    pal.setColor(QPalette::Text, col);
246
 
247
    if (!mMultiline)
303 andreas 248
        mEdit->setPalette(pal);
310 andreas 249
    else
250
        mTextArea->setPalette(pal);
188 andreas 251
}
252
 
425 andreas 253
void TQEditLine::setBgColor(QColor &col)
254
{
255
    DECL_TRACER("TQEditLine::setBgColor(QColor &col)");
256
 
257
    mBgColor = col;
258
}
259
 
421 andreas 260
void TQEditLine::setBackgroundPixmap(QPixmap& pixmap)
261
{
262
    DECL_TRACER("TQEditLine::setBackgroundPixmap(QPixmap& pixmap)");
263
 
264
    if (pixmap.isNull())
265
        return;
266
 
267
    mBackground = pixmap;
268
    update();
269
}
270
 
188 andreas 271
void TQEditLine::grabGesture(Qt::GestureType type, Qt::GestureFlags flags)
272
{
273
    DECL_TRACER("TQEditLine::grabGesture(Qt::GestureType type, Qt::GestureFlags flags)");
274
 
192 andreas 275
    if (mMultiline && mTextArea)
276
        mTextArea->grabGesture(type, flags);
277
    else if (!mMultiline && mEdit)
189 andreas 278
        mEdit->grabGesture(type, flags);
188 andreas 279
}
280
 
281
void TQEditLine::setPadding(int left, int top, int right, int bottom)
282
{
283
    DECL_TRACER("TQEditLine::setPadding(int left, int top, int right, int bottom)");
284
 
285
    mPadLeft = (left < 0 ? 0 : left);
286
    mPadTop = (top < 0 ? 0 : top);
287
    mPadRight = (right < 0 ? 0 : right);
288
    mPadBottom = (bottom < 0 ? 0 : bottom);
190 andreas 289
 
192 andreas 290
    if (mMultiline && mTextArea)
419 andreas 291
        mTextArea->setFixedSize(mWidth - 1 - mPadLeft - mPadRight, mHeight - 1 - mPadTop - mPadBottom);
192 andreas 292
    else if (!mMultiline && mEdit)
419 andreas 293
        mEdit->setFixedSize(mWidth - 1 - mPadLeft - mPadRight, mHeight - 1 - mPadTop - mPadBottom);
294
 
295
    mLayout->setContentsMargins(mPadLeft, mPadTop, mPadRight, mPadBottom);
188 andreas 296
}
297
 
190 andreas 298
void TQEditLine::setFrameSize(int s)
299
{
300
    DECL_TRACER("TQEditLine::setFrameSize(int s)");
301
 
302
    setPadding(s + mPadLeft, s + mPadTop, s + mPadRight, s + mPadBottom);
303
}
304
 
419 andreas 305
void TQEditLine::setWordWrapMode(bool mode)
306
{
307
    DECL_TRACER("TQEditLine::setWordWrapMode(bool mode)");
308
 
309
    if (!mMultiline || !mTextArea)
310
        return;
311
 
312
    mTextArea->setWordWrapMode((mode ? QTextOption::WordWrap : QTextOption::NoWrap));
313
}
314
 
188 andreas 315
void TQEditLine::clear()
316
{
317
    DECL_TRACER("TQEditLine::clear()");
318
 
192 andreas 319
    if (mMultiline && mTextArea)
320
        mTextArea->clear();
321
    else if (!mMultiline && mEdit)
189 andreas 322
        mEdit->clear();
192 andreas 323
 
324
    mText.clear();
188 andreas 325
}
189 andreas 326
 
213 andreas 327
void TQEditLine::setInputMask(const std::string& mask)
328
{
329
    DECL_TRACER("TQEditLine::setInputMask(const std::string& mask)");
330
 
331
    if (!mMultiline && mEdit)
425 andreas 332
        mEdit->setInputMask(QString::fromStdString(mask));
213 andreas 333
}
334
 
335
void TQEditLine::setNumericInput()
336
{
337
    DECL_TRACER("TQEditLine::setNumericInput()");
338
 
339
    if (!mMultiline && mEdit)
340
        mEdit->setInputMethodHints(mEdit->inputMethodHints() | Qt::ImhDigitsOnly);
341
}
342
 
419 andreas 343
#ifndef __ANDROID__
213 andreas 344
void TQEditLine::setClearButtonEnabled(bool state)
345
{
346
    DECL_TRACER("TQEditLine::setClearButtonEnabled(bool state)");
347
 
348
    if (!mMultiline && mEdit)
349
        mEdit->setClearButtonEnabled(state);
350
}
351
 
352
void TQEditLine::setCursor(const QCursor& qc)
353
{
354
    DECL_TRACER("TQEditLine::setCursor(const QCursor& qc)");
355
 
356
    if (mMultiline && mTextArea)
357
        mTextArea->setCursor(qc);
358
    else if (!mMultiline && mEdit)
359
        mEdit->setCursor(qc);
360
}
361
#endif
362
 
189 andreas 363
/*
364
 * Here the signal and callback functions follow.
365
 */
310 andreas 366
void TQEditLine::onKeyPressed(int key)
189 andreas 367
{
310 andreas 368
    DECL_TRACER("TQEditLine::onKeyPressed(int key)");
369
 
370
    if (key == Qt::Key_Enter || key == Qt::Key_Return)
189 andreas 371
    {
303 andreas 372
        string txt;
189 andreas 373
 
303 andreas 374
        if (mMultiline && mTextArea)
375
            txt = mTextArea->toPlainText().toStdString();
376
        else if (!mMultiline && mEdit)
377
            txt = mEdit->text().toStdString();
378
 
379
        if (mChanged || txt != mText)
189 andreas 380
        {
425 andreas 381
            mText = txt;
303 andreas 382
            emit inputChanged(mHandle, mText);
383
            mChanged = false;
384
        }
421 andreas 385
    }
213 andreas 386
 
421 andreas 387
    QApplication::processEvents();
303 andreas 388
}
192 andreas 389
 
303 andreas 390
void TQEditLine::hideEvent(QHideEvent *event)
391
{
310 andreas 392
    DECL_TRACER("TQEditLine::hideEvent(QHideEvent *event)");
393
 
303 andreas 394
    Q_UNUSED(event);
395
    _end();
396
}
192 andreas 397
 
303 andreas 398
void TQEditLine::leaveEvent(QEvent *event)
399
{
310 andreas 400
    DECL_TRACER("TQEditLine::leaveEvent(QEvent *event)");
401
 
303 andreas 402
    Q_UNUSED(event);
403
    _end();
404
}
213 andreas 405
 
303 andreas 406
void TQEditLine::closeEvent(QCloseEvent *event)
407
{
310 andreas 408
    DECL_TRACER("TQEditLine::closeEvent(QCloseEvent *event)");
409
 
303 andreas 410
    Q_UNUSED(event);
411
    _end();
412
}
413
 
310 andreas 414
void TQEditLine::onFocusChanged(bool in)
309 andreas 415
{
310 andreas 416
    DECL_TRACER("TQEditLine::onFocusChanged(bool in)");
309 andreas 417
 
310 andreas 418
    emit focusChanged(mHandle, in);
309 andreas 419
}
420
 
310 andreas 421
void TQEditLine::_end()
309 andreas 422
{
310 andreas 423
    DECL_TRACER("TQEditLine::_end()");
309 andreas 424
 
303 andreas 425
    string text;
426
 
427
    if (mMultiline && mTextArea)
428
        text = mTextArea->toPlainText().toStdString();
429
    else if (!mMultiline && mEdit)
430
        text = mEdit->text().toStdString();
431
 
437 andreas 432
    MSG_DEBUG("Current text: " << text);
433
 
303 andreas 434
    if (mChanged || text != mText)
192 andreas 435
    {
425 andreas 436
        mText = text;
303 andreas 437
        emit inputChanged(mHandle, mText);
438
        mChanged = false;
192 andreas 439
    }
189 andreas 440
}
191 andreas 441
 
189 andreas 442
void TQEditLine::onTextChanged(const QString &text)
443
{
444
    DECL_TRACER("TQEditLine::onTextChanged(const QString &text)");
445
 
446
    mText = text.toStdString();
437 andreas 447
    MSG_DEBUG("Text changed to: " << mText);
192 andreas 448
    mChanged = true;
189 andreas 449
}
190 andreas 450
 
192 andreas 451
void TQEditLine::onTextAreaChanged()
452
{
453
    DECL_TRACER("TQEditLine::onTextAreaChanged()");
454
 
455
    mText = mTextArea->toPlainText().toStdString();
437 andreas 456
    MSG_DEBUG("Multiline text changed to: " << mText);
192 andreas 457
    mChanged = true;
458
}
309 andreas 459
 
460
void TQEditLine::onCursorPositionChangedS(int oldPos, int newPos)
461
{
462
    DECL_TRACER("TQEditLine::onCursorPositionChangedS(int oldPos, int newPos)");
463
 
464
    emit cursorPositionChanged(mHandle, oldPos, newPos);
465
}
466
 
467
void TQEditLine::onEditingFinished()
468
{
469
    DECL_TRACER("TQEditLine::onEditingFinished()");
470
 
471
    _end();
472
}
421 andreas 473
 
474
void TQEditLine::paintEvent(QPaintEvent* event)
475
{
476
    DECL_TRACER("TQEditLine::paintEvent(QPaintEvent* event)");
477
 
425 andreas 478
    Q_UNUSED(event);
479
 
421 andreas 480
    if (mBackground.isNull())
425 andreas 481
    {
482
        mBackground = QPixmap(width(), height());
483
        mBackground.fill(mBgColor);
484
    }
421 andreas 485
 
486
    QPainter p(this);
487
    p.drawPixmap(0, 0, mBackground);
488
}