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 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731
|
/******************************************************************************
* The MIT License (MIT)
*
* Copyright (c) 2020-2022 Baldur Karlsson
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
******************************************************************************/
#include "ConfigEditor.h"
#include <float.h>
#include <QDoubleSpinBox>
#include <QKeyEvent>
#include <QSpinBox>
#include "Code/QRDUtils.h"
#include "Code/Resources.h"
#include "Widgets/Extended/RDHeaderView.h"
#include "Widgets/Extended/RDLineEdit.h"
#include "Widgets/OrderedListEditor.h"
#include "ui_ConfigEditor.h"
static QString valueString(const SDObject *o)
{
if(o->type.basetype == SDBasic::String)
return o->data.str;
if(o->type.basetype == SDBasic::UnsignedInteger)
return Formatter::Format(o->data.basic.u);
if(o->type.basetype == SDBasic::SignedInteger)
return Formatter::Format(o->data.basic.i);
if(o->type.basetype == SDBasic::Float)
return Formatter::Format(o->data.basic.d);
if(o->type.basetype == SDBasic::Boolean)
return o->data.basic.b ? lit("True") : lit("False");
if(o->type.basetype == SDBasic::Array)
return lit("{...}");
return lit("??");
}
static bool anyChildChanged(const SDObject *o)
{
const SDObject *def = o->FindChild("default");
const SDObject *val = o->FindChild("value");
if(val && def)
return !val->HasEqualValue(def);
for(const SDObject *c : *o)
{
if(anyChildChanged(c))
return true;
}
return false;
}
class SettingModel : public QAbstractItemModel
{
public:
SettingModel(ConfigEditor *view) : QAbstractItemModel(view), m_Viewer(view)
{
populateParents(m_Viewer->m_Config, QModelIndex());
}
void refresh()
{
emit beginResetModel();
emit endResetModel();
}
QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override
{
SDObject *o = obj(parent);
if(row < 0 || row > rowCount(parent))
return QModelIndex();
return createIndex(row, column, o->GetChild(row));
}
QModelIndex parent(const QModelIndex &index) const override
{
SDObject *o = obj(index);
if(o == m_Viewer->m_Config)
return QModelIndex();
QModelIndex ret = parents[o];
if(!ret.isValid())
return ret;
return createIndex(ret.row(), index.column(), ret.internalPointer());
}
int rowCount(const QModelIndex &parent = QModelIndex()) const override
{
SDObject *o = obj(parent);
// values don't have children
if(o->FindChild("value"))
return 0;
return (int)o->NumChildren();
}
enum Columns
{
Column_Name,
Column_Value,
Column_ResetButton,
Column_Count,
};
Qt::ItemFlags flags(const QModelIndex &index) const override
{
if(!index.isValid())
return 0;
Qt::ItemFlags ret = QAbstractItemModel::flags(index);
if(index.column() == Column_Value)
{
SDObject *o = obj(index);
SDObject *value = o->FindChild("value");
if(value)
{
ret |= Qt::ItemIsEditable;
if(value->type.basetype == SDBasic::Boolean)
ret |= Qt::ItemIsUserCheckable;
}
}
return ret;
}
int columnCount(const QModelIndex &parent = QModelIndex()) const override { return Column_Count; }
QVariant headerData(int section, Qt::Orientation orientation, int role) const override
{
if(orientation == Qt::Horizontal && role == Qt::DisplayRole)
{
switch(section)
{
case Column_Name: return lit("Setting");
case Column_Value: return lit("Value");
case Column_ResetButton: return lit("Reset");
default: break;
}
}
return QVariant();
}
bool setData(const QModelIndex &index, const QVariant &val, int role) override
{
SDObject *o = NULL;
if(role == Qt::UserRole)
{
// if we have setData for user role, that means we got reset. Just need to emit data changed
o = obj(index);
}
else if(index.column() == Column_Value)
{
o = obj(index);
SDObject *value = o->FindChild("value");
if(role == Qt::CheckStateRole && value)
{
value->data.basic.b = (val.toInt() == Qt::CheckState::Checked);
}
else
{
// didn't change anything we care about
o = NULL;
}
}
if(o)
{
// dataChanged this index and all parents (in case a section became non-customised, or
// customised, and it wasn't before)
QModelIndex idx = index;
while(idx.isValid())
{
o = obj(idx);
emit dataChanged(createIndex(idx.row(), 0, o), createIndex(idx.row(), Column_Count, o),
{Qt::DisplayRole, Qt::CheckStateRole, Qt::FontRole});
idx = parents[o];
}
return true;
}
return false;
}
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override
{
if(index.isValid())
{
SDObject *o = obj(index);
int col = index.column();
SDObject *value = o->FindChild("value");
if(role == Qt::UserRole)
{
return QVariant::fromValue((quintptr)o);
}
else if(role == Qt::DisplayRole)
{
switch(col)
{
case Column_Name: return o->name;
case Column_Value:
return value && value->type.basetype != SDBasic::Boolean ? valueString(value)
: QVariant();
case Column_ResetButton: return anyChildChanged(o) ? lit("...") : QVariant();
default: break;
}
}
else if(role == Qt::CheckStateRole && col == Column_Value)
{
if(value && value->type.basetype == SDBasic::Boolean)
return value->data.basic.b ? Qt::CheckState::Checked : Qt::CheckState::Unchecked;
return QVariant();
}
else if(role == Qt::TextAlignmentRole && col == Column_ResetButton)
{
return Qt::AlignHCenter + Qt::AlignTop;
}
else if(role == Qt::ToolTipRole)
{
SDObject *desc = o->FindChild("description");
if(desc)
{
rdcstr ret = desc->AsString();
if(o->FindChild("key") == NULL)
{
ret =
"WARNING: Unknown setting, possibly it has been removed or from a different "
"build.\n\n" +
ret;
}
return ret;
}
}
else if(role == Qt::FontRole)
{
if(anyChildChanged(o))
{
QFont font;
font.setBold(true);
return font;
}
// if this is a value but has no key, it's an unrecognised setting (stale/removed, or from
// a different or future build).
if(o->FindChild("description") && o->FindChild("key") == NULL)
{
QFont font;
font.setItalic(true);
return font;
}
}
}
return QVariant();
}
private:
SDObject *obj(const QModelIndex &parent) const
{
SDObject *ret = (SDObject *)parent.internalPointer();
if(ret == NULL)
ret = m_Viewer->m_Config;
return ret;
}
// Qt models need child->parent relationships. We don't have that with SDObject but they are
// immutable so we can cache them
QMap<SDObject *, QModelIndex> parents;
void populateParents(SDObject *o, QModelIndex parent)
{
if(o->FindChild("value"))
return;
int i = 0;
for(SDObject *c : *o)
{
parents[c] = parent;
populateParents(c, index(i++, 0, parent));
}
}
ConfigEditor *m_Viewer;
};
class SettingFilterModel : public QSortFilterProxyModel
{
public:
explicit SettingFilterModel(ConfigEditor *view) : QSortFilterProxyModel(view), m_Viewer(view) {}
void setFilter(QString text)
{
m_Text = text;
m_KeyText = m_Text;
m_KeyText.replace(QLatin1Char('.'), QLatin1Char('_'));
invalidateFilter();
}
protected:
virtual bool filterAcceptsRow(int source_row, const QModelIndex &source_parent) const override
{
if(m_Text.isEmpty())
return true;
SDObject *o = obj(source_parent);
return matchesAnyChild(o->GetChild(source_row));
}
bool matchesAnyChild(SDObject *o) const
{
if(!o)
return false;
if(QString(o->name).contains(m_Text, Qt::CaseInsensitive))
return true;
if(o->FindChild("value"))
{
if(o->FindChild("key") &&
QString(o->FindChild("key")->AsString()).contains(m_KeyText, Qt::CaseInsensitive))
return true;
return false;
}
for(SDObject *c : *o)
if(matchesAnyChild(c))
return true;
return false;
}
private:
SDObject *obj(const QModelIndex &parent) const
{
SDObject *ret = (SDObject *)parent.internalPointer();
if(ret == NULL)
ret = m_Viewer->m_Config;
return ret;
}
ConfigEditor *m_Viewer;
QString m_Text;
QString m_KeyText;
};
SettingDelegate::SettingDelegate(ConfigEditor *editor, RDTreeView *parent)
: QStyledItemDelegate(parent), m_Editor(editor), m_View(parent)
{
}
SettingDelegate::~SettingDelegate()
{
}
void SettingDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
if(index.column() == SettingModel::Column_ResetButton)
{
SDObject *o = (SDObject *)index.data(Qt::UserRole).toULongLong();
SDObject *def = o->FindChild("default");
SDObject *val = o->FindChild("value");
if(val && def && !val->HasEqualValue(def))
{
// draw the item without text, so we get the proper background/selection/etc.
// we'd like to be able to use the parent delegate's paint here, but either it calls to
// QStyledItemDelegate which will re-fetch the text (bleh), or it calls to the manual
// delegate which could do anything. So for this case we just use the style and skip the
// delegate and hope it works out.
QStyleOptionViewItem opt = option;
QStyledItemDelegate::initStyleOption(&opt, index);
opt.text.clear();
m_Editor->style()->drawControl(QStyle::CE_ItemViewItem, &opt, painter, m_Editor);
QStyleOptionToolButton buttonOpt;
int size = m_Editor->style()->pixelMetric(QStyle::PM_SmallIconSize, 0, m_Editor);
buttonOpt.iconSize = QSize(size, size);
buttonOpt.subControls = 0;
buttonOpt.activeSubControls = 0;
buttonOpt.features = QStyleOptionToolButton::None;
buttonOpt.arrowType = Qt::NoArrow;
buttonOpt.state = QStyle::State_Active | QStyle::State_Enabled | QStyle::State_AutoRaise;
buttonOpt.rect = option.rect.adjusted(0, 0, -1, -1);
buttonOpt.icon = Icons::arrow_undo();
if(m_View->currentHoverIndex() == index)
buttonOpt.state |= QStyle::State_MouseOver;
m_Editor->style()->drawComplexControl(QStyle::CC_ToolButton, &buttonOpt, painter, m_Editor);
return;
}
}
return QStyledItemDelegate::paint(painter, option, index);
}
QSize SettingDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
return QStyledItemDelegate::sizeHint(option, index);
}
bool SettingDelegate::editorEvent(QEvent *event, QAbstractItemModel *model,
const QStyleOptionViewItem &option, const QModelIndex &index)
{
if(event->type() == QEvent::MouseButtonRelease && index.column() == SettingModel::Column_ResetButton)
{
SDObject *o = (SDObject *)index.data(Qt::UserRole).toULongLong();
SDObject *def = o->FindChild("default");
SDObject *val = o->FindChild("value");
if(def && val)
{
val->data.str = def->data.str;
memcpy(&val->data.basic, &def->data.basic, sizeof(val->data.basic));
val->DeleteChildren();
for(size_t c = 0; c < def->NumChildren(); c++)
val->DuplicateAndAddChild(def->GetChild(c));
// call setData() to emit the dataChanged for this element and all parents
model->setData(index, QVariant(), Qt::UserRole);
return true;
}
}
return QStyledItemDelegate::editorEvent(event, model, option, index);
}
QWidget *SettingDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
QWidget *ret = NULL;
SDObject *o = (SDObject *)index.data(Qt::UserRole).toULongLong();
SDObject *val = o->FindChild("value");
if(val)
{
// bools should have checkboxes
if(val->type.basetype == SDBasic::Boolean)
{
qWarning() << "Unexpected createEditor for boolean " << QString(o->name);
return ret;
}
QString settingName;
SDObject *key = o->FindChild("key");
if(key)
settingName = key->AsString();
else
settingName = tr("Unknown Setting %1").arg(o->name);
// for numbers, provide a spinbox
if(val->type.basetype == SDBasic::UnsignedInteger || val->type.basetype == SDBasic::SignedInteger)
{
QSpinBox *spin = new QSpinBox(parent);
ret = spin;
if(val->type.basetype == SDBasic::UnsignedInteger)
spin->setMinimum(0);
else
spin->setMinimum(INT_MIN);
spin->setMaximum(INT_MAX);
}
else if(val->type.basetype == SDBasic::Float)
{
QDoubleSpinBox *spin = new QDoubleSpinBox(parent);
ret = spin;
spin->setSingleStep(0.1);
spin->setMinimum(-FLT_MAX);
spin->setMaximum(FLT_MAX);
}
else if(val->type.basetype == SDBasic::String)
{
if(QString(o->name).contains(lit("DirPath"), Qt::CaseSensitive))
{
QString dir = RDDialog::getExistingDirectory(m_Editor, tr("Browse for %1").arg(settingName));
if(!dir.isEmpty())
{
val->data.str = dir;
// we've handled the edit synchronously, don't create an edit widget
ret = NULL;
// call setData() to emit the dataChanged for this element and all parents
m_View->model()->setData(index, QVariant(), Qt::UserRole);
}
}
else if(QString(o->name).contains(lit("Path"), Qt::CaseSensitive))
{
QString file = RDDialog::getOpenFileName(m_Editor, tr("Browse for %1").arg(settingName));
if(!file.isEmpty())
{
val->data.str = file;
// we've handled the edit synchronously, don't create an edit widget
ret = NULL;
// call setData() to emit the dataChanged for this element and all parents
m_View->model()->setData(index, QVariant(), Qt::UserRole);
}
}
else
{
RDLineEdit *line = new RDLineEdit(parent);
ret = line;
QObject::connect(line, &RDLineEdit::keyPress, this, &SettingDelegate::editorKeyPress);
}
}
else if(val->type.basetype == SDBasic::Array)
{
// only support arrays of strings. Pop up a separate editor to handle this
QDialog listEditor;
listEditor.setWindowTitle(tr("Edit values of %1").arg(QString(settingName)));
listEditor.setWindowFlags(listEditor.windowFlags() & ~Qt::WindowContextHelpButtonHint);
BrowseMode mode = BrowseMode::None;
if(QString(o->name).contains(lit("DirPath"), Qt::CaseSensitive))
mode = BrowseMode::Folder;
else if(QString(o->name).contains(lit("Path"), Qt::CaseSensitive))
mode = BrowseMode::File;
OrderedListEditor list(tr("Entry"), mode);
QVBoxLayout layout;
QDialogButtonBox okCancel;
okCancel.setStandardButtons(QDialogButtonBox::Cancel | QDialogButtonBox::Ok);
layout.addWidget(&list);
layout.addWidget(&okCancel);
QObject::connect(&okCancel, &QDialogButtonBox::accepted, &listEditor, &QDialog::accept);
QObject::connect(&okCancel, &QDialogButtonBox::rejected, &listEditor, &QDialog::reject);
listEditor.setLayout(&layout);
QStringList items;
for(SDObject *c : *val)
items << c->data.str;
list.setItems(items);
int res = RDDialog::show(&listEditor);
if(res)
{
items = list.getItems();
val->DeleteChildren();
val->ReserveChildren(items.size());
for(int i = 0; i < items.size(); i++)
val->AddAndOwnChild(makeSDString("$el"_lit, items[i]));
}
// we've handled the edit synchronously, don't create an edit widget
ret = NULL;
// call setData() to emit the dataChanged for this element and all parents
m_View->model()->setData(index, QVariant(), Qt::UserRole);
}
else
{
qWarning() << "Unexpected type of " << QString(o->name)
<< " to edit: " << ToQStr(val->type.basetype);
}
}
return ret;
}
void SettingDelegate::editorKeyPress(QKeyEvent *ev)
{
QLineEdit *line = qobject_cast<QLineEdit *>(QObject::sender());
if(ev->key() == Qt::Key_Return || ev->key() == Qt::Key_Enter)
{
commitData(line);
closeEditor(line);
}
else if(ev->key() == Qt::Key_Escape)
{
closeEditor(line);
}
}
void SettingDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
SDObject *o = (SDObject *)index.data(Qt::UserRole).toULongLong();
SDObject *val = o->FindChild("value");
if(val)
{
if(val->type.basetype == SDBasic::Boolean)
{
qWarning() << "Unexpected setEditorData for boolean " << QString(o->name);
return;
}
if(val->type.basetype == SDBasic::UnsignedInteger)
qobject_cast<QSpinBox *>(editor)->setValue(val->AsUInt32() & 0x7fffffffU);
else if(val->type.basetype == SDBasic::SignedInteger)
qobject_cast<QSpinBox *>(editor)->setValue(val->AsInt32());
else if(val->type.basetype == SDBasic::Float)
qobject_cast<QDoubleSpinBox *>(editor)->setValue(val->AsDouble());
else if(val->type.basetype == SDBasic::String)
qobject_cast<QLineEdit *>(editor)->setText(val->AsString());
else
qWarning() << "Unexpected type of " << QString(o->name) << ": " << ToQStr(val->type.basetype);
}
}
void SettingDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index) const
{
SDObject *o = (SDObject *)index.data(Qt::UserRole).toULongLong();
SDObject *val = o->FindChild("value");
if(val)
{
if(val->type.basetype == SDBasic::Boolean)
{
qWarning() << "Unexpected setModelData for boolean " << QString(o->name);
return;
}
if(val->type.basetype == SDBasic::UnsignedInteger)
val->data.basic.u = qMax(0, qobject_cast<QSpinBox *>(editor)->value());
else if(val->type.basetype == SDBasic::SignedInteger)
val->data.basic.i = qobject_cast<QSpinBox *>(editor)->value();
else if(val->type.basetype == SDBasic::Float)
val->data.basic.d = qobject_cast<QSpinBox *>(editor)->value();
else if(val->type.basetype == SDBasic::String)
val->data.str = qobject_cast<QLineEdit *>(editor)->text();
else
qWarning() << "Unexpected type of " << QString(o->name) << ": " << ToQStr(val->type.basetype);
}
}
ConfigEditor::ConfigEditor(QWidget *parent) : QDialog(parent), ui(new Ui::ConfigEditor)
{
ui->setupUi(this);
setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
m_Config = RENDERDOC_SetConfigSetting("");
m_SettingModel = new SettingModel(this);
m_FilterModel = new SettingFilterModel(this);
m_FilterModel->setSourceModel(m_SettingModel);
ui->settings->setModel(m_FilterModel);
{
RDHeaderView *header = new RDHeaderView(Qt::Horizontal, ui->settings);
ui->settings->setHeader(header);
header->setColumnStretchHints({-1, 1, -1});
}
ui->settings->setItemDelegate(new SettingDelegate(this, ui->settings));
}
ConfigEditor::~ConfigEditor()
{
delete ui;
}
void ConfigEditor::on_filter_textChanged(const QString &text)
{
RDTreeViewExpansionState state;
ui->settings->saveExpansion(state, 0);
m_FilterModel->setFilter(text);
ui->settings->applyExpansion(state, 0);
}
void ConfigEditor::keyPressEvent(QKeyEvent *e)
{
if(e->key() == Qt::Key_Enter || e->key() == Qt::Key_Return)
return;
QDialog::keyPressEvent(e);
}
|