properties-view.cpp 21 KB

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