Monday, February 4, 2008

Uyghur Input Text Box Linux C++ code

After so many years of MFC programming on Windows, I tried my first C++ GUI code on Linux. I installed the Qt4.3 on OpenSuse 10.3 and tested this (classic) Uyghur Input Text Box.

The Textbox works fine. It can map the keyboard input to the user defined strings. User can map the key press event to any string, so that this code can be modified to input any language. It is a free code, withoot fancy features. It is tested on OpenSuse 10.3 with Qt4.3. Please enjoy.

-------------------------
File 1: main.cpp
------------------------
/*
* This is a free software. Please feel free to modify
* and improve it.
*
* UTextEdit class inherits the QTextEdit class and changes the
* key press event.
*
*
* compile:
* qmake -project
* qmake
* make
*
* Tested on OpenSuse10.3 with Qt4.3
*
* Anwar Mamat anwar@cse.unl.edu
* Feb 4,2008
*/

#include "interface.h"

int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MyWidget uEdit;

uEdit.show();

return app.exec();
}


--------------------------
file 2: utextedit.h
--------------------------
/*
* UTextEdit Class
* This class inherits the QTextEdit class, intercepts the
* keypress events, and changes the keypress events based
* on the user predefined mapping.
*
* This is a free code. Please feel free to modify it.
*
* Anwar Mamat anwar@cse.unl.edu
* Feb 4,2008
*/

#ifndef UTEXTEDIT_H
#define UTEXTEDIT_H

#include "QtCore/QVariant"
#include "QtGui/QApplication"
#include "QtGui/QTextEdit"
#include "QtGui/QWidget"
#include "QKeyEvent"

class UTextEdit : public QTextEdit
{
Q_OBJECT
public:
UTextEdit(QWidget *parent = 0);

protected:
void keyPressEvent(QKeyEvent *e);

};

#endif //UTEXTEDIT_H

File3: utextedit.cpp

#include "utextedit.h"

UTextEdit::UTextEdit(QWidget *parent)

: QTextEdit(parent)

{

}

void UTextEdit::keyPressEvent(QKeyEvent *e)

{

QString Latin = {"abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ/?.,:;"};

char Uyghur[][5] = {

"ھ",//a

"ب",//b

"غ",//c

"د",//d

"ې",//e

"ا",//f

"ە",//g

"ى",//h

"ڭ",//i

"ق",//j

"ك",//k

"ل",//l

"م",//m

"ن",//n

"و",//o

"پ",//p

"چ",//q

"ر",//r

"س",//s

"ت",//t

"ۇ",//u

"ۈ",//v

"ۋ",//w

"ش",//x

"ي",//y

"ز",//z

" ",// space

"",// A

"",// B

"",// C

"ژ",// D

"",//E

"ف",//F

"گ",//G

"خ",//H

"",//I

"ج",//J

"ۆ",//K

"لا",//L

"",//M

"",//N

"",//O

"",//P

"",//Q

"",//R

"",//S

"",//T

"",//U

"",//V

"",//W

"",//X

"",//Y

"",//Z

"ئ", // /

"؟",//?

".",//.

"،",//,

":",//:

"؛",//;

};

//Do something here.

switch (e->key()) {

case Qt::Key_Enter:

case Qt::Key_Return:

case Qt::Key_Escape:

case Qt::Key_Tab:

case Qt::Key_Backtab:

e->ignore();

return;

}

unsigned int iPos = Latin.indexOf(e->text()) ;

//Intercept the predefined keyevents

if(e->key() < style="color: blue;">sizeof(Uyghur) / 5 )

{

QKeyEvent ker(QKeyEvent::KeyPress, Qt::Key_0, 0, QObject::trUtf8(Uyghur[iPos]), 0);

QTextEdit::keyPressEvent(&ker);

}

else //other keys

QTextEdit::keyPressEvent(e);

}

----------------------

file 4: interface.4

#ifndef INTERFACE_H

#define INTERFACE_H

#include "QObject"

#include "QtGui/QApplication"

#include "QtGui/QWidget"

#include "QMessageBox"

#include "QPushButton"

class MyWidget : public QWidget

{

Q_OBJECT

public:

MyWidget(QWidget *parent = 0);

public slots:

void about();

};

#endif //INTERFACE_H

--------------------------

file 5: interface.cpp

--------------------------

#include "interface.h"

#include "utextedit.h"

void MyWidget::about()

{

QMessageBox::about(this, "UTextEdit","A simple Uyghur Input Program.");

}

MyWidget::MyWidget(QWidget *parent )

: QWidget(parent)

{

setFixedSize(640, 240);

QPushButton *quit = new QPushButton(tr("Quit"), this);

QPushButton *about = new QPushButton(tr("About"), this);

UTextEdit *txtBox = new UTextEdit(this);

quit->setGeometry(480, 200, 60, 30);

quit->setFont(QFont("Times", 14, QFont::Bold));

about->setGeometry(540, 200, 80, 30);

about->setFont(QFont("Times", 14, QFont::Bold));

txtBox->setGeometry(10,10,620,180);

txtBox->setFont(QFont("UKIJ Tuz", 24, QFont::Bold));

txtBox->setAlignment(Qt::AlignRight);

connect(quit, SIGNAL(clicked()), qApp, SLOT(quit()));

connect(about, SIGNAL(clicked()), this, SLOT(about(void)));

}


No comments: