knoda, or more exactly the hk_classes library, has an integrated python interpreter. For further information about python see http://www.python.org. This tutorial will not teach python, it will only show the hk_classes/python interface. This tutorial is by no means complete, it only exists now because the final documentation hasn't been written yet.
The examples in this tutorial are based on the tables as described in the knoda tutorial.
Each object in a form has its own programmable actions. General actions are
script that will be executed when a mouse click on this object occurs
script that will be executed when a mouse doubleclick on this object occurs
script that will be executed when the form is created or put into viewmode
script that will be executed when the form is closed or put into designmode
script that will be executed when this object gets the keyboard focus
script that will be executed when this object looses the keyboard focus
script that will be executed when this object has the keyboard focus and a key on the keyboard is pressed
The global variable hk_this is set from hk_classes and represents the current object, in this case the button. show_warningmessage() is a method of this object, that allows you to display a string in a GUI independent way (as knoda is a KDE application it will be displayed as a KDE window but if you start it within a command line program it will be displayed on the standard output).
Important: Other GUI-independent ways for user interaction are
- show_warningmessage(message)
displays 'message'
- bool show_yesnodialog(message, bool default_value)
displays 'message' and returns true if you answer 'yes'. The parameter 'default_value' is used for conevience to preselect 'yes' or 'no' in dialog boxes
- hk_string show_stringvaluedialog(text)
displays 'text' as a question and returns your response
Each visible object of hk_classes is inherited from a class called hk_visible, so we will now have a look on the general available methods
Figure 2-1. Geometry specific methods
- set_size(x, y,width,height)
lets you to set position and size of an object
- set_size(width,height)
lets you to set size of an object
- set_position(x,y)
lets you to set position of an object
- set_x(x)
lets you to set the horizontal position of an object
- set_y(y)
lets you to set the vertical position of an object
- set_width(width)
- set_height(height)
- x()
returns the x co-ordinate of the object
- y()
returns the y co-ordinate of the object
- width()
returns the width of the object
- height()
returns the height of the object
Figure 2-2. Look and Feel methods
- set_font(fontname,size)
sets the font, e.g. set_font("Arial",12)
- set_font(font)
- hk_font font()
returns a font object
- set_foregroundcolour(colour)
- foregroundcolour()
- set_backgroundcolour(colour)
- hk_colour backgroundcolour()
Figure 2-3. Miscelleanous methods
- set_label(labeltext)
- label()
- enum enum_visibletype {textlabel,button,rowselector,boolean,lineedit,memo,combobox,grid,form,report,reportsection,reportdata,other}
- enum_visibletype type(void)
- identifier()
- hk_presentation* presentation()
returns the parent presentation object (either a form or a report)
Example 2-1. Changing colour and position
redcolour =hk_colour(255,0,0) greencolour =hk_colour(0,255,0) if hk_this.foregroundcolour().red()!=255: hk_this.set_foregroundcolour(redcolour) hk_this.set_backgroundcolour(greencolour) hk_this.set_label("green button") else: hk_this.set_foregroundcolour(greencolour) hk_this.set_backgroundcolour(redcolour) hk_this.set_label("red button") hk_this.set_position(hk_this.x()+50,hk_this.y()+10)
Before we start writing programs, we will first have some theory. Below you see the (c++)- structure of hk_classes. There are classes that handle the contact to the database (see the left side of the graphic), while others handle the interaction with the user (we have already seen the hk_visible class in the previous section).
All data-sensitive objects are of type hk_dsvisible (or its child hk_dsdatavisible). hk_dsvisible inherits from hk_visible. The most important method of hk_dsvisible is datasource(). This method returns a class of type hk_datasource, representing the whole datasource (either a table or a query)
Figure 3-3. hk_datasource methods
- name()
returns the name of the datasource
- goto_row(rownumber)
moves the row selector (row cursor) to 'rownumber'
- goto_first()
- goto_last()
- goto_next()
- goto_previous()
- row_position()
returns the row number of the current row
- max_rows()
returns the total number of existing rows
- enable()
- disable()
- set_enabled(e)
- is_enabled()
- hk_column*column_by_name(name)
returns an hk_column object of the column with the name 'name'
- store_changed_data()
- hk_database*database()
For the next example, create a form, set a datasource, create a lineedit field and connect it to a column of the datasource (for details see the knoda tutorial at http://hk-classes.sourceforge.net/tutorials.
A lineeditfield is of type hk_dsdatavisible. hk_dsdatavisible inherits from hk_dsvisible. The most important method of hk_dsdatavisible is column(). This method returns a class of type hk_column, representing a specific column of a table.
Figure 3-4. hk_column data methods
- set_asstring(value)
lets you set a new value for this object
- asstring
returns the current value as a string value
- set_asdouble(value)
lets you set a new value for this object
- asdouble()
returns the current value as a double value
- set_asinteger(value)
lets you set a new value for this object
- asinteger()
returns the current value as a integer value
- is_readonly()
returns true if this column is read-only; if data can be changed it returns false
- unsigned int find(from_rownumber,to_rownumber,searchtext,bool wholephrase=false,bool casesensitive=false,bool backwards=false)
searches for a specific value in a column, returns the row number if found, hk_datasource.max_rows()+1 if not found
- unsigned int find(searchtext,bool wholephrase=false,bool casesensitive=false,bool backwards=false)
searches for a specific value in a column, returns the row number if found, hk_datasource.max_rows()+1 if not found. This version searches all rows of a datasource.
- hk_datasource* datasource()
Figure 3-5. hk_column type methods
- hk_string name(void)
- void set_name(const hk_string& n)
sets the column name
- enum_columntype {textcolumn,auto_inccolumn,smallintegercolumn,integercolumn,smallfloatingcolumn, floatingcolumn,datecolumn,datetimecolumn,timecolumn,timestampcolumn,binarycolumn, memocolumn,boolcolumn,othercolumn}
- columntype()
returns the type of the column
- size()
returns the column size (e.g. if this column was created as CHAR(10) it will return 10)
- bool is_primary(void)
returns true if this column is part of a primary key
- bool set_primary(bool i)
- bool is_notnull(void)
- bool set_notnull(bool i)
if true the column needs a value
Example 3-3. Read data
col=hk_this.datasource().column_by_name("name") hk_this.show_warningmessage(col.asstring())
Example 3-4. Write data
col=hk_this.datasource().column_by_name("name") col.set_asstring("my new value")
the row position changes (e.g. by calling hk_datasource.goto_row())
the datasource is disabled (e.g. by calling hk_datasource.disable())
the changes are manually stored by calling hk_datasource.store_changed_data()
Example 3-5. Search data
col=hk_this.datasource().column_by_name("name") result=col.find("Schiller") if result > hk_this.datasource().max_rows(): hk_this.show_warningmessage("value not found") else: hk_this.show_warningmessage("Value found at row position: "+str(result))
Figure 3-6. hk_dsdatavisible methods
- set_value(newvalue)
sets the current value,where 'value' is a string. If a column is set, the datasource will be changed, if not it will be only displayed
- value()
returns the displayed string (the current value)
- find(from_rownumber,to_rownumber,searchtext[,wholephrase[,casesensitive[,backwards]]])
searches for a specific value in a column, returns the row number if found, hk_datasource.max_rows()+1 if not found
- find(searchtext[,wholephrase[,casesensitive[,backwards]]])
searches for a specific value in a column, returns the row number if found, hk_datasource.max_rows()+1 if not found. This version searches all rows of a datasource.
Both forms and reports inherit from hk_presentation. The most import function of hk_presentation is set_mode(mode). The two possible modes are hk_presentation.designmode and hk_presentation.viewmode
If you want to get a reference to a specific object in a form you have two possibilities
hk_visible* get_visible(unique_number)
hk_visible* get_visible(const hk_string& identifier)
Important: To find this number, click on the object. In the caption of the property editor you can see the number in brackets.
Both functions return a reference of type hk_visible. To change it to the type you need there are some type casting functions
Figure 3-7. Type casting
hk_button *cast_button(hk_visible*);
hk_dslineedit *cast_dslineedit(hk_visible*);
hk_dsmemo *cast_dsmemo(hk_visible*);
hk_dsgrid *cast_dsgrid(hk_visible*);
hk_dscombobox *cast_dscombobox(hk_visible*);
hk_dsboolean *cast_dsboolean(hk_visible*);
hk_dsvisible *cast_dsvisible(hk_visible*);
hk_form *cast_form(hk_visible*);
hk_report *cast_report(hk_visible*);
The next program shows you how to start a form:
Here is how to start a report:
Example 3-10. displaying a report
myreport=hk_this.datasource().database().new_reportvisible() myreport.load_report("complexreport") myreport.set_mode(myreport.viewmode)
A visible object in a report is of type hk_reportdata, which inherits from hk_dsdatavisible. The main methods are
set_data(const hk_string& d)
hk_string data(void)
The following example shows how to print numbers in different colours. For this we use the "onprint" action
You can use hk_classes within Python. You can write your own Python applications using all the elements of hk_classes or interactively explore your data.
Example 4-1. Python module basics
horst@horstnotebook:~> python Python 2.2.2 (#1, Mar 17 2003, 15:17:58) [GCC 3.3 20030226 (prerelease) (SuSE Linux)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> from hk_classes import * >>> dr=hk_drivermanager() >>> con=dr.new_connection("mysql") >>> con.set_password("mypassword") >>> con.connect() 1 >>> db=con.new_database("exampledb") >>> mytable=db.new_table("authors") >>> mytable.enable()
hk_drivermanager handles the database drivers. You need just one object of this type for your whole application.
vector<hk_string>* driverlist(void): returns a list of all available database drivers
hk_connection* new_connection(const hk_string& drivername): creates a new object of type hk_connection;
hk_connection connects to the SQL Server. The most important functions are set_host(), set_user(), set_password() and connect().
- void set_host(const hk_string& )
sets the host name or host IP number
- hk_string host(void)
- void set_user(const hk_string& )
sets the user name used on the host
- hk_string user(void)
- void set_password(const hk_string& p)
sets the password for the user
- void set_tcp_port(unsigned int t)
sets the TCP port
- unsigned int tcp_port(void)
- virtual unsigned int default_tcp_port(void) const
returns the default TCP port for this database server
- bool connect(enum_interaction c=interactive)
connects to the server using the user, host and TCP data
- bool disconnect(void)
disconnects from the server
- bool is_connected(void)
returns true if this connection is connected to the server
- vector<hk_string>* dblist(void)
returns a list of all existing databases in this connection
- hk_database* new_database(const hk_string& name="")
creates a new hk_database object
- bool delete_database(const hk_string& dbase)
deletes an exisiting database
- bool database_exists(const hk_string& databasename)
returns true if the database "databasename" exists
hk_database represents a particular database on the SQL Server
- vector<hk_string>* tablelist(void)
returns a list of all existing tables in this database
- vector<hk_string>* querylist(void)
returns a list of all existing queries in this database
- vector<hk_string>* formlist(void)
returns a list of all existing forms in this database
- vector<hk_string>* reportlist(void)
returns a list of all existing reports in this database
- hk_datasource* new_table(const hk_string& name="",hk_presentation* p=NULL)
gets a new table object of type hk_datasource (read and write)
- hk_datasource* new_resultquery(hk_presentation* p=NULL)
gets a new query object of type hk_datasource (readonly)
- hk_actionquery* new_actionquery(void)
gets a hk_actionquery object. It can execute SQL statements that don't return data and are only successful or not successful (e.g. CREATE TABLE)
- hk_datasource* load_datasource(const hk_string& name,bool query=false,hk_presentation* p=NULL)
a convenience function for new_table and new_resultquery, that loads an existing datasource
- bool delete_table(const hk_string& table,enum_interaction x=interactive)
deletes a table
- bool table_exists(const hk_string& tablename)
returns true if the table 'tablename' exists
- bool query_exists(const hk_string& queryname)
returns true if the query 'queryname' exists
Example 5-1. Show data
horst@horstnotebook:~> python Python 2.2.2 (#1, Mar 17 2003, 15:17:58) [GCC 3.3 20030226 (prerelease) (SuSE Linux)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> from hk_classes import * >>> dr=hk_drivermanager() >>> con=dr.new_connection("mysql") >>> con.set_password("secret") >>> con.connect() 1 >>> db=con.new_database("exampledb") >>> table=db.new_table("authors") >>> i=0 >>> table.enable() SQL : SELECT * FROM `authors` 1 >>> table.goto_first() 1 >>> while i< table.max_rows(): ... table.show_currentrow() ... table.goto_next() ... i=i+1 ['1', 'Goethe,Johann Wolfgang', '1749', '1832', 'FALSE'] 1 ['2', 'Schiller, Friedrich von', '1759', '1805', 'TRUE'] 1 ['3', 'Lessing, Gotthold Ephraim', '1729', '1781', 'TRUE'] 1 ['4', 'Kleist', '1400', '0', 'FALSE']
Result queries (SELECT statement) return data when they are executed.
Example 5-2. Execute a "SELECT" query
horst@horstnotebook:~> python Python 2.2.2 (#1, Mar 17 2003, 15:17:58) [GCC 3.3 20030226 (prerelease) (SuSE Linux)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> from hk_classes import * d>>> dr=hk_drivermanager() >>> con=dr.new_connection("mysql") >>> con.set_password("secret") >>> con.connect() 1 >>> db=con.new_database("exampledb") >>> query=db.new_resultquery() >>> query.set_sql("SELECT * FROM authors") 1 >>> query.enable() SQL : SELECT * FROM authors 1
Example 5-3. Execute an action query
horst@horstnotebook:~> python Python 2.2.2 (#1, Mar 17 2003, 15:17:58) [GCC 3.3 20030226 (prerelease) (SuSE Linux)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> from hk_classes import * d>>> dr=hk_drivermanager() >>> con=dr.new_connection("mysql") >>> con.set_password("secret") >>> con.connect() 1 >>> db=con.new_database("exampledb") >>> query=db.new_actionquery() >>> query.set_sql("CREATE TABLE `another new table` ( `id` BIGINT(1) NOT NULL AUTO_INCREMENT , `name` BIGINT, PRIMARY KEY ( `id` ) )") >>> query.execute() CREATE TABLE `another new table` ( `id` BIGINT(1) NOT NULL AUTO_INCREMENT , `name` BIGINT, PRIMARY KEY ( `id` ) ) 1
To create a table, first get a new table object, set a name and set the mode to "createtable".
After that you can define new columns. First create it with new_column() and then set the type, name etc. When finished, create the table with create_table_now().
Example 5-4. create table
>>> table = db.new_table() >>> table.set_name("my new table") >>> table.setmode_createtable() >>> col=table.new_column() >>> col.set_columntype(hk_column.auto_inccolumn) >>> col.set_name("id") >>> col=table.new_column() >>> col.set_name("name") >>> table.create_table_now() CREATE TABLE `my new table` ( `id` BIGINT(1) NOT NULL AUTO_INCREMENT , BIGINT, PRIMARY KEY ( `id` ) ) Table created 1
Figure 5-1. hk_column type methods
- name()
returns the name of this column
- set_name(name)
sets the column name
- set_columntype(type)
sets the type of the column
Possible values are
textcolumn
auto_inccolumn
smallintegercolumn
integercolumn
smallfloatingcolumn
floatingcolumn
datecolumn
datetimecolumn
timecolumn
timestampcolumn
binarycolumn
memocolumn
boolcolumn
othercolumn
- columntype()
returns the type of the column.
- set_size()
sets the column size (e.g. if this column was should be a textcolumn with 10 characters set the type with set_type and the size with this function)
- size()
returns the column size (e.g. if this column was created as CHAR(10) it will return 10)
- set_primary(primary)
if 'primary' is true the column will be part of the primary key (primary index). Can only be edited if the datasource is in the mode ALTER or CREATE.
- is_primary()
returns true if this column is part of a primary key
- set_notnull(notnull)
if 'notnull' true the column needs a value
- is_notnull()
returns True if this column has to have a value
Create a lineedit field with the name "calculated_field" and don't set any datasource or field property. Put the following code in the onpush action of a button. The value you want to display has first be formatted with format_number and then be set with hk_dsdatavisible.set_value.
Example 5-5. calculated values
col1=hk_this.datasource().column_by_name("field_1") col2=hk_this.datasource().column_by_name("field_2") result=(col1.curval_asdouble()*col2.curval_asdouble()) calcfield=cast_dslineedit(hk_thisform.get_visible("calculated_field")) calcfield.set_value(format_number(result, calcfield.use_numberseparator(),calcfield.precision()))
You can define macros - that means global available Python functions - in the onopen() action of the form, and then call it from any other action:
Example 5-6. Macro example
def mymakro(hk_this,v): hk_this.show_warningmessage("Displaying the text: "+v)
This can be called from a buttons onclick action:
To format a number you can use the function format_number(). It will return a string. The first parameter is the number you want to format, the second is a boolean value whether or not you want a thousands separator, the third parameter shows the number of digits, and with the last you can set the target locale.
Example 5-8. Formatting a number
horst@horstnotebook:~> python Python 2.3.2 (#1, Oct 28 2003, 21:22:16) [GCC 3.3 20030226 (prerelease) (SuSE Linux)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> from hk_classes import * >>> number=12345.6789 >>> print number 12345.6789 >>> print format_number(number,1,2) 12.345,68 >>> print format_number(number,True,2) 12.345,68 >>> print format_number(number,True,2,"C") 12345.68 >>> print format_number(number,True,2,"de_DE") 12.345,68 >>> print format_number(number,False,2,"de_DE") 12345,68 >>>
When printing a report it is often wanted to have every second row printed with a different colour to make it easier to read the output. To do so put the following code in the on_print action of a report field
Put the following code into the on_click action of a button:
Example 5-10. Setting the taborder
hk_this.datasource().setmode_insertrow() hk_thisform.goto_taborder_first()
- set_focus(widget)
will set the focus to 'widget'
- set_taborder( taborder[,registerchange [, forcesetting]])
sets the tab order (the focus order) of the widgets. 'taborder' is a list of the presentation numbers of the widgets, which should get the focus
- goto_taborder_next()
will move the the focus to the next widget (see set_taborder( taborder[,registerchange [, forcesetting]]) )
- goto_taborder_previous()
will move the the focus to the previous widget (see set_taborder( taborder[,registerchange [, forcesetting]]) )
- goto_taborder_first()
will move the the focus to the first widget (see set_taborder( taborder[,registerchange [, forcesetting]]) )
- goto_taborder_last()
will move the the focus to the last widget (see set_taborder( taborder[,registerchange [, forcesetting]]) )
Put the following code into the on_key action of a lineedit field:
Example 5-12. Reacting on keyboard input
key=hk_this.key() print "on_key=(",key.key(),")(",key.text(),")" if key.state()&key.state_ctrl: print "ctrl" if key.state()&key.state_shift: print "shift" if key.state()&key.state_alt: print "alt" if key.key()==key.key_F5: show_warningmessage("F5 pressed. This will be ignored") key.set_accept_key(False) if key.key()==key.key_M and key.state()&key.state_ctrl: show_warningmessage("ctrl m pressed")
Put the following code into the on_click action of a button:
This example lets the user select a file(line 1) and load its content (line 2 and 3) into a field (line 4 and 5).