Subversion Repositories tpanel

Rev

Details | Last modification | View Log | RSS feed

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