properties-view.cpp 52 KB

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