[Kde-bindings] small qtruby example

Alexander Kellett lypanov at kde.org
Thu Jul 31 15:57:11 UTC 2003


heya all,

okay. first small test.

being able to do this
in ruby is just soo
much faster development
wise than c++ has ever 
been for me.

some really tiny chunks
of documentation about
writing using qtruby would
be nice so i'll try to
jot some notes down
starting on monday.
just a few minor things
about the syntax that 
get you baffled at 
first, for example 
the Qt.RichText thing
kind of baffled me :)
(as opposed to Qt::RichText)

also. you can get quite
strange bugs when you 
subclass a Qt object 
as method_missing is 
called for any locals
you forgot to declare,
makes for really wierd
error messages for
very simple errors :)

source is pretty unreadable
at the moment as i really
did it in a dash effort
but, still, for such a
short amount of code, 
it does a *lot*, so:

thanks richard/ashley/germain/anyone else who worked on it + related stuff :)

mvg,
Alex
-------------- next part --------------
#!/usr/bin/ruby -w

require 'rexml/document'
require 'Qt'

class MyTextEditor < Qt::TextEdit
   signals 'saved()'
   slots 'insert_icon()', 'load()', 'save()'
   def initialize(w = nil)
      @images = {}
      @@next_image_id = 0
      super(w)
      self.setTextFormat(Qt.RichText)
   end
   def insert_richtext(richtext)
      # todo, use a rand string
      unique_string = '000___xxx123456789xxx___xxx123456789xxx___000'
      insert(unique_string)
      txt = self.text().gsub(unique_string, richtext)
      self.setText(txt)
   end
   def next_image_id
      @@next_image_id += 1
   end
   def load_image(fname, image_id)
      pixmap = Qt::Pixmap.new(fname)
      msfactory = Qt::MimeSourceFactory.defaultFactory()
      msfactory.setPixmap(image_id, pixmap);
      @images[image_id] = fname
      image_id
   end
   def insert_icon
      fname = Qt::FileDialog.getOpenFileName
      image_id = "image_#{next_image_id}"
      load_image(fname, image_id)
      insert_richtext('<qt><img source="'+image_id+'"></qt>')
   end
   def createPopupMenu(pos) # virtual
      pm = Qt::PopupMenu.new
      id = pm.insertItem("Insert Image!")
      pm.connectItem(id, self, SLOT('insert_icon()'))
      pm
   end
   def load
      @images = {}
      fname = Qt::FileDialog.getOpenFileName
      if File.exists?(fname+".metadata.xml")
         file = File.new(fname+".metadata.xml")
         @xmldoc = REXML::Document.new file
         @xmldoc.root.elements.each("image") {
            |image|
            image_id = image.attributes["ident"]
            img_fname = image.attributes["filename"]
            load_image(img_fname, image_id)
         }
      end
      txt = File.open(fname).gets(nil)
      self.setText(txt)
   end
   def save
      # pressing cancel here crashes with an error about NULL, maybe as we expect a value yet it passed back QString::null?
      fname = Qt::FileDialog.getSaveFileName
      if File.exists? fname
         # ret = Qt::MessageBox.question(self, "File Already Exists", "Are you sure you want to overwrite this file?")
         Qt::MessageBox.critical(self, "File Already Exists", "Sorry, file already exists. Please choice a non-existing filename!")
         return save
      end
      File.new(fname, "w").puts(text())
      unless @images.empty?
         metadata_doc = REXML::Document.new '<metadata/>'
         @images.each {
            |id, img_fname|
            metadata_doc.root.add_element("image", {"filename"=>img_fname, "ident"=>id})
         }
         File.new(fname+".metadata.xml", "w").puts(metadata_doc)
      end
      emit saved()
   end
end

class MyWidget < Qt::VBox
   slots 'text_changed()', 'saved()'
   def initialize()
      super
      @textedit = MyTextEditor.new(self)
      connect(@textedit, SIGNAL('textChanged()'), self, SLOT('text_changed()'))
      connect(@textedit, SIGNAL('saved()'), self, SLOT('saved()'))

      buttonbar = Qt::HBox.new(self)
      @load = Qt::PushButton.new("Load", buttonbar)
      connect(@load, SIGNAL('clicked()'), @textedit, SLOT('load()'))
      @save = Qt::PushButton.new("Save!", buttonbar)
      connect(@save, SIGNAL('clicked()'), @textedit, SLOT('save()'))

      quit = Qt::PushButton.new("Quit!", self)
	   connect(quit, SIGNAL('clicked()'), $qApp, SLOT('quit()'))
   end
   def saved
      @save.setDisabled(true)
   end
   def text_changed
      @save.setDisabled(false)
   end
end

a = Qt::Application.new(ARGV)

w = MyWidget.new
w.show

a.setMainWidget(w)
a.exec()
exit


More information about the Kde-bindings mailing list