From 7e0abc7cde4a9c02fabc27cc2b6506bd41f0588d Mon Sep 17 00:00:00 2001 From: Arno Date: Sat, 23 Oct 2010 15:09:49 +0200 Subject: Fix hover issues Hopefully this commit fixes all issues with hovering over items. First, only use QCursor::pos() to determine the position of the hover window and fix position calculation accordingly. For that SmGlobals now return a QSize of the actual cursor size. Introduced a hoverOffeset to HoverWindow defaulting to SmGlobals::cursorSize() + 30 to prevent a HoverLeave event on showing the HoverWindow. Also fixed Qt::WindowFlags of HoverWindow. We don't want the HoverWindow to show in the taskbar or get sent to background when clicking on an item. --- hoverwindow.cpp | 43 ++++++++++++++++++++++++++++--------------- 1 file changed, 28 insertions(+), 15 deletions(-) (limited to 'hoverwindow.cpp') diff --git a/hoverwindow.cpp b/hoverwindow.cpp index 7f2e4af..6ca6bce 100644 --- a/hoverwindow.cpp +++ b/hoverwindow.cpp @@ -9,21 +9,30 @@ #include #include #include +#include #include "hoverwindow.h" +#include "smglobals.h" -HoverWindow::HoverWindow(QWidget *parent, Qt::WindowFlags f) : QWidget(parent, f){ +HoverWindow::HoverWindow(QWidget *parent, Qt::WindowFlags f) : QWidget(parent, f), mAlignCenter(false), mDesktopHeight(-1){ setVisible(false); setWindowOpacity(0.7); + setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum); setStyleSheet("QLabel { background-color: #D6A583; color: black; border-width: 2px; border-style: solid; padding: 4px; }"); - QHBoxLayout *mainLayout = new QHBoxLayout; - mainLayout->setContentsMargins(4, 4, 4, 4); + QDesktopWidget *desktop = qApp->desktop(); + mDesktopHeight = desktop->availableGeometry().height(); + QSize curSize = SmGlobals::instance()->cursorSize(); + setHoverOffset(QPoint(curSize.width() + 30, 0)); + mMainLayout = new QHBoxLayout; + mMainLayout->setSizeConstraint(QLayout::SetMinAndMaxSize); + mMainLayout->setContentsMargins(4, 4, 4, 4); mLabel = new QLabel; - mainLayout->addWidget(mLabel); - setLayout(mainLayout); + mMainLayout->addWidget(mLabel); + setLayout(mMainLayout); } void HoverWindow::setContent(const QString &parent, const QStringList &children){ + mAlignCenter = false; QString curText = QString("

%1

").arg(parent); curText.append("
    "); int count = qMin(6, children.size()); @@ -39,6 +48,7 @@ void HoverWindow::setContent(const QString &parent, const QStringList &children) } void HoverWindow::setPixmap(const QPixmap &pm){ + mAlignCenter = true; QPixmap curPm = pm; if(curPm.height() > 500){ curPm = curPm.scaledToHeight(500); @@ -47,6 +57,8 @@ void HoverWindow::setPixmap(const QPixmap &pm){ curPm = curPm.scaledToWidth(300); } mLabel->setPixmap(curPm); + setMaximumSize(curPm.width() + 10, curPm.height() + 10); + setMinimumSize(curPm.width() + 10, curPm.height() + 10); } void HoverWindow::setData(const QList &data){ @@ -72,16 +84,17 @@ int HoverWindow::pixmapHeight() const{ return pm ? pm->height() : 0; } -void HoverWindow::setPos(const QPoint &cursorPos){ - if(cursorPos.y() - height() / 2 < 0){ - move(QPoint(cursorPos.x(), 0)); - return; +void HoverWindow::setPos(){ + const QPoint globalPos(QCursor::pos()); + QPoint hoverPos(globalPos.x() + mHoverOffset.x(), globalPos.y() + mHoverOffset.y()); + if(mAlignCenter){ + hoverPos = QPoint(hoverPos.x(), hoverPos.y() - height() / 2); } - QDesktopWidget *desktop = qApp->desktop(); - int desktopHeight = desktop->availableGeometry().height(); - if(cursorPos.y() + height() / 2 > desktopHeight){ - move(QPoint(cursorPos.x(), desktopHeight - height())); - return; + if(hoverPos.y() < 0){ + hoverPos = QPoint(hoverPos.x(), 0); + } + if(hoverPos.y() + height() > mDesktopHeight){ + hoverPos = QPoint(hoverPos.x(), mDesktopHeight - height()); } - move(cursorPos); + move(hoverPos); } -- cgit v1.2.3-70-g09d2