2016-08-30 20:01:07 +02:00
|
|
|
#include "./traymenu.h"
|
2016-09-24 16:19:23 +02:00
|
|
|
#include "./trayicon.h"
|
2017-05-01 03:34:43 +02:00
|
|
|
#include "./traywidget.h"
|
2016-08-30 20:01:07 +02:00
|
|
|
|
2021-01-25 19:48:11 +01:00
|
|
|
#include <syncthingwidgets/settings/settings.h>
|
2016-09-01 16:34:30 +02:00
|
|
|
|
2019-05-04 22:18:50 +02:00
|
|
|
#include <qtutilities/misc/dialogutils.h>
|
|
|
|
|
2016-12-26 19:50:10 +01:00
|
|
|
#include <QApplication>
|
2017-05-01 03:34:43 +02:00
|
|
|
#include <QHBoxLayout>
|
2022-12-10 12:07:06 +01:00
|
|
|
#include <QPaintEvent>
|
|
|
|
#include <QPainter>
|
2022-11-29 23:06:15 +01:00
|
|
|
#include <QWindow>
|
2016-08-30 20:01:07 +02:00
|
|
|
|
2019-06-10 22:48:26 +02:00
|
|
|
using namespace QtUtilities;
|
|
|
|
|
2016-08-30 20:01:07 +02:00
|
|
|
namespace QtGui {
|
|
|
|
|
2018-05-15 22:01:03 +02:00
|
|
|
TrayMenu::TrayMenu(TrayIcon *trayIcon, QWidget *parent)
|
2017-05-01 03:34:43 +02:00
|
|
|
: QMenu(parent)
|
2018-01-27 23:27:50 +01:00
|
|
|
, m_trayIcon(trayIcon)
|
2023-02-18 21:27:09 +01:00
|
|
|
, m_windowType(WindowType::Popup)
|
2016-08-30 20:01:07 +02:00
|
|
|
{
|
2019-07-31 20:47:19 +02:00
|
|
|
setObjectName(QStringLiteral("QtGui::TrayMenu"));
|
2018-04-02 20:26:00 +02:00
|
|
|
auto *const menuLayout = new QHBoxLayout;
|
2019-06-25 13:49:53 +02:00
|
|
|
menuLayout->setContentsMargins(0, 0, 0, 0);
|
2018-04-02 20:26:00 +02:00
|
|
|
menuLayout->setSpacing(0);
|
2018-05-15 22:01:03 +02:00
|
|
|
menuLayout->addWidget(m_trayWidget = new TrayWidget(this));
|
2016-08-30 20:01:07 +02:00
|
|
|
setLayout(menuLayout);
|
|
|
|
setPlatformMenu(nullptr);
|
2019-07-28 10:56:59 +02:00
|
|
|
setWindowFlags(Qt::FramelessWindowHint | Qt::Popup);
|
2022-11-29 23:06:15 +01:00
|
|
|
setWindowIcon(m_trayWidget->windowIcon());
|
2016-08-30 20:01:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QSize TrayMenu::sizeHint() const
|
|
|
|
{
|
2016-11-02 20:03:38 +01:00
|
|
|
return Settings::values().appearance.trayMenuSize;
|
2016-08-30 20:01:07 +02:00
|
|
|
}
|
|
|
|
|
2016-12-26 19:50:10 +01:00
|
|
|
/*!
|
|
|
|
* \brief Moves the specified \a innerRect at the specified \a point into the specified \a outerRect
|
|
|
|
* by altering \a point.
|
|
|
|
*/
|
|
|
|
void moveInside(QPoint &point, const QSize &innerRect, const QRect &outerRect)
|
|
|
|
{
|
2017-05-01 03:34:43 +02:00
|
|
|
if (point.y() < outerRect.top()) {
|
2016-12-26 19:50:10 +01:00
|
|
|
point.setY(outerRect.top());
|
2017-05-01 03:34:43 +02:00
|
|
|
} else if (point.y() + innerRect.height() > outerRect.bottom()) {
|
2016-12-26 19:50:10 +01:00
|
|
|
point.setY(outerRect.bottom() - innerRect.height());
|
|
|
|
}
|
2017-05-01 03:34:43 +02:00
|
|
|
if (point.x() < outerRect.left()) {
|
2016-12-26 19:50:10 +01:00
|
|
|
point.setX(outerRect.left());
|
2017-05-01 03:34:43 +02:00
|
|
|
} else if (point.x() + innerRect.width() > outerRect.right()) {
|
2016-12-26 19:50:10 +01:00
|
|
|
point.setX(outerRect.right() - innerRect.width());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-22 17:28:09 +02:00
|
|
|
void TrayMenu::showUsingPositioningSettings()
|
2016-12-26 19:50:10 +01:00
|
|
|
{
|
|
|
|
resize(sizeHint());
|
2019-06-22 17:28:09 +02:00
|
|
|
auto pos = Settings::values().appearance.positioning.positionToUse();
|
2022-11-29 23:32:14 +01:00
|
|
|
if (pos.has_value()) {
|
|
|
|
moveInside(pos.value(), size(), availableScreenGeometryAtPoint(pos.value()));
|
|
|
|
popup(pos.value());
|
|
|
|
} else {
|
|
|
|
show();
|
|
|
|
}
|
2022-10-07 01:06:13 +02:00
|
|
|
activateWindow();
|
2016-12-26 19:50:10 +01:00
|
|
|
}
|
2022-10-07 01:06:13 +02:00
|
|
|
|
2023-02-18 21:27:09 +01:00
|
|
|
void TrayMenu::setWindowType(int windowType)
|
2022-10-07 01:06:13 +02:00
|
|
|
{
|
2023-02-18 21:27:09 +01:00
|
|
|
if (windowType >= 0 && windowType <= 2) {
|
|
|
|
setWindowType(static_cast<WindowType>(windowType));
|
2022-11-29 23:06:15 +01:00
|
|
|
}
|
2022-10-07 01:06:13 +02:00
|
|
|
}
|
|
|
|
|
2023-02-18 21:27:09 +01:00
|
|
|
void TrayMenu::setWindowType(WindowType windowType)
|
|
|
|
{
|
|
|
|
if (m_windowType == windowType) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto flags = Qt::WindowFlags();
|
|
|
|
switch (m_windowType = windowType) {
|
|
|
|
case WindowType::Popup:
|
|
|
|
flags = Qt::FramelessWindowHint | Qt::Popup;
|
|
|
|
break;
|
|
|
|
case WindowType::NormalWindow:
|
|
|
|
flags = Qt::Window;
|
|
|
|
break;
|
|
|
|
case WindowType::CustomWindow:
|
|
|
|
flags = Qt::Dialog | Qt::CustomizeWindowHint;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
setWindowFlags(flags);
|
|
|
|
}
|
|
|
|
|
2022-10-07 01:06:13 +02:00
|
|
|
void TrayMenu::mousePressEvent(QMouseEvent *event)
|
|
|
|
{
|
2023-02-18 21:27:09 +01:00
|
|
|
if (m_windowType != TrayMenu::WindowType::NormalWindow) {
|
2022-10-07 01:06:13 +02:00
|
|
|
QMenu::mousePressEvent(event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-18 21:27:09 +01:00
|
|
|
void TrayMenu::mouseReleaseEvent(QMouseEvent *event)
|
|
|
|
{
|
|
|
|
if (m_windowType != TrayMenu::WindowType::NormalWindow) {
|
|
|
|
QMenu::mouseReleaseEvent(event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-10 12:07:06 +01:00
|
|
|
void TrayMenu::paintEvent(QPaintEvent *event)
|
|
|
|
{
|
2023-02-18 21:27:09 +01:00
|
|
|
if (m_windowType == WindowType::Popup) {
|
2022-12-10 12:07:06 +01:00
|
|
|
QMenu::paintEvent(event);
|
|
|
|
} else {
|
|
|
|
QPainter(this).fillRect(event->rect(), palette().window());
|
|
|
|
QWidget::paintEvent(event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-18 21:27:09 +01:00
|
|
|
void TrayMenu::focusOutEvent(QFocusEvent *)
|
2022-10-07 01:06:13 +02:00
|
|
|
{
|
2023-02-18 21:27:09 +01:00
|
|
|
if (m_windowType == WindowType::CustomWindow) {
|
|
|
|
if (const auto *fw = focusWidget(); fw->hasFocus()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
close();
|
2022-10-07 01:06:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-17 21:48:15 +02:00
|
|
|
} // namespace QtGui
|