The Ring Programming Language Version 1.4 Book - Part 19 of 30
The Ring Programming Language Version 1.4 Book - Part 19 of 30
51.55. Classes and their Methods to use the default events 518
Ring Documentation, Release 1.4
51.55. Classes and their Methods to use the default events 519
Ring Documentation, Release 1.4
RingQt define a new class called QAllEvents that help you in using Events Filter
The next table presents the methods that we have
Methods to get parameters Class Name
getKeyCode() > Number QAllEvents
getx() > Number
gety() > Number
getglobalx() > Number
getglobaly() > Number
getbutton() > Number
getbuttons() > Number
The next table presents the methods that we have to use events.
1. RingQt use simple methods to set the code that will be executed for events.
Syntax:
Set<Event_Name>Event(cEventCode)
2. RingQt change the name of some methods to avoid conflict with Ring Keywords.
The next table present these little changes
Qt Documentation : http://doc.qt.io/qt-5/classes.html
See the RingQt Classes and Methods Reference chapter for supported classes and methods.
We added new classes to RingQt - another version of classes where the class names doesnt start with the q letter
Also updated methods so the index start from 1 when we deal with the GUI controls like
ComboBox
ListWidget
TableWidget
TreeWidget
These classes are inside guilib.ring under the package name : System.GUI
To use it
load "guilib.ring"
import System.GUI
This doesnt have any effect on our previous code, Its just another choice for better code that is consistent with Ring
rules.
Also the form designer is updated to provide us the choice between using classes where (index start from 0) or (index
start from 1)
Example (Uses the Form Designer)
1. https://github.com/ring-lang/ring/blob/master/applications/formdesigner/tests/indexstart/indexstartView.ring
2. https://github.com/ring-lang/ring/blob/master/applications/formdesigner/tests/indexstart/indexstartController.ring
FIFTYTWO
In this chapter we will learn about Building RingQt Applications for Mobile.
Update the Android SDK to get the API and tools packages required for development
523
Ring Documentation, Release 1.4
Note: All of the missing libraries ((LibCurl, OpenSSL & Allegro) can be compiled for Android, but they are not
included in this Qt project.
3. use if isandroid() when you want to modify the code just for android
Example:
if isandroid()
// Android code
else
// other platforms
ok
(4) Sometimes you will find that the button text/image is repeated in drawing ! its Qt problem that you can avoid
using the next code.
if isandroid()
setStyleSheet("
border-style: outset;
border-width: 2px;
border-radius: 4px;
border-color: black;
padding: 6px;")
ok
5. Always use Layouts instead of manual setting of controls position and size.
This is the best way to get the expected user interface to avoid problems like (controls with small/extra size)
6. When you deal with Qt Classes you can determine the images from resources (you dont need to copy them
using main.cpp)
Example:
if isandroid()
mypic = new QPixmap(":/resources/cardsimage")
else
mypic = new QPixmap("cards.jpg")
ok
In the previous example, cards.jpg is added to the resources then we write the cardsimage as alias for cards.jpg
FIFTYTHREE
In this chapter we will learn about the objects library for RingQt applications.
Ring comes with the Objects library for RingQt applications. Instead of using global variables for windows objects
and connecting events to objects using the object name, the Objects Library will manage the GUI objects and will
provide a more natural API to quickly create one or many windows from the same class and the library provide a way
to quickly set methods to be executed when an event is fired. Also the library provide a natural interface to quickly
use the parent or the caller windows from the child or sub windows.
The Objects Library is designed to be used with the MVC Design Pattern.
The Objects Library is merged in RingQt so you can use it directly when you use RingQt
53.2 Example
526
Ring Documentation, Release 1.4
The User Can click on the button many times to open many sub windows.
Each Sub Window contains Two buttons.
The first button in the sub window change the Main and the Sub Windows Titles.
The second button in the sub window close the Sub Window.
load "guilib.ring"
new qApp {
open_window( :MainWindowController )
exec()
}
The next screen shot after clicking on the button in each sub window.
We can use the Open_WindowAndLink() function to connect between the application windows, pass messages (call
methods) between the objects.
This function uses Meta-programming to define dynamic methods in the Caller Class to use the dynamic objects of
other windows that we create.
Example : (Uses the Form Designer)
First Window
1. https://github.com/ring-lang/ring/blob/master/applications/formdesigner/tests/twowindowspart5/firstwindowView.ring
2. https://github.com/ring-lang/ring/blob/master/applications/formdesigner/tests/twowindowspart5/firstwindowController.ring
Second Window
1. https://github.com/ring-lang/ring/blob/master/applications/formdesigner/tests/twowindowspart5/secondwindowView.ring
2. https://github.com/ring-lang/ring/blob/master/applications/formdesigner/tests/twowindowspart5/secondwindowController.ring
func OpenSecondWindow
Open_WindowAndLink(:SecondWindowController,self)
func SendMessage
if IsSecondWindow()
SecondWindow().setMessage("Message from the first window")
ok
The library source code is very simple, You can check the source code files
https://github.com/ring-lang/ring/blob/master/extensions/ringqt/objectslib/objects.ring
https://github.com/ring-lang/ring/blob/master/extensions/ringqt/objectslib/subwindows.ring
FIFTYFOUR
531
Ring Documentation, Release 1.4
When we select the window or one control, We will have the selected object properties.
Also In the properties window we have a combobox to select the active control.
Some properties provide a button next to the property value. We can click on the button to get more options.
When we select more than one control, We will have options for multi-selection
When we save the form file (*.rform), The Form Designer will create two Ring files
The Controller Class
The View Class
For example, if the form file is helloworld.rform
The form designer will generate two files
helloworldcontroller.ring
helloworldview.ring
To run the program, Open the controller class file then click the Run button (CTRL+F5)
Tip: When you open a form using Ring Notepad, the controller class will be opened automatically, So we can press
(CTRL+F5) or click on the Run button while the form designer window is active.
Where inside the controller class, We uses the oView object to access the form.
Another Example :
From the Window properties we can open the Window Flags window.
For some controls like the List Widget we can enter items separated by comma ,
FIFTYFIVE
In this chapter we will learn about scope rules and how Ring find variables.
Also we will learn about conflicts and how to solve/avoid them.
The next information are important once you start developing large applications using Ring
These application may uses
Global variables (Try to avoid them)
Classes (Object-Oriented)
braces { } to access objects
Declarative Programming
Natural Programming
1. Ring uses lexical scoping, i.e. the scope of the variable is based on where we defined the variable.
2. Inside braces { } when you access an object, You will change the current active object scope to this object scope
but you still can access the global scope and the local scope.
3. After the Class keyword and the class name, when you write variable names to be defined as attributes, You
still can access the global scope.
In this region (class region - after the class name and before methods) we have
Global Scope -> The Global Scope
Object Scope -> The Object Scope
Local Scope -> The Object Scope
539
Ring Documentation, Release 1.4
Note: Since the local scope in the class region point also to the object scope in this region, we can use nested braces
and still have access to the object scope of the class through the local scope.
Tip: You can create windows and controls as attibutes by defining them in this region.
Tip: In the class region if you created objects and used braces {} to access them then using self.attribute inside braces
will use the class (not the object that you access) because you have access to the class through the local scope.
When we use object.attribute the search will be in the object attributes only.
I.e. no search will be done in the local scope or in the global scope for the object attribute.
Note: Using self.attribute will search for the first self before searching for attributes.
The self object is a reference to the current object that we can use from the class methods.
When we are inside class method and use Self we mean the object that will be created from this class.
Inside the class methods if we used Braces { } this will change the current object scope and self will be changed also
inside braces to reference the object that we access using Braces.
Inside the Class Region (after the class name and before any method) we have access to the object through the object
scope and the local scope also. In this region using Self will always be a reference to the class object. if we used
Braces to change the object scope then used Self inside Braces, Also self will be a reference to the class object (not
the object that we already access using braces) because in the class region we have :-
Global Scope > Global Scope
Object Scope > Object Scope
class person
name
address
phone
In the previous example we have a global variable called name inside the class person.
when we use the variable name, Ring will start the search operation and will try to find it.
if found > Use it
if not found > Define new attribute
But the variable name is a global variable, so it will be found and used!
We will not have the attribute name! added to the object.
Solution (1) - Use the Main Function
func main
name = "test"
o1 = new person
see o1
class person
name
address
phone
Solution (2) - Use special mark for global variable names like $
$name = "test"
o1 = new person
see o1
class person
name
address
phone
class person
AddAttribute(self,"name")
address
phone
class person
self.name
address
phone
This conflict may happen when we access the object using braces
Example:
func main
name = "nice"
o1 = new person {name="mahmoud" address="Egypt" phone = 000 }
see o1
class person
name
address
phone
class person
name
address
phone
class person
name
address
phone
class person
name
address
phone
Remember that we have Three scopes (Local Scope, Object Scope and Global Scope) and when we are inside a class
method, we expect that we have access to the object attributes and methods and this is true until we use braces to
access another object attributes and methods because in this case our object scope will be switched to another object.
new point { test() }
class point
x=10 y=20
func test
see x + nl + y + nl # works fine
myobj = new otherclass {
see name + nl
see x + nl + y + nl # error !
class otherclass
name = "test"
Output:
10
20
test
class point
x=10 y=20
func test
see x + nl + y + nl # works fine
myobj = new otherclass {
see name + nl
}
see x + nl + y + nl # Outside braces - works fine
class otherclass
name = "test"
Output:
10
20
test
10
20
class point
x=10 y=20
func test
see x + nl + y + nl
myobj = new otherclass
see myobj.name
see x + nl + y + nl
class otherclass
name = "test"
class point
x=10 y=20
func test
oSelf = self
see x + nl + y + nl
myobj = new otherclass {
see name + nl
see oself.x + nl + oself.y + nl
}
class otherclass
name = "test"
Output:
10
20
test
10
20
The problem with the previous line is that we will have a new copy from the object Because in Ring the assignment
operator copy lists and objects by value (not by reference).
When we access the new object attributes (reading) we dont have problems
But if we modified the object attributes (Then we will modify the copy!).
class point
x=10 y=20
func test
oSelf = self
see x + nl + y + nl
myobj = new otherclass {
see name + nl
oSelf {
see x + nl + y + nl
}
}
class otherclass
name = "test"
In a GUI application, we may create a class contains the window objects as attributes to be able to access the controls
from different methods. Remember the previous information when you try to access objects using braces inside
methods because in this case you cant access the object attributes directly and if you copied the self object you will
work on a copy and the new controls that you create will be related to the copy and you cant access them.
55.10 Accessing the class attributes from braces inside class meth-
ods
We access the class attributes directly from the class methods, also we have the choice to use the Self reference before
the attribute/method name. Using Braces {} inside class method change the active object scope and prevent us from
getting direct access to the class attributes. Also using Self will not help because the Self reference will be changed to
the object that we access using Braces.
In this case if you want to read an attribute you have to copy the Self object before using Braces and if you want to
modify an attribute you have to the copy from local variable to the object attribute after using Braces.
This case happens when you want to read/modify attribute insead braces.
Class MyApp
oCon # Attribute
Func OpenDatabase
# some code here
new QSqlDatabase() {
oCon = addDatabase("QSQLITE") {
setDatabaseName("weighthistory.db")
open()
}
}
self.oCon = oCon
# some code here
In the previous example we want to create the connection object and save it inside the oCon attribute.
The object is an output from the addDatabase() method that we use after accessing the QSQLDatabase() object.
Inside braces we cant use the Self reference to use the object created from the MyApp class, Because the Self reference
here will be to the object that we access using Braces.
We solved the problem in the previous example by creating a local variable called oCon then after Braces we copied
that variable to the oCon attribute.
The next code is another solution.
Class MyApp
oCon # Attribute
Func OpenDatabase
# some code here
oCon = new QSqlDatabase()
oCon = oCon.addDatabase("QSQLITE") {
setDatabaseName("weighthistory.db")
Open()
}
# some code here
55.10. Accessing the class attributes from braces inside class methods 546
Ring Documentation, Release 1.4
Class MyApp
oCon # Attribute
Func OpenDatabase
# some code here
new QSqlDatabase() {
this.oCon = addDatabase("QSQLITE") {
setDatabaseName("weighthistory.db")
Open()
}
}
# some code here
Note: We used this.attribute to access the class attribute (oCon) while we are inside Braces.
A good way for creating classes for windows is to define the window directly after the class name
In this area you can use nested braces without problems to define the window and the controls, and they will be
attributes that you can access from methods.
Example:
Load "guilib.ring"
new qApp
{
$ObjectName = "oFirstWindow"
oFirstWindow = new FirstWindow
$ObjectName = "oSecondWindow"
oSecondWindow = new SecondWindow
exec()
}
Class FirstWindow