visibility-item-widget.cpp 4.3 KB

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