#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QObject>
#include <QWidget>
#include <QVector>
#include <QMouseEvent>
#include <QGraphicsView>
#include <QGraphicsScene>
#include <QGraphicsSceneMouseEvent>
#include <QGraphicsItem>

QColor color = Qt::red;

bool dragging = false;

class myImgViewer : public QGraphicsView
{
public:
	myImgViewer(QWidget *parent);
protected:
	void mouseDoubleClickEvent(QMouseEvent *event);
	void mouseMoveEvent(QMouseEvent *event);
};


	myImgViewer *myImgView;	


	QGraphicsScene *myGraphicsScene;
	

myImgViewer::myImgViewer(QWidget *parent)
: QGraphicsView(parent)
{}



class HotSpot : public QGraphicsItem
{
public:
	HotSpot();
	QRectF boundingRect() const;
	void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
protected:
	void mousePressEvent(QGraphicsSceneMouseEvent *event);
	void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
};



	HotSpot::HotSpot() {}


QRectF HotSpot::boundingRect() const
{
	return QRectF(0,0,20,20);
}

void HotSpot::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
	Q_UNUSED(option);
	Q_UNUSED(widget);
	painter->setPen(QPen(color, 2));
	painter->drawRect(0,0,20,20);
}



	QVector<HotSpot*> mySpots;


class myWindow : public QWidget
{
Q_OBJECT
public:
	myWindow(void);
};


myWindow::myWindow(void) {

	this->resize(400, 400);
	myImgView = new myImgViewer( this );
	myImgView->setGeometry(10, 40, 380, 340);
	myGraphicsScene = new QGraphicsScene( this );
	myImgView->setScene(myGraphicsScene);
}


void HotSpot::mousePressEvent(QGraphicsSceneMouseEvent *event) {

	color = Qt::red;
	for (int i = 0; i < mySpots.count(); i++) {
		mySpots.at(i)->update();
	}
	color = Qt::yellow;
	mySpots.last()->update();
	if (event->buttons() & Qt::LeftButton) {
		dragging = true;
	}

}


void HotSpot::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {

	dragging = false;
}


void myImgViewer::mouseMoveEvent(QMouseEvent *event) {
	if (dragging == true) {

	/// SNIP ///

	}

}


void myImgViewer::mouseDoubleClickEvent(QMouseEvent *event) {

	mySpots.append( new HotSpot() );
        mySpots.last()->setPos(event->x(), event->y());
        myGraphicsScene->addItem(mySpots.last());

}

#include "moc_mainwindow.cpp"
#endif
