MainWindow: decide DPI on the screen where vnote starts

This commit is contained in:
Le Tan 2021-12-06 21:04:13 +08:00
parent 956842f102
commit fa238b3e22
3 changed files with 23 additions and 14 deletions

View File

@ -8,6 +8,8 @@
#include <QDateTime>
#include <QSysInfo>
#include <QProcess>
#include <QWebEngineSettings>
#include <QWindow>
#include <core/configmgr.h>
#include <core/mainconfig.h>
@ -17,8 +19,8 @@
#include <core/vnotex.h>
#include <core/logger.h>
#include <widgets/mainwindow.h>
#include <QWebEngineSettings>
#include <core/exception.h>
#include <utils/widgetutils.h>
#include <widgets/messageboxhelper.h>
#include "commandlineoptions.h"
#include "application.h"
@ -145,17 +147,8 @@ int main(int argc, char *argv[])
// Should set the correct locale before VNoteX::getInst().
loadTranslators(app);
if (app.styleSheet().isEmpty()) {
auto style = VNoteX::getInst().getThemeMgr().fetchQtStyleSheet();
if (!style.isEmpty()) {
app.setStyleSheet(style);
}
}
MainWindow window;
window.show();
VNoteX::getInst().getThemeMgr().setBaseBackground(window.palette().color(QPalette::Base));
QObject::connect(&guard, &SingleInstanceGuard::showRequested,
&window, &MainWindow::showMainWindow);
@ -167,6 +160,19 @@ int main(int argc, char *argv[])
window.openFiles(QStringList() << p_filePath);
});
// Let MainWindow show first to decide the screen on which app is running.
WidgetUtils::calculateScaleFactor(window.windowHandle()->screen());
if (app.styleSheet().isEmpty()) {
auto style = VNoteX::getInst().getThemeMgr().fetchQtStyleSheet();
if (!style.isEmpty()) {
app.setStyleSheet(style);
WidgetUtils::updateStyle(&window);
}
}
VNoteX::getInst().getThemeMgr().setBaseBackground(window.palette().color(QPalette::Base));
window.kickOffOnStart(cmdOptions.m_pathsToOpen);
int ret = app.exec();

View File

@ -45,12 +45,14 @@ void WidgetUtils::updateStyle(QWidget *p_widget)
p_widget->update();
}
qreal WidgetUtils::calculateScaleFactor(bool p_update)
qreal WidgetUtils::calculateScaleFactor(const QScreen *p_screen)
{
static qreal factor = -1;
if (factor < 0 || p_update) {
factor = QGuiApplication::primaryScreen()->devicePixelRatio();
if (factor < 0 || p_screen) {
auto screen = p_screen ? p_screen : QGuiApplication::primaryScreen();
factor = screen->devicePixelRatio();
qDebug() << screen->name() << "dpi" << factor;
}
return factor;

View File

@ -21,6 +21,7 @@ class QLineEdit;
class QLayout;
class QPushButton;
class QSplitter;
class QScreen;
namespace vnotex
{
@ -35,7 +36,7 @@ namespace vnotex
static void updateStyle(QWidget *p_widget);
static qreal calculateScaleFactor(bool p_update = false);
static qreal calculateScaleFactor(const QScreen *p_screen = nullptr);
static bool isScrollBarVisible(QAbstractScrollArea *p_widget, bool p_horizontal);