Simple Ruby Plasmoid

Simple Ruby Plasmoid

Hello Planet KDE, hello RubyCorner

After some blog posts on my old blog about KDE programming with Ruby I decided to bring the content a little bit more to the people. Here I am.

Two days before yesterday I got a mail concerning a problem related to creating plasmoids with Ruby. I never had the wish to create my own plasmoid. I thought it would be difficult, but while getting a closer look, I’ve noticed how easy it is - in the case of using Ruby.

Simple Ruby Plasmoid

Simple Plasmoid made with Ruby This example is a modified version from the one in the KDE techbase wiki.

The plasmoid sends the content of a QLineEdit to the clipboard when pressing the QPushButton or pressing the enter key and clears the QLineEdit afterwards.

Test It

If you want to try it yourself, you just have to extract simple_ruby_plasmoid_clipboard.tar.gz, change the directory to ruby-test-applet and start the plasmoid in a special viewer with plasmoidviewer.

You have to install the Ruby KDE bindings package (on opensuse it is called ruby-kde4), but on a lot of KDE4 systems this should be installed already.

Understand It

First you need the right directory tree for your plasmoid. It should be look like this:

ruby-test-applet/
|-- contents
|   `-- code
|       `-- main.rb
`-- metadata.desktop

You need at least two files. The first one is main.rb, that contains your program code.

When accessing KDE libs from Ruby you write nearly the same code as you would write in C++.

A short example in C++:

setMinimumSize( 150, 150 )
Plasma::LineEdit line_edit( parent )

The same using Ruby:

set_minimum_size 150, 150
# or
setMinimumSize 150, 150
# or
self.minimum_size = 150, 150
line_edit Plasma::LineEdit.new self

Member variables begins with an @ sign. There are different aliases for KDE and Qt methods. You can omit brackets in a lot of cases. You don’t need any header files. You don’t need to compile.

require 'plasma_applet'

module RubyTestApplet
  class Main < PlasmaScripting::Applet

    slots :addText

    def init
      set_minimum_size 150, 150

      @layout = Qt::GraphicsLinearLayout.new Qt::Vertical, self
      self.layout = @layout

      @label = Plasma::Label.new self
      @label.text = 'This plasmoid will copy the text you enter below to the clipboard.'
      @layout.add_item @label

      @line_edit = Plasma::LineEdit.new self

      begin
        @line_edit.clear_button_shown = true # not supported in early plasma versions
      rescue
        nil # but that doesn't matter
      end

      @layout.add_item @line_edit

      @button = Plasma::PushButton.new self
      @button.text = 'Copy to clipboard'
      @layout.add_item @button

      Qt::Object.connect( @button, SIGNAL(:clicked), self, SLOT(:addText) )
      Qt::Object.connect( @line_edit, SIGNAL(:returnPressed), self, SLOT(:addText) )
    end

    def addText
      Qt::Application.clipboard.text = @line_edit.text
      @line_edit.text = ""
    end

  end
end

# kate: remove_trailing_space on; replace-trailing-space-save on; indent-width 2; indent-mode ruby;

The second file you need is the metadata.desktop containing all the meta data. :wink:

[Desktop Entry]
Name=Simple Ruby applet
Comment=This is a simple applet written in Ruby
Icon=chronometer
Type=Service
ServiceTypes=Plasma/Applet

X-Plasma-API=ruby-script
X-Plasma-MainScript=code/main.rb

X-KDE-PluginInfo-Author=Me
X-KDE-PluginInfo-Email=me@example.com
X-KDE-PluginInfo-Name=ruby-test
X-KDE-PluginInfo-Version=0.1
X-KDE-PluginInfo-Website=http://plasma.kde.org/
X-KDE-PluginInfo-Category=Examples
X-KDE-PluginInfo-Depends=
X-KDE-PluginInfo-License=GPL
X-KDE-PluginInfo-EnabledByDefault=true

metadata.desktop

What do you think? So easy, isn’t it? If you have ever played a little bit with Ruby and Qt or KDE you should know enough to create your own plasmoid within a quarter of an hour!

Just moved to Jekyll

Static Website Generators

After using the static website generator webgen I started to appreciate the idea not to depend on special hosts with php, mysql and so on enabled. You can put all files on a CD and it will works nevertheless. Like in the good old days, when there was only the notepad and the Netscape Navigator.

My last blog was powered by Movable Type. A really good, full-featured engine, but editing in the ajax backend was damn slow, because I hosted Movable Type myself via a dynamic dns service. And at home the provided upload rate is not good enough to host a page serious. I don’t want to pay for websites. There is so much static webspace in the www. So I switched over to the ruby1 driven Jekyll and got in addtion the hosting for free on http://github.com.

Building The Blog

I started with a copy of http://github.com/schacon/schacon.github.com. At this point a big “Horay!” to schacon and open source, that made a quick start possible.

While editing the files I found some usefull tricks you, you maybe googled for.

CSS selector for elements without class attribute

I wanted a template which allows to use nice CSS formatted <code> and <pre> tags for source code, but on the other hand I didn’t want to change the style of included code snippets from http://pastie.org or http://refactormycode.com.

So I found out how to change only these html tags in the source code, which doesn’t have an class attribute, because that is exactly the difference between these pastie-snippets and my own pre-elements.

The solution is to use the negation pseudo-class selector.

pre, code {
  font-family:Consolas,"Andale Mono","Courier New",Courier,mono !important;
  font-weight: 400;
  font-size:0.9em;
}

*:not(PRE) > code {
  background-color: #333;
  color: #fff;
}

pre:not([class]) {
  background-color: #333;
  border: 1px solid #000;
  color: #fff;
  margin: 1em 0;
  overflow: auto;
  padding: 0.5em;
}

First I define some basic font styles for all kind of code. After that I define styles for the code, which isn’t in a pre-element and at last follows the style for the pygments-generated in-built code.

Integrating other static pages

As BSc physics student I got a userpage on the IT system of the physics institute, where I store some files. Furthermore I have an account at DESY, from where I also get a userpage. So I thought it would be nice to integrate both pages.

In the end I got it by defining a special header and footer in the .htaccess file for the Apache auto-generated directory index.

ReadmeName footer.html 
HeaderName header.html
IndexStyleSheet style.css
IndexIgnore style.css footer.html header.html

.htaccess

More explanation are on the official Apache Module page given.

You might also want to look in my blog sources.

Generating pygments CSS files

I didn’t found it immediately in the internet, so I want to increase the pages containting the hint by 1.

If you want to create the CSS file, maybe with the look and feel of http://pastie.org, try this in your cmd to generate a complete “highlight.css”.

pygmentize -f html -a .highlight -S pastie > highlight.css

To get an overview over possible you have to execute this command:

pygmentize -L styles

Syntax highlighting is done via pygments, which bases on python. Is that really necessary?

  1. Ruby is the best language I’ve seen so far. So the new system had to be based on it. ↩︎

CAcert SSL certificates in KMail KDE 4

In preparation of the sixth “Brandenburger Linux-Infotag” I passed the CAcert Assurer Challange, which was the last requirement I had to fulfil to be allowed to verify people.

To start the “challenge” it is necessary to login with your own SSL certificate from CAcert. I only used gpg the last years, so I had have to create a new one and configure KDE to use it.

That’s unfortunately not easy.

The perhaps smallest KDE application in the world

For testing some parts of the korundum bindings, that allow the usage of KDE in Ruby, i wrote a small and lightweight application with only the necessary parts.

Maybe you can take it also to do some testings or just as a little example.

With its 12 lines (17 lines minus 3 empty lines minus Shebang line and $KCode line, that are not really required) of code it is perhaps the smalles KDE application you will find.

Of course, every Qt app would be a few lines smaller, because you dont need the KAbout object.

#!/usr/bin/env ruby

$KCODE = 'u'
require 'korundum4'

about = KDE::AboutData.new("ktest", "",
    KDE.ki18n("KTest"), "0.1")
KDE::CmdLineArgs.init(ARGV, about)
a = KDE::Application.new
w = KDE::PushButton.new( "Click me to quit" ) do
  connect( SIGNAL :clicked ) do
    puts "Do something else"
    KDE::Application.instance.quit
  end
end
w.show

a.exec

Ruby Qt/KDE4 Template KMainWindow

![KMainWindow](<%= site_url %>/images/kmainwindow.png “KMainWindow”){: .pull-right} Starting using the Qt-Toolkit (or the extension with KDE classes) is a little difficult. Especially if you never used really the toolkit with C++. So you have to learn translating the C++ API reference into ruby.

Some usefull links:

But when you got it, the usage is pretty cool. In the following code listing you can find the code for a full-featured kmainwindow.

#!/usr/bin/env ruby
# file: kmainwindow.rb
$KCODE = 'u'

require 'korundum4' # kd4 bindings

class CustomWidget < KDE::MainWindow

  def initialize

    super

    resize(520, 535)

    # Prepare the Actions
    @actionQuit = KDE::Action.new( self ) {
      setIcon KDE::Icon.new "application-exit"
    }
    connect( @actionQuit, SIGNAL( :triggered ), SLOT( :close ) )

    # Prepare the Menu
    @menuBar = menuBar
    @menuFile = Qt::Menu.new @menuBar
    @menuFile.addAction @actionQuit
    @menuBar.addAction @menuFile.menuAction
    @helpMenu = helpMenu
    @menuBar.addAction @helpMenu.menuAction

    setMenuBar(@menuBar)

    # Prepare Statusbar
    @statusBar = statusBar
    setStatusBar @statusBar

    # Prepare Toolbar
    @toolBar = toolBar
    @toolBar.addAction @actionQuit
    addToolBar(Qt::TopToolBarArea, @toolBar)


    # Prepare Central Widget
    @centralwidget = KDE::TextEdit.new self
    setCentralWidget @centralwidget

    retranslateUi
  end

  def retranslateUi
    @menuFile.title = i18n "File"
    setWindowTitle i18n "MainWindow"
    # @statusBar.showMessage i18n "Loading"
    @actionQuit.text = i18n "Quit"
    @actionQuit.shortcut =  KDE::Shortcut.new i18nc( "Quit", "Ctrl+Q" )
end

end

about = KDE::AboutData.new(
  "app",                           # internal application name
  # language catlog name for i10n (konqueror's catalog for the beginning is better than no catalog)
  "konqueror",
  KDE.ki18n("KApp"),                 # application name in the about menu and everywhere else
  "0.1",                             # application version
  KDE::ki18n("A Tool to easily create HTML formatted Code"),  # short description
  KDE::AboutData::License_GPL_V3,    # license
  KDE::ki18n("(c) 1999-2000, Name"), # copyright info
  # text in the about box - maybe with \n line breaks
  KDE::ki18n("just some text in the about box"),
  # project homepage and eMail adress for bug reports - attention: homepage changes standard dbus/dcop name!
  "http://homepage.de", "bugs@homepage.de" )

about.setProgramIconName  "plasma" # use the plasma-icon instead of question mark
KDE::CmdLineArgs.init(ARGV, about)
a = KDE::Application.new
w = CustomWidget.new
a.topWidget = w
w.show
a.exec

kmainwindow.rb

Pagination