syncthingtray/tray/gui/traymenu.cpp

103 lines
2.6 KiB
C++
Raw Normal View History

2016-08-30 20:01:07 +02:00
#include "./traymenu.h"
#include "./trayicon.h"
2017-05-01 03:34:43 +02:00
#include "./traywidget.h"
2016-08-30 20:01:07 +02: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>
#include <QPaintEvent>
#include <QPainter>
#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 {
TrayMenu::TrayMenu(TrayIcon *trayIcon, QWidget *parent)
2017-05-01 03:34:43 +02:00
: QMenu(parent)
, m_trayIcon(trayIcon)
, m_windowed(false)
2016-08-30 20:01:07 +02:00
{
setObjectName(QStringLiteral("QtGui::TrayMenu"));
auto *const menuLayout = new QHBoxLayout;
menuLayout->setContentsMargins(0, 0, 0, 0);
menuLayout->setSpacing(0);
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);
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());
}
}
void TrayMenu::showUsingPositioningSettings()
2016-12-26 19:50:10 +01:00
{
resize(sizeHint());
auto pos = Settings::values().appearance.positioning.positionToUse();
if (pos.has_value()) {
moveInside(pos.value(), size(), availableScreenGeometryAtPoint(pos.value()));
popup(pos.value());
} else {
show();
}
activateWindow();
2016-12-26 19:50:10 +01:00
}
void TrayMenu::setWindowed(bool windowed)
{
if (m_windowed != windowed) {
setWindowFlags((m_windowed = windowed) ? Qt::Window : Qt::FramelessWindowHint | Qt::Popup);
}
}
void TrayMenu::mousePressEvent(QMouseEvent *event)
{
if (!m_windowed) {
QMenu::mousePressEvent(event);
}
}
void TrayMenu::paintEvent(QPaintEvent *event)
{
if (!m_windowed) {
QMenu::paintEvent(event);
} else {
QPainter(this).fillRect(event->rect(), palette().window());
QWidget::paintEvent(event);
}
}
void TrayMenu::mouseReleaseEvent(QMouseEvent *event)
{
if (!m_windowed) {
QMenu::mouseReleaseEvent(event);
}
}
} // namespace QtGui