~ubuntu-branches/ubuntu/hoary/kvirc/hoary

« back to all changes in this revision

Viewing changes to src/kvilib/qt/kvi_lineedit_qt.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Robin Verduijn
  • Date: 2004-12-14 15:32:19 UTC
  • mfrom: (0.2.1 upstream) (1.1.1 warty)
  • Revision ID: james.westby@ubuntu.com-20041214153219-fdink3gyp2s20b6g
Tags: 2:2.1.3.1-2
* Change Recommends on xmms to a Suggests.
* Rebuild against KDE 3.3.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// =============================================================================
 
2
//
 
3
//      --- kvi_lineedit_qt.cpp ---
 
4
//
 
5
//   This file is part of the KVIrc IRC client distribution
 
6
//   Copyright (C) 1999-2000 Szymon Stefanek (stefanek@tin.it)
 
7
//
 
8
//   This program is FREE software. You can redistribute it and/or
 
9
//   modify it under the terms of the GNU General Public License
 
10
//   as published by the Free Software Foundation; either version 2
 
11
//   of the License, or (at your opinion) any later version.
 
12
//
 
13
//   This program is distributed in the HOPE that it will be USEFUL,
 
14
//   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 
16
//   See the GNU General Public License for more details.
 
17
//
 
18
//   You should have received a copy of the GNU General Public License
 
19
//   along with this program. If not, write to the Free Software Foundation,
 
20
//   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
21
//
 
22
// =============================================================================
 
23
 
 
24
#define _KVI_DEBUG_CHECK_RANGE_
 
25
#define _KVI_DEBUG_CLASS_NAME_ "KviLineEdit"
 
26
 
 
27
#include "kvi_lineedit_qt.h"
 
28
#include "kvi_malloc.h"
 
29
#include "kvi_mirccntrl.h"
 
30
 
 
31
KviLineEdit::KviLineEdit(QWidget *parent, const char *name)
 
32
        : QLineEdit(parent, name)
 
33
{
 
34
        setFocusPolicy(ClickFocus);
 
35
        m_szAltKeyCode = "";
 
36
        connect(this, SIGNAL(returnPressed(const QString &)), SLOT(slot_returnPressed(const QString &)));
 
37
}
 
38
 
 
39
KviLineEdit::~KviLineEdit()
 
40
{
 
41
        // Nothing here
 
42
}
 
43
 
 
44
QString KviLineEdit::text() const
 
45
{
 
46
        KviStr text(QLineEdit::text());
 
47
        char *p = (char *) kvi_malloc(text.len() + 1);
 
48
        qstrcpy(p, text.ptr());
 
49
        text = "";
 
50
        while( *p ) {
 
51
                if( *p == '~' ) {
 
52
                        p++;
 
53
                        switch( *p ) {
 
54
                                case '~': text.append(*p);
 
55
                                        break;
 
56
                                case 'b': text.append((char) KVI_TEXT_BOLD);
 
57
                                        break;
 
58
                                case 'c': text.append((char) KVI_TEXT_RESET);
 
59
                                        break;
 
60
                                case 'r': text.append((char) KVI_TEXT_REVERSE);
 
61
                                        break;
 
62
                                case 'u': text.append((char) KVI_TEXT_UNDERLINE);
 
63
                                        break;
 
64
                                default:
 
65
                                        text.append((char) KVI_TEXT_COLOR);
 
66
                                        text.append(*p);
 
67
                        }
 
68
                } else text.append(*p);
 
69
                p++;
 
70
        }
 
71
        return QString(text.ptr());
 
72
}
 
73
 
 
74
void KviLineEdit::keyPressEvent(QKeyEvent *e)
 
75
{
 
76
        if( e->state() & ControlButton ) {
 
77
                switch( e->key() ) {
 
78
                        case Qt::Key_B:
 
79
                                QLineEdit::insert(QString("~b"));
 
80
                                break;
 
81
                        case Qt::Key_K:
 
82
                                QLineEdit::insert(QString("~"));
 
83
                                break;
 
84
                        case Qt::Key_O:
 
85
                                QLineEdit::insert(QString("~c"));
 
86
                                break;
 
87
                        case Qt::Key_R:
 
88
                                QLineEdit::insert(QString("~r"));
 
89
                                break;
 
90
                        case Qt::Key_U:
 
91
                                QLineEdit::insert(QString("~u"));
 
92
                                break;
 
93
                        default:
 
94
                                QLineEdit::keyPressEvent(e);
 
95
                }
 
96
                return;
 
97
        }
 
98
 
 
99
        if( e->state() & AltButton ) {
 
100
                // Qt::Key_Meta seems to substitute Key_Alt on some keyboards
 
101
                if( (e->key() == Qt::Key_Alt) || (e->key() == Qt::Key_Meta) ) {
 
102
                        m_szAltKeyCode = "";
 
103
                        return;
 
104
                } else if( (e->ascii() >= '0') && (e->ascii() <= '9') ) {
 
105
                        m_szAltKeyCode += e->ascii();
 
106
                        return;
 
107
                }
 
108
        }
 
109
        QLineEdit::keyPressEvent(e);
 
110
}
 
111
 
 
112
void KviLineEdit::keyReleaseEvent(QKeyEvent *e)
 
113
{
 
114
        if( (e->key() == Qt::Key_Alt) || (e->key() == Qt::Key_Meta) ) {
 
115
                if( m_szAltKeyCode.hasData() ) {
 
116
                        bool bOk;
 
117
                        char ch = m_szAltKeyCode.toChar(&bOk);
 
118
                        if( bOk && ch > ' ' ) {
 
119
                                insert(KviStr(ch).ptr());
 
120
                                e->accept();
 
121
                        }
 
122
                }
 
123
                m_szAltKeyCode = "";
 
124
        }
 
125
        e->ignore();
 
126
}
 
127
 
 
128
void KviLineEdit::setText(const char *text)
 
129
{
 
130
        KviStr t;
 
131
        while( *text ) {
 
132
                switch( *text ) {
 
133
                        case '~':
 
134
                                t.append("~~");
 
135
                                break;
 
136
                        case KVI_TEXT_BOLD:
 
137
                                t.append("~b");
 
138
                                break;
 
139
                        case KVI_TEXT_RESET:
 
140
                                t.append("~c");
 
141
                                break;
 
142
                        case KVI_TEXT_REVERSE:
 
143
                                t.append("~r");
 
144
                                break;
 
145
                        case KVI_TEXT_UNDERLINE:
 
146
                                t.append("~u");
 
147
                                break;
 
148
                        case KVI_TEXT_COLOR:
 
149
                                t.append('~');
 
150
                                break;
 
151
                        default:
 
152
                                t.append(*text);
 
153
                }
 
154
                text++;
 
155
        }
 
156
        QLineEdit::setText(QString(t.ptr()));
 
157
}
 
158
 
 
159
void KviLineEdit::insert(const char *newText)
 
160
{
 
161
        KviStr t;
 
162
        while( *newText ) {
 
163
                switch( *newText ) {
 
164
                        case '~':
 
165
                                t.append("~~");
 
166
                                break;
 
167
                        case KVI_TEXT_BOLD:
 
168
                                t.append("~b");
 
169
                                break;
 
170
                        case KVI_TEXT_RESET:
 
171
                                t.append("~c");
 
172
                                break;
 
173
                        case KVI_TEXT_REVERSE:
 
174
                                t.append("~r");
 
175
                                break;
 
176
                        case KVI_TEXT_UNDERLINE:
 
177
                                t.append("~u");
 
178
                                break;
 
179
                        case KVI_TEXT_COLOR:
 
180
                                t.append("~");
 
181
                                break;
 
182
                        default:
 
183
                                t.append(*newText);
 
184
                }
 
185
                newText++;
 
186
        }
 
187
        QLineEdit::insert(QString(t.ptr()));
 
188
}
 
189
 
 
190
void KviLineEdit::slot_returnPressed(const QString &)
 
191
{
 
192
        emit QLineEdit::returnPressed();
 
193
}
 
194
 
 
195
#include "m_kvi_lineedit_qt.moc"