C.10. Chapter 10

C.10.1. Exercises

C.10.1.1. Examine the KHTMLWidget reference documentation. Modify KSimpleBrowser to turn on Java applet and JavaScript support. Try it out. See Listings C.31 and C.32 for the answers.
C.10.1.2. Load an image file into a QImage instance and perform the following image transformation on an 8-bit color image (try one of the images in $KDEDIR/share/wallpapers): Replace each color in the color table (accessed via QImage::color()), with

C.10.1.1. Examine the KHTMLWidget reference documentation. Modify KSimpleBrowser to turn on Java applet and JavaScript support. Try it out. See Listings C.31 and C.32 for the answers.

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


Example C.32. ksimplebrowser.cpp—Class Definition for KSimpleBrowser

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

C.10.1.2. Load an image file into a QImage instance and perform the following image transformation on an 8-bit color image (try one of the images in $KDEDIR/share/wallpapers): Replace each color in the color table (accessed via QImage::color()), with


   1 
   2    qRgb (qGray (color), qGray (color), qGray (color));
   3 Display the image.
   4 

See Listings C.33 and C.34 for possible answers.


Example C.33. ktransform.h—Class Declaration for KTransform

   1 
   2 #ifndef __KTRANSFORM_H__
   3 #define __KTRANSFORM_H__
   4 
   5 #include <qwidget.h>
   6 class QImage;
   7 
   8 /**
   9 * KTransform
  10  * Transform a color image to grayscale.
  11  **/
  12 class KTransform : public QWidget
  13 {
  14  public:
  15   KTransform  (const QString &filename,
  16            QWidget *parent, const char *name=0);
  17 
  18  protected:
  19     void paintEvent (QPaintEvent *);
  20 
  21  private:
  22   QImage *qimage;
  23 };
  24 
  25 #endif
  26 


Example C.34. ktransform.cpp—Class Declaration for KTransform

   1 
   2 #include <qimage.h>
   3 #include <qpainter.h>
   4 
   5 #include "ktransform.h"
   6 
   7 KTransform::KTransform (const QString &filename,
   8             QWidget *parent, const char *name=0) :
   9   QWidget (parent, name)
  10 {
  11   qimage = new QImage;
  12   qimage->load (filename);
  13 
  14   int i;
  15   for (i=0; i<qimage->numColors(); i++)
  16     {
  17       QRgb color = qimage->color (i);
  18       QRgb gray = qRgb (qGray (color), qGray (color), qGray (color));
  19       qimage->setColor (i, gray);
  20     }
  21 
  22 }
  23 void
  24 KTransform::paintEvent (QPaintEvent *)
  25 {
  26   QPainter qpainter (this);
  27 
  28   qpainter.drawImage (0, 0, *qimage);
  29 }
  30