Chapter 10. Complex-Function KDE Widgets

by David Sweet

In This Chapter

Among the KDE/Qt libraries are classes that support complex functions. Using these classes can help reduce the development time of your application and can make your application function in a way more consistent with other KDE applications.

In this chapter we will discuss the following complex functions that are provided by the KDE/Qt libraries:

10.1. Rendering HTML Files

Both konqueror and khelpcenter need to display HTML pages. konqueror generates HTML and acts as a Web browser, displaying HTML pages fetched from remote machines or stored locally. The KDE Help files displayed by khelpcenter are in HTML format (see Chapter 15, "Creating Documentation") and need to be rendered to be viewed.

Long ago, it was realized that a single HTML-rendering class could do the job for both applications, and KHTMLWidget was born. It has since been completely rewritten and now supports HTML 4.0, Java applets, JavaScript, and cascading style sheets (CSS1 and some of CSS2) by the time KDE 2.0 is released.

Currently, KHTMLWidget is used by KMail and KRN for rendering HTML emails and USENET news articles.

10.1.1. A Simple Web Browser

To show how KHTMLWidget can be used in an application, let's construct a simple Web browser. When the application—which we'll call (consistently unimaginatively) KSimpleBrowser—starts, it displays a short HTML page of instructions telling how to use the application. Then the user can enter the URL of a Web page, such as http://www.kde.org, and press Enter to view the page.


Example 10.1. ksimplebrowser.h: Class Declaration for KSimpleBrowser, a Simple Web Browser

   1 
   2  1: #ifndef __KSIMPLEBROWSER_H__
   3  2: #define __KSIMPLEBROWSER_H__
   4  3:
   5  4: #include <ktmainwindow.h>
   6  5:
   7  6: class KHTMLPart;
   8  7:
   9  8: /**
  10  9:  * KSimpleBrowser
  11 10:  * A feature-limited Web browser.
  12 11:  **/
  13 12: class KSimpleBrowser : public KTMainWindow
  14 13: {
  15 14:  Q_OBJECT
  16 15:  public:
  17 16:   KSimpleBrowser (const char *name=0);
  18 17:
  19 18:  public slots:
  20 19:   void slotNewURL ();
  21 20:
  22 21:  protected:
  23 22:   KHTMLPart *khtmlpart;
  24 23: };
  25 24:
  26 25: #endif

KSimpleBrowser is derived from KTMainWindow. This allows us to add a toolbar containing a line editor for entering a URL (see Listing 10.2, lines 10–13) and place an instance of KHTMLPart in our content area (see Listing 10.2, lines 16–24) and have it all managed by KTMainWindow.


Example 10.2. ksimplebrowser.cpp: Class Definition for KSimpleBrowser

   1 
   2  1: #include <khtmlview.h>
   3  2: #include <khtml_part.h>
   4  3:
   5  4: #include "ksimplebrowser.moc"
   6  5:
   7  6: const int URLLined = 1;
   8  7: KSimpleBrowser::KSimpleBrowser (const char *name=0) :
   9  8:   KTMainWindow (name)
  10  9: {
  11 10:
  12 11:   toolBar()->insertLined ( "", URLLined,
  13 12:                SIGNAL (returnPressed ()),
  14 13:                this, SLOT (slotNewURL ()) );
  15 14:   toolBar()->setItemAutoSized (URLLined);
  16 15:
  17 16:
  18 17:   khtmlpart = new KHTMLPart (this);
  19 18:   khtmlpart->begin();
  20 19:   khtmlpart->write("<HTML><BODY><H1>KSimpleBrowser</H1>"
  21 20:              "<P>To load a web page, type its URL in the line "
  22 21:              "edit box and press enter.</P>"
  23 22:              "</BODY></HTML>");
  24 23:   khtmlpart->end();
  25 24:
  26 25:   setView (khtmlpart->view());
  27 26: }
  28 27:
  29 28: void
  30 29: KSimpleBrowser::slotNewURL  ()
  31 30: {
  32 31:   khtmlpart->openURL (toolBar()->getLinedText (URLLined));
  33 32: }

Short isn't it? If you compile and execute this code (using the main() function given in Listing 10.3) you'll find that you can view fully rendered HTML pages (including images, tables, and frames) and follow links.

In KSimpleBrowser, you use two methods of HTML rendering. The first is to create our HTML on-the-fly. The sequence of statements in lines 17–23 of Listing 10.2 tells khtmlpart (an instance of KHTMLPart) to render the HTML page specified by HTML-marked text. You may call write() multiple times before calling end(), but the fewer calls you make, the faster the rendering process will be. The second way to get HTML pages rendered is to instruct khtmlwpart to open a URL. The URL may be of type file://, http://, ftp://, or any type that points to a valid HTML page. This means that you can load local or remote files using the same techniques. The method KHTMLPart::openURL(), used in line 30 of Listing 10.2, loads the Web page. This method returns immediately while the rendering continues in the background.

When compiling Listings 10.1–10.3, you need to pass the option -lkhtml to g++. This tells g++ to link the program against libkhtml, the library that contains KHTMLPart. Figure 10.1 shows KSimpleBrowser displaying the initial instructions page.


Example 10.3. main.cpp: A main() Function that Creates and Executes KSimpleBrowser

   1 
   2  1: #include <kapp.h>
   3  2:
   4  3: #include "ksimplebrowser.h"
   5  4:
   6  5: int
   7  6: main (int argc, char *argv[])
   8  7: {
   9  8:   KApplication kapplication (argc, argv, "ksimplebrowser");
  10  9:
  11 10:   KSimpleBrowser *ksimplebrowser  = new KSimpleBrowser;
  12 11:
  13 12:   ksimplebrowser->show();
  14 13:
  15 14:   return kapplication.exec();
  16 15: }


Figure 10.1. KSimpleBrowser displaying the initial instructions page.