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