ColorSourceToolbar.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #include "ColorSourceToolbar.hpp"
  2. #include "ui_color-source-toolbar.h"
  3. #include <QColorDialog>
  4. #include "moc_ColorSourceToolbar.cpp"
  5. QColor color_from_int(long long val)
  6. {
  7. return QColor(val & 0xff, (val >> 8) & 0xff, (val >> 16) & 0xff, (val >> 24) & 0xff);
  8. }
  9. long long color_to_int(QColor color)
  10. {
  11. auto shift = [&](unsigned val, int shift) {
  12. return ((val & 0xff) << shift);
  13. };
  14. return shift(color.red(), 0) | shift(color.green(), 8) | shift(color.blue(), 16) | shift(color.alpha(), 24);
  15. }
  16. ColorSourceToolbar::ColorSourceToolbar(QWidget *parent, OBSSource source)
  17. : SourceToolbar(parent, source),
  18. ui(new Ui_ColorSourceToolbar)
  19. {
  20. ui->setupUi(this);
  21. OBSDataAutoRelease settings = obs_source_get_settings(source);
  22. unsigned int val = (unsigned int)obs_data_get_int(settings, "color");
  23. color = color_from_int(val);
  24. UpdateColor();
  25. }
  26. ColorSourceToolbar::~ColorSourceToolbar() {}
  27. void ColorSourceToolbar::UpdateColor()
  28. {
  29. QPalette palette = QPalette(color);
  30. ui->color->setFrameStyle(QFrame::Sunken | QFrame::Panel);
  31. ui->color->setText(color.name(QColor::HexRgb));
  32. ui->color->setPalette(palette);
  33. ui->color->setStyleSheet(QString("background-color :%1; color: %2;")
  34. .arg(palette.color(QPalette::Window).name(QColor::HexRgb))
  35. .arg(palette.color(QPalette::WindowText).name(QColor::HexRgb)));
  36. ui->color->setAutoFillBackground(true);
  37. ui->color->setAlignment(Qt::AlignCenter);
  38. }
  39. void ColorSourceToolbar::on_choose_clicked()
  40. {
  41. OBSSource source = GetSource();
  42. if (!source) {
  43. return;
  44. }
  45. obs_property_t *p = obs_properties_get(props.get(), "color");
  46. const char *desc = obs_property_description(p);
  47. QColorDialog::ColorDialogOptions options;
  48. options |= QColorDialog::ShowAlphaChannel;
  49. #ifdef __linux__
  50. // TODO: Revisit hang on Ubuntu with native dialog
  51. options |= QColorDialog::DontUseNativeDialog;
  52. #endif
  53. QColor newColor = QColorDialog::getColor(color, this, desc, options);
  54. if (!newColor.isValid()) {
  55. return;
  56. }
  57. color = newColor;
  58. UpdateColor();
  59. SaveOldProperties(source);
  60. OBSDataAutoRelease settings = obs_data_create();
  61. obs_data_set_int(settings, "color", color_to_int(color));
  62. obs_source_update(source, settings);
  63. SetUndoProperties(source);
  64. }