| 1 | /****************************************************************************
|
|---|
| 2 | ** $Id: lineedits.cpp 2 2005-11-16 15:49:26Z dmik $
|
|---|
| 3 | **
|
|---|
| 4 | ** Copyright (C) 1992-2003 Trolltech AS. All rights reserved.
|
|---|
| 5 | **
|
|---|
| 6 | ** This file is part of an example program for Qt. This example
|
|---|
| 7 | ** program may be used, distributed and modified without limitation.
|
|---|
| 8 | **
|
|---|
| 9 | *****************************************************************************/
|
|---|
| 10 |
|
|---|
| 11 | #include "lineedits.h"
|
|---|
| 12 |
|
|---|
| 13 | #include <qlineedit.h>
|
|---|
| 14 | #include <qcombobox.h>
|
|---|
| 15 | #include <qframe.h>
|
|---|
| 16 | #include <qvalidator.h>
|
|---|
| 17 | #include <qlabel.h>
|
|---|
| 18 | #include <qlayout.h>
|
|---|
| 19 | #include <qhbox.h>
|
|---|
| 20 |
|
|---|
| 21 | /*
|
|---|
| 22 | * Constructor
|
|---|
| 23 | *
|
|---|
| 24 | * Creates child widgets of the LineEdits widget
|
|---|
| 25 | */
|
|---|
| 26 |
|
|---|
| 27 | LineEdits::LineEdits( QWidget *parent, const char *name )
|
|---|
| 28 | : QGroupBox( 0, Horizontal, "Line edits", parent, name )
|
|---|
| 29 | {
|
|---|
| 30 | setMargin( 10 );
|
|---|
| 31 |
|
|---|
| 32 | QVBoxLayout* box = new QVBoxLayout( layout() );
|
|---|
| 33 |
|
|---|
| 34 | QHBoxLayout *row1 = new QHBoxLayout( box );
|
|---|
| 35 | row1->setMargin( 5 );
|
|---|
| 36 |
|
|---|
| 37 | // Create a Label
|
|---|
| 38 | QLabel* label = new QLabel( "Echo Mode: ", this);
|
|---|
| 39 | row1->addWidget( label );
|
|---|
| 40 |
|
|---|
| 41 | // Create a Combobox with three items...
|
|---|
| 42 | combo1 = new QComboBox( FALSE, this );
|
|---|
| 43 | row1->addWidget( combo1 );
|
|---|
| 44 | combo1->insertItem( "Normal" );
|
|---|
| 45 | combo1->insertItem( "Password" );
|
|---|
| 46 | combo1->insertItem( "No Echo" );
|
|---|
| 47 | // ...and connect the activated() SIGNAL with the slotEchoChanged() SLOT to be able
|
|---|
| 48 | // to react when an item is selected
|
|---|
| 49 | connect( combo1, SIGNAL( activated( int ) ), this, SLOT( slotEchoChanged( int ) ) );
|
|---|
| 50 |
|
|---|
| 51 | // insert the first LineEdit
|
|---|
| 52 | lined1 = new QLineEdit( this );
|
|---|
| 53 | box->addWidget( lined1 );
|
|---|
| 54 |
|
|---|
| 55 | // another widget which is used for layouting
|
|---|
| 56 | QHBoxLayout *row2 = new QHBoxLayout( box );
|
|---|
| 57 | row2->setMargin( 5 );
|
|---|
| 58 |
|
|---|
| 59 | // and the second label
|
|---|
| 60 | label = new QLabel( "Validator: ", this );
|
|---|
| 61 | row2->addWidget( label );
|
|---|
| 62 |
|
|---|
| 63 | // A second Combobox with again three items...
|
|---|
| 64 | combo2 = new QComboBox( FALSE, this );
|
|---|
| 65 | row2->addWidget( combo2 );
|
|---|
| 66 | combo2->insertItem( "No Validator" );
|
|---|
| 67 | combo2->insertItem( "Integer Validator" );
|
|---|
| 68 | combo2->insertItem( "Double Validator" );
|
|---|
| 69 | // ...and again the activated() SIGNAL gets connected with a SLOT
|
|---|
| 70 | connect( combo2, SIGNAL( activated( int ) ), this, SLOT( slotValidatorChanged( int ) ) );
|
|---|
| 71 |
|
|---|
| 72 | // and the second LineEdit
|
|---|
| 73 | lined2 = new QLineEdit( this );
|
|---|
| 74 | box->addWidget( lined2 );
|
|---|
| 75 |
|
|---|
| 76 | // yet another widget which is used for layouting
|
|---|
| 77 | QHBoxLayout *row3 = new QHBoxLayout( box );
|
|---|
| 78 | row3->setMargin( 5 );
|
|---|
| 79 |
|
|---|
| 80 | // we need a label for this too
|
|---|
| 81 | label = new QLabel( "Alignment: ", this );
|
|---|
| 82 | row3->addWidget( label );
|
|---|
| 83 |
|
|---|
| 84 | // A combo box for setting alignment
|
|---|
| 85 | combo3 = new QComboBox( FALSE, this );
|
|---|
| 86 | row3->addWidget( combo3 );
|
|---|
| 87 | combo3->insertItem( "Left" );
|
|---|
| 88 | combo3->insertItem( "Centered" );
|
|---|
| 89 | combo3->insertItem( "Right" );
|
|---|
| 90 | // ...and again the activated() SIGNAL gets connected with a SLOT
|
|---|
| 91 | connect( combo3, SIGNAL( activated( int ) ), this, SLOT( slotAlignmentChanged( int ) ) );
|
|---|
| 92 |
|
|---|
| 93 | // and the third lineedit
|
|---|
| 94 | lined3 = new QLineEdit( this );
|
|---|
| 95 | box->addWidget( lined3 );
|
|---|
| 96 |
|
|---|
| 97 | // exactly the same for the fourth
|
|---|
| 98 | QHBoxLayout *row4 = new QHBoxLayout( box );
|
|---|
| 99 | row4->setMargin( 5 );
|
|---|
| 100 |
|
|---|
| 101 | // we need a label for this too
|
|---|
| 102 | label = new QLabel( "Input mask: ", this );
|
|---|
| 103 | row4->addWidget( label );
|
|---|
| 104 |
|
|---|
| 105 | // A combo box for choosing an input mask
|
|---|
| 106 | combo4 = new QComboBox( FALSE, this );
|
|---|
| 107 | row4->addWidget( combo4 );
|
|---|
| 108 | combo4->insertItem( "No mask" );
|
|---|
| 109 | combo4->insertItem( "Phone number" );
|
|---|
| 110 | combo4->insertItem( "ISO date" );
|
|---|
| 111 | combo4->insertItem( "License key" );
|
|---|
| 112 |
|
|---|
| 113 | // ...this time we use the activated( const QString & ) signal
|
|---|
| 114 | connect( combo4, SIGNAL( activated( int ) ),
|
|---|
| 115 | this, SLOT( slotInputMaskChanged( int ) ) );
|
|---|
| 116 |
|
|---|
| 117 | // and the fourth lineedit
|
|---|
| 118 | lined4 = new QLineEdit( this );
|
|---|
| 119 | box->addWidget( lined4 );
|
|---|
| 120 |
|
|---|
| 121 | // last widget used for layouting
|
|---|
| 122 | QHBox *row5 = new QHBox( this );
|
|---|
| 123 | box->addWidget( row5 );
|
|---|
| 124 | row5->setMargin( 5 );
|
|---|
| 125 |
|
|---|
| 126 | // last label
|
|---|
| 127 | (void)new QLabel( "Read-Only: ", row5 );
|
|---|
| 128 |
|
|---|
| 129 | // A combo box for setting alignment
|
|---|
| 130 | combo5 = new QComboBox( FALSE, row5 );
|
|---|
| 131 | combo5->insertItem( "False" );
|
|---|
| 132 | combo5->insertItem( "True" );
|
|---|
| 133 | // ...and again the activated() SIGNAL gets connected with a SLOT
|
|---|
| 134 | connect( combo5, SIGNAL( activated( int ) ), this, SLOT( slotReadOnlyChanged( int ) ) );
|
|---|
| 135 |
|
|---|
| 136 | // and the last lineedit
|
|---|
| 137 | lined5 = new QLineEdit( this );
|
|---|
| 138 | box->addWidget( lined5 );
|
|---|
| 139 |
|
|---|
| 140 | // give the first LineEdit the focus at the beginning
|
|---|
| 141 | lined1->setFocus();
|
|---|
| 142 | }
|
|---|
| 143 |
|
|---|
| 144 | /*
|
|---|
| 145 | * SLOT slotEchoChanged( int i )
|
|---|
| 146 | *
|
|---|
| 147 | * i contains the number of the item which the user has been chosen in the
|
|---|
| 148 | * first Combobox. According to this value, we set the Echo-Mode for the
|
|---|
| 149 | * first LineEdit.
|
|---|
| 150 | */
|
|---|
| 151 |
|
|---|
| 152 | void LineEdits::slotEchoChanged( int i )
|
|---|
| 153 | {
|
|---|
| 154 | switch ( i ) {
|
|---|
| 155 | case 0:
|
|---|
| 156 | lined1->setEchoMode( QLineEdit::Normal );
|
|---|
| 157 | break;
|
|---|
| 158 | case 1:
|
|---|
| 159 | lined1->setEchoMode( QLineEdit::Password );
|
|---|
| 160 | break;
|
|---|
| 161 | case 2:
|
|---|
| 162 | lined1->setEchoMode( QLineEdit::NoEcho );
|
|---|
| 163 | break;
|
|---|
| 164 | }
|
|---|
| 165 |
|
|---|
| 166 | lined1->setFocus();
|
|---|
| 167 | }
|
|---|
| 168 |
|
|---|
| 169 | /*
|
|---|
| 170 | * SLOT slotValidatorChanged( int i )
|
|---|
| 171 | *
|
|---|
| 172 | * i contains the number of the item which the user has been chosen in the
|
|---|
| 173 | * second Combobox. According to this value, we set a validator for the
|
|---|
| 174 | * second LineEdit. A validator checks in a LineEdit each character which
|
|---|
| 175 | * the user enters and accepts it if it is valid, else the character gets
|
|---|
| 176 | * ignored and not inserted into the lineedit.
|
|---|
| 177 | */
|
|---|
| 178 |
|
|---|
| 179 | void LineEdits::slotValidatorChanged( int i )
|
|---|
| 180 | {
|
|---|
| 181 | switch ( i ) {
|
|---|
| 182 | case 0:
|
|---|
| 183 | lined2->setValidator( 0 );
|
|---|
| 184 | break;
|
|---|
| 185 | case 1:
|
|---|
| 186 | lined2->setValidator( new QIntValidator( lined2 ) );
|
|---|
| 187 | break;
|
|---|
| 188 | case 2:
|
|---|
| 189 | lined2->setValidator( new QDoubleValidator( -999.0, 999.0, 2,
|
|---|
| 190 | lined2 ) );
|
|---|
| 191 | break;
|
|---|
| 192 | }
|
|---|
| 193 |
|
|---|
| 194 | lined2->setText( "" );
|
|---|
| 195 | lined2->setFocus();
|
|---|
| 196 | }
|
|---|
| 197 |
|
|---|
| 198 |
|
|---|
| 199 | /*
|
|---|
| 200 | * SLOT slotAlignmentChanged( int i )
|
|---|
| 201 | *
|
|---|
| 202 | * i contains the number of the item which the user has been chosen in
|
|---|
| 203 | * the third Combobox. According to this value, we set an alignment
|
|---|
| 204 | * third LineEdit.
|
|---|
| 205 | */
|
|---|
| 206 |
|
|---|
| 207 | void LineEdits::slotAlignmentChanged( int i )
|
|---|
| 208 | {
|
|---|
| 209 | switch ( i ) {
|
|---|
| 210 | case 0:
|
|---|
| 211 | lined3->setAlignment( QLineEdit::AlignLeft );
|
|---|
| 212 | break;
|
|---|
| 213 | case 1:
|
|---|
| 214 | lined3->setAlignment( QLineEdit::AlignCenter );
|
|---|
| 215 | break;
|
|---|
| 216 | case 2:
|
|---|
| 217 | lined3->setAlignment( QLineEdit::AlignRight );
|
|---|
| 218 | break;
|
|---|
| 219 | }
|
|---|
| 220 |
|
|---|
| 221 | lined3->setFocus();
|
|---|
| 222 | }
|
|---|
| 223 |
|
|---|
| 224 | /*
|
|---|
| 225 | * SLOT slotInputMaskChanged( const QString &mask )
|
|---|
| 226 | *
|
|---|
| 227 | * i contains the number of the item which the user has been chosen in
|
|---|
| 228 | * the third Combobox. According to this value, we set an input mask on
|
|---|
| 229 | * third LineEdit.
|
|---|
| 230 | */
|
|---|
| 231 |
|
|---|
| 232 | void LineEdits::slotInputMaskChanged( int i )
|
|---|
| 233 | {
|
|---|
| 234 | switch( i ) {
|
|---|
| 235 | case 0:
|
|---|
| 236 | lined4->setInputMask( QString::null );
|
|---|
| 237 | break;
|
|---|
| 238 | case 1:
|
|---|
| 239 | lined4->setInputMask( "+99 99 99 99 99;_" );
|
|---|
| 240 | break;
|
|---|
| 241 | case 2:
|
|---|
| 242 | lined4->setInputMask( "0000-00-00" );
|
|---|
| 243 | lined4->setText( "00000000" );
|
|---|
| 244 | lined4->setCursorPosition( 0 );
|
|---|
| 245 | break;
|
|---|
| 246 | case 3:
|
|---|
| 247 | lined4->setInputMask( ">AAAAA-AAAAA-AAAAA-AAAAA-AAAAA;#" );
|
|---|
| 248 | break;
|
|---|
| 249 | }
|
|---|
| 250 | lined4->setFocus();
|
|---|
| 251 | }
|
|---|
| 252 |
|
|---|
| 253 | /*
|
|---|
| 254 | * SLOT slotReadOnlyChanged( int i )
|
|---|
| 255 | *
|
|---|
| 256 | * i contains the number of the item which the user has been chosen in
|
|---|
| 257 | * the fourth Combobox. According to this value, we toggle read-only.
|
|---|
| 258 | */
|
|---|
| 259 |
|
|---|
| 260 | void LineEdits::slotReadOnlyChanged( int i )
|
|---|
| 261 | {
|
|---|
| 262 | switch ( i ) {
|
|---|
| 263 | case 0:
|
|---|
| 264 | lined5->setReadOnly( FALSE );
|
|---|
| 265 | break;
|
|---|
| 266 | case 1:
|
|---|
| 267 | lined5->setReadOnly( TRUE );
|
|---|
| 268 | break;
|
|---|
| 269 | }
|
|---|
| 270 |
|
|---|
| 271 | lined5->setFocus();
|
|---|
| 272 | }
|
|---|
| 273 |
|
|---|