X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/graphlib.git/blobdiff_plain/ceee0bef6e7c4c0e07ee9ef60e254f19bb15156a..c05e66a47050a15645c8d9398b90053d6c1f018e:/DrawingWindow.cpp diff --git a/DrawingWindow.cpp b/DrawingWindow.cpp index ad74339..78a6296 100644 --- a/DrawingWindow.cpp +++ b/DrawingWindow.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2007-2010, Arnaud Giersch + * Copyright (c) 2007-2013, Arnaud Giersch * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -36,8 +36,8 @@ /*! \class DrawingWindow * \brief Fenêtre de dessin. * - * \author Arnaud Giersch - * \date 2007-2010 + * \author Arnaud Giersch + * \date 2007-2013 * * Cette classe décrit un widget Qt permettant d'écrire des * applications graphiques simples. Pour cela, il faut définir une @@ -433,10 +433,14 @@ void DrawingWindow::drawPoint(int x, int y) */ void DrawingWindow::drawLine(int x1, int y1, int x2, int y2) { - safeLock(imageMutex); - painter->drawLine(x1, y1, x2, y2); - dirty(x1, y1, x2, y2); - safeUnlock(imageMutex); + if (x1 == x2 && y1 == y2) { + drawPoint(x1, y1); + } else { + safeLock(imageMutex); + painter->drawLine(x1, y1, x2, y2); + dirty(x1, y1, x2, y2); + safeUnlock(imageMutex); + } } //! Dessine un rectangle. @@ -452,14 +456,18 @@ void DrawingWindow::drawLine(int x1, int y1, int x2, int y2) */ void DrawingWindow::drawRect(int x1, int y1, int x2, int y2) { - QRect r; - r.setCoords(x1, y1, x2 - 1, y2 - 1); - r = r.normalized(); - safeLock(imageMutex); - painter->drawRect(r); - r.adjust(0, 0, 1, 1); - dirty(r); - safeUnlock(imageMutex); + if (x1 == x2 && y1 == y2) { + drawPoint(x1, y1); + } else { + QRect r; + r.setCoords(x1, y1, x2 - 1, y2 - 1); + r = r.normalized(); + safeLock(imageMutex); + painter->drawRect(r); + r.adjust(0, 0, 1, 1); + dirty(r); + safeUnlock(imageMutex); + } } //! Dessine un rectangle plein. @@ -657,11 +665,11 @@ bool DrawingWindow::waitMousePress(int &x, int &y, int &button, unsigned long time) { bool pressed; - safeLock(mouseMutex); + safeLock(inputMutex); if (terminateThread) { pressed = false; } else { - pressed = mouseCondition.wait(&mouseMutex, time); + pressed = inputCondition.wait(&inputMutex, time) && !terminateThread; if (pressed) { x = mousePos.x(); y = mousePos.y(); @@ -675,7 +683,7 @@ bool DrawingWindow::waitMousePress(int &x, int &y, int &button, button = 0; } } - safeUnlock(mouseMutex); + safeUnlock(inputMutex); return pressed; } @@ -737,15 +745,18 @@ void DrawingWindow::usleep(unsigned long usecs) DrawingThread::usleep(usecs); } +//--- DrawingWindow (protected methods) -------------------------------- +//! \cond show_protected + /*! * \see QWidget */ void DrawingWindow::closeEvent(QCloseEvent *ev) { timer.stop(); - thread->terminate(); + thread->exit(); syncMutex.lock(); - mouseMutex.lock(); + inputMutex.lock(); terminateThread = true; // this flag is needed for the case // where the following wakeAll() call // occurs between the @@ -753,11 +764,14 @@ void DrawingWindow::closeEvent(QCloseEvent *ev) // mutex lock in safeLock() called // from sync() syncCondition.wakeAll(); - mouseCondition.wakeAll(); - mouseMutex.unlock(); + inputCondition.wakeAll(); + inputMutex.unlock(); syncMutex.unlock(); QWidget::closeEvent(ev); - thread->wait(); + if (!thread->wait(250)) { + thread->terminate(); + thread->wait(); + } } /*! @@ -786,12 +800,12 @@ void DrawingWindow::customEvent(QEvent *ev) */ void DrawingWindow::mousePressEvent(QMouseEvent *ev) { - mouseMutex.lock(); + inputMutex.lock(); mousePos = ev->pos(); mouseButton = ev->button(); ev->accept(); - mouseCondition.wakeAll(); - mouseMutex.unlock(); + inputCondition.wakeAll(); + inputMutex.unlock(); } /*! @@ -799,17 +813,10 @@ void DrawingWindow::mousePressEvent(QMouseEvent *ev) */ void DrawingWindow::keyPressEvent(QKeyEvent *ev) { - bool accept = true; - switch (ev->key()) { - case Qt::Key_Escape: + if (ev->key() == Qt::Key_Escape) { + ev->accept(); close(); - break; - default: - accept = false; - break; } - if (accept) - ev->accept(); } /*! @@ -850,6 +857,8 @@ void DrawingWindow::timerEvent(QTimerEvent *ev) } } +// \endcond + //--- DrawingWindow (private methods) ---------------------------------- //! Fonction d'initialisation.