properties-view.cpp 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253
  1. #include <QFormLayout>
  2. #include <QScrollBar>
  3. #include <QLabel>
  4. #include <QCheckBox>
  5. #include <QFont>
  6. #include <QFontDialog>
  7. #include <QLineEdit>
  8. #include <QSpinBox>
  9. #include <QSlider>
  10. #include <QDoubleSpinBox>
  11. #include <QComboBox>
  12. #include <QListWidget>
  13. #include <QPushButton>
  14. #include <QStandardItem>
  15. #include <QFileDialog>
  16. #include <QColorDialog>
  17. #include <QPlainTextEdit>
  18. #include <QDialogButtonBox>
  19. #include <QMenu>
  20. #include <QStackedWidget>
  21. #include <QDir>
  22. #include <QGroupBox>
  23. #include <QObject>
  24. #include "double-slider.hpp"
  25. #include "slider-ignorewheel.hpp"
  26. #include "spinbox-ignorewheel.hpp"
  27. #include "combobox-ignorewheel.hpp"
  28. #include "qt-wrappers.hpp"
  29. #include "properties-view.hpp"
  30. #include "properties-view.moc.hpp"
  31. #include "obs-app.hpp"
  32. #include <cstdlib>
  33. #include <initializer_list>
  34. #include <obs-data.h>
  35. #include <obs.h>
  36. #include <qtimer.h>
  37. #include <string>
  38. using namespace std;
  39. static inline QColor color_from_int(long long val)
  40. {
  41. return QColor(val & 0xff, (val >> 8) & 0xff, (val >> 16) & 0xff,
  42. (val >> 24) & 0xff);
  43. }
  44. static inline long long color_to_int(QColor color)
  45. {
  46. auto shift = [&](unsigned val, int shift) {
  47. return ((val & 0xff) << shift);
  48. };
  49. return shift(color.red(), 0) | shift(color.green(), 8) |
  50. shift(color.blue(), 16) | shift(color.alpha(), 24);
  51. }
  52. namespace {
  53. struct frame_rate_tag {
  54. enum tag_type {
  55. SIMPLE,
  56. RATIONAL,
  57. USER,
  58. } type = SIMPLE;
  59. const char *val = nullptr;
  60. frame_rate_tag() = default;
  61. explicit frame_rate_tag(tag_type type) : type(type) {}
  62. explicit frame_rate_tag(const char *val) : type(USER), val(val) {}
  63. static frame_rate_tag simple() { return frame_rate_tag{SIMPLE}; }
  64. static frame_rate_tag rational() { return frame_rate_tag{RATIONAL}; }
  65. };
  66. struct common_frame_rate {
  67. const char *fps_name;
  68. media_frames_per_second fps;
  69. };
  70. }
  71. Q_DECLARE_METATYPE(frame_rate_tag);
  72. Q_DECLARE_METATYPE(media_frames_per_second);
  73. void OBSPropertiesView::ReloadProperties()
  74. {
  75. if (obj) {
  76. properties.reset(reloadCallback(obj));
  77. } else {
  78. properties.reset(reloadCallback((void *)type.c_str()));
  79. obs_properties_apply_settings(properties.get(), settings);
  80. }
  81. uint32_t flags = obs_properties_get_flags(properties.get());
  82. deferUpdate = (flags & OBS_PROPERTIES_DEFER_UPDATE) != 0;
  83. RefreshProperties();
  84. }
  85. #define NO_PROPERTIES_STRING QTStr("Basic.PropertiesWindow.NoProperties")
  86. void OBSPropertiesView::RefreshProperties()
  87. {
  88. int h, v;
  89. GetScrollPos(h, v);
  90. children.clear();
  91. if (widget)
  92. widget->deleteLater();
  93. widget = new QWidget();
  94. QFormLayout *layout = new QFormLayout;
  95. layout->setFieldGrowthPolicy(QFormLayout::AllNonFixedFieldsGrow);
  96. widget->setLayout(layout);
  97. QSizePolicy mainPolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
  98. layout->setLabelAlignment(Qt::AlignRight);
  99. obs_property_t *property = obs_properties_first(properties.get());
  100. bool hasNoProperties = !property;
  101. while (property) {
  102. AddProperty(property, layout);
  103. obs_property_next(&property);
  104. }
  105. setWidgetResizable(true);
  106. setWidget(widget);
  107. SetScrollPos(h, v);
  108. setSizePolicy(mainPolicy);
  109. lastFocused.clear();
  110. if (lastWidget) {
  111. lastWidget->setFocus(Qt::OtherFocusReason);
  112. lastWidget = nullptr;
  113. }
  114. if (hasNoProperties) {
  115. QLabel *noPropertiesLabel = new QLabel(NO_PROPERTIES_STRING);
  116. layout->addWidget(noPropertiesLabel);
  117. }
  118. emit PropertiesRefreshed();
  119. }
  120. void OBSPropertiesView::SetScrollPos(int h, int v)
  121. {
  122. QScrollBar *scroll = horizontalScrollBar();
  123. if (scroll)
  124. scroll->setValue(h);
  125. scroll = verticalScrollBar();
  126. if (scroll)
  127. scroll->setValue(v);
  128. }
  129. void OBSPropertiesView::GetScrollPos(int &h, int &v)
  130. {
  131. h = v = 0;
  132. QScrollBar *scroll = horizontalScrollBar();
  133. if (scroll)
  134. h = scroll->value();
  135. scroll = verticalScrollBar();
  136. if (scroll)
  137. v = scroll->value();
  138. }
  139. OBSPropertiesView::OBSPropertiesView(OBSData settings_, void *obj_,
  140. PropertiesReloadCallback reloadCallback,
  141. PropertiesUpdateCallback callback_,
  142. PropertiesVisualUpdateCb cb_, int minSize_)
  143. : VScrollArea(nullptr),
  144. properties(nullptr, obs_properties_destroy),
  145. settings(settings_),
  146. obj(obj_),
  147. reloadCallback(reloadCallback),
  148. callback(callback_),
  149. cb(cb_),
  150. minSize(minSize_)
  151. {
  152. setFrameShape(QFrame::NoFrame);
  153. ReloadProperties();
  154. }
  155. OBSPropertiesView::OBSPropertiesView(OBSData settings_, const char *type_,
  156. PropertiesReloadCallback reloadCallback_,
  157. int minSize_)
  158. : VScrollArea(nullptr),
  159. properties(nullptr, obs_properties_destroy),
  160. settings(settings_),
  161. type(type_),
  162. reloadCallback(reloadCallback_),
  163. minSize(minSize_)
  164. {
  165. setFrameShape(QFrame::NoFrame);
  166. ReloadProperties();
  167. }
  168. void OBSPropertiesView::resizeEvent(QResizeEvent *event)
  169. {
  170. emit PropertiesResized();
  171. VScrollArea::resizeEvent(event);
  172. }
  173. QWidget *OBSPropertiesView::NewWidget(obs_property_t *prop, QWidget *widget,
  174. const char *signal)
  175. {
  176. const char *long_desc = obs_property_long_description(prop);
  177. WidgetInfo *info = new WidgetInfo(this, prop, widget);
  178. connect(widget, signal, info, SLOT(ControlChanged()));
  179. children.emplace_back(info);
  180. widget->setToolTip(QT_UTF8(long_desc));
  181. return widget;
  182. }
  183. QWidget *OBSPropertiesView::AddCheckbox(obs_property_t *prop)
  184. {
  185. const char *name = obs_property_name(prop);
  186. const char *desc = obs_property_description(prop);
  187. bool val = obs_data_get_bool(settings, name);
  188. QCheckBox *checkbox = new QCheckBox(QT_UTF8(desc));
  189. checkbox->setCheckState(val ? Qt::Checked : Qt::Unchecked);
  190. return NewWidget(prop, checkbox, SIGNAL(stateChanged(int)));
  191. }
  192. QWidget *OBSPropertiesView::AddText(obs_property_t *prop, QFormLayout *layout,
  193. QLabel *&label)
  194. {
  195. const char *name = obs_property_name(prop);
  196. const char *val = obs_data_get_string(settings, name);
  197. const bool monospace = obs_property_text_monospace(prop);
  198. obs_text_type type = obs_property_text_type(prop);
  199. if (type == OBS_TEXT_MULTILINE) {
  200. QPlainTextEdit *edit = new QPlainTextEdit(QT_UTF8(val));
  201. #if QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)
  202. edit->setTabStopDistance(40);
  203. #else
  204. edit->setTabStopWidth(40);
  205. #endif
  206. if (monospace) {
  207. QFont f("Courier");
  208. f.setStyleHint(QFont::Monospace);
  209. edit->setFont(f);
  210. }
  211. return NewWidget(prop, edit, SIGNAL(textChanged()));
  212. } else if (type == OBS_TEXT_PASSWORD) {
  213. QLayout *subLayout = new QHBoxLayout();
  214. QLineEdit *edit = new QLineEdit();
  215. QPushButton *show = new QPushButton();
  216. show->setText(QTStr("Show"));
  217. show->setCheckable(true);
  218. edit->setText(QT_UTF8(val));
  219. edit->setEchoMode(QLineEdit::Password);
  220. subLayout->addWidget(edit);
  221. subLayout->addWidget(show);
  222. WidgetInfo *info = new WidgetInfo(this, prop, edit);
  223. connect(show, &QAbstractButton::toggled, info,
  224. &WidgetInfo::TogglePasswordText);
  225. connect(show, &QAbstractButton::toggled, [=](bool hide) {
  226. show->setText(hide ? QTStr("Hide") : QTStr("Show"));
  227. });
  228. children.emplace_back(info);
  229. label = new QLabel(QT_UTF8(obs_property_description(prop)));
  230. layout->addRow(label, subLayout);
  231. edit->setToolTip(QT_UTF8(obs_property_long_description(prop)));
  232. connect(edit, SIGNAL(textEdited(const QString &)), info,
  233. SLOT(ControlChanged()));
  234. return nullptr;
  235. }
  236. QLineEdit *edit = new QLineEdit();
  237. edit->setText(QT_UTF8(val));
  238. edit->setToolTip(QT_UTF8(obs_property_long_description(prop)));
  239. return NewWidget(prop, edit, SIGNAL(textEdited(const QString &)));
  240. }
  241. void OBSPropertiesView::AddPath(obs_property_t *prop, QFormLayout *layout,
  242. QLabel **label)
  243. {
  244. const char *name = obs_property_name(prop);
  245. const char *val = obs_data_get_string(settings, name);
  246. QLayout *subLayout = new QHBoxLayout();
  247. QLineEdit *edit = new QLineEdit();
  248. QPushButton *button = new QPushButton(QTStr("Browse"));
  249. if (!obs_property_enabled(prop)) {
  250. edit->setEnabled(false);
  251. button->setEnabled(false);
  252. }
  253. button->setProperty("themeID", "settingsButtons");
  254. edit->setText(QT_UTF8(val));
  255. edit->setReadOnly(true);
  256. edit->setToolTip(QT_UTF8(obs_property_long_description(prop)));
  257. subLayout->addWidget(edit);
  258. subLayout->addWidget(button);
  259. WidgetInfo *info = new WidgetInfo(this, prop, edit);
  260. connect(button, SIGNAL(clicked()), info, SLOT(ControlChanged()));
  261. children.emplace_back(info);
  262. *label = new QLabel(QT_UTF8(obs_property_description(prop)));
  263. layout->addRow(*label, subLayout);
  264. }
  265. void OBSPropertiesView::AddInt(obs_property_t *prop, QFormLayout *layout,
  266. QLabel **label)
  267. {
  268. obs_number_type type = obs_property_int_type(prop);
  269. QLayout *subLayout = new QHBoxLayout();
  270. const char *name = obs_property_name(prop);
  271. int val = (int)obs_data_get_int(settings, name);
  272. QSpinBox *spin = new SpinBoxIgnoreScroll();
  273. spin->setEnabled(obs_property_enabled(prop));
  274. int minVal = obs_property_int_min(prop);
  275. int maxVal = obs_property_int_max(prop);
  276. int stepVal = obs_property_int_step(prop);
  277. const char *suffix = obs_property_int_suffix(prop);
  278. spin->setMinimum(minVal);
  279. spin->setMaximum(maxVal);
  280. spin->setSingleStep(stepVal);
  281. spin->setValue(val);
  282. spin->setToolTip(QT_UTF8(obs_property_long_description(prop)));
  283. spin->setSuffix(QT_UTF8(suffix));
  284. WidgetInfo *info = new WidgetInfo(this, prop, spin);
  285. children.emplace_back(info);
  286. if (type == OBS_NUMBER_SLIDER) {
  287. QSlider *slider = new SliderIgnoreScroll();
  288. slider->setMinimum(minVal);
  289. slider->setMaximum(maxVal);
  290. slider->setPageStep(stepVal);
  291. slider->setValue(val);
  292. slider->setOrientation(Qt::Horizontal);
  293. slider->setEnabled(obs_property_enabled(prop));
  294. subLayout->addWidget(slider);
  295. connect(slider, SIGNAL(valueChanged(int)), spin,
  296. SLOT(setValue(int)));
  297. connect(spin, SIGNAL(valueChanged(int)), slider,
  298. SLOT(setValue(int)));
  299. }
  300. connect(spin, SIGNAL(valueChanged(int)), info, SLOT(ControlChanged()));
  301. subLayout->addWidget(spin);
  302. *label = new QLabel(QT_UTF8(obs_property_description(prop)));
  303. layout->addRow(*label, subLayout);
  304. }
  305. void OBSPropertiesView::AddFloat(obs_property_t *prop, QFormLayout *layout,
  306. QLabel **label)
  307. {
  308. obs_number_type type = obs_property_float_type(prop);
  309. QLayout *subLayout = new QHBoxLayout();
  310. const char *name = obs_property_name(prop);
  311. double val = obs_data_get_double(settings, name);
  312. QDoubleSpinBox *spin = new QDoubleSpinBox();
  313. if (!obs_property_enabled(prop))
  314. spin->setEnabled(false);
  315. double minVal = obs_property_float_min(prop);
  316. double maxVal = obs_property_float_max(prop);
  317. double stepVal = obs_property_float_step(prop);
  318. const char *suffix = obs_property_float_suffix(prop);
  319. spin->setMinimum(minVal);
  320. spin->setMaximum(maxVal);
  321. spin->setSingleStep(stepVal);
  322. spin->setValue(val);
  323. spin->setToolTip(QT_UTF8(obs_property_long_description(prop)));
  324. spin->setSuffix(QT_UTF8(suffix));
  325. WidgetInfo *info = new WidgetInfo(this, prop, spin);
  326. children.emplace_back(info);
  327. if (type == OBS_NUMBER_SLIDER) {
  328. DoubleSlider *slider = new DoubleSlider();
  329. slider->setDoubleConstraints(minVal, maxVal, stepVal, val);
  330. slider->setOrientation(Qt::Horizontal);
  331. subLayout->addWidget(slider);
  332. connect(slider, SIGNAL(doubleValChanged(double)), spin,
  333. SLOT(setValue(double)));
  334. connect(spin, SIGNAL(valueChanged(double)), slider,
  335. SLOT(setDoubleVal(double)));
  336. }
  337. connect(spin, SIGNAL(valueChanged(double)), info,
  338. SLOT(ControlChanged()));
  339. subLayout->addWidget(spin);
  340. *label = new QLabel(QT_UTF8(obs_property_description(prop)));
  341. layout->addRow(*label, subLayout);
  342. }
  343. static void AddComboItem(QComboBox *combo, obs_property_t *prop,
  344. obs_combo_format format, size_t idx)
  345. {
  346. const char *name = obs_property_list_item_name(prop, idx);
  347. QVariant var;
  348. if (format == OBS_COMBO_FORMAT_INT) {
  349. long long val = obs_property_list_item_int(prop, idx);
  350. var = QVariant::fromValue<long long>(val);
  351. } else if (format == OBS_COMBO_FORMAT_FLOAT) {
  352. double val = obs_property_list_item_float(prop, idx);
  353. var = QVariant::fromValue<double>(val);
  354. } else if (format == OBS_COMBO_FORMAT_STRING) {
  355. var = QByteArray(obs_property_list_item_string(prop, idx));
  356. }
  357. combo->addItem(QT_UTF8(name), var);
  358. if (!obs_property_list_item_disabled(prop, idx))
  359. return;
  360. int index = combo->findText(QT_UTF8(name));
  361. if (index < 0)
  362. return;
  363. QStandardItemModel *model =
  364. dynamic_cast<QStandardItemModel *>(combo->model());
  365. if (!model)
  366. return;
  367. QStandardItem *item = model->item(index);
  368. item->setFlags(Qt::NoItemFlags);
  369. }
  370. template<long long get_int(obs_data_t *, const char *),
  371. double get_double(obs_data_t *, const char *),
  372. const char *get_string(obs_data_t *, const char *)>
  373. static string from_obs_data(obs_data_t *data, const char *name,
  374. obs_combo_format format)
  375. {
  376. switch (format) {
  377. case OBS_COMBO_FORMAT_INT:
  378. return to_string(get_int(data, name));
  379. case OBS_COMBO_FORMAT_FLOAT:
  380. return to_string(get_double(data, name));
  381. case OBS_COMBO_FORMAT_STRING:
  382. return get_string(data, name);
  383. default:
  384. return "";
  385. }
  386. }
  387. static string from_obs_data(obs_data_t *data, const char *name,
  388. obs_combo_format format)
  389. {
  390. return from_obs_data<obs_data_get_int, obs_data_get_double,
  391. obs_data_get_string>(data, name, format);
  392. }
  393. static string from_obs_data_autoselect(obs_data_t *data, const char *name,
  394. obs_combo_format format)
  395. {
  396. return from_obs_data<obs_data_get_autoselect_int,
  397. obs_data_get_autoselect_double,
  398. obs_data_get_autoselect_string>(data, name,
  399. format);
  400. }
  401. QWidget *OBSPropertiesView::AddList(obs_property_t *prop, bool &warning)
  402. {
  403. const char *name = obs_property_name(prop);
  404. QComboBox *combo = new ComboBoxIgnoreScroll();
  405. obs_combo_type type = obs_property_list_type(prop);
  406. obs_combo_format format = obs_property_list_format(prop);
  407. size_t count = obs_property_list_item_count(prop);
  408. int idx = -1;
  409. for (size_t i = 0; i < count; i++)
  410. AddComboItem(combo, prop, format, i);
  411. if (type == OBS_COMBO_TYPE_EDITABLE)
  412. combo->setEditable(true);
  413. combo->setMaxVisibleItems(40);
  414. combo->setToolTip(QT_UTF8(obs_property_long_description(prop)));
  415. string value = from_obs_data(settings, name, format);
  416. if (format == OBS_COMBO_FORMAT_STRING &&
  417. type == OBS_COMBO_TYPE_EDITABLE) {
  418. combo->lineEdit()->setText(QT_UTF8(value.c_str()));
  419. } else {
  420. idx = combo->findData(QByteArray(value.c_str()));
  421. }
  422. if (type == OBS_COMBO_TYPE_EDITABLE)
  423. return NewWidget(prop, combo,
  424. SIGNAL(editTextChanged(const QString &)));
  425. if (idx != -1)
  426. combo->setCurrentIndex(idx);
  427. if (obs_data_has_autoselect_value(settings, name)) {
  428. string autoselect =
  429. from_obs_data_autoselect(settings, name, format);
  430. int id = combo->findData(QT_UTF8(autoselect.c_str()));
  431. if (id != -1 && id != idx) {
  432. QString actual = combo->itemText(id);
  433. QString selected = combo->itemText(idx);
  434. QString combined = QTStr(
  435. "Basic.PropertiesWindow.AutoSelectFormat");
  436. combo->setItemText(idx,
  437. combined.arg(selected).arg(actual));
  438. }
  439. }
  440. QAbstractItemModel *model = combo->model();
  441. warning = idx != -1 &&
  442. model->flags(model->index(idx, 0)) == Qt::NoItemFlags;
  443. WidgetInfo *info = new WidgetInfo(this, prop, combo);
  444. connect(combo, SIGNAL(currentIndexChanged(int)), info,
  445. SLOT(ControlChanged()));
  446. children.emplace_back(info);
  447. /* trigger a settings update if the index was not found */
  448. if (idx == -1)
  449. info->ControlChanged();
  450. return combo;
  451. }
  452. static void NewButton(QLayout *layout, WidgetInfo *info, const char *themeIcon,
  453. void (WidgetInfo::*method)())
  454. {
  455. QPushButton *button = new QPushButton();
  456. button->setProperty("themeID", themeIcon);
  457. button->setFlat(true);
  458. button->setMaximumSize(22, 22);
  459. button->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
  460. QObject::connect(button, &QPushButton::clicked, info, method);
  461. layout->addWidget(button);
  462. }
  463. void OBSPropertiesView::AddEditableList(obs_property_t *prop,
  464. QFormLayout *layout, QLabel *&label)
  465. {
  466. const char *name = obs_property_name(prop);
  467. obs_data_array_t *array = obs_data_get_array(settings, name);
  468. QListWidget *list = new QListWidget();
  469. size_t count = obs_data_array_count(array);
  470. if (!obs_property_enabled(prop))
  471. list->setEnabled(false);
  472. list->setSortingEnabled(false);
  473. list->setSelectionMode(QAbstractItemView::ExtendedSelection);
  474. list->setToolTip(QT_UTF8(obs_property_long_description(prop)));
  475. for (size_t i = 0; i < count; i++) {
  476. obs_data_t *item = obs_data_array_item(array, i);
  477. list->addItem(QT_UTF8(obs_data_get_string(item, "value")));
  478. QListWidgetItem *const list_item = list->item((int)i);
  479. list_item->setSelected(obs_data_get_bool(item, "selected"));
  480. list_item->setHidden(obs_data_get_bool(item, "hidden"));
  481. obs_data_release(item);
  482. }
  483. WidgetInfo *info = new WidgetInfo(this, prop, list);
  484. list->setDragDropMode(QAbstractItemView::InternalMove);
  485. connect(list->model(),
  486. SIGNAL(rowsMoved(QModelIndex, int, int, QModelIndex, int)),
  487. info,
  488. SLOT(EditListReordered(const QModelIndex &, int, int,
  489. const QModelIndex &, int)));
  490. QVBoxLayout *sideLayout = new QVBoxLayout();
  491. NewButton(sideLayout, info, "addIconSmall", &WidgetInfo::EditListAdd);
  492. NewButton(sideLayout, info, "removeIconSmall",
  493. &WidgetInfo::EditListRemove);
  494. NewButton(sideLayout, info, "configIconSmall",
  495. &WidgetInfo::EditListEdit);
  496. NewButton(sideLayout, info, "upArrowIconSmall",
  497. &WidgetInfo::EditListUp);
  498. NewButton(sideLayout, info, "downArrowIconSmall",
  499. &WidgetInfo::EditListDown);
  500. sideLayout->addStretch(0);
  501. QHBoxLayout *subLayout = new QHBoxLayout();
  502. subLayout->addWidget(list);
  503. subLayout->addLayout(sideLayout);
  504. children.emplace_back(info);
  505. label = new QLabel(QT_UTF8(obs_property_description(prop)));
  506. layout->addRow(label, subLayout);
  507. obs_data_array_release(array);
  508. }
  509. QWidget *OBSPropertiesView::AddButton(obs_property_t *prop)
  510. {
  511. const char *desc = obs_property_description(prop);
  512. QPushButton *button = new QPushButton(QT_UTF8(desc));
  513. button->setProperty("themeID", "settingsButtons");
  514. button->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
  515. return NewWidget(prop, button, SIGNAL(clicked()));
  516. }
  517. void OBSPropertiesView::AddColorInternal(obs_property_t *prop,
  518. QFormLayout *layout, QLabel *&label,
  519. bool supportAlpha)
  520. {
  521. QPushButton *button = new QPushButton;
  522. QLabel *colorLabel = new QLabel;
  523. const char *name = obs_property_name(prop);
  524. long long val = obs_data_get_int(settings, name);
  525. QColor color = color_from_int(val);
  526. QColor::NameFormat format;
  527. if (!obs_property_enabled(prop)) {
  528. button->setEnabled(false);
  529. colorLabel->setEnabled(false);
  530. }
  531. button->setProperty("themeID", "settingsButtons");
  532. button->setText(QTStr("Basic.PropertiesWindow.SelectColor"));
  533. button->setToolTip(QT_UTF8(obs_property_long_description(prop)));
  534. if (supportAlpha) {
  535. format = QColor::HexArgb;
  536. } else {
  537. format = QColor::HexRgb;
  538. color.setAlpha(255);
  539. }
  540. QPalette palette = QPalette(color);
  541. colorLabel->setFrameStyle(QFrame::Sunken | QFrame::Panel);
  542. colorLabel->setText(color.name(format));
  543. colorLabel->setPalette(palette);
  544. colorLabel->setStyleSheet(
  545. QString("background-color :%1; color: %2;")
  546. .arg(palette.color(QPalette::Window).name(format))
  547. .arg(palette.color(QPalette::WindowText).name(format)));
  548. colorLabel->setAutoFillBackground(true);
  549. colorLabel->setAlignment(Qt::AlignCenter);
  550. colorLabel->setToolTip(QT_UTF8(obs_property_long_description(prop)));
  551. QHBoxLayout *subLayout = new QHBoxLayout;
  552. subLayout->setContentsMargins(0, 0, 0, 0);
  553. subLayout->addWidget(colorLabel);
  554. subLayout->addWidget(button);
  555. WidgetInfo *info = new WidgetInfo(this, prop, colorLabel);
  556. connect(button, SIGNAL(clicked()), info, SLOT(ControlChanged()));
  557. children.emplace_back(info);
  558. label = new QLabel(QT_UTF8(obs_property_description(prop)));
  559. layout->addRow(label, subLayout);
  560. }
  561. void OBSPropertiesView::AddColor(obs_property_t *prop, QFormLayout *layout,
  562. QLabel *&label)
  563. {
  564. AddColorInternal(prop, layout, label, false);
  565. }
  566. void OBSPropertiesView::AddColorAlpha(obs_property_t *prop, QFormLayout *layout,
  567. QLabel *&label)
  568. {
  569. AddColorInternal(prop, layout, label, true);
  570. }
  571. void MakeQFont(obs_data_t *font_obj, QFont &font, bool limit = false)
  572. {
  573. const char *face = obs_data_get_string(font_obj, "face");
  574. const char *style = obs_data_get_string(font_obj, "style");
  575. int size = (int)obs_data_get_int(font_obj, "size");
  576. uint32_t flags = (uint32_t)obs_data_get_int(font_obj, "flags");
  577. if (face) {
  578. font.setFamily(face);
  579. font.setStyleName(style);
  580. }
  581. if (size) {
  582. if (limit) {
  583. int max_size = font.pointSize();
  584. if (max_size < 28)
  585. max_size = 28;
  586. if (size > max_size)
  587. size = max_size;
  588. }
  589. font.setPointSize(size);
  590. }
  591. if (flags & OBS_FONT_BOLD)
  592. font.setBold(true);
  593. if (flags & OBS_FONT_ITALIC)
  594. font.setItalic(true);
  595. if (flags & OBS_FONT_UNDERLINE)
  596. font.setUnderline(true);
  597. if (flags & OBS_FONT_STRIKEOUT)
  598. font.setStrikeOut(true);
  599. }
  600. void OBSPropertiesView::AddFont(obs_property_t *prop, QFormLayout *layout,
  601. QLabel *&label)
  602. {
  603. const char *name = obs_property_name(prop);
  604. obs_data_t *font_obj = obs_data_get_obj(settings, name);
  605. const char *face = obs_data_get_string(font_obj, "face");
  606. const char *style = obs_data_get_string(font_obj, "style");
  607. QPushButton *button = new QPushButton;
  608. QLabel *fontLabel = new QLabel;
  609. QFont font;
  610. if (!obs_property_enabled(prop)) {
  611. button->setEnabled(false);
  612. fontLabel->setEnabled(false);
  613. }
  614. font = fontLabel->font();
  615. MakeQFont(font_obj, font, true);
  616. button->setProperty("themeID", "settingsButtons");
  617. button->setText(QTStr("Basic.PropertiesWindow.SelectFont"));
  618. button->setToolTip(QT_UTF8(obs_property_long_description(prop)));
  619. fontLabel->setFrameStyle(QFrame::Sunken | QFrame::Panel);
  620. fontLabel->setFont(font);
  621. fontLabel->setText(QString("%1 %2").arg(face, style));
  622. fontLabel->setAlignment(Qt::AlignCenter);
  623. fontLabel->setToolTip(QT_UTF8(obs_property_long_description(prop)));
  624. QHBoxLayout *subLayout = new QHBoxLayout;
  625. subLayout->setContentsMargins(0, 0, 0, 0);
  626. subLayout->addWidget(fontLabel);
  627. subLayout->addWidget(button);
  628. WidgetInfo *info = new WidgetInfo(this, prop, fontLabel);
  629. connect(button, SIGNAL(clicked()), info, SLOT(ControlChanged()));
  630. children.emplace_back(info);
  631. label = new QLabel(QT_UTF8(obs_property_description(prop)));
  632. layout->addRow(label, subLayout);
  633. obs_data_release(font_obj);
  634. }
  635. namespace std {
  636. template<> struct default_delete<obs_data_t> {
  637. void operator()(obs_data_t *data) { obs_data_release(data); }
  638. };
  639. template<> struct default_delete<obs_data_item_t> {
  640. void operator()(obs_data_item_t *item) { obs_data_item_release(&item); }
  641. };
  642. }
  643. template<typename T> static double make_epsilon(T val)
  644. {
  645. return val * 0.00001;
  646. }
  647. static bool matches_range(media_frames_per_second &match,
  648. media_frames_per_second fps,
  649. const frame_rate_range_t &pair)
  650. {
  651. auto val = media_frames_per_second_to_frame_interval(fps);
  652. auto max_ = media_frames_per_second_to_frame_interval(pair.first);
  653. auto min_ = media_frames_per_second_to_frame_interval(pair.second);
  654. if (min_ <= val && val <= max_) {
  655. match = fps;
  656. return true;
  657. }
  658. return false;
  659. }
  660. static bool matches_ranges(media_frames_per_second &best_match,
  661. media_frames_per_second fps,
  662. const frame_rate_ranges_t &fps_ranges,
  663. bool exact = false)
  664. {
  665. auto convert_fn = media_frames_per_second_to_frame_interval;
  666. auto val = convert_fn(fps);
  667. auto epsilon = make_epsilon(val);
  668. bool match = false;
  669. auto best_dist = numeric_limits<double>::max();
  670. for (auto &pair : fps_ranges) {
  671. auto max_ = convert_fn(pair.first);
  672. auto min_ = convert_fn(pair.second);
  673. /*blog(LOG_INFO, "%lg ≤ %lg ≤ %lg? %s %s %s",
  674. min_, val, max_,
  675. fabsl(min_ - val) < epsilon ? "true" : "false",
  676. min_ <= val && val <= max_ ? "true" : "false",
  677. fabsl(min_ - val) < epsilon ? "true" :
  678. "false");*/
  679. if (matches_range(best_match, fps, pair))
  680. return true;
  681. if (exact)
  682. continue;
  683. auto min_dist = fabsl(min_ - val);
  684. auto max_dist = fabsl(max_ - val);
  685. if (min_dist < epsilon && min_dist < best_dist) {
  686. best_match = pair.first;
  687. match = true;
  688. continue;
  689. }
  690. if (max_dist < epsilon && max_dist < best_dist) {
  691. best_match = pair.second;
  692. match = true;
  693. continue;
  694. }
  695. }
  696. return match;
  697. }
  698. static media_frames_per_second make_fps(uint32_t num, uint32_t den)
  699. {
  700. media_frames_per_second fps{};
  701. fps.numerator = num;
  702. fps.denominator = den;
  703. return fps;
  704. }
  705. static const common_frame_rate common_fps[] = {
  706. {"60", {60, 1}}, {"59.94", {60000, 1001}}, {"50", {50, 1}},
  707. {"48", {48, 1}}, {"30", {30, 1}}, {"29.97", {30000, 1001}},
  708. {"25", {25, 1}}, {"24", {24, 1}}, {"23.976", {24000, 1001}},
  709. };
  710. static void UpdateSimpleFPSSelection(OBSFrameRatePropertyWidget *fpsProps,
  711. const media_frames_per_second *current_fps)
  712. {
  713. if (!current_fps || !media_frames_per_second_is_valid(*current_fps)) {
  714. fpsProps->simpleFPS->setCurrentIndex(0);
  715. return;
  716. }
  717. auto combo = fpsProps->simpleFPS;
  718. auto num = combo->count();
  719. for (int i = 0; i < num; i++) {
  720. auto variant = combo->itemData(i);
  721. if (!variant.canConvert<media_frames_per_second>())
  722. continue;
  723. auto fps = variant.value<media_frames_per_second>();
  724. if (fps != *current_fps)
  725. continue;
  726. combo->setCurrentIndex(i);
  727. return;
  728. }
  729. combo->setCurrentIndex(0);
  730. }
  731. static void AddFPSRanges(vector<common_frame_rate> &items,
  732. const frame_rate_ranges_t &ranges)
  733. {
  734. auto InsertFPS = [&](media_frames_per_second fps) {
  735. auto fps_val = media_frames_per_second_to_fps(fps);
  736. auto end_ = end(items);
  737. auto i = begin(items);
  738. for (; i != end_; i++) {
  739. auto i_fps_val = media_frames_per_second_to_fps(i->fps);
  740. if (fabsl(i_fps_val - fps_val) < 0.01)
  741. return;
  742. if (i_fps_val > fps_val)
  743. continue;
  744. break;
  745. }
  746. items.insert(i, {nullptr, fps});
  747. };
  748. for (auto &range : ranges) {
  749. InsertFPS(range.first);
  750. InsertFPS(range.second);
  751. }
  752. }
  753. static QWidget *
  754. CreateSimpleFPSValues(OBSFrameRatePropertyWidget *fpsProps, bool &selected,
  755. const media_frames_per_second *current_fps)
  756. {
  757. auto widget = new QWidget{};
  758. widget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
  759. auto layout = new QVBoxLayout{};
  760. layout->setContentsMargins(0, 0, 0, 0);
  761. auto items = vector<common_frame_rate>{};
  762. items.reserve(sizeof(common_fps) / sizeof(common_frame_rate));
  763. auto combo = fpsProps->simpleFPS = new ComboBoxIgnoreScroll{};
  764. combo->addItem("", QVariant::fromValue(make_fps(0, 0)));
  765. for (const auto &fps : common_fps) {
  766. media_frames_per_second best_match{};
  767. if (!matches_ranges(best_match, fps.fps, fpsProps->fps_ranges))
  768. continue;
  769. items.push_back({fps.fps_name, best_match});
  770. }
  771. AddFPSRanges(items, fpsProps->fps_ranges);
  772. for (const auto &item : items) {
  773. auto var = QVariant::fromValue(item.fps);
  774. auto name = item.fps_name
  775. ? QString(item.fps_name)
  776. : QString("%1").arg(
  777. media_frames_per_second_to_fps(
  778. item.fps));
  779. combo->addItem(name, var);
  780. bool select = current_fps && *current_fps == item.fps;
  781. if (select) {
  782. combo->setCurrentIndex(combo->count() - 1);
  783. selected = true;
  784. }
  785. }
  786. layout->addWidget(combo, 0, Qt::AlignTop);
  787. widget->setLayout(layout);
  788. return widget;
  789. }
  790. static void UpdateRationalFPSWidgets(OBSFrameRatePropertyWidget *fpsProps,
  791. const media_frames_per_second *current_fps)
  792. {
  793. if (!current_fps || !media_frames_per_second_is_valid(*current_fps)) {
  794. fpsProps->numEdit->setValue(0);
  795. fpsProps->denEdit->setValue(0);
  796. return;
  797. }
  798. auto combo = fpsProps->fpsRange;
  799. auto num = combo->count();
  800. for (int i = 0; i < num; i++) {
  801. auto variant = combo->itemData(i);
  802. if (!variant.canConvert<size_t>())
  803. continue;
  804. auto idx = variant.value<size_t>();
  805. if (fpsProps->fps_ranges.size() < idx)
  806. continue;
  807. media_frames_per_second match{};
  808. if (!matches_range(match, *current_fps,
  809. fpsProps->fps_ranges[idx]))
  810. continue;
  811. combo->setCurrentIndex(i);
  812. break;
  813. }
  814. fpsProps->numEdit->setValue(current_fps->numerator);
  815. fpsProps->denEdit->setValue(current_fps->denominator);
  816. }
  817. static QWidget *CreateRationalFPS(OBSFrameRatePropertyWidget *fpsProps,
  818. bool &selected,
  819. const media_frames_per_second *current_fps)
  820. {
  821. auto widget = new QWidget{};
  822. widget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
  823. auto layout = new QFormLayout{};
  824. layout->setContentsMargins(0, 0, 0, 0);
  825. layout->setSpacing(4);
  826. auto str = QTStr("Basic.PropertiesView.FPS.ValidFPSRanges");
  827. auto rlabel = new QLabel{str};
  828. auto combo = fpsProps->fpsRange = new ComboBoxIgnoreScroll{};
  829. auto convert_fps = media_frames_per_second_to_fps;
  830. //auto convert_fi = media_frames_per_second_to_frame_interval;
  831. for (size_t i = 0; i < fpsProps->fps_ranges.size(); i++) {
  832. auto &pair = fpsProps->fps_ranges[i];
  833. combo->addItem(QString{"%1 - %2"}
  834. .arg(convert_fps(pair.first))
  835. .arg(convert_fps(pair.second)),
  836. QVariant::fromValue(i));
  837. media_frames_per_second match;
  838. if (!current_fps || !matches_range(match, *current_fps, pair))
  839. continue;
  840. combo->setCurrentIndex(combo->count() - 1);
  841. selected = true;
  842. }
  843. layout->addRow(rlabel, combo);
  844. auto num_edit = fpsProps->numEdit = new SpinBoxIgnoreScroll{};
  845. auto den_edit = fpsProps->denEdit = new SpinBoxIgnoreScroll{};
  846. num_edit->setRange(0, INT_MAX);
  847. den_edit->setRange(0, INT_MAX);
  848. if (current_fps) {
  849. num_edit->setValue(current_fps->numerator);
  850. den_edit->setValue(current_fps->denominator);
  851. }
  852. layout->addRow(QTStr("Basic.Settings.Video.Numerator"), num_edit);
  853. layout->addRow(QTStr("Basic.Settings.Video.Denominator"), den_edit);
  854. widget->setLayout(layout);
  855. return widget;
  856. }
  857. static OBSFrameRatePropertyWidget *
  858. CreateFrameRateWidget(obs_property_t *prop, bool &warning, const char *option,
  859. media_frames_per_second *current_fps,
  860. frame_rate_ranges_t &fps_ranges)
  861. {
  862. auto widget = new OBSFrameRatePropertyWidget{};
  863. auto hlayout = new QHBoxLayout{};
  864. hlayout->setContentsMargins(0, 0, 0, 0);
  865. swap(widget->fps_ranges, fps_ranges);
  866. auto combo = widget->modeSelect = new ComboBoxIgnoreScroll{};
  867. combo->addItem(QTStr("Basic.PropertiesView.FPS.Simple"),
  868. QVariant::fromValue(frame_rate_tag::simple()));
  869. combo->addItem(QTStr("Basic.PropertiesView.FPS.Rational"),
  870. QVariant::fromValue(frame_rate_tag::rational()));
  871. combo->setToolTip(QT_UTF8(obs_property_long_description(prop)));
  872. auto num = obs_property_frame_rate_options_count(prop);
  873. if (num)
  874. combo->insertSeparator(combo->count());
  875. bool option_found = false;
  876. for (size_t i = 0; i < num; i++) {
  877. auto name = obs_property_frame_rate_option_name(prop, i);
  878. auto desc = obs_property_frame_rate_option_description(prop, i);
  879. combo->addItem(desc, QVariant::fromValue(frame_rate_tag{name}));
  880. if (!name || !option || string(name) != option)
  881. continue;
  882. option_found = true;
  883. combo->setCurrentIndex(combo->count() - 1);
  884. }
  885. hlayout->addWidget(combo, 0, Qt::AlignTop);
  886. auto stack = widget->modeDisplay = new QStackedWidget{};
  887. bool match_found = option_found;
  888. auto AddWidget = [&](decltype(CreateRationalFPS) func) {
  889. bool selected = false;
  890. stack->addWidget(func(widget, selected, current_fps));
  891. if (match_found || !selected)
  892. return;
  893. match_found = true;
  894. stack->setCurrentIndex(stack->count() - 1);
  895. combo->setCurrentIndex(stack->count() - 1);
  896. };
  897. AddWidget(CreateSimpleFPSValues);
  898. AddWidget(CreateRationalFPS);
  899. stack->addWidget(new QWidget{});
  900. if (option_found)
  901. stack->setCurrentIndex(stack->count() - 1);
  902. else if (!match_found) {
  903. int idx = current_fps ? 1 : 0; // Rational for "unsupported"
  904. // Simple as default
  905. stack->setCurrentIndex(idx);
  906. combo->setCurrentIndex(idx);
  907. warning = true;
  908. }
  909. hlayout->addWidget(stack, 0, Qt::AlignTop);
  910. auto label_area = widget->labels = new QWidget{};
  911. label_area->setSizePolicy(QSizePolicy::Expanding,
  912. QSizePolicy::Expanding);
  913. auto vlayout = new QVBoxLayout{};
  914. vlayout->setContentsMargins(0, 0, 0, 0);
  915. auto fps_label = widget->currentFPS = new QLabel{"FPS: 22"};
  916. auto time_label = widget->timePerFrame =
  917. new QLabel{"Frame Interval: 0.123 ms"};
  918. auto min_label = widget->minLabel = new QLabel{"Min FPS: 1/1"};
  919. auto max_label = widget->maxLabel = new QLabel{"Max FPS: 2/1"};
  920. min_label->setHidden(true);
  921. max_label->setHidden(true);
  922. auto flags = Qt::TextSelectableByMouse | Qt::TextSelectableByKeyboard;
  923. min_label->setTextInteractionFlags(flags);
  924. max_label->setTextInteractionFlags(flags);
  925. vlayout->addWidget(fps_label);
  926. vlayout->addWidget(time_label);
  927. vlayout->addWidget(min_label);
  928. vlayout->addWidget(max_label);
  929. label_area->setLayout(vlayout);
  930. hlayout->addWidget(label_area, 0, Qt::AlignTop);
  931. widget->setLayout(hlayout);
  932. return widget;
  933. }
  934. static void UpdateMinMaxLabels(OBSFrameRatePropertyWidget *w)
  935. {
  936. auto Hide = [&](bool hide) {
  937. w->minLabel->setHidden(hide);
  938. w->maxLabel->setHidden(hide);
  939. };
  940. auto variant = w->modeSelect->currentData();
  941. if (!variant.canConvert<frame_rate_tag>() ||
  942. variant.value<frame_rate_tag>().type != frame_rate_tag::RATIONAL) {
  943. Hide(true);
  944. return;
  945. }
  946. variant = w->fpsRange->currentData();
  947. if (!variant.canConvert<size_t>()) {
  948. Hide(true);
  949. return;
  950. }
  951. auto idx = variant.value<size_t>();
  952. if (idx >= w->fps_ranges.size()) {
  953. Hide(true);
  954. return;
  955. }
  956. Hide(false);
  957. auto min = w->fps_ranges[idx].first;
  958. auto max = w->fps_ranges[idx].second;
  959. w->minLabel->setText(QString("Min FPS: %1/%2")
  960. .arg(min.numerator)
  961. .arg(min.denominator));
  962. w->maxLabel->setText(QString("Max FPS: %1/%2")
  963. .arg(max.numerator)
  964. .arg(max.denominator));
  965. }
  966. static void UpdateFPSLabels(OBSFrameRatePropertyWidget *w)
  967. {
  968. UpdateMinMaxLabels(w);
  969. unique_ptr<obs_data_item_t> obj{
  970. obs_data_item_byname(w->settings, w->name)};
  971. media_frames_per_second fps{};
  972. media_frames_per_second *valid_fps = nullptr;
  973. if (obs_data_item_get_autoselect_frames_per_second(obj.get(), &fps,
  974. nullptr) ||
  975. obs_data_item_get_frames_per_second(obj.get(), &fps, nullptr))
  976. valid_fps = &fps;
  977. const char *option = nullptr;
  978. obs_data_item_get_frames_per_second(obj.get(), nullptr, &option);
  979. if (!valid_fps) {
  980. w->currentFPS->setHidden(true);
  981. w->timePerFrame->setHidden(true);
  982. if (!option)
  983. w->warningLabel->setStyleSheet(
  984. "QLabel { color: red; }");
  985. return;
  986. }
  987. w->currentFPS->setHidden(false);
  988. w->timePerFrame->setHidden(false);
  989. media_frames_per_second match{};
  990. if (!option && !matches_ranges(match, *valid_fps, w->fps_ranges, true))
  991. w->warningLabel->setStyleSheet("QLabel { color: red; }");
  992. else
  993. w->warningLabel->setStyleSheet("");
  994. auto convert_to_fps = media_frames_per_second_to_fps;
  995. auto convert_to_frame_interval =
  996. media_frames_per_second_to_frame_interval;
  997. w->currentFPS->setText(
  998. QString("FPS: %1").arg(convert_to_fps(*valid_fps)));
  999. w->timePerFrame->setText(
  1000. QString("Frame Interval: %1 ms")
  1001. .arg(convert_to_frame_interval(*valid_fps) * 1000));
  1002. }
  1003. void OBSPropertiesView::AddFrameRate(obs_property_t *prop, bool &warning,
  1004. QFormLayout *layout, QLabel *&label)
  1005. {
  1006. const char *name = obs_property_name(prop);
  1007. bool enabled = obs_property_enabled(prop);
  1008. unique_ptr<obs_data_item_t> obj{obs_data_item_byname(settings, name)};
  1009. const char *option = nullptr;
  1010. obs_data_item_get_frames_per_second(obj.get(), nullptr, &option);
  1011. media_frames_per_second fps{};
  1012. media_frames_per_second *valid_fps = nullptr;
  1013. if (obs_data_item_get_frames_per_second(obj.get(), &fps, nullptr))
  1014. valid_fps = &fps;
  1015. frame_rate_ranges_t fps_ranges;
  1016. size_t num = obs_property_frame_rate_fps_ranges_count(prop);
  1017. fps_ranges.reserve(num);
  1018. for (size_t i = 0; i < num; i++)
  1019. fps_ranges.emplace_back(
  1020. obs_property_frame_rate_fps_range_min(prop, i),
  1021. obs_property_frame_rate_fps_range_max(prop, i));
  1022. auto widget = CreateFrameRateWidget(prop, warning, option, valid_fps,
  1023. fps_ranges);
  1024. auto info = new WidgetInfo(this, prop, widget);
  1025. widget->setToolTip(QT_UTF8(obs_property_long_description(prop)));
  1026. widget->name = name;
  1027. widget->settings = settings;
  1028. widget->modeSelect->setEnabled(enabled);
  1029. widget->simpleFPS->setEnabled(enabled);
  1030. widget->fpsRange->setEnabled(enabled);
  1031. widget->numEdit->setEnabled(enabled);
  1032. widget->denEdit->setEnabled(enabled);
  1033. label = widget->warningLabel =
  1034. new QLabel{obs_property_description(prop)};
  1035. layout->addRow(label, widget);
  1036. children.emplace_back(info);
  1037. UpdateFPSLabels(widget);
  1038. auto stack = widget->modeDisplay;
  1039. auto combo = widget->modeSelect;
  1040. stack->setToolTip(QT_UTF8(obs_property_long_description(prop)));
  1041. combo->setToolTip(QT_UTF8(obs_property_long_description(prop)));
  1042. auto comboIndexChanged = static_cast<void (QComboBox::*)(int)>(
  1043. &QComboBox::currentIndexChanged);
  1044. connect(combo, comboIndexChanged, stack, [=](int index) {
  1045. bool out_of_bounds = index >= stack->count();
  1046. auto idx = out_of_bounds ? stack->count() - 1 : index;
  1047. stack->setCurrentIndex(idx);
  1048. if (widget->updating)
  1049. return;
  1050. UpdateFPSLabels(widget);
  1051. emit info->ControlChanged();
  1052. });
  1053. connect(widget->simpleFPS, comboIndexChanged, [=](int) {
  1054. if (widget->updating)
  1055. return;
  1056. emit info->ControlChanged();
  1057. });
  1058. connect(widget->fpsRange, comboIndexChanged, [=](int) {
  1059. if (widget->updating)
  1060. return;
  1061. UpdateFPSLabels(widget);
  1062. });
  1063. auto sbValueChanged =
  1064. static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged);
  1065. connect(widget->numEdit, sbValueChanged, [=](int) {
  1066. if (widget->updating)
  1067. return;
  1068. emit info->ControlChanged();
  1069. });
  1070. connect(widget->denEdit, sbValueChanged, [=](int) {
  1071. if (widget->updating)
  1072. return;
  1073. emit info->ControlChanged();
  1074. });
  1075. }
  1076. void OBSPropertiesView::AddGroup(obs_property_t *prop, QFormLayout *layout)
  1077. {
  1078. const char *name = obs_property_name(prop);
  1079. bool val = obs_data_get_bool(settings, name);
  1080. const char *desc = obs_property_description(prop);
  1081. enum obs_group_type type = obs_property_group_type(prop);
  1082. // Create GroupBox
  1083. QGroupBox *groupBox = new QGroupBox(QT_UTF8(desc));
  1084. groupBox->setCheckable(type == OBS_GROUP_CHECKABLE);
  1085. groupBox->setChecked(groupBox->isCheckable() ? val : true);
  1086. groupBox->setAccessibleName("group");
  1087. groupBox->setEnabled(obs_property_enabled(prop));
  1088. // Create Layout and build content
  1089. QFormLayout *subLayout = new QFormLayout();
  1090. subLayout->setFieldGrowthPolicy(QFormLayout::AllNonFixedFieldsGrow);
  1091. groupBox->setLayout(subLayout);
  1092. obs_properties_t *content = obs_property_group_content(prop);
  1093. obs_property_t *el = obs_properties_first(content);
  1094. while (el != nullptr) {
  1095. AddProperty(el, subLayout);
  1096. obs_property_next(&el);
  1097. }
  1098. // Insert into UI
  1099. layout->setWidget(layout->rowCount(),
  1100. QFormLayout::ItemRole::SpanningRole, groupBox);
  1101. // Register Group Widget
  1102. WidgetInfo *info = new WidgetInfo(this, prop, groupBox);
  1103. children.emplace_back(info);
  1104. // Signals
  1105. connect(groupBox, SIGNAL(toggled(bool)), info, SLOT(ControlChanged()));
  1106. }
  1107. void OBSPropertiesView::AddProperty(obs_property_t *property,
  1108. QFormLayout *layout)
  1109. {
  1110. const char *name = obs_property_name(property);
  1111. obs_property_type type = obs_property_get_type(property);
  1112. if (!obs_property_visible(property))
  1113. return;
  1114. QLabel *label = nullptr;
  1115. QWidget *widget = nullptr;
  1116. bool warning = false;
  1117. switch (type) {
  1118. case OBS_PROPERTY_INVALID:
  1119. return;
  1120. case OBS_PROPERTY_BOOL:
  1121. widget = AddCheckbox(property);
  1122. break;
  1123. case OBS_PROPERTY_INT:
  1124. AddInt(property, layout, &label);
  1125. break;
  1126. case OBS_PROPERTY_FLOAT:
  1127. AddFloat(property, layout, &label);
  1128. break;
  1129. case OBS_PROPERTY_TEXT:
  1130. widget = AddText(property, layout, label);
  1131. break;
  1132. case OBS_PROPERTY_PATH:
  1133. AddPath(property, layout, &label);
  1134. break;
  1135. case OBS_PROPERTY_LIST:
  1136. widget = AddList(property, warning);
  1137. break;
  1138. case OBS_PROPERTY_COLOR:
  1139. AddColor(property, layout, label);
  1140. break;
  1141. case OBS_PROPERTY_FONT:
  1142. AddFont(property, layout, label);
  1143. break;
  1144. case OBS_PROPERTY_BUTTON:
  1145. widget = AddButton(property);
  1146. break;
  1147. case OBS_PROPERTY_EDITABLE_LIST:
  1148. AddEditableList(property, layout, label);
  1149. break;
  1150. case OBS_PROPERTY_FRAME_RATE:
  1151. AddFrameRate(property, warning, layout, label);
  1152. break;
  1153. case OBS_PROPERTY_GROUP:
  1154. AddGroup(property, layout);
  1155. break;
  1156. case OBS_PROPERTY_COLOR_ALPHA:
  1157. AddColorAlpha(property, layout, label);
  1158. }
  1159. if (widget && !obs_property_enabled(property))
  1160. widget->setEnabled(false);
  1161. if (!label && type != OBS_PROPERTY_BOOL &&
  1162. type != OBS_PROPERTY_BUTTON && type != OBS_PROPERTY_GROUP)
  1163. label = new QLabel(QT_UTF8(obs_property_description(property)));
  1164. if (warning && label) //TODO: select color based on background color
  1165. label->setStyleSheet("QLabel { color: red; }");
  1166. if (label && minSize) {
  1167. label->setMinimumWidth(minSize);
  1168. label->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
  1169. }
  1170. if (label && !obs_property_enabled(property))
  1171. label->setEnabled(false);
  1172. if (!widget)
  1173. return;
  1174. if (obs_property_long_description(property)) {
  1175. bool lightTheme = palette().text().color().redF() < 0.5;
  1176. QString file = lightTheme ? ":/res/images/help.svg"
  1177. : ":/res/images/help_light.svg";
  1178. if (label) {
  1179. QString lStr = "<html>%1 <img src='%2' style=' \
  1180. vertical-align: bottom; \
  1181. ' /></html>";
  1182. label->setText(lStr.arg(label->text(), file));
  1183. label->setToolTip(
  1184. obs_property_long_description(property));
  1185. } else if (type == OBS_PROPERTY_BOOL) {
  1186. QString bStr = "<html> <img src='%1' style=' \
  1187. vertical-align: bottom; \
  1188. ' /></html>";
  1189. const char *desc = obs_property_description(property);
  1190. QWidget *newWidget = new QWidget();
  1191. QHBoxLayout *boxLayout = new QHBoxLayout(newWidget);
  1192. boxLayout->setContentsMargins(0, 0, 0, 0);
  1193. boxLayout->setAlignment(Qt::AlignLeft);
  1194. boxLayout->setSpacing(0);
  1195. QCheckBox *check = qobject_cast<QCheckBox *>(widget);
  1196. check->setText(desc);
  1197. check->setToolTip(
  1198. obs_property_long_description(property));
  1199. QLabel *help = new QLabel(check);
  1200. help->setText(bStr.arg(file));
  1201. help->setToolTip(
  1202. obs_property_long_description(property));
  1203. boxLayout->addWidget(check);
  1204. boxLayout->addWidget(help);
  1205. widget = newWidget;
  1206. }
  1207. }
  1208. layout->addRow(label, widget);
  1209. if (!lastFocused.empty())
  1210. if (lastFocused.compare(name) == 0)
  1211. lastWidget = widget;
  1212. }
  1213. void OBSPropertiesView::SignalChanged()
  1214. {
  1215. emit Changed();
  1216. }
  1217. static bool FrameRateChangedVariant(const QVariant &variant,
  1218. media_frames_per_second &fps,
  1219. obs_data_item_t *&obj,
  1220. const media_frames_per_second *valid_fps)
  1221. {
  1222. if (!variant.canConvert<media_frames_per_second>())
  1223. return false;
  1224. fps = variant.value<media_frames_per_second>();
  1225. if (valid_fps && fps == *valid_fps)
  1226. return false;
  1227. obs_data_item_set_frames_per_second(&obj, fps, nullptr);
  1228. return true;
  1229. }
  1230. static bool FrameRateChangedCommon(OBSFrameRatePropertyWidget *w,
  1231. obs_data_item_t *&obj,
  1232. const media_frames_per_second *valid_fps)
  1233. {
  1234. media_frames_per_second fps{};
  1235. if (!FrameRateChangedVariant(w->simpleFPS->currentData(), fps, obj,
  1236. valid_fps))
  1237. return false;
  1238. UpdateRationalFPSWidgets(w, &fps);
  1239. return true;
  1240. }
  1241. static bool FrameRateChangedRational(OBSFrameRatePropertyWidget *w,
  1242. obs_data_item_t *&obj,
  1243. const media_frames_per_second *valid_fps)
  1244. {
  1245. auto num = w->numEdit->value();
  1246. auto den = w->denEdit->value();
  1247. auto fps = make_fps(num, den);
  1248. if (valid_fps && media_frames_per_second_is_valid(fps) &&
  1249. fps == *valid_fps)
  1250. return false;
  1251. obs_data_item_set_frames_per_second(&obj, fps, nullptr);
  1252. UpdateSimpleFPSSelection(w, &fps);
  1253. return true;
  1254. }
  1255. static bool FrameRateChanged(QWidget *widget, const char *name,
  1256. OBSData &settings)
  1257. {
  1258. auto w = qobject_cast<OBSFrameRatePropertyWidget *>(widget);
  1259. if (!w)
  1260. return false;
  1261. auto variant = w->modeSelect->currentData();
  1262. if (!variant.canConvert<frame_rate_tag>())
  1263. return false;
  1264. auto StopUpdating = [&](void *) { w->updating = false; };
  1265. unique_ptr<void, decltype(StopUpdating)> signalGuard(
  1266. static_cast<void *>(w), StopUpdating);
  1267. w->updating = true;
  1268. if (!obs_data_has_user_value(settings, name))
  1269. obs_data_set_obj(settings, name, nullptr);
  1270. unique_ptr<obs_data_item_t> obj{obs_data_item_byname(settings, name)};
  1271. auto obj_ptr = obj.get();
  1272. auto CheckObj = [&]() {
  1273. if (!obj_ptr)
  1274. obj.release();
  1275. };
  1276. const char *option = nullptr;
  1277. obs_data_item_get_frames_per_second(obj.get(), nullptr, &option);
  1278. media_frames_per_second fps{};
  1279. media_frames_per_second *valid_fps = nullptr;
  1280. if (obs_data_item_get_frames_per_second(obj.get(), &fps, nullptr))
  1281. valid_fps = &fps;
  1282. auto tag = variant.value<frame_rate_tag>();
  1283. switch (tag.type) {
  1284. case frame_rate_tag::SIMPLE:
  1285. if (!FrameRateChangedCommon(w, obj_ptr, valid_fps))
  1286. return false;
  1287. break;
  1288. case frame_rate_tag::RATIONAL:
  1289. if (!FrameRateChangedRational(w, obj_ptr, valid_fps))
  1290. return false;
  1291. break;
  1292. case frame_rate_tag::USER:
  1293. if (tag.val && option && strcmp(tag.val, option) == 0)
  1294. return false;
  1295. obs_data_item_set_frames_per_second(&obj_ptr, {}, tag.val);
  1296. break;
  1297. }
  1298. UpdateFPSLabels(w);
  1299. CheckObj();
  1300. return true;
  1301. }
  1302. void WidgetInfo::BoolChanged(const char *setting)
  1303. {
  1304. QCheckBox *checkbox = static_cast<QCheckBox *>(widget);
  1305. obs_data_set_bool(view->settings, setting,
  1306. checkbox->checkState() == Qt::Checked);
  1307. }
  1308. void WidgetInfo::IntChanged(const char *setting)
  1309. {
  1310. QSpinBox *spin = static_cast<QSpinBox *>(widget);
  1311. obs_data_set_int(view->settings, setting, spin->value());
  1312. }
  1313. void WidgetInfo::FloatChanged(const char *setting)
  1314. {
  1315. QDoubleSpinBox *spin = static_cast<QDoubleSpinBox *>(widget);
  1316. obs_data_set_double(view->settings, setting, spin->value());
  1317. }
  1318. void WidgetInfo::TextChanged(const char *setting)
  1319. {
  1320. obs_text_type type = obs_property_text_type(property);
  1321. if (type == OBS_TEXT_MULTILINE) {
  1322. QPlainTextEdit *edit = static_cast<QPlainTextEdit *>(widget);
  1323. obs_data_set_string(view->settings, setting,
  1324. QT_TO_UTF8(edit->toPlainText()));
  1325. return;
  1326. }
  1327. QLineEdit *edit = static_cast<QLineEdit *>(widget);
  1328. obs_data_set_string(view->settings, setting, QT_TO_UTF8(edit->text()));
  1329. }
  1330. bool WidgetInfo::PathChanged(const char *setting)
  1331. {
  1332. const char *desc = obs_property_description(property);
  1333. obs_path_type type = obs_property_path_type(property);
  1334. const char *filter = obs_property_path_filter(property);
  1335. const char *default_path = obs_property_path_default_path(property);
  1336. QString path;
  1337. if (type == OBS_PATH_DIRECTORY)
  1338. path = SelectDirectory(view, QT_UTF8(desc),
  1339. QT_UTF8(default_path));
  1340. else if (type == OBS_PATH_FILE)
  1341. path = OpenFile(view, QT_UTF8(desc), QT_UTF8(default_path),
  1342. QT_UTF8(filter));
  1343. else if (type == OBS_PATH_FILE_SAVE)
  1344. path = SaveFile(view, QT_UTF8(desc), QT_UTF8(default_path),
  1345. QT_UTF8(filter));
  1346. if (path.isEmpty())
  1347. return false;
  1348. QLineEdit *edit = static_cast<QLineEdit *>(widget);
  1349. edit->setText(path);
  1350. obs_data_set_string(view->settings, setting, QT_TO_UTF8(path));
  1351. return true;
  1352. }
  1353. void WidgetInfo::ListChanged(const char *setting)
  1354. {
  1355. QComboBox *combo = static_cast<QComboBox *>(widget);
  1356. obs_combo_format format = obs_property_list_format(property);
  1357. obs_combo_type type = obs_property_list_type(property);
  1358. QVariant data;
  1359. if (type == OBS_COMBO_TYPE_EDITABLE) {
  1360. data = combo->currentText().toUtf8();
  1361. } else {
  1362. int index = combo->currentIndex();
  1363. if (index != -1)
  1364. data = combo->itemData(index);
  1365. else
  1366. return;
  1367. }
  1368. switch (format) {
  1369. case OBS_COMBO_FORMAT_INVALID:
  1370. return;
  1371. case OBS_COMBO_FORMAT_INT:
  1372. obs_data_set_int(view->settings, setting,
  1373. data.value<long long>());
  1374. break;
  1375. case OBS_COMBO_FORMAT_FLOAT:
  1376. obs_data_set_double(view->settings, setting,
  1377. data.value<double>());
  1378. break;
  1379. case OBS_COMBO_FORMAT_STRING:
  1380. obs_data_set_string(view->settings, setting,
  1381. data.toByteArray().constData());
  1382. break;
  1383. }
  1384. }
  1385. bool WidgetInfo::ColorChangedInternal(const char *setting, bool supportAlpha)
  1386. {
  1387. const char *desc = obs_property_description(property);
  1388. long long val = obs_data_get_int(view->settings, setting);
  1389. QColor color = color_from_int(val);
  1390. QColor::NameFormat format;
  1391. QColorDialog::ColorDialogOptions options;
  1392. if (supportAlpha) {
  1393. options |= QColorDialog::ShowAlphaChannel;
  1394. }
  1395. /* The native dialog on OSX has all kinds of problems, like closing
  1396. * other open QDialogs on exit, and
  1397. * https://bugreports.qt-project.org/browse/QTBUG-34532
  1398. */
  1399. #ifndef _WIN32
  1400. options |= QColorDialog::DontUseNativeDialog;
  1401. #endif
  1402. color = QColorDialog::getColor(color, view, QT_UTF8(desc), options);
  1403. if (!color.isValid())
  1404. return false;
  1405. if (supportAlpha) {
  1406. format = QColor::HexArgb;
  1407. } else {
  1408. color.setAlpha(255);
  1409. format = QColor::HexRgb;
  1410. }
  1411. QLabel *label = static_cast<QLabel *>(widget);
  1412. label->setText(color.name(format));
  1413. QPalette palette = QPalette(color);
  1414. label->setPalette(palette);
  1415. label->setStyleSheet(
  1416. QString("background-color :%1; color: %2;")
  1417. .arg(palette.color(QPalette::Window).name(format))
  1418. .arg(palette.color(QPalette::WindowText).name(format)));
  1419. obs_data_set_int(view->settings, setting, color_to_int(color));
  1420. return true;
  1421. }
  1422. bool WidgetInfo::ColorChanged(const char *setting)
  1423. {
  1424. return ColorChangedInternal(setting, false);
  1425. }
  1426. bool WidgetInfo::ColorAlphaChanged(const char *setting)
  1427. {
  1428. return ColorChangedInternal(setting, true);
  1429. }
  1430. bool WidgetInfo::FontChanged(const char *setting)
  1431. {
  1432. obs_data_t *font_obj = obs_data_get_obj(view->settings, setting);
  1433. bool success;
  1434. uint32_t flags;
  1435. QFont font;
  1436. QFontDialog::FontDialogOptions options;
  1437. #ifndef _WIN32
  1438. options = QFontDialog::DontUseNativeDialog;
  1439. #endif
  1440. if (!font_obj) {
  1441. QFont initial;
  1442. font = QFontDialog::getFont(&success, initial, view,
  1443. "Pick a Font", options);
  1444. } else {
  1445. MakeQFont(font_obj, font);
  1446. font = QFontDialog::getFont(&success, font, view, "Pick a Font",
  1447. options);
  1448. obs_data_release(font_obj);
  1449. }
  1450. if (!success)
  1451. return false;
  1452. font_obj = obs_data_create();
  1453. obs_data_set_string(font_obj, "face", QT_TO_UTF8(font.family()));
  1454. obs_data_set_string(font_obj, "style", QT_TO_UTF8(font.styleName()));
  1455. obs_data_set_int(font_obj, "size", font.pointSize());
  1456. flags = font.bold() ? OBS_FONT_BOLD : 0;
  1457. flags |= font.italic() ? OBS_FONT_ITALIC : 0;
  1458. flags |= font.underline() ? OBS_FONT_UNDERLINE : 0;
  1459. flags |= font.strikeOut() ? OBS_FONT_STRIKEOUT : 0;
  1460. obs_data_set_int(font_obj, "flags", flags);
  1461. QLabel *label = static_cast<QLabel *>(widget);
  1462. QFont labelFont;
  1463. MakeQFont(font_obj, labelFont, true);
  1464. label->setFont(labelFont);
  1465. label->setText(QString("%1 %2").arg(font.family(), font.styleName()));
  1466. obs_data_set_obj(view->settings, setting, font_obj);
  1467. obs_data_release(font_obj);
  1468. return true;
  1469. }
  1470. void WidgetInfo::GroupChanged(const char *setting)
  1471. {
  1472. QGroupBox *groupbox = static_cast<QGroupBox *>(widget);
  1473. obs_data_set_bool(view->settings, setting,
  1474. groupbox->isCheckable() ? groupbox->isChecked()
  1475. : true);
  1476. }
  1477. void WidgetInfo::EditListReordered(const QModelIndex &parent, int start,
  1478. int end, const QModelIndex &destination,
  1479. int row)
  1480. {
  1481. UNUSED_PARAMETER(parent);
  1482. UNUSED_PARAMETER(start);
  1483. UNUSED_PARAMETER(end);
  1484. UNUSED_PARAMETER(destination);
  1485. UNUSED_PARAMETER(row);
  1486. EditableListChanged();
  1487. }
  1488. void WidgetInfo::EditableListChanged()
  1489. {
  1490. const char *setting = obs_property_name(property);
  1491. QListWidget *list = reinterpret_cast<QListWidget *>(widget);
  1492. obs_data_array *array = obs_data_array_create();
  1493. for (int i = 0; i < list->count(); i++) {
  1494. QListWidgetItem *item = list->item(i);
  1495. obs_data_t *arrayItem = obs_data_create();
  1496. obs_data_set_string(arrayItem, "value",
  1497. QT_TO_UTF8(item->text()));
  1498. obs_data_set_bool(arrayItem, "selected", item->isSelected());
  1499. obs_data_set_bool(arrayItem, "hidden", item->isHidden());
  1500. obs_data_array_push_back(array, arrayItem);
  1501. obs_data_release(arrayItem);
  1502. }
  1503. obs_data_set_array(view->settings, setting, array);
  1504. obs_data_array_release(array);
  1505. ControlChanged();
  1506. }
  1507. void WidgetInfo::ButtonClicked()
  1508. {
  1509. if (obs_property_button_clicked(property, view->obj)) {
  1510. QMetaObject::invokeMethod(view, "RefreshProperties",
  1511. Qt::QueuedConnection);
  1512. }
  1513. }
  1514. void WidgetInfo::TogglePasswordText(bool show)
  1515. {
  1516. reinterpret_cast<QLineEdit *>(widget)->setEchoMode(
  1517. show ? QLineEdit::Normal : QLineEdit::Password);
  1518. }
  1519. void WidgetInfo::ControlChanged()
  1520. {
  1521. const char *setting = obs_property_name(property);
  1522. obs_property_type type = obs_property_get_type(property);
  1523. if (!recently_updated) {
  1524. old_settings_cache = obs_data_create();
  1525. obs_data_apply(old_settings_cache, view->settings);
  1526. obs_data_release(old_settings_cache);
  1527. }
  1528. switch (type) {
  1529. case OBS_PROPERTY_INVALID:
  1530. return;
  1531. case OBS_PROPERTY_BOOL:
  1532. BoolChanged(setting);
  1533. break;
  1534. case OBS_PROPERTY_INT:
  1535. IntChanged(setting);
  1536. break;
  1537. case OBS_PROPERTY_FLOAT:
  1538. FloatChanged(setting);
  1539. break;
  1540. case OBS_PROPERTY_TEXT:
  1541. TextChanged(setting);
  1542. break;
  1543. case OBS_PROPERTY_LIST:
  1544. ListChanged(setting);
  1545. break;
  1546. case OBS_PROPERTY_BUTTON:
  1547. ButtonClicked();
  1548. return;
  1549. case OBS_PROPERTY_COLOR:
  1550. if (!ColorChanged(setting))
  1551. return;
  1552. break;
  1553. case OBS_PROPERTY_FONT:
  1554. if (!FontChanged(setting))
  1555. return;
  1556. break;
  1557. case OBS_PROPERTY_PATH:
  1558. if (!PathChanged(setting))
  1559. return;
  1560. break;
  1561. case OBS_PROPERTY_EDITABLE_LIST:
  1562. break;
  1563. case OBS_PROPERTY_FRAME_RATE:
  1564. if (!FrameRateChanged(widget, setting, view->settings))
  1565. return;
  1566. break;
  1567. case OBS_PROPERTY_GROUP:
  1568. GroupChanged(setting);
  1569. break;
  1570. case OBS_PROPERTY_COLOR_ALPHA:
  1571. if (!ColorAlphaChanged(setting))
  1572. return;
  1573. break;
  1574. }
  1575. if (!recently_updated) {
  1576. recently_updated = true;
  1577. update_timer = new QTimer;
  1578. connect(update_timer, &QTimer::timeout,
  1579. [this, &ru = recently_updated]() {
  1580. if (view->callback && !view->deferUpdate) {
  1581. view->callback(view->obj,
  1582. old_settings_cache,
  1583. view->settings);
  1584. }
  1585. ru = false;
  1586. });
  1587. connect(update_timer, &QTimer::timeout, &QTimer::deleteLater);
  1588. update_timer->setSingleShot(true);
  1589. }
  1590. if (update_timer) {
  1591. update_timer->stop();
  1592. update_timer->start(500);
  1593. } else {
  1594. blog(LOG_DEBUG, "No update timer or no callback!");
  1595. }
  1596. if (view->cb && !view->deferUpdate)
  1597. view->cb(view->obj, view->settings);
  1598. view->SignalChanged();
  1599. if (obs_property_modified(property, view->settings)) {
  1600. view->lastFocused = setting;
  1601. QMetaObject::invokeMethod(view, "RefreshProperties",
  1602. Qt::QueuedConnection);
  1603. }
  1604. }
  1605. class EditableItemDialog : public QDialog {
  1606. QLineEdit *edit;
  1607. QString filter;
  1608. QString default_path;
  1609. void BrowseClicked()
  1610. {
  1611. QString curPath = QFileInfo(edit->text()).absoluteDir().path();
  1612. if (curPath.isEmpty())
  1613. curPath = default_path;
  1614. QString path = OpenFile(App()->GetMainWindow(), QTStr("Browse"),
  1615. curPath, filter);
  1616. if (path.isEmpty())
  1617. return;
  1618. edit->setText(path);
  1619. }
  1620. public:
  1621. EditableItemDialog(QWidget *parent, const QString &text, bool browse,
  1622. const char *filter_ = nullptr,
  1623. const char *default_path_ = nullptr)
  1624. : QDialog(parent),
  1625. filter(QT_UTF8(filter_)),
  1626. default_path(QT_UTF8(default_path_))
  1627. {
  1628. QHBoxLayout *topLayout = new QHBoxLayout();
  1629. QVBoxLayout *mainLayout = new QVBoxLayout();
  1630. edit = new QLineEdit();
  1631. edit->setText(text);
  1632. topLayout->addWidget(edit);
  1633. topLayout->setAlignment(edit, Qt::AlignVCenter);
  1634. if (browse) {
  1635. QPushButton *browseButton =
  1636. new QPushButton(QTStr("Browse"));
  1637. browseButton->setProperty("themeID", "settingsButtons");
  1638. topLayout->addWidget(browseButton);
  1639. topLayout->setAlignment(browseButton, Qt::AlignVCenter);
  1640. connect(browseButton, &QPushButton::clicked, this,
  1641. &EditableItemDialog::BrowseClicked);
  1642. }
  1643. QDialogButtonBox::StandardButtons buttons =
  1644. QDialogButtonBox::Ok | QDialogButtonBox::Cancel;
  1645. QDialogButtonBox *buttonBox = new QDialogButtonBox(buttons);
  1646. buttonBox->setCenterButtons(true);
  1647. mainLayout->addLayout(topLayout);
  1648. mainLayout->addWidget(buttonBox);
  1649. setLayout(mainLayout);
  1650. resize(QSize(400, 80));
  1651. connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
  1652. connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
  1653. }
  1654. inline QString GetText() const { return edit->text(); }
  1655. };
  1656. void WidgetInfo::EditListAdd()
  1657. {
  1658. enum obs_editable_list_type type =
  1659. obs_property_editable_list_type(property);
  1660. if (type == OBS_EDITABLE_LIST_TYPE_STRINGS) {
  1661. EditListAddText();
  1662. return;
  1663. }
  1664. /* Files and URLs */
  1665. QMenu popup(view->window());
  1666. QAction *action;
  1667. action = new QAction(QTStr("Basic.PropertiesWindow.AddFiles"), this);
  1668. connect(action, &QAction::triggered, this,
  1669. &WidgetInfo::EditListAddFiles);
  1670. popup.addAction(action);
  1671. action = new QAction(QTStr("Basic.PropertiesWindow.AddDir"), this);
  1672. connect(action, &QAction::triggered, this, &WidgetInfo::EditListAddDir);
  1673. popup.addAction(action);
  1674. if (type == OBS_EDITABLE_LIST_TYPE_FILES_AND_URLS) {
  1675. action = new QAction(QTStr("Basic.PropertiesWindow.AddURL"),
  1676. this);
  1677. connect(action, &QAction::triggered, this,
  1678. &WidgetInfo::EditListAddText);
  1679. popup.addAction(action);
  1680. }
  1681. popup.exec(QCursor::pos());
  1682. }
  1683. void WidgetInfo::EditListAddText()
  1684. {
  1685. QListWidget *list = reinterpret_cast<QListWidget *>(widget);
  1686. const char *desc = obs_property_description(property);
  1687. EditableItemDialog dialog(widget->window(), QString(), false);
  1688. auto title = QTStr("Basic.PropertiesWindow.AddEditableListEntry")
  1689. .arg(QT_UTF8(desc));
  1690. dialog.setWindowTitle(title);
  1691. if (dialog.exec() == QDialog::Rejected)
  1692. return;
  1693. QString text = dialog.GetText();
  1694. if (text.isEmpty())
  1695. return;
  1696. list->addItem(text);
  1697. EditableListChanged();
  1698. }
  1699. void WidgetInfo::EditListAddFiles()
  1700. {
  1701. QListWidget *list = reinterpret_cast<QListWidget *>(widget);
  1702. const char *desc = obs_property_description(property);
  1703. const char *filter = obs_property_editable_list_filter(property);
  1704. const char *default_path =
  1705. obs_property_editable_list_default_path(property);
  1706. QString title = QTStr("Basic.PropertiesWindow.AddEditableListFiles")
  1707. .arg(QT_UTF8(desc));
  1708. QStringList files = OpenFiles(App()->GetMainWindow(), title,
  1709. QT_UTF8(default_path), QT_UTF8(filter));
  1710. if (files.count() == 0)
  1711. return;
  1712. list->addItems(files);
  1713. EditableListChanged();
  1714. }
  1715. void WidgetInfo::EditListAddDir()
  1716. {
  1717. QListWidget *list = reinterpret_cast<QListWidget *>(widget);
  1718. const char *desc = obs_property_description(property);
  1719. const char *default_path =
  1720. obs_property_editable_list_default_path(property);
  1721. QString title = QTStr("Basic.PropertiesWindow.AddEditableListDir")
  1722. .arg(QT_UTF8(desc));
  1723. QString dir = SelectDirectory(App()->GetMainWindow(), title,
  1724. QT_UTF8(default_path));
  1725. if (dir.isEmpty())
  1726. return;
  1727. list->addItem(dir);
  1728. EditableListChanged();
  1729. }
  1730. void WidgetInfo::EditListRemove()
  1731. {
  1732. QListWidget *list = reinterpret_cast<QListWidget *>(widget);
  1733. QList<QListWidgetItem *> items = list->selectedItems();
  1734. for (QListWidgetItem *item : items)
  1735. delete item;
  1736. EditableListChanged();
  1737. }
  1738. void WidgetInfo::EditListEdit()
  1739. {
  1740. QListWidget *list = reinterpret_cast<QListWidget *>(widget);
  1741. enum obs_editable_list_type type =
  1742. obs_property_editable_list_type(property);
  1743. const char *desc = obs_property_description(property);
  1744. const char *filter = obs_property_editable_list_filter(property);
  1745. QList<QListWidgetItem *> selectedItems = list->selectedItems();
  1746. if (!selectedItems.count())
  1747. return;
  1748. QListWidgetItem *item = selectedItems[0];
  1749. if (type == OBS_EDITABLE_LIST_TYPE_FILES) {
  1750. QDir pathDir(item->text());
  1751. QString path;
  1752. if (pathDir.exists())
  1753. path = SelectDirectory(App()->GetMainWindow(),
  1754. QTStr("Browse"), item->text());
  1755. else
  1756. path = OpenFile(App()->GetMainWindow(), QTStr("Browse"),
  1757. item->text(), QT_UTF8(filter));
  1758. if (path.isEmpty())
  1759. return;
  1760. item->setText(path);
  1761. EditableListChanged();
  1762. return;
  1763. }
  1764. EditableItemDialog dialog(widget->window(), item->text(),
  1765. type != OBS_EDITABLE_LIST_TYPE_STRINGS,
  1766. filter);
  1767. auto title = QTStr("Basic.PropertiesWindow.EditEditableListEntry")
  1768. .arg(QT_UTF8(desc));
  1769. dialog.setWindowTitle(title);
  1770. if (dialog.exec() == QDialog::Rejected)
  1771. return;
  1772. QString text = dialog.GetText();
  1773. if (text.isEmpty())
  1774. return;
  1775. item->setText(text);
  1776. EditableListChanged();
  1777. }
  1778. void WidgetInfo::EditListUp()
  1779. {
  1780. QListWidget *list = reinterpret_cast<QListWidget *>(widget);
  1781. int lastItemRow = -1;
  1782. for (int i = 0; i < list->count(); i++) {
  1783. QListWidgetItem *item = list->item(i);
  1784. if (!item->isSelected())
  1785. continue;
  1786. int row = list->row(item);
  1787. if ((row - 1) != lastItemRow) {
  1788. lastItemRow = row - 1;
  1789. list->takeItem(row);
  1790. list->insertItem(lastItemRow, item);
  1791. item->setSelected(true);
  1792. } else {
  1793. lastItemRow = row;
  1794. }
  1795. }
  1796. EditableListChanged();
  1797. }
  1798. void WidgetInfo::EditListDown()
  1799. {
  1800. QListWidget *list = reinterpret_cast<QListWidget *>(widget);
  1801. int lastItemRow = list->count();
  1802. for (int i = list->count() - 1; i >= 0; i--) {
  1803. QListWidgetItem *item = list->item(i);
  1804. if (!item->isSelected())
  1805. continue;
  1806. int row = list->row(item);
  1807. if ((row + 1) != lastItemRow) {
  1808. lastItemRow = row + 1;
  1809. list->takeItem(row);
  1810. list->insertItem(lastItemRow, item);
  1811. item->setSelected(true);
  1812. } else {
  1813. lastItemRow = row;
  1814. }
  1815. }
  1816. EditableListChanged();
  1817. }