resize and move the tooltip in one move

This commit is contained in:
Marco Martin 2014-01-12 17:29:07 +01:00
parent e03580242e
commit 331b6c5d0d
4 changed files with 70 additions and 40 deletions

View File

@ -53,12 +53,12 @@ DialogProxy::DialogProxy(QQuickItem *parent)
m_syncTimer = new QTimer(this);
m_syncTimer->setSingleShot(true);
m_syncTimer->setInterval(250);
m_syncTimer->setInterval(0);
connect(m_syncTimer, &QTimer::timeout, this, &DialogProxy::syncToMainItemSize);
connect(this, &QWindow::xChanged, [=](){m_syncTimer->start();});
connect(this, &QWindow::yChanged, [=](){m_syncTimer->start();});
connect(this, &QWindow::visibleChanged, this, &DialogProxy::onVisibleChanged);
connect(this, &QWindow::xChanged, [=](){m_syncTimer->start(150);});
connect(this, &QWindow::yChanged, [=](){m_syncTimer->start(150);});
// connect(this, &QWindow::visibleChanged, this, &DialogProxy::onVisibleChanged);
//HACK: this property is invoked due to the initialization that gets done to contentItem() in the getter
property("data");
//Create the FrameSvg background.
@ -91,12 +91,17 @@ void DialogProxy::setMainItem(QQuickItem *mainItem)
mainItem->setProperty("parent", QVariant::fromValue(contentItem()));
if (mainItem->metaObject()->indexOfSignal("widthChanged")) {
connect(mainItem, &QQuickItem::widthChanged, [=](){m_syncTimer->start();});
connect(mainItem, &QQuickItem::widthChanged, [=](){m_syncTimer->start(0);});
}
if (mainItem->metaObject()->indexOfSignal("heightChanged")) {
connect(mainItem, &QQuickItem::heightChanged, [=](){m_syncTimer->start();});
connect(mainItem, &QQuickItem::heightChanged, [=](){m_syncTimer->start(0);});
}
if (isVisible()) {
m_syncTimer->start(0);
} else {
m_syncTimer->stop();
syncToMainItemSize();
}
syncToMainItemSize();
}
//if this is called in Component.onCompleted we have to wait a loop the item is added to a scene
@ -117,14 +122,18 @@ void DialogProxy::setVisualParent(QQuickItem *visualParent)
m_visualParent = visualParent;
emit visualParentChanged();
if (visualParent && isVisible()) {
adjustGeometry(QRect(popupPosition(visualParent, Qt::AlignCenter), size()));
if (visualParent) {
if (isVisible()) {
m_syncTimer->start(0);
} else {
m_syncTimer->stop();
syncToMainItemSize();
}
}
}
void DialogProxy::onVisibleChanged()
void DialogProxy::updateVisibility(bool visible)
{
const bool visible = isVisible();
if (visible) {
if (location() == Plasma::Types::FullScreen) {
m_frameSvgItem->setEnabledBorders(Plasma::FrameSvg::NoBorder);
@ -142,6 +151,7 @@ void DialogProxy::onVisibleChanged()
m_cachedGeometry = QRect();
}
m_syncTimer->stop();
syncToMainItemSize();
}
}
@ -182,7 +192,7 @@ void DialogProxy::onVisibleChanged()
}
}
QPoint DialogProxy::popupPosition(QQuickItem *item, Qt::AlignmentFlag alignment)
QPoint DialogProxy::popupPosition(QQuickItem *item, const QSize &size, Qt::AlignmentFlag alignment)
{
if (!item) {
//If no item was specified try to align at the center of the parent view
@ -190,20 +200,20 @@ QPoint DialogProxy::popupPosition(QQuickItem *item, Qt::AlignmentFlag alignment)
if (parentItem && parentItem->window()) {
switch (m_location) {
case Plasma::Types::TopEdge:
return QPoint(screen()->availableGeometry().center().x() - width()/2, screen()->availableGeometry().y());
return QPoint(screen()->availableGeometry().center().x() - size.width()/2, screen()->availableGeometry().y());
break;
case Plasma::Types::LeftEdge:
return QPoint(screen()->availableGeometry().x(), screen()->availableGeometry().center().y() - height()/2);
return QPoint(screen()->availableGeometry().x(), screen()->availableGeometry().center().y() - size.height()/2);
break;
case Plasma::Types::RightEdge:
return QPoint(screen()->availableGeometry().right() - width(), screen()->availableGeometry().center().y() - height()/2);
return QPoint(screen()->availableGeometry().right() - size.width(), screen()->availableGeometry().center().y() - size.height()/2);
break;
case Plasma::Types::BottomEdge:
return QPoint(screen()->availableGeometry().center().x() - width()/2, screen()->availableGeometry().bottom()-height());
return QPoint(screen()->availableGeometry().center().x() - size.width()/2, screen()->availableGeometry().bottom() -size.height());
break;
//Default center in the screen
default:
return screen()->geometry().center() - QPoint(width()/2, height()/2);
return screen()->geometry().center() - QPoint(size.width()/2, size.height()/2);
}
} else {
return QPoint();
@ -226,15 +236,15 @@ QPoint DialogProxy::popupPosition(QQuickItem *item, Qt::AlignmentFlag alignment)
}
}
const QPoint topPoint((item->boundingRect().width() - width())/2,
-height());
const QPoint bottomPoint((item->boundingRect().width() - width())/2,
const QPoint topPoint((item->boundingRect().width() - size.width())/2,
-size.height());
const QPoint bottomPoint((item->boundingRect().width() - size.width())/2,
item->boundingRect().height());
const QPoint leftPoint(-width(),
(item->boundingRect().height() - height())/2);
const QPoint leftPoint(-size.width(),
(item->boundingRect().height() - size.height())/2);
const QPoint rightPoint(item->boundingRect().width(),
(item->boundingRect().height() - height())/2);
(item->boundingRect().height() - size.height())/2);
QPoint offset(0, 0);
if (m_location == Plasma::Types::BottomEdge) {
@ -265,10 +275,10 @@ QPoint DialogProxy::popupPosition(QQuickItem *item, Qt::AlignmentFlag alignment)
menuPos.setX(pos.x() + rightPoint.x());
}
}
if (menuPos.x() + width() > avail.width() - rightMargin) {
if (menuPos.x() + size.width() > avail.width() - rightMargin) {
// popup hits rhs
if (m_location == Plasma::Types::TopEdge || m_location == Plasma::Types::BottomEdge) {
menuPos.setX(avail.width() - width());
menuPos.setX(avail.width() - size.width());
} else {
menuPos.setX(pos.x() + leftPoint.x());
}
@ -281,7 +291,7 @@ QPoint DialogProxy::popupPosition(QQuickItem *item, Qt::AlignmentFlag alignment)
menuPos.setY(pos.y() + bottomPoint.y());
}
}
if (menuPos.y() + height() > avail.height() - bottomMargin) {
if (menuPos.y() + size.height() > avail.height() - bottomMargin) {
// hitting bottom
if (m_location == Plasma::Types::TopEdge || m_location == Plasma::Types::BottomEdge) {
menuPos.setY(pos.y() + topPoint.y());
@ -351,8 +361,13 @@ void DialogProxy::syncToMainItemSize()
QSize(m_frameSvgItem->margins()->left() + m_frameSvgItem->margins()->right(),
m_frameSvgItem->margins()->top() + m_frameSvgItem->margins()->bottom());
const QRect geom(popupPosition(visualParent(), s, Qt::AlignCenter), s);
if (geom == geometry()) {
return;
}
if (visualParent()) {
adjustGeometry(QRect(popupPosition(visualParent(), Qt::AlignCenter), s));
adjustGeometry(geom);
} else {
resize(s);
}
@ -407,6 +422,22 @@ void DialogProxy::showEvent(QShowEvent *event)
QQuickWindow::showEvent(event);
}
bool DialogProxy::event(QEvent *event)
{
if (event->type() == QEvent::Show) {
updateVisibility(true);
} else if (event->type() == QEvent::Hide) {
updateVisibility(false);
}
return QQuickWindow::event(event);
}
void DialogProxy::hideEvent(QHideEvent *event)
{
QQuickWindow::hideEvent(event);
}
void DialogProxy::syncBorders()
{
const QRect avail = screen()->availableGeometry();

View File

@ -115,7 +115,7 @@ public:
* @arg item the item the popup has to be positioned relatively to. if null, the popup will be positioned in the center of the window
* @arg alignment alignment of the popup compared to the item
*/
QPoint popupPosition(QQuickItem *item, Qt::AlignmentFlag alignment=Qt::AlignCenter) ;
QPoint popupPosition(QQuickItem *item, const QSize &size, Qt::AlignmentFlag alignment=Qt::AlignCenter) ;
void setType(WindowType type);
WindowType type() const;
@ -139,6 +139,8 @@ protected:
void focusInEvent(QFocusEvent *ev);
void focusOutEvent(QFocusEvent *ev);
void showEvent(QShowEvent *event);
void hideEvent(QHideEvent *event);
bool event(QEvent *event);
QTimer *m_syncTimer;
Plasma::Types::Location m_location;
@ -149,7 +151,7 @@ protected:
private Q_SLOTS:
void syncBorders();
void onVisibleChanged();
void updateVisibility(bool visible);
private:
QRect m_cachedGeometry;

View File

@ -84,7 +84,7 @@ void ToolTip::showToolTip()
if (window() && window()->screen()) {
pos = window()->mapToGlobal(pos);
}
QPoint popupPos = dlg->popupPosition(this, Qt::AlignCenter);
QPoint popupPos = dlg->popupPosition(this, dlg->size(), Qt::AlignCenter);
if (pos.y() + height() <= popupPos.y()) {
dir = Plasma::Types::Down;

View File

@ -42,6 +42,13 @@ ToolTipDialog::ToolTipDialog(QQuickItem *parent)
setDirection(Plasma::Types::Up);
m_frameSvgItem->setImagePath("widgets/tooltip");
m_animation = new QPropertyAnimation(this);
connect(m_animation, SIGNAL(valueChanged(QVariant)),
this, SLOT(valueChanged(QVariant)));
m_animation->setTargetObject(this);
m_animation->setEasingCurve(QEasingCurve::InOutQuad);
m_animation->setDuration(250);
m_showTimer = new QTimer(this);
m_showTimer->setSingleShot(true);
connect(m_showTimer, &QTimer::timeout, [=]() {
@ -86,7 +93,6 @@ void ToolTipDialog::showEvent(QShowEvent *event)
{
m_showTimer->start(m_hideTimeout);
m_animation->stop();
DialogProxy::showEvent(event);
}
@ -117,15 +123,6 @@ void ToolTipDialog::adjustGeometry(const QRect &geom)
}
if (isVisible()) {
if (!m_animation) {
m_animation = new QPropertyAnimation(this);
connect(m_animation, SIGNAL(valueChanged(QVariant)),
this, SLOT(valueChanged(QVariant)));
m_animation->setTargetObject(this);
m_animation->setEasingCurve(QEasingCurve::InOutQuad);
m_animation->setDuration(250);
}
resize(geom.size());