/*
- * Copyright (c) 2007-2010, Arnaud Giersch <arnaud.giersch@iut-bm.univ-fcomte.fr>
+ * Copyright (c) 2007-2013, Arnaud Giersch <arnaud.giersch@univ-fcomte.fr>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
/*! \class DrawingWindow
* \brief Fenêtre de dessin.
*
- * \author Arnaud Giersch <arnaud.giersch@iut-bm.univ-fcomte.fr>
- * \date 2007-2010
+ * \author Arnaud Giersch <arnaud.giersch@univ-fcomte.fr>
+ * \date 2007-2013
*
* Cette classe décrit un widget Qt permettant d'écrire des
* applications graphiques simples. Pour cela, il faut définir une
{ }
};
-//! Demande de tracé de texte.
+//! Demande de tracé de texte.
class DrawTextEvent: public QEvent {
public:
const int x;
setBgColor(QColor::fromRgbF(red, green, blue));
}
+//! Change l'épaisseur du pinceau
+/*!
+ * Le pinceau à une épaisseur de 1 par défaut.
+ *
+ * \param width épaisseur du pinceau
+ */
+void DrawingWindow::setPenWidth(int width)
+{
+ QPen pen(painter->pen());
+ pen.setWidth(width);
+ painter->setPen(pen);
+}
+
//! Retourne la fonte courante utilisée pour dessiner du texte.
/*!
* \see QFont, setFont
painter->setFont(font);
}
+//! Active ou non l'antialiasing.
+/*!
+ * Permet de lisser le dessin.
+ * Fonctionnalité désactivée par défaut.
+ *
+ * \param state état de l'antialiasing
+ *
+ * \bug expérimental
+ */
+void DrawingWindow::setAntialiasing(bool state)
+{
+ painter->setRenderHint(QPainter::Antialiasing, state);
+}
+
//! Efface la fenêtre.
/*!
* La fenêtre est effacée avec la couleur de fond courante.
*/
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.
*/
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.
* \param text texte à écrire
* \param flags paramètres d'alignement
*
+ * \see drawText(int, int, const std::string &, int)
* \see drawTextBg, setColor
* \see QPainter::drawText
*/
safeUnlock(syncMutex);
}
+//! Écrit du texte.
+/*!
+ * \see drawText(int, int, const char *, int)
+ */
+void DrawingWindow::drawText(int x, int y, const std::string &text, int flags)
+{
+ drawText(x, y, text.c_str(), flags);
+}
+
//! Écrit du texte sur fond coloré.
/*!
* Écrit du texte comme drawText, mais l'arrière-plan est coloré avec
* \param text texte à écrire
* \param flags paramètres d'alignement
*
+ * \see drawTextBg(int, int, const std::string &, int)
* \see drawText, setColor, setColorBg
*/
void DrawingWindow::drawTextBg(int x, int y, const char *text, int flags)
painter->setBackgroundMode(Qt::TransparentMode);
}
+//! Écrit du texte sur fond coloré.
+/*!
+ * \see drawTextBg(int, int, const char *, int)
+ */
+void DrawingWindow::drawTextBg(int x, int y, const std::string &text, int flags)
+{
+ drawTextBg(x, y, text.c_str(), flags);
+}
+
//! Retourne la couleur d'un pixel.
/*!
* Retourne la couleur du pixel de coordonnées (x, y). La valeur
*
* \see setColor(unsigned int), setBgColor(unsigned int)
*/
-unsigned int DrawingWindow::getPointColor(int x, int y)
+unsigned int DrawingWindow::getPointColor(int x, int y) const
{
return image->pixel(x, y);
}
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();
button = 0;
}
}
- safeUnlock(mouseMutex);
+ safeUnlock(inputMutex);
return pressed;
}
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
// 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();
+ }
}
/*!
*/
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();
}
/*!
*/
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();
}
/*!
}
}
+// \endcond
+
//--- DrawingWindow (private methods) ----------------------------------
//! Fonction d'initialisation.