Subversion Repositories tpanel

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
446 andreas 1
/*
2
 * Copyright (C) 2023, 2024 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
 
19
#include <QLineEdit>
20
#include <QTextEdit>
21
#include <QLabel>
22
#include <QHBoxLayout>
23
#include <QEvent>
24
#include <QKeyEvent>
25
#include <QApplication>
26
#include <QPainter>
27
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
28
#include <QAnyStringView>
29
#endif
30
 
31
#include "tqeditline.h"
32
#include "tqsingleline.h"
33
#include "tqmultiline.h"
34
#include "terror.h"
35
 
36
using std::string;
37
 
38
TQEditLine::TQEditLine(QWidget *widget, bool multiline)
39
    : QWidget(widget),
40
      mMultiline(multiline)
41
{
42
    DECL_TRACER("TQEditLine::TQEditLine(QWidget *widget, bool multiline)");
43
 
44
    init();
45
}
46
 
47
TQEditLine::TQEditLine(string &text, QWidget *widget, bool multiline)
48
    : QWidget(widget),
49
      mText(text),
50
      mMultiline(multiline)
51
 
52
{
53
    DECL_TRACER("TQEditLine::TQEditLine(string &text, QWidget *widget, bool multiline)");
54
 
55
    init();
56
}
57
 
58
TQEditLine::~TQEditLine()
59
{
60
    DECL_TRACER("TQEditLine::~TQEditLine()");
61
 
62
    if (mMultiline && mTextArea)
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
    }
68
    else if (!mMultiline && mEdit)
69
    {
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);
75
    }
76
 
77
    if (mLayout)
78
        delete mLayout;
79
}
80
 
81
void TQEditLine::init()
82
{
83
    DECL_TRACER("TQEditLine::init()");
84
 
85
    setAutoFillBackground(false);
86
 
87
    if (!mLayout)
88
    {
89
        mLayout = new QHBoxLayout(this);
90
        mLayout->setSpacing(0);
91
        mLayout->setContentsMargins(0, 0, 0, 0);
92
 
93
        if (mMultiline)
94
            mTextArea = new TQMultiLine;
95
        else
96
            mEdit = new TQSingleLine;
97
 
98
        QPalette pal(palette());
99
 
100
        pal.setColor(QPalette::Window, Qt::transparent);
101
        pal.setColor(QPalette::Base, Qt::transparent);
102
        pal.setColor(QPalette::WindowText, Qt::black);
103
        pal.setColor(QPalette::Text, Qt::black);
104
 
105
        if (!mText.empty())
106
        {
107
            if (mMultiline)
108
                mTextArea->setText(QString::fromStdString(mText));
109
            else
110
                mEdit->setText(QString::fromStdString(mText));
111
        }
112
 
113
        if (mMultiline)
114
        {
115
            mTextArea->setPalette(pal);
116
 
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);
135
    }
136
}
137
 
138
void TQEditLine::setText(string &text)
139
{
140
    DECL_TRACER("TQEditLine::setText(string &text)");
141
 
142
    mText = text;
143
    MSG_DEBUG("Setting text: " << text);
144
 
145
    if (mMultiline && mTextArea)
146
        mTextArea->setText(QString::fromStdString(text));
147
    else if (!mMultiline && mEdit)
148
        mEdit->setText(QString::fromStdString(text));
149
 
150
    mChanged = false;
151
}
152
 
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
 
163
void TQEditLine::setObjectName(const string& name)
164
{
165
    DECL_TRACER("TQEditLine::setObjectName(const string& name)");
166
 
167
    if (name.empty())
168
        return;
169
 
170
    QWidget::setObjectName(QString::fromStdString(name));
171
    QString editName("Edit#");
172
    editName.append(QString::fromStdString(name));
173
 
174
    if (mMultiline && mTextArea)
175
        mTextArea->setObjectName(editName);
176
    else if (!mMultiline && mEdit)
177
        mEdit->setObjectName(editName);
178
 
179
    if (mLayout)
180
        mLayout->setObjectName(QString("Layout#%1").arg(QString::fromStdString(name)));
181
}
182
 
183
void TQEditLine::setPasswordChar(uint c)
184
{
185
    DECL_TRACER("TQEditLine::setPasswordChar(uint c)");
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
 
196
void TQEditLine::setFixedSize(int w, int h)
197
{
198
    DECL_TRACER("TQEditLine::setFixedSize(int w, int h)");
199
 
200
    if (mLayout && w > 0 && h > 0)
201
    {
202
        QWidget::setFixedSize(w, h);
203
 
204
        if (mMultiline)
205
            mTextArea->setFixedSize(w - 1 - mPadLeft - mPadRight, h - 1 - mPadTop - mPadBottom);
206
        else
207
            mEdit->setFixedSize(w - 1 - mPadLeft - mPadRight, h - 1 - mPadTop - mPadBottom);
208
 
209
        mWidth = w;
210
        mHeight = h;
211
    }
212
}
213
 
214
void TQEditLine::setFont(QFont &font)
215
{
216
    DECL_TRACER("TQEditLine::setFont(QFont &font)");
217
 
218
    if (!mMultiline && mEdit)
219
        mEdit->setFont(font);
220
    else if (mMultiline && mTextArea)
221
        mTextArea->setFont(font);
222
}
223
 
224
void TQEditLine::setPalette(QPalette &pal)
225
{
226
    DECL_TRACER("TQEditLine::setPalette(QPalette &pal)");
227
 
228
    QWidget::setPalette(pal);
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)
248
        mEdit->setPalette(pal);
249
    else
250
        mTextArea->setPalette(pal);
251
}
252
 
253
void TQEditLine::setBgColor(QColor &col)
254
{
255
    DECL_TRACER("TQEditLine::setBgColor(QColor &col)");
256
 
257
    mBgColor = col;
258
}
259
 
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
 
271
void TQEditLine::grabGesture(Qt::GestureType type, Qt::GestureFlags flags)
272
{
273
    DECL_TRACER("TQEditLine::grabGesture(Qt::GestureType type, Qt::GestureFlags flags)");
274
 
275
    if (mMultiline && mTextArea)
276
        mTextArea->grabGesture(type, flags);
277
    else if (!mMultiline && mEdit)
278
        mEdit->grabGesture(type, flags);
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);
289
 
290
    if (mMultiline && mTextArea)
291
        mTextArea->setFixedSize(mWidth - 1 - mPadLeft - mPadRight, mHeight - 1 - mPadTop - mPadBottom);
292
    else if (!mMultiline && mEdit)
293
        mEdit->setFixedSize(mWidth - 1 - mPadLeft - mPadRight, mHeight - 1 - mPadTop - mPadBottom);
294
 
295
    mLayout->setContentsMargins(mPadLeft, mPadTop, mPadRight, mPadBottom);
296
}
297
 
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
 
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
 
315
void TQEditLine::clear()
316
{
317
    DECL_TRACER("TQEditLine::clear()");
318
 
319
    if (mMultiline && mTextArea)
320
        mTextArea->clear();
321
    else if (!mMultiline && mEdit)
322
        mEdit->clear();
323
 
324
    mText.clear();
325
}
326
 
327
void TQEditLine::setInputMask(const std::string& mask)
328
{
329
    DECL_TRACER("TQEditLine::setInputMask(const std::string& mask)");
330
 
331
    if (!mMultiline && mEdit)
332
        mEdit->setInputMask(QString::fromStdString(mask));
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
 
343
#ifndef __ANDROID__
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
 
363
/*
364
 * Here the signal and callback functions follow.
365
 */
366
void TQEditLine::onKeyPressed(int key)
367
{
368
    DECL_TRACER("TQEditLine::onKeyPressed(int key)");
369
 
370
    if (key == Qt::Key_Enter || key == Qt::Key_Return)
371
    {
372
        string txt;
373
 
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)
380
        {
381
            mText = txt;
382
            emit inputChanged(mHandle, mText);
383
            mChanged = false;
384
        }
385
    }
386
 
387
    QApplication::processEvents();
388
}
389
 
390
void TQEditLine::hideEvent(QHideEvent *event)
391
{
392
    DECL_TRACER("TQEditLine::hideEvent(QHideEvent *event)");
393
 
394
    Q_UNUSED(event);
395
    _end();
396
}
397
 
398
void TQEditLine::leaveEvent(QEvent *event)
399
{
400
    DECL_TRACER("TQEditLine::leaveEvent(QEvent *event)");
401
 
402
    Q_UNUSED(event);
403
    _end();
404
}
405
 
406
void TQEditLine::closeEvent(QCloseEvent *event)
407
{
408
    DECL_TRACER("TQEditLine::closeEvent(QCloseEvent *event)");
409
 
410
    Q_UNUSED(event);
411
    _end();
412
}
413
 
414
void TQEditLine::onFocusChanged(bool in)
415
{
416
    DECL_TRACER("TQEditLine::onFocusChanged(bool in)");
417
 
418
    emit focusChanged(mHandle, in);
419
}
420
 
421
void TQEditLine::_end()
422
{
423
    DECL_TRACER("TQEditLine::_end()");
424
 
425
    string text;
426
 
427
    if (mMultiline && mTextArea)
428
        text = mTextArea->toPlainText().toStdString();
429
    else if (!mMultiline && mEdit)
430
        text = mEdit->text().toStdString();
431
 
432
    MSG_DEBUG("Current text: " << text);
433
 
434
    if (mChanged || text != mText)
435
    {
436
        mText = text;
437
        emit inputChanged(mHandle, mText);
438
        mChanged = false;
439
    }
440
}
441
 
442
void TQEditLine::onTextChanged(const QString &text)
443
{
444
    DECL_TRACER("TQEditLine::onTextChanged(const QString &text)");
445
 
446
    mText = text.toStdString();
447
    MSG_DEBUG("Text changed to: " << mText);
448
    mChanged = true;
449
}
450
 
451
void TQEditLine::onTextAreaChanged()
452
{
453
    DECL_TRACER("TQEditLine::onTextAreaChanged()");
454
 
455
    mText = mTextArea->toPlainText().toStdString();
456
    MSG_DEBUG("Multiline text changed to: " << mText);
457
    mChanged = true;
458
}
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
}
473
 
474
void TQEditLine::paintEvent(QPaintEvent* event)
475
{
476
    DECL_TRACER("TQEditLine::paintEvent(QPaintEvent* event)");
477
 
478
    Q_UNUSED(event);
479
 
480
    if (mBackground.isNull())
481
    {
482
        mBackground = QPixmap(width(), height());
483
        mBackground.fill(mBgColor);
484
    }
485
 
486
    QPainter p(this);
487
    p.drawPixmap(0, 0, mBackground);
488
}