Showing posts with label Languages. Show all posts
Showing posts with label Languages. Show all posts

Saturday, September 3, 2011

PyQt: power of Python, beauty of Qt

Qt is very powerful tool for GUI programming. But programming using Qt C++ is not always easy because of unavailability of libraries and difficulty in coding. But Python is powerful and has abundant list of libraries related to virtually anything. Some operations in Python is very easy to use. But the problem of Python is that, it has no GUI its own as handy. PyGTK (Python and GTK) and PyQt (Python and Qt) are the remedy. Here I am explaining how to start with PyQt.
PyQt has no support from QtCreator yet, but the features of QtDesigner can also be used for the PyQt coding. This tutorial explains how to start PyQt programming if you are already aware of Qt C++ programming. This is not just a 'hello world' program, but the code will do something and make you capable of coding your own programs.
In this example, we are building a simple GUI with three LineEdit bars and a single button. When the button is clicked, the numbers entered in the LineEdit a and b are added and the sum is shown in the third LineEdit.


First of all, make GUI widget in Qt Designer and rename the objects a, b, sum and pushButton as shown in screen-shot below. Also make a custom slot named calc_sum(). Save and quit the Designer program.


Now you can see the user interface file named 'widget.ui' (the filename may vary if you changed the widget name while creating your project. Anyway, the filename with .ui as extension is the file we need). We need to create the curresponding python file for user interface. In C++, it is done using uic(User Interface Compiler). In our case, in python, we use a utility named 'pyuic4'. If 'pyuic4' will be automatically installed while you install PyQt. If not, especially in Linux, install it from the repository. Then make the user interface file 'calc_ui.py' (or any name you wish. But for the first time use this name to avoid ambiguity) file using the command:

pyuic4 widget.ui > calc_ui.py

This command will generate a new file named 'cal_ui.py'. This is the user interface file only. We are going to make another file 'calc.py' which will handle the core program. Use a text editor and copy the code below:

import sys
from PyQt4 import QtCore, QtGui

from calc_ui import Ui_Widget


class MyForm(QtGui.QMainWindow):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.ui = Ui_Widget()
        self.ui.setupUi(self)
    def calc_sum(Widget):
    value1=Widget.ui.a.text();
    value2=Widget.ui.b.text();
    result=float(value1)+float(value2);
    str_result=str(result)
    Widget.ui.sum.setText(str_result);


if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    myapp = MyForm()
    myapp.show()
    sys.exit(app.exec_())

Save the file. Then run the file:

pyhon calc.py
I am not so good in explaining the code. I think you got the idea of how to make a custom slot and to make a standalone program. If any reader need clarification or more explanation, I will try to explain further.

Saturday, August 6, 2011

Qt Programming in Malayalam Part-2

Before reading this tutorial, read my previous post "Qt programming in Malayalam" if you have not read it yet.
    Have you ever wonder about easy switching from English to other languages such as French, Spanish etc. in software applications? Even though Malayalam switching is rare, translation is possible in Malayalam as well like any other language. Qt provides an easy way of language switching by means of Qt linguist. This tutorial describe how to build a Malayalam switchable application in Qt. An easy to start tutorial is packaged with Qt Help itself. Here is a quick tutorial of how to build a Malayalam application if you have the source code of the English version. Before we start, I assume:
1) You have Qt installed in your PC.
2) You have a very basic knowledge of Qt programming. How to compile, build and all.
    If the above is not qualified, sorry, it is better to pause here and read this and resume. Suppose you have a Qt application in English (If you have not your own, a lot of examples  are there in qtdir/qt/examples. (qtdir is the directory where you installed Qt). For better understanding, I prefer an application named 'QMusicPlayer' from examples(qtdir/qt/examples/phonon/qmusicplayer). You can take other examples as well. Open qmusicplayer.pro using Qt, then build and run it. You will get a minimal musicplayer in English. Now our aim is to make the malayalam version of the same application.


Now we need to edit two files 'main.cpp' and 'qmusicplayer.pro' as follows:

main.cpp

#include

#include "mainwindow.h"
#include

//![1]
int main(int argv, char **args)
{
    QApplication app(argv, args);
    QTranslator translator;
    translator.load("malayalam");
    app.installTranslator(&translator);

    app.setApplicationName("Music Player");
    app.setQuitOnLastWindowClosed(true);

    MainWindow window;
    window.show();

    return app.exec();
}

qmusicplayer.pro

QT        += phonon

HEADERS   += mainwindow.h
SOURCES   += main.cpp \
             mainwindow.cpp
TRANSLATIONS = malayalam.ts

# install
target.path = $$[QT_INSTALL_EXAMPLES]/phonon/qmusicplayer
sources.files = $$SOURCES $$HEADERS $$FORMS $$RESOURCES *.pro *.png images
sources.path = $$[QT_INSTALL_EXAMPLES]/phonon/qmusicplayer
INSTALLS += target sources

wince*{
DEPLOYMENT_PLUGIN += phonon_ds9 phonon_waveout
}

symbian:TARGET.UID3 = 0xA000CF6A

Changes made in the code are highlighted. The changes in code simply add a Translator feature to your application. While running, your application will seek a file named 'malayalam.qm' which contains one to one translation of all English words used in program to Malayalam. 'malayalam.qm' is a binary file and is to be generated from a file named 'malayalam.ts'. This file is automatically generated using the command in terminal:

lupdate -verbose qmusicplayer.pro

Then the newly generated file - 'malayalam.ts' can be seen in the project directory. But it is an empty file and we need to update that file with all one to one translations. To do so, open the file with Qt linguist:

linguist malayalam.ts

If you couldn't find any command like 'lupdate' or 'linguist', most probably, you have not added the qt directory to the PATH. To do this use the following command Linux,

PATH=$PATH:qtdir/qt/bin/

directory may be different and must change it as your needs. Then use 'lupdate' and 'linguist'. If you are still stucked, check whether you completely installed Qt SDK. In Windows, if you are using Qt enabled prompt, I hope there will be no any problem.

    Now I assume you opened 'malayalam.ts' successfully in Qt Linguist. It shows all english words which translation is enabled. (Refer fig). Click on each english word in 'Source Text' list and add curresponding Malayalam translation in the 'English Translation' edit box at the bottom (highlighted on figure). Repeat the process for all words and save. Then File->Release. It will generate the binary file 'malayalam.qm' in the same directory.
    Now run the qmusicplayer application with file 'malayalam.qm' is placed in the same directory of the executable. Now you can see the malayalam version of 'qmusicplayer'.

In the absense of the file 'malayalam.qm', there will be no run time error in the program, it simply shows the english version. Similarly you can build a multi-lingual swithable version.

Thursday, September 9, 2010

Indian languages support and software tools

Indian Govt. have an official website for Indian language support. You can download language support and software tools from this website or order for a free CD. The major tools are:
  1. True type fonts with keyboard driver
  2. Multifont keyborad engine for the true type fonts
  3. Unicode complient fonts
  4. BHARATEEYA Open Source
  5. Language OCR
  6. Language spell checker
  7. Bilingual dictionary
  8. Font design tool
  9. Transliteration tool
  10. Language text editor
  11. Tutor package
  12. Text to speech system
  13. Database sorting tool
  14. Number to word tool
  15. Typing tutor
  16. Language suppoted microsoft excel
  17. Type assistant
The CD is released by Ministry of Communications and Information Technology, Government of India. Contibuters are, C-DAC, Cyber space multimedia Ltd.,C K Technologies, Cadgraf Digitals System Ltd., Cyber Thomson, IMRC.
Click here to visit Technology Development for Indian Languages(TDIL).

Saturday, August 28, 2010

Qt Programming in Malayalam Part-1


Qt Programming in Malayalam is quite easy. Most of the strings used in Qt is of type QString. QString support all kind of unicode standards. So one can include mathematica and technical symbols, and non-English characters. Malayalam characters are also included in unicode. So usage of Malayalam in Qt is possible almost anywhere in Qt software designing. There are three major standards for unicode- UTF8-8bit, UTF-16-16bit, UTF-32-32bit. QString supports all standards and there's a bunch of translating functions to and fro for transforming QString to other types such as const char*.

You can transform simple programs in Qt to Malayalam with a little care. But still there may be problems while playing with non-English languages. Like other GUI's Qt is also limited in support to non-English languages, but still it is translatable.
Download this example (Windows binary - 4.76MB)
Download the source code (Tested in both Linux and Windows - 3.27KB)

Also read  Qt Programming in Malayalam Part-2

Friday, August 27, 2010

Malayalam for Windows

*Note: The above information is adapted from the resources of http://malayalam.web4all.in
The information in this post is specific to Malayalam users in Kerala, India. Other readers may not find anything useful in this post
"I need to do some work in Malayalam in Windows XP. What should I need to do?''
My post here tries to find the answer for the above question. Malayalam implementation can be done in two ways.
1)In-script keyboard - Each corrector in Malayalam is assigned to the key layout in the US keyboard. It become easier only after some practice2)Transliteration - This is easier method for beginners. In this method, you just type a word similar to the Malayalam word. (For example അമ്മ in Malayalam can be printed on screen by typing 'amma' in English.

Setting up Windows XP for In-script keyboard
Step1: Go to Control Panel->Regional and Language Options
Click on 'Language' tab
Step2: Check whether the check-box corresponding to "Install files for complex script and right to left languages(including Thai).
If not check it. If it is already checked, go to Step5.Step3: Then Computer ask your Windows CD for further installation of fonts. Then put your CD on the drive and click OK. Wait until complete installation. Then reboot your system after closing all dialog boxes.
Step4: Again do Step1 and this time the check-box will be checked.
Step5: Click 'Details' button. Then click 'Add' on the new dialog box. Choose Malayalam(India) and click OK.
Step6: Close all dialog boxes. In your task bar, you can see the current layout as 'EN" by default. Click on it and choose Malayalam from the list. Then 'EN' is changed to 'MY'. Now Malayalam keyboard is activated. and you can start type in Malayalam.
Open Wordpad (or any other text editor) from Start menu and start typing in Malayalam.

Setting up Windows XP for Transliteration
Step1: Download Mozhi Keyman from this link. Install it.
Step2: Just after installation, Mozhi Keyman icon will be visible on your task bar. Click on it and choose 'Mozhi Keymap' from the following list and you are done.
Open a text editor- I recommend Wordpad and type 'amma' and press enter. You can see the difference now.

You will get more information and support from
http://smc.org.in
http://malayalam.web4all.in