properties-view.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859
  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 <QPushButton>
  13. #include <QStandardItem>
  14. #include <QFileDialog>
  15. #include <QColorDialog>
  16. #include <QPlainTextEdit>
  17. #include "double-slider.hpp"
  18. #include "qt-wrappers.hpp"
  19. #include "properties-view.hpp"
  20. #include "obs-app.hpp"
  21. #include <string>
  22. using namespace std;
  23. static inline QColor color_from_int(long long val)
  24. {
  25. return QColor( val & 0xff,
  26. (val >> 8) & 0xff,
  27. (val >> 16) & 0xff,
  28. (val >> 24) & 0xff);
  29. }
  30. static inline long long color_to_int(QColor color)
  31. {
  32. auto shift = [&](unsigned val, int shift)
  33. {
  34. return ((val & 0xff) << shift);
  35. };
  36. return shift(color.red(), 0) |
  37. shift(color.green(), 8) |
  38. shift(color.blue(), 16) |
  39. shift(color.alpha(), 24);
  40. }
  41. void OBSPropertiesView::ReloadProperties()
  42. {
  43. if (obj) {
  44. properties.reset(reloadCallback(obj));
  45. } else {
  46. properties.reset(reloadCallback((void*)type.c_str()));
  47. obs_properties_apply_settings(properties.get(), settings);
  48. }
  49. uint32_t flags = obs_properties_get_flags(properties.get());
  50. deferUpdate = (flags & OBS_PROPERTIES_DEFER_UPDATE) != 0;
  51. RefreshProperties();
  52. }
  53. #define NO_PROPERTIES_STRING QTStr("Basic.PropertiesWindow.NoProperties")
  54. void OBSPropertiesView::RefreshProperties()
  55. {
  56. int h, v;
  57. GetScrollPos(h, v);
  58. children.clear();
  59. if (widget)
  60. widget->deleteLater();
  61. widget = new QWidget();
  62. QFormLayout *layout = new QFormLayout;
  63. layout->setFieldGrowthPolicy(QFormLayout::AllNonFixedFieldsGrow);
  64. widget->setLayout(layout);
  65. QSizePolicy mainPolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
  66. QSizePolicy policy(QSizePolicy::Preferred, QSizePolicy::Preferred);
  67. //widget->setSizePolicy(policy);
  68. layout->setLabelAlignment(Qt::AlignRight);
  69. obs_property_t *property = obs_properties_first(properties.get());
  70. bool hasNoProperties = !property;
  71. while (property) {
  72. AddProperty(property, layout);
  73. obs_property_next(&property);
  74. }
  75. setWidgetResizable(true);
  76. setWidget(widget);
  77. SetScrollPos(h, v);
  78. setSizePolicy(mainPolicy);
  79. lastFocused.clear();
  80. if (lastWidget) {
  81. lastWidget->setFocus(Qt::OtherFocusReason);
  82. lastWidget = nullptr;
  83. }
  84. if (hasNoProperties) {
  85. QLabel *noPropertiesLabel = new QLabel(NO_PROPERTIES_STRING);
  86. layout->addWidget(noPropertiesLabel);
  87. }
  88. }
  89. void OBSPropertiesView::SetScrollPos(int h, int v)
  90. {
  91. QScrollBar *scroll = horizontalScrollBar();
  92. if (scroll)
  93. scroll->setValue(h);
  94. scroll = verticalScrollBar();
  95. if (scroll)
  96. scroll->setValue(v);
  97. }
  98. void OBSPropertiesView::GetScrollPos(int &h, int &v)
  99. {
  100. h = v = 0;
  101. QScrollBar *scroll = horizontalScrollBar();
  102. if (scroll)
  103. h = scroll->value();
  104. scroll = verticalScrollBar();
  105. if (scroll)
  106. v = scroll->value();
  107. }
  108. OBSPropertiesView::OBSPropertiesView(OBSData settings_, void *obj_,
  109. PropertiesReloadCallback reloadCallback,
  110. PropertiesUpdateCallback callback_, int minSize_)
  111. : VScrollArea (nullptr),
  112. properties (nullptr, obs_properties_destroy),
  113. settings (settings_),
  114. obj (obj_),
  115. reloadCallback (reloadCallback),
  116. callback (callback_),
  117. minSize (minSize_)
  118. {
  119. setFrameShape(QFrame::NoFrame);
  120. ReloadProperties();
  121. }
  122. OBSPropertiesView::OBSPropertiesView(OBSData settings_, const char *type_,
  123. PropertiesReloadCallback reloadCallback_, int minSize_)
  124. : VScrollArea (nullptr),
  125. properties (nullptr, obs_properties_destroy),
  126. settings (settings_),
  127. type (type_),
  128. reloadCallback (reloadCallback_),
  129. minSize (minSize_)
  130. {
  131. setFrameShape(QFrame::NoFrame);
  132. ReloadProperties();
  133. }
  134. void OBSPropertiesView::resizeEvent(QResizeEvent *event)
  135. {
  136. emit PropertiesResized();
  137. VScrollArea::resizeEvent(event);
  138. }
  139. QWidget *OBSPropertiesView::NewWidget(obs_property_t *prop, QWidget *widget,
  140. const char *signal)
  141. {
  142. WidgetInfo *info = new WidgetInfo(this, prop, widget);
  143. connect(widget, signal, info, SLOT(ControlChanged()));
  144. children.emplace_back(info);
  145. return widget;
  146. }
  147. QWidget *OBSPropertiesView::AddCheckbox(obs_property_t *prop)
  148. {
  149. const char *name = obs_property_name(prop);
  150. const char *desc = obs_property_description(prop);
  151. bool val = obs_data_get_bool(settings, name);
  152. QCheckBox *checkbox = new QCheckBox(QT_UTF8(desc));
  153. checkbox->setCheckState(val ? Qt::Checked : Qt::Unchecked);
  154. return NewWidget(prop, checkbox, SIGNAL(stateChanged(int)));
  155. }
  156. QWidget *OBSPropertiesView::AddText(obs_property_t *prop, QFormLayout *layout,
  157. QLabel *&label)
  158. {
  159. const char *name = obs_property_name(prop);
  160. const char *val = obs_data_get_string(settings, name);
  161. obs_text_type type = obs_proprety_text_type(prop);
  162. if (type == OBS_TEXT_MULTILINE) {
  163. QPlainTextEdit *edit = new QPlainTextEdit(QT_UTF8(val));
  164. return NewWidget(prop, edit, SIGNAL(textChanged()));
  165. } else if (type == OBS_TEXT_PASSWORD) {
  166. QLayout *subLayout = new QHBoxLayout();
  167. QLineEdit *edit = new QLineEdit();
  168. QPushButton *show = new QPushButton();
  169. show->setText(QTStr("Show"));
  170. show->setCheckable(true);
  171. edit->setText(QT_UTF8(val));
  172. edit->setEchoMode(QLineEdit::Password);
  173. subLayout->addWidget(edit);
  174. subLayout->addWidget(show);
  175. WidgetInfo *info = new WidgetInfo(this, prop, edit);
  176. connect(show, &QAbstractButton::toggled,
  177. info, &WidgetInfo::TogglePasswordText);
  178. children.emplace_back(info);
  179. label = new QLabel(QT_UTF8(obs_property_description(prop)));
  180. layout->addRow(label, subLayout);
  181. return nullptr;
  182. }
  183. QLineEdit *edit = new QLineEdit();
  184. edit->setText(QT_UTF8(val));
  185. return NewWidget(prop, edit, SIGNAL(textEdited(const QString &)));
  186. }
  187. void OBSPropertiesView::AddPath(obs_property_t *prop, QFormLayout *layout,
  188. QLabel **label)
  189. {
  190. const char *name = obs_property_name(prop);
  191. const char *val = obs_data_get_string(settings, name);
  192. QLayout *subLayout = new QHBoxLayout();
  193. QLineEdit *edit = new QLineEdit();
  194. QPushButton *button = new QPushButton(QTStr("Browse"));
  195. edit->setText(QT_UTF8(val));
  196. edit->setReadOnly(true);
  197. subLayout->addWidget(edit);
  198. subLayout->addWidget(button);
  199. WidgetInfo *info = new WidgetInfo(this, prop, edit);
  200. connect(button, SIGNAL(clicked()), info, SLOT(ControlChanged()));
  201. children.emplace_back(info);
  202. *label = new QLabel(QT_UTF8(obs_property_description(prop)));
  203. layout->addRow(*label, subLayout);
  204. }
  205. void OBSPropertiesView::AddInt(obs_property_t *prop, QFormLayout *layout,
  206. QLabel **label)
  207. {
  208. obs_number_type type = obs_property_int_type(prop);
  209. QLayout *subLayout = new QHBoxLayout();
  210. const char *name = obs_property_name(prop);
  211. int val = (int)obs_data_get_int(settings, name);
  212. QSpinBox *spin = new QSpinBox();
  213. int minVal = obs_property_int_min(prop);
  214. int maxVal = obs_property_int_max(prop);
  215. int stepVal = obs_property_int_step(prop);
  216. spin->setMinimum(minVal);
  217. spin->setMaximum(maxVal);
  218. spin->setSingleStep(stepVal);
  219. spin->setValue(val);
  220. WidgetInfo *info = new WidgetInfo(this, prop, spin);
  221. children.emplace_back(info);
  222. if (type == OBS_NUMBER_SLIDER) {
  223. QSlider *slider = new QSlider();
  224. slider->setMinimum(minVal);
  225. slider->setMaximum(maxVal);
  226. slider->setPageStep(stepVal);
  227. slider->setValue(val);
  228. slider->setOrientation(Qt::Horizontal);
  229. subLayout->addWidget(slider);
  230. connect(slider, SIGNAL(valueChanged(int)),
  231. spin, SLOT(setValue(int)));
  232. connect(spin, SIGNAL(valueChanged(int)),
  233. slider, SLOT(setValue(int)));
  234. }
  235. connect(spin, SIGNAL(valueChanged(int)), info, SLOT(ControlChanged()));
  236. subLayout->addWidget(spin);
  237. *label = new QLabel(QT_UTF8(obs_property_description(prop)));
  238. layout->addRow(*label, subLayout);
  239. }
  240. void OBSPropertiesView::AddFloat(obs_property_t *prop, QFormLayout *layout,
  241. QLabel **label)
  242. {
  243. obs_number_type type = obs_property_float_type(prop);
  244. QLayout *subLayout = new QHBoxLayout();
  245. const char *name = obs_property_name(prop);
  246. double val = obs_data_get_double(settings, name);
  247. QDoubleSpinBox *spin = new QDoubleSpinBox();
  248. double minVal = obs_property_float_min(prop);
  249. double maxVal = obs_property_float_max(prop);
  250. double stepVal = obs_property_float_step(prop);
  251. spin->setMinimum(minVal);
  252. spin->setMaximum(maxVal);
  253. spin->setSingleStep(stepVal);
  254. spin->setValue(val);
  255. WidgetInfo *info = new WidgetInfo(this, prop, spin);
  256. children.emplace_back(info);
  257. if (type == OBS_NUMBER_SLIDER) {
  258. DoubleSlider *slider = new DoubleSlider();
  259. slider->setDoubleConstraints(minVal, maxVal, stepVal, val);
  260. slider->setOrientation(Qt::Horizontal);
  261. subLayout->addWidget(slider);
  262. connect(slider, SIGNAL(doubleValChanged(double)),
  263. spin, SLOT(setValue(double)));
  264. connect(spin, SIGNAL(valueChanged(double)),
  265. slider, SLOT(setDoubleVal(double)));
  266. }
  267. connect(spin, SIGNAL(valueChanged(double)), info,
  268. SLOT(ControlChanged()));
  269. subLayout->addWidget(spin);
  270. *label = new QLabel(QT_UTF8(obs_property_description(prop)));
  271. layout->addRow(*label, subLayout);
  272. }
  273. static void AddComboItem(QComboBox *combo, obs_property_t *prop,
  274. obs_combo_format format, size_t idx)
  275. {
  276. const char *name = obs_property_list_item_name(prop, idx);
  277. QVariant var;
  278. if (format == OBS_COMBO_FORMAT_INT) {
  279. long long val = obs_property_list_item_int(prop, idx);
  280. var = QVariant::fromValue<long long>(val);
  281. } else if (format == OBS_COMBO_FORMAT_FLOAT) {
  282. double val = obs_property_list_item_float(prop, idx);
  283. var = QVariant::fromValue<double>(val);
  284. } else if (format == OBS_COMBO_FORMAT_STRING) {
  285. var = obs_property_list_item_string(prop, idx);
  286. }
  287. combo->addItem(QT_UTF8(name), var);
  288. if (!obs_property_list_item_disabled(prop, idx))
  289. return;
  290. int index = combo->findText(QT_UTF8(name));
  291. if (index < 0)
  292. return;
  293. QStandardItemModel *model =
  294. dynamic_cast<QStandardItemModel*>(combo->model());
  295. if (!model)
  296. return;
  297. QStandardItem *item = model->item(index);
  298. item->setFlags(Qt::NoItemFlags);
  299. }
  300. template <long long get_int(obs_data_t*, const char*),
  301. double get_double(obs_data_t*, const char*),
  302. const char *get_string(obs_data_t*, const char*)>
  303. static string from_obs_data(obs_data_t *data, const char *name,
  304. obs_combo_format format)
  305. {
  306. switch (format) {
  307. case OBS_COMBO_FORMAT_INT:
  308. return to_string(get_int(data, name));
  309. case OBS_COMBO_FORMAT_FLOAT:
  310. return to_string(get_double(data, name));
  311. case OBS_COMBO_FORMAT_STRING:
  312. return get_string(data, name);
  313. default:
  314. return "";
  315. }
  316. }
  317. static string from_obs_data(obs_data_t *data, const char *name,
  318. obs_combo_format format)
  319. {
  320. return from_obs_data<obs_data_get_int, obs_data_get_double,
  321. obs_data_get_string>(data, name, format);
  322. }
  323. static string from_obs_data_autoselect(obs_data_t *data, const char *name,
  324. obs_combo_format format)
  325. {
  326. return from_obs_data<obs_data_get_autoselect_int,
  327. obs_data_get_autoselect_double,
  328. obs_data_get_autoselect_string>(data, name, format);
  329. }
  330. QWidget *OBSPropertiesView::AddList(obs_property_t *prop, bool &warning)
  331. {
  332. const char *name = obs_property_name(prop);
  333. QComboBox *combo = new QComboBox();
  334. obs_combo_type type = obs_property_list_type(prop);
  335. obs_combo_format format = obs_property_list_format(prop);
  336. size_t count = obs_property_list_item_count(prop);
  337. int idx = -1;
  338. for (size_t i = 0; i < count; i++)
  339. AddComboItem(combo, prop, format, i);
  340. if (type == OBS_COMBO_TYPE_EDITABLE)
  341. combo->setEditable(true);
  342. string value = from_obs_data(settings, name, format);
  343. if (format == OBS_COMBO_FORMAT_STRING &&
  344. type == OBS_COMBO_TYPE_EDITABLE)
  345. combo->lineEdit()->setText(QT_UTF8(value.c_str()));
  346. else
  347. idx = combo->findData(QT_UTF8(value.c_str()));
  348. if (type == OBS_COMBO_TYPE_EDITABLE)
  349. return NewWidget(prop, combo,
  350. SIGNAL(editTextChanged(const QString &)));
  351. if (idx != -1)
  352. combo->setCurrentIndex(idx);
  353. if (obs_data_has_autoselect_value(settings, name)) {
  354. string autoselect =
  355. from_obs_data_autoselect(settings, name, format);
  356. int id = combo->findData(QT_UTF8(autoselect.c_str()));
  357. if (id != -1 && id != idx) {
  358. QString actual = combo->itemText(id);
  359. QString selected = combo->itemText(idx);
  360. QString combined = QTStr(
  361. "Basic.PropertiesWindow.AutoSelectFormat");
  362. combo->setItemText(idx,
  363. combined.arg(selected).arg(actual));
  364. }
  365. }
  366. QAbstractItemModel *model = combo->model();
  367. warning = idx != -1 &&
  368. model->flags(model->index(idx, 0)) == Qt::NoItemFlags;
  369. WidgetInfo *info = new WidgetInfo(this, prop, combo);
  370. connect(combo, SIGNAL(currentIndexChanged(int)), info,
  371. SLOT(ControlChanged()));
  372. children.emplace_back(info);
  373. /* trigger a settings update if the index was not found */
  374. if (idx == -1)
  375. info->ControlChanged();
  376. return combo;
  377. }
  378. QWidget *OBSPropertiesView::AddButton(obs_property_t *prop)
  379. {
  380. const char *desc = obs_property_description(prop);
  381. QPushButton *button = new QPushButton(QT_UTF8(desc));
  382. button->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
  383. return NewWidget(prop, button, SIGNAL(clicked()));
  384. }
  385. void OBSPropertiesView::AddColor(obs_property_t *prop, QFormLayout *layout,
  386. QLabel *&label)
  387. {
  388. QPushButton *button = new QPushButton;
  389. QLabel *colorLabel = new QLabel;
  390. const char *name = obs_property_name(prop);
  391. long long val = obs_data_get_int(settings, name);
  392. QColor color = color_from_int(val);
  393. button->setText(QTStr("Basic.PropertiesWindow.SelectColor"));
  394. colorLabel->setFrameStyle(QFrame::Sunken | QFrame::Panel);
  395. colorLabel->setText(color.name(QColor::HexArgb));
  396. colorLabel->setPalette(QPalette(color));
  397. colorLabel->setAutoFillBackground(true);
  398. colorLabel->setAlignment(Qt::AlignCenter);
  399. QHBoxLayout *subLayout = new QHBoxLayout;
  400. subLayout->setContentsMargins(0, 0, 0, 0);
  401. subLayout->addWidget(colorLabel);
  402. subLayout->addWidget(button);
  403. WidgetInfo *info = new WidgetInfo(this, prop, colorLabel);
  404. connect(button, SIGNAL(clicked()), info, SLOT(ControlChanged()));
  405. children.emplace_back(info);
  406. label = new QLabel(QT_UTF8(obs_property_description(prop)));
  407. layout->addRow(label, subLayout);
  408. }
  409. static void MakeQFont(obs_data_t *font_obj, QFont &font)
  410. {
  411. const char *face = obs_data_get_string(font_obj, "face");
  412. const char *style = obs_data_get_string(font_obj, "style");
  413. int size = (int)obs_data_get_int(font_obj, "size");
  414. uint32_t flags = (uint32_t)obs_data_get_int(font_obj, "flags");
  415. if (face) {
  416. font.setFamily(face);
  417. font.setStyleName(style);
  418. }
  419. if (size)
  420. font.setPointSize(size);
  421. if (flags & OBS_FONT_BOLD) font.setBold(true);
  422. if (flags & OBS_FONT_ITALIC) font.setItalic(true);
  423. if (flags & OBS_FONT_UNDERLINE) font.setUnderline(true);
  424. if (flags & OBS_FONT_STRIKEOUT) font.setStrikeOut(true);
  425. }
  426. void OBSPropertiesView::AddFont(obs_property_t *prop, QFormLayout *layout,
  427. QLabel *&label)
  428. {
  429. const char *name = obs_property_name(prop);
  430. obs_data_t *font_obj = obs_data_get_obj(settings, name);
  431. const char *face = obs_data_get_string(font_obj, "face");
  432. const char *style = obs_data_get_string(font_obj, "style");
  433. QPushButton *button = new QPushButton;
  434. QLabel *fontLabel = new QLabel;
  435. QFont font;
  436. font = fontLabel->font();
  437. MakeQFont(font_obj, font);
  438. button->setText(QTStr("Basic.PropertiesWindow.SelectFont"));
  439. fontLabel->setFrameStyle(QFrame::Sunken | QFrame::Panel);
  440. fontLabel->setFont(font);
  441. fontLabel->setText(QString("%1 %2").arg(face, style));
  442. fontLabel->setAlignment(Qt::AlignCenter);
  443. QHBoxLayout *subLayout = new QHBoxLayout;
  444. subLayout->setContentsMargins(0, 0, 0, 0);
  445. subLayout->addWidget(fontLabel);
  446. subLayout->addWidget(button);
  447. WidgetInfo *info = new WidgetInfo(this, prop, fontLabel);
  448. connect(button, SIGNAL(clicked()), info, SLOT(ControlChanged()));
  449. children.emplace_back(info);
  450. label = new QLabel(QT_UTF8(obs_property_description(prop)));
  451. layout->addRow(label, subLayout);
  452. obs_data_release(font_obj);
  453. }
  454. void OBSPropertiesView::AddProperty(obs_property_t *property,
  455. QFormLayout *layout)
  456. {
  457. const char *name = obs_property_name(property);
  458. obs_property_type type = obs_property_get_type(property);
  459. if (!obs_property_visible(property))
  460. return;
  461. QLabel *label = nullptr;
  462. QWidget *widget = nullptr;
  463. bool warning = false;
  464. switch (type) {
  465. case OBS_PROPERTY_INVALID:
  466. return;
  467. case OBS_PROPERTY_BOOL:
  468. widget = AddCheckbox(property);
  469. break;
  470. case OBS_PROPERTY_INT:
  471. AddInt(property, layout, &label);
  472. break;
  473. case OBS_PROPERTY_FLOAT:
  474. AddFloat(property, layout, &label);
  475. break;
  476. case OBS_PROPERTY_TEXT:
  477. widget = AddText(property, layout, label);
  478. break;
  479. case OBS_PROPERTY_PATH:
  480. AddPath(property, layout, &label);
  481. break;
  482. case OBS_PROPERTY_LIST:
  483. widget = AddList(property, warning);
  484. break;
  485. case OBS_PROPERTY_COLOR:
  486. AddColor(property, layout, label);
  487. break;
  488. case OBS_PROPERTY_FONT:
  489. AddFont(property, layout, label);
  490. break;
  491. case OBS_PROPERTY_BUTTON:
  492. widget = AddButton(property);
  493. break;
  494. }
  495. if (widget && !obs_property_enabled(property))
  496. widget->setEnabled(false);
  497. if (!label &&
  498. type != OBS_PROPERTY_BOOL &&
  499. type != OBS_PROPERTY_BUTTON)
  500. label = new QLabel(QT_UTF8(obs_property_description(property)));
  501. if (warning && label) //TODO: select color based on background color
  502. label->setStyleSheet("QLabel { color: red; }");
  503. if (label && minSize) {
  504. label->setMinimumWidth(minSize);
  505. label->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
  506. }
  507. if (!widget)
  508. return;
  509. layout->addRow(label, widget);
  510. if (!lastFocused.empty())
  511. if (lastFocused.compare(name) == 0)
  512. lastWidget = widget;
  513. }
  514. void OBSPropertiesView::SignalChanged()
  515. {
  516. emit Changed();
  517. }
  518. void WidgetInfo::BoolChanged(const char *setting)
  519. {
  520. QCheckBox *checkbox = static_cast<QCheckBox*>(widget);
  521. obs_data_set_bool(view->settings, setting,
  522. checkbox->checkState() == Qt::Checked);
  523. }
  524. void WidgetInfo::IntChanged(const char *setting)
  525. {
  526. QSpinBox *spin = static_cast<QSpinBox*>(widget);
  527. obs_data_set_int(view->settings, setting, spin->value());
  528. }
  529. void WidgetInfo::FloatChanged(const char *setting)
  530. {
  531. QDoubleSpinBox *spin = static_cast<QDoubleSpinBox*>(widget);
  532. obs_data_set_double(view->settings, setting, spin->value());
  533. }
  534. void WidgetInfo::TextChanged(const char *setting)
  535. {
  536. obs_text_type type = obs_proprety_text_type(property);
  537. if (type == OBS_TEXT_MULTILINE) {
  538. QPlainTextEdit *edit = static_cast<QPlainTextEdit*>(widget);
  539. obs_data_set_string(view->settings, setting,
  540. QT_TO_UTF8(edit->toPlainText()));
  541. return;
  542. }
  543. QLineEdit *edit = static_cast<QLineEdit*>(widget);
  544. obs_data_set_string(view->settings, setting, QT_TO_UTF8(edit->text()));
  545. }
  546. bool WidgetInfo::PathChanged(const char *setting)
  547. {
  548. const char *desc = obs_property_description(property);
  549. obs_path_type type = obs_property_path_type(property);
  550. const char *filter = obs_property_path_filter(property);
  551. const char *default_path = obs_property_path_default_path(property);
  552. QString path;
  553. if (type == OBS_PATH_DIRECTORY)
  554. path = QFileDialog::getExistingDirectory(view,
  555. QT_UTF8(desc), QT_UTF8(default_path),
  556. QFileDialog::ShowDirsOnly |
  557. QFileDialog::DontResolveSymlinks);
  558. else if (type == OBS_PATH_FILE)
  559. path = QFileDialog::getOpenFileName(view,
  560. QT_UTF8(desc), QT_UTF8(default_path),
  561. QT_UTF8(filter));
  562. if (path.isEmpty())
  563. return false;
  564. QLineEdit *edit = static_cast<QLineEdit*>(widget);
  565. edit->setText(path);
  566. obs_data_set_string(view->settings, setting, QT_TO_UTF8(path));
  567. return true;
  568. }
  569. void WidgetInfo::ListChanged(const char *setting)
  570. {
  571. QComboBox *combo = static_cast<QComboBox*>(widget);
  572. obs_combo_format format = obs_property_list_format(property);
  573. obs_combo_type type = obs_property_list_type(property);
  574. QVariant data;
  575. if (type == OBS_COMBO_TYPE_EDITABLE) {
  576. data = combo->currentText();
  577. } else {
  578. int index = combo->currentIndex();
  579. if (index != -1)
  580. data = combo->itemData(index);
  581. else
  582. return;
  583. }
  584. switch (format) {
  585. case OBS_COMBO_FORMAT_INVALID:
  586. return;
  587. case OBS_COMBO_FORMAT_INT:
  588. obs_data_set_int(view->settings, setting,
  589. data.value<long long>());
  590. break;
  591. case OBS_COMBO_FORMAT_FLOAT:
  592. obs_data_set_double(view->settings, setting,
  593. data.value<double>());
  594. break;
  595. case OBS_COMBO_FORMAT_STRING:
  596. obs_data_set_string(view->settings, setting,
  597. QT_TO_UTF8(data.toString()));
  598. break;
  599. }
  600. }
  601. bool WidgetInfo::ColorChanged(const char *setting)
  602. {
  603. const char *desc = obs_property_description(property);
  604. long long val = obs_data_get_int(view->settings, setting);
  605. QColor color = color_from_int(val);
  606. QColorDialog::ColorDialogOptions options =
  607. QColorDialog::ShowAlphaChannel;
  608. /* The native dialog on OSX has all kinds of problems, like closing
  609. * other open QDialogs on exit, and
  610. * https://bugreports.qt-project.org/browse/QTBUG-34532
  611. */
  612. #ifdef __APPLE__
  613. options |= QColorDialog::DontUseNativeDialog;
  614. #endif
  615. color = QColorDialog::getColor(color, view, QT_UTF8(desc), options);
  616. if (!color.isValid())
  617. return false;
  618. QLabel *label = static_cast<QLabel*>(widget);
  619. label->setText(color.name(QColor::HexArgb));
  620. label->setPalette(QPalette(color));
  621. obs_data_set_int(view->settings, setting, color_to_int(color));
  622. return true;
  623. }
  624. bool WidgetInfo::FontChanged(const char *setting)
  625. {
  626. obs_data_t *font_obj = obs_data_get_obj(view->settings, setting);
  627. bool success;
  628. uint32_t flags;
  629. QFont font;
  630. if (!font_obj) {
  631. font = QFontDialog::getFont(&success, view);
  632. } else {
  633. MakeQFont(font_obj, font);
  634. font = QFontDialog::getFont(&success, font, view);
  635. obs_data_release(font_obj);
  636. }
  637. if (!success)
  638. return false;
  639. font_obj = obs_data_create();
  640. obs_data_set_string(font_obj, "face", QT_TO_UTF8(font.family()));
  641. obs_data_set_string(font_obj, "style", QT_TO_UTF8(font.styleName()));
  642. obs_data_set_int(font_obj, "size", font.pointSize());
  643. flags = font.bold() ? OBS_FONT_BOLD : 0;
  644. flags |= font.italic() ? OBS_FONT_ITALIC : 0;
  645. flags |= font.underline() ? OBS_FONT_UNDERLINE : 0;
  646. flags |= font.strikeOut() ? OBS_FONT_STRIKEOUT : 0;
  647. obs_data_set_int(font_obj, "flags", flags);
  648. QLabel *label = static_cast<QLabel*>(widget);
  649. label->setFont(font);
  650. label->setText(QString("%1 %2").arg(font.family(), font.styleName()));
  651. obs_data_set_obj(view->settings, setting, font_obj);
  652. obs_data_release(font_obj);
  653. return true;
  654. }
  655. void WidgetInfo::ButtonClicked()
  656. {
  657. if (obs_property_button_clicked(property, view->obj)) {
  658. QMetaObject::invokeMethod(view, "RefreshProperties",
  659. Qt::QueuedConnection);
  660. }
  661. }
  662. void WidgetInfo::TogglePasswordText(bool show)
  663. {
  664. reinterpret_cast<QLineEdit*>(widget)->setEchoMode(
  665. show ? QLineEdit::Normal : QLineEdit::Password);
  666. }
  667. void WidgetInfo::ControlChanged()
  668. {
  669. const char *setting = obs_property_name(property);
  670. obs_property_type type = obs_property_get_type(property);
  671. switch (type) {
  672. case OBS_PROPERTY_INVALID: return;
  673. case OBS_PROPERTY_BOOL: BoolChanged(setting); break;
  674. case OBS_PROPERTY_INT: IntChanged(setting); break;
  675. case OBS_PROPERTY_FLOAT: FloatChanged(setting); break;
  676. case OBS_PROPERTY_TEXT: TextChanged(setting); break;
  677. case OBS_PROPERTY_LIST: ListChanged(setting); break;
  678. case OBS_PROPERTY_BUTTON: ButtonClicked(); return;
  679. case OBS_PROPERTY_COLOR:
  680. if (!ColorChanged(setting))
  681. return;
  682. break;
  683. case OBS_PROPERTY_FONT:
  684. if (!FontChanged(setting))
  685. return;
  686. break;
  687. case OBS_PROPERTY_PATH:
  688. if (!PathChanged(setting))
  689. return;
  690. }
  691. if (view->callback && !view->deferUpdate)
  692. view->callback(view->obj, view->settings);
  693. view->SignalChanged();
  694. if (obs_property_modified(property, view->settings)) {
  695. view->lastFocused = setting;
  696. QMetaObject::invokeMethod(view, "RefreshProperties",
  697. Qt::QueuedConnection);
  698. }
  699. }