1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 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 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 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 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591
|
#include "window-extra-browsers.hpp"
#include "window-dock-browser.hpp"
#include "window-basic-main.hpp"
#include "qt-wrappers.hpp"
#include <QLineEdit>
#include <QHBoxLayout>
#include <QUuid>
#include <json11.hpp>
#include "ui_OBSExtraBrowsers.h"
using namespace json11;
#define OBJ_NAME_SUFFIX "_extraBrowser"
enum class Column : int {
Title,
Url,
Delete,
Count,
};
/* ------------------------------------------------------------------------- */
void ExtraBrowsersModel::Reset()
{
items.clear();
OBSBasic *main = OBSBasic::Get();
for (int i = 0; i < main->extraBrowserDocks.size(); i++) {
Item item;
item.prevIdx = i;
item.title = main->extraBrowserDockNames[i];
item.url = main->extraBrowserDockTargets[i];
items.push_back(item);
}
}
int ExtraBrowsersModel::rowCount(const QModelIndex &) const
{
int count = items.size() + 1;
return count;
}
int ExtraBrowsersModel::columnCount(const QModelIndex &) const
{
return (int)Column::Count;
}
QVariant ExtraBrowsersModel::data(const QModelIndex &index, int role) const
{
int column = index.column();
int idx = index.row();
int count = items.size();
bool validRole = role == Qt::DisplayRole ||
role == Qt::AccessibleTextRole;
if (!validRole)
return QVariant();
if (idx >= 0 && idx < count) {
switch (column) {
case (int)Column::Title:
return items[idx].title;
case (int)Column::Url:
return items[idx].url;
}
} else if (idx == count) {
switch (column) {
case (int)Column::Title:
return newTitle;
case (int)Column::Url:
return newURL;
}
}
return QVariant();
}
QVariant ExtraBrowsersModel::headerData(int section,
Qt::Orientation orientation,
int role) const
{
bool validRole = role == Qt::DisplayRole ||
role == Qt::AccessibleTextRole;
if (validRole && orientation == Qt::Orientation::Horizontal) {
switch (section) {
case (int)Column::Title:
return QTStr("ExtraBrowsers.DockName");
case (int)Column::Url:
return QStringLiteral("URL");
}
}
return QVariant();
}
Qt::ItemFlags ExtraBrowsersModel::flags(const QModelIndex &index) const
{
Qt::ItemFlags flags = QAbstractTableModel::flags(index);
if (index.column() != (int)Column::Delete)
flags |= Qt::ItemIsEditable;
return flags;
}
class DelButton : public QPushButton {
public:
inline DelButton(QModelIndex index_) : QPushButton(), index(index_) {}
QPersistentModelIndex index;
};
class EditWidget : public QLineEdit {
public:
inline EditWidget(QWidget *parent, QModelIndex index_)
: QLineEdit(parent),
index(index_)
{
}
QPersistentModelIndex index;
};
void ExtraBrowsersModel::AddDeleteButton(int idx)
{
QTableView *widget = reinterpret_cast<QTableView *>(parent());
QModelIndex index = createIndex(idx, (int)Column::Delete, nullptr);
QPushButton *del = new DelButton(index);
del->setProperty("themeID", "removeIconSmall");
del->setObjectName("extraPanelDelete");
del->setMinimumSize(QSize(20, 20));
connect(del, &QPushButton::clicked, this,
&ExtraBrowsersModel::DeleteItem);
widget->setIndexWidget(index, del);
widget->setRowHeight(idx, 20);
widget->setColumnWidth(idx, 20);
}
void ExtraBrowsersModel::CheckToAdd()
{
if (newTitle.isEmpty() || newURL.isEmpty())
return;
int idx = items.size() + 1;
beginInsertRows(QModelIndex(), idx, idx);
Item item;
item.prevIdx = -1;
item.title = newTitle;
item.url = newURL;
items.push_back(item);
newTitle = "";
newURL = "";
endInsertRows();
AddDeleteButton(idx - 1);
}
void ExtraBrowsersModel::UpdateItem(Item &item)
{
int idx = item.prevIdx;
OBSBasic *main = OBSBasic::Get();
BrowserDock *dock = reinterpret_cast<BrowserDock *>(
main->extraBrowserDocks[idx].get());
dock->setWindowTitle(item.title);
dock->setObjectName(item.title + OBJ_NAME_SUFFIX);
if (main->extraBrowserDockNames[idx] != item.title) {
main->extraBrowserDockNames[idx] = item.title;
dock->toggleViewAction()->setText(item.title);
dock->setTitle(item.title);
}
if (main->extraBrowserDockTargets[idx] != item.url) {
dock->cefWidget->setURL(QT_TO_UTF8(item.url));
main->extraBrowserDockTargets[idx] = item.url;
}
}
void ExtraBrowsersModel::DeleteItem()
{
QTableView *widget = reinterpret_cast<QTableView *>(parent());
DelButton *del = reinterpret_cast<DelButton *>(sender());
int row = del->index.row();
/* there's some sort of internal bug in Qt and deleting certain index
* widgets or "editors" that can cause a crash inside Qt if the widget
* is not manually removed, at least on 5.7 */
widget->setIndexWidget(del->index, nullptr);
del->deleteLater();
/* --------- */
beginRemoveRows(QModelIndex(), row, row);
int prevIdx = items[row].prevIdx;
items.removeAt(row);
if (prevIdx != -1) {
int i = 0;
for (; i < deleted.size() && deleted[i] < prevIdx; i++)
;
deleted.insert(i, prevIdx);
}
endRemoveRows();
}
void ExtraBrowsersModel::Apply()
{
OBSBasic *main = OBSBasic::Get();
for (Item &item : items) {
if (item.prevIdx != -1) {
UpdateItem(item);
} else {
QString uuid = QUuid::createUuid().toString();
uuid.replace(QRegularExpression("[{}-]"), "");
main->AddExtraBrowserDock(item.title, item.url, uuid,
true);
}
}
for (int i = deleted.size() - 1; i >= 0; i--) {
int idx = deleted[i];
main->extraBrowserDockTargets.removeAt(idx);
main->extraBrowserDockNames.removeAt(idx);
main->extraBrowserDocks.removeAt(idx);
}
if (main->extraBrowserDocks.empty())
main->extraBrowserMenuDocksSeparator.clear();
deleted.clear();
Reset();
}
void ExtraBrowsersModel::TabSelection(bool forward)
{
QListView *widget = reinterpret_cast<QListView *>(parent());
QItemSelectionModel *selModel = widget->selectionModel();
QModelIndex sel = selModel->currentIndex();
int row = sel.row();
int col = sel.column();
switch (sel.column()) {
case (int)Column::Title:
if (!forward) {
if (row == 0) {
return;
}
row -= 1;
}
col += 1;
break;
case (int)Column::Url:
if (forward) {
if (row == items.size()) {
return;
}
row += 1;
}
col -= 1;
}
sel = createIndex(row, col, nullptr);
selModel->setCurrentIndex(sel, QItemSelectionModel::Clear);
}
void ExtraBrowsersModel::Init()
{
for (int i = 0; i < items.count(); i++)
AddDeleteButton(i);
}
/* ------------------------------------------------------------------------- */
QWidget *ExtraBrowsersDelegate::createEditor(QWidget *parent,
const QStyleOptionViewItem &,
const QModelIndex &index) const
{
QLineEdit *text = new EditWidget(parent, index);
text->installEventFilter(const_cast<ExtraBrowsersDelegate *>(this));
text->setSizePolicy(QSizePolicy(QSizePolicy::Policy::Expanding,
QSizePolicy::Policy::Expanding,
QSizePolicy::ControlType::LineEdit));
return text;
}
void ExtraBrowsersDelegate::setEditorData(QWidget *editor,
const QModelIndex &index) const
{
QLineEdit *text = reinterpret_cast<QLineEdit *>(editor);
text->blockSignals(true);
text->setText(index.data().toString());
text->blockSignals(false);
}
bool ExtraBrowsersDelegate::eventFilter(QObject *object, QEvent *event)
{
QLineEdit *edit = qobject_cast<QLineEdit *>(object);
if (!edit)
return false;
if (LineEditCanceled(event)) {
RevertText(edit);
}
if (LineEditChanged(event)) {
UpdateText(edit);
if (event->type() == QEvent::KeyPress) {
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
if (keyEvent->key() == Qt::Key_Tab) {
model->TabSelection(true);
} else if (keyEvent->key() == Qt::Key_Backtab) {
model->TabSelection(false);
}
}
return true;
}
return false;
}
bool ExtraBrowsersDelegate::ValidName(const QString &name) const
{
for (auto &item : model->items) {
if (name.compare(item.title, Qt::CaseInsensitive) == 0) {
return false;
}
}
return true;
}
void ExtraBrowsersDelegate::RevertText(QLineEdit *edit_)
{
EditWidget *edit = reinterpret_cast<EditWidget *>(edit_);
int row = edit->index.row();
int col = edit->index.column();
bool newItem = (row == model->items.size());
QString oldText;
if (col == (int)Column::Title) {
oldText = newItem ? model->newTitle : model->items[row].title;
} else {
oldText = newItem ? model->newURL : model->items[row].url;
}
edit->setText(oldText);
}
bool ExtraBrowsersDelegate::UpdateText(QLineEdit *edit_)
{
EditWidget *edit = reinterpret_cast<EditWidget *>(edit_);
int row = edit->index.row();
int col = edit->index.column();
bool newItem = (row == model->items.size());
QString text = edit->text().trimmed();
if (!newItem && text.isEmpty()) {
return false;
}
if (col == (int)Column::Title) {
QString oldText = newItem ? model->newTitle
: model->items[row].title;
bool same = oldText.compare(text, Qt::CaseInsensitive) == 0;
if (!same && !ValidName(text)) {
edit->setText(oldText);
return false;
}
}
if (!newItem) {
/* if edited existing item, update it*/
switch (col) {
case (int)Column::Title:
model->items[row].title = text;
break;
case (int)Column::Url:
model->items[row].url = text;
break;
}
} else {
/* if both new values filled out, create new one */
switch (col) {
case (int)Column::Title:
model->newTitle = text;
break;
case (int)Column::Url:
model->newURL = text;
break;
}
model->CheckToAdd();
}
emit commitData(edit);
return true;
}
/* ------------------------------------------------------------------------- */
OBSExtraBrowsers::OBSExtraBrowsers(QWidget *parent)
: QDialog(parent),
ui(new Ui::OBSExtraBrowsers)
{
ui->setupUi(this);
setAttribute(Qt::WA_DeleteOnClose, true);
setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
model = new ExtraBrowsersModel(ui->table);
ui->table->setModel(model);
ui->table->setItemDelegateForColumn((int)Column::Title,
new ExtraBrowsersDelegate(model));
ui->table->setItemDelegateForColumn((int)Column::Url,
new ExtraBrowsersDelegate(model));
ui->table->horizontalHeader()->setSectionResizeMode(
QHeaderView::ResizeMode::Stretch);
ui->table->horizontalHeader()->setSectionResizeMode(
(int)Column::Delete, QHeaderView::ResizeMode::Fixed);
ui->table->setEditTriggers(
QAbstractItemView::EditTrigger::CurrentChanged);
}
OBSExtraBrowsers::~OBSExtraBrowsers() {}
void OBSExtraBrowsers::closeEvent(QCloseEvent *event)
{
QDialog::closeEvent(event);
model->Apply();
}
void OBSExtraBrowsers::on_apply_clicked()
{
model->Apply();
}
/* ------------------------------------------------------------------------- */
void OBSBasic::ClearExtraBrowserDocks()
{
extraBrowserDockTargets.clear();
extraBrowserDockNames.clear();
extraBrowserDocks.clear();
}
void OBSBasic::LoadExtraBrowserDocks()
{
const char *jsonStr = config_get_string(
App()->GlobalConfig(), "BasicWindow", "ExtraBrowserDocks");
std::string err;
Json json = Json::parse(jsonStr, err);
if (!err.empty())
return;
Json::array array = json.array_items();
if (!array.empty())
extraBrowserMenuDocksSeparator = ui->menuDocks->addSeparator();
for (Json &item : array) {
std::string title = item["title"].string_value();
std::string url = item["url"].string_value();
std::string uuid = item["uuid"].string_value();
AddExtraBrowserDock(title.c_str(), url.c_str(), uuid.c_str(),
false);
}
}
void OBSBasic::SaveExtraBrowserDocks()
{
Json::array array;
for (int i = 0; i < extraBrowserDocks.size(); i++) {
QDockWidget *dock = extraBrowserDocks[i].get();
QString title = extraBrowserDockNames[i];
QString url = extraBrowserDockTargets[i];
QString uuid = dock->property("uuid").toString();
Json::object obj{
{"title", QT_TO_UTF8(title)},
{"url", QT_TO_UTF8(url)},
{"uuid", QT_TO_UTF8(uuid)},
};
array.push_back(obj);
}
std::string output = Json(array).dump();
config_set_string(App()->GlobalConfig(), "BasicWindow",
"ExtraBrowserDocks", output.c_str());
}
void OBSBasic::ManageExtraBrowserDocks()
{
if (!extraBrowsers.isNull()) {
extraBrowsers->show();
extraBrowsers->raise();
return;
}
extraBrowsers = new OBSExtraBrowsers(this);
extraBrowsers->show();
}
void OBSBasic::AddExtraBrowserDock(const QString &title, const QString &url,
const QString &uuid, bool firstCreate)
{
static int panel_version = -1;
if (panel_version == -1) {
panel_version = obs_browser_qcef_version();
}
BrowserDock *dock = new BrowserDock(title);
QString bId(uuid.isEmpty() ? QUuid::createUuid().toString() : uuid);
bId.replace(QRegularExpression("[{}-]"), "");
dock->setProperty("uuid", bId);
dock->setObjectName(title + OBJ_NAME_SUFFIX);
dock->resize(460, 600);
dock->setMinimumSize(80, 80);
dock->setWindowTitle(title);
dock->setAllowedAreas(Qt::AllDockWidgetAreas);
QCefWidget *browser =
cef->create_widget(dock, QT_TO_UTF8(url), nullptr);
if (browser && panel_version >= 1)
browser->allowAllPopups(true);
dock->SetWidget(browser);
/* Add support for Twitch Dashboard panels */
if (url.contains("twitch.tv/popout") &&
url.contains("dashboard/live")) {
QRegularExpression re("twitch.tv\\/popout\\/([^/]+)\\/");
QRegularExpressionMatch match = re.match(url);
QString username = match.captured(1);
if (username.length() > 0) {
std::string script;
script =
"Object.defineProperty(document, 'referrer', { get: () => '";
script += "https://twitch.tv/";
script += QT_TO_UTF8(username);
script += "/dashboard/live";
script += "'});";
browser->setStartupScript(script);
}
}
AddDockWidget(dock, Qt::RightDockWidgetArea, true);
extraBrowserDocks.push_back(std::shared_ptr<QDockWidget>(dock));
extraBrowserDockNames.push_back(title);
extraBrowserDockTargets.push_back(url);
if (firstCreate) {
dock->setFloating(true);
QPoint curPos = pos();
QSize wSizeD2 = size() / 2;
QSize dSizeD2 = dock->size() / 2;
curPos.setX(curPos.x() + wSizeD2.width() - dSizeD2.width());
curPos.setY(curPos.y() + wSizeD2.height() - dSizeD2.height());
dock->move(curPos);
dock->setVisible(true);
}
}
|