main.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #include "app.h"
  2. #include "filesystem.h"
  3. #include <QApplication>
  4. #include <QFile>
  5. #include <QTextCodec>
  6. #include <QLockFile>
  7. #include <QtGlobal>
  8. #include <QDebug>
  9. #include <QScreen>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <QThread>
  13. #include <QtConcurrent/QtConcurrentRun>
  14. void myMessageOutput(QtMsgType type, const QMessageLogContext &, const QString &msg)
  15. {
  16. QByteArray localMsg = msg.toUtf8();
  17. const char * format = "[" + QDate::currentDate().toString(Qt::ISODate).toUtf8() + " " + QTime::currentTime().toString(Qt::ISODate).toUtf8() + "]";
  18. switch (type) {
  19. case QtDebugMsg:
  20. fprintf(stderr, "%s DEB: %s\n", format, localMsg.constData());
  21. break;
  22. case QtInfoMsg:
  23. fprintf(stderr, "%s INF: %s\n", format, localMsg.constData());
  24. break;
  25. case QtWarningMsg:
  26. fprintf(stderr, "%s WAR: %s\n", format, localMsg.constData());
  27. break;
  28. case QtCriticalMsg:
  29. fprintf(stderr, "%s CRT: %s\n", format, localMsg.constData());
  30. break;
  31. case QtFatalMsg:
  32. fprintf(stderr, "%s FAT: %s\n", format, localMsg.constData());
  33. abort();
  34. }
  35. }
  36. int main(int argc, char *argv[]){
  37. //if (argc <= 1 || (argc > 1 && std::string(argv[1]) != std::string("-S") && std::string(argv[1]) != "-prelaunched"))
  38. // return 0;
  39. // QApplication::setAttribute(Qt::AA_EnableHighDpiScaling, true);
  40. float scale_factor = 1;
  41. if (!FileSystem::fileExists("dpi.stml")) {
  42. QApplication a(argc, argv);
  43. std::vector<float> dpi_scales = {1, 1.25, 1.5, 2, 2.5};
  44. float current_dpi = ((QGuiApplication*)QCoreApplication::instance())->primaryScreen()->physicalDotsPerInch() / 96.0;
  45. qDebug() << "No value. Detected " << current_dpi;
  46. float best_scale = 1;
  47. for (float scale : dpi_scales) {
  48. qDebug() << "Looking at " << scale << " Current diff = " << std::abs(current_dpi - scale) << ", best diff = " << std::abs(current_dpi - best_scale);
  49. if (std::abs(current_dpi - scale) < std::abs(current_dpi - best_scale))
  50. best_scale = scale;
  51. }
  52. qDebug() << "Using dpi scale factor " << best_scale;
  53. QFile file("dpi.stml");
  54. file.open(QIODevice::WriteOnly);
  55. QTextStream writeStream(&file);
  56. writeStream << best_scale;
  57. file.close();
  58. QMessageBox msgBox;
  59. msgBox.setIcon(QMessageBox::Information);
  60. msgBox.setText("Первоначальная настройка завершена. Пожалуйста, перезапустите приложение");
  61. msgBox.exec();
  62. a.exit(0);
  63. return 0;
  64. } else {
  65. QFile file("dpi.stml");
  66. file.open(QIODevice::ReadOnly);
  67. QString dpi = file.readLine();
  68. scale_factor = dpi.toDouble();
  69. qDebug() << dpi;
  70. qputenv("QT_SCALE_FACTOR", dpi.toLatin1());
  71. //qputenv("QT_SCREEN_SCALE_FACTORS", dpi.toLatin1());
  72. }
  73. freopen("log.txt", "w", stdout);
  74. freopen("log.txt", "a", stderr);
  75. setbuf(stdout, NULL);
  76. setbuf(stderr, NULL);
  77. setvbuf (stdout, NULL, _IONBF, BUFSIZ);
  78. setvbuf (stderr, NULL, _IONBF, BUFSIZ);/**/
  79. qInstallMessageHandler(myMessageOutput);// вывод логов в файл
  80. QApplication a(argc, argv);
  81. QLockFile lockFile(QDir::temp().absoluteFilePath("rulotro.lock"));
  82. if(!lockFile.tryLock(1)){
  83. QMessageBox msgBox;
  84. msgBox.setIcon(QMessageBox::Warning);
  85. msgBox.setText("Приложение уже запущено.\nРазрешено запускать только один экземпляр приложения.");
  86. msgBox.exec();
  87. return 1;
  88. }
  89. MainWindow w(scale_factor);
  90. auto result = a.exec();
  91. lockFile.unlock();
  92. return result;
  93. }