windowscreen.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <QGuiApplication>
  9. #include <QSettings>
  10. #include <QScreen>
  11. #include "windowscreen.h"
  12. WindowScreen::WindowScreen(QObject* parent)
  13. : QObject(parent)
  14. {
  15. }
  16. WindowScreen::~WindowScreen()
  17. {
  18. }
  19. void WindowScreen::loadSettings(int width, int height, int minimumWidth, int minimumHeight)
  20. {
  21. QSettings loader;
  22. setPositionX(loader.value(m_windowName + "/" + "PositionX", 99999).toInt());
  23. setPositionY(loader.value(m_windowName + "/" + "PositionY", 99999).toInt());
  24. setWidth(loader.value(m_windowName + "/" + "Width", width).toInt());
  25. setHeight(loader.value(m_windowName + "/" + "Height", height).toInt());
  26. setWindowState(static_cast<QWindow::Visibility>(loader.value(m_windowName + "/" + "WindowState", QWindow::Windowed).toInt()));
  27. if (!CheckSettings(minimumWidth, minimumHeight))
  28. {
  29. CenterWindowInPrimaryScreen(minimumWidth, minimumHeight);
  30. }
  31. }
  32. void WindowScreen::saveSettings()
  33. {
  34. QSettings saver;
  35. saver.remove(m_windowName + "/" + "WindowState");
  36. saver.setValue(m_windowName + "/" + "WindowState", m_windowCurrentInfo.m_windowState);
  37. //If the window current state is maximised or fullscreen than we save x,y,width and height
  38. //of the window prev state so that it can be minimised correctly the next time we start the application
  39. WindowScreenInfo* screenInfo = nullptr;
  40. if (m_windowCurrentInfo.m_windowState != QWindow::Maximized && m_windowCurrentInfo.m_windowState != QWindow::FullScreen)
  41. {
  42. screenInfo = &m_windowCurrentInfo;
  43. }
  44. else
  45. {
  46. screenInfo = &m_windowPreviousInfo;
  47. }
  48. saver.remove(m_windowName + "/" + "PositionX");
  49. saver.setValue(m_windowName + "/" + "PositionX", screenInfo->m_positionX);
  50. saver.remove(m_windowName + "/" + "PositionY");
  51. saver.setValue(m_windowName + "/" + "PositionY", screenInfo->m_positionY);
  52. saver.remove(m_windowName + "/" + "Width");
  53. saver.setValue(m_windowName + "/" + "Width", screenInfo->m_width);
  54. saver.remove(m_windowName + "/" + "Height");
  55. saver.setValue(m_windowName + "/" + "Height", screenInfo->m_height);
  56. }
  57. bool WindowScreen::CheckSettings(int minimumWidth, int minimumHeight)
  58. {
  59. if (m_windowCurrentInfo.m_positionX == 99999 && m_windowCurrentInfo.m_positionY == 99999)
  60. {
  61. //we are running the app first time and there is no settings to load from,
  62. //we will centre the window ourselves
  63. return false;
  64. }
  65. if (m_windowCurrentInfo.m_width < minimumWidth || m_windowCurrentInfo.m_height < minimumHeight)
  66. {
  67. return false;
  68. }
  69. //Check whether the window is fully inside the display
  70. QScreen* screen = QGuiApplication::primaryScreen();
  71. bool isPosXOK = m_windowCurrentInfo.m_positionX >= (screen->availableVirtualGeometry().x()) && m_windowCurrentInfo.m_positionX <= (screen->availableVirtualGeometry().width()) ? true : false;
  72. bool isPosYOK = m_windowCurrentInfo.m_positionY >= (screen->availableVirtualGeometry().y()) && m_windowCurrentInfo.m_positionY <= (screen->availableVirtualGeometry().height()) ? true : false;
  73. bool isWidthOK = m_windowCurrentInfo.m_positionX + m_windowCurrentInfo.m_width <= screen->availableVirtualSize().width() ? true : false;
  74. bool isHeightOK = m_windowCurrentInfo.m_positionY + m_windowCurrentInfo.m_height <= screen->availableVirtualSize().height() ? true : false;
  75. return isPosXOK && isPosYOK && isWidthOK && isHeightOK;
  76. }
  77. void WindowScreen::CenterWindowInPrimaryScreen(int minimumWidth, int minimumHeight)
  78. {
  79. //check to see whether the width is more than minimum width
  80. if (m_windowCurrentInfo.m_width < minimumWidth)
  81. {
  82. m_windowCurrentInfo.m_width = minimumWidth;
  83. }
  84. //check to see whether the height is more than minimum height
  85. if (m_windowCurrentInfo.m_height < minimumHeight)
  86. {
  87. m_windowCurrentInfo.m_height = minimumHeight;
  88. }
  89. QScreen* screen = QGuiApplication::primaryScreen();
  90. m_windowCurrentInfo.m_positionX = qRound((screen->availableGeometry().width() - screen->availableGeometry().x()) / 2.0 - m_windowCurrentInfo.m_width / 2.0);
  91. m_windowCurrentInfo.m_positionY = qRound((screen->availableGeometry().height() - screen->availableGeometry().y()) / 2.0 - m_windowCurrentInfo.m_height / 2.0);
  92. m_windowCurrentInfo.m_windowState = QWindow::Windowed;
  93. }
  94. int WindowScreen::positionX() const
  95. {
  96. return m_windowCurrentInfo.m_positionX;
  97. }
  98. void WindowScreen::setPositionX(int posX)
  99. {
  100. if (posX == m_windowCurrentInfo.m_positionX)
  101. {
  102. return;
  103. }
  104. m_windowPreviousInfo.m_positionX = m_windowCurrentInfo.m_positionX;
  105. m_windowCurrentInfo.m_positionX = posX;
  106. Q_EMIT positionXChanged();
  107. }
  108. int WindowScreen::positionY() const
  109. {
  110. return m_windowCurrentInfo.m_positionY;
  111. }
  112. void WindowScreen::setPositionY(int posY)
  113. {
  114. if (posY == m_windowCurrentInfo.m_positionY)
  115. {
  116. return;
  117. }
  118. m_windowPreviousInfo.m_positionY = m_windowCurrentInfo.m_positionY;
  119. m_windowCurrentInfo.m_positionY = posY;
  120. Q_EMIT positionYChanged();
  121. }
  122. int WindowScreen::width() const
  123. {
  124. return m_windowCurrentInfo.m_width;
  125. }
  126. void WindowScreen::setWidth(int width)
  127. {
  128. if (width == m_windowCurrentInfo.m_width)
  129. {
  130. return;
  131. }
  132. m_windowPreviousInfo.m_width = m_windowCurrentInfo.m_width;
  133. m_windowCurrentInfo.m_width = width;
  134. Q_EMIT widthChanged();
  135. }
  136. int WindowScreen::height() const
  137. {
  138. return m_windowCurrentInfo.m_height;
  139. }
  140. void WindowScreen::setHeight(int height)
  141. {
  142. if (height == m_windowCurrentInfo.m_height)
  143. {
  144. return;
  145. }
  146. m_windowPreviousInfo.m_height = m_windowCurrentInfo.m_height;
  147. m_windowCurrentInfo.m_height = height;
  148. Q_EMIT heightChanged();
  149. }
  150. QString WindowScreen::windowName() const
  151. {
  152. return m_windowName;
  153. }
  154. void WindowScreen::setWindowName(QString windowName)
  155. {
  156. m_windowName = windowName;
  157. }
  158. QWindow::Visibility WindowScreen::windowState() const
  159. {
  160. return m_windowCurrentInfo.m_windowState;
  161. }
  162. void WindowScreen::setWindowState(QWindow::Visibility state)
  163. {
  164. if (m_windowCurrentInfo.m_windowState == state)
  165. {
  166. return;
  167. }
  168. m_windowPreviousInfo.m_windowState = m_windowCurrentInfo.m_windowState;
  169. m_windowCurrentInfo.m_windowState = state;
  170. Q_EMIT windowStateChanged();
  171. }