Skip to content

Commit

Permalink
resize edit dialog to proper size.
Browse files Browse the repository at this point in the history
  • Loading branch information
sorayuki committed Mar 2, 2024
1 parent faeb686 commit 02b2d30
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 10 deletions.
80 changes: 71 additions & 9 deletions src/edit-widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include "obs.hpp"
#include <QMenu>
#include <QTabWidget>
#include <QScrollBar>
#include <qevent.h>

#include "obs-properties-widget.h"

Expand Down Expand Up @@ -55,10 +57,24 @@ static obs_properties* AddBF(obs_properties* p) {
}


template<class T>
class EventFilter: public QObject {
T lambda_;
public:
bool eventFilter(QObject *watched, QEvent *event) override {
return lambda_(watched, event);
}

EventFilter(QObject* parent, T&& lambda): lambda_(std::move(lambda)) {
setParent(parent);
}
};


class EditOutputWidgetImpl: public EditOutputWidget
{
QWidget* container_;
QScrollArea scroll_;
QScrollArea* scroll_;

std::string targetid_;
OutputTargetConfigPtr config_ = nullptr;
Expand Down Expand Up @@ -278,17 +294,24 @@ class EditOutputWidgetImpl: public EditOutputWidget
: QDialog(parent)
, targetid_(targetid)
{
container_ = new QWidget(this);

setWindowTitle(obs_module_text("StreamingSettings"));

auto& global = GlobalMultiOutputConfig();
config_ = FindById(global.targets, targetid_);
if (config_ == nullptr) {
return;
}
config_ = std::make_shared<OutputTargetConfig>(*config_);

setWindowTitle(obs_module_text("StreamingSettings"));

scroll_ = new QScrollArea(this);
scroll_->setHorizontalScrollBarPolicy(Qt::ScrollBarPolicy::ScrollBarAsNeeded);
scroll_->setVerticalScrollBarPolicy(Qt::ScrollBarPolicy::ScrollBarAsNeeded);
scroll_->setSizePolicy(QSizePolicy::Policy::Expanding, QSizePolicy::Policy::Expanding);
scroll_->setSizeAdjustPolicy(QScrollArea::SizeAdjustPolicy::AdjustToContents);

container_ = new QWidget(scroll_);
container_->setSizePolicy(QSizePolicy::Policy::Expanding, QSizePolicy::Policy::Expanding);

auto layout = new QVBoxLayout(container_);

int currow = 0;
Expand Down Expand Up @@ -433,14 +456,14 @@ class EditOutputWidgetImpl: public EditOutputWidget
layout->setSizeConstraint(QLayout::SetFixedSize);
container_->setLayout(layout);

scroll_.setWidget(container_);
// scroll_.setWidgetResizable(true);
scroll_->setWidget(container_);
scroll_->setWidgetResizable(true);

auto fullLayout = new QGridLayout(this);
fullLayout->addWidget(container_, 0, 0);
fullLayout->setContentsMargins(0, 0, 0, 0);
fullLayout->addWidget(scroll_, 0, 0);
fullLayout->setRowStretch(0, 1);
fullLayout->setColumnStretch(0, 1);
layout->setSizeConstraint(QLayout::SetFixedSize);
setLayout(fullLayout);

LoadFPSDenumerator();
Expand All @@ -450,6 +473,45 @@ class EditOutputWidgetImpl: public EditOutputWidget
LoadConfig();
ConnectWidgetSignals();
UpdateUI();

auto resizeCount = std::make_shared<int>(0);
container_->installEventFilter(new EventFilter(container_, [this, resizeCount](QObject* watched, QEvent* ev) {
if (watched == container_ && ev->type() == QEvent::Resize) {
++*resizeCount;
if (*resizeCount == 2) { // why 2? I don't know
auto frameGeo = frameGeometry();
auto clientGeo = geometry();

auto sizehint = container_->layout()->sizeHint();
// add frame size
sizehint = sizehint.grownBy(QMargins(
frameGeo.width() - clientGeo.width(),
frameGeo.height() - clientGeo.height(),
0, 0
));
auto vs = scroll_->verticalScrollBar();
auto hs = scroll_->horizontalScrollBar();
sizehint = sizehint.grownBy(QMargins(
vs ? vs->width() / 2 : 0, hs ? hs->height() / 2 : 0,
vs ? vs->width() / 2 : 0, hs ? hs->height() / 2 : 0
));
auto parentCenter = parentWidget()->geometry().center();
QRect g;
g.setSize(sizehint);
g.moveCenter(parentCenter);
auto avail = screen()->availableGeometry();
g = avail.intersected(g);

// remove frame size
g.setTop(g.top() + (clientGeo.top() - frameGeo.top()));
g.setBottom(g.bottom() - (frameGeo.bottom() - clientGeo.bottom()));
g.setLeft(g.left() + (clientGeo.left() - frameGeo.left()));
g.setRight(g.right() - (frameGeo.right() - clientGeo.right()));
setGeometry(g);
}
}
return false;
}));
}

void ConnectWidgetSignals()
Expand Down
2 changes: 2 additions & 0 deletions src/obs-properties-widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ namespace {

layout->setContentsMargins(0, 0, 0, 0);
setLayout(layout);

setSizePolicy(QSizePolicy::Policy::Expanding, QSizePolicy::Policy::Fixed);
}

QLineEdit* edit() const { return edit_; }
Expand Down
2 changes: 1 addition & 1 deletion src/push-widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,7 @@ class PushWidgetImpl : public PushWidget, public IOBSOutputEventHanlder

bool ShowEditDlg() override
{
std::unique_ptr<EditOutputWidget> dlg{ createEditOutputWidget(targetid_, this) };
std::unique_ptr<EditOutputWidget> dlg{ createEditOutputWidget(targetid_, (QMainWindow*)obs_frontend_get_main_window()) };

if (dlg->exec() == QDialog::DialogCode::Accepted)
{
Expand Down

0 comments on commit 02b2d30

Please sign in to comment.