window-basic-properties.cpp 7.4 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
	OBSData settings = obs_source_get_settings(source);
59
	obs_data_apply(oldSettings, settings);
60 61
	obs_data_release(settings);

62 63
	view = new OBSPropertiesView(settings, source,
			(PropertiesReloadCallback)obs_source_properties,
64
			(PropertiesUpdateCallback)obs_source_update);
65 66

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

74 75 76
	connect(view, SIGNAL(PropertiesResized()),
			this, SLOT(OnPropertiesResized()));

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

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

	obs_source_inc_showing(source);
J
jp9000 已提交
87 88 89 90 91

	updatePropertiesSignal.Connect(obs_source_get_signal_handler(source),
			"update_properties",
			OBSBasicProperties::UpdateProperties,
			this);
92 93 94 95 96
}

OBSBasicProperties::~OBSBasicProperties()
{
	obs_source_dec_showing(source);
97 98
}

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

	UNUSED_PARAMETER(params);
}

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

113 114 115 116 117 118 119
void OBSBasicProperties::on_buttonBox_clicked(QAbstractButton *button)
{
	QDialogButtonBox::ButtonRole val = buttonBox->buttonRole(button);

	if (val == QDialogButtonBox::AcceptRole) {
		acceptClicked = true;
		close();
120 121 122

		if (view->DeferUpdate())
			view->UpdateSettings();
123 124 125
	}

	if (val == QDialogButtonBox::RejectRole) {
126 127 128 129
		obs_data_t *settings = obs_source_get_settings(source);
		obs_data_clear(settings);
		obs_data_release(settings);

130
		obs_source_update(source, oldSettings);
131

132 133 134 135
		close();
	}
}

136 137 138 139 140 141 142
void OBSBasicProperties::DrawPreview(void *data, uint32_t cx, uint32_t cy)
{
	OBSBasicProperties *window = static_cast<OBSBasicProperties*>(data);

	if (!window->source)
		return;

143 144
	uint32_t sourceCX = max(obs_source_get_width(window->source), 1u);
	uint32_t sourceCY = max(obs_source_get_height(window->source), 1u);
145 146

	int   x, y;
147
	int   newCX, newCY;
148 149 150 151
	float scale;

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

152 153 154 155 156 157 158
	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);
159
	gs_set_viewport(x, y, newCX, newCY);
160

161
	obs_source_video_render(window->source);
162 163 164

	gs_projection_pop();
	gs_viewport_pop();
165 166
}

167 168 169 170 171 172 173
void OBSBasicProperties::OnPropertiesResized()
{
	if (resizeTimer)
		killTimer(resizeTimer);
	resizeTimer = startTimer(100);
}

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

182
	QDialog::resizeEvent(event);
183 184 185 186 187 188 189 190
}

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

191
		QSize size = GetPixelSize(ui->preview);
192 193 194 195
		obs_display_resize(display, size.width(), size.height());
	}
}

196 197
void OBSBasicProperties::closeEvent(QCloseEvent *event)
{
198 199 200 201 202 203 204
	if (!acceptClicked && (CheckSettings() != 0)) {
		if (!ConfirmQuit()) {
			event->ignore();
			return;
		}
	}

205 206 207 208
	QDialog::closeEvent(event);
	if (!event->isAccepted())
		return;

209 210
	obs_data_release(oldSettings);

211 212
	// remove draw callback and release display in case our drawable
	// surfaces go away before the destructor gets called
213 214
	obs_display_remove_draw_callback(display,
			OBSBasicProperties::DrawPreview, this);
215
	display = nullptr;
216 217 218 219 220

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

223 224 225 226 227 228
void OBSBasicProperties::Init()
{
	gs_init_data init_data = {};

	show();

229
	QSize previewSize = GetPixelSize(ui->preview);
230 231 232 233 234 235 236 237 238 239 240
	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);
}
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265

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:
266 267
		if (view->DeferUpdate())
			view->UpdateSettings();
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
		// 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;
}