diff options
author | Arno <am@disconnect.de> | 2010-10-23 15:09:49 +0200 |
---|---|---|
committer | Arno <am@disconnect.de> | 2010-10-23 15:09:49 +0200 |
commit | 7e0abc7cde4a9c02fabc27cc2b6506bd41f0588d (patch) | |
tree | dff223d42a9515f961ded35eaae9ceba77824580 /hoverwindow.cpp | |
parent | 9092371858356efde3e2d6d002f692308127389c (diff) | |
download | SheMov-7e0abc7cde4a9c02fabc27cc2b6506bd41f0588d.tar.gz SheMov-7e0abc7cde4a9c02fabc27cc2b6506bd41f0588d.tar.bz2 SheMov-7e0abc7cde4a9c02fabc27cc2b6506bd41f0588d.zip |
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.
Diffstat (limited to 'hoverwindow.cpp')
-rw-r--r-- | hoverwindow.cpp | 43 |
1 files changed, 28 insertions, 15 deletions
diff --git a/hoverwindow.cpp b/hoverwindow.cpp index 7f2e4af..6ca6bce 100644 --- a/hoverwindow.cpp +++ b/hoverwindow.cpp @@ -9,21 +9,30 @@ #include <QLabel> #include <QApplication> #include <QDesktopWidget> +#include <QSize> #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("<p style=\"align: center; text-decoration: underline; font-weight: bold; margin-bottom: 0;\">%1</p>").arg(parent); curText.append("<ul style=\"margin-left: -25; margin-top: 0px;\">"); 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<QVariant> &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); } |