visibility-item-widget.cpp 3.8 KB

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