syncthingtray/tray/gui/traymenu.cpp

61 lines
1.7 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>
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)
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);
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();
2019-06-10 22:48:26 +02:00
moveInside(pos, size(), availableScreenGeometryAtPoint(pos));
2016-12-26 19:50:10 +01:00
popup(pos);
}
} // namespace QtGui