#include "lts_picture_view.h" #include #include #include #include using namespace LtsWidgets; LtsPictureView::LtsPictureView(QWidget *parent) : QGraphicsView(parent) , m_gsPicture(new QGraphicsScene(this)) , m_rubberband(new QRubberBand(QRubberBand::Rectangle, this)) , m_image(nullptr) , m_origimage(nullptr) { m_mouseActive = false; m_moveable = false; m_aspectRatioEnabled = true; m_s_inEdit = false; m_aspectRatio = 1; setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); } LtsPictureView::~LtsPictureView() { delete m_rubberband; } void LtsPictureView::setAspectRatio(double aspectRatio) { m_aspectRatio = aspectRatio; newRubberBand(); } void LtsPictureView::ratioClicked(bool enabled) { m_aspectRatioEnabled = enabled; if (enabled) newRubberBand(); } void LtsPictureView::mousePressEvent(QMouseEvent *event) { if (event->button() == Qt::LeftButton) { if (m_rubberband->geometry().contains(event->pos())) { m_bandoffset = event->pos() - m_rubberband->pos(); m_moveable = true; m_rubberband->show(); } else { m_origin = event->pos(); m_curRectangle = QRect(m_origin.x(), m_origin.y(), 0, 0); m_rubberband->setGeometry(m_curRectangle); m_rubberband->show(); m_mouseActive = true; } } else { hideRubberband(); newRubberBand(); m_mouseActive = false; } QGraphicsView::mousePressEvent(event); } void LtsPictureView::mouseMoveEvent(QMouseEvent *event) { if (m_moveable == true) { QPoint newCoord = event->pos() - m_bandoffset; if (newCoord.x() < 0) newCoord.setX(0); if (newCoord.y() < 0) newCoord.setY(0); if (m_origimage->pixmap().width() < (newCoord.x() + m_rubberband->width())) newCoord.setX(m_origimage->pixmap().width() - m_rubberband->width() + 1); if (m_origimage->pixmap().height() < (newCoord.y() + m_rubberband->height())) newCoord.setY(m_origimage->pixmap().height() - m_rubberband->height() + 1); m_rubberband->move(newCoord); QPoint move = m_rubberband->pos(); int left = move.x(); int top = move.y(); int width = m_rubberband->rect().width(); int height = m_rubberband->rect().height(); emit mouseMoved(QRect(left, top, width, height)); } else { if (m_mouseActive) { QRect rect; int xMax = event->pos().x(); int yMax = event->pos().y(); if (event->pos().x() > m_origimage->pixmap().width()) xMax = m_origimage->pixmap().width(); if (event->pos().x() < 0) xMax = 0; if (event->pos().y() > m_origimage->pixmap().height()) yMax = m_origimage->pixmap().height(); if (event->pos().y() < 0) yMax = 0; if (m_aspectRatioEnabled) { const int xMin = m_origin.x(); const int yMin = m_origin.y(); int xLengthAbs(std::abs(xMax - xMin)); int yLengthAbs(std::abs(yMax - yMin)); QPoint length; if (xLengthAbs / m_aspectRatio > yLengthAbs * m_aspectRatio) { length.setX(xLengthAbs); length.setY(length.x() / m_aspectRatio); } else { length.setY(yLengthAbs); length.setX(length.y() * m_aspectRatio); } if (!(yMax - yMin < 0) && length.y() + m_origin.y() > m_origimage->pixmap().height()) { length.setY(m_origimage->pixmap().height() - m_origin.y()); length.setX(length.y()* m_aspectRatio); } if (!(xMax - xMin < 0) && length.x() + m_origin.x() > m_origimage->pixmap().width()) { length.setX(m_origimage->pixmap().width() - m_origin.x()); length.setY(length.x() / m_aspectRatio); } if (xMax - xMin < 0) { length.setX(length.x() * -1); if (length.x() + m_origin.x() < 0) { length.setX(0 - m_origin.x()); length.setY((length.x() / m_aspectRatio) * -1); } } if (yMax - yMin < 0) { length.setY(length.y() * -1); if (length.y() + m_origin.y() < 0) { length.setY(0 - m_origin.y()); length.setX(length.y() * m_aspectRatio); if (!(xMax - xMin < 0)) length.setX(length.x() * -1); } } length = m_origin + length; rect.setTopLeft(m_origin); rect.setBottomRight(length); } else { rect.setTopLeft(m_origin); rect.setBottomRight(QPoint(xMax, yMax)); } m_curRectangle = rect.normalized(); m_rubberband->setGeometry(m_curRectangle); emit mouseMoved(m_curRectangle); } } QGraphicsView::mouseMoveEvent(event); } void LtsPictureView::mouseReleaseEvent(QMouseEvent *event) { m_moveable = false; QGraphicsView::mouseReleaseEvent(event); } void LtsPictureView::hideRubberband() { m_rubberband->hide(); } void LtsPictureView::newRubberBand() { delete m_rubberband; m_rubberband = new QRubberBand(QRubberBand::Rectangle, this); } void LtsPictureView::setPicture(const QImage &image, const QPoint &pos, const bool flag) { if (m_image) { m_gsPicture->removeItem(m_image); delete m_image; } if (flag) { if (m_origimage) { m_gsPicture->removeItem(m_origimage); delete m_origimage; } QGraphicsOpacityEffect *opacity = new QGraphicsOpacityEffect; opacity->setOpacity(0.2); m_origimage = new QGraphicsPixmapItem(QPixmap::fromImage(image)); m_origimage->setGraphicsEffect(opacity); m_gsPicture->addItem(m_origimage); setMinimumSize(image.size()); setMaximumSize(image.size()); emit setLimits(m_origimage->pixmap().size(), true); } m_image = new QGraphicsPixmapItem(QPixmap::fromImage(image)); m_image->setPos(pos); m_gsPicture->addItem(m_image); setScene(m_gsPicture); } void LtsPictureView::leftChanged(int left) { if (m_s_inEdit || m_mouseActive) return; m_s_inEdit = true; m_curRectangle.setLeft(left); if(m_aspectRatioEnabled) { int newHeight = m_curRectangle.width() / m_aspectRatio; if (m_origin.y() < m_curRectangle.bottom()) { m_curRectangle.setHeight(newHeight); } else { int offset = m_curRectangle.height() - newHeight; m_curRectangle.setTop(m_curRectangle.top() + offset); } } m_rubberband->setGeometry(m_curRectangle); emit mouseMoved(m_curRectangle); m_s_inEdit = false; } void LtsPictureView::rightChanged(int right) { if (m_s_inEdit || m_mouseActive) return; m_s_inEdit = true; m_curRectangle.setRight(right); if(m_aspectRatioEnabled) { int newHeight = m_curRectangle.width() / m_aspectRatio; if (m_origin.y() < m_curRectangle.bottom()) { m_curRectangle.setHeight(newHeight); } else { int offset = m_curRectangle.height() - newHeight; m_curRectangle.setTop(m_curRectangle.top() + offset); } } m_rubberband->setGeometry(m_curRectangle); emit mouseMoved(m_curRectangle); m_s_inEdit = false; } void LtsPictureView::topChanged(int top) { if (m_s_inEdit || m_mouseActive) return; m_s_inEdit = true; m_curRectangle.setTop(top); if(m_aspectRatioEnabled) { int newWidth = m_curRectangle.height() * m_aspectRatio; if (m_origin.x() <= m_curRectangle.left()) { m_curRectangle.setWidth(newWidth); } else { int offset = m_curRectangle.width() - newWidth; m_curRectangle.setLeft(m_curRectangle.left() + offset); } } m_rubberband->setGeometry(m_curRectangle); emit mouseMoved(m_curRectangle); m_s_inEdit = false; } void LtsPictureView::bottomChanged(int bottom) { if (m_s_inEdit || m_mouseActive) return; m_s_inEdit = true; m_curRectangle.setBottom(bottom); if(m_aspectRatioEnabled) { int newWidth = m_curRectangle.height() * m_aspectRatio; if (m_origin.x() <= m_curRectangle.left()) { m_curRectangle.setWidth(newWidth); } else { int offset = m_curRectangle.width() - newWidth; m_curRectangle.setLeft(m_curRectangle.left() + offset); } } m_rubberband->setGeometry(m_curRectangle); emit mouseMoved(m_curRectangle); m_s_inEdit = false; } QGraphicsPixmapItem *LtsPictureView::getImage() { if (!m_image) m_image = new QGraphicsPixmapItem; return m_image; }