Fix the hotspots

This commit is contained in:
ArsenArsen 2017-09-08 23:47:56 +02:00
parent 7ec7b47249
commit 423383dca9
5 changed files with 37 additions and 22 deletions

View File

@ -246,7 +246,7 @@ void CropScene::mouseMoveEvent(QGraphicsSceneMouseEvent *e) {
cursorItem->setPos(cursorPos);
updateMag();
if (rect) {
if (rect && !drawingRect) {
// qAbs(e->scenePos().<axis>() - rect->rect().<edge>()) < 10
bool close = false;
QRectF newRect = rect->rect();
@ -254,21 +254,21 @@ void CropScene::mouseMoveEvent(QGraphicsSceneMouseEvent *e) {
if (qAbs(e->scenePos().y() - rect->rect().bottom()) < 10) {
close = true;
views()[0]->setCursor(Qt::SizeFDiagCursor);
if (e->buttons() & Qt::LeftButton && prevButtons != e->buttons()) newRect.setBottomRight(cursorPos);
if (e->buttons() & Qt::LeftButton) newRect.setBottomRight(cursorPos);
} else if (qAbs(e->scenePos().y() - rect->rect().top()) < 10) {
close = true;
views()[0]->setCursor(Qt::SizeBDiagCursor);
if (e->buttons() & Qt::LeftButton && prevButtons != e->buttons()) newRect.setTopRight(cursorPos);
if (e->buttons() & Qt::LeftButton) newRect.setTopRight(cursorPos);
}
} else if (qAbs(e->scenePos().x() - rect->rect().left()) < 10) {
if (qAbs(e->scenePos().y() - rect->rect().top()) < 10) {
close = true;
views()[0]->setCursor(Qt::SizeFDiagCursor);
if (e->buttons() & Qt::LeftButton && prevButtons != e->buttons()) newRect.setTopLeft(cursorPos);
if (e->buttons() & Qt::LeftButton) newRect.setTopLeft(cursorPos);
} else if (qAbs(e->scenePos().y() - rect->rect().bottom()) < 10) {
close = true;
views()[0]->setCursor(Qt::SizeBDiagCursor);
if (e->buttons() & Qt::LeftButton && prevButtons != e->buttons()) newRect.setBottomLeft(cursorPos);
if (e->buttons() & Qt::LeftButton) newRect.setBottomLeft(cursorPos);
}
}
if (!close)
@ -276,6 +276,7 @@ void CropScene::mouseMoveEvent(QGraphicsSceneMouseEvent *e) {
else {
rect->setRect(newRect);
prevButtons = e->buttons();
updatePoly();
return;
}
}
@ -292,6 +293,7 @@ void CropScene::mouseMoveEvent(QGraphicsSceneMouseEvent *e) {
} else {
QPointF p = cursorPos;
if (rect == nullptr) {
drawingRect = true;
rect = new SelectionRectangle(p.x(), p.y(), 1, 1);
initPos = p;
QPen pen(Qt::NoBrush, 1);
@ -308,22 +310,7 @@ void CropScene::mouseMoveEvent(QGraphicsSceneMouseEvent *e) {
qAbs(initPos.y() - p.y())));
}
}
QPolygonF poly;
QPointF theMagicWikipediaPoint(rect->rect().right(), sceneRect().bottom());
poly << sceneRect().topLeft();
poly << sceneRect().topRight();
poly << sceneRect().bottomRight();
poly << theMagicWikipediaPoint;
poly << rect->rect().bottomRight();
poly << rect->rect().topRight();
poly << rect->rect().topLeft();
poly << rect->rect().bottomLeft();
poly << rect->rect().bottomRight();
poly << theMagicWikipediaPoint;
poly << sceneRect().bottomLeft();
poly << sceneRect().topLeft();
this->polyItem->setPolygon(poly);
updatePoly();
e->accept();
}
}
@ -333,6 +320,7 @@ void CropScene::mouseMoveEvent(QGraphicsSceneMouseEvent *e) {
}
void CropScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *e) {
drawingRect = false;
if (drawingSelection) {
drawingSelection->mouseDragEndEvent(e, this);
delete drawingSelection;
@ -424,6 +412,25 @@ void CropScene::updateMag() {
magnifier->setPos(magnifierPos);
}
void CropScene::updatePoly() {
QPolygonF poly;
QPointF theMagicWikipediaPoint(rect->rect().right(), sceneRect().bottom());
poly << sceneRect().topLeft();
poly << sceneRect().topRight();
poly << sceneRect().bottomRight();
poly << theMagicWikipediaPoint;
poly << rect->rect().bottomRight();
poly << rect->rect().topRight();
poly << rect->rect().topLeft();
poly << rect->rect().bottomLeft();
poly << rect->rect().bottomRight();
poly << theMagicWikipediaPoint;
poly << sceneRect().bottomLeft();
poly << sceneRect().topLeft();
this->polyItem->setPolygon(poly);
}
void CropScene::initMagnifierGrid() {
if (!gridRectsX.isEmpty() || !gridRectsY.isEmpty()) return;

View File

@ -78,13 +78,15 @@ private slots:
private:
void updateMag();
void updatePoly();
void initMagnifierGrid();
void addDrawingAction(QMenuBar *menu, QString name, QString icon, std::function<DrawItem *()> item);
QPointF cursorPos;
std::function<DrawItem *()> drawingSelectionMaker;
QFlags<Qt::MouseButton> prevButtons;
QPixmap _pixmap;
SelectionRectangle *rect = nullptr;
QGraphicsRectItem *rect = nullptr;
bool drawingRect = true;
QGraphicsPixmapItem *magnifier = nullptr;
QGraphicsRectItem *magnifierBox = nullptr;
QGraphicsTextItem *magnifierHint = nullptr;

View File

@ -12,6 +12,10 @@ SelectionRectangle::SelectionRectangle(qreal x, qreal y, qreal w, qreal h, QGrap
: QGraphicsRectItem(x, y, w, h, parent) {
}
SelectionRectangle::SelectionRectangle(QRectF rect, QGraphicsItem *parent)
: SelectionRectangle(rect.left(), rect.top(), rect.width(), rect.height()) {
}
void SelectionRectangle::paint(QPainter *painter, const QStyleOptionGraphicsItem *options, QWidget *widget) {
QRect rect = this->rect().toRect();
if (rect.height() > 30 && rect.width() > 30) {

View File

@ -8,6 +8,7 @@ class SelectionRectangle : public QGraphicsRectItem {
public:
SelectionRectangle();
explicit SelectionRectangle(qreal x, qreal y, qreal w, qreal h, QGraphicsItem *parent = nullptr);
explicit SelectionRectangle(QRectF rect, QGraphicsItem *parent = nullptr);
protected:
void paint(QPainter *painter, const QStyleOptionGraphicsItem *options, QWidget *widget) override;

View File

@ -108,5 +108,6 @@ int main(int argc, char *argv[]) {
a.connect(&a, &QApplication::aboutToQuit, [] { stillAlive = false; });
if (!parser.isSet(h)) w.show();
qDebug() << "lol";
return a.exec();
}