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