HOWTO extend a class using inheritance
Posted: Sun Nov 27, 2011 8:04 pm
As I described in this example ( viewtopic.php?f=32&t=2121 ) HMG4 is based on full OOP and it is not difficult to use this for your own extensions without touching the source code.
A simple example: Let us extend the bitmap class so it will react on a double click.
Usually we define a simple click to call a function in BITMAP:
Using ONDBLCLICK instead of ONCLICK will not work because the BITMAP class didn´t know about the double click.
Let´s extend the BITMAP class:
1. We define the new CLASS MYIMAGE based on IMAGE:
2. Now we add the method for the doubleclick. To make it easy for us we look through the source codes and find classes that still react on double click. We take the code from them:
Because we use CLASS commands we need to include "hbclass.ch" !
3. We change our program code to define a MYIMAGE instead of IMAGE:
4. We now get a compiler error, because the preprocessor didn´t find "DEFINE MYIMAGE" in HMG.CH.
Since we are using oneliners in our define, we only have to change the translate command for the first line. We search for the translate commands of IMAGE in HMG.CH, copy the lines, put them into the head of our program and change IMAGE TO MYIMAGE (we don´t need to understand the rest of #xcommand
):
That´s it. Here is the whole program, the image will now react on double click instead of a click.
And the best thing is that we didn´t change anything of the source codes so any update will not influence our work.
Ricci
A simple example: Let us extend the bitmap class so it will react on a double click.
Usually we define a simple click to call a function in BITMAP:
Code: Select all
#include "hmg.ch"
#include "hbqtgui.ch"
FUNCTION Main()
LOCAL oForm1
HbQt_ErrorSys()
DEFINE MAINWINDOW oForm1
ROW 10
COL 10
WIDTH 300
HEIGHT 200
FONT "ARIAL"; SIZE 09
DEFINE IMAGE oBitmap
ROW 10; COL 10
WIDTH 200; HEIGHT 200
PICTURE "mypicture.png"
ONCLICK { || msginfo( "Click" ) }
END IMAGE
END Window
ACTIVATE WINDOW oForm1
RETURN Nil
Let´s extend the BITMAP class:
1. We define the new CLASS MYIMAGE based on IMAGE:
Code: Select all
CLASS MYIMAGE FROM BITMAP
ENDCLASS
Code: Select all
CLASS MYIMAGE FROM BITMAP
METHOD OnDblClick SETGET
ENDCLASS
METHOD OnDblClick( bValue ) CLASS MYIMAGE
IF PCOUNT() == 0
RETURN ::bOnDblClick
ELSEIF PCOUNT() == 1
IF hb_IsBlock( bValue ) == .T.
::bOnDblClick := bValue
::oQtObject:connect( QEvent_MouseButtonDblClick, bValue )
ELSEIF hb_IsNil( bValue ) == .T.
::bOnDblClick := NIL
::oQtObject:disconnect( QEvent_MouseButtonDblClick )
ENDIF
ENDIF
RETURN Self
3. We change our program code to define a MYIMAGE instead of IMAGE:
Code: Select all
DEFINE MYIMAGE oBitmap
ROW 10; COL 10
WIDTH 200; HEIGHT 200
PICTURE "mypicture.png"
ONDBLCLICK { || msginfo( "Click" ) }
END MYIMAGE
Since we are using oneliners in our define, we only have to change the translate command for the first line. We search for the translate commands of IMAGE in HMG.CH, copy the lines, put them into the head of our program and change IMAGE TO MYIMAGE (we don´t need to understand the rest of #xcommand

Code: Select all
#xcommand DEFINE MYIMAGE <tagreference>[ <dummy1: OF, PARENT> <oParent> ] [ TOVAR <varname> ] => ;
With Object [<varname> :=] MYIMAGE():New( <"tagreference">[, <oParent> ] )
#xcommand END MYIMAGE => End With
And the best thing is that we didn´t change anything of the source codes so any update will not influence our work.
Code: Select all
#include "hmg.ch"
#include "hbqtgui.ch"
#include "hbclass.ch"
#xcommand DEFINE MYIMAGE <tagreference>[ <dummy1: OF, PARENT> <oParent> ] [ TOVAR <varname> ] => ;
With Object [<varname> :=] MYIMAGE():New( <"tagreference">[, <oParent> ] )
#xcommand END MYIMAGE => End With
FUNCTION Main()
LOCAL oForm1
HbQt_ErrorSys()
DEFINE MAINWINDOW oForm1
ROW 10
COL 10
WIDTH 300
HEIGHT 200
FONT "ARIAL"; SIZE 09
DEFINE MYIMAGE oBitmap
ROW 10; COL 10
WIDTH 200; HEIGHT 200
PICTURE "mypicture.png"
ONDBLCLICK { || msginfo( "Click" ) }
END IMAGE
END Window
ACTIVATE WINDOW oForm1
RETURN Nil
//-------------------------------------------------------------------------------------------------------------------------------
/*==============================================================================
MYIMAGE class
==============================================================================*/
CLASS MYIMAGE FROM BITMAP
// Methods
METHOD OnDblClick SETGET
ENDCLASS
/*..............................................................................
OnDblClick
..............................................................................*/
METHOD OnDblClick( bValue ) CLASS MYIMAGE
IF PCOUNT() == 0
RETURN ::bOnDblClick
ELSEIF PCOUNT() == 1
IF hb_IsBlock( bValue ) == .T.
::bOnDblClick := bValue
::oQtObject:connect( QEvent_MouseButtonDblClick, bValue )
ELSEIF hb_IsNil( bValue ) == .T.
::bOnDblClick := NIL
::oQtObject:disconnect( QEvent_MouseButtonDblClick )
ENDIF
ENDIF
RETURN Self