Next Previous Table of Contents

Now that we know how to create a menu we will make a nearly-real application with p4 . Thanks to the work of Lars Knoll, Antti Koivisto, Waldo Bastian and many others, we will be able to use a KDE::HTML, a widget that is able to display an HTML page, and that using the kio library, is able to automatically retrieve a page from the net. Let's see how it works.
require 'Korundum'
class MainWindow < KDE::MainWindow
slots 'changeLocation()',
'openURLRequest(const KURL&, const KParts::URLArgs& )'
def initialize( name )
super(nil, name)
setCaption("KDE Tutorial - p4")
filemenu = Qt::PopupMenu.new
filemenu.insertItem( i18n( "&Quit" ), $kapp, SLOT( 'quit()' ) )
about =
i18n("p4 1.0\n\n" +
"(C) 1999-2002 Antonio Larrosa Jimenez\n" +
"larrosa@kde.org\t\tantlarr@supercable.es\n" +
"Malaga (Spain)\n\n" +
"Simple KDE Tutorial\n" +
"This tutorial comes with ABSOLUTELY NO WARRANTY \n" +
"This is free software, and you are welcome to redistribute it\n" +
"under certain conditions\n");
helpmenu = helpMenu(about)
menu = menuBar()
menu.insertItem( i18n( "&File" ), filemenu)
menu.insertSeparator()
menu.insertItem(i18n("&Help"), helpmenu)
vbox = Qt::VBox.new( self )
@location = Qt::LineEdit.new( vbox )
@location.setText( "http://localhost" )
@browser = KDE::HTMLPart.new( vbox )
@browser.openURL( KDE::URL.new(@location.text()) )
connect( @location , SIGNAL( 'returnPressed()' ),
self, SLOT( 'changeLocation()' ) )
connect( @browser.browserExtension(),
SIGNAL( 'openURLRequest( const KURL &, const KParts::URLArgs & )' ),
self, SLOT( 'openURLRequest(const KURL &, const KParts::URLArgs & )' ) )
setCentralWidget(vbox)
end
def changeLocation()
@browser.openURL( KDE::URL.new(@location.text()) )
end
def openURLRequest(url, part)
@location.text = url.url()
changeLocation()
end
end
about = KDE::AboutData.new("p4", "Tutorial - p4", "0.1")
KDE::CmdLineArgs.init(ARGV, about)
a = KDE::Application.new()
window = MainWindow.new( "Tutorial - p4" )
window.resize( 300, 200 )
a.mainWidget = window
window.show
a.exec
The first difference with p3 is in p4.rb. We aren't going to use the menu to anything but exiting the application, so we have removed the two slots for the Open and Save dialogs.
slots 'changeLocation()',
'openURLRequest(const KURL&, const KParts::URLArgs& )'
We are defining a changeLocation() slot to be called when the
user wants to load a new page by clicking the Enter key on the location bar.
The openURLRequest method is a slot which gets called when the user clicks on
a url link in the html page, and the html page requests us to open that url
(which gets passed as a parameter). We are going to use two widgets, one is a
Qt::LineEdit widget and the other is a KDE::HTMLPart. Qt::LineEdit implements a line in
which the user may edit some text, in this case, the html page that is displayed
in the KDE::HTMLPart.
vbox = Qt::VBox.new( self )
We are using a Qt::VBox to manage the layout of the widget. As we want to display the Qt::LineEdit widget and the KDE::HTMLPart one under the another, this is the perfect task for a Qt::VBox. This widget automatically puts all its children in a vertical layout (as well as Qt::HBox puts them in an horizontal layout), and manages to update their geometry automatically whenever the Qt::VBox is resized. Note that you can use more complex layout classed (such as Qt::GridLayout) to manage the geometry, or even do it manually as other toolkits do. But this simplifies the code, while being extremely powerful. Note, for example, that whenever the user resizes the window, the KDE::HTMLPart always takes the full height while the Qt::LineEdit always takes a fixed height, and they both always take the full width of the application's main window.
@location = Qt::LineEdit.new( vbox )
@location.text = "http://localhost"
We create the a Qt::LineEdit object, @location, as a child of vbox and set some text to be shown by default.
You can browse the internet with this application, but as many people will only test this with a local web server, let's put the local host as default site. Be sure to have apache running on your machine for this application (and the next ones!) to work.
@browser = KDE::HTMLPart.new( vbox )
@browser.openURL( KDE::URL.new(@location.text()) )
With this code, we create the @browser object as a child of vbox, so it will be added under the location bar.
Calling openURL we ask our KDE::HTMLPart to download the url pointed to by the text that is in the location bar, and display it.
connect( @location , SIGNAL( 'returnPressed()' ),
self, SLOT( 'changeLocation()' ) )
When the user presses the return key on the location bar, it emits a returnPressed()
signal, and with this code we connect it to the changeLocation() slot in self.
We can call connect directly (without using Qt::Object::), because we are now
inside a class the inherits Qt::Object (indirectly, as we inherit KDE::MainWindow, which inherits Qt::Widget
which inherits Qt::Object).
connect( @browser.browserExtension(),
SIGNAL( 'openURLRequest( const KURL&, const KParts::URLArgs& )' ),
self, SLOT( 'openURLRequest(const KURL&, const KParts::URLArgs& )' ) )
We are now connecting another signal with a slot. The nice thing now is that
browser emits a signal openURLRequest( const KURL&,
const KParts::URLArgs& ) when it wants to open a URL (for example, because
the user clicked on a url). In fact, the signal is not emitted directly by the KDE::HTMLPart
object, but by its browserExtension object, and that's why we use it as the signal emitter
object.
Btw, the second parameter is not used, so we will ignore it as it's only used in very special cases.
setCentralWidget(vbox)
We now set the vbox widget as the central widget of this KDE::MainWindow object.
@browser.openURL( KDE::URL.new(@location.text()) )
The changeLocation slot simply asks @browser to open
the url written in the location bar.
def openURLRequest(url, part)
@location.text = url.url()
changeLocation()
end
This is the code that gets called when the browser wants to open a new url, as we remember, the url() method in a KDE::URL object returns the complete url, so we write it into the location bar. Then we call changeLocation(), to set the url stored in the location bar in the html part.
Think of the functionality you get with only 66 lines, imagine what you can do in 1000 lines! :-)
Next Previous Table of Contents