summaryrefslogtreecommitdiff
path: root/dldialog/src/DLD_QT/dld_input.cc
blob: f81b541a3d0e8d1bc002e5f3f078ddc1f4d00367 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/******************************************************************************
**
** $Id: dld_input.cc,v 1.15 2000/03/23 11:39:00 harald Exp $
**
**	This program is free software; you can redistribute it and/or
**	modify it under the terms of the GNU General Public License
**	as published by the Free Software Foundation; either version
**	2 of the License, or (at your option) any later version.
**
** (C) 1999,2000 Harald Hoyer <DLDialog@parzelle.de> 
**
******************************************************************************/
#include "dld_qtlib.h"
#include "dld_input.h"
#include "dld_text.h"
#include "dld_dialog.h"

#include <iostream.h>
#include <stdlib.h>
#include <stdio.h>
#include <qregexp.h>
#include <qstring.h>
#include <qkeycode.h>

DLD_QTInput::
DLD_QTInput (QWidget *_parent, const string& name, DLD_QTDialog *pd) :
   QLineEdit (_parent, convstr(name).c_str()),
   DLD_QTObj (name, pd),
   isIp(false),
   isNum(false),
   isPass(false),
   dld_length(0)
{
#ifdef DEBUG
   DLDdbg << DLD_LIB_PREFIX "input " << dld_name << " constructed "
	  << endl << flush;
#endif
   setMaxLength (1024);
}


DLD_QTInput::
~DLD_QTInput ()
{
#ifdef DEBUG
   DLDdbg << DLD_LIB_PREFIX "input deleted\n";
#endif
}

void DLD_QTInput::slotTextChanged1(const char *)
{
   varvalue = text();
   DLD_Obj::activate ();
}

void DLD_QTInput::slotTextChanged2(const QString &)
{
   varvalue = text();
   DLD_Obj::activate ();
}

void DLD_QTInput::slotReturn()
{
#ifdef DEBUG
   DLDdbg << DLD_LIB_PREFIX "return pressed" 
	  << endl << flush;
#endif      
   varvalue = text ();
   DLD_QTObj::activate ();
}

void DLD_QTInput::
create ()
{
   setText(pardia->get_env(varname).c_str());

   if (isPass)
   {
      setEchoMode(Password);
#ifdef DEBUG
      DLDdbg << DLD_LIB_PREFIX "input passwd\n";
#endif      
   }

   if (isNum)
   {
      setValidator (new DLD_NumInput (this));
#ifdef DEBUG
      DLDdbg << DLD_LIB_PREFIX "input numeric\n";
#endif
      setMinimumWidth (sizeHint ().width ());
   }
   else if (isIp)
   {
      if(!strlen(text())) 
	 setText("0.0.0.0");
      setMaxLength (15);
      setValidator (new DLD_IPInput (this));
      setFixedWidth (sizeHint ().width ());
      fixed_width = true;
#ifdef DEBUG
      DLDdbg << DLD_LIB_PREFIX "input IP\n";
#endif
   }
   else
      setMinimumWidth (sizeHint ().width ());

   setFixedHeight (sizeHint ().height ());
   fixed_height = true;
      
   if(dld_length) {
      setMaxLength (dld_length);
      int w = sizeHint ().width () / 15 * (dld_length+1);
      setMinimumWidth (w);
      setFixedWidth (w);
      fixed_width = true;
   }

   connect( this, SIGNAL(returnPressed(void)),
	    this, SLOT(slotReturn(void)));
#if QT_VERSION < 200
   connect( this, SIGNAL(textChanged(const char *)),
	    this, SLOT(slotTextChanged1(const char *)));
#else
   connect( this, SIGNAL(textChanged(const QString &)),
	    this, SLOT(slotTextChanged2(const QString &)));
#endif

   DLD_Obj::create();
}

void DLD_QTInput::keyPressEvent ( QKeyEvent * e ) 
{
   if (e->key() == Key_Return || e->key() == Key_Enter ) {
      QLineEdit::keyPressEvent(e);
      emit returnPressed();
   }
   else
      QLineEdit::keyPressEvent(e);
}

void DLD_QTInput::focusInEvent ( QFocusEvent * e) {
   setText(pardia->get_env(varname).c_str());
   QLineEdit::focusInEvent(e);
}


DLD_IPInput::
 DLD_IPInput (QWidget * parent, const char *name):
    QValidator (parent, name)
{
}

DLD_IPInput::
~DLD_IPInput ()
{
}

QValidator::State DLD_IPInput::
validate (QString & input, int &)
#if QT_VERSION > 200
const
#endif
{
   QRegExp accept = QRegExp ("^[0-2]?[0-9]?[0-9]?\\.[0-2]?[0-9]?[0-9]?\\.[0-2]?[0-9]?[0-9]?\\.[0-2]?[0-9]?[0-9]?$", FALSE, FALSE);

   QRegExp correct = QRegExp ("^[0-2]?[0-9]?[0-9]\\.[0-2]?[0-9]?[0-9]\\.[0-2]?[0-9]?[0-9]\\.[0-2]?[0-9]?[0-9]$", FALSE, FALSE);

   if(correct.match(input) == 0) {
      int num[4], i;

      if(sscanf(input, "%d.%d.%d.%d", &num[0], &num[1], &num[2], &num[3]) != 4)
	 return QValidator::Invalid;

      for(i=0; i<4; i++) {
	 // DLDerr << "Checking num" << i << "=" << num[i] << endl << flush;
	 if(num[i] < 0 || num[i] > 255) 
	    return QValidator::Invalid;
      }

      return QValidator::Valid;
   }

   if (accept.match(input) == 0) {
      return QValidator::Acceptable;
   }

 return QValidator::Invalid;

}

void DLD_IPInput::
fixup (QString &)
{
}

DLD_NumInput::
 DLD_NumInput (QWidget * parent, const char *name):
    QValidator (parent, name)
{
}

DLD_NumInput::
~DLD_NumInput ()
{
}

QValidator::State DLD_NumInput::
validate (QString & input, int &)
#if QT_VERSION > 200
const
#endif
{
   QRegExp accept = QRegExp ("^[0-9]*$");
   QRegExp correct = QRegExp ("^[0-9]+$");

   if(correct.match(input) == 0)
     return QValidator::Valid;
   
   if (accept.match(input) == 0)
    return QValidator::Acceptable;

 return QValidator::Invalid;

}

void DLD_NumInput::
fixup (QString &)
{
}