1
0

visibility-item-widget.cpp 4.5 KB

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