Files
c-evo-evo/widget/src/DpiForms.cpp
T

190 lines
4.9 KiB
C++

// This file is part of c-evo-evo.
//
// This software is governed by the CeCILL license under French law and
// abiding by the rules of distribution of free software. You can use,
// modify and/or redistribute the software under the terms of the CeCILL
// license as circulated by CEA, CNRS and INRIA at the following URL
// "http://www.cecill.info".
//
// As a counterpart to the access to the source code and rights to copy,
// modify and redistribute granted by the license, users are provided only
// with a limited warranty and the software's author, the holder of the
// economic rights, and the successive licensors have only limited
// liability.
//
// In this respect, the user's attention is drawn to the risks associated
// with loading, using, modifying and/or developing or reproducing the
// software by the user in light of its specific status of free software,
// that may mean that it is complicated to manipulate, and that also
// therefore means that it is reserved for developers and experienced
// professionals having in-depth computer knowledge. Users are therefore
// encouraged to load and test the software's suitability as regards their
// requirements in conditions enabling the security of their systems and/or
// data to be ensured and, more generally, to use and operate it in the
// same conditions as regards security.
//
// The fact that you are presently reading this means that you have had
// knowledge of the CeCILL license and that you accept its terms.
#include "Dpi/DpiForms.h"
#include <QApplication>
#include <QCloseEvent>
#include <QEventLoop>
#include <QGuiApplication>
#include <QKeyEvent>
#include <QScreen>
#include <QtMath>
namespace Dpi {
Form::Form(QWidget *parent) : QWidget(parent) {}
Form::~Form()
{
if (m_modalLoop)
m_modalLoop->quit();
}
ModalResult Form::showModal()
{
setWindowModality(Qt::ApplicationModal);
m_modalResult = ModalResult::None;
show();
QEventLoop loop;
m_modalLoop = &loop;
loop.exec();
m_modalLoop = nullptr;
return m_modalResult;
}
void Form::showEvent(QShowEvent *event)
{
QWidget::showEvent(event);
if (onShow)
onShow();
}
void Form::hideEvent(QHideEvent *event)
{
QWidget::hideEvent(event);
if (onHide)
onHide();
if (m_modalLoop)
m_modalLoop->quit();
}
void Form::closeEvent(QCloseEvent *event)
{
if (onCloseQuery && !onCloseQuery()) {
event->ignore();
return;
}
if (onClose)
onClose();
QWidget::closeEvent(event);
}
void Form::keyPressEvent(QKeyEvent *event)
{
if (onKeyDown)
onKeyDown(event);
QWidget::keyPressEvent(event);
}
void Form::keyReleaseEvent(QKeyEvent *event)
{
if (onKeyUp)
onKeyUp(event);
QWidget::keyReleaseEvent(event);
}
bool Form::event(QEvent *event)
{
if (event->type() == QEvent::WindowActivate && onActivate)
onActivate();
if (event->type() == QEvent::WindowDeactivate && onDeactivate)
onDeactivate();
return QWidget::event(event);
}
Form *Application::s_mainForm = nullptr;
void Application::run()
{
if (s_mainForm)
s_mainForm->show();
QApplication::exec();
}
void Application::processMessages()
{
QApplication::processEvents();
}
void Application::terminate()
{
QApplication::quit();
}
QString Application::title() { return QApplication::applicationDisplayName(); }
void Application::setTitle(const QString &value) { QApplication::setApplicationDisplayName(value); }
QString Application::exeName() { return QApplication::applicationFilePath(); }
int Monitor::left() const { return m_screen ? m_screen->geometry().left() : 0; }
int Monitor::top() const { return m_screen ? m_screen->geometry().top() : 0; }
int Monitor::width() const { return m_screen ? m_screen->geometry().width() : 0; }
int Monitor::height() const { return m_screen ? m_screen->geometry().height() : 0; }
QRect Monitor::boundsRect() const { return m_screen ? m_screen->geometry() : QRect(); }
int Screen::formCount()
{
int count = 0;
const auto widgets = QApplication::topLevelWidgets();
for (QWidget *widget : widgets) {
if (qobject_cast<Form *>(widget))
++count;
}
return count;
}
Form *Screen::activeForm()
{
return qobject_cast<Form *>(QApplication::activeWindow());
}
int Screen::dpi()
{
QScreen *screen = QGuiApplication::primaryScreen();
return screen ? qRound(screen->logicalDotsPerInch()) : 96;
}
int Screen::pixelsPerInch() { return dpi(); }
int Screen::width()
{
QScreen *screen = QGuiApplication::primaryScreen();
return screen ? screen->geometry().width() : 0;
}
int Screen::height()
{
QScreen *screen = QGuiApplication::primaryScreen();
return screen ? screen->geometry().height() : 0;
}
QRect Screen::desktopRect()
{
QScreen *screen = QGuiApplication::primaryScreen();
return screen ? screen->availableGeometry() : QRect();
}
Monitor Screen::primaryMonitor()
{
return Monitor(QGuiApplication::primaryScreen());
}
} // namespace Dpi