Subversion Repositories tpanel

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
420 andreas 1
/*
2
 * Copyright (C) 2022, 2023 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 <QPainter>
20
#include <QEvent>
21
#include <QKeyEvent>
421 andreas 22
#include <QApplication>
420 andreas 23
 
24
#include "tqtextedit.h"
25
#include "terror.h"
26
 
27
using std::string;
28
 
29
TQTextEdit::TQTextEdit(QWidget* parent)
421 andreas 30
    : QWidget(parent)
420 andreas 31
{
32
    DECL_TRACER("TQTextEdit::TQTextEdit(QWidget* parent)");
33
 
34
    init();
35
}
36
 
37
TQTextEdit::TQTextEdit(const QString& text, QWidget* parent)
421 andreas 38
    : QWidget(parent),
420 andreas 39
      mText(text)
40
{
41
    DECL_TRACER("TQTextEdit::TQTextEdit(const QString& text, QWidget* parent)");
42
}
43
 
44
void TQTextEdit::init()
45
{
46
    DECL_TRACER("TQTextEdit::init()");
47
 
48
    setCursor(Qt::IBeamCursor);
421 andreas 49
    setAutoFillBackground(true);
50
    update();
51
//    setTextInteractionFlags(Qt::TextEditorInteraction);
52
//    setTextFormat(Qt::PlainText);
420 andreas 53
}
54
 
55
void TQTextEdit::setText(const QString& text)
56
{
57
    DECL_TRACER("TQTextEdit::setText(const std::string& text)");
58
 
59
    if (mText != text)
60
    {
61
        mText = text;
421 andreas 62
        update();
420 andreas 63
    }
64
}
65
 
66
bool TQTextEdit::event(QEvent* event)
67
{
68
    if (event->type() == QEvent::KeyPress)
69
    {
70
        QKeyEvent *sKey = static_cast<QKeyEvent*>(event);
71
 
72
        if (sKey)
73
        {
74
            switch(sKey->key())
75
            {
76
                case Qt::Key_Enter:
77
                case Qt::Key_Return:
78
                    emit contentChanged(mText);
79
                break;
80
 
81
                case Qt::Key_0: insert("0"); break;
82
                case Qt::Key_1: insert("1"); break;
83
                case Qt::Key_2: insert("2"); break;
84
                case Qt::Key_3: insert("3"); break;
85
                case Qt::Key_4: insert("4"); break;
86
                case Qt::Key_5: insert("5"); break;
87
                case Qt::Key_6: insert("6"); break;
88
                case Qt::Key_7: insert("7"); break;
89
                case Qt::Key_8: insert("8"); break;
90
                case Qt::Key_9: insert("9"); break;
91
                case Qt::Key_A: insert(mShift || mCapsLock ? "A" : "a"); break;
92
                case Qt::Key_B: insert(mShift || mCapsLock ? "B" : "b"); break;
93
                case Qt::Key_C: insert(mShift || mCapsLock ? "C" : "c"); break;
94
                case Qt::Key_D: insert(mShift || mCapsLock ? "D" : "d"); break;
95
                case Qt::Key_E: insert(mShift || mCapsLock ? "E" : "e"); break;
96
                case Qt::Key_F: insert(mShift || mCapsLock ? "F" : "f"); break;
97
                case Qt::Key_G: insert(mShift || mCapsLock ? "G" : "g"); break;
98
                case Qt::Key_H: insert(mShift || mCapsLock ? "H" : "h"); break;
99
                case Qt::Key_I: insert(mShift || mCapsLock ? "I" : "i"); break;
100
                case Qt::Key_J: insert(mShift || mCapsLock ? "J" : "j"); break;
101
                case Qt::Key_K: insert(mShift || mCapsLock ? "K" : "k"); break;
102
                case Qt::Key_L: insert(mShift || mCapsLock ? "L" : "l"); break;
103
                case Qt::Key_M: insert(mShift || mCapsLock ? "M" : "m"); break;
104
                case Qt::Key_N: insert(mShift || mCapsLock ? "N" : "n"); break;
105
                case Qt::Key_O: insert(mShift || mCapsLock ? "O" : "o"); break;
106
                case Qt::Key_P: insert(mShift || mCapsLock ? "P" : "p"); break;
107
                case Qt::Key_Q: insert(mShift || mCapsLock ? "Q" : "q"); break;
108
                case Qt::Key_R: insert(mShift || mCapsLock ? "R" : "r"); break;
109
                case Qt::Key_S: insert(mShift || mCapsLock ? "S" : "s"); break;
110
                case Qt::Key_T: insert(mShift || mCapsLock ? "T" : "t"); break;
111
                case Qt::Key_U: insert(mShift || mCapsLock ? "U" : "u"); break;
112
                case Qt::Key_V: insert(mShift || mCapsLock ? "V" : "v"); break;
113
                case Qt::Key_W: insert(mShift || mCapsLock ? "W" : "w"); break;
114
                case Qt::Key_X: insert(mShift || mCapsLock ? "X" : "x"); break;
115
                case Qt::Key_Y: insert(mShift || mCapsLock ? "Y" : "y"); break;
116
                case Qt::Key_Z: insert(mShift || mCapsLock ? "Z" : "z"); break;
117
 
118
                case Qt::Key_Exclam:        insert("!"); break;
119
                case Qt::Key_Space:         insert(" "); break;
120
                case Qt::Key_Ampersand:     insert("&"); break;
121
                case Qt::Key_Slash:         insert("/"); break;
122
                case Qt::Key_Backslash:     insert("\\"); break;
123
                case Qt::Key_QuoteDbl:      insert("\""); break;
124
                case Qt::Key_Percent:       insert("%"); break;
125
                case Qt::Key_Dollar:        insert("$"); break;
126
                case Qt::Key_paragraph:     insert("§"); break;
127
                case Qt::Key_BraceLeft:     insert("{"); break;
128
                case Qt::Key_BraceRight:    insert("}"); break;
129
                case Qt::Key_BracketLeft:   insert("["); break;
130
                case Qt::Key_BracketRight:  insert("]"); break;
131
                case Qt::Key_ParenLeft:     insert("("); break;
132
                case Qt::Key_ParenRight:    insert(")"); break;
133
                case Qt::Key_Equal:         insert("="); break;
134
                case Qt::Key_Question:      insert("?"); break;
135
                case Qt::Key_degree:        insert("°"); break;
136
                case Qt::Key_Colon:         insert(":"); break;
137
                case Qt::Key_Comma:         insert(","); break;
138
                case Qt::Key_Underscore:    insert("_"); break;
139
                case Qt::Key_hyphen:        insert("-"); break;
140
                case Qt::Key_Semicolon:     insert(";"); break;
141
                case Qt::Key_Bar:           insert("|"); break;
142
                case Qt::Key_Greater:       insert(">"); break;
143
                case Qt::Key_Less:          insert("<"); break;
144
                case Qt::Key_AsciiCircum:   insert("^"); break;
145
                case Qt::Key_Plus:          insert("+"); break;
146
                case Qt::Key_Period:        insert("."); break;
147
                case Qt::Key_QuoteLeft:     insert("`"); break;
148
                case Qt::Key_Apostrophe:    insert("'"); break;
149
                case Qt::Key_Asterisk:      insert("*"); break;
150
                case Qt::Key_AsciiTilde:    insert("~"); break;
151
                case Qt::Key_At:            insert("@"); break;
152
                case Qt::Key_mu:            insert("µ"); break;
153
 
154
                default:
155
                    return false;
156
            }
157
 
158
            return true;
159
        }
160
    }
161
 
162
    return false;
163
}
164
 
165
void TQTextEdit::append(const QString& txt)
166
{
167
    DECL_TRACER("TQTextEdit::append(const QString& txt)");
168
 
169
    mText += txt;
170
//    QLabel::setText(mText);
171
    mPos = mText.length();
421 andreas 172
    update();
420 andreas 173
}
174
 
175
void TQTextEdit::insert(const QString& txt, int pos)
176
{
177
    DECL_TRACER("TQTextEdit::insert(const QString& txt, int pos)");
178
 
179
    if (mText.isEmpty())
180
    {
181
        mText = txt;
182
//        QLabel::setText(mText);
183
        mPos = mText.length();
421 andreas 184
        update();
420 andreas 185
        return;
186
    }
187
 
188
    if (pos >= 0)
189
    {
190
        if ((qsizetype)pos > mText.length())
191
            mPos = mText.length();
192
        else
193
            mPos = pos;
194
    }
195
 
196
    if ((qsizetype)mPos == mText.length())
197
    {
198
        append(txt);
199
        return;
200
    }
201
 
202
    if (mPos > 0)
203
    {
204
        QString left = txt.mid(0, mPos);
205
        QString right = txt.mid(mPos);
206
 
207
        mText = left + txt + right;
208
        mPos += txt.length();
209
    }
210
    else
211
    {
212
        mText = txt + mText;
213
        mPos += txt.length();
214
    }
215
 
421 andreas 216
    update();
420 andreas 217
//    QLabel::setText(mText);
218
}
219
 
220
void TQTextEdit::setAlignment(Qt::Alignment al)
221
{
222
    DECL_TRACER("TQTextEdit::setAlignment(Qt::Alignment al)");
223
 
224
    mAlignment = al;
225
    updateCoordinates();
226
}
227
 
228
void TQTextEdit::setPadding(int left, int top, int right, int bottom)
229
{
230
    DECL_TRACER("TQTextEdit::setPadding(int left, int top, int right, int bottom)");
231
 
232
    mPadLeft = (left >= 0 ? left : 0);
233
    mPadTop = (top >= 0 ? left : 0);
234
    mPadRight = (right >= 0 ? left : 0);
235
    mPadBottom = (bottom >= 0 ? left : 0);
236
    updateCoordinates();
237
}
238
 
239
void TQTextEdit::updateCoordinates()
240
{
241
    DECL_TRACER("TQTextEdit::updateCoordinates()");
242
 
243
    mFontPointSize = font().pointSize() / 2;
244
    mTextLength = fontMetrics().horizontalAdvance(mText);
245
    mTextHeight = fontMetrics().height();
246
 
247
    if (mAlignment & Qt::AlignLeft)
248
        mPosX = mPadLeft;
249
    else if (mAlignment & Qt::AlignHCenter)
250
        mPosX = width() / 2 - mTextLength / 2;
251
    else if (mAlignment & Qt::AlignRight)
252
        mPosX = width() - mPadRight - mTextLength;
253
 
254
    if (mAlignment & Qt::AlignTop)
255
        mPosY = mPadTop + mTextHeight / 2;
256
    else if (mAlignment & Qt::AlignVCenter)
257
        mPosY = height() / 2;
258
    else if (mAlignment & Qt::AlignBottom)
259
        mPosY = height() - mPadBottom - mTextHeight / 2;
260
 
261
    MSG_DEBUG("Font point size: " << mFontPointSize << ", text length: " << mTextLength << ", text height: " << mTextHeight);
262
    MSG_DEBUG("Pos X: " << mPosX << ", Pos Y: " << mPosY);
421 andreas 263
//    QApplication::processEvents();
264
    update();
420 andreas 265
}
266
 
267
void TQTextEdit::setBackgroundPixmap(const QPixmap& pixmap)
268
{
269
    DECL_TRACER("TQTextEdit::setBackgroundPixmap(const QPixmap& pixmap)");
270
 
271
    if (pixmap.isNull())
272
        return;
273
 
274
    mBackground = pixmap;
421 andreas 275
//    setPixmap(mBackground);
276
//    QApplication::processEvents();
277
    update();
420 andreas 278
}
279
 
280
void TQTextEdit::resizeEvent(QResizeEvent *evt)
281
{
282
    DECL_TRACER("TQTextEdit::resizeEvent(QResizeEvent *evt)");
283
 
284
    updateCoordinates();
421 andreas 285
    QWidget::resizeEvent(evt);
420 andreas 286
}
287
 
288
void TQTextEdit::paintEvent(QPaintEvent*)
289
{
290
    DECL_TRACER("TQTextEdit::paintEvent(QPaintEvent*)");
291
 
292
    QPainter p(this);
293
 
294
    if (!mBackground.isNull())
295
        p.drawPixmap(0, 0, mBackground);
296
 
297
    p.drawText(mPosX, mPosY + mFontPointSize, mText);
298
}