visibility-item-widget.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #include "visibility-item-widget.hpp"
  2. #include "visibility-checkbox.hpp"
  3. #include "qt-wrappers.hpp"
  4. #include "obs-app.hpp"
  5. #include <QListWidget>
  6. #include <QLineEdit>
  7. #include <QHBoxLayout>
  8. #include <QMessageBox>
  9. #include <QLabel>
  10. VisibilityItemWidget::VisibilityItemWidget(obs_source_t *source_)
  11. : source(source_),
  12. enabledSignal(obs_source_get_signal_handler(source), "enable",
  13. OBSSourceEnabled, this),
  14. renamedSignal(obs_source_get_signal_handler(source), "rename",
  15. OBSSourceRenamed, this)
  16. {
  17. const char *name = obs_source_get_name(source);
  18. bool enabled = obs_source_enabled(source);
  19. vis = new VisibilityCheckBox();
  20. vis->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
  21. /* Fix for non-apple systems where the spacing would be too big */
  22. #ifndef __APPLE__
  23. vis->setMaximumSize(16, 16);
  24. #endif
  25. vis->setChecked(enabled);
  26. label = new QLabel(QT_UTF8(name));
  27. label->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
  28. QHBoxLayout *itemLayout = new QHBoxLayout();
  29. itemLayout->addWidget(vis);
  30. itemLayout->addWidget(label);
  31. itemLayout->setContentsMargins(5, 2, 5, 2);
  32. setLayout(itemLayout);
  33. setStyleSheet("background-color: rgba(255, 255, 255, 0);");
  34. connect(vis, SIGNAL(clicked(bool)), this,
  35. SLOT(VisibilityClicked(bool)));
  36. }
  37. void VisibilityItemWidget::OBSSourceEnabled(void *param, calldata_t *data)
  38. {
  39. VisibilityItemWidget *window =
  40. reinterpret_cast<VisibilityItemWidget *>(param);
  41. bool enabled = calldata_bool(data, "enabled");
  42. QMetaObject::invokeMethod(window, "SourceEnabled",
  43. Q_ARG(bool, enabled));
  44. }
  45. void VisibilityItemWidget::OBSSourceRenamed(void *param, calldata_t *data)
  46. {
  47. VisibilityItemWidget *window =
  48. reinterpret_cast<VisibilityItemWidget *>(param);
  49. const char *name = calldata_string(data, "new_name");
  50. QMetaObject::invokeMethod(window, "SourceRenamed",
  51. Q_ARG(QString, QT_UTF8(name)));
  52. }
  53. void VisibilityItemWidget::VisibilityClicked(bool visible)
  54. {
  55. obs_source_set_enabled(source, visible);
  56. }
  57. void VisibilityItemWidget::SourceEnabled(bool enabled)
  58. {
  59. if (vis->isChecked() != enabled)
  60. vis->setChecked(enabled);
  61. }
  62. void VisibilityItemWidget::SourceRenamed(QString name)
  63. {
  64. if (label && name != label->text())
  65. label->setText(name);
  66. }
  67. void VisibilityItemWidget::SetColor(const QColor &color, bool active_,
  68. bool selected_)
  69. {
  70. /* Do not update unless the state has actually changed */
  71. if (active_ == active && selected_ == selected)
  72. return;
  73. QPalette pal = vis->palette();
  74. pal.setColor(QPalette::WindowText, color);
  75. vis->setPalette(pal);
  76. label->setStyleSheet(QString("color: %1;").arg(color.name()));
  77. active = active_;
  78. selected = selected_;
  79. }
  80. VisibilityItemDelegate::VisibilityItemDelegate(QObject *parent)
  81. : QStyledItemDelegate(parent)
  82. {
  83. }
  84. void VisibilityItemDelegate::paint(QPainter *painter,
  85. const QStyleOptionViewItem &option,
  86. const QModelIndex &index) const
  87. {
  88. QStyledItemDelegate::paint(painter, option, index);
  89. QObject *parentObj = parent();
  90. QListWidget *list = qobject_cast<QListWidget *>(parentObj);
  91. if (!list)
  92. return;
  93. QListWidgetItem *item = list->item(index.row());
  94. VisibilityItemWidget *widget =
  95. qobject_cast<VisibilityItemWidget *>(list->itemWidget(item));
  96. if (!widget)
  97. return;
  98. bool selected = option.state.testFlag(QStyle::State_Selected);
  99. bool active = option.state.testFlag(QStyle::State_Active);
  100. QPalette palette = list->palette();
  101. #if defined(_WIN32) || defined(__APPLE__)
  102. QPalette::ColorGroup group = active ? QPalette::Active
  103. : QPalette::Inactive;
  104. #else
  105. QPalette::ColorGroup group = QPalette::Active;
  106. #endif
  107. #ifdef _WIN32
  108. QPalette::ColorRole highlightRole = QPalette::WindowText;
  109. #else
  110. QPalette::ColorRole highlightRole = QPalette::HighlightedText;
  111. #endif
  112. QPalette::ColorRole role;
  113. if (selected && active)
  114. role = highlightRole;
  115. else
  116. role = QPalette::WindowText;
  117. widget->SetColor(palette.color(group, role), active, selected);
  118. }
  119. void SetupVisibilityItem(QListWidget *list, QListWidgetItem *item,
  120. obs_source_t *source)
  121. {
  122. VisibilityItemWidget *baseWidget = new VisibilityItemWidget(source);
  123. item->setSizeHint(baseWidget->sizeHint());
  124. list->setItemWidget(item, baseWidget);
  125. }