properties-view.cpp 59 KB

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