properties-view.cpp 21 KB

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