1
0
mirror of https://github.com/qTox/qTox.git synced 2024-03-22 14:00:36 +08:00
qTox/src/widget/tool/movablewidget.cpp

288 lines
8.4 KiB
C++
Raw Normal View History

2015-07-21 05:57:46 +08:00
/*
Copyright © 2015 by The qTox Project Contributors
2015-07-21 05:57:46 +08:00
This file is part of qTox, a Qt-based graphical interface for Tox.
qTox is libre software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
qTox is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with qTox. If not, see <http://www.gnu.org/licenses/>.
*/
#include "movablewidget.h"
#include <QMouseEvent>
#include <QGraphicsOpacityEffect>
#include <cmath>
2015-08-19 01:40:11 +08:00
2017-01-06 19:02:54 +08:00
MovableWidget::MovableWidget(QWidget* parent)
2015-08-13 21:31:15 +08:00
: QWidget(parent)
2015-07-21 05:57:46 +08:00
{
2015-08-13 21:31:15 +08:00
setMinimumHeight(64);
2015-08-12 02:01:49 +08:00
setSizePolicy(QSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored));
actualSize = minimumSize();
2015-08-13 21:31:15 +08:00
boundaryRect = QRect(0, 0, 0, 0);
setRatio(1.0f);
resize(minimumSize());
actualPos = QPoint(0, 0);
2015-07-21 05:57:46 +08:00
}
2015-08-12 02:01:49 +08:00
2015-08-13 21:31:15 +08:00
void MovableWidget::resetBoundary(QRect newBoundary)
{
boundaryRect = newBoundary;
2015-08-19 01:40:11 +08:00
resize(QSize(round(actualSize.width()), round(actualSize.height())));
2015-08-13 21:31:15 +08:00
QPoint moveTo = QPoint(round(actualPos.x()), round(actualPos.y()));
checkBoundary(moveTo);
move(moveTo);
2015-08-19 01:40:11 +08:00
actualPos = moveTo;
2015-08-13 21:31:15 +08:00
}
void MovableWidget::setBoundary(QRect newBoundary)
{
if (boundaryRect.isNull())
{
boundaryRect = newBoundary;
return;
}
float changeX = newBoundary.width() / static_cast<float>(boundaryRect.width());
float changeY = newBoundary.height() / static_cast<float>(boundaryRect.height());
float percentageX = (x() - boundaryRect.x()) / static_cast<float>(boundaryRect.width() - width());
float percentageY = (y() - boundaryRect.y()) / static_cast<float>(boundaryRect.height() - height());
actualSize.setWidth(actualSize.width() * changeX);
actualSize.setHeight(actualSize.height() * changeY);
if (actualSize.width() == 0)
actualSize.setWidth(1);
if (actualSize.height() == 0)
actualSize.setHeight(1);
resize(QSize(round(actualSize.width()), round(actualSize.height())));
actualPos = QPointF(percentageX * (newBoundary.width() - width()), percentageY * (newBoundary.height() - height()));
actualPos += QPointF(newBoundary.topLeft());
QPoint moveTo = QPoint(round(actualPos.x()), round(actualPos.y()));
move(moveTo);
boundaryRect = newBoundary;
2015-08-19 01:40:11 +08:00
2015-08-13 21:31:15 +08:00
}
float MovableWidget::getRatio() const
{
return ratio;
}
void MovableWidget::setRatio(float r)
{
ratio = r;
resize(width(), width() / ratio);
QPoint position = QPoint(actualPos.x(), actualPos.y());
checkBoundary(position);
move(position);
actualPos = pos();
actualSize = size();
}
2015-07-21 05:57:46 +08:00
void MovableWidget::mousePressEvent(QMouseEvent* event)
{
if (event->buttons() & Qt::LeftButton)
{
2015-08-12 02:01:49 +08:00
if (!(mode & Resize))
mode |= Moving;
2015-07-21 05:57:46 +08:00
lastPoint = event->globalPos();
}
}
void MovableWidget::mouseMoveEvent(QMouseEvent* event)
{
2015-08-12 02:01:49 +08:00
if (mode & Moving)
2015-07-21 05:57:46 +08:00
{
QPoint moveTo = pos() - (lastPoint - event->globalPos());
checkBoundary(moveTo);
2015-07-21 05:57:46 +08:00
move(moveTo);
lastPoint = event->globalPos();
2015-08-12 02:01:49 +08:00
actualPos = pos();
}
else
{
if (!(event->buttons() & Qt::LeftButton))
{
if (event->x() < 6)
mode |= ResizeLeft;
else
mode &= ~ResizeLeft;
if (event->y() < 6)
mode |= ResizeUp;
else
mode &= ~ResizeUp;
if (event->x() > width() - 6)
mode |= ResizeRight;
else
mode &= ~ResizeRight;
if (event->y() > height() - 6)
mode |= ResizeDown;
else
mode &= ~ResizeDown;
}
if (mode & Resize)
{
const Modes ResizeUpRight = ResizeUp | ResizeRight;
const Modes ResizeUpLeft = ResizeUp | ResizeLeft;
const Modes ResizeDownRight = ResizeDown | ResizeRight;
const Modes ResizeDownLeft = ResizeDown | ResizeLeft;
if ((mode & ResizeUpRight) == ResizeUpRight || (mode & ResizeDownLeft) == ResizeDownLeft)
setCursor(Qt::SizeBDiagCursor);
else if ((mode & ResizeUpLeft) == ResizeUpLeft || (mode & ResizeDownRight) == ResizeDownRight)
setCursor(Qt::SizeFDiagCursor);
else if (mode & (ResizeLeft | ResizeRight))
setCursor(Qt::SizeHorCursor);
else
setCursor(Qt::SizeVerCursor);
if (event->buttons() & Qt::LeftButton)
{
QPoint lastPosition = pos();
QPoint displacement = lastPoint - event->globalPos();
QSize lastSize = size();
if (mode & ResizeUp)
{
lastSize.setHeight(height() + displacement.y());
2015-08-19 01:40:11 +08:00
if (lastSize.height() > maximumHeight())
lastPosition.setY(y() - displacement.y() + (lastSize.height() - maximumHeight()));
2015-08-12 02:01:49 +08:00
else
lastPosition.setY(y() - displacement.y());
}
if (mode & ResizeLeft)
{
lastSize.setWidth(width() + displacement.x());
2015-08-19 01:40:11 +08:00
if (lastSize.width() > maximumWidth())
lastPosition.setX(x() - displacement.x() + (lastSize.width() - maximumWidth()));
2015-08-12 02:01:49 +08:00
else
lastPosition.setX(x() - displacement.x());
}
if (mode & ResizeRight)
lastSize.setWidth(width() - displacement.x());
if (mode & ResizeDown)
lastSize.setHeight(height() - displacement.y());
2015-08-19 01:40:11 +08:00
if (lastSize.height() > maximumHeight())
lastSize.setHeight(maximumHeight());
2015-08-12 02:01:49 +08:00
2015-08-19 01:40:11 +08:00
if (lastSize.width() > maximumWidth())
lastSize.setWidth(maximumWidth());
2015-08-12 02:01:49 +08:00
if (mode & (ResizeLeft | ResizeRight))
{
if (mode & (ResizeUp | ResizeDown))
{
int height = lastSize.width() / getRatio();
if (!(mode & ResizeDown))
lastPosition.setY(lastPosition.y() - (height - lastSize.height()));
resize(lastSize.width(), height);
if (lastSize.width() < minimumWidth())
lastPosition.setX(pos().x());
if (height < minimumHeight())
lastPosition.setY(pos().y());
}
else
{
resize(lastSize.width(), lastSize.width() / getRatio());
}
}
else
{
resize(lastSize.height() * getRatio(), lastSize.height());
}
updateGeometry();
checkBoundary(lastPosition);
move(lastPosition);
lastPoint = event->globalPos();
actualSize = size();
actualPos = pos();
}
}
else
{
unsetCursor();
}
2015-07-21 05:57:46 +08:00
}
2015-08-13 21:31:15 +08:00
2015-07-21 05:57:46 +08:00
}
void MovableWidget::mouseReleaseEvent(QMouseEvent* event)
{
if (!(event->buttons() & Qt::LeftButton))
2015-08-12 02:01:49 +08:00
mode = 0;
2015-07-21 05:57:46 +08:00
}
void MovableWidget::mouseDoubleClickEvent(QMouseEvent* event)
{
if (!(event->buttons() & Qt::LeftButton))
return;
if (!graphicsEffect())
{
QGraphicsOpacityEffect* opacityEffect = new QGraphicsOpacityEffect(this);
opacityEffect->setOpacity(0.5);
setGraphicsEffect(opacityEffect);
}
else
{
setGraphicsEffect(nullptr);
}
}
void MovableWidget::checkBoundary(QPoint& point) const
{
int x1, y1, x2, y2;
boundaryRect.getCoords(&x1, &y1, &x2, &y2);
2015-08-13 21:31:15 +08:00
if (point.x() < boundaryRect.left())
point.setX(boundaryRect.left());
2015-08-13 21:31:15 +08:00
if (point.y() < boundaryRect.top())
point.setY(boundaryRect.top());
2015-08-13 21:31:15 +08:00
if (point.x() + width() > boundaryRect.right() + 1)
point.setX(boundaryRect.right() - width() + 1);
2015-08-13 21:31:15 +08:00
if (point.y() + height() > boundaryRect.bottom() + 1)
point.setY(boundaryRect.bottom() - height() + 1);
}