11.3. Panel Applets
Panel applets are small applications with minimal user interfaces that run in the KDE panel, Kicker. They generally indicate the status of some part of the system, such as CPU load, network activity, the system time, or provide easy access to functions that are not directly related to the work being performed by the user, such as a desktop pager, a CD player controller, or an instant-messaging client. For creating your own panel applet, the KDE libraries provide the class KPanelApplet. It is derived from QWidget (and DCOPObject) and takes the place of KTMainWindow in the design of your application. Listing 11.7 shows our usual main() function modified to create an applet.
Example 11.7. main.cpp: A main() Function Suitable for Starting a Panel applet
This applet, the subclass of KPanelApplet, is called KWeather. KWeather is a mock-up of an applet that displays the outside weather, say, to a graduate student trapped in a windowless office. A complete implementation of KWeather might query a weather service via HTTP (using KIONetAccess) to determine the actual weather. In this implementation the weather is always rainy and 48°F so that the graduate student won't feel that he is missing out on a nice day.
Listing 11.8 shows how the KWeather class is implemented.
Example 11.8. kweather.cpp: Class Definition for KWeather, a Panel Applet
In the constructor, you create your content area, a QLabel. Be sure when you design your content area that it will fit comfortably in the small area given to it by Kicker, the KDE panel. The content area here consists of two lines of text on a colored background. It fits nicely. Generally, a well-designed icon can convey more information in the small space—or at least convey it in a more appealing way.
The call on line 16 to setMinimumSize() keeps Kicker from shrinking the widget so that the text is too small to read. Kicker is trying to minimize usage of the valuable panel space, so be sure to set a minimum size for your widget.
KPanelApplet provides a context menu (a pop-up menu that appears when the user clicks the applet with the right mouse button, also known as an "RMB menu"). This menu provides the minimum of choices to the user: Move and Remove, which allow the user to, respectively, move the applet along the panel or remove from the panel, thus exiting the applet.You may add other menu entries—About, Help, and Preferences—using the method setActions(), as on line 18. The enum constants About, Help and Preferences may be combined with the bitwis-or operator (i.e., &) and passed to setActions() to add any combination of these menu entries. To respond to the user's selection of one of these menu entries, you should reimplement the corresponding virtual method: about(), help(), or preferences(). The latter is reimplemented, as an example, on lines 23-27.
The final bit of code needed for KWeather is given in Listing 11.9.
Example 11.9. kweather.h: Class Declaration for KWeather