Skip to content

Commit 8a3a48f

Browse files
committed
Remove PTZDevice reference from SourceNameDelegate
Source name delegate was directly accessing PTZDevice in the displayText() method. It had to do that because displayText doesn't have access to the QModelIndex which is needed to look up additional data. Replace this with overriding paint() and sizeHint() instead. Those methods are passed the index, which it can use to fetch more data from the model. This patch also enables using Qt::UserRole to fetch the connection details as a string from the camera device. Signed-off-by: Grant Likely <grant.likely@secretlab.ca>
1 parent f7c6bf2 commit 8a3a48f

3 files changed

Lines changed: 31 additions & 19 deletions

File tree

src/ptz-device.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,11 @@ QVariant PTZListModel::data(const QModelIndex &index, int role) const
6868
if (role == Qt::DisplayRole || role == Qt::EditRole) {
6969
return devices.value(devices.keys().at(index.row()))->objectName();
7070
}
71-
#if 0
71+
7272
if (role == Qt::UserRole) {
73-
return get_device(index.row());
73+
return devices.value(devices.keys().at(index.row()))->description();
7474
}
75-
#endif
75+
7676
return QVariant();
7777
}
7878

src/settings.cpp

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,34 @@ static PTZSettings *ptzSettingsWindow = nullptr;
3535

3636
/* ----------------------------------------------------------------- */
3737

38-
QString SourceNameDelegate::displayText(const QVariant &value, const QLocale &locale) const
39-
{
40-
auto string = QStyledItemDelegate::displayText(value, locale);
41-
auto ptz = ptzDeviceList.getDeviceByName(string);
42-
if (ptz)
43-
return ptz->description() + " - " + string;
44-
return string;
45-
}
38+
class SourceNameDelegate : public QStyledItemDelegate {
39+
Q_DISABLE_COPY(SourceNameDelegate)
40+
41+
public:
42+
using QStyledItemDelegate::QStyledItemDelegate;
43+
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override
44+
{
45+
Q_ASSERT(index.isValid());
46+
QStyleOptionViewItem opt = option;
47+
initStyleOption(&opt, index);
48+
opt.text = opt.text + " [" + index.data(Qt::UserRole).toString() + "]";
49+
const QWidget *widget = option.widget;
50+
QStyle *style = widget ? widget->style() : QApplication::style();
51+
style->drawControl(QStyle::CE_ItemViewItem, &opt, painter, widget);
52+
};
53+
QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override
54+
{
55+
QVariant value = index.data(Qt::SizeHintRole);
56+
if (value.isValid())
57+
return qvariant_cast<QSize>(value);
58+
QStyleOptionViewItem opt = option;
59+
initStyleOption(&opt, index);
60+
opt.text = opt.text + " [" + index.data(Qt::UserRole).toString() + "]";
61+
const QWidget *widget = option.widget;
62+
QStyle *style = widget ? widget->style() : QApplication::style();
63+
return style->sizeFromContents(QStyle::CT_ItemViewItem, &opt, QSize(), widget);
64+
};
65+
};
4666

4767
obs_properties_t *PTZSettings::getProperties(void)
4868
{

src/settings.hpp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,6 @@ public slots:
3535
};
3636
#endif
3737

38-
class SourceNameDelegate : public QStyledItemDelegate {
39-
Q_OBJECT
40-
41-
public:
42-
SourceNameDelegate(QObject *parent = nullptr) : QStyledItemDelegate(parent) {};
43-
virtual QString displayText(const QVariant &value, const QLocale &locale) const;
44-
};
45-
4638
class PTZSettings : public QWidget {
4739
Q_OBJECT
4840

0 commit comments

Comments
 (0)