moved menu code to ripple effect button

This commit is contained in:
fourtf
2018-08-07 23:46:00 +02:00
parent e1b8faacc9
commit 7a9af4ae84
13 changed files with 198 additions and 170 deletions
+43
View File
@@ -1,9 +1,12 @@
#include "RippleEffectButton.hpp"
#include <QApplication>
#include <QDebug>
#include <QDesktopWidget>
#include <QPainter>
#include "singletons/Theme.hpp"
#include "util/FunctionEventFilter.hpp"
namespace chatterino {
@@ -76,6 +79,20 @@ const QColor &RippleEffectButton::getBorderColor() const
return this->borderColor_;
}
void RippleEffectButton::setMenu(std::unique_ptr<QMenu> menu)
{
this->menu_ = std::move(menu);
this->menu_->installEventFilter(
new FunctionEventFilter(this, [this](QObject *, QEvent *event) {
if (event->type() == QEvent::Hide) {
QTimer::singleShot(20, this,
[this] { this->menuVisible_ = false; });
}
return false;
}));
}
void RippleEffectButton::paintEvent(QPaintEvent *)
{
QPainter painter(this);
@@ -177,6 +194,10 @@ void RippleEffectButton::mousePressEvent(QMouseEvent *event)
this->mouseDown_ = true;
emit this->leftMousePress();
if (this->menu_ && !this->menuVisible_) {
QTimer::singleShot(80, this, [this] { this->showMenu(); });
}
}
void RippleEffectButton::mouseReleaseEvent(QMouseEvent *event)
@@ -251,4 +272,26 @@ void RippleEffectButton::onMouseEffectTimeout()
}
}
void RippleEffectButton::showMenu()
{
if (!this->menu_) return;
auto point = [this] {
auto bounds = QApplication::desktop()->availableGeometry(this);
auto point = this->mapToGlobal(
QPoint(this->width() - this->menu_->width(), this->height()));
if (point.y() + this->menu_->height() > bounds.bottom()) {
point.setY(point.y() - this->menu_->height() - this->height());
}
return point;
};
this->menu_->popup(point());
this->menu_->move(point());
this->menuVisible_ = true;
}
} // namespace chatterino
+18 -12
View File
@@ -4,6 +4,7 @@
#include "widgets/BaseWidget.hpp"
#include <QMenu>
#include <QMouseEvent>
#include <QPainter>
#include <QPoint>
@@ -43,6 +44,8 @@ public:
void setBorderColor(const QColor &color);
const QColor &getBorderColor() const;
void setMenu(std::unique_ptr<QMenu> menu);
signals:
void clicked();
void leftMousePress();
@@ -57,22 +60,25 @@ protected:
void fancyPaint(QPainter &painter);
bool enabled_ = true;
bool selected_ = false;
bool mouseOver_ = false;
bool mouseDown_ = false;
bool enabled_{true};
bool selected_{false};
bool mouseOver_{false};
bool mouseDown_{false};
bool menuVisible_{false};
private:
void onMouseEffectTimeout();
void showMenu();
QColor borderColor_;
QPixmap pixmap_;
bool dimPixmap_ = true;
QPoint mousePos_;
double hoverMultiplier_ = 0.0;
QTimer effectTimer_;
std::vector<ClickEffect> clickEffects_;
boost::optional<QColor> mouseEffectColor_ = boost::none;
QColor borderColor_{};
QPixmap pixmap_{};
bool dimPixmap_{true};
QPoint mousePos_{};
double hoverMultiplier_{0.0};
QTimer effectTimer_{};
std::vector<ClickEffect> clickEffects_{};
boost::optional<QColor> mouseEffectColor_{};
std::unique_ptr<QMenu> menu_{};
};
} // namespace chatterino