Ruby Logo Home

p8

Next Previous Table of Contents

Screenshot of tutorial eight

p8 is an upgraded p6 with the ability to send to p7 the url in the selected item of the list. This will allow p7 to open the favourite urls.


require 'Korundum'
 
class BookMarkList < KDE::ListView
    k_dcop 'void add(QString)'
	
    slots 'setURLInBrowser(QListViewItem *)'
 
    def initialize()
       super(nil, "Bookmarks")
       addColumn( i18n("My Bookmarks") );
       connect( self, SIGNAL('clicked(QListViewItem *)'), 
		    self, SLOT('setURLInBrowser(QListViewItem *)'))
    end
 
    def add( s )
        insertItem( KDE::ListViewItem.new( self , s ) )
    end
 
    def setURLInBrowser( item )
        if item.nil? then return end
        dcopRef = KDE::DCOPRef.new("p7", "Browser")
        if ! dcopRef.setURL(item.text(0))
            Qt.qWarning("Error with DCOP\n")
        end
    end
end

    about = KDE::AboutData.new("p8", "Tutorial - p8", "0.1")
    KDE::CmdLineArgs.init(ARGV, about)
    a = KDE::UniqueApplication.new()
	
    mylist = BookMarkList.new
    mylist.resize( 300, 200 )
	
    a.mainWidget = mylist
    mylist.show
	
    a.exec 
 

p8.rb


       connect( self, SIGNAL('clicked(QListViewItem *)'), 
		    self, SLOT('setURLInBrowser(QListViewItem *)'))

We've added a new slot, setURLInBrowser(QListViewItem *), which passes the url in the KDE::ListViewItem to the browser (p7).

This is a curious example of how you can even connect a signal in an object to a slot in the same object. Of course, this may not be the best way to do it, but at least it's clean to be used on a tutorial like this one.

Just in case you don't remember from p6, KDE::ListViewItem is the class that represents an item on a KDE::ListView. When you click on a item, the KDE::ListView object emits a signal with a parameter that is the KDE::ListViewItem that the user clicked on.

    def setURLInBrowser( item )
        if item.nil? then return end

In this slot, we first make sure that the user clicked on an item. If the user clicks on an empty area, the widget also emits the clicked signal (the user clicked on it after all), but with a nil parameter. In this case we just do nothing.

        dcopRef = KDE::DCOPRef.new("p7", "Browser")

If we have an url to send, we get the DCOPRef object as we do in p5 or p7 to send a DCOP message.

        if ! dcopRef.setURL(item.text(0))
            Qt.qWarning("Error with DCOP\n")
        end
    end

With item.text(0) we get the text in the first column of the clicked item (the first column is the only one we have created). Finally, we send a DCOP call to the application p7, object Browser and method setURL(QString) with a parameter of the url text.

We are now reaching the end of the tutorial, but first, we will make some modifications to simplify the sources while using the latest available technologies.

Next Previous Table of Contents


© 1999-2002 Antonio Larrosa © 2004 Richard Dale