properties-view.hpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. #pragma once
  2. #include "vertical-scroll-area.hpp"
  3. #include <obs-data.h>
  4. #include <obs.hpp>
  5. #include <qtimer.h>
  6. #include <QPointer>
  7. #include <vector>
  8. #include <memory>
  9. class QFormLayout;
  10. class OBSPropertiesView;
  11. class QLabel;
  12. typedef obs_properties_t *(*PropertiesReloadCallback)(void *obj);
  13. typedef void (*PropertiesUpdateCallback)(void *obj, obs_data_t *old_settings,
  14. obs_data_t *new_settings);
  15. typedef void (*PropertiesVisualUpdateCb)(void *obj, obs_data_t *settings);
  16. /* ------------------------------------------------------------------------- */
  17. class WidgetInfo : public QObject {
  18. Q_OBJECT
  19. friend class OBSPropertiesView;
  20. private:
  21. OBSPropertiesView *view;
  22. obs_property_t *property;
  23. QWidget *widget;
  24. QPointer<QTimer> update_timer;
  25. bool recently_updated = false;
  26. OBSData old_settings_cache;
  27. void BoolChanged(const char *setting);
  28. void IntChanged(const char *setting);
  29. void FloatChanged(const char *setting);
  30. void TextChanged(const char *setting);
  31. bool PathChanged(const char *setting);
  32. void ListChanged(const char *setting);
  33. bool ColorChangedInternal(const char *setting, bool supportAlpha);
  34. bool ColorChanged(const char *setting);
  35. bool ColorAlphaChanged(const char *setting);
  36. bool FontChanged(const char *setting);
  37. void GroupChanged(const char *setting);
  38. void EditableListChanged();
  39. void ButtonClicked();
  40. void TogglePasswordText(bool checked);
  41. public:
  42. inline WidgetInfo(OBSPropertiesView *view_, obs_property_t *prop,
  43. QWidget *widget_)
  44. : view(view_), property(prop), widget(widget_)
  45. {
  46. }
  47. ~WidgetInfo()
  48. {
  49. if (update_timer) {
  50. update_timer->stop();
  51. QMetaObject::invokeMethod(update_timer, "timeout");
  52. update_timer->deleteLater();
  53. obs_data_release(old_settings_cache);
  54. }
  55. }
  56. public slots:
  57. void ControlChanged();
  58. /* editable list */
  59. void EditListAdd();
  60. void EditListAddText();
  61. void EditListAddFiles();
  62. void EditListAddDir();
  63. void EditListRemove();
  64. void EditListEdit();
  65. void EditListUp();
  66. void EditListDown();
  67. void EditListReordered(const QModelIndex &parent, int start, int end,
  68. const QModelIndex &destination, int row);
  69. };
  70. /* ------------------------------------------------------------------------- */
  71. class OBSPropertiesView : public VScrollArea {
  72. Q_OBJECT
  73. friend class WidgetInfo;
  74. using properties_delete_t = decltype(&obs_properties_destroy);
  75. using properties_t =
  76. std::unique_ptr<obs_properties_t, properties_delete_t>;
  77. private:
  78. QWidget *widget = nullptr;
  79. properties_t properties;
  80. OBSData settings;
  81. OBSWeakObjectAutoRelease weakObj;
  82. void *rawObj;
  83. std::string type;
  84. PropertiesReloadCallback reloadCallback;
  85. PropertiesUpdateCallback callback = nullptr;
  86. PropertiesVisualUpdateCb cb = nullptr;
  87. int minSize;
  88. std::vector<std::unique_ptr<WidgetInfo>> children;
  89. std::string lastFocused;
  90. QWidget *lastWidget = nullptr;
  91. bool deferUpdate;
  92. QWidget *NewWidget(obs_property_t *prop, QWidget *widget,
  93. const char *signal);
  94. QWidget *AddCheckbox(obs_property_t *prop);
  95. QWidget *AddText(obs_property_t *prop, QFormLayout *layout,
  96. QLabel *&label);
  97. void AddPath(obs_property_t *prop, QFormLayout *layout, QLabel **label);
  98. void AddInt(obs_property_t *prop, QFormLayout *layout, QLabel **label);
  99. void AddFloat(obs_property_t *prop, QFormLayout *layout,
  100. QLabel **label);
  101. QWidget *AddList(obs_property_t *prop, bool &warning);
  102. void AddEditableList(obs_property_t *prop, QFormLayout *layout,
  103. QLabel *&label);
  104. QWidget *AddButton(obs_property_t *prop);
  105. void AddColorInternal(obs_property_t *prop, QFormLayout *layout,
  106. QLabel *&label, bool supportAlpha);
  107. void AddColor(obs_property_t *prop, QFormLayout *layout,
  108. QLabel *&label);
  109. void AddColorAlpha(obs_property_t *prop, QFormLayout *layout,
  110. QLabel *&label);
  111. void AddFont(obs_property_t *prop, QFormLayout *layout, QLabel *&label);
  112. void AddFrameRate(obs_property_t *prop, bool &warning,
  113. QFormLayout *layout, QLabel *&label);
  114. void AddGroup(obs_property_t *prop, QFormLayout *layout);
  115. void AddProperty(obs_property_t *property, QFormLayout *layout);
  116. void resizeEvent(QResizeEvent *event) override;
  117. void GetScrollPos(int &h, int &v);
  118. void SetScrollPos(int h, int v);
  119. public slots:
  120. void ReloadProperties();
  121. void RefreshProperties();
  122. void SignalChanged();
  123. signals:
  124. void PropertiesResized();
  125. void Changed();
  126. void PropertiesRefreshed();
  127. public:
  128. OBSPropertiesView(OBSData settings, obs_object_t *obj,
  129. PropertiesReloadCallback reloadCallback,
  130. PropertiesUpdateCallback callback,
  131. PropertiesVisualUpdateCb cb = nullptr,
  132. int minSize = 0);
  133. OBSPropertiesView(OBSData settings, void *obj,
  134. PropertiesReloadCallback reloadCallback,
  135. PropertiesUpdateCallback callback,
  136. PropertiesVisualUpdateCb cb = nullptr,
  137. int minSize = 0);
  138. OBSPropertiesView(OBSData settings, const char *type,
  139. PropertiesReloadCallback reloadCallback,
  140. int minSize = 0);
  141. #define obj_constructor(type) \
  142. inline OBSPropertiesView(OBSData settings, obs_##type##_t *type, \
  143. PropertiesReloadCallback reloadCallback, \
  144. PropertiesUpdateCallback callback, \
  145. PropertiesVisualUpdateCb cb = nullptr, \
  146. int minSize = 0) \
  147. : OBSPropertiesView(settings, (obs_object_t *)type, \
  148. reloadCallback, callback, cb, minSize) \
  149. { \
  150. }
  151. obj_constructor(source);
  152. obj_constructor(output);
  153. obj_constructor(encoder);
  154. obj_constructor(service);
  155. #undef obj_constructor
  156. inline obs_data_t *GetSettings() const { return settings; }
  157. inline void UpdateSettings()
  158. {
  159. callback(OBSGetStrongRef(weakObj), nullptr, settings);
  160. }
  161. inline bool DeferUpdate() const { return deferUpdate; }
  162. inline OBSObject GetObject() const { return OBSGetStrongRef(weakObj); }
  163. #define Def_IsObject(type) \
  164. inline bool IsObject(obs_##type##_t *type) const \
  165. { \
  166. OBSObject obj = OBSGetStrongRef(weakObj); \
  167. return obj.Get() == (obs_object_t *)type; \
  168. }
  169. /* clang-format off */
  170. Def_IsObject(source)
  171. Def_IsObject(output)
  172. Def_IsObject(encoder)
  173. Def_IsObject(service)
  174. /* clang-format on */
  175. #undef Def_IsObject
  176. };