UI: GraphWindow: Simplify checkWindowEdges()

This commit is contained in:
Nodir Temirkhodjaev 2023-05-29 16:49:13 +03:00
parent 4e96330494
commit c95ff73532

View File

@ -18,6 +18,34 @@
namespace { namespace {
constexpr int stickyDistance = 30;
inline void checkWindowHorizontalEdges(const QRect &screenRect, const QRect &winRect, QPoint &diff)
{
const int leftDiff = screenRect.x() - winRect.x();
if (qAbs(leftDiff) < stickyDistance) {
diff.setX(leftDiff);
} else {
const int rightDiff = screenRect.width() - winRect.right();
if (qAbs(rightDiff) < stickyDistance) {
diff.setX(rightDiff);
}
}
}
inline void checkWindowVerticalEdges(const QRect &screenRect, const QRect &winRect, QPoint &diff)
{
const int topDiff = screenRect.y() - winRect.y();
if (qAbs(topDiff) < stickyDistance) {
diff.setY(topDiff);
} else {
const int bottomDiff = screenRect.height() - winRect.bottom();
if (qAbs(bottomDiff) < stickyDistance) {
diff.setY(bottomDiff);
}
}
}
bool clearGraphData( bool clearGraphData(
const QSharedPointer<QCPBarsDataContainer> &data, double rangeLowerKey, double unixTimeKey) const QSharedPointer<QCPBarsDataContainer> &data, double rangeLowerKey, double unixTimeKey)
{ {
@ -404,38 +432,19 @@ void GraphWindow::setWindowOpacityPercent(int percent)
void GraphWindow::checkWindowEdges() void GraphWindow::checkWindowEdges()
{ {
constexpr int stickyDistance = 30;
const auto screen = this->screen(); const auto screen = this->screen();
if (!screen) if (!screen)
return; return;
const QRect sg = screen->geometry(); const QRect screenRect = screen->geometry();
const QRect wg = this->frameGeometry(); const QRect winRect = this->frameGeometry();
QPoint diff(0, 0); QPoint diff(0, 0);
const int leftDiff = sg.x() - wg.x(); checkWindowHorizontalEdges(screenRect, winRect, diff);
if (qAbs(leftDiff) < stickyDistance) { checkWindowVerticalEdges(screenRect, winRect, diff);
diff.setX(leftDiff);
} else {
const int rightDiff = sg.width() - wg.right();
if (qAbs(rightDiff) < stickyDistance) {
diff.setX(rightDiff);
}
}
const int topDiff = sg.y() - wg.y();
if (qAbs(topDiff) < stickyDistance) {
diff.setY(topDiff);
} else {
const int bottomDiff = sg.height() - wg.bottom();
if (qAbs(bottomDiff) < stickyDistance) {
diff.setY(bottomDiff);
}
}
if (diff.x() != 0 || diff.y() != 0) { if (diff.x() != 0 || diff.y() != 0) {
this->move(wg.x() + diff.x(), wg.y() + diff.y()); this->move(winRect.x() + diff.x(), winRect.y() + diff.y());
} }
} }