properties-view.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833
  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)
  157. {
  158. const char *name = obs_property_name(prop);
  159. const char *val = obs_data_get_string(settings, name);
  160. obs_text_type type = obs_proprety_text_type(prop);
  161. if (type == OBS_TEXT_MULTILINE) {
  162. QPlainTextEdit *edit = new QPlainTextEdit(QT_UTF8(val));
  163. return NewWidget(prop, edit, SIGNAL(textChanged()));
  164. }
  165. QLineEdit *edit = new QLineEdit();
  166. if (type == OBS_TEXT_PASSWORD)
  167. edit->setEchoMode(QLineEdit::Password);
  168. edit->setText(QT_UTF8(val));
  169. return NewWidget(prop, edit, SIGNAL(textEdited(const QString &)));
  170. }
  171. void OBSPropertiesView::AddPath(obs_property_t *prop, QFormLayout *layout,
  172. QLabel **label)
  173. {
  174. const char *name = obs_property_name(prop);
  175. const char *val = obs_data_get_string(settings, name);
  176. QLayout *subLayout = new QHBoxLayout();
  177. QLineEdit *edit = new QLineEdit();
  178. QPushButton *button = new QPushButton(QTStr("Browse"));
  179. edit->setText(QT_UTF8(val));
  180. edit->setReadOnly(true);
  181. subLayout->addWidget(edit);
  182. subLayout->addWidget(button);
  183. WidgetInfo *info = new WidgetInfo(this, prop, edit);
  184. connect(button, SIGNAL(clicked()), info, SLOT(ControlChanged()));
  185. children.emplace_back(info);
  186. *label = new QLabel(QT_UTF8(obs_property_description(prop)));
  187. layout->addRow(*label, subLayout);
  188. }
  189. void OBSPropertiesView::AddInt(obs_property_t *prop, QFormLayout *layout,
  190. QLabel **label)
  191. {
  192. obs_number_type type = obs_property_int_type(prop);
  193. QLayout *subLayout = new QHBoxLayout();
  194. const char *name = obs_property_name(prop);
  195. int val = (int)obs_data_get_int(settings, name);
  196. QSpinBox *spin = new QSpinBox();
  197. int minVal = obs_property_int_min(prop);
  198. int maxVal = obs_property_int_max(prop);
  199. int stepVal = obs_property_int_step(prop);
  200. spin->setMinimum(minVal);
  201. spin->setMaximum(maxVal);
  202. spin->setSingleStep(stepVal);
  203. spin->setValue(val);
  204. WidgetInfo *info = new WidgetInfo(this, prop, spin);
  205. children.emplace_back(info);
  206. if (type == OBS_NUMBER_SLIDER) {
  207. QSlider *slider = new QSlider();
  208. slider->setMinimum(minVal);
  209. slider->setMaximum(maxVal);
  210. slider->setPageStep(stepVal);
  211. slider->setValue(val);
  212. slider->setOrientation(Qt::Horizontal);
  213. subLayout->addWidget(slider);
  214. connect(slider, SIGNAL(valueChanged(int)),
  215. spin, SLOT(setValue(int)));
  216. connect(spin, SIGNAL(valueChanged(int)),
  217. slider, SLOT(setValue(int)));
  218. }
  219. connect(spin, SIGNAL(valueChanged(int)), info, SLOT(ControlChanged()));
  220. subLayout->addWidget(spin);
  221. *label = new QLabel(QT_UTF8(obs_property_description(prop)));
  222. layout->addRow(*label, subLayout);
  223. }
  224. void OBSPropertiesView::AddFloat(obs_property_t *prop, QFormLayout *layout,
  225. QLabel **label)
  226. {
  227. obs_number_type type = obs_property_float_type(prop);
  228. QLayout *subLayout = new QHBoxLayout();
  229. const char *name = obs_property_name(prop);
  230. double val = obs_data_get_double(settings, name);
  231. QDoubleSpinBox *spin = new QDoubleSpinBox();
  232. double minVal = obs_property_float_min(prop);
  233. double maxVal = obs_property_float_max(prop);
  234. double stepVal = obs_property_float_step(prop);
  235. spin->setMinimum(minVal);
  236. spin->setMaximum(maxVal);
  237. spin->setSingleStep(stepVal);
  238. spin->setValue(val);
  239. WidgetInfo *info = new WidgetInfo(this, prop, spin);
  240. children.emplace_back(info);
  241. if (type == OBS_NUMBER_SLIDER) {
  242. DoubleSlider *slider = new DoubleSlider();
  243. slider->setDoubleConstraints(minVal, maxVal, stepVal, val);
  244. slider->setOrientation(Qt::Horizontal);
  245. subLayout->addWidget(slider);
  246. connect(slider, SIGNAL(doubleValChanged(double)),
  247. spin, SLOT(setValue(double)));
  248. connect(spin, SIGNAL(valueChanged(double)),
  249. slider, SLOT(setDoubleVal(double)));
  250. }
  251. connect(spin, SIGNAL(valueChanged(double)), info,
  252. SLOT(ControlChanged()));
  253. subLayout->addWidget(spin);
  254. *label = new QLabel(QT_UTF8(obs_property_description(prop)));
  255. layout->addRow(*label, subLayout);
  256. }
  257. static void AddComboItem(QComboBox *combo, obs_property_t *prop,
  258. obs_combo_format format, size_t idx)
  259. {
  260. const char *name = obs_property_list_item_name(prop, idx);
  261. QVariant var;
  262. if (format == OBS_COMBO_FORMAT_INT) {
  263. long long val = obs_property_list_item_int(prop, idx);
  264. var = QVariant::fromValue<long long>(val);
  265. } else if (format == OBS_COMBO_FORMAT_FLOAT) {
  266. double val = obs_property_list_item_float(prop, idx);
  267. var = QVariant::fromValue<double>(val);
  268. } else if (format == OBS_COMBO_FORMAT_STRING) {
  269. var = obs_property_list_item_string(prop, idx);
  270. }
  271. combo->addItem(QT_UTF8(name), var);
  272. if (!obs_property_list_item_disabled(prop, idx))
  273. return;
  274. int index = combo->findText(QT_UTF8(name));
  275. if (index < 0)
  276. return;
  277. QStandardItemModel *model =
  278. dynamic_cast<QStandardItemModel*>(combo->model());
  279. if (!model)
  280. return;
  281. QStandardItem *item = model->item(index);
  282. item->setFlags(Qt::NoItemFlags);
  283. }
  284. template <long long get_int(obs_data_t*, const char*),
  285. double get_double(obs_data_t*, const char*),
  286. const char *get_string(obs_data_t*, const char*)>
  287. static string from_obs_data(obs_data_t *data, const char *name,
  288. obs_combo_format format)
  289. {
  290. switch (format) {
  291. case OBS_COMBO_FORMAT_INT:
  292. return to_string(get_int(data, name));
  293. case OBS_COMBO_FORMAT_FLOAT:
  294. return to_string(get_double(data, name));
  295. case OBS_COMBO_FORMAT_STRING:
  296. return get_string(data, name);
  297. default:
  298. return "";
  299. }
  300. }
  301. static string from_obs_data(obs_data_t *data, const char *name,
  302. obs_combo_format format)
  303. {
  304. return from_obs_data<obs_data_get_int, obs_data_get_double,
  305. obs_data_get_string>(data, name, format);
  306. }
  307. static string from_obs_data_autoselect(obs_data_t *data, const char *name,
  308. obs_combo_format format)
  309. {
  310. return from_obs_data<obs_data_get_autoselect_int,
  311. obs_data_get_autoselect_double,
  312. obs_data_get_autoselect_string>(data, name, format);
  313. }
  314. QWidget *OBSPropertiesView::AddList(obs_property_t *prop, bool &warning)
  315. {
  316. const char *name = obs_property_name(prop);
  317. QComboBox *combo = new QComboBox();
  318. obs_combo_type type = obs_property_list_type(prop);
  319. obs_combo_format format = obs_property_list_format(prop);
  320. size_t count = obs_property_list_item_count(prop);
  321. int idx = -1;
  322. for (size_t i = 0; i < count; i++)
  323. AddComboItem(combo, prop, format, i);
  324. if (type == OBS_COMBO_TYPE_EDITABLE)
  325. combo->setEditable(true);
  326. string value = from_obs_data(settings, name, format);
  327. if (format == OBS_COMBO_FORMAT_STRING &&
  328. type == OBS_COMBO_TYPE_EDITABLE)
  329. combo->lineEdit()->setText(QT_UTF8(value.c_str()));
  330. else
  331. idx = combo->findData(QT_UTF8(value.c_str()));
  332. if (type == OBS_COMBO_TYPE_EDITABLE)
  333. return NewWidget(prop, combo,
  334. SIGNAL(editTextChanged(const QString &)));
  335. if (idx != -1)
  336. combo->setCurrentIndex(idx);
  337. if (obs_data_has_autoselect_value(settings, name)) {
  338. string autoselect =
  339. from_obs_data_autoselect(settings, name, format);
  340. int id = combo->findData(QT_UTF8(autoselect.c_str()));
  341. if (id != -1 && id != idx) {
  342. QString actual = combo->itemText(id);
  343. QString selected = combo->itemText(idx);
  344. QString combined = QTStr(
  345. "Basic.PropertiesWindow.AutoSelectFormat");
  346. combo->setItemText(idx,
  347. combined.arg(selected).arg(actual));
  348. }
  349. }
  350. QAbstractItemModel *model = combo->model();
  351. warning = idx != -1 &&
  352. model->flags(model->index(idx, 0)) == Qt::NoItemFlags;
  353. WidgetInfo *info = new WidgetInfo(this, prop, combo);
  354. connect(combo, SIGNAL(currentIndexChanged(int)), info,
  355. SLOT(ControlChanged()));
  356. children.emplace_back(info);
  357. /* trigger a settings update if the index was not found */
  358. if (idx == -1)
  359. info->ControlChanged();
  360. return combo;
  361. }
  362. QWidget *OBSPropertiesView::AddButton(obs_property_t *prop)
  363. {
  364. const char *desc = obs_property_description(prop);
  365. QPushButton *button = new QPushButton(QT_UTF8(desc));
  366. button->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
  367. return NewWidget(prop, button, SIGNAL(clicked()));
  368. }
  369. void OBSPropertiesView::AddColor(obs_property_t *prop, QFormLayout *layout,
  370. QLabel *&label)
  371. {
  372. QPushButton *button = new QPushButton;
  373. QLabel *colorLabel = new QLabel;
  374. const char *name = obs_property_name(prop);
  375. long long val = obs_data_get_int(settings, name);
  376. QColor color = color_from_int(val);
  377. button->setText(QTStr("Basic.PropertiesWindow.SelectColor"));
  378. colorLabel->setFrameStyle(QFrame::Sunken | QFrame::Panel);
  379. colorLabel->setText(color.name(QColor::HexArgb));
  380. colorLabel->setPalette(QPalette(color));
  381. colorLabel->setAutoFillBackground(true);
  382. colorLabel->setAlignment(Qt::AlignCenter);
  383. QHBoxLayout *subLayout = new QHBoxLayout;
  384. subLayout->setContentsMargins(0, 0, 0, 0);
  385. subLayout->addWidget(colorLabel);
  386. subLayout->addWidget(button);
  387. WidgetInfo *info = new WidgetInfo(this, prop, colorLabel);
  388. connect(button, SIGNAL(clicked()), info, SLOT(ControlChanged()));
  389. children.emplace_back(info);
  390. label = new QLabel(QT_UTF8(obs_property_description(prop)));
  391. layout->addRow(label, subLayout);
  392. }
  393. static void MakeQFont(obs_data_t *font_obj, QFont &font)
  394. {
  395. const char *face = obs_data_get_string(font_obj, "face");
  396. const char *style = obs_data_get_string(font_obj, "style");
  397. int size = (int)obs_data_get_int(font_obj, "size");
  398. uint32_t flags = (uint32_t)obs_data_get_int(font_obj, "flags");
  399. if (face) {
  400. font.setFamily(face);
  401. font.setStyleName(style);
  402. }
  403. if (size)
  404. font.setPointSize(size);
  405. if (flags & OBS_FONT_BOLD) font.setBold(true);
  406. if (flags & OBS_FONT_ITALIC) font.setItalic(true);
  407. if (flags & OBS_FONT_UNDERLINE) font.setUnderline(true);
  408. if (flags & OBS_FONT_STRIKEOUT) font.setStrikeOut(true);
  409. }
  410. void OBSPropertiesView::AddFont(obs_property_t *prop, QFormLayout *layout,
  411. QLabel *&label)
  412. {
  413. const char *name = obs_property_name(prop);
  414. obs_data_t *font_obj = obs_data_get_obj(settings, name);
  415. const char *face = obs_data_get_string(font_obj, "face");
  416. const char *style = obs_data_get_string(font_obj, "style");
  417. QPushButton *button = new QPushButton;
  418. QLabel *fontLabel = new QLabel;
  419. QFont font;
  420. font = fontLabel->font();
  421. MakeQFont(font_obj, font);
  422. button->setText(QTStr("Basic.PropertiesWindow.SelectFont"));
  423. fontLabel->setFrameStyle(QFrame::Sunken | QFrame::Panel);
  424. fontLabel->setFont(font);
  425. fontLabel->setText(QString("%1 %2").arg(face, style));
  426. fontLabel->setAlignment(Qt::AlignCenter);
  427. QHBoxLayout *subLayout = new QHBoxLayout;
  428. subLayout->setContentsMargins(0, 0, 0, 0);
  429. subLayout->addWidget(fontLabel);
  430. subLayout->addWidget(button);
  431. WidgetInfo *info = new WidgetInfo(this, prop, fontLabel);
  432. connect(button, SIGNAL(clicked()), info, SLOT(ControlChanged()));
  433. children.emplace_back(info);
  434. label = new QLabel(QT_UTF8(obs_property_description(prop)));
  435. layout->addRow(label, subLayout);
  436. obs_data_release(font_obj);
  437. }
  438. void OBSPropertiesView::AddProperty(obs_property_t *property,
  439. QFormLayout *layout)
  440. {
  441. const char *name = obs_property_name(property);
  442. obs_property_type type = obs_property_get_type(property);
  443. if (!obs_property_visible(property))
  444. return;
  445. QLabel *label = nullptr;
  446. QWidget *widget = nullptr;
  447. bool warning = false;
  448. switch (type) {
  449. case OBS_PROPERTY_INVALID:
  450. return;
  451. case OBS_PROPERTY_BOOL:
  452. widget = AddCheckbox(property);
  453. break;
  454. case OBS_PROPERTY_INT:
  455. AddInt(property, layout, &label);
  456. break;
  457. case OBS_PROPERTY_FLOAT:
  458. AddFloat(property, layout, &label);
  459. break;
  460. case OBS_PROPERTY_TEXT:
  461. widget = AddText(property);
  462. break;
  463. case OBS_PROPERTY_PATH:
  464. AddPath(property, layout, &label);
  465. break;
  466. case OBS_PROPERTY_LIST:
  467. widget = AddList(property, warning);
  468. break;
  469. case OBS_PROPERTY_COLOR:
  470. AddColor(property, layout, label);
  471. break;
  472. case OBS_PROPERTY_FONT:
  473. AddFont(property, layout, label);
  474. break;
  475. case OBS_PROPERTY_BUTTON:
  476. widget = AddButton(property);
  477. break;
  478. }
  479. if (widget && !obs_property_enabled(property))
  480. widget->setEnabled(false);
  481. if (!label &&
  482. type != OBS_PROPERTY_BOOL &&
  483. type != OBS_PROPERTY_BUTTON)
  484. label = new QLabel(QT_UTF8(obs_property_description(property)));
  485. if (warning && label) //TODO: select color based on background color
  486. label->setStyleSheet("QLabel { color: red; }");
  487. if (label && minSize) {
  488. label->setMinimumWidth(minSize);
  489. label->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
  490. }
  491. if (!widget)
  492. return;
  493. layout->addRow(label, widget);
  494. if (!lastFocused.empty())
  495. if (lastFocused.compare(name) == 0)
  496. lastWidget = widget;
  497. }
  498. void OBSPropertiesView::SignalChanged()
  499. {
  500. emit Changed();
  501. }
  502. void WidgetInfo::BoolChanged(const char *setting)
  503. {
  504. QCheckBox *checkbox = static_cast<QCheckBox*>(widget);
  505. obs_data_set_bool(view->settings, setting,
  506. checkbox->checkState() == Qt::Checked);
  507. }
  508. void WidgetInfo::IntChanged(const char *setting)
  509. {
  510. QSpinBox *spin = static_cast<QSpinBox*>(widget);
  511. obs_data_set_int(view->settings, setting, spin->value());
  512. }
  513. void WidgetInfo::FloatChanged(const char *setting)
  514. {
  515. QDoubleSpinBox *spin = static_cast<QDoubleSpinBox*>(widget);
  516. obs_data_set_double(view->settings, setting, spin->value());
  517. }
  518. void WidgetInfo::TextChanged(const char *setting)
  519. {
  520. obs_text_type type = obs_proprety_text_type(property);
  521. if (type == OBS_TEXT_MULTILINE) {
  522. QPlainTextEdit *edit = static_cast<QPlainTextEdit*>(widget);
  523. obs_data_set_string(view->settings, setting,
  524. QT_TO_UTF8(edit->toPlainText()));
  525. return;
  526. }
  527. QLineEdit *edit = static_cast<QLineEdit*>(widget);
  528. obs_data_set_string(view->settings, setting, QT_TO_UTF8(edit->text()));
  529. }
  530. bool WidgetInfo::PathChanged(const char *setting)
  531. {
  532. const char *desc = obs_property_description(property);
  533. obs_path_type type = obs_property_path_type(property);
  534. const char *filter = obs_property_path_filter(property);
  535. const char *default_path = obs_property_path_default_path(property);
  536. QString path;
  537. if (type == OBS_PATH_DIRECTORY)
  538. path = QFileDialog::getExistingDirectory(view,
  539. QT_UTF8(desc), QT_UTF8(default_path),
  540. QFileDialog::ShowDirsOnly |
  541. QFileDialog::DontResolveSymlinks);
  542. else if (type == OBS_PATH_FILE)
  543. path = QFileDialog::getOpenFileName(view,
  544. QT_UTF8(desc), QT_UTF8(default_path),
  545. QT_UTF8(filter));
  546. if (path.isEmpty())
  547. return false;
  548. QLineEdit *edit = static_cast<QLineEdit*>(widget);
  549. edit->setText(path);
  550. obs_data_set_string(view->settings, setting, QT_TO_UTF8(path));
  551. return true;
  552. }
  553. void WidgetInfo::ListChanged(const char *setting)
  554. {
  555. QComboBox *combo = static_cast<QComboBox*>(widget);
  556. obs_combo_format format = obs_property_list_format(property);
  557. obs_combo_type type = obs_property_list_type(property);
  558. QVariant data;
  559. if (type == OBS_COMBO_TYPE_EDITABLE) {
  560. data = combo->currentText();
  561. } else {
  562. int index = combo->currentIndex();
  563. if (index != -1)
  564. data = combo->itemData(index);
  565. else
  566. return;
  567. }
  568. switch (format) {
  569. case OBS_COMBO_FORMAT_INVALID:
  570. return;
  571. case OBS_COMBO_FORMAT_INT:
  572. obs_data_set_int(view->settings, setting,
  573. data.value<long long>());
  574. break;
  575. case OBS_COMBO_FORMAT_FLOAT:
  576. obs_data_set_double(view->settings, setting,
  577. data.value<double>());
  578. break;
  579. case OBS_COMBO_FORMAT_STRING:
  580. obs_data_set_string(view->settings, setting,
  581. QT_TO_UTF8(data.toString()));
  582. break;
  583. }
  584. }
  585. bool WidgetInfo::ColorChanged(const char *setting)
  586. {
  587. const char *desc = obs_property_description(property);
  588. long long val = obs_data_get_int(view->settings, setting);
  589. QColor color = color_from_int(val);
  590. QColorDialog::ColorDialogOptions options =
  591. QColorDialog::ShowAlphaChannel;
  592. /* The native dialog on OSX has all kinds of problems, like closing
  593. * other open QDialogs on exit, and
  594. * https://bugreports.qt-project.org/browse/QTBUG-34532
  595. */
  596. #ifdef __APPLE__
  597. options |= QColorDialog::DontUseNativeDialog;
  598. #endif
  599. color = QColorDialog::getColor(color, view, QT_UTF8(desc), options);
  600. if (!color.isValid())
  601. return false;
  602. QLabel *label = static_cast<QLabel*>(widget);
  603. label->setText(color.name(QColor::HexArgb));
  604. label->setPalette(QPalette(color));
  605. obs_data_set_int(view->settings, setting, color_to_int(color));
  606. return true;
  607. }
  608. bool WidgetInfo::FontChanged(const char *setting)
  609. {
  610. obs_data_t *font_obj = obs_data_get_obj(view->settings, setting);
  611. bool success;
  612. uint32_t flags;
  613. QFont font;
  614. if (!font_obj) {
  615. font = QFontDialog::getFont(&success, view);
  616. } else {
  617. MakeQFont(font_obj, font);
  618. font = QFontDialog::getFont(&success, font, view);
  619. obs_data_release(font_obj);
  620. }
  621. if (!success)
  622. return false;
  623. font_obj = obs_data_create();
  624. obs_data_set_string(font_obj, "face", QT_TO_UTF8(font.family()));
  625. obs_data_set_string(font_obj, "style", QT_TO_UTF8(font.styleName()));
  626. obs_data_set_int(font_obj, "size", font.pointSize());
  627. flags = font.bold() ? OBS_FONT_BOLD : 0;
  628. flags |= font.italic() ? OBS_FONT_ITALIC : 0;
  629. flags |= font.underline() ? OBS_FONT_UNDERLINE : 0;
  630. flags |= font.strikeOut() ? OBS_FONT_STRIKEOUT : 0;
  631. obs_data_set_int(font_obj, "flags", flags);
  632. QLabel *label = static_cast<QLabel*>(widget);
  633. label->setFont(font);
  634. label->setText(QString("%1 %2").arg(font.family(), font.styleName()));
  635. obs_data_set_obj(view->settings, setting, font_obj);
  636. obs_data_release(font_obj);
  637. return true;
  638. }
  639. void WidgetInfo::ButtonClicked()
  640. {
  641. if (obs_property_button_clicked(property, view->obj)) {
  642. QMetaObject::invokeMethod(view, "RefreshProperties",
  643. Qt::QueuedConnection);
  644. }
  645. }
  646. void WidgetInfo::ControlChanged()
  647. {
  648. const char *setting = obs_property_name(property);
  649. obs_property_type type = obs_property_get_type(property);
  650. switch (type) {
  651. case OBS_PROPERTY_INVALID: return;
  652. case OBS_PROPERTY_BOOL: BoolChanged(setting); break;
  653. case OBS_PROPERTY_INT: IntChanged(setting); break;
  654. case OBS_PROPERTY_FLOAT: FloatChanged(setting); break;
  655. case OBS_PROPERTY_TEXT: TextChanged(setting); break;
  656. case OBS_PROPERTY_LIST: ListChanged(setting); break;
  657. case OBS_PROPERTY_BUTTON: ButtonClicked(); return;
  658. case OBS_PROPERTY_COLOR:
  659. if (!ColorChanged(setting))
  660. return;
  661. break;
  662. case OBS_PROPERTY_FONT:
  663. if (!FontChanged(setting))
  664. return;
  665. break;
  666. case OBS_PROPERTY_PATH:
  667. if (!PathChanged(setting))
  668. return;
  669. }
  670. if (view->callback && !view->deferUpdate)
  671. view->callback(view->obj, view->settings);
  672. view->SignalChanged();
  673. if (obs_property_modified(property, view->settings)) {
  674. view->lastFocused = setting;
  675. QMetaObject::invokeMethod(view, "RefreshProperties",
  676. Qt::QueuedConnection);
  677. }
  678. }