OBSBasic_Clipboard.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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 <dialogs/OBSBasicFilters.hpp>
  18. #include <dialogs/OBSBasicSourceSelect.hpp>
  19. #include <widgets/VolControl.hpp>
  20. extern void undo_redo(const std::string &data);
  21. void OBSBasic::on_actionCopyTransform_triggered()
  22. {
  23. OBSSceneItem item = GetCurrentSceneItem();
  24. obs_sceneitem_get_info2(item, &copiedTransformInfo);
  25. obs_sceneitem_get_crop(item, &copiedCropInfo);
  26. ui->actionPasteTransform->setEnabled(true);
  27. hasCopiedTransform = true;
  28. }
  29. void OBSBasic::on_actionPasteTransform_triggered()
  30. {
  31. OBSDataAutoRelease wrapper = obs_scene_save_transform_states(GetCurrentScene(), false);
  32. auto func = [](obs_scene_t *, obs_sceneitem_t *item, void *) {
  33. if (!obs_sceneitem_selected(item))
  34. return true;
  35. if (obs_sceneitem_locked(item))
  36. return true;
  37. OBSBasic *main = OBSBasic::Get();
  38. obs_sceneitem_defer_update_begin(item);
  39. obs_sceneitem_set_info2(item, &main->copiedTransformInfo);
  40. obs_sceneitem_set_crop(item, &main->copiedCropInfo);
  41. obs_sceneitem_defer_update_end(item);
  42. return true;
  43. };
  44. obs_scene_enum_items(GetCurrentScene(), func, nullptr);
  45. OBSDataAutoRelease rwrapper = obs_scene_save_transform_states(GetCurrentScene(), false);
  46. std::string undo_data(obs_data_get_json(wrapper));
  47. std::string redo_data(obs_data_get_json(rwrapper));
  48. undo_s.add_action(QTStr("Undo.Transform.Paste").arg(obs_source_get_name(GetCurrentSceneSource())), undo_redo,
  49. undo_redo, undo_data, redo_data);
  50. }
  51. void OBSBasic::on_actionCopySource_triggered()
  52. {
  53. clipboard.clear();
  54. for (auto &selectedSource : GetAllSelectedSourceItems()) {
  55. OBSSceneItem item = ui->sources->Get(selectedSource.row());
  56. if (!item)
  57. continue;
  58. OBSSource source = obs_sceneitem_get_source(item);
  59. SourceCopyInfo copyInfo;
  60. copyInfo.weak_source = OBSGetWeakRef(source);
  61. obs_sceneitem_get_info2(item, &copyInfo.transform);
  62. obs_sceneitem_get_crop(item, &copyInfo.crop);
  63. copyInfo.blend_method = obs_sceneitem_get_blending_method(item);
  64. copyInfo.blend_mode = obs_sceneitem_get_blending_mode(item);
  65. copyInfo.visible = obs_sceneitem_visible(item);
  66. clipboard.push_back(copyInfo);
  67. }
  68. UpdateEditMenu();
  69. }
  70. void OBSBasic::on_actionPasteRef_triggered()
  71. {
  72. OBSSource scene_source = GetCurrentSceneSource();
  73. OBSData undo_data = BackupScene(scene_source);
  74. OBSScene scene = GetCurrentScene();
  75. undo_s.push_disabled();
  76. for (size_t i = clipboard.size(); i > 0; i--) {
  77. SourceCopyInfo &copyInfo = clipboard[i - 1];
  78. OBSSource source = OBSGetStrongRef(copyInfo.weak_source);
  79. if (!source)
  80. continue;
  81. const char *name = obs_source_get_name(source);
  82. /* do not allow duplicate refs of the same group in the same
  83. * scene */
  84. if (!!obs_scene_get_group(scene, name)) {
  85. continue;
  86. }
  87. OBSBasicSourceSelect::SourcePaste(copyInfo, false);
  88. }
  89. undo_s.pop_disabled();
  90. QString action_name = QTStr("Undo.PasteSourceRef");
  91. const char *scene_name = obs_source_get_name(scene_source);
  92. OBSData redo_data = BackupScene(scene_source);
  93. CreateSceneUndoRedoAction(action_name.arg(scene_name), undo_data, redo_data);
  94. }
  95. void OBSBasic::on_actionPasteDup_triggered()
  96. {
  97. OBSSource scene_source = GetCurrentSceneSource();
  98. OBSData undo_data = BackupScene(scene_source);
  99. undo_s.push_disabled();
  100. for (size_t i = clipboard.size(); i > 0; i--) {
  101. SourceCopyInfo &copyInfo = clipboard[i - 1];
  102. OBSBasicSourceSelect::SourcePaste(copyInfo, true);
  103. }
  104. undo_s.pop_disabled();
  105. QString action_name = QTStr("Undo.PasteSource");
  106. const char *scene_name = obs_source_get_name(scene_source);
  107. OBSData redo_data = BackupScene(scene_source);
  108. CreateSceneUndoRedoAction(action_name.arg(scene_name), undo_data, redo_data);
  109. }
  110. void OBSBasic::SourcePasteFilters(OBSSource source, OBSSource dstSource)
  111. {
  112. if (source == dstSource)
  113. return;
  114. OBSDataArrayAutoRelease undo_array = obs_source_backup_filters(dstSource);
  115. obs_source_copy_filters(dstSource, source);
  116. OBSDataArrayAutoRelease redo_array = obs_source_backup_filters(dstSource);
  117. const char *srcName = obs_source_get_name(source);
  118. const char *dstName = obs_source_get_name(dstSource);
  119. QString text = QTStr("Undo.Filters.Paste.Multiple").arg(srcName, dstName);
  120. CreateFilterPasteUndoRedoAction(text, dstSource, undo_array, redo_array);
  121. }
  122. void OBSBasic::AudioMixerCopyFilters()
  123. {
  124. QAction *action = reinterpret_cast<QAction *>(sender());
  125. VolControl *vol = action->property("volControl").value<VolControl *>();
  126. obs_source_t *source = vol->GetSource();
  127. copyFiltersSource = obs_source_get_weak_source(source);
  128. ui->actionPasteFilters->setEnabled(true);
  129. }
  130. void OBSBasic::AudioMixerPasteFilters()
  131. {
  132. QAction *action = reinterpret_cast<QAction *>(sender());
  133. VolControl *vol = action->property("volControl").value<VolControl *>();
  134. obs_source_t *dstSource = vol->GetSource();
  135. OBSSourceAutoRelease source = obs_weak_source_get_source(copyFiltersSource);
  136. SourcePasteFilters(source.Get(), dstSource);
  137. }
  138. void OBSBasic::SceneCopyFilters()
  139. {
  140. copyFiltersSource = obs_source_get_weak_source(GetCurrentSceneSource());
  141. ui->actionPasteFilters->setEnabled(true);
  142. }
  143. void OBSBasic::ScenePasteFilters()
  144. {
  145. OBSSourceAutoRelease source = obs_weak_source_get_source(copyFiltersSource);
  146. OBSSource dstSource = GetCurrentSceneSource();
  147. SourcePasteFilters(source.Get(), dstSource);
  148. }
  149. void OBSBasic::on_actionCopyFilters_triggered()
  150. {
  151. OBSSceneItem item = GetCurrentSceneItem();
  152. if (!item)
  153. return;
  154. OBSSource source = obs_sceneitem_get_source(item);
  155. copyFiltersSource = obs_source_get_weak_source(source);
  156. ui->actionPasteFilters->setEnabled(true);
  157. }
  158. void OBSBasic::CreateFilterPasteUndoRedoAction(const QString &text, obs_source_t *source, obs_data_array_t *undo_array,
  159. obs_data_array_t *redo_array)
  160. {
  161. auto undo_redo = [this](const std::string &json) {
  162. OBSDataAutoRelease data = obs_data_create_from_json(json.c_str());
  163. OBSDataArrayAutoRelease array = obs_data_get_array(data, "array");
  164. OBSSourceAutoRelease source = obs_get_source_by_uuid(obs_data_get_string(data, "uuid"));
  165. obs_source_restore_filters(source, array);
  166. if (filters)
  167. filters->UpdateSource(source);
  168. };
  169. const char *uuid = obs_source_get_uuid(source);
  170. OBSDataAutoRelease undo_data = obs_data_create();
  171. OBSDataAutoRelease redo_data = obs_data_create();
  172. obs_data_set_array(undo_data, "array", undo_array);
  173. obs_data_set_array(redo_data, "array", redo_array);
  174. obs_data_set_string(undo_data, "uuid", uuid);
  175. obs_data_set_string(redo_data, "uuid", uuid);
  176. undo_s.add_action(text, undo_redo, undo_redo, obs_data_get_json(undo_data), obs_data_get_json(redo_data));
  177. }
  178. void OBSBasic::on_actionPasteFilters_triggered()
  179. {
  180. OBSSourceAutoRelease source = obs_weak_source_get_source(copyFiltersSource);
  181. OBSSceneItem sceneItem = GetCurrentSceneItem();
  182. OBSSource dstSource = obs_sceneitem_get_source(sceneItem);
  183. SourcePasteFilters(source.Get(), dstSource);
  184. }