visibility-item-widget.cpp 4.3 KB

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