Subversion Repositories tpanel

Rev

Details | Last modification | View Log | RSS feed

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