2014-05-10 18:47:48 -07:00
|
|
|
/******************************************************************************
|
|
|
|
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/>.
|
|
|
|
******************************************************************************/
|
|
|
|
|
2014-05-12 15:32:55 -07:00
|
|
|
#include <QMessageBox>
|
2014-05-10 18:47:48 -07:00
|
|
|
#include "window-basic-main.hpp"
|
|
|
|
#include "window-basic-source-select.hpp"
|
|
|
|
#include "qt-wrappers.hpp"
|
2014-05-09 15:09:17 -07:00
|
|
|
#include "obs-app.hpp"
|
2014-05-10 18:47:48 -07:00
|
|
|
|
|
|
|
bool OBSBasicSourceSelect::EnumSources(void *data, obs_source_t source)
|
|
|
|
{
|
|
|
|
OBSBasicSourceSelect *window = static_cast<OBSBasicSourceSelect*>(data);
|
|
|
|
const char *name = obs_source_getname(source);
|
2014-08-02 12:42:47 -07:00
|
|
|
const char *id = obs_source_get_id(source);
|
2014-05-10 18:47:48 -07:00
|
|
|
|
2014-08-02 12:38:55 -07:00
|
|
|
if (strcmp(id, window->id) == 0)
|
2014-05-10 18:47:48 -07:00
|
|
|
window->ui->sourceList->addItem(QT_UTF8(name));
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void OBSBasicSourceSelect::OBSSourceAdded(void *data, calldata_t calldata)
|
|
|
|
{
|
|
|
|
OBSBasicSourceSelect *window = static_cast<OBSBasicSourceSelect*>(data);
|
|
|
|
obs_source_t source = (obs_source_t)calldata_ptr(calldata, "source");
|
|
|
|
|
|
|
|
QMetaObject::invokeMethod(window, "SourceAdded",
|
|
|
|
Q_ARG(OBSSource, source));
|
|
|
|
}
|
|
|
|
|
|
|
|
void OBSBasicSourceSelect::OBSSourceRemoved(void *data, calldata_t calldata)
|
|
|
|
{
|
|
|
|
OBSBasicSourceSelect *window = static_cast<OBSBasicSourceSelect*>(data);
|
|
|
|
obs_source_t source = (obs_source_t)calldata_ptr(calldata, "source");
|
|
|
|
|
|
|
|
QMetaObject::invokeMethod(window, "SourceRemoved",
|
|
|
|
Q_ARG(OBSSource, source));
|
|
|
|
}
|
|
|
|
|
|
|
|
void OBSBasicSourceSelect::SourceAdded(OBSSource source)
|
|
|
|
{
|
2014-08-02 12:42:47 -07:00
|
|
|
const char *name = obs_source_getname(source);
|
|
|
|
const char *sourceId = obs_source_get_id(source);
|
2014-05-10 18:47:48 -07:00
|
|
|
|
2014-08-02 12:38:55 -07:00
|
|
|
if (strcmp(sourceId, id) != 0)
|
2014-05-10 18:47:48 -07:00
|
|
|
return;
|
|
|
|
|
|
|
|
ui->sourceList->addItem(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
void OBSBasicSourceSelect::SourceRemoved(OBSSource source)
|
|
|
|
{
|
2014-08-02 12:42:47 -07:00
|
|
|
const char *name = obs_source_getname(source);
|
|
|
|
const char *sourceId = obs_source_get_id(source);
|
2014-05-10 18:47:48 -07:00
|
|
|
|
2014-08-02 12:38:55 -07:00
|
|
|
if (strcmp(sourceId, id) != 0)
|
2014-05-10 18:47:48 -07:00
|
|
|
return;
|
|
|
|
|
|
|
|
QList<QListWidgetItem*> items =
|
|
|
|
ui->sourceList->findItems(name, Qt::MatchFixedString);
|
|
|
|
|
|
|
|
if (!items.count())
|
|
|
|
return;
|
|
|
|
|
|
|
|
delete items[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
static void AddExisting(const char *name)
|
|
|
|
{
|
|
|
|
obs_source_t source = obs_get_output_source(0);
|
|
|
|
obs_scene_t scene = obs_scene_fromsource(source);
|
|
|
|
if (!scene)
|
|
|
|
return;
|
|
|
|
|
|
|
|
source = obs_get_source_by_name(name);
|
|
|
|
if (source) {
|
|
|
|
obs_scene_add(scene, source);
|
|
|
|
obs_source_release(source);
|
|
|
|
}
|
|
|
|
|
|
|
|
obs_scene_release(scene);
|
|
|
|
}
|
|
|
|
|
2014-05-12 15:32:55 -07:00
|
|
|
bool AddNew(QWidget *parent, const char *id, const char *name)
|
2014-05-10 18:47:48 -07:00
|
|
|
{
|
2014-05-12 15:32:55 -07:00
|
|
|
obs_source_t source = obs_get_output_source(0);
|
|
|
|
obs_scene_t scene = obs_scene_fromsource(source);
|
|
|
|
bool success = false;
|
|
|
|
if (!source)
|
|
|
|
return false;
|
2014-05-10 18:47:48 -07:00
|
|
|
|
2014-05-12 15:32:55 -07:00
|
|
|
source = obs_get_source_by_name(name);
|
|
|
|
if (source) {
|
|
|
|
QMessageBox::information(parent,
|
|
|
|
QTStr("NameExists.Title"),
|
|
|
|
QTStr("NameExists.Text"));
|
2014-05-10 18:47:48 -07:00
|
|
|
|
2014-05-12 15:32:55 -07:00
|
|
|
} else {
|
|
|
|
source = obs_source_create(OBS_SOURCE_TYPE_INPUT,
|
|
|
|
id, name, NULL);
|
2014-05-10 18:47:48 -07:00
|
|
|
|
2014-05-12 15:32:55 -07:00
|
|
|
if (source) {
|
|
|
|
obs_add_source(source);
|
|
|
|
obs_scene_add(scene, source);
|
|
|
|
|
|
|
|
success = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
obs_source_release(source);
|
2014-05-10 18:47:48 -07:00
|
|
|
obs_scene_release(scene);
|
2014-05-12 15:32:55 -07:00
|
|
|
|
|
|
|
return success;
|
2014-05-10 18:47:48 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void OBSBasicSourceSelect::on_buttonBox_accepted()
|
|
|
|
{
|
|
|
|
bool useExisting = ui->selectExisting->isChecked();
|
|
|
|
|
|
|
|
if (useExisting) {
|
|
|
|
QListWidgetItem *item = ui->sourceList->currentItem();
|
|
|
|
if (!item)
|
|
|
|
return;
|
|
|
|
|
|
|
|
AddExisting(QT_TO_UTF8(item->text()));
|
|
|
|
} else {
|
2014-05-12 15:32:55 -07:00
|
|
|
if (ui->sourceName->text().isEmpty()) {
|
|
|
|
QMessageBox::information(this,
|
|
|
|
QTStr("NoNameEntered"),
|
|
|
|
QTStr("NoNameEntered"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-08-02 12:38:55 -07:00
|
|
|
if (!AddNew(this, id, QT_TO_UTF8(ui->sourceName->text())))
|
2014-05-12 15:32:55 -07:00
|
|
|
return;
|
2014-05-10 18:47:48 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
done(DialogCode::Accepted);
|
|
|
|
}
|
|
|
|
|
|
|
|
void OBSBasicSourceSelect::on_buttonBox_rejected()
|
|
|
|
{
|
|
|
|
done(DialogCode::Rejected);
|
|
|
|
}
|
|
|
|
|
2014-08-02 12:38:55 -07:00
|
|
|
OBSBasicSourceSelect::OBSBasicSourceSelect(OBSBasic *parent, const char *id_)
|
2014-05-10 18:47:48 -07:00
|
|
|
: QDialog (parent),
|
|
|
|
ui (new Ui::OBSBasicSourceSelect),
|
2014-08-02 12:38:55 -07:00
|
|
|
id (id_)
|
2014-05-10 18:47:48 -07:00
|
|
|
{
|
|
|
|
ui->setupUi(this);
|
|
|
|
|
2014-05-12 16:12:22 -07:00
|
|
|
QString placeHolderText{QT_UTF8(obs_source_getdisplayname(
|
2014-08-02 12:38:55 -07:00
|
|
|
OBS_SOURCE_TYPE_INPUT, id))};
|
2014-05-09 15:09:17 -07:00
|
|
|
|
2014-05-12 16:12:22 -07:00
|
|
|
QString text{placeHolderText};
|
|
|
|
int i = 1;
|
2014-05-14 13:20:08 -07:00
|
|
|
obs_source_t source = nullptr;
|
|
|
|
while ((source = obs_get_source_by_name(QT_TO_UTF8(text)))) {
|
|
|
|
obs_source_release(source);
|
2014-05-12 16:12:22 -07:00
|
|
|
text = QString("%1 %2").arg(placeHolderText).arg(i++);
|
2014-05-14 13:20:08 -07:00
|
|
|
}
|
2014-05-12 16:12:22 -07:00
|
|
|
|
|
|
|
ui->sourceName->setText(text);
|
2014-05-09 15:09:17 -07:00
|
|
|
ui->sourceName->setFocus(); //Fixes deselect of text.
|
|
|
|
ui->sourceName->selectAll();
|
|
|
|
|
2014-05-10 18:47:48 -07:00
|
|
|
obs_enum_sources(EnumSources, this);
|
|
|
|
}
|