Add ListEditor helper widget

This commit is contained in:
WarmUpTill 2024-05-31 16:32:22 +02:00 committed by WarmUpTill
parent b7516cac5b
commit 286f781f4d
9 changed files with 159 additions and 246 deletions

View File

@ -170,6 +170,8 @@ target_sources(
lib/utils/item-selection-helpers.hpp
lib/utils/layout-helpers.cpp
lib/utils/layout-helpers.hpp
lib/utils/list-editor.cpp
lib/utils/list-editor.hpp
lib/utils/log-helper.cpp
lib/utils/log-helper.hpp
lib/utils/math-helpers.cpp

View File

@ -7,68 +7,16 @@
namespace advss {
MacroList::MacroList(QWidget *parent, bool allowDuplicates, bool reorder)
: QWidget(parent),
_list(new QListWidget()),
_add(new QPushButton()),
_remove(new QPushButton()),
_up(new QPushButton()),
_down(new QPushButton()),
_controlsLayout(new QHBoxLayout()),
_allowDuplicates(allowDuplicates),
_reorder(reorder)
: ListEditor(parent, reorder),
_allowDuplicates(allowDuplicates)
{
_add->setMaximumWidth(22);
_add->setProperty("themeID",
QVariant(QString::fromUtf8("addIconSmall")));
_add->setFlat(true);
_remove->setMaximumWidth(22);
_remove->setProperty("themeID",
QVariant(QString::fromUtf8("removeIconSmall")));
_remove->setFlat(true);
_up->setMaximumWidth(22);
_up->setProperty("themeID",
QVariant(QString::fromUtf8("upArrowIconSmall")));
_up->setFlat(true);
_down->setMaximumWidth(22);
_down->setProperty("themeID",
QVariant(QString::fromUtf8("downArrowIconSmall")));
_down->setFlat(true);
QWidget::connect(_add, SIGNAL(clicked()), this, SLOT(Add()));
QWidget::connect(_remove, SIGNAL(clicked()), this, SLOT(Remove()));
QWidget::connect(_up, SIGNAL(clicked()), this, SLOT(Up()));
QWidget::connect(_down, SIGNAL(clicked()), this, SLOT(Down()));
QWidget::connect(_list, SIGNAL(itemDoubleClicked(QListWidgetItem *)),
this, SLOT(MacroItemClicked(QListWidgetItem *)));
QWidget::connect(window(),
SIGNAL(MacroRenamed(const QString &, const QString &)),
this,
SLOT(MacroRename(const QString &, const QString &)));
QWidget::connect(window(), SIGNAL(MacroRemoved(const QString &)), this,
SLOT(MacroRemove(const QString &)));
_controlsLayout->addWidget(_add);
_controlsLayout->addWidget(_remove);
if (reorder) {
QFrame *line = new QFrame();
line->setFrameShape(QFrame::VLine);
line->setFrameShadow(QFrame::Sunken);
_controlsLayout->addWidget(line);
}
_controlsLayout->addWidget(_up);
_controlsLayout->addWidget(_down);
_controlsLayout->addStretch();
auto mainLayout = new QVBoxLayout;
mainLayout->setContentsMargins(0, 0, 0, 0);
mainLayout->addWidget(_list);
mainLayout->addLayout(_controlsLayout);
setLayout(mainLayout);
_up->setVisible(reorder);
_down->setVisible(reorder);
SetMacroListSize();
UpdateListSize();
}
void MacroList::SetContent(const std::vector<MacroRef> &macros)
@ -89,7 +37,7 @@ void MacroList::SetContent(const std::vector<MacroRef> &macros)
new QListWidgetItem(listEntryName, _list);
item->setData(Qt::UserRole, listEntryName);
}
SetMacroListSize();
UpdateListSize();
}
void MacroList::AddControl(QWidget *widget)
@ -122,7 +70,7 @@ void MacroList::MacroRemove(const QString &name)
delete _list->item(idx);
idx = FindEntry(name.toStdString());
}
SetMacroListSize();
UpdateListSize();
}
void MacroList::Add()
@ -142,7 +90,7 @@ void MacroList::Add()
auto item =
new QListWidgetItem(QString::fromStdString(macroName), _list);
item->setData(Qt::UserRole, QString::fromStdString(macroName));
SetMacroListSize();
UpdateListSize();
emit Added(macroName);
}
@ -154,7 +102,7 @@ void MacroList::Remove()
return;
}
delete item;
SetMacroListSize();
UpdateListSize();
emit Removed(idx);
}
@ -178,7 +126,7 @@ void MacroList::Down()
}
}
void MacroList::MacroItemClicked(QListWidgetItem *item)
void MacroList::Clicked(QListWidgetItem *item)
{
std::string macroName;
bool accepted = MacroSelectionDialog::AskForMacro(this, macroName);
@ -216,10 +164,4 @@ int MacroList::FindEntry(const std::string &macro)
return idx;
}
void MacroList::SetMacroListSize()
{
SetHeightToContentHeight(_list);
adjustSize();
}
} // namespace advss

View File

@ -1,13 +1,10 @@
#pragma once
#include "list-editor.hpp"
#include "macro-ref.hpp"
#include <QListWidget>
#include <QPushButton>
#include <QHBoxLayout>
namespace advss {
class ADVSS_EXPORT MacroList : public QWidget {
class ADVSS_EXPORT MacroList final : public ListEditor {
Q_OBJECT
public:
MacroList(QWidget *parent, bool allowDuplicates, bool reorder);
@ -22,7 +19,7 @@ private slots:
void Remove();
void Up();
void Down();
void MacroItemClicked(QListWidgetItem *);
void Clicked(QListWidgetItem *);
signals:
void Added(const std::string &);
@ -33,16 +30,8 @@ signals:
private:
int FindEntry(const std::string &macro);
void SetMacroListSize();
QListWidget *_list;
QPushButton *_add;
QPushButton *_remove;
QPushButton *_up;
QPushButton *_down;
QHBoxLayout *_controlsLayout;
const bool _allowDuplicates;
const bool _reorder;
};
} // namespace advss

77
lib/utils/list-editor.cpp Normal file
View File

@ -0,0 +1,77 @@
#include "list-editor.hpp"
#include "ui-helpers.hpp"
namespace advss {
ListEditor::ListEditor(QWidget *parent, bool reorder)
: QWidget(parent),
_list(new QListWidget()),
_add(new QPushButton()),
_remove(new QPushButton()),
_up(new QPushButton()),
_down(new QPushButton()),
_controlsLayout(new QHBoxLayout()),
_mainLayout(new QVBoxLayout())
{
_add->setMaximumWidth(22);
_add->setProperty("themeID",
QVariant(QString::fromUtf8("addIconSmall")));
_add->setFlat(true);
_remove->setMaximumWidth(22);
_remove->setProperty("themeID",
QVariant(QString::fromUtf8("removeIconSmall")));
_remove->setFlat(true);
_up->setMaximumWidth(22);
_up->setProperty("themeID",
QVariant(QString::fromUtf8("upArrowIconSmall")));
_up->setFlat(true);
_down->setMaximumWidth(22);
_down->setProperty("themeID",
QVariant(QString::fromUtf8("downArrowIconSmall")));
_down->setFlat(true);
QWidget::connect(_add, SIGNAL(clicked()), this, SLOT(Add()));
QWidget::connect(_remove, SIGNAL(clicked()), this, SLOT(Remove()));
QWidget::connect(_up, SIGNAL(clicked()), this, SLOT(Up()));
QWidget::connect(_down, SIGNAL(clicked()), this, SLOT(Down()));
QWidget::connect(_list, SIGNAL(itemDoubleClicked(QListWidgetItem *)),
this, SLOT(Clicked(QListWidgetItem *)));
_controlsLayout->setContentsMargins(0, 0, 0, 0);
_controlsLayout->addWidget(_add);
_controlsLayout->addWidget(_remove);
if (reorder) {
auto line = new QFrame();
line->setFrameShape(QFrame::VLine);
line->setFrameShadow(QFrame::Sunken);
_controlsLayout->addWidget(line);
_controlsLayout->addWidget(_up);
_controlsLayout->addWidget(_down);
}
_controlsLayout->addStretch();
_mainLayout->setContentsMargins(0, 0, 0, 0);
_mainLayout->addWidget(_list);
_mainLayout->addLayout(_controlsLayout);
setLayout(_mainLayout);
}
void ListEditor::showEvent(QShowEvent *e)
{
QWidget::showEvent(e);
// This is necessary as the list viewport might not be updated yet while
// the list was hidden.
// Thus, previous calls to UpdateListSize() might not have resized the
// widget correctly, for example due to not regarding the horizontal
// scrollbar yet.
UpdateListSize();
}
void ListEditor::UpdateListSize()
{
SetHeightToContentHeight(_list);
adjustSize();
updateGeometry();
}
} // namespace advss

38
lib/utils/list-editor.hpp Normal file
View File

@ -0,0 +1,38 @@
#pragma once
#include "export-symbol-helper.hpp"
#include <QPushButton>
#include <QListWidget>
#include <QLayout>
namespace advss {
class ADVSS_EXPORT ListEditor : public QWidget {
Q_OBJECT
public:
ListEditor(QWidget *parent = nullptr, bool reorder = true);
protected:
void showEvent(QShowEvent *);
private slots:
virtual void Add() = 0;
virtual void Remove() = 0;
virtual void Up(){};
virtual void Down(){};
virtual void Clicked(QListWidgetItem *) {}
protected:
void UpdateListSize();
QListWidget *_list;
QPushButton *_add;
QPushButton *_remove;
QPushButton *_up;
QPushButton *_down;
QHBoxLayout *_controlsLayout;
QVBoxLayout *_mainLayout;
};
} // namespace advss

View File

@ -529,68 +529,26 @@ void OSCMessageElementEdit::TypeChanged(int idx)
}
OSCMessageEdit::OSCMessageEdit(QWidget *parent)
: QWidget(parent),
_address(new VariableLineEdit(this)),
_elements(new QListWidget()),
_add(new QPushButton()),
_remove(new QPushButton()),
_up(new QPushButton()),
_down(new QPushButton())
: ListEditor(parent),
_address(new VariableLineEdit(this))
{
_elements->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
_elements->setAutoScroll(false);
_add->setMaximumWidth(22);
_add->setProperty("themeID",
QVariant(QString::fromUtf8("addIconSmall")));
_add->setFlat(true);
_remove->setMaximumWidth(22);
_remove->setProperty("themeID",
QVariant(QString::fromUtf8("removeIconSmall")));
_remove->setFlat(true);
_up->setMaximumWidth(22);
_up->setProperty("themeID",
QVariant(QString::fromUtf8("upArrowIconSmall")));
_up->setFlat(true);
_down->setMaximumWidth(22);
_down->setProperty("themeID",
QVariant(QString::fromUtf8("downArrowIconSmall")));
_down->setFlat(true);
_list->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
_list->setAutoScroll(false);
QWidget::connect(_address, SIGNAL(editingFinished()), this,
SLOT(AddressChanged()));
QWidget::connect(_add, SIGNAL(clicked()), this, SLOT(Add()));
QWidget::connect(_remove, SIGNAL(clicked()), this, SLOT(Remove()));
QWidget::connect(_up, SIGNAL(clicked()), this, SLOT(Up()));
QWidget::connect(_down, SIGNAL(clicked()), this, SLOT(Down()));
auto controlsLayout = new QHBoxLayout();
controlsLayout->addWidget(_add);
controlsLayout->addWidget(_remove);
QFrame *line = new QFrame();
line->setFrameShape(QFrame::VLine);
line->setFrameShadow(QFrame::Sunken);
controlsLayout->addWidget(line);
controlsLayout->addWidget(_up);
controlsLayout->addWidget(_down);
controlsLayout->addStretch();
auto layout = new QVBoxLayout;
layout->setContentsMargins(0, 0, 0, 0);
layout->addWidget(_address);
layout->addWidget(_elements);
layout->addLayout(controlsLayout);
setLayout(layout);
_mainLayout->insertWidget(0, _address);
}
void OSCMessageEdit::InsertElement(const OSCMessageElement &element)
{
auto item = new QListWidgetItem(_elements);
_elements->addItem(item);
auto item = new QListWidgetItem(_list);
_list->addItem(item);
auto elementEdit = new OSCMessageElementEdit(this);
elementEdit->SetMessageElement(element);
item->setSizeHint(elementEdit->minimumSizeHint());
_elements->setItemWidget(item, elementEdit);
_list->setItemWidget(item, elementEdit);
QWidget::connect(elementEdit,
SIGNAL(ElementValueChanged(const OSCMessageElement &)),
this,
@ -607,7 +565,7 @@ void OSCMessageEdit::SetMessage(const OSCMessage &message)
InsertElement(element);
}
_currentSelection = message;
SetWidgetSize();
UpdateListSize();
}
void OSCMessageEdit::AddressChanged()
@ -621,13 +579,13 @@ void OSCMessageEdit::Add()
OSCMessageElement element;
InsertElement(element);
emit MessageChanged(_currentSelection);
SetWidgetSize();
UpdateListSize();
}
void OSCMessageEdit::Remove()
{
auto item = _elements->currentItem();
int idx = _elements->currentRow();
auto item = _list->currentItem();
int idx = _list->currentRow();
if (!item || idx == -1) {
return;
}
@ -635,7 +593,7 @@ void OSCMessageEdit::Remove()
_currentSelection._elements.erase(_currentSelection._elements.begin() +
idx);
emit MessageChanged(_currentSelection);
SetWidgetSize();
UpdateListSize();
}
static bool moveUp(QListWidget *list)
@ -658,8 +616,8 @@ static bool moveUp(QListWidget *list)
void OSCMessageEdit::Up()
{
int idx = _elements->currentRow();
if (!moveUp(_elements)) {
int idx = _list->currentRow();
if (!moveUp(_list)) {
return;
}
@ -667,7 +625,7 @@ void OSCMessageEdit::Up()
_currentSelection._elements.begin() + idx - 1);
emit MessageChanged(_currentSelection);
SetWidgetSize();
UpdateListSize();
}
static bool moveDown(QListWidget *list)
@ -690,8 +648,8 @@ static bool moveDown(QListWidget *list)
void OSCMessageEdit::Down()
{
int idx = _elements->currentRow();
if (!moveDown(_elements)) {
int idx = _list->currentRow();
if (!moveDown(_list)) {
return;
}
@ -699,7 +657,7 @@ void OSCMessageEdit::Down()
_currentSelection._elements.begin() + idx + 1);
emit MessageChanged(_currentSelection);
SetWidgetSize();
UpdateListSize();
}
static QListWidgetItem *getItemFromWidget(QListWidget *list, QWidget *widget)
@ -727,7 +685,7 @@ int OSCMessageEdit::GetIndexOfSignal()
if (!widget) {
return -1;
}
return _elements->row(getItemFromWidget(_elements, widget));
return _list->row(getItemFromWidget(_list, widget));
}
void OSCMessageEdit::ElementFocussed()
@ -736,7 +694,7 @@ void OSCMessageEdit::ElementFocussed()
if (idx == -1) {
return;
}
_elements->setCurrentRow(idx);
_list->setCurrentRow(idx);
}
void OSCMessageEdit::ElementValueChanged(const OSCMessageElement &element)
@ -747,15 +705,8 @@ void OSCMessageEdit::ElementValueChanged(const OSCMessageElement &element)
}
_currentSelection._elements.at(idx) = element;
_elements->setCurrentRow(idx);
_list->setCurrentRow(idx);
emit MessageChanged(_currentSelection);
}
void OSCMessageEdit::SetWidgetSize()
{
SetHeightToContentHeight(_elements);
adjustSize();
updateGeometry();
}
} // namespace advss

View File

@ -1,4 +1,5 @@
#pragma once
#include "list-editor.hpp"
#include "variable-string.hpp"
#include "variable-number.hpp"
#include "variable-line-edit.hpp"
@ -6,7 +7,6 @@
#include <variant>
#include <unordered_map>
#include <QListWidget>
namespace advss {
@ -137,7 +137,7 @@ private:
VariableLineEdit *_binaryText;
};
class OSCMessageEdit : public QWidget {
class OSCMessageEdit final : public ListEditor {
Q_OBJECT
public:
OSCMessageEdit(QWidget *);
@ -157,15 +157,9 @@ signals:
private:
void InsertElement(const OSCMessageElement &);
void SetWidgetSize();
int GetIndexOfSignal();
VariableLineEdit *_address;
QListWidget *_elements;
QPushButton *_add;
QPushButton *_remove;
QPushButton *_up;
QPushButton *_down;
OSCMessage _currentSelection;
};

View File

@ -49,60 +49,12 @@ void StringList::ResolveVariables()
StringListEdit::StringListEdit(QWidget *parent, const QString &addString,
const QString &addStringDescription,
int maxStringSize, bool allowEmtpy)
: QWidget(parent),
_list(new QListWidget()),
_add(new QPushButton()),
_remove(new QPushButton()),
_up(new QPushButton()),
_down(new QPushButton()),
: ListEditor(parent),
_addString(addString),
_addStringDescription(addStringDescription),
_maxStringSize(maxStringSize),
_allowEmpty(allowEmtpy)
{
_add->setMaximumWidth(22);
_add->setProperty("themeID",
QVariant(QString::fromUtf8("addIconSmall")));
_add->setFlat(true);
_remove->setMaximumWidth(22);
_remove->setProperty("themeID",
QVariant(QString::fromUtf8("removeIconSmall")));
_remove->setFlat(true);
_up->setMaximumWidth(22);
_up->setProperty("themeID",
QVariant(QString::fromUtf8("upArrowIconSmall")));
_up->setFlat(true);
_down->setMaximumWidth(22);
_down->setProperty("themeID",
QVariant(QString::fromUtf8("downArrowIconSmall")));
_down->setFlat(true);
QWidget::connect(_add, SIGNAL(clicked()), this, SLOT(Add()));
QWidget::connect(_remove, SIGNAL(clicked()), this, SLOT(Remove()));
QWidget::connect(_up, SIGNAL(clicked()), this, SLOT(Up()));
QWidget::connect(_down, SIGNAL(clicked()), this, SLOT(Down()));
QWidget::connect(_list, SIGNAL(itemDoubleClicked(QListWidgetItem *)),
this, SLOT(Clicked(QListWidgetItem *)));
auto controlLayout = new QHBoxLayout;
controlLayout->setContentsMargins(0, 0, 0, 0);
controlLayout->addWidget(_add);
controlLayout->addWidget(_remove);
QFrame *line = new QFrame();
line->setFrameShape(QFrame::VLine);
line->setFrameShadow(QFrame::Sunken);
controlLayout->addWidget(line);
controlLayout->addWidget(_up);
controlLayout->addWidget(_down);
controlLayout->addStretch();
auto mainLayout = new QVBoxLayout;
mainLayout->setContentsMargins(0, 0, 0, 0);
mainLayout->addWidget(_list);
mainLayout->addLayout(controlLayout);
setLayout(mainLayout);
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Minimum);
}
void StringListEdit::SetStringList(const StringList &list)
@ -115,7 +67,7 @@ void StringListEdit::SetStringList(const StringList &list)
_list);
item->setData(Qt::UserRole, string);
}
SetListSize();
UpdateListSize();
}
void StringListEdit::SetMaxStringSize(int size)
@ -123,17 +75,6 @@ void StringListEdit::SetMaxStringSize(int size)
_maxStringSize = size;
}
void StringListEdit::showEvent(QShowEvent *e)
{
QWidget::showEvent(e);
// This is necessary as the list viewport might not be updated yet
// while the list was hidden.
// Thus, previous calls to SetListSize() might not have resized the
// widget correctly, for example due to not regarding the horizontal
// scrollbar yet.
SetListSize();
}
void StringListEdit::Add()
{
std::string name;
@ -153,7 +94,7 @@ void StringListEdit::Add()
_stringList << string;
// Delay resizing to make sure the list viewport was already updated
QTimer::singleShot(0, this, [this]() { SetListSize(); });
QTimer::singleShot(0, this, [this]() { UpdateListSize(); });
StringListChanged(_stringList);
}
@ -173,7 +114,7 @@ void StringListEdit::Remove()
delete item;
// Delay resizing to make sure the list viewport was already updated
QTimer::singleShot(0, this, [this]() { SetListSize(); });
QTimer::singleShot(0, this, [this]() { UpdateListSize(); });
StringListChanged(_stringList);
}
@ -222,16 +163,9 @@ void StringListEdit::Clicked(QListWidgetItem *item)
_stringList[idx] = string;
// Delay resizing to make sure the list viewport was already updated
QTimer::singleShot(0, this, [this]() { SetListSize(); });
QTimer::singleShot(0, this, [this]() { UpdateListSize(); });
StringListChanged(_stringList);
}
void StringListEdit::SetListSize()
{
SetHeightToContentHeight(_list);
adjustSize();
updateGeometry();
}
} // namespace advss

View File

@ -1,13 +1,10 @@
#pragma once
#include "variable-string.hpp"
#include "list-editor.hpp"
#include "obs-module-helper.hpp"
#include <obs-data.h>
#include <QPushButton>
#include <QListWidget>
#include <QStringList>
namespace advss {
class StringList : public QList<StringVariable> {
@ -21,7 +18,7 @@ public:
friend class StringListEdit;
};
class StringListEdit : public QWidget {
class StringListEdit final : public ListEditor {
Q_OBJECT
public:
@ -31,9 +28,6 @@ public:
void SetStringList(const StringList &);
void SetMaxStringSize(int);
protected:
void showEvent(QShowEvent *);
private slots:
void Add();
void Remove();
@ -44,16 +38,8 @@ signals:
void StringListChanged(const StringList &);
private:
void SetListSize();
StringList _stringList;
QListWidget *_list;
QPushButton *_add;
QPushButton *_remove;
QPushButton *_up;
QPushButton *_down;
QString _addString;
QString _addStringDescription;
int _maxStringSize = 170;