fixed #250 tooltips don't stay in window geometry

This commit is contained in:
fourtf 2018-01-22 15:06:36 +01:00
parent dc8086f8b6
commit d966c24bc3
4 changed files with 47 additions and 13 deletions

View file

@ -652,7 +652,7 @@ void ChannelView::mouseMoveEvent(QMouseEvent *event)
if (tooltip.isEmpty()) { if (tooltip.isEmpty()) {
tooltipWidget->hide(); tooltipWidget->hide();
} else { } else {
tooltipWidget->moveTo(event->globalPos()); tooltipWidget->moveTo(this, event->globalPos());
tooltipWidget->setText(tooltip); tooltipWidget->setText(tooltip);
tooltipWidget->show(); tooltipWidget->show();
} }

View file

@ -211,7 +211,7 @@ void SplitHeader::mouseMoveEvent(QMouseEvent *event)
{ {
if (!this->dragging && this->isLive) { if (!this->dragging && this->isLive) {
auto tooltipWidget = TooltipWidget::getInstance(); auto tooltipWidget = TooltipWidget::getInstance();
tooltipWidget->moveTo(event->globalPos()); tooltipWidget->moveTo(this, event->globalPos());
tooltipWidget->setText(tooltip); tooltipWidget->setText(tooltip);
tooltipWidget->show(); tooltipWidget->show();
} }

View file

@ -1,7 +1,8 @@
#include "tooltipwidget.hpp" #include "tooltipwidget.hpp"
#include "singletons/thememanager.hpp"
#include "singletons/fontmanager.hpp" #include "singletons/fontmanager.hpp"
#include "singletons/thememanager.hpp"
#include <QDesktopWidget>
#include <QStyle> #include <QStyle>
#include <QVBoxLayout> #include <QVBoxLayout>
@ -9,7 +10,7 @@ namespace chatterino {
namespace widgets { namespace widgets {
TooltipWidget::TooltipWidget(BaseWidget *parent) TooltipWidget::TooltipWidget(BaseWidget *parent)
: BaseWidget(parent) : BaseWindow(parent)
, displayText(new QLabel()) , displayText(new QLabel())
{ {
QColor black(0, 0, 0); QColor black(0, 0, 0);
@ -21,11 +22,12 @@ TooltipWidget::TooltipWidget(BaseWidget *parent)
this->setPalette(palette); this->setPalette(palette);
this->displayText->setStyleSheet("color: #ffffff"); this->displayText->setStyleSheet("color: #ffffff");
this->setWindowOpacity(0.8); this->setWindowOpacity(0.8);
this->setFont(singletons::FontManager::getInstance().getFont(singletons::FontManager::Type::MediumSmall, this->setFont(singletons::FontManager::getInstance().getFont(
this->getDpiMultiplier())); singletons::FontManager::Type::MediumSmall, this->getDpiMultiplier()));
this->setAttribute(Qt::WA_ShowWithoutActivating); this->setAttribute(Qt::WA_ShowWithoutActivating);
this->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint | Qt::X11BypassWindowManagerHint); this->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint |
Qt::X11BypassWindowManagerHint);
displayText->setAlignment(Qt::AlignHCenter); displayText->setAlignment(Qt::AlignHCenter);
displayText->setText("lmao xD"); displayText->setText("lmao xD");
@ -35,8 +37,8 @@ TooltipWidget::TooltipWidget(BaseWidget *parent)
this->setLayout(layout); this->setLayout(layout);
singletons::FontManager::getInstance().fontChanged.connect([this] { singletons::FontManager::getInstance().fontChanged.connect([this] {
this->setFont(singletons::FontManager::getInstance().getFont(singletons::FontManager::Type::MediumSmall, this->setFont(singletons::FontManager::getInstance().getFont(
this->getDpiMultiplier())); singletons::FontManager::Type::MediumSmall, this->getDpiMultiplier()));
}); });
} }
@ -45,12 +47,39 @@ void TooltipWidget::setText(QString text)
this->displayText->setText(text); this->displayText->setText(text);
} }
void TooltipWidget::moveTo(QPoint point) void TooltipWidget::moveTo(QWidget *parent, QPoint point)
{ {
point.rx() += 16; point.rx() += 16;
point.ry() += 16; point.ry() += 16;
this->move(point); this->move(point);
} }
void TooltipWidget::resizeEvent(QResizeEvent *)
{
this->moveIntoDesktopRect(this);
}
void TooltipWidget::moveIntoDesktopRect(QWidget *parent)
{
QDesktopWidget *desktop = QApplication::desktop();
QRect s = desktop->screenGeometry(parent);
QRect w = this->geometry();
if (w.left() < s.left()) {
w.moveLeft(s.left());
}
if (w.right() < s.right()) {
w.moveRight(s.right());
}
if (w.top() < s.top()) {
w.moveTop(s.top());
}
if (w.bottom() < s.bottom()) {
w.moveBottom(s.bottom());
}
this->setGeometry(w);
}
} // namespace widgets } // namespace widgets
} // namespace chatterino } // namespace chatterino

View file

@ -1,5 +1,5 @@
#pragma once #pragma once
#include "widgets/basewidget.hpp" #include "widgets/basewindow.hpp"
#include <QLabel> #include <QLabel>
#include <QWidget> #include <QWidget>
@ -7,14 +7,14 @@
namespace chatterino { namespace chatterino {
namespace widgets { namespace widgets {
class TooltipWidget : public BaseWidget class TooltipWidget : public BaseWindow
{ {
Q_OBJECT Q_OBJECT
public: public:
TooltipWidget(BaseWidget *parent = nullptr); TooltipWidget(BaseWidget *parent = nullptr);
void setText(QString text); void setText(QString text);
void moveTo(QPoint point); void moveTo(QWidget *widget, QPoint point);
static TooltipWidget *getInstance() static TooltipWidget *getInstance()
{ {
@ -25,8 +25,13 @@ public:
return tooltipWidget; return tooltipWidget;
} }
protected:
virtual void resizeEvent(QResizeEvent *) override;
private: private:
QLabel *displayText; QLabel *displayText;
void moveIntoDesktopRect(QWidget *parent);
}; };
} // namespace widgets } // namespace widgets