visibility-item-widget.cpp 3.7 KB

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