| 1 | /**************************************************************************** | 
|---|
| 2 | ** $Id: rot13.cpp 2 2005-11-16 15:49:26Z dmik $ | 
|---|
| 3 | ** | 
|---|
| 4 | ** Copyright (C) 1992-2000 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 "rot13.h" | 
|---|
| 12 |  | 
|---|
| 13 | #include <qmultilineedit.h> | 
|---|
| 14 | #include <qpushbutton.h> | 
|---|
| 15 | #include <qapplication.h> | 
|---|
| 16 | #include <qlayout.h> | 
|---|
| 17 |  | 
|---|
| 18 | Rot13::Rot13() | 
|---|
| 19 | { | 
|---|
| 20 | left = new QMultiLineEdit( this, "left" ); | 
|---|
| 21 | right = new QMultiLineEdit( this, "right" ); | 
|---|
| 22 | connect( left, SIGNAL(textChanged()), this, SLOT(changeRight()) ); | 
|---|
| 23 | connect( right, SIGNAL(textChanged()), this, SLOT(changeLeft()) ); | 
|---|
| 24 |  | 
|---|
| 25 | QPushButton * quit = new QPushButton( "&Quit", this ); | 
|---|
| 26 | quit->setFocusPolicy( NoFocus ); | 
|---|
| 27 | connect( quit, SIGNAL(clicked()), qApp, SLOT(quit()) ); | 
|---|
| 28 |  | 
|---|
| 29 | QGridLayout * l = new QGridLayout( this, 2, 2, 5 ); | 
|---|
| 30 | l->addWidget( left, 0, 0 ); | 
|---|
| 31 | l->addWidget( right, 0, 1 ); | 
|---|
| 32 | l->addWidget( quit, 1, 1, AlignRight ); | 
|---|
| 33 |  | 
|---|
| 34 | left->setFocus(); | 
|---|
| 35 | } | 
|---|
| 36 |  | 
|---|
| 37 |  | 
|---|
| 38 | void Rot13::changeLeft() | 
|---|
| 39 | { | 
|---|
| 40 | left->blockSignals( TRUE ); | 
|---|
| 41 | left->setText( rot13( right->text() ) ); | 
|---|
| 42 | left->blockSignals( FALSE ); | 
|---|
| 43 | } | 
|---|
| 44 |  | 
|---|
| 45 |  | 
|---|
| 46 | void Rot13::changeRight() | 
|---|
| 47 | { | 
|---|
| 48 | right->blockSignals( TRUE ); | 
|---|
| 49 | right->setText( rot13( left->text() ) ); | 
|---|
| 50 | right->blockSignals( FALSE ); | 
|---|
| 51 | } | 
|---|
| 52 |  | 
|---|
| 53 |  | 
|---|
| 54 | QString Rot13::rot13( const QString & input ) const | 
|---|
| 55 | { | 
|---|
| 56 | QString r = input; | 
|---|
| 57 | int i = r.length(); | 
|---|
| 58 | while( i-- ) { | 
|---|
| 59 | if ( r[i] >= QChar('A') && r[i] <= QChar('M') || | 
|---|
| 60 | r[i] >= QChar('a') && r[i] <= QChar('m') ) | 
|---|
| 61 | r[i] = (char)((int)QChar(r[i]) + 13); | 
|---|
| 62 | else if  ( r[i] >= QChar('N') && r[i] <= QChar('Z') || | 
|---|
| 63 | r[i] >= QChar('n') && r[i] <= QChar('z') ) | 
|---|
| 64 | r[i] = (char)((int)QChar(r[i]) - 13); | 
|---|
| 65 | } | 
|---|
| 66 | return r; | 
|---|
| 67 | } | 
|---|
| 68 |  | 
|---|
| 69 |  | 
|---|
| 70 | int main( int argc, char ** argv ) | 
|---|
| 71 | { | 
|---|
| 72 | QApplication a( argc, argv ); | 
|---|
| 73 | Rot13 r; | 
|---|
| 74 | r.resize( 400, 400 ); | 
|---|
| 75 | a.setMainWidget( &r ); | 
|---|
| 76 | r.setCaption("Qt Example - ROT13"); | 
|---|
| 77 | r.show(); | 
|---|
| 78 | return a.exec(); | 
|---|
| 79 | } | 
|---|