visibility-item-widget.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #include "moc_visibility-item-widget.cpp"
  2. #include "obs-app.hpp"
  3. #include "source-label.hpp"
  4. #include <qt-wrappers.hpp>
  5. #include <QListWidget>
  6. #include <QLineEdit>
  7. #include <QHBoxLayout>
  8. #include <QMessageBox>
  9. #include <QLabel>
  10. #include <QKeyEvent>
  11. #include <QCheckBox>
  12. VisibilityItemWidget::VisibilityItemWidget(obs_source_t *source_)
  13. : source(source_),
  14. enabledSignal(obs_source_get_signal_handler(source), "enable", OBSSourceEnabled, this)
  15. {
  16. bool enabled = obs_source_enabled(source);
  17. vis = new QCheckBox();
  18. vis->setProperty("class", "checkbox-icon indicator-visibility");
  19. vis->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
  20. vis->setChecked(enabled);
  21. label = new OBSSourceLabel(source);
  22. label->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
  23. QHBoxLayout *itemLayout = new QHBoxLayout();
  24. itemLayout->addWidget(vis);
  25. itemLayout->addWidget(label);
  26. itemLayout->setContentsMargins(0, 0, 0, 0);
  27. setLayout(itemLayout);
  28. connect(vis, &QCheckBox::clicked, [this](bool visible) { obs_source_set_enabled(source, visible); });
  29. }
  30. void VisibilityItemWidget::OBSSourceEnabled(void *param, calldata_t *data)
  31. {
  32. VisibilityItemWidget *window = reinterpret_cast<VisibilityItemWidget *>(param);
  33. bool enabled = calldata_bool(data, "enabled");
  34. QMetaObject::invokeMethod(window, "SourceEnabled", Q_ARG(bool, enabled));
  35. }
  36. void VisibilityItemWidget::SourceEnabled(bool enabled)
  37. {
  38. if (vis->isChecked() != enabled)
  39. vis->setChecked(enabled);
  40. }
  41. void VisibilityItemWidget::SetColor(const QColor &color, bool active_, bool selected_)
  42. {
  43. /* Do not update unless the state has actually changed */
  44. if (active_ == active && selected_ == selected)
  45. return;
  46. QPalette pal = vis->palette();
  47. pal.setColor(QPalette::WindowText, color);
  48. vis->setPalette(pal);
  49. label->setStyleSheet(QString("color: %1;").arg(color.name()));
  50. active = active_;
  51. selected = selected_;
  52. }
  53. VisibilityItemDelegate::VisibilityItemDelegate(QObject *parent) : QStyledItemDelegate(parent) {}
  54. void VisibilityItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
  55. const QModelIndex &index) const
  56. {
  57. QStyledItemDelegate::paint(painter, option, index);
  58. QObject *parentObj = parent();
  59. QListWidget *list = qobject_cast<QListWidget *>(parentObj);
  60. if (!list)
  61. return;
  62. QListWidgetItem *item = list->item(index.row());
  63. VisibilityItemWidget *widget = qobject_cast<VisibilityItemWidget *>(list->itemWidget(item));
  64. if (!widget)
  65. return;
  66. bool selected = option.state.testFlag(QStyle::State_Selected);
  67. bool active = option.state.testFlag(QStyle::State_Active);
  68. QPalette palette = list->palette();
  69. #if defined(_WIN32) || defined(__APPLE__)
  70. QPalette::ColorGroup group = active ? QPalette::Active : QPalette::Inactive;
  71. #else
  72. QPalette::ColorGroup group = QPalette::Active;
  73. #endif
  74. #ifdef _WIN32
  75. QPalette::ColorRole highlightRole = QPalette::WindowText;
  76. #else
  77. QPalette::ColorRole highlightRole = QPalette::HighlightedText;
  78. #endif
  79. QPalette::ColorRole role;
  80. if (selected && active)
  81. role = highlightRole;
  82. else
  83. role = QPalette::WindowText;
  84. widget->SetColor(palette.color(group, role), active, selected);
  85. }
  86. bool VisibilityItemDelegate::eventFilter(QObject *object, QEvent *event)
  87. {
  88. QWidget *editor = qobject_cast<QWidget *>(object);
  89. if (!editor)
  90. return false;
  91. if (event->type() == QEvent::KeyPress) {
  92. QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
  93. if (keyEvent->key() == Qt::Key_Tab || keyEvent->key() == Qt::Key_Backtab) {
  94. return false;
  95. }
  96. }
  97. return QStyledItemDelegate::eventFilter(object, event);
  98. }
  99. void SetupVisibilityItem(QListWidget *list, QListWidgetItem *item, obs_source_t *source)
  100. {
  101. VisibilityItemWidget *baseWidget = new VisibilityItemWidget(source);
  102. list->setItemWidget(item, baseWidget);
  103. }