C.7. Chapter 7

C.7.1. Exercises

C.7.1.1. Starting with KDropDemo as a base, write a program that accepts drops of images. Use QImageObject instead of QTextObject.
C.7.1.2. Now, using KDragDemo as a base, write a program that lets the user drag a pixmap to another application. You can use a QPixmap returned by BarIcon() as the data for the drag.
C.7.1.3. Look up KAudio in the KDE class documentation. Using KStandardDirs and KAudio, locate and play one of the sounds distributed with KDE. (The sounds are in $KDEDIR/share/sounds.) See Listings C.23–C.25.

C.7.1.1. Starting with KDropDemo as a base, write a program that accepts drops of images. Use QImageObject instead of QTextObject.

Only kdropdemo.cpp was modified. See Listing C.20.


Example C.20. Modified kdropdemo.cpp

   1 
   2 #include <qdragobject.h>
   3 
   4 #include "kdropdemo.h"
   5 
   6 KDropDemo::KDropDemo (QWidget *parent, const char *name) :
   7   QLabel (parent, name)
   8 {
   9   setAcceptDrops(true);
  10 
  11   //Chapter 7, Exercise 1
  12   setAlignment (AlignCenter);
  13   setText ("Drop\nan\n     image     \non\n me!");
  14 }
  15 
  16 void
  17 KDropDemo::dragEnterEvent (QDragEnterEvent *qdragenterevent)
  18 {
  19   //Chapter 7, Exercise 1
  20   qdragenterevent->accept (QImageDrag::canDecode (qdragenterevent));
  21 }
  22 
  23 void
  24 KDropDemo::dropEvent (QDropEvent *qdropevent)
  25 {
  26   //Chapter 7, Exercise 1
  27   QPixmap qpixmap;
  28 
  29   if (QImageDrag::decode (qdropevent, qpixmap))
  30     {
  31       setPixmap (qpixmap);
  32     }
  33 }
  34 

C.7.1.2. Now, using KDragDemo as a base, write a program that lets the user drag a pixmap to another application. You can use a QPixmap returned by BarIcon() as the data for the drag.

kdragdemo.h and kdragdemo.cpp were modified (see Listings C.21 and C.22). You can use main.cpp, given in Listing 7.3.


Example C.21. Modified kdragdemo.h

   1 
   2 #ifndef __KDRAGDEMO_H__
   3 #define __KDRAGDEMO_H__
   4 
   5 
   6 #include <qlabel.h>
   7 
   8 //Chapter 7, Exercise 2
   9 class QImage;
  10 
  11 /**
  12  * KDragDemo
  13  *
  14  **/
  15 class KDragDemo : public QLabel
  16 {
  17  public:
  18   KDragDemo (QWidget *parent, const char *name=0);
  19 
  20  protected:
  21   bool dragging;
  22   //Chapter 7, Exercise 2
  23   QImage *qimage;
  24 
  25   void mouseMoveEvent (QMouseEvent *qmouseevent);
  26   void mouseReleaseEvent (QMouseEvent *qmouseevent);
  27 };
  28 
  29 #endif
  30 


Example C.22. Modified kdragdemo.cpp

   1 
   2 #include <qdragobject.h>
   3 #include <qimage.h>
   4 
   5 #include <kiconloader.h>
   6 #include "kdragdemo.h"
   7 
   8 KDragDemo::KDragDemo (QWidget *parent, const char *name) :
   9   QLabel (parent, name)
  10 {
  11   dragging = false;
  12 
  13 //Chapter 7, Exercise 2
  14   QPixmap qpixmap;
  15   qpixmap = BarIcon ("exit");
  16   setPixmap (qpixmap);
  17 
  18   qimage = new QImage;
  19   *qimage = qpixmap;
  20 
  21 }
  22 
  23 
  24 void
  25 KDragDemo::mouseMoveEvent (QMouseEvent *qmouseevent)
  26 {
  27   if (!dragging &&qmouseevent->state() == Qt::LeftButton)
  28     {
  29       dragging = true;
  30 //Chapter 7, Exercise 2
  31       QImageDrag *qimagedrag = new QImageDrag (*qimage, this);
  32       qimagedrag->dragCopy();
  33     }
  34 }
  35 
  36 void
  37 KDragDemo::mouseReleaseEvent (QMouseEvent *)
  38 {
  39   dragging = false;
  40 }
  41 

C.7.1.3. Look up KAudio in the KDE class documentation. Using KStandardDirs and KAudio, locate and play one of the sounds distributed with KDE. (The sounds are in $KDEDIR/share/sounds.) See Listings C.23–C.25.

   1 
   2 #ifndef __KPLAYSOUND_H__
   3 #define __KPLAYSOUND_H__
   4 
   5 
   6 #include <qlabel.h>
   7 
   8 class KAudio;
   9 
  10 /**
  11  * KPlaySound
  12  *
  13  **/
  14 class KPlaySound : public QLabel
  15 {
  16  Q_OBJECT
  17 
  18  public:
  19   KPlaySound (QWidget *parent, const char *name=0);
  20 
  21  protected slots:
  22    /**
  23     * The sound is done playing.
  24     **/
  25    void slotPlayFinished();
  26  protected:
  27    KAudio *kaudio;
  28 };
  29 
  30 #endif
  31 


Example C.24. kplaysound.cpp

   1 
   2 #include <kaudio.h>
   3 #include <kstddirs.h>
   4 
   5 #include "kplaysound.moc"
   6 
   7 
   8 KPlaySound::KPlaySound (QWidget *parent, const char *name) :
   9   QLabel (parent, name)
  10 {
  11 
  12   kaudio = new KAudio;
  13   connect ( kaudio, SIGNAL (playFinished()),
  14             this, SLOT (slotPlayFinished()) );
  15 
  16   KStandardDirs *dirs = KGlobal::dirs();
  17 
  18   QString soundpath;
  19   soundpath = dirs->findResource ("sound", "KDE_Startup.wav");
  20 
  21   kaudio->play (soundpath);
  22 }
  23 
  24 void
  25 KPlaySound::slotPlayFinished()
  26 {
  27   //playing has finished
  28 }
  29 


Example C.25. main.cpp

   1 
   2 #include <kapp.h>
   3 
   4 #include "kplaysound.h"
   5 int main (int argc, char *argv[])
   6 {
   7   KApplication kapplication (argc, argv, "kplaysoundtest");
   8   KPlaySound kplaysound (0);
   9 
  10   kplaysound.show();
  11   kapplication.setMainWidget (&kplaysound);
  12   kapplication.exec();
  13 }
  14