OBSBasic_Docks.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. /******************************************************************************
  2. Copyright (C) 2023 by Lain Bailey <[email protected]>
  3. Zachary Lund <[email protected]>
  4. Philippe Groarke <[email protected]>
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. ******************************************************************************/
  16. #include "OBSBasic.hpp"
  17. #include <qt-wrappers.hpp>
  18. void assignDockToggle(QDockWidget *dock, QAction *action)
  19. {
  20. auto handleWindowToggle = [action](bool vis) {
  21. action->blockSignals(true);
  22. action->setChecked(vis);
  23. action->blockSignals(false);
  24. };
  25. auto handleMenuToggle = [dock](bool check) {
  26. dock->blockSignals(true);
  27. dock->setVisible(check);
  28. dock->blockSignals(false);
  29. };
  30. dock->connect(dock->toggleViewAction(), &QAction::toggled, handleWindowToggle);
  31. dock->connect(action, &QAction::toggled, handleMenuToggle);
  32. }
  33. void setupDockAction(QDockWidget *dock)
  34. {
  35. QAction *action = dock->toggleViewAction();
  36. auto neverDisable = [action]() {
  37. QSignalBlocker block(action);
  38. action->setEnabled(true);
  39. };
  40. auto newToggleView = [dock](bool check) {
  41. QSignalBlocker block(dock);
  42. dock->setVisible(check);
  43. };
  44. // Replace the slot connected by default
  45. QObject::disconnect(action, &QAction::triggered, nullptr, 0);
  46. dock->connect(action, &QAction::triggered, newToggleView);
  47. // Make the action unable to be disabled
  48. action->connect(action, &QAction::enabledChanged, neverDisable);
  49. }
  50. void OBSBasic::on_resetDocks_triggered(bool force)
  51. {
  52. /* prune deleted extra docks */
  53. for (int i = oldExtraDocks.size() - 1; i >= 0; i--) {
  54. if (!oldExtraDocks[i]) {
  55. oldExtraDocks.removeAt(i);
  56. oldExtraDockNames.removeAt(i);
  57. }
  58. }
  59. #ifdef BROWSER_AVAILABLE
  60. if ((oldExtraDocks.size() || extraDocks.size() || extraCustomDocks.size() || extraBrowserDocks.size()) &&
  61. !force)
  62. #else
  63. if ((oldExtraDocks.size() || extraDocks.size() || extraCustomDocks.size()) && !force)
  64. #endif
  65. {
  66. QMessageBox::StandardButton button =
  67. OBSMessageBox::question(this, QTStr("ResetUIWarning.Title"), QTStr("ResetUIWarning.Text"));
  68. if (button == QMessageBox::No)
  69. return;
  70. }
  71. /* undock/hide/center extra docks */
  72. for (int i = oldExtraDocks.size() - 1; i >= 0; i--) {
  73. if (oldExtraDocks[i]) {
  74. oldExtraDocks[i]->setVisible(true);
  75. oldExtraDocks[i]->setFloating(true);
  76. oldExtraDocks[i]->move(frameGeometry().topLeft() + rect().center() -
  77. oldExtraDocks[i]->rect().center());
  78. oldExtraDocks[i]->setVisible(false);
  79. }
  80. }
  81. #define RESET_DOCKLIST(dockList) \
  82. for (int i = dockList.size() - 1; i >= 0; i--) { \
  83. dockList[i]->setVisible(true); \
  84. dockList[i]->setFloating(true); \
  85. dockList[i]->move(frameGeometry().topLeft() + rect().center() - dockList[i]->rect().center()); \
  86. dockList[i]->setVisible(false); \
  87. }
  88. RESET_DOCKLIST(extraDocks)
  89. RESET_DOCKLIST(extraCustomDocks)
  90. #ifdef BROWSER_AVAILABLE
  91. RESET_DOCKLIST(extraBrowserDocks)
  92. #endif
  93. #undef RESET_DOCKLIST
  94. restoreState(startingDockLayout);
  95. int cx = width();
  96. int cy = height();
  97. int cx22_5 = cx * 225 / 1000;
  98. int cx5 = cx * 5 / 100;
  99. int cx21 = cx * 21 / 100;
  100. cy = cy * 225 / 1000;
  101. int mixerSize = cx - (cx22_5 * 2 + cx5 + cx21);
  102. QList<QDockWidget *> docks{ui->scenesDock, ui->sourcesDock, ui->mixerDock, ui->transitionsDock, controlsDock};
  103. QList<int> sizes{cx22_5, cx22_5, mixerSize, cx5, cx21};
  104. ui->scenesDock->setVisible(true);
  105. ui->sourcesDock->setVisible(true);
  106. ui->mixerDock->setVisible(true);
  107. ui->transitionsDock->setVisible(true);
  108. controlsDock->setVisible(true);
  109. statsDock->setVisible(false);
  110. statsDock->setFloating(true);
  111. resizeDocks(docks, {cy, cy, cy, cy, cy}, Qt::Vertical);
  112. resizeDocks(docks, sizes, Qt::Horizontal);
  113. activateWindow();
  114. }
  115. void OBSBasic::on_lockDocks_toggled(bool lock)
  116. {
  117. QDockWidget::DockWidgetFeatures features =
  118. lock ? QDockWidget::NoDockWidgetFeatures
  119. : (QDockWidget::DockWidgetClosable | QDockWidget::DockWidgetMovable |
  120. QDockWidget::DockWidgetFloatable);
  121. QDockWidget::DockWidgetFeatures mainFeatures = features;
  122. mainFeatures &= ~QDockWidget::QDockWidget::DockWidgetClosable;
  123. ui->scenesDock->setFeatures(mainFeatures);
  124. ui->sourcesDock->setFeatures(mainFeatures);
  125. ui->mixerDock->setFeatures(mainFeatures);
  126. ui->transitionsDock->setFeatures(mainFeatures);
  127. controlsDock->setFeatures(mainFeatures);
  128. statsDock->setFeatures(features);
  129. for (int i = extraDocks.size() - 1; i >= 0; i--)
  130. extraDocks[i]->setFeatures(features);
  131. for (int i = extraCustomDocks.size() - 1; i >= 0; i--)
  132. extraCustomDocks[i]->setFeatures(features);
  133. #ifdef BROWSER_AVAILABLE
  134. for (int i = extraBrowserDocks.size() - 1; i >= 0; i--)
  135. extraBrowserDocks[i]->setFeatures(features);
  136. #endif
  137. for (int i = oldExtraDocks.size() - 1; i >= 0; i--) {
  138. if (!oldExtraDocks[i]) {
  139. oldExtraDocks.removeAt(i);
  140. oldExtraDockNames.removeAt(i);
  141. } else {
  142. oldExtraDocks[i]->setFeatures(features);
  143. }
  144. }
  145. }
  146. void OBSBasic::on_sideDocks_toggled(bool side)
  147. {
  148. if (side) {
  149. setCorner(Qt::TopLeftCorner, Qt::LeftDockWidgetArea);
  150. setCorner(Qt::TopRightCorner, Qt::RightDockWidgetArea);
  151. setCorner(Qt::BottomLeftCorner, Qt::LeftDockWidgetArea);
  152. setCorner(Qt::BottomRightCorner, Qt::RightDockWidgetArea);
  153. } else {
  154. setCorner(Qt::TopLeftCorner, Qt::TopDockWidgetArea);
  155. setCorner(Qt::TopRightCorner, Qt::TopDockWidgetArea);
  156. setCorner(Qt::BottomLeftCorner, Qt::BottomDockWidgetArea);
  157. setCorner(Qt::BottomRightCorner, Qt::BottomDockWidgetArea);
  158. }
  159. }
  160. QAction *OBSBasic::AddDockWidget(QDockWidget *dock)
  161. {
  162. // Prevent the object name from being changed
  163. connect(dock, &QObject::objectNameChanged, this, &OBSBasic::RepairOldExtraDockName);
  164. #ifdef BROWSER_AVAILABLE
  165. QAction *action = new QAction(dock->windowTitle(), ui->menuDocks);
  166. if (!extraBrowserMenuDocksSeparator.isNull())
  167. ui->menuDocks->insertAction(extraBrowserMenuDocksSeparator, action);
  168. else
  169. ui->menuDocks->addAction(action);
  170. #else
  171. QAction *action = ui->menuDocks->addAction(dock->windowTitle());
  172. #endif
  173. action->setCheckable(true);
  174. action->setMenuRole(QAction::NoRole);
  175. assignDockToggle(dock, action);
  176. oldExtraDocks.push_back(dock);
  177. oldExtraDockNames.push_back(dock->objectName());
  178. bool lock = ui->lockDocks->isChecked();
  179. QDockWidget::DockWidgetFeatures features =
  180. lock ? QDockWidget::NoDockWidgetFeatures
  181. : (QDockWidget::DockWidgetClosable | QDockWidget::DockWidgetMovable |
  182. QDockWidget::DockWidgetFloatable);
  183. dock->setFeatures(features);
  184. /* prune deleted docks */
  185. for (int i = oldExtraDocks.size() - 1; i >= 0; i--) {
  186. if (!oldExtraDocks[i]) {
  187. oldExtraDocks.removeAt(i);
  188. oldExtraDockNames.removeAt(i);
  189. }
  190. }
  191. return action;
  192. }
  193. void OBSBasic::RepairOldExtraDockName()
  194. {
  195. QDockWidget *dock = reinterpret_cast<QDockWidget *>(sender());
  196. int idx = oldExtraDocks.indexOf(dock);
  197. QSignalBlocker block(dock);
  198. if (idx == -1) {
  199. blog(LOG_WARNING, "A dock got its object name changed");
  200. return;
  201. }
  202. blog(LOG_WARNING, "The dock '%s' got its object name restored", QT_TO_UTF8(oldExtraDockNames[idx]));
  203. dock->setObjectName(oldExtraDockNames[idx]);
  204. }
  205. void OBSBasic::AddDockWidget(QDockWidget *dock, Qt::DockWidgetArea area, bool extraBrowser)
  206. {
  207. if (dock->objectName().isEmpty())
  208. return;
  209. bool lock = ui->lockDocks->isChecked();
  210. QDockWidget::DockWidgetFeatures features =
  211. lock ? QDockWidget::NoDockWidgetFeatures
  212. : (QDockWidget::DockWidgetClosable | QDockWidget::DockWidgetMovable |
  213. QDockWidget::DockWidgetFloatable);
  214. setupDockAction(dock);
  215. dock->setFeatures(features);
  216. addDockWidget(area, dock);
  217. #ifdef BROWSER_AVAILABLE
  218. if (extraBrowser && extraBrowserMenuDocksSeparator.isNull())
  219. extraBrowserMenuDocksSeparator = ui->menuDocks->addSeparator();
  220. if (!extraBrowser && !extraBrowserMenuDocksSeparator.isNull())
  221. ui->menuDocks->insertAction(extraBrowserMenuDocksSeparator, dock->toggleViewAction());
  222. else
  223. ui->menuDocks->addAction(dock->toggleViewAction());
  224. if (extraBrowser)
  225. return;
  226. #else
  227. UNUSED_PARAMETER(extraBrowser);
  228. ui->menuDocks->addAction(dock->toggleViewAction());
  229. #endif
  230. extraDockNames.push_back(dock->objectName());
  231. extraDocks.push_back(std::shared_ptr<QDockWidget>(dock));
  232. }
  233. void OBSBasic::RemoveDockWidget(const QString &name)
  234. {
  235. if (extraDockNames.contains(name)) {
  236. int idx = extraDockNames.indexOf(name);
  237. extraDockNames.removeAt(idx);
  238. extraDocks[idx].reset();
  239. extraDocks.removeAt(idx);
  240. } else if (extraCustomDockNames.contains(name)) {
  241. int idx = extraCustomDockNames.indexOf(name);
  242. extraCustomDockNames.removeAt(idx);
  243. removeDockWidget(extraCustomDocks[idx]);
  244. extraCustomDocks.removeAt(idx);
  245. }
  246. }
  247. bool OBSBasic::IsDockObjectNameUsed(const QString &name)
  248. {
  249. QStringList list;
  250. list << "scenesDock"
  251. << "sourcesDock"
  252. << "mixerDock"
  253. << "transitionsDock"
  254. << "controlsDock"
  255. << "statsDock";
  256. list << oldExtraDockNames;
  257. list << extraDockNames;
  258. list << extraCustomDockNames;
  259. return list.contains(name);
  260. }
  261. void OBSBasic::AddCustomDockWidget(QDockWidget *dock)
  262. {
  263. // Prevent the object name from being changed
  264. connect(dock, &QObject::objectNameChanged, this, &OBSBasic::RepairCustomExtraDockName);
  265. bool lock = ui->lockDocks->isChecked();
  266. QDockWidget::DockWidgetFeatures features =
  267. lock ? QDockWidget::NoDockWidgetFeatures
  268. : (QDockWidget::DockWidgetClosable | QDockWidget::DockWidgetMovable |
  269. QDockWidget::DockWidgetFloatable);
  270. dock->setFeatures(features);
  271. addDockWidget(Qt::RightDockWidgetArea, dock);
  272. extraCustomDockNames.push_back(dock->objectName());
  273. extraCustomDocks.push_back(dock);
  274. }
  275. void OBSBasic::RepairCustomExtraDockName()
  276. {
  277. QDockWidget *dock = reinterpret_cast<QDockWidget *>(sender());
  278. int idx = extraCustomDocks.indexOf(dock);
  279. QSignalBlocker block(dock);
  280. if (idx == -1) {
  281. blog(LOG_WARNING, "A custom dock got its object name changed");
  282. return;
  283. }
  284. blog(LOG_WARNING, "The custom dock '%s' got its object name restored", QT_TO_UTF8(extraCustomDockNames[idx]));
  285. dock->setObjectName(extraCustomDockNames[idx]);
  286. }