REFAC(client): Move utility functions to own file

This commit is contained in:
Robert Adam 2021-03-15 16:50:59 +01:00
parent 003b6c6bb2
commit cdd37615dd
4 changed files with 68 additions and 24 deletions

View File

@ -185,6 +185,8 @@ set(MUMBLE_SOURCES
"PTTButtonWidget.cpp"
"PTTButtonWidget.h"
"PTTButtonWidget.ui"
"QtWidgetUtils.cpp"
"QtWidgetUtils.h"
"RichTextEditor.cpp"
"RichTextEditor.h"
"RichTextEditorLink.ui"

View File

@ -0,0 +1,31 @@
// Copyright 2021 The Mumble Developers. All rights reserved.
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file at the root of the
// Mumble source tree or at <https://www.mumble.info/LICENSE>.
#include "QtWidgetUtils.h"
#include <QGuiApplication>
#include <QScreen>
namespace Mumble {
namespace QtUtils {
QScreen *screenAt(QPoint point) {
#if QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)
// screenAt was only introduced in Qt 5.10
return QGuiApplication::screenAt(point);
#else
for (QScreen *currentScreen : QGuiApplication::screens()) {
if (currentScreen->availableGeometry().contains(point)) {
return currentScreen;
}
}
return nullptr;
#endif
}
bool positionIsOnScreen(QPoint point) { return screenAt(point) != nullptr; }
}; // namespace QtUtils
}; // namespace Mumble

View File

@ -0,0 +1,29 @@
// Copyright 2021 The Mumble Developers. All rights reserved.
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file at the root of the
// Mumble source tree or at <https://www.mumble.info/LICENSE>.
#ifndef MUMBLE_MUMBLE_QTWIDGETUTILS_H_
#define MUMBLE_MUMBLE_QTWIDGETUTILS_H_
#include <QPoint>
class QScreen;
namespace Mumble {
namespace QtUtils {
/**
* @returns The QScreen the given position is on or nullptr if there is no such screen
*/
QScreen *screenAt(QPoint position);
/**
* @returns Whether the given position is on any screen (as opposed to off-screen)
*/
bool positionIsOnScreen(QPoint position);
}; // namespace QtUtils
}; // namespace Mumble
#endif // MUMBLE_MUMBLE_QTWIDGETUTILS_H_

View File

@ -37,6 +37,7 @@
#include "NetworkConfig.h"
#include "PluginInstaller.h"
#include "PluginManager.h"
#include "QtWidgetUtils.h"
#include "SSL.h"
#include "SocketRPC.h"
#include "TalkingUI.h"
@ -72,29 +73,10 @@ void throw_exception(std::exception const &) {
extern void os_init();
extern char *os_lang;
QScreen *screenAt(QPoint point) {
#if QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)
// screenAt was only introduced in Qt 5.10
return QGuiApplication::screenAt(point);
#else
for (QScreen *currentScreen : QGuiApplication::screens()) {
if (currentScreen->availableGeometry().contains(point)) {
return currentScreen;
}
}
return nullptr;
#endif
}
bool positionIsOnScreen(QPoint point) {
return screenAt(point) != nullptr;
}
QPoint getTalkingUIPosition() {
QPoint talkingUIPos = QPoint(0, 0);
if (Global::get().s.qpTalkingUI_Position != Settings::UNSPECIFIED_POSITION
&& positionIsOnScreen(Global::get().s.qpTalkingUI_Position)) {
&& Mumble::QtUtils::positionIsOnScreen(Global::get().s.qpTalkingUI_Position)) {
// Restore last position
talkingUIPos = Global::get().s.qpTalkingUI_Position;
} else {
@ -104,7 +86,7 @@ QPoint getTalkingUIPosition() {
const QPoint defaultPos =
QPoint(mainWindowPos.x() + Global::get().mw->size().width() + horizontalBuffer, mainWindowPos.y());
if (positionIsOnScreen(defaultPos)) {
if (Mumble::QtUtils::positionIsOnScreen(defaultPos)) {
talkingUIPos = defaultPos;
}
}
@ -115,18 +97,18 @@ QPoint getTalkingUIPosition() {
const QSize talkingUISize = Global::get().talkingUI->size();
// The screen should always be found at this point as we have chosen to pos to be on a screen
const QScreen *screen = screenAt(talkingUIPos);
const QScreen *screen = Mumble::QtUtils::screenAt(talkingUIPos);
const QRect screenGeom = screen ? screen->availableGeometry() : QRect(0, 0, 0, 0);
// Check whether the TalkingUI fits on the screen in x-direction
if (!positionIsOnScreen(talkingUIPos + QPoint(talkingUISize.width(), 0))) {
if (!Mumble::QtUtils::positionIsOnScreen(talkingUIPos + QPoint(talkingUISize.width(), 0))) {
int overlap = talkingUIPos.x() + talkingUISize.width() - screenGeom.x() - screenGeom.width();
// Correct the x coordinate but don't move it below 0
talkingUIPos.setX(std::max(talkingUIPos.x() - overlap, 0));
}
// Check whether the TalkingUI fits on the screen in y-direction
if (!positionIsOnScreen(talkingUIPos + QPoint(0, talkingUISize.height()))) {
if (!Mumble::QtUtils::positionIsOnScreen(talkingUIPos + QPoint(0, talkingUISize.height()))) {
int overlap = talkingUIPos.y() + talkingUISize.height() - screenGeom.y() - screenGeom.height();
// Correct the x coordinate but don't move it below 0