Descargar

Caesar Cipher Dialog v1.0 in Qt 4 (página 2)

Enviado por Jaime Montoya


Partes: 1, 2

Select a location and give a name to your project:

edu.red

Click on Next, and after that click on Finish.

Now right click on the project and select Add New…:

edu.red

Select the type of file you want to add, for example C++ Source File for the .cpp files and C++ Header File for the .h files.

edu.red

For this program, you will eventually need to have these files:

edu.red

This is the content of each of these four files:

caesarcipher.pro

HEADERS += caesarcipher.h

SOURCES += main.cpp

caesarcipher.cpp

FORMS +=

caesarcipher.cpp

#include "caesarcipher.h"

#include

#include

#include

#include

#include

#include

CaesarCipher::CaesarCipher()

{

encryptionmode = TRUE; //Have Encryption Mode active when the program starts.

//Allocate layouts.

QVBoxLayout* mainLayout = new QVBoxLayout(this);

QHBoxLayout* topLayout = new QHBoxLayout;

QGridLayout* middleLayout = new QGridLayout;

QHBoxLayout* bottomLayout = new QHBoxLayout;

//Set dialog title

setWindowTitle(tr("Caesar Cipher Dialog v1.0"));

//Build main dialog layout.

mainLayout->addLayout(topLayout); //Place topLayout first at top of dialog.

mainLayout->addStretch(); //Add stretch.

mainLayout->addLayout(middleLayout); //Place middleLayout second in the middle of dialog.

mainLayout->addStretch(); //Add stretch.

mainLayout->addLayout(bottomLayout); //Place bottomLayout third at bottom of dialog.

//Build topLayout.

labelEncryptionMode = new QLabel(tr("Encryption Key")); //Allocate labelEncryptionMode (it was not necessary to initialize it because it was already done in "caesarcipher.h" file. It is on the heap, not on the stack. In case I did not have it on the heap, I should put it on the stack, which means that I should not only allocate but also initialize. In that case, I should have written this line this way: QLabel* labelEncryptionMode = new QLabel(tr("Encryption Key")); // Allocate and initialize topLabel.

spinInputShift = new QSpinBox; //Allocate inpux spin box (it was not necessary to initialize it because it is already on the stack (caesarcipher.h file).

spinInputShift->setMaximum(MAXIMUM_SPINBOX);

topLayout->addWidget(labelEncryptionMode); // Add widgets to top layout.

topLayout->addWidget(spinInputShift);

//Build middleLayout.

labelPlaintext = new QLabel(tr("Plaintext")); //Allocate labelPlaintext.

labelCiphertext = new QLabel(tr("Ciphertext")); //Allocate labelCiphertext (it is already initialized on the heap, in caesarcipher.h file).

lineEditPlaintext = new QLineEdit; //Allocate lineEditPlaintext;

lineEditPlaintext->setMaxLength(MAXIMUM_LENGTH); //The maximum number of input characters is 26, since MAXIMUM_LENGTH = 26.

QRegExp regExp("[a-z]{0,26}"); //Validation rules for lineEditPlainText. Only lower case letters from the alphabet. From 0 to 26 allowed.

lineEditPlaintext->setValidator(new QRegExpValidator(regExp, this));

lineEditCiphertext = new QLineEdit; //Allocate lineEditCiphertxt;

lineEditCiphertext->setMaxLength(MAXIMUM_LENGTH); //The maximum number of input characters is 26, since MAXIMUM_LENGHT = 26.

lineEditCiphertext->setValidator(new QRegExpValidator(regExp, this));

lineEditCiphertext->setReadOnly(true); //Make lineEditCiphertext read only.

middleLayout->addWidget(labelPlaintext, 0, 0); //Place widgets into grid.

middleLayout->addWidget(labelCiphertext, 1, 0);

middleLayout->addWidget(lineEditPlaintext, 0, 1);

middleLayout->addWidget(lineEditCiphertext, 1, 1);

//Buttons.

buttonSwitchMode = new QPushButton(tr("Switch to Decryption Mode")); //Allocate Swith Mode button.

buttonClear = new QPushButton(tr("Clear")); //Allocate Clear button.

buttonQuit = new QPushButton(tr("Quit")); //Allocate Quit button.

buttonQuit->setDefault(true); // Make it default button of dialog.

bottomLayout->addWidget(buttonSwitchMode);

bottomLayout->addStretch(); // Add stretch.

bottomLayout->addWidget(buttonClear);

bottomLayout->addStretch();

bottomLayout->addWidget(buttonQuit);

//Make signal/slot connections.

connect(buttonQuit, SIGNAL(clicked()), this, SLOT(accept())); //Connect buttonQuit clicked to Dialog accept() function.

connect(buttonClear, SIGNAL(clicked()), this, SLOT(clearText())); //Connect buttonClear clicked to Dialog clearText() function.

connect(buttonSwitchMode, SIGNAL(clicked()), this, SLOT(switchMode())); //Connect buttonSwitchMode clicked to Dialog switchMode() function.

connect(lineEditPlaintext, SIGNAL(textChanged(const QString&)), this, SLOT(textChanged(QString))); //Connect lineEditPlaintext to Dialog textChanged() function.

connect(lineEditCiphertext, SIGNAL(textChanged(const QString&)), this, SLOT(textChanged(QString))); //Connect lineEditCiphertext to Dialog textChanged() function.

connect(spinInputShift, SIGNAL(valueChanged(int)), this, SLOT(caesarCipherConversion()));

/* //Testing code. Please ignore it.

QString str = "Hello";

if (str.size() == 5){

lineEditPlaintext->setText("It is 5.");

}else{

lineEditPlaintext->setText("It is not 5.");

}

lineEditPlaintext->setText("12345");

if (lineEditPlaintext->text().size() == 5){

lineEditPlaintext->setText("It is 5.");

}else{

lineEditPlaintext->setText("It is not 5.");

}*/

/*if (lineEditPlaintext->text()[0] == 'j'){

lineEditCiphertext->setText("Congratulations.");

}else{

lineEditCiphertext->setText("Sorry");

}*/

alphabet="abcdefghijklmnopqrstuvwxyz";

/* //Testing code. Please ignore it.

alphabet = alphabet[25];

lineEditCiphertext->setText(alphabet);

*/

}// End CaesarCipher::CaesarCipher().

//Create slots.

void CaesarCipher::textChanged(const QString& value)

{

//lineEditPlaintext->setText(value);

//lineEditCiphertext->setText(value);

caesarCipherConversion();

} //End EchoDialog::textChanged().

void CaesarCipher::clearText(){

lineEditPlaintext->setText("");

lineEditCiphertext->setText("");

} //End CaesarCipher::clearText().

void CaesarCipher::switchMode(){

if(encryptionmode){ //Encryption mode is active.

labelEncryptionMode->setText("Decryption Key");

lineEditPlaintext->setReadOnly(true);

lineEditCiphertext->setReadOnly(false); //Make lineEditCiphertext read only.

buttonSwitchMode->setText("Switch to Encryption Mode");

encryptionmode = FALSE;

//clearText(); //It's possible to call void CaesarCipher::switchMode() function just by typing "clearText();"

caesarCipherConversion();

}

else{ //Decryption mode is active.

labelEncryptionMode->setText("Encryption Key");

lineEditPlaintext->setReadOnly(false); //Make lineEditPlaintext read only.

lineEditCiphertext->setReadOnly(true);

buttonSwitchMode->setText("Switch to Decryption Mode");

encryptionmode = TRUE;

caesarCipherConversion();

}

}

void CaesarCipher::caesarCipherConversion(){

if (encryptionmode == TRUE){

ciphertext = "";

for (int i = 0; i < lineEditPlaintext->text().size(); i++){

int i1 = 0;

while (lineEditPlaintext->text()[i] != alphabet[i1]){

i1 += 1;

}

i1 += spinInputShift->value();

if (i1 > 25){

i1 -= 26;

}

ciphertext[i] = alphabet[i1];

}

lineEditCiphertext->setText(ciphertext);

}else{ //encryptionmode = FALSE. Accordingly, Decryption Mode is active.

plaintext = "";

for (int i = 0; i < lineEditCiphertext->text().size(); i++){

int i1 = 0;

while (lineEditCiphertext->text()[i] != alphabet[i1]){

i1 += 1;

}

i1 -= spinInputShift->value();

if (i1 < 0){

i1 += 26;

}

plaintext[i] = alphabet[i1];

}

lineEditPlaintext->setText(plaintext);

}

}

caesarcipher.h

#ifndef CAESARCIPHER_H

#define CAESARCIPHER_H

#include

class QLabel; //Forward declaration of QLabel class.

class QPushButton; //Forward declaration of QPushButton class.

class QLineEdit; //Forward declaration of QLineEdit class.

class QSpinBox; //Forward declaration of QSpinBox class.

const int MAXIMUM_LENGTH = 26; //Limit maximum number of characters of input.

const int MAXIMUM_SPINBOX = 25; //Limit maximum value for spin box.

class CaesarCipher : public QDialog

{

Q_OBJECT //Required Q_OBJECT macro.

public:

CaesarCipher(); //Default constructor.

bool encryptionmode;

QString alphabet;

QString ciphertext;

QString plaintext;

private:

QLabel* labelEncryptionMode; //Pointer to QLabel to show the current Mode: Encryption Key or Decryption Key.

QSpinBox* spinInputShift; //Pointer to QSpinBox for specifying an integer value that indicates the desired alphabet shift (encryption/decryption key) for the Caesar Cipher.

QLabel* labelPlaintext; //Pointer to QLabel "Plaintext".

QLabel* labelCiphertext; //Pointer to QLabel "Ciphertext".

QLineEdit* lineEditPlaintext; //Pointer to QLineEdit Plaintext.

QLineEdit* lineEditCiphertext; //Pointer to QLineEdit Plaintext.

QPushButton* buttonSwitchMode; //Pointer to QPushButton for switching mode (encryption/decription).

QPushButton* buttonClear; //Pointer to QPushButton to clear QLineEdits.

QPushButton* buttonQuit; //Pointer to QPushButton to clear close the program.

private slots:

void textChanged(const QString&); //Add a private slot to accept changes in user input line edit.

void clearText(); //Add a private slot to process buttonClear.

void switchMode(); //Add a private slot to process buttonSwitchMode.

void caesarCipherConversion(); //Add private slot to process caesarCipherConversion() function, which is the kernel/engine/most important function of the program.

};

#endif

main.cpp

#include

#include "caesarcipher.h"

int main (int argc, char * argv[])

{

QApplication myApp(argc, argv); //Create application.

CaesarCipher myCaesarCipher; //Static allocation of CaesarCipher object.

//Configure to quit application on dialog close.

myCaesarCipher.setAttribute(Qt::WA_QuitOnClose);

myCaesarCipher.show(); //Make dialog visible.

return myApp.exec(); //Execute application.

} //End main().

Program running

edu.red

edu.red

edu.red

edu.red

edu.red

edu.red

edu.red

edu.red

edu.red

 

 

 

 

 

 

 

Autor:

Jaime Montoya

www.jaimemontoya.com

November 29, 2009

Huntsville, Alabama, USA

NOTE: This document, and the entire program are available to download at http://www.jaimemontoya.com/softdesignandengineering/caesarcipher.php

Partes: 1, 2
 Página anterior Volver al principio del trabajoPágina siguiente