window-basic-properties.cpp 7.6 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
/******************************************************************************
    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"
23
#include "properties-view.hpp"
24

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

30 31 32
using namespace std;

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

52 53 54 55
	buttonBox->setStandardButtons(QDialogButtonBox::Ok |
			QDialogButtonBox::Cancel);
	buttonBox->setObjectName(QStringLiteral("buttonBox"));

56 57
	if (cx > 400 && cy > 400)
		resize(cx, cy);
58 59
	else
		resize(720, 580);
60

61 62
	QMetaObject::connectSlotsByName(this);

63 64 65
	/* The OBSData constructor increments the reference once */
	obs_data_release(oldSettings);

66
	OBSData settings = obs_source_get_settings(source);
67
	obs_data_apply(oldSettings, settings);
68 69
	obs_data_release(settings);

70 71
	view = new OBSPropertiesView(settings, source,
			(PropertiesReloadCallback)obs_source_properties,
72
			(PropertiesUpdateCallback)obs_source_update);
73

74 75 76 77 78 79
	preview->setMinimumSize(20, 20);
	preview->setSizePolicy(QSizePolicy(QSizePolicy::Expanding,
				QSizePolicy::Expanding));

	setLayout(new QVBoxLayout(this));
	layout()->addWidget(preview);
80
	layout()->addWidget(view);
81 82
	layout()->addWidget(buttonBox);
	layout()->setAlignment(buttonBox, Qt::AlignRight | Qt::AlignBottom);
83
	layout()->setAlignment(view, Qt::AlignBottom);
84
	view->setMaximumHeight(250);
85
	view->setMinimumHeight(150);
86
	view->show();
87

P
Palana 已提交
88 89
	installEventFilter(CreateShortcutFilter());

90
	const char *name = obs_source_get_name(source);
J
jp9000 已提交
91
	setWindowTitle(QTStr("Basic.PropertiesWindow").arg(QT_UTF8(name)));
92 93

	obs_source_inc_showing(source);
J
jp9000 已提交
94 95 96 97 98

	updatePropertiesSignal.Connect(obs_source_get_signal_handler(source),
			"update_properties",
			OBSBasicProperties::UpdateProperties,
			this);
99 100 101 102 103 104 105

	auto addDrawCallback = [this] ()
	{
		obs_display_add_draw_callback(preview->GetDisplay(),
				OBSBasicProperties::DrawPreview, this);
	};

106 107 108 109 110 111 112 113
	enum obs_source_type type = obs_source_get_type(source);
	uint32_t caps = obs_source_get_output_flags(source);
	bool drawable_type = type == OBS_SOURCE_TYPE_INPUT ||
		type == OBS_SOURCE_TYPE_SCENE;

	if (drawable_type && (caps & OBS_SOURCE_VIDEO) != 0)
		connect(preview.data(), &OBSQTDisplay::DisplayCreated,
				addDrawCallback);
114 115 116 117 118
}

OBSBasicProperties::~OBSBasicProperties()
{
	obs_source_dec_showing(source);
J
jp9000 已提交
119
	main->SaveProject();
120 121
}

122
void OBSBasicProperties::SourceRemoved(void *data, calldata_t *params)
123 124 125 126 127 128 129
{
	QMetaObject::invokeMethod(static_cast<OBSBasicProperties*>(data),
			"close");

	UNUSED_PARAMETER(params);
}

130 131 132 133 134 135 136 137 138
void OBSBasicProperties::SourceRenamed(void *data, calldata_t *params)
{
	const char *name = calldata_string(params, "new_name");
	QString title = QTStr("Basic.PropertiesWindow").arg(QT_UTF8(name));

	QMetaObject::invokeMethod(static_cast<OBSBasicProperties*>(data),
	                "setWindowTitle", Q_ARG(QString, title));
}

139 140 141 142 143 144
void OBSBasicProperties::UpdateProperties(void *data, calldata_t *)
{
	QMetaObject::invokeMethod(static_cast<OBSBasicProperties*>(data)->view,
			"ReloadProperties");
}

145 146 147 148 149 150 151
void OBSBasicProperties::on_buttonBox_clicked(QAbstractButton *button)
{
	QDialogButtonBox::ButtonRole val = buttonBox->buttonRole(button);

	if (val == QDialogButtonBox::AcceptRole) {
		acceptClicked = true;
		close();
152 153 154

		if (view->DeferUpdate())
			view->UpdateSettings();
155 156 157
	}

	if (val == QDialogButtonBox::RejectRole) {
158 159 160 161
		obs_data_t *settings = obs_source_get_settings(source);
		obs_data_clear(settings);
		obs_data_release(settings);

J
jp9000 已提交
162 163 164 165
		if (view->DeferUpdate())
			obs_data_apply(settings, oldSettings);
		else
			obs_source_update(source, oldSettings);
166

167 168 169 170
		close();
	}
}

171 172 173 174 175 176 177
void OBSBasicProperties::DrawPreview(void *data, uint32_t cx, uint32_t cy)
{
	OBSBasicProperties *window = static_cast<OBSBasicProperties*>(data);

	if (!window->source)
		return;

178 179
	uint32_t sourceCX = max(obs_source_get_width(window->source), 1u);
	uint32_t sourceCY = max(obs_source_get_height(window->source), 1u);
180 181

	int   x, y;
182
	int   newCX, newCY;
183 184 185 186
	float scale;

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

187 188 189 190 191 192 193
	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);
194
	gs_set_viewport(x, y, newCX, newCY);
195

196
	obs_source_video_render(window->source);
197 198 199

	gs_projection_pop();
	gs_viewport_pop();
200 201
}

202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
void OBSBasicProperties::Cleanup()
{
	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);
}

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

231 232 233 234
	QDialog::closeEvent(event);
	if (!event->isAccepted())
		return;

235
	Cleanup();
236 237
}

238 239 240 241
void OBSBasicProperties::Init()
{
	show();
}
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266

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