1
0

OBSBasic_ContextToolbar.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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 <components/ApplicationAudioCaptureToolbar.hpp>
  18. #include <components/AudioCaptureToolbar.hpp>
  19. #include <components/BrowserToolbar.hpp>
  20. #include <components/ColorSourceToolbar.hpp>
  21. #include <components/DeviceCaptureToolbar.hpp>
  22. #include <components/DisplayCaptureToolbar.hpp>
  23. #include <components/GameCaptureToolbar.hpp>
  24. #include <components/ImageSourceToolbar.hpp>
  25. #include <components/MediaControls.hpp>
  26. #include <components/TextSourceToolbar.hpp>
  27. #include <components/WindowCaptureToolbar.hpp>
  28. #include <qt-wrappers.hpp>
  29. void OBSBasic::copyActionsDynamicProperties()
  30. {
  31. // Themes need the QAction dynamic properties
  32. for (QAction *x : ui->scenesToolbar->actions()) {
  33. QWidget *temp = ui->scenesToolbar->widgetForAction(x);
  34. if (!temp)
  35. continue;
  36. for (QByteArray &y : x->dynamicPropertyNames()) {
  37. temp->setProperty(y, x->property(y));
  38. }
  39. }
  40. for (QAction *x : ui->sourcesToolbar->actions()) {
  41. QWidget *temp = ui->sourcesToolbar->widgetForAction(x);
  42. if (!temp)
  43. continue;
  44. for (QByteArray &y : x->dynamicPropertyNames()) {
  45. temp->setProperty(y, x->property(y));
  46. }
  47. }
  48. for (QAction *x : ui->mixerToolbar->actions()) {
  49. QWidget *temp = ui->mixerToolbar->widgetForAction(x);
  50. if (!temp)
  51. continue;
  52. for (QByteArray &y : x->dynamicPropertyNames()) {
  53. temp->setProperty(y, x->property(y));
  54. }
  55. }
  56. }
  57. void OBSBasic::ClearContextBar()
  58. {
  59. QLayoutItem *la = ui->emptySpace->layout()->itemAt(0);
  60. if (la) {
  61. delete la->widget();
  62. ui->emptySpace->layout()->removeItem(la);
  63. }
  64. }
  65. void OBSBasic::UpdateContextBarVisibility()
  66. {
  67. int width = ui->centralwidget->size().width();
  68. ContextBarSize contextBarSizeNew;
  69. if (width >= 740) {
  70. contextBarSizeNew = ContextBarSize_Normal;
  71. } else if (width >= 600) {
  72. contextBarSizeNew = ContextBarSize_Reduced;
  73. } else {
  74. contextBarSizeNew = ContextBarSize_Minimized;
  75. }
  76. if (contextBarSize == contextBarSizeNew)
  77. return;
  78. contextBarSize = contextBarSizeNew;
  79. UpdateContextBarDeferred();
  80. }
  81. static bool is_network_media_source(obs_source_t *source, const char *id)
  82. {
  83. if (strcmp(id, "ffmpeg_source") != 0)
  84. return false;
  85. OBSDataAutoRelease s = obs_source_get_settings(source);
  86. bool is_local_file = obs_data_get_bool(s, "is_local_file");
  87. return !is_local_file;
  88. }
  89. void OBSBasic::UpdateContextBarDeferred(bool force)
  90. {
  91. QMetaObject::invokeMethod(this, "UpdateContextBar", Qt::QueuedConnection, Q_ARG(bool, force));
  92. }
  93. void OBSBasic::SourceToolBarActionsSetEnabled()
  94. {
  95. bool enable = false;
  96. bool disableProps = false;
  97. OBSSceneItem item = GetCurrentSceneItem();
  98. if (item) {
  99. OBSSource source = obs_sceneitem_get_source(item);
  100. disableProps = !obs_source_configurable(source);
  101. enable = true;
  102. }
  103. if (disableProps)
  104. ui->actionSourceProperties->setEnabled(false);
  105. else
  106. ui->actionSourceProperties->setEnabled(enable);
  107. ui->actionRemoveSource->setEnabled(enable);
  108. ui->actionSourceUp->setEnabled(enable);
  109. ui->actionSourceDown->setEnabled(enable);
  110. RefreshToolBarStyling(ui->sourcesToolbar);
  111. }
  112. void OBSBasic::UpdateContextBar(bool force)
  113. {
  114. SourceToolBarActionsSetEnabled();
  115. if (!ui->contextContainer->isVisible() && !force)
  116. return;
  117. OBSSceneItem item = GetCurrentSceneItem();
  118. if (item) {
  119. obs_source_t *source = obs_sceneitem_get_source(item);
  120. bool updateNeeded = true;
  121. QLayoutItem *la = ui->emptySpace->layout()->itemAt(0);
  122. if (la) {
  123. if (SourceToolbar *toolbar = dynamic_cast<SourceToolbar *>(la->widget())) {
  124. if (toolbar->GetSource() == source)
  125. updateNeeded = false;
  126. } else if (MediaControls *toolbar = dynamic_cast<MediaControls *>(la->widget())) {
  127. if (toolbar->GetSource() == source)
  128. updateNeeded = false;
  129. }
  130. }
  131. const char *id = obs_source_get_unversioned_id(source);
  132. uint32_t flags = obs_source_get_output_flags(source);
  133. ui->sourceInteractButton->setVisible(flags & OBS_SOURCE_INTERACTION);
  134. if (contextBarSize >= ContextBarSize_Reduced && (updateNeeded || force)) {
  135. ClearContextBar();
  136. if (flags & OBS_SOURCE_CONTROLLABLE_MEDIA) {
  137. if (!is_network_media_source(source, id)) {
  138. MediaControls *mediaControls = new MediaControls(ui->emptySpace);
  139. mediaControls->SetSource(source);
  140. ui->emptySpace->layout()->addWidget(mediaControls);
  141. }
  142. } else if (strcmp(id, "browser_source") == 0) {
  143. BrowserToolbar *c = new BrowserToolbar(ui->emptySpace, source);
  144. ui->emptySpace->layout()->addWidget(c);
  145. } else if (strcmp(id, "wasapi_input_capture") == 0 ||
  146. strcmp(id, "wasapi_output_capture") == 0 ||
  147. strcmp(id, "coreaudio_input_capture") == 0 ||
  148. strcmp(id, "coreaudio_output_capture") == 0 ||
  149. strcmp(id, "pulse_input_capture") == 0 || strcmp(id, "pulse_output_capture") == 0 ||
  150. strcmp(id, "alsa_input_capture") == 0) {
  151. AudioCaptureToolbar *c = new AudioCaptureToolbar(ui->emptySpace, source);
  152. c->Init();
  153. ui->emptySpace->layout()->addWidget(c);
  154. } else if (strcmp(id, "wasapi_process_output_capture") == 0) {
  155. ApplicationAudioCaptureToolbar *c =
  156. new ApplicationAudioCaptureToolbar(ui->emptySpace, source);
  157. c->Init();
  158. ui->emptySpace->layout()->addWidget(c);
  159. } else if (strcmp(id, "window_capture") == 0 || strcmp(id, "xcomposite_input") == 0) {
  160. WindowCaptureToolbar *c = new WindowCaptureToolbar(ui->emptySpace, source);
  161. c->Init();
  162. ui->emptySpace->layout()->addWidget(c);
  163. } else if (strcmp(id, "monitor_capture") == 0 || strcmp(id, "display_capture") == 0 ||
  164. strcmp(id, "xshm_input") == 0) {
  165. DisplayCaptureToolbar *c = new DisplayCaptureToolbar(ui->emptySpace, source);
  166. c->Init();
  167. ui->emptySpace->layout()->addWidget(c);
  168. } else if (strcmp(id, "dshow_input") == 0) {
  169. DeviceCaptureToolbar *c = new DeviceCaptureToolbar(ui->emptySpace, source);
  170. ui->emptySpace->layout()->addWidget(c);
  171. } else if (strcmp(id, "game_capture") == 0) {
  172. GameCaptureToolbar *c = new GameCaptureToolbar(ui->emptySpace, source);
  173. ui->emptySpace->layout()->addWidget(c);
  174. } else if (strcmp(id, "image_source") == 0) {
  175. ImageSourceToolbar *c = new ImageSourceToolbar(ui->emptySpace, source);
  176. ui->emptySpace->layout()->addWidget(c);
  177. } else if (strcmp(id, "color_source") == 0) {
  178. ColorSourceToolbar *c = new ColorSourceToolbar(ui->emptySpace, source);
  179. ui->emptySpace->layout()->addWidget(c);
  180. } else if (strcmp(id, "text_ft2_source") == 0 || strcmp(id, "text_gdiplus") == 0) {
  181. TextSourceToolbar *c = new TextSourceToolbar(ui->emptySpace, source);
  182. ui->emptySpace->layout()->addWidget(c);
  183. }
  184. } else if (contextBarSize == ContextBarSize_Minimized) {
  185. ClearContextBar();
  186. }
  187. QIcon icon;
  188. if (strcmp(id, "scene") == 0)
  189. icon = GetSceneIcon();
  190. else if (strcmp(id, "group") == 0)
  191. icon = GetGroupIcon();
  192. else
  193. icon = GetSourceIcon(id);
  194. QPixmap pixmap = icon.pixmap(QSize(16, 16));
  195. ui->contextSourceIcon->setPixmap(pixmap);
  196. ui->contextSourceIconSpacer->hide();
  197. ui->contextSourceIcon->show();
  198. const char *name = obs_source_get_name(source);
  199. ui->contextSourceLabel->setText(name);
  200. ui->sourceFiltersButton->setEnabled(true);
  201. ui->sourcePropertiesButton->setEnabled(obs_source_configurable(source));
  202. } else {
  203. ClearContextBar();
  204. ui->contextSourceIcon->hide();
  205. ui->contextSourceIconSpacer->show();
  206. ui->contextSourceLabel->setText(QTStr("ContextBar.NoSelectedSource"));
  207. ui->sourceFiltersButton->setEnabled(false);
  208. ui->sourcePropertiesButton->setEnabled(false);
  209. ui->sourceInteractButton->setVisible(false);
  210. }
  211. if (contextBarSize == ContextBarSize_Normal) {
  212. ui->sourcePropertiesButton->setText(QTStr("Properties"));
  213. ui->sourceFiltersButton->setText(QTStr("Filters"));
  214. ui->sourceInteractButton->setText(QTStr("Interact"));
  215. } else {
  216. ui->sourcePropertiesButton->setText("");
  217. ui->sourceFiltersButton->setText("");
  218. ui->sourceInteractButton->setText("");
  219. }
  220. }
  221. void OBSBasic::ShowContextBar()
  222. {
  223. on_toggleContextBar_toggled(true);
  224. ui->toggleContextBar->setChecked(true);
  225. }
  226. void OBSBasic::HideContextBar()
  227. {
  228. on_toggleContextBar_toggled(false);
  229. ui->toggleContextBar->setChecked(false);
  230. }
  231. void OBSBasic::on_toggleContextBar_toggled(bool visible)
  232. {
  233. config_set_bool(App()->GetUserConfig(), "BasicWindow", "ShowContextToolbars", visible);
  234. this->ui->contextContainer->setVisible(visible);
  235. UpdateContextBar(true);
  236. }