From: giersch Date: Wed, 20 Jan 2010 17:52:15 +0000 (+0000) Subject: Added: DrawingWindo::waitMousePress() X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/graphlib.git/commitdiff_plain/984344660cf8dad0c54bd4aefba2a034c144da3c Added: DrawingWindo::waitMousePress() --- diff --git a/DrawingWindow.cpp b/DrawingWindow.cpp index 4da3e82..e5556c4 100644 --- a/DrawingWindow.cpp +++ b/DrawingWindow.cpp @@ -37,7 +37,7 @@ * \brief Fenêtre de dessin. * * \author Arnaud Giersch - * \date novembre 2007 + * \date 2007-2010 * * Cette classe décrit un widget Qt permettant d'écrire des * applications graphiques simples. Pour cela, il faut définir une @@ -95,7 +95,7 @@ * * 4. Exécuter le programme avec la commande : * - * \verbatim ./exemple \endverbatim + * \verbatim ./hello \endverbatim * * Code source de l'exemple */ @@ -534,6 +534,43 @@ unsigned int DrawingWindow::getPointColor(int x, int y) return image->pixel(x, y); } +//! Attend l'appui sur un des boutons de la souris. +/*! + * Attend l'appui sur un des boutons de la souris. Retourne le bouton + * qui a été pressé et les coordonnées du pointeur de souris à ce + * moment-là. + * + * \param x, y coordonnées du pointeur de souris + * \param button numéro du bouton qui a été pressé + * \param time durée maximale de l'attente + * \return true si un bouton a été pressé + */ +bool DrawingWindow::waitMousePress(int &x, int &y, int &button, + unsigned long time) +{ + bool pressed; + safeLock(mouseMutex); + if (terminateThread) { + pressed = false; + } else { + pressed = mouseCondition.wait(&mouseMutex, time); + if (pressed) { + x = mousePos.x(); + y = mousePos.y(); + if (mouseButton & Qt::LeftButton) + button = 1; + else if (mouseButton & Qt::RightButton) + button = 2; + else if (mouseButton & Qt::MidButton) + button = 3; + else + button = 0; + } + } + safeUnlock(mouseMutex); + return pressed; +} + //! Synchronise le contenu de la fenêtre. /*! * Pour des raisons d'efficacités, le résultat des fonctions de dessin @@ -600,6 +637,7 @@ void DrawingWindow::closeEvent(QCloseEvent *ev) timer.stop(); thread->terminate(); syncMutex.lock(); + mouseMutex.lock(); terminateThread = true; // this flag is needed for the case // where the following wakeAll() call // occurs between the @@ -607,6 +645,8 @@ void DrawingWindow::closeEvent(QCloseEvent *ev) // mutex lock in safeLock() called // from sync() syncCondition.wakeAll(); + mouseCondition.wakeAll(); + mouseMutex.unlock(); syncMutex.unlock(); QWidget::closeEvent(ev); thread->wait(); @@ -631,6 +671,19 @@ void DrawingWindow::customEvent(QEvent *ev) } } +/*! + * \see QWidget + */ +void DrawingWindow::mousePressEvent(QMouseEvent *ev) +{ + mouseMutex.lock(); + mousePos = ev->pos(); + mouseButton = ev->button(); + ev->accept(); + mouseCondition.wakeAll(); + mouseMutex.unlock(); +} + /*! * \see QWidget */ diff --git a/DrawingWindow.h b/DrawingWindow.h index d1c174b..732183a 100644 --- a/DrawingWindow.h +++ b/DrawingWindow.h @@ -57,6 +57,8 @@ public: unsigned int getPointColor(int x, int y); + bool waitMousePress(int &x, int &y, int &button, + unsigned long time = ULONG_MAX); bool sync(unsigned long time = ULONG_MAX); void closeGraph(); @@ -68,6 +70,7 @@ public: protected: void closeEvent(QCloseEvent *ev); void customEvent(QEvent *ev); + void mousePressEvent(QMouseEvent *ev); void keyPressEvent(QKeyEvent *ev); void paintEvent(QPaintEvent *ev); void showEvent(QShowEvent *ev); @@ -79,6 +82,8 @@ private: QBasicTimer timer; QMutex imageMutex; + QMutex mouseMutex; + QWaitCondition mouseCondition; QMutex syncMutex; QWaitCondition syncCondition; bool terminateThread; @@ -87,6 +92,9 @@ private: QImage *image; QPainter *painter; + QPoint mousePos; + Qt::MouseButton mouseButton; + bool dirtyFlag; QRect dirtyRect;