| 1 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 |
/**************************************************************************** |
| 3 |
* * |
| 4 |
* Copyright (c) 2025 Kacper Donat <[email protected]> * |
| 5 |
* * |
| 6 |
* This file is part of FreeCAD. * |
| 7 |
* * |
| 8 |
* FreeCAD is free software: you can redistribute it and/or modify it * |
| 9 |
* under the terms of the GNU Lesser General Public License as * |
| 10 |
* published by the Free Software Foundation, either version 2.1 of the * |
| 11 |
* License, or (at your option) any later version. * |
| 12 |
* * |
| 13 |
* FreeCAD is distributed in the hope that it will be useful, but * |
| 14 |
* WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * |
| 16 |
* Lesser General Public License for more details. * |
| 17 |
* * |
| 18 |
* You should have received a copy of the GNU Lesser General Public * |
| 19 |
* License along with FreeCAD. If not, see * |
| 20 |
* <https://www.gnu.org/licenses/>. * |
| 21 |
* * |
| 22 |
***************************************************************************/ |
| 23 |
|
| 24 |
#include "Tools.h" |
| 25 |
#include "Application.h" |
| 26 |
#include "OverlayManager.h" |
| 27 |
#include "DlgThemeEditor.h" |
| 28 |
#include "ui_DlgThemeEditor.h" |
| 29 |
#include "BitmapFactory.h" |
| 30 |
|
| 31 |
#include <Utilities.h> |
| 32 |
#include <Base/ServiceProvider.h> |
| 33 |
#include <Base/Tools.h> |
| 34 |
|
| 35 |
#include <ranges> |
| 36 |
#include <QImageReader> |
| 37 |
#include <QPainter> |
| 38 |
#include <QStyledItemDelegate> |
| 39 |
#include <QTimer> |
| 40 |
|
| 41 |
QPixmap colorPreview(const QColor& color) |
| 42 |
{ |
| 43 |
constexpr qsizetype size = 16; |
| 44 |
|
| 45 |
QPixmap preview = Gui::BitmapFactory().empty({size, size}); |
| 46 |
|
| 47 |
QPainter painter(&preview); |
| 48 |
painter.setRenderHint(QPainter::Antialiasing); |
| 49 |
painter.setPen(Qt::NoPen); |
| 50 |
painter.setBrush(color); |
| 51 |
painter.drawEllipse(QRect {0, 0, size, size}); |
| 52 |
|
| 53 |
return preview; |
| 54 |
} |
| 55 |
|
| 56 |
QString typeOfTokenValue(const Gui::StyleParameters::Value& value) |
| 57 |
{ |
| 58 |
// clang-format off |
| 59 |
return std::visit( |
| 60 |
Base::Overloads { |
| 61 |
[](const std::string&) { |
| 62 |
return QWidget::tr("Generic"); |
| 63 |
}, |
| 64 |
[](const Gui::StyleParameters::Numeric&) { |
| 65 |
return QWidget::tr("Numeric"); |
| 66 |
}, |
| 67 |
[](const Base::Color&) { |
| 68 |
return QWidget::tr("Color"); |
| 69 |
} |
| 70 |
}, |
| 71 |
value |
| 72 |
); |
| 73 |
// clang-format on |
| 74 |
} |
| 75 |
|
| 76 |
namespace Gui |
| 77 |
{ |
| 78 |
struct StyleParametersModel::Item |
| 79 |
{ |
| 80 |
Item() = default; |
| 81 |
virtual ~Item() = default; |
| 82 |
|
| 83 |
FC_DEFAULT_COPY_MOVE(Item); |
| 84 |
|
| 85 |
virtual bool isHeader() const = 0; |
| 86 |
}; |
| 87 |
|
| 88 |
struct StyleParametersModel::GroupItem: Item |
| 89 |
{ |
| 90 |
explicit GroupItem(QString title, ParameterSource* source) |
| 91 |
: title(std::move(title)) |
| 92 |
, canAddNewParameters(source && source->metadata.options.testFlag(UserEditable)) |
| 93 |
, source(source) |
| 94 |
{} |
| 95 |
|
| 96 |
bool isHeader() const override |
| 97 |
{ |
| 98 |
return true; |
| 99 |
} |
| 100 |
|
| 101 |
QString title; |
| 102 |
bool canAddNewParameters {false}; |
| 103 |
ParameterSource* source; |
| 104 |
std::set<std::string> deleted {}; |
| 105 |
}; |
| 106 |
|
| 107 |
struct StyleParametersModel::ParameterItem: Item |
| 108 |
{ |
| 109 |
ParameterItem(QString name, StyleParameters::Parameter token) |
| 110 |
: name(std::move(name)) |
| 111 |
, token(std::move(token)) |
| 112 |
{} |
| 113 |
|
| 114 |
bool isHeader() const override |
| 115 |
{ |
| 116 |
return false; |
| 117 |
} |
| 118 |
|
| 119 |
QString name; |
| 120 |
StyleParameters::Parameter token; |
| 121 |
QFlags<Qt::ItemFlag> flags = Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable; |
| 122 |
}; |
| 123 |
|
| 124 |
class StyleParametersModel::Node |
| 125 |
{ |
| 126 |
public: |
| 127 |
explicit Node(std::unique_ptr<Item> data, Node* parent = nullptr) |
| 128 |
: _parent(parent) |
| 129 |
, _data(std::move(data)) |
| 130 |
{} |
| 131 |
|
| 132 |
void appendChild(std::unique_ptr<Node> child) |
| 133 |
{ |
| 134 |
child->_parent = this; |
| 135 |
_children.push_back(std::move(child)); |
| 136 |
} |
| 137 |
|
| 138 |
void removeChild(const int row) |
| 139 |
{ |
| 140 |
if (row >= 0 && row < static_cast<int>(_children.size())) { |
| 141 |
_children.erase(_children.begin() + row); |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
Node* child(const int row) const |
| 146 |
{ |
| 147 |
if (row < 0 || row >= static_cast<int>(_children.size())) { |
| 148 |
if (!_empty) { |
| 149 |
_empty = std::make_unique<Node>(nullptr, const_cast<Node*>(this)); |
| 150 |
} |
| 151 |
|
| 152 |
return _empty.get(); |
| 153 |
} |
| 154 |
|
| 155 |
return _children[row].get(); |
| 156 |
} |
| 157 |
|
| 158 |
int childCount() const |
| 159 |
{ |
| 160 |
return static_cast<int>(_children.size()); |
| 161 |
} |
| 162 |
|
| 163 |
int row() const |
| 164 |
{ |
| 165 |
if (!_parent) { |
| 166 |
return 0; |
| 167 |
} |
| 168 |
|
| 169 |
const auto& siblings = _parent->_children; |
| 170 |
for (size_t i = 0; i < siblings.size(); ++i) { |
| 171 |
if (siblings[i].get() == this) { |
| 172 |
return static_cast<int>(i); |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
return -1; |
| 177 |
} |
| 178 |
|
| 179 |
Item* data() const |
| 180 |
{ |
| 181 |
return _data.get(); |
| 182 |
} |
| 183 |
|
| 184 |
template<class T> |
| 185 |
T* data() const |
| 186 |
{ |
| 187 |
return dynamic_cast<T*>(_data.get()); |
| 188 |
} |
| 189 |
|
| 190 |
Node* parent() const |
| 191 |
{ |
| 192 |
return _parent; |
| 193 |
} |
| 194 |
|
| 195 |
private: |
| 196 |
Node* _parent; |
| 197 |
std::vector<std::unique_ptr<Node>> _children {}; |
| 198 |
|
| 199 |
mutable std::unique_ptr<Node> _empty {}; |
| 200 |
std::unique_ptr<Item> _data {}; |
| 201 |
}; |
| 202 |
|
| 203 |
class DlgThemeEditor::Delegate: public QStyledItemDelegate |
| 204 |
{ |
| 205 |
Q_OBJECT |
| 206 |
|
| 207 |
QRegularExpression validNameRegExp {QStringLiteral("^[A-Z][a-zA-Z0-9]*$")}; |
| 208 |
QRegularExpressionValidator* nameValidator; |
| 209 |
|
| 210 |
public: |
| 211 |
explicit Delegate(QObject* parent = nullptr) |
| 212 |
: QStyledItemDelegate(parent) |
| 213 |
, nameValidator(new QRegularExpressionValidator(validNameRegExp, this)) |
| 214 |
{} |
| 215 |
|
| 216 |
QWidget* createEditor( |
| 217 |
QWidget* parent, |
| 218 |
[[maybe_unused]] const QStyleOptionViewItem& option, |
| 219 |
const QModelIndex& index |
| 220 |
) const override |
| 221 |
{ |
| 222 |
auto model = dynamic_cast<const StyleParametersModel*>(index.model()); |
| 223 |
if (!model) { |
| 224 |
return nullptr; |
| 225 |
} |
| 226 |
|
| 227 |
if (model->item<StyleParametersModel::ParameterItem>(index) |
| 228 |
&& index.column() == StyleParametersModel::ParameterExpression) { |
| 229 |
return new QLineEdit(parent); |
| 230 |
} |
| 231 |
|
| 232 |
if (index.column() == StyleParametersModel::ParameterName) { |
| 233 |
auto editor = new QLineEdit(parent); |
| 234 |
editor->setValidator(nameValidator); |
| 235 |
return editor; |
| 236 |
} |
| 237 |
|
| 238 |
return nullptr; |
| 239 |
} |
| 240 |
|
| 241 |
void setEditorData(QWidget* editor, const QModelIndex& index) const override |
| 242 |
{ |
| 243 |
if (auto* lineEdit = qobject_cast<QLineEdit*>(editor)) { |
| 244 |
lineEdit->setText(index.data(Qt::DisplayRole).toString()); |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
void setModelData(QWidget* editor, QAbstractItemModel* model, const QModelIndex& index) const override |
| 249 |
{ |
| 250 |
if (auto* lineEdit = qobject_cast<QLineEdit*>(editor)) { |
| 251 |
model->setData(index, lineEdit->text(), Qt::EditRole); |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
void updateEditorGeometry( |
| 256 |
QWidget* editor, |
| 257 |
const QStyleOptionViewItem& option, |
| 258 |
[[maybe_unused]] const QModelIndex& index |
| 259 |
) const override |
| 260 |
{ |
| 261 |
editor->setGeometry(option.rect); |
| 262 |
} |
| 263 |
|
| 264 |
QSize sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const override |
| 265 |
{ |
| 266 |
constexpr int height = 36; |
| 267 |
|
| 268 |
QSize base = QStyledItemDelegate::sizeHint(option, index); |
| 269 |
return {base.width(), std::max(base.height(), height)}; |
| 270 |
} |
| 271 |
|
| 272 |
void paintAddPlaceholder(QPainter* painter, const QStyleOptionViewItem& option) const |
| 273 |
{ |
| 274 |
QStyle* style = option.widget ? option.widget->style() : QApplication::style(); |
| 275 |
QRect rect = style->subElementRect(QStyle::SE_ItemViewItemText, &option, option.widget); |
| 276 |
|
| 277 |
QFont font = option.font; |
| 278 |
font.setItalic(true); |
| 279 |
|
| 280 |
painter->setFont(font); |
| 281 |
painter->drawText(rect, Qt::AlignLeft | Qt::AlignVCenter, tr("New parameter...")); |
| 282 |
} |
| 283 |
|
| 284 |
void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const override |
| 285 |
{ |
| 286 |
auto model = dynamic_cast<const StyleParametersModel*>(index.model()); |
| 287 |
|
| 288 |
painter->save(); |
| 289 |
|
| 290 |
QStyleOptionViewItem opt(option); |
| 291 |
initStyleOption(&opt, index); |
| 292 |
|
| 293 |
if (model->isAddPlaceholder(index)) { |
| 294 |
if (index.column() == StyleParametersModel::ParameterName) { |
| 295 |
paintAddPlaceholder(painter, opt); |
| 296 |
} |
| 297 |
} |
| 298 |
else if (model->item<StyleParametersModel::GroupItem>(index)) { |
| 299 |
constexpr int headerContrast = 120; |
| 300 |
|
| 301 |
const bool isLightTheme = option.palette.color(QPalette::Text).lightness() < 128; |
| 302 |
|
| 303 |
const QColor headerBackgroundColor = QtTools::valueOr( |
| 304 |
option.widget->property("headerBackgroundColor"), |
| 305 |
isLightTheme ? option.palette.color(QPalette::AlternateBase).darker(headerContrast) |
| 306 |
: option.palette.color(QPalette::AlternateBase).lighter(headerContrast) |
| 307 |
); |
| 308 |
|
| 309 |
painter->fillRect(option.rect, headerBackgroundColor); |
| 310 |
QStyledItemDelegate::paint(painter, option, index); |
| 311 |
} |
| 312 |
else { |
| 313 |
QStyledItemDelegate::paint(painter, option, index); |
| 314 |
} |
| 315 |
|
| 316 |
painter->restore(); |
| 317 |
} |
| 318 |
}; |
| 319 |
|
| 320 |
void TokenTreeView::keyPressEvent(QKeyEvent* event) |
| 321 |
{ |
| 322 |
static constexpr auto expressionEditKeys = {Qt::Key_Return, Qt::Key_Enter, Qt::Key_Space}; |
| 323 |
static constexpr auto nameEditKeys = {Qt::Key_F2}; |
| 324 |
static constexpr auto deleteKeys = {Qt::Key_Delete}; |
| 325 |
|
| 326 |
const auto isCorrectKey = [&event](auto key) { |
| 327 |
return event->key() == key; |
| 328 |
}; |
| 329 |
|
| 330 |
if (QModelIndex index = currentIndex(); index.isValid()) { |
| 331 |
if (std::ranges::any_of(expressionEditKeys, isCorrectKey)) { |
| 332 |
edit(index.siblingAtColumn(StyleParametersModel::ParameterExpression)); |
| 333 |
return; |
| 334 |
} |
| 335 |
|
| 336 |
if (std::ranges::any_of(nameEditKeys, isCorrectKey)) { |
| 337 |
edit(index.siblingAtColumn(StyleParametersModel::ParameterName)); |
| 338 |
return; |
| 339 |
} |
| 340 |
|
| 341 |
if (std::ranges::any_of(deleteKeys, isCorrectKey)) { |
| 342 |
requestRemove(currentIndex()); |
| 343 |
return; |
| 344 |
} |
| 345 |
} |
| 346 |
|
| 347 |
QTreeView::keyPressEvent(event); |
| 348 |
} |
| 349 |
|
| 350 |
StyleParametersModel::StyleParametersModel( |
| 351 |
const std::list<StyleParameters::ParameterSource*>& sources, |
| 352 |
QObject* parent |
| 353 |
) |
| 354 |
: QAbstractItemModel(parent) |
| 355 |
, ParameterSource({.name = QT_TR_NOOP("All Theme Editor Parameters")}) |
| 356 |
, sources(sources) |
| 357 |
, manager(new StyleParameters::ParameterManager()) |
| 358 |
{ |
| 359 |
// The parameter model serves as the source, so the manager can compute all necessary things |
| 360 |
manager->addSource(this); |
| 361 |
|
| 362 |
reset(); |
| 363 |
} |
| 364 |
|
| 365 |
StyleParametersModel::~StyleParametersModel() = default; |
| 366 |
|
| 367 |
std::list<StyleParameters::Parameter> StyleParametersModel::all() const |
| 368 |
{ |
| 369 |
std::map<std::string, StyleParameters::Parameter> result; |
| 370 |
|
| 371 |
QtTools::walkTreeModel(this, [this, &result](const QModelIndex& index) { |
| 372 |
if (auto parameterItem = item<ParameterItem>(index)) { |
| 373 |
if (result.contains(parameterItem->token.name)) { |
| 374 |
return; |
| 375 |
} |
| 376 |
|
| 377 |
result[parameterItem->token.name] = parameterItem->token; |
| 378 |
} |
| 379 |
}); |
| 380 |
|
| 381 |
const auto values = result | std::ranges::views::values; |
| 382 |
return std::list<StyleParameters::Parameter>(values.begin(), values.end()); |
| 383 |
} |
| 384 |
|
| 385 |
std::optional<StyleParameters::Parameter> StyleParametersModel::get(const std::string& name) const |
| 386 |
{ |
| 387 |
std::optional<StyleParameters::Parameter> result = std::nullopt; |
| 388 |
|
| 389 |
QtTools::walkTreeModel(this, [this, &name, &result](const QModelIndex& index) { |
| 390 |
if (auto parameterItem = item<ParameterItem>(index)) { |
| 391 |
if (parameterItem->token.name == name) { |
| 392 |
result = parameterItem->token; |
| 393 |
return true; |
| 394 |
} |
| 395 |
} |
| 396 |
|
| 397 |
return false; |
| 398 |
}); |
| 399 |
|
| 400 |
return result; |
| 401 |
} |
| 402 |
|
| 403 |
void StyleParametersModel::removeItem(const QModelIndex& index) |
| 404 |
{ |
| 405 |
if (auto parameterItem = item<ParameterItem>(index)) { |
| 406 |
auto groupItem = item<GroupItem>(index.parent()); |
| 407 |
|
| 408 |
if (!groupItem->source->metadata.options.testFlag(UserEditable)) { |
| 409 |
return; |
| 410 |
} |
| 411 |
|
| 412 |
groupItem->deleted.insert(parameterItem->token.name); |
| 413 |
|
| 414 |
beginRemoveRows(index.parent(), index.row(), index.row()); |
| 415 |
node(index.parent())->removeChild(index.row()); |
| 416 |
endRemoveRows(); |
| 417 |
} |
| 418 |
} |
| 419 |
|
| 420 |
void StyleParametersModel::reset() |
| 421 |
{ |
| 422 |
using enum StyleParameters::ParameterSourceOption; |
| 423 |
|
| 424 |
beginResetModel(); |
| 425 |
root = std::make_unique<Node>(std::make_unique<GroupItem>(tr("Root"), nullptr)); |
| 426 |
|
| 427 |
for (auto* source : sources) { |
| 428 |
auto groupNode = std::make_unique<Node>( |
| 429 |
std::make_unique<GroupItem>(tr(source->metadata.name.c_str()), source) |
| 430 |
); |
| 431 |
|
| 432 |
for (const auto& parameter : source->all()) { |
| 433 |
auto item = std::make_unique<Node>( |
| 434 |
std::make_unique<ParameterItem>(QString::fromStdString(parameter.name), parameter) |
| 435 |
); |
| 436 |
|
| 437 |
if (source->metadata.options.testFlag(ReadOnly)) { |
| 438 |
item->data<ParameterItem>()->flags = Qt::ItemIsEnabled | Qt::ItemIsSelectable; |
| 439 |
} |
| 440 |
|
| 441 |
groupNode->appendChild(std::move(item)); |
| 442 |
} |
| 443 |
|
| 444 |
root->appendChild(std::move(groupNode)); |
| 445 |
} |
| 446 |
|
| 447 |
endResetModel(); |
| 448 |
} |
| 449 |
|
| 450 |
void StyleParametersModel::flush() |
| 451 |
{ |
| 452 |
QtTools::walkTreeModel(this, [this](const QModelIndex& index) { |
| 453 |
if (const auto& groupItem = item<GroupItem>(index)) { |
| 454 |
for (const auto& parameter : groupItem->deleted) { |
| 455 |
groupItem->source->remove(parameter); |
| 456 |
} |
| 457 |
|
| 458 |
groupItem->deleted.clear(); |
| 459 |
} |
| 460 |
|
| 461 |
if (const auto& parameterItem = item<ParameterItem>(index)) { |
| 462 |
const auto& groupItem = item<GroupItem>(index.parent()); |
| 463 |
|
| 464 |
groupItem->source->define(parameterItem->token); |
| 465 |
} |
| 466 |
}); |
| 467 |
|
| 468 |
for (auto* source : sources) { |
| 469 |
source->flush(); |
| 470 |
} |
| 471 |
|
| 472 |
reset(); |
| 473 |
} |
| 474 |
|
| 475 |
int StyleParametersModel::rowCount(const QModelIndex& index) const |
| 476 |
{ |
| 477 |
if (index.column() > 0) { |
| 478 |
return 0; |
| 479 |
} |
| 480 |
|
| 481 |
int childCount = node(index)->childCount(); |
| 482 |
|
| 483 |
if (const auto& groupItem = item<GroupItem>(index)) { |
| 484 |
return childCount + (groupItem->canAddNewParameters ? 1 : 0); |
| 485 |
} |
| 486 |
|
| 487 |
return childCount; |
| 488 |
} |
| 489 |
|
| 490 |
int StyleParametersModel::columnCount([[maybe_unused]] const QModelIndex& index) const |
| 491 |
{ |
| 492 |
return ColumnCount; |
| 493 |
} |
| 494 |
|
| 495 |
QVariant StyleParametersModel::headerData(int section, Qt::Orientation orientation, int role) const |
| 496 |
{ |
| 497 |
if (orientation == Qt::Horizontal && role == Qt::DisplayRole) { |
| 498 |
switch (section) { |
| 499 |
case ParameterName: |
| 500 |
return tr("Name"); |
| 501 |
case ParameterExpression: |
| 502 |
return tr("Expression"); |
| 503 |
case ParameterPreview: |
| 504 |
return tr("Preview"); |
| 505 |
case ParameterType: |
| 506 |
return tr("Type"); |
| 507 |
default: |
| 508 |
return {}; |
| 509 |
} |
| 510 |
} |
| 511 |
|
| 512 |
return {}; |
| 513 |
} |
| 514 |
|
| 515 |
QVariant StyleParametersModel::data(const QModelIndex& index, int role) const |
| 516 |
{ |
| 517 |
if (auto parameterItem = item<ParameterItem>(index)) { |
| 518 |
const auto& [name, token, _] = *parameterItem; |
| 519 |
const auto& value = manager->resolve(name.toStdString()); |
| 520 |
|
| 521 |
if (!value) { |
| 522 |
return {}; |
| 523 |
} |
| 524 |
|
| 525 |
if (role == Qt::DisplayRole) { |
| 526 |
if (index.column() == ParameterName) { |
| 527 |
return name; |
| 528 |
} |
| 529 |
if (index.column() == ParameterExpression) { |
| 530 |
return QString::fromStdString(token.value); |
| 531 |
} |
| 532 |
if (index.column() == ParameterType) { |
| 533 |
return typeOfTokenValue(*value); |
| 534 |
} |
| 535 |
if (index.column() == ParameterPreview) { |
| 536 |
return QString::fromStdString(value->toString()); |
| 537 |
} |
| 538 |
} |
| 539 |
|
| 540 |
if (role == Qt::DecorationRole) { |
| 541 |
if (index.column() == ParameterPreview && std::holds_alternative<Base::Color>(*value)) { |
| 542 |
return colorPreview(std::get<Base::Color>(*value).asValue<QColor>()); |
| 543 |
} |
| 544 |
} |
| 545 |
} |
| 546 |
|
| 547 |
if (auto groupItem = item<GroupItem>(index)) { |
| 548 |
if (role == Qt::DisplayRole && index.column() == ParameterName) { |
| 549 |
return groupItem->title; |
| 550 |
} |
| 551 |
} |
| 552 |
|
| 553 |
return {}; |
| 554 |
} |
| 555 |
|
| 556 |
bool StyleParametersModel::setData( |
| 557 |
const QModelIndex& index, |
| 558 |
const QVariant& value, |
| 559 |
[[maybe_unused]] int role |
| 560 |
) |
| 561 |
{ |
| 562 |
if (auto parameterItem = item<ParameterItem>(index)) { |
| 563 |
auto groupItem = item<GroupItem>(index.parent()); |
| 564 |
|
| 565 |
if (index.column() == ParameterName) { |
| 566 |
QString newName = value.toString(); |
| 567 |
|
| 568 |
StyleParameters::Parameter newToken = parameterItem->token; |
| 569 |
newToken.name = newName.toStdString(); |
| 570 |
|
| 571 |
// there is no rename operation, so we need to mark the previous token as deleted |
| 572 |
groupItem->deleted.insert(parameterItem->token.name); |
| 573 |
|
| 574 |
parameterItem->name = newName; |
| 575 |
parameterItem->token = newToken; |
| 576 |
} |
| 577 |
|
| 578 |
if (index.column() == ParameterExpression) { |
| 579 |
QString newValue = value.toString(); |
| 580 |
|
| 581 |
StyleParameters::Parameter newToken = parameterItem->token; |
| 582 |
newToken.value = newValue.toStdString(); |
| 583 |
|
| 584 |
parameterItem->token = newToken; |
| 585 |
} |
| 586 |
} |
| 587 |
|
| 588 |
if (isAddPlaceholder(index)) { |
| 589 |
if (index.column() == ParameterName) { |
| 590 |
QString newName = value.toString(); |
| 591 |
|
| 592 |
if (newName.isEmpty()) { |
| 593 |
return false; |
| 594 |
} |
| 595 |
|
| 596 |
StyleParameters::Parameter token {.name = newName.toStdString(), .value = ""}; |
| 597 |
|
| 598 |
int start = rowCount(index.parent()); |
| 599 |
|
| 600 |
beginInsertRows(index.parent(), start, start + 1); |
| 601 |
auto item = std::make_unique<Node>(std::make_unique<ParameterItem>(newName, token)); |
| 602 |
node(index.parent())->appendChild(std::move(item)); |
| 603 |
endInsertRows(); |
| 604 |
|
| 605 |
// this must be queued to basically next frame so widget has a chance to update |
| 606 |
QTimer::singleShot(0, [this, index]() { this->newParameterAdded(index); }); |
| 607 |
} |
| 608 |
} |
| 609 |
|
| 610 |
this->manager->reload(); |
| 611 |
|
| 612 |
QtTools::walkTreeModel(this, [this](const QModelIndex& index) { |
| 613 |
const QModelIndex previewColumnIndex = index.siblingAtColumn(ParameterPreview); |
| 614 |
|
| 615 |
Q_EMIT dataChanged(previewColumnIndex, previewColumnIndex); |
| 616 |
}); |
| 617 |
|
| 618 |
return true; |
| 619 |
} |
| 620 |
|
| 621 |
Qt::ItemFlags StyleParametersModel::flags(const QModelIndex& index) const |
| 622 |
{ |
| 623 |
if (auto parameterItem = item<ParameterItem>(index)) { |
| 624 |
if (index.column() == ParameterName || index.column() == ParameterExpression) { |
| 625 |
return parameterItem->flags | QAbstractItemModel::flags(index); |
| 626 |
} |
| 627 |
} |
| 628 |
|
| 629 |
if (isAddPlaceholder(index)) { |
| 630 |
if (index.column() == ParameterName) { |
| 631 |
return Qt::ItemIsEnabled | Qt::ItemIsEditable | QAbstractItemModel::flags(index); |
| 632 |
} |
| 633 |
} |
| 634 |
|
| 635 |
return QAbstractItemModel::flags(index); |
| 636 |
} |
| 637 |
|
| 638 |
QModelIndex StyleParametersModel::index(int row, int col, const QModelIndex& parent) const |
| 639 |
{ |
| 640 |
if (!hasIndex(row, col, parent)) { |
| 641 |
return {}; |
| 642 |
} |
| 643 |
|
| 644 |
if (auto child = node(parent)->child(row)) { |
| 645 |
return createIndex(row, col, child); |
| 646 |
} |
| 647 |
|
| 648 |
return {}; |
| 649 |
} |
| 650 |
|
| 651 |
QModelIndex StyleParametersModel::parent(const QModelIndex& index) const |
| 652 |
{ |
| 653 |
if (!index.isValid()) { |
| 654 |
return {}; |
| 655 |
} |
| 656 |
|
| 657 |
auto node = static_cast<Node*>(index.internalPointer()); |
| 658 |
auto parent = node->parent(); |
| 659 |
|
| 660 |
if (!parent || parent == root.get()) { |
| 661 |
return {}; |
| 662 |
} |
| 663 |
|
| 664 |
return createIndex(parent->row(), 0, parent); |
| 665 |
} |
| 666 |
|
| 667 |
bool StyleParametersModel::isAddPlaceholder(const QModelIndex& index) const |
| 668 |
{ |
| 669 |
return item(index) == nullptr; |
| 670 |
} |
| 671 |
|
| 672 |
StyleParametersModel::Node* StyleParametersModel::node(const QModelIndex& index) const |
| 673 |
{ |
| 674 |
return index.isValid() ? static_cast<Node*>(index.internalPointer()) : root.get(); |
| 675 |
} |
| 676 |
|
| 677 |
StyleParametersModel::Item* StyleParametersModel::item(const QModelIndex& index) const |
| 678 |
{ |
| 679 |
return node(index)->data(); |
| 680 |
} |
| 681 |
|
| 682 |
DlgThemeEditor::DlgThemeEditor(QWidget* parent) |
| 683 |
: QDialog(parent) |
| 684 |
, ui(new Ui::DlgThemeEditor) |
| 685 |
, model( |
| 686 |
std::make_unique<StyleParametersModel>( |
| 687 |
Base::provideServiceImplementations<StyleParameters::ParameterSource>(), |
| 688 |
this |
| 689 |
) |
| 690 |
) |
| 691 |
{ |
| 692 |
ui->setupUi(this); |
| 693 |
|
| 694 |
ui->tokensTreeView->setMouseTracking(true); |
| 695 |
ui->tokensTreeView->setItemDelegate(new Delegate(ui->tokensTreeView)); |
| 696 |
ui->tokensTreeView->setModel(model.get()); |
| 697 |
|
| 698 |
constexpr int typeColumnWidth = 80; |
| 699 |
constexpr int nameColumnWidth = 200; |
| 700 |
|
| 701 |
struct ColumnDefinition // NOLINT(*-pro-type-member-init) |
| 702 |
{ |
| 703 |
StyleParametersModel::Column column; |
| 704 |
QHeaderView::ResizeMode mode; |
| 705 |
qsizetype defaultWidth = 0; |
| 706 |
}; |
| 707 |
|
| 708 |
static constexpr std::initializer_list<ColumnDefinition> columnSizingDefinitions = { |
| 709 |
{StyleParametersModel::ParameterName, QHeaderView::ResizeMode::ResizeToContents}, |
| 710 |
{StyleParametersModel::ParameterExpression, QHeaderView::ResizeMode::Stretch}, |
| 711 |
{StyleParametersModel::ParameterPreview, QHeaderView::ResizeMode::Stretch}, |
| 712 |
{StyleParametersModel::ParameterType, QHeaderView::ResizeMode::Fixed, typeColumnWidth}, |
| 713 |
}; |
| 714 |
|
| 715 |
for (const auto& [column, mode, defaultWidth] : columnSizingDefinitions) { |
| 716 |
ui->tokensTreeView->header()->setSectionResizeMode(column, mode); |
| 717 |
|
| 718 |
if (defaultWidth > 0) { |
| 719 |
ui->tokensTreeView->header()->setDefaultSectionSize(defaultWidth); |
| 720 |
} |
| 721 |
} |
| 722 |
|
| 723 |
ui->tokensTreeView->setColumnWidth(StyleParametersModel::ParameterName, nameColumnWidth); |
| 724 |
ui->tokensTreeView->expandAll(); |
| 725 |
|
| 726 |
connect(ui->buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept); |
| 727 |
connect(ui->buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject); |
| 728 |
|
| 729 |
connect(ui->buttonBox, &QDialogButtonBox::clicked, this, &DlgThemeEditor::handleButtonClick); |
| 730 |
|
| 731 |
connect( |
| 732 |
ui->tokensTreeView, |
| 733 |
&TokenTreeView::requestRemove, |
| 734 |
model.get(), |
| 735 |
qOverload<const QModelIndex&>(&StyleParametersModel::removeItem) |
| 736 |
); |
| 737 |
|
| 738 |
connect(model.get(), &StyleParametersModel::modelReset, ui->tokensTreeView, [this] { |
| 739 |
ui->tokensTreeView->expandAll(); |
| 740 |
}); |
| 741 |
connect(model.get(), &StyleParametersModel::newParameterAdded, this, [this](const QModelIndex& index) { |
| 742 |
const auto newParameterExpressionIndex = index.siblingAtColumn( |
| 743 |
StyleParametersModel::ParameterExpression |
| 744 |
); |
| 745 |
|
| 746 |
ui->tokensTreeView->scrollTo(newParameterExpressionIndex); |
| 747 |
ui->tokensTreeView->setCurrentIndex(newParameterExpressionIndex); |
| 748 |
ui->tokensTreeView->edit(newParameterExpressionIndex); |
| 749 |
}); |
| 750 |
} |
| 751 |
|
| 752 |
DlgThemeEditor::~DlgThemeEditor() = default; |
| 753 |
|
| 754 |
void DlgThemeEditor::handleButtonClick(QAbstractButton* button) |
| 755 |
{ |
| 756 |
auto role = ui->buttonBox->buttonRole(button); |
| 757 |
|
| 758 |
switch (role) { |
| 759 |
case QDialogButtonBox::ApplyRole: |
| 760 |
case QDialogButtonBox::AcceptRole: |
| 761 |
model->flush(); |
| 762 |
Application::Instance->reloadStyleSheet(); |
| 763 |
break; |
| 764 |
case QDialogButtonBox::ResetRole: |
| 765 |
model->reset(); |
| 766 |
break; |
| 767 |
default: |
| 768 |
// no-op |
| 769 |
break; |
| 770 |
} |
| 771 |
} |
| 772 |
|
| 773 |
} // namespace Gui |
| 774 |
|
| 775 |
#include "DlgThemeEditor.moc" |