Development/Tutorials/Plasma4/RubyApplet (ru)


Этот туториал покажет как создать свой собственный Plasma Виджет/Апплет/Плазмоид используя скриптовый язык программирования Ruby.

Смотрите также:

Файл desktop

Файл desktop является входной точкой нашего апплета. Он определяет для чего этот апплет, как он должен быть загружен и управляться plasma.

Для нашего апплета ВебБраузера, мы используем файл plasma-ruby-applet-web.desktop, который содержит:

[Desktop Entry]
Name=Ruby Web Browser




The important parts within those desktop service file are the referenced X-KDE-Library which will be loaded on request and is responsible for executing our with X-KDE-PluginKeyword defined Ruby scrip file. Our applet will be known by Plasma under the unique name "plasma-ruby-web-applet".

The Ruby Script

It follows the Ruby scripting code which does implement the whole functionality to display a webbrowser within the applet and to browse around those content.

The web_applet.rb ruby script looks like;

# import the plasma-applet functionality which we need to
# implement our own webbrowser applet.
require 'plasma_applet'

# We define an own module
module PlasmaRubyWebApplet

  # The WebApplet class does implement a Plasma::Applet.
  class WebApplet < Plasma::Applet

    # The both slots used by our applet.
    slots 'load(QUrl)',

    # Constructor for our WebApplet class.
    def initialize(parent, args)

    # On initialization this method will be called by plasma. Here we
    # are able to do expensive initialization work to show something
    # within our applet.
    def init
      # Let's start with a nice default size.
      resize(600, 400)

      # Create the Browser widget which will display the content
      # of webpages.
      @page = = = Qt::WebPage::DelegateAllLinks
        Qt::WebSettings::LinksIncludedInFocusChain, true)

      # Connect signals the webbrowser provides with own functions.
      connect(@page, SIGNAL('loadFinished(bool)'),
              self, SLOT('loadFinished(bool)'))
      connect(, SIGNAL('linkClicked(QUrl)'),
              self, SLOT('load(QUrl)'))

      # Start browsing some webpage
      @page.url ="")

    # This method will be called once the user clicks on a link.
    # We just load the defined URL-address and let the
    # webbrowser-page display the content.
    def load(url)
      puts "Loading #{url.toString}"
      @page.url = url

    # This method will be called once loading a page finished.
    def loadFinished(success)
      puts "page loaded #{}"

    # This method will be called each time a constraint changed. the
    # for us most interesting constraints are;
    # * LocationConstraint if the location of the applet changed
    # * ScreenConstraint if the screen on which the applet is located
    #   on changed
    # * SizeConstraint if the size of the applet changed
    # * ImmutableConstraint if the applet got locked/unlocked
    def constraintsEvent(constraints)
      if constraints.to_i & Plasma::SizeConstraint.to_i
        # if the size of the Applet changed, then resize also the
        # webbrowser.


That was already all the code needed to implement our own webbrowser Applet using the Ruby language. The in this sample used webbrowser does provide additional functionality to use optional a full powered webbrowser or adopt it to your needings.

Install and test the Applet

To install the Applet the CMakeLists.txt is used. It basically does copy the both files above to the right locations. Translated to command-line arguments it would look like;

sudo cp plasma-ruby-applet-web.desktop `kde4-config --install services`
sudo mkdir `kde4-config --install data`/plasma_ruby_web_applet/
sudo cp web_applet.rb `kde4-config --install data`/plasma_ruby_web_applet/

what installs the both files to the global installation directory to make hem available for all users on your sysem. If you like to have them accessible only from within one user, then just copy those both files to the users $KDEHOME (the command "kde4-config --localprefix" should provide you a hint where that may be).

To take a look at your applet or test it during development, you can;

plasmoidviewer plasma-ruby-web-applet

where the kbuildsycoca4 does rebuild the desktop-cache to update changes done to the desktop file. The plasmoidviewer is a tool that allows you to start and view your applet outside of plasma. Media:Example.ogg

This page was last edited on 11 September 2014, at 23:27. Content is available under Creative Commons License SA 4.0 unless otherwise noted.