feat: resize window policy
This commit is contained in:
parent
771ec335fe
commit
b3e29ad4a6
|
@ -14,6 +14,7 @@ GraphicsView::GraphicsView(QWidget *parent)
|
||||||
setDragMode(QGraphicsView::ScrollHandDrag);
|
setDragMode(QGraphicsView::ScrollHandDrag);
|
||||||
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
||||||
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
||||||
|
setResizeAnchor(QGraphicsView::AnchorUnderMouse);
|
||||||
setStyleSheet("background-color: rgba(0, 0, 0, 180);"
|
setStyleSheet("background-color: rgba(0, 0, 0, 180);"
|
||||||
"border-radius: 3px;");
|
"border-radius: 3px;");
|
||||||
setAcceptDrops(true);
|
setAcceptDrops(true);
|
||||||
|
@ -21,7 +22,12 @@ GraphicsView::GraphicsView(QWidget *parent)
|
||||||
|
|
||||||
void GraphicsView::showImage(const QPixmap &pixmap)
|
void GraphicsView::showImage(const QPixmap &pixmap)
|
||||||
{
|
{
|
||||||
|
resetTransform();
|
||||||
scene()->showImage(pixmap);
|
scene()->showImage(pixmap);
|
||||||
|
if (!isThingSmallerThanWindowWith(transform())) {
|
||||||
|
m_enableFitInView = true;
|
||||||
|
fitInView(sceneRect(), Qt::KeepAspectRatio);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void GraphicsView::showText(const QString &text)
|
void GraphicsView::showText(const QString &text)
|
||||||
|
@ -41,23 +47,19 @@ void GraphicsView::setScene(GraphicsScene *scene)
|
||||||
|
|
||||||
void GraphicsView::mousePressEvent(QMouseEvent *event)
|
void GraphicsView::mousePressEvent(QMouseEvent *event)
|
||||||
{
|
{
|
||||||
QGraphicsItem *item = itemAt(event->pos());
|
if (shouldIgnoreMousePressMoveEvent(event)) {
|
||||||
if (!item) {
|
|
||||||
event->ignore();
|
event->ignore();
|
||||||
// blumia: return here, or the QMouseEvent event transparency won't
|
// blumia: return here, or the QMouseEvent event transparency won't
|
||||||
// work if we set a QGraphicsView::ScrollHandDrag drag mode.
|
// work if we set a QGraphicsView::ScrollHandDrag drag mode.
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
qDebug() << item;
|
|
||||||
|
|
||||||
return QGraphicsView::mousePressEvent(event);
|
return QGraphicsView::mousePressEvent(event);
|
||||||
}
|
}
|
||||||
|
|
||||||
void GraphicsView::mouseMoveEvent(QMouseEvent *event)
|
void GraphicsView::mouseMoveEvent(QMouseEvent *event)
|
||||||
{
|
{
|
||||||
QGraphicsItem *item = itemAt(event->pos());
|
if (shouldIgnoreMousePressMoveEvent(event)) {
|
||||||
if (!item) {
|
|
||||||
event->ignore();
|
event->ignore();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -76,6 +78,7 @@ void GraphicsView::mouseReleaseEvent(QMouseEvent *event)
|
||||||
|
|
||||||
void GraphicsView::wheelEvent(QWheelEvent *event)
|
void GraphicsView::wheelEvent(QWheelEvent *event)
|
||||||
{
|
{
|
||||||
|
m_enableFitInView = false;
|
||||||
if (event->delta() > 0) {
|
if (event->delta() > 0) {
|
||||||
scale(1.25, 1.25);
|
scale(1.25, 1.25);
|
||||||
} else {
|
} else {
|
||||||
|
@ -83,6 +86,20 @@ void GraphicsView::wheelEvent(QWheelEvent *event)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void GraphicsView::resizeEvent(QResizeEvent *event)
|
||||||
|
{
|
||||||
|
if (m_enableFitInView) {
|
||||||
|
if (isThingSmallerThanWindowWith(QTransform()) && transform().m11() >= 1) {
|
||||||
|
// no longer need to do fitInView()
|
||||||
|
// but we leave the m_enableFitInView value unchanged in case
|
||||||
|
// user resize down the window again.
|
||||||
|
} else {
|
||||||
|
fitInView(sceneRect(), Qt::KeepAspectRatio);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return QGraphicsView::resizeEvent(event);
|
||||||
|
}
|
||||||
|
|
||||||
void GraphicsView::dragEnterEvent(QDragEnterEvent *event)
|
void GraphicsView::dragEnterEvent(QDragEnterEvent *event)
|
||||||
{
|
{
|
||||||
if (event->mimeData()->hasUrls() || event->mimeData()->hasImage() || event->mimeData()->hasText()) {
|
if (event->mimeData()->hasUrls() || event->mimeData()->hasImage() || event->mimeData()->hasText()) {
|
||||||
|
@ -129,5 +146,27 @@ void GraphicsView::dropEvent(QDropEvent *event)
|
||||||
}
|
}
|
||||||
} else if (mimeData->hasText()) {
|
} else if (mimeData->hasText()) {
|
||||||
showText(mimeData->text());
|
showText(mimeData->text());
|
||||||
|
} else {
|
||||||
|
showText("Not supported mimedata: " + mimeData->formats().first());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool GraphicsView::isThingSmallerThanWindowWith(const QTransform &transform) const
|
||||||
|
{
|
||||||
|
return rect().size().expandedTo(transform.mapRect(sceneRect()).size().toSize())
|
||||||
|
== rect().size();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool GraphicsView::shouldIgnoreMousePressMoveEvent(const QMouseEvent *event) const
|
||||||
|
{
|
||||||
|
if (isThingSmallerThanWindowWith(transform())) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
QGraphicsItem *item = itemAt(event->pos());
|
||||||
|
if (!item) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
|
@ -20,10 +20,16 @@ private:
|
||||||
void mouseMoveEvent(QMouseEvent * event) override;
|
void mouseMoveEvent(QMouseEvent * event) override;
|
||||||
void mouseReleaseEvent(QMouseEvent * event) override;
|
void mouseReleaseEvent(QMouseEvent * event) override;
|
||||||
void wheelEvent(QWheelEvent *event) override;
|
void wheelEvent(QWheelEvent *event) override;
|
||||||
|
void resizeEvent(QResizeEvent *event) override;
|
||||||
|
|
||||||
void dragEnterEvent(QDragEnterEvent *event) override;
|
void dragEnterEvent(QDragEnterEvent *event) override;
|
||||||
void dragMoveEvent(QDragMoveEvent *event) override;
|
void dragMoveEvent(QDragMoveEvent *event) override;
|
||||||
void dropEvent(QDropEvent *event) override;
|
void dropEvent(QDropEvent *event) override;
|
||||||
|
|
||||||
|
bool isThingSmallerThanWindowWith(const QTransform &transform) const;
|
||||||
|
bool shouldIgnoreMousePressMoveEvent(const QMouseEvent *event) const;
|
||||||
|
|
||||||
|
bool m_enableFitInView = false;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // GRAPHICSVIEW_H
|
#endif // GRAPHICSVIEW_H
|
||||||
|
|
Loading…
Reference in New Issue
Block a user