123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225 |
- #include <QFile>
- #include <QColor>
- #include <QTextStream>
- #include <QDebug>
- #include <QStringList>
- #include "WorkArea.hpp"
- #include "StrengthLine.hpp"
- WorkArea::WorkArea (int screenSizeX, int screenSizeY, QWidget * parent):
- QWidget(parent)
- {
- this->screenSizeX = screenSizeX;
- this->screenSizeY = screenSizeY;
- readOnly = false;
- img = NULL;
- indexPointClicked = -1;
- }
- WorkArea::WorkArea (int screenSizeX, int screenSizeY, const std::string & filename, QWidget * parent):QWidget(parent)
- {
- this->screenSizeX = screenSizeX;
- this->screenSizeY = screenSizeY;
- readOnly = false;
- img = NULL;
- indexPointClicked = -1;
- loadImage(filename);
- }
- WorkArea::WorkArea (int screenSizeX, int screenSizeY, const std::string & imageFilename, const std::string & SLFilename, bool readOnly, QWidget * parent):QWidget(parent)
- {
- this->screenSizeX = screenSizeX;
- this->screenSizeY = screenSizeY;
- img = NULL;
- indexPointClicked = -1;
- loadImage(imageFilename);
- this->readOnly = readOnly;
- QFile file(QString::fromStdString (SLFilename));
- if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
- return;
-
- QTextStream in(&file);
- while (!in.atEnd()) {
- QString line = in.readLine();
- QStringList parts = line.split(" ");
- liste_points << new QPoint(parts[0].toInt(), parts[1].toInt());
- liste_points << new QPoint(parts[2].toInt(), parts[3].toInt());
- }
- file.close();
- }
- 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::loadSL (const std::string & filename)
- {
- liste_points.clear();
- QFile file(QString::fromStdString (filename));
- if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
- return;
-
- QTextStream in(&file);
- while (!in.atEnd()) {
- QString line = in.readLine();
- QStringList parts = line.split(" ");
- liste_points << new QPoint(parts[0].toInt(), parts[1].toInt());
- liste_points << new QPoint(parts[2].toInt(), parts[3].toInt());
- }
- file.close();
- }
- 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::setReadOnly(bool readOnly){
- this->readOnly = readOnly;
- }
- 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++)
- {
- if(readOnly==false)
- {
- 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(readOnly==false)
- {
- 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(readOnly==false)
- {
- if (indexPointClicked != -1)
- {
- liste_points[indexPointClicked]->setX (event->x ());
- liste_points[indexPointClicked]->setY (event->y ());
- }
- }
- repaint ();
- }
- void
- WorkArea::mouseReleaseEvent (QMouseEvent * event)
- {
- indexPointClicked = -1;
- }
|