window-basic-properties.cpp 7.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/******************************************************************************
    Copyright (C) 2014 by Hugh Bailey <obs.jim@gmail.com>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/

#include "obs-app.hpp"
#include "window-basic-properties.hpp"
#include "window-basic-main.hpp"
#include "qt-wrappers.hpp"
#include "display-helpers.hpp"

24
#include <QCloseEvent>
25 26
#include <QScreen>
#include <QWindow>
27
#include <QMessageBox>
28

29 30 31
using namespace std;

OBSBasicProperties::OBSBasicProperties(QWidget *parent, OBSSource source_)
32 33 34
	: QDialog                (parent),
	  main                   (qobject_cast<OBSBasic*>(parent)),
	  resizeTimer            (0),
35
	  acceptClicked          (false),
36 37 38 39 40
	  ui                     (new Ui::OBSBasicProperties),
	  source                 (source_),
	  removedSignal          (obs_source_get_signal_handler(source),
	                          "remove", OBSBasicProperties::SourceRemoved,
	                          this),
J
jp9000 已提交
41 42
	  oldSettings            (obs_data_create()),
	  buttonBox              (new QDialogButtonBox(this))
43
{
44 45 46 47 48
	int cx = (int)config_get_int(App()->GlobalConfig(), "PropertiesWindow",
			"cx");
	int cy = (int)config_get_int(App()->GlobalConfig(), "PropertiesWindow",
			"cy");

49 50 51 52
	buttonBox->setStandardButtons(QDialogButtonBox::Ok |
			QDialogButtonBox::Cancel);
	buttonBox->setObjectName(QStringLiteral("buttonBox"));

53 54
	ui->setupUi(this);

55 56 57
	if (cx > 400 && cy > 400)
		resize(cx, cy);

58 59 60
	/* The OBSData constructor increments the reference once */
	obs_data_release(oldSettings);

61
	OBSData settings = obs_source_get_settings(source);
62
	obs_data_apply(oldSettings, settings);
63 64
	obs_data_release(settings);

65 66
	view = new OBSPropertiesView(settings, source,
			(PropertiesReloadCallback)obs_source_properties,
67
			(PropertiesUpdateCallback)obs_source_update);
68 69

	layout()->addWidget(view);
70 71
	layout()->addWidget(buttonBox);
	layout()->setAlignment(buttonBox, Qt::AlignRight | Qt::AlignBottom);
72
	layout()->setAlignment(view, Qt::AlignBottom);
73
	view->setMaximumHeight(250);
74
	view->setMinimumHeight(150);
75
	view->show();
76

77 78 79
	connect(view, SIGNAL(PropertiesResized()),
			this, SLOT(OnPropertiesResized()));

80 81 82 83 84
	connect(windowHandle(), &QWindow::screenChanged, [this]() {
		if (resizeTimer)
			killTimer(resizeTimer);
		resizeTimer = startTimer(100);
	});
J
jp9000 已提交
85

86
	const char *name = obs_source_get_name(source);
J
jp9000 已提交
87
	setWindowTitle(QTStr("Basic.PropertiesWindow").arg(QT_UTF8(name)));
88 89

	obs_source_inc_showing(source);
J
jp9000 已提交
90 91 92 93 94

	updatePropertiesSignal.Connect(obs_source_get_signal_handler(source),
			"update_properties",
			OBSBasicProperties::UpdateProperties,
			this);
95 96 97 98 99
}

OBSBasicProperties::~OBSBasicProperties()
{
	obs_source_dec_showing(source);
100 101
}

102
void OBSBasicProperties::SourceRemoved(void *data, calldata_t *params)
103 104 105 106 107 108 109
{
	QMetaObject::invokeMethod(static_cast<OBSBasicProperties*>(data),
			"close");

	UNUSED_PARAMETER(params);
}

110 111 112 113 114 115
void OBSBasicProperties::UpdateProperties(void *data, calldata_t *)
{
	QMetaObject::invokeMethod(static_cast<OBSBasicProperties*>(data)->view,
			"ReloadProperties");
}

116 117 118 119 120 121 122
void OBSBasicProperties::on_buttonBox_clicked(QAbstractButton *button)
{
	QDialogButtonBox::ButtonRole val = buttonBox->buttonRole(button);

	if (val == QDialogButtonBox::AcceptRole) {
		acceptClicked = true;
		close();
123 124 125

		if (view->DeferUpdate())
			view->UpdateSettings();
126 127 128
	}

	if (val == QDialogButtonBox::RejectRole) {
129 130 131 132
		obs_data_t *settings = obs_source_get_settings(source);
		obs_data_clear(settings);
		obs_data_release(settings);

133
		obs_source_update(source, oldSettings);
134

135 136 137 138
		close();
	}
}

139 140 141 142 143 144 145
void OBSBasicProperties::DrawPreview(void *data, uint32_t cx, uint32_t cy)
{
	OBSBasicProperties *window = static_cast<OBSBasicProperties*>(data);

	if (!window->source)
		return;

146 147
	uint32_t sourceCX = max(obs_source_get_width(window->source), 1u);
	uint32_t sourceCY = max(obs_source_get_height(window->source), 1u);
148 149

	int   x, y;
150
	int   newCX, newCY;
151 152 153 154
	float scale;

	GetScaleAndCenterPos(sourceCX, sourceCY, cx, cy, x, y, scale);

155 156 157 158 159 160 161
	newCX = int(scale * float(sourceCX));
	newCY = int(scale * float(sourceCY));

	gs_viewport_push();
	gs_projection_push();
	gs_ortho(0.0f, float(sourceCX), 0.0f, float(sourceCY),
			-100.0f, 100.0f);
162
	gs_set_viewport(x, y, newCX, newCY);
163

164
	obs_source_video_render(window->source);
165 166 167

	gs_projection_pop();
	gs_viewport_pop();
168 169
}

170 171 172 173 174 175 176
void OBSBasicProperties::OnPropertiesResized()
{
	if (resizeTimer)
		killTimer(resizeTimer);
	resizeTimer = startTimer(100);
}

177 178 179 180 181 182 183 184
void OBSBasicProperties::resizeEvent(QResizeEvent *event)
{
	if (isVisible()) {
		if (resizeTimer)
			killTimer(resizeTimer);
		resizeTimer = startTimer(100);
	}

185
	QDialog::resizeEvent(event);
186 187 188 189 190 191 192 193
}

void OBSBasicProperties::timerEvent(QTimerEvent *event)
{
	if (event->timerId() == resizeTimer) {
		killTimer(resizeTimer);
		resizeTimer = 0;

194
		QSize size = GetPixelSize(ui->preview);
195 196 197 198
		obs_display_resize(display, size.width(), size.height());
	}
}

199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
void OBSBasicProperties::Cleanup()
{
	// remove draw callback and release display in case our drawable
	// surfaces go away before the destructor gets called
	obs_display_remove_draw_callback(display,
			OBSBasicProperties::DrawPreview, this);
	display = nullptr;

	config_set_int(App()->GlobalConfig(), "PropertiesWindow", "cx",
			width());
	config_set_int(App()->GlobalConfig(), "PropertiesWindow", "cy",
			height());
}

void OBSBasicProperties::reject()
{
	if (!acceptClicked && (CheckSettings() != 0)) {
		if (!ConfirmQuit()) {
			return;
		}
	}

	Cleanup();
	done(0);
}

225 226
void OBSBasicProperties::closeEvent(QCloseEvent *event)
{
227 228 229 230 231 232 233
	if (!acceptClicked && (CheckSettings() != 0)) {
		if (!ConfirmQuit()) {
			event->ignore();
			return;
		}
	}

234 235 236 237
	QDialog::closeEvent(event);
	if (!event->isAccepted())
		return;

238
	Cleanup();
239 240
}

241 242 243 244 245 246
void OBSBasicProperties::Init()
{
	gs_init_data init_data = {};

	show();

247
	QSize previewSize = GetPixelSize(ui->preview);
248 249 250 251 252 253 254 255 256 257 258
	init_data.cx      = uint32_t(previewSize.width());
	init_data.cy      = uint32_t(previewSize.height());
	init_data.format  = GS_RGBA;
	QTToGSWindow(ui->preview->winId(), init_data.window);

	display = obs_display_create(&init_data);

	if (display)
		obs_display_add_draw_callback(display,
				OBSBasicProperties::DrawPreview, this);
}
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283

int OBSBasicProperties::CheckSettings()
{
	OBSData currentSettings = obs_source_get_settings(source);
	const char *oldSettingsJson = obs_data_get_json(oldSettings);
	const char *currentSettingsJson = obs_data_get_json(currentSettings);

	int ret = strcmp(currentSettingsJson, oldSettingsJson);

	obs_data_release(currentSettings);
	return ret;
}

bool OBSBasicProperties::ConfirmQuit()
{
	QMessageBox::StandardButton button;

	button = QMessageBox::question(this,
			QTStr("Basic.PropertiesWindow.ConfirmTitle"),
			QTStr("Basic.PropertiesWindow.Confirm"),
			QMessageBox::Save | QMessageBox::Discard |
			QMessageBox::Cancel);

	switch (button) {
	case QMessageBox::Save:
284 285
		if (view->DeferUpdate())
			view->UpdateSettings();
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
		// Do nothing because the settings are already updated
		break;
	case QMessageBox::Discard:
		obs_source_update(source, oldSettings);
		break;
	case QMessageBox::Cancel:
		return false;
		break;
	default:
		/* If somehow the dialog fails to show, just default to
		 * saving the settings. */
		break;
	}
	return true;
}