visibility-item-widget.cpp 4.3 KB

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