forked from qt-creator/qt-creator
QuickToolBar: use absolute positioning for dragging
This resolves several usability bugs.
This commit is contained in:
@@ -92,7 +92,7 @@ DragWidget::DragWidget(QWidget *parent) : QFrame(parent)
|
|||||||
setFrameStyle(QFrame::NoFrame);
|
setFrameStyle(QFrame::NoFrame);
|
||||||
setFrameShape(QFrame::StyledPanel);
|
setFrameShape(QFrame::StyledPanel);
|
||||||
setFrameShadow(QFrame::Sunken);
|
setFrameShadow(QFrame::Sunken);
|
||||||
m_oldPos = QPoint(-1, -1);
|
m_startPos = QPoint(-1, -1);
|
||||||
m_pos = QPoint(-1, -1);
|
m_pos = QPoint(-1, -1);
|
||||||
|
|
||||||
m_dropShadowEffect = new QGraphicsDropShadowEffect;
|
m_dropShadowEffect = new QGraphicsDropShadowEffect;
|
||||||
@@ -104,7 +104,7 @@ DragWidget::DragWidget(QWidget *parent) : QFrame(parent)
|
|||||||
void DragWidget::mousePressEvent(QMouseEvent * event)
|
void DragWidget::mousePressEvent(QMouseEvent * event)
|
||||||
{
|
{
|
||||||
if (event->button() == Qt::LeftButton) {
|
if (event->button() == Qt::LeftButton) {
|
||||||
m_oldPos = event->globalPos();
|
m_startPos = event->globalPos() - parentWidget()->mapToGlobal((pos()));
|
||||||
m_opacityEffect = new QGraphicsOpacityEffect;
|
m_opacityEffect = new QGraphicsOpacityEffect;
|
||||||
setGraphicsEffect(m_opacityEffect);
|
setGraphicsEffect(m_opacityEffect);
|
||||||
event->accept();
|
event->accept();
|
||||||
@@ -115,7 +115,7 @@ void DragWidget::mousePressEvent(QMouseEvent * event)
|
|||||||
void DragWidget::mouseReleaseEvent(QMouseEvent *event)
|
void DragWidget::mouseReleaseEvent(QMouseEvent *event)
|
||||||
{
|
{
|
||||||
if (event->button() == Qt::LeftButton) {
|
if (event->button() == Qt::LeftButton) {
|
||||||
m_oldPos = QPoint(-1, -1);
|
m_startPos = QPoint(-1, -1);
|
||||||
m_dropShadowEffect = new QGraphicsDropShadowEffect;
|
m_dropShadowEffect = new QGraphicsDropShadowEffect;
|
||||||
m_dropShadowEffect->setBlurRadius(6);
|
m_dropShadowEffect->setBlurRadius(6);
|
||||||
m_dropShadowEffect->setOffset(2, 2);
|
m_dropShadowEffect->setOffset(2, 2);
|
||||||
@@ -124,26 +124,35 @@ void DragWidget::mouseReleaseEvent(QMouseEvent *event)
|
|||||||
QFrame::mouseReleaseEvent(event);
|
QFrame::mouseReleaseEvent(event);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static inline int limit(int a, int min, int max)
|
||||||
|
{
|
||||||
|
if (a < min)
|
||||||
|
return min;
|
||||||
|
if (a > max)
|
||||||
|
return max;
|
||||||
|
return a;
|
||||||
|
}
|
||||||
|
|
||||||
void DragWidget::mouseMoveEvent(QMouseEvent * event)
|
void DragWidget::mouseMoveEvent(QMouseEvent * event)
|
||||||
{
|
{
|
||||||
if (event->buttons() && Qt::LeftButton) {
|
if (event->buttons() && Qt::LeftButton) {
|
||||||
if (pos().x() < 10 && event->pos().x() < -20)
|
if (m_startPos != QPoint(-1, -1)) {
|
||||||
return;
|
QPoint newPos = parentWidget()->mapFromGlobal(event->globalPos() - m_startPos);
|
||||||
if (m_oldPos != QPoint(-1, -1)) {
|
|
||||||
QPoint diff = event->globalPos() - m_oldPos;
|
newPos.setX(limit(newPos.x(), 20, parentWidget()->width() - 20 - width()));
|
||||||
QPoint newPos = pos() + diff;
|
newPos.setY(limit(newPos.y(), 2, parentWidget()->height() - 20 - height()));
|
||||||
if (newPos.x() > 0 && newPos.y() > 0 && (newPos.x() + width()) < parentWidget()->width() && (newPos.y() + height()) < parentWidget()->height()) {
|
|
||||||
|
QPoint diff = pos() - newPos;
|
||||||
if (m_secondaryTarget)
|
if (m_secondaryTarget)
|
||||||
m_secondaryTarget->move(m_secondaryTarget->pos() + diff);
|
m_secondaryTarget->move(m_secondaryTarget->pos() - diff);
|
||||||
move(newPos);
|
move(newPos);
|
||||||
m_pos = newPos;
|
if (m_pos != newPos)
|
||||||
protectedMoved();
|
protectedMoved();
|
||||||
}
|
m_pos = newPos;
|
||||||
} else {
|
} else {
|
||||||
m_opacityEffect = new QGraphicsOpacityEffect;
|
m_opacityEffect = new QGraphicsOpacityEffect;
|
||||||
setGraphicsEffect(m_opacityEffect);
|
setGraphicsEffect(m_opacityEffect);
|
||||||
}
|
}
|
||||||
m_oldPos = event->globalPos();
|
|
||||||
event->accept();
|
event->accept();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -68,7 +68,7 @@ protected:
|
|||||||
private:
|
private:
|
||||||
QGraphicsDropShadowEffect *m_dropShadowEffect;
|
QGraphicsDropShadowEffect *m_dropShadowEffect;
|
||||||
QGraphicsOpacityEffect *m_opacityEffect;
|
QGraphicsOpacityEffect *m_opacityEffect;
|
||||||
QPoint m_oldPos;
|
QPoint m_startPos;
|
||||||
QWeakPointer<QWidget> m_secondaryTarget;
|
QWeakPointer<QWidget> m_secondaryTarget;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user