#include #include #include #include "WorkArea.hpp" #include "StrengthLine.hpp" WorkArea::WorkArea (int screenSizeX, int screenSizeY, QWidget * parent): QWidget { parent} { this->screenSizeX = screenSizeX; this->screenSizeY = screenSizeY; indexPointClicked = -1; img = NULL; } WorkArea::~WorkArea () { } void WorkArea::loadImage (const std::string & filename) { img = new QImage (QImage (QString::fromStdString (filename)). scaled (screenSizeX, screenSizeY - this->geometry ().y (), Qt::KeepAspectRatio, Qt::FastTransformation)); liste_points.clear (); resize (img->width (), img->height ()); } void WorkArea::saveStrengthLine (const std::string & filename) { QFile file (QString::fromStdString (filename)); if (!file.open (QIODevice::WriteOnly | QIODevice::Text)) return; QTextStream out (&file); for (int i = 0; i < liste_points.size () / 2; i++) out << liste_points[i*2]->x () << " " << liste_points[i*2]->y () << " " << liste_points[i*2+1]->x () << " " << liste_points[i*2+1]->y () << "\n"; file.close (); } void WorkArea::paintEvent (QPaintEvent * event) { QPainter painter (this); paint (painter); } void WorkArea::paint (QPainter & painter) { if (img != NULL) painter.drawImage (0, 0, *img); for (int i = 0; i < liste_points.length (); i++) { painter.setBrush (QBrush (Qt::green)); painter.setPen (QPen (Qt::black)); painter.drawEllipse (liste_points[i]->x () - taillePoint / 2, liste_points[i]->y () - taillePoint / 2, taillePoint, taillePoint); if ((i % 2) == 1) { painter.setPen (QPen (Qt::red, epaisseurLigne)); std::pair < QPoint *, QPoint * >endPoints = StrengthLine (liste_points[i - 1], liste_points[i]).toDraw (img); painter.drawLine (*(std::get < 0 > (endPoints)), *(std::get < 1 > (endPoints))); } } } void WorkArea::mousePressEvent (QMouseEvent * event) { if (img != NULL) { // Est ce qu'on a cliqué sur un point existant ? indexPointClicked = -1; for (int i = 0; i < liste_points.length (); i++) { QPoint point = event->pos () - *(liste_points[i]); if (point.manhattanLength () < taillePoint) { indexPointClicked = i; } } // Clic gauche if (event->button () == Qt::LeftButton) { if (indexPointClicked == -1) { indexPointClicked = liste_points.size (); liste_points << new QPoint (event->x (), event->y ()); } } // Clic droit if (event->button () == Qt::RightButton) { if (indexPointClicked != -1) { if (indexPointClicked == (liste_points.size () - 1)) liste_points.removeAt (indexPointClicked); else { if ((indexPointClicked % 2) == 0) { QPoint *single = liste_points.takeAt (indexPointClicked + 1); liste_points.removeAt (indexPointClicked); liste_points << single; } else { QPoint *single = liste_points.takeAt (indexPointClicked - 1); liste_points.removeAt (indexPointClicked - 1); liste_points << single; } } indexPointClicked = -1; } } } repaint (); } void WorkArea::mouseMoveEvent (QMouseEvent * event) { if (indexPointClicked != -1) { liste_points[indexPointClicked]->setX (event->x ()); liste_points[indexPointClicked]->setY (event->y ()); } repaint (); } void WorkArea::mouseReleaseEvent (QMouseEvent * event) { indexPointClicked = -1; }