| 1 | #include "./paletteeditor.h" |
| 2 | #include "./colorbutton.h" |
| 3 | |
| 4 | #include "ui_paletteeditor.h" |
| 5 | |
| 6 | #include <QFileDialog> |
| 7 | #include <QHeaderView> |
| 8 | #include <QLabel> |
| 9 | #include <QMessageBox> |
| 10 | #include <QMetaProperty> |
| 11 | #include <QPainter> |
| 12 | #include <QPushButton> |
| 13 | #include <QSettings> |
| 14 | #include <QStyle> |
| 15 | #include <QToolButton> |
| 16 | |
| 17 | #include <type_traits> |
| 18 | |
| 19 | namespace QtUtilities { |
| 20 | |
| 21 | enum { BrushRole = 33 }; |
| 22 | |
| 23 | PaletteEditor::PaletteEditor(QWidget *parent) |
| 24 | : QDialog(parent) |
| 25 | , m_ui(new Ui::PaletteEditor) |
| 26 | , m_currentColorGroup(QPalette::Active) |
| 27 | , m_paletteModel(new PaletteModel(this)) |
| 28 | , m_modelUpdated(false) |
| 29 | , m_paletteUpdated(false) |
| 30 | , m_compute(true) |
| 31 | { |
| 32 | m_ui->setupUi(this); |
| 33 | m_ui->paletteView->setModel(m_paletteModel); |
| 34 | updateStyledButton(); |
| 35 | m_ui->paletteView->setModel(m_paletteModel); |
| 36 | auto *const delegate = new ColorDelegate(this); |
| 37 | m_ui->paletteView->setItemDelegate(delegate); |
| 38 | m_ui->paletteView->setEditTriggers(QAbstractItemView::AllEditTriggers); |
| 39 | m_ui->paletteView->setSelectionBehavior(QAbstractItemView::SelectRows); |
| 40 | m_ui->paletteView->setDragEnabled(true); |
| 41 | m_ui->paletteView->setDropIndicatorShown(true); |
| 42 | m_ui->paletteView->setRootIsDecorated(false); |
| 43 | m_ui->paletteView->setColumnHidden(2, true); |
| 44 | m_ui->paletteView->setColumnHidden(3, true); |
| 45 | |
| 46 | auto saveButton = m_ui->buttonBox->addButton(tr("Save…"), QDialogButtonBox::ActionRole); |
| 47 | connect(saveButton, &QPushButton::clicked, this, &PaletteEditor::save); |
| 48 | auto loadButton = m_ui->buttonBox->addButton(tr("Load…"), QDialogButtonBox::ActionRole); |
| 49 | connect(loadButton, &QPushButton::clicked, this, &PaletteEditor::load); |
| 50 | |
| 51 | connect(m_paletteModel, &PaletteModel::paletteChanged, this, &PaletteEditor::paletteChanged); |
| 52 | connect(m_ui->buildButton, &ColorButton::colorChanged, this, &PaletteEditor::buildPalette); |
| 53 | connect(m_ui->computeRadio, &QRadioButton::clicked, this, &PaletteEditor::handleComputeRadioClicked); |
| 54 | connect(m_ui->detailsRadio, &QRadioButton::clicked, this, &PaletteEditor::handleDetailsRadioClicked); |
| 55 | } |
| 56 | |
| 57 | PaletteEditor::~PaletteEditor() |
| 58 | { |
| 59 | } |
| 60 | |
| 61 | QPalette PaletteEditor::palette() const |
| 62 | { |
| 63 | return m_editPalette; |
| 64 | } |
| 65 | |
| 66 | void PaletteEditor::setPalette(const QPalette &palette) |
| 67 | { |
| 68 | m_editPalette = palette; |
| 69 | const auto mask = palette. |
| 70 | #if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)) |
| 71 | resolveMask() |
| 72 | #else |
| 73 | resolve() |
| 74 | #endif |
| 75 | ; |
| 76 | using MaskType = std::remove_cv_t<decltype(mask)>; |
| 77 | for (int i = 0; i < static_cast<int>(QPalette::NColorRoles); ++i) { |
| 78 | if (mask & (static_cast<MaskType>(1) << static_cast<MaskType>(i))) { |
| 79 | continue; |
| 80 | } |
| 81 | m_editPalette.setBrush( |
| 82 | QPalette::Active, static_cast<QPalette::ColorRole>(i), m_parentPalette.brush(QPalette::Active, static_cast<QPalette::ColorRole>(i))); |
| 83 | m_editPalette.setBrush( |
| 84 | QPalette::Inactive, static_cast<QPalette::ColorRole>(i), m_parentPalette.brush(QPalette::Inactive, static_cast<QPalette::ColorRole>(i))); |
| 85 | m_editPalette.setBrush( |
| 86 | QPalette::Disabled, static_cast<QPalette::ColorRole>(i), m_parentPalette.brush(QPalette::Disabled, static_cast<QPalette::ColorRole>(i))); |
| 87 | } |
| 88 | m_editPalette. |
| 89 | #if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)) |
| 90 | setResolveMask(mask); |
| 91 | m_editPalette = m_editPalette.resolve(m_editPalette) |
| 92 | #else |
| 93 | resolve(mask) |
| 94 | #endif |
| 95 | ; |
| 96 | updateStyledButton(); |
| 97 | m_paletteUpdated = true; |
| 98 | if (!m_modelUpdated) { |
| 99 | m_paletteModel->setPalette(m_editPalette, m_parentPalette); |
| 100 | } |
| 101 | m_paletteUpdated = false; |
| 102 | } |
| 103 | |
| 104 | void PaletteEditor::setPalette(const QPalette &palette, const QPalette &parentPalette) |
| 105 | { |
| 106 | m_parentPalette = parentPalette; |
| 107 | setPalette(palette); |
| 108 | } |
| 109 | |
| 110 | bool PaletteEditor::event(QEvent *event) |
| 111 | { |
| 112 | switch (event->type()) { |
| 113 | case QEvent::LanguageChange: |
| 114 | m_ui->retranslateUi(this); |
| 115 | break; |
| 116 | default:; |
| 117 | } |
| 118 | return QDialog::event(event); |
| 119 | } |
| 120 | |
| 121 | void PaletteEditor::handleComputeRadioClicked() |
| 122 | { |
| 123 | if (m_compute) { |
| 124 | return; |
| 125 | } |
| 126 | m_ui->paletteView->setColumnHidden(2, true); |
| 127 | m_ui->paletteView->setColumnHidden(3, true); |
| 128 | m_compute = true; |
| 129 | m_paletteModel->setCompute(true); |
| 130 | } |
| 131 | |
| 132 | void PaletteEditor::handleDetailsRadioClicked() |
| 133 | { |
| 134 | if (!m_compute) { |
| 135 | return; |
| 136 | } |
| 137 | const int w = m_ui->paletteView->columnWidth(1); |
| 138 | m_ui->paletteView->setColumnHidden(2, false); |
| 139 | m_ui->paletteView->setColumnHidden(3, false); |
| 140 | auto *const header = m_ui->paletteView->header(); |
| 141 | header->resizeSection(1, w / 3); |
| 142 | header->resizeSection(2, w / 3); |
| 143 | header->resizeSection(3, w / 3); |
| 144 | m_compute = false; |
| 145 | m_paletteModel->setCompute(false); |
| 146 | } |
| 147 | |
| 148 | static inline QString paletteSuffix() |
| 149 | { |
| 150 | return QStringLiteral("ini"); |
| 151 | } |
| 152 | |
| 153 | static inline QString paletteFilter() |
| 154 | { |
| 155 | return PaletteEditor::tr("Color palette configuration (*.ini)"); |
| 156 | } |
| 157 | |
| 158 | static bool loadPalette(const QString &fileName, QPalette *pal, QString *errorMessage) |
| 159 | { |
| 160 | const auto settings = QSettings(fileName, QSettings::IniFormat); |
| 161 | if (settings.status() != QSettings::NoError) { |
| 162 | *errorMessage = PaletteEditor::tr("Unable to load \"%1\".").arg(fileName); |
| 163 | return false; |
| 164 | } |
| 165 | const auto value = settings.value(QStringLiteral("palette")); |
| 166 | if (!value.isValid() || !value.canConvert<QPalette>()) { |
| 167 | *errorMessage = PaletteEditor::tr("\"%1\" does not contain a valid palette.").arg(fileName); |
| 168 | return false; |
| 169 | } |
| 170 | *pal = settings.value(QStringLiteral("palette")).value<QPalette>(); |
| 171 | return true; |
| 172 | } |
| 173 | |
| 174 | static bool savePalette(const QString &fileName, const QPalette &pal, QString *errorMessage) |
| 175 | { |
| 176 | auto settings = QSettings(fileName, QSettings::IniFormat); |
| 177 | settings.setValue(QStringLiteral("palette"), QVariant(pal)); |
| 178 | settings.sync(); |
| 179 | if (settings.status() != QSettings::NoError) { |
| 180 | *errorMessage = PaletteEditor::tr("Unable to write \"%1\".").arg(fileName); |
| 181 | return false; |
| 182 | } |
| 183 | return true; |
| 184 | } |
| 185 | |
| 186 | void PaletteEditor::load() |
| 187 | { |
| 188 | auto dialog = QFileDialog(this, tr("Load palette"), QString(), paletteFilter()); |
| 189 | dialog.setAcceptMode(QFileDialog::AcceptOpen); |
| 190 | if (dialog.exec() != QDialog::Accepted) { |
| 191 | return; |
| 192 | } |
| 193 | auto pal = QPalette(); |
| 194 | auto errorMessage = QString(); |
| 195 | if (loadPalette(dialog.selectedFiles().constFirst(), &pal, &errorMessage)) { |
| 196 | setPalette(pal); |
| 197 | // apply again as otherwise highlight and possibly other roles are not shown until the next restart |
| 198 | setPalette(pal, pal); |
| 199 | } else { |
| 200 | QMessageBox::warning(this, tr("Error reading palette"), errorMessage); |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | void PaletteEditor::save() |
| 205 | { |
| 206 | auto dialog = QFileDialog(this, tr("Save palette"), QString(), paletteFilter()); |
| 207 | dialog.setAcceptMode(QFileDialog::AcceptSave); |
| 208 | dialog.setDefaultSuffix(paletteSuffix()); |
| 209 | if (dialog.exec() != QDialog::Accepted) { |
| 210 | return; |
| 211 | } |
| 212 | auto errorMessage = QString(); |
| 213 | if (!savePalette(dialog.selectedFiles().constFirst(), palette(), &errorMessage)) { |
| 214 | QMessageBox::warning(this, tr("Error writing palette"), errorMessage); |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | void PaletteEditor::paletteChanged(const QPalette &palette) |
| 219 | { |
| 220 | m_modelUpdated = true; |
| 221 | if (!m_paletteUpdated) { |
| 222 | setPalette(palette); |
| 223 | } |
| 224 | m_modelUpdated = false; |
| 225 | } |
| 226 | |
| 227 | void PaletteEditor::buildPalette() |
| 228 | { |
| 229 | const QColor btn(m_ui->buildButton->color()); |
| 230 | const QPalette temp(btn); |
| 231 | setPalette(temp); |
| 232 | } |
| 233 | |
| 234 | void PaletteEditor::updateStyledButton() |
| 235 | { |
| 236 | m_ui->buildButton->setColor(palette().color(QPalette::Active, QPalette::Button)); |
| 237 | } |
| 238 | |
| 239 | QPalette PaletteEditor::getPalette(QWidget *parent, const QPalette &init, const QPalette &parentPal, int *ok) |
| 240 | { |
| 241 | PaletteEditor dlg(parent); |
| 242 | auto parentPalette(parentPal); |
| 243 | const auto mask = init. |
| 244 | #if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)) |
| 245 | resolveMask() |
| 246 | #else |
| 247 | resolve() |
| 248 | #endif |
| 249 | ; |
| 250 | using MaskType = std::remove_cv_t<decltype(mask)>; |
| 251 | for (int i = 0; i < static_cast<int>(QPalette::NColorRoles); ++i) { |
| 252 | if (mask & (static_cast<MaskType>(1) << static_cast<MaskType>(i))) { |
| 253 | continue; |
| 254 | } |
| 255 | parentPalette.setBrush( |
| 256 | QPalette::Active, static_cast<QPalette::ColorRole>(i), init.brush(QPalette::Active, static_cast<QPalette::ColorRole>(i))); |
| 257 | parentPalette.setBrush( |
| 258 | QPalette::Inactive, static_cast<QPalette::ColorRole>(i), init.brush(QPalette::Inactive, static_cast<QPalette::ColorRole>(i))); |
| 259 | parentPalette.setBrush( |
| 260 | QPalette::Disabled, static_cast<QPalette::ColorRole>(i), init.brush(QPalette::Disabled, static_cast<QPalette::ColorRole>(i))); |
| 261 | } |
| 262 | dlg.setPalette(init, parentPalette); |
| 263 | |
| 264 | const int result = dlg.exec(); |
| 265 | if (ok) { |
| 266 | *ok = result; |
| 267 | } |
| 268 | return result == QDialog::Accepted ? dlg.palette() : init; |
| 269 | } |
| 270 | |
| 271 | PaletteModel::PaletteModel(QObject *parent) |
| 272 | : QAbstractTableModel(parent) |
| 273 | , m_compute(true) |
| 274 | { |
| 275 | const QMetaObject *meta = metaObject(); |
| 276 | const QMetaProperty property = meta->property(meta->indexOfProperty("colorRole")); |
| 277 | const QMetaEnum enumerator = property.enumerator(); |
| 278 | for (int r = QPalette::WindowText; r < QPalette::NColorRoles; ++r) { |
| 279 | m_roleNames[static_cast<QPalette::ColorRole>(r)] = QLatin1String(enumerator.key(r)); |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | int PaletteModel::rowCount(const QModelIndex &) const |
| 284 | { |
| 285 | return static_cast<int>(m_roleNames.count()); |
| 286 | } |
| 287 | |
| 288 | int PaletteModel::columnCount(const QModelIndex &) const |
| 289 | { |
| 290 | return 4; |
| 291 | } |
| 292 | |
| 293 | QVariant PaletteModel::data(const QModelIndex &index, int role) const |
| 294 | { |
| 295 | if (!index.isValid() || index.row() < 0 || index.row() >= QPalette::NColorRoles || index.column() < 0 || index.column() >= 4) { |
| 296 | return QVariant(); |
| 297 | } |
| 298 | |
| 299 | if (index.column() == 0) { |
| 300 | if (role == Qt::DisplayRole) { |
| 301 | return m_roleNames[static_cast<QPalette::ColorRole>(index.row())]; |
| 302 | } |
| 303 | if (role == Qt::EditRole) { |
| 304 | const auto mask = m_palette. |
| 305 | #if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)) |
| 306 | resolveMask() |
| 307 | #else |
| 308 | resolve() |
| 309 | #endif |
| 310 | ; |
| 311 | using MaskType = std::remove_cv_t<decltype(mask)>; |
| 312 | return mask & (static_cast<MaskType>(1) << static_cast<MaskType>(index.row())); |
| 313 | } |
| 314 | return QVariant(); |
| 315 | } |
| 316 | if (role == BrushRole) { |
| 317 | return m_palette.brush(columnToGroup(index.column()), static_cast<QPalette::ColorRole>(index.row())); |
| 318 | } |
| 319 | return QVariant(); |
| 320 | } |
| 321 | |
| 322 | bool PaletteModel::setData(const QModelIndex &index, const QVariant &value, int role) |
| 323 | { |
| 324 | if (!index.isValid()) { |
| 325 | return false; |
| 326 | } |
| 327 | |
| 328 | if (index.column() != 0 && role == BrushRole) { |
| 329 | const QBrush br = qvariant_cast<QBrush>(value); |
| 330 | const QPalette::ColorRole r = static_cast<QPalette::ColorRole>(index.row()); |
| 331 | const QPalette::ColorGroup g = columnToGroup(index.column()); |
| 332 | m_palette.setBrush(g, r, br); |
| 333 | |
| 334 | QModelIndex idxBegin = PaletteModel::index(r, 0); |
| 335 | QModelIndex idxEnd = PaletteModel::index(r, 3); |
| 336 | if (m_compute) { |
| 337 | m_palette.setBrush(QPalette::Inactive, r, br); |
| 338 | switch (r) { |
| 339 | case QPalette::WindowText: |
| 340 | case QPalette::Text: |
| 341 | case QPalette::ButtonText: |
| 342 | case QPalette::Base: |
| 343 | break; |
| 344 | case QPalette::Dark: |
| 345 | m_palette.setBrush(QPalette::Disabled, QPalette::WindowText, br); |
| 346 | m_palette.setBrush(QPalette::Disabled, QPalette::Dark, br); |
| 347 | m_palette.setBrush(QPalette::Disabled, QPalette::Text, br); |
| 348 | m_palette.setBrush(QPalette::Disabled, QPalette::ButtonText, br); |
| 349 | idxBegin = PaletteModel::index(0, 0); |
| 350 | idxEnd = PaletteModel::index(static_cast<int>(m_roleNames.count()) - 1, 3); |
| 351 | break; |
| 352 | case QPalette::Window: |
| 353 | m_palette.setBrush(QPalette::Disabled, QPalette::Base, br); |
| 354 | m_palette.setBrush(QPalette::Disabled, QPalette::Window, br); |
| 355 | idxBegin = PaletteModel::index(QPalette::Base, 0); |
| 356 | break; |
| 357 | case QPalette::Highlight: |
| 358 | break; |
| 359 | default: |
| 360 | m_palette.setBrush(QPalette::Disabled, r, br); |
| 361 | break; |
| 362 | } |
| 363 | } |
| 364 | emit paletteChanged(m_palette); |
| 365 | emit dataChanged(idxBegin, idxEnd); |
| 366 | return true; |
| 367 | } |
| 368 | if (index.column() == 0 && role == Qt::EditRole) { |
| 369 | auto mask = m_palette. |
| 370 | #if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)) |
| 371 | resolveMask() |
| 372 | #else |
| 373 | resolve() |
| 374 | #endif |
| 375 | ; |
| 376 | const bool isMask = qvariant_cast<bool>(value); |
| 377 | const int r = index.row(); |
| 378 | if (isMask) { |
| 379 | mask |= (static_cast<decltype(mask)>(1) << static_cast<decltype(mask)>(r)); |
| 380 | } else { |
| 381 | m_palette.setBrush( |
| 382 | QPalette::Active, static_cast<QPalette::ColorRole>(r), m_parentPalette.brush(QPalette::Active, static_cast<QPalette::ColorRole>(r))); |
| 383 | m_palette.setBrush(QPalette::Inactive, static_cast<QPalette::ColorRole>(r), |
| 384 | m_parentPalette.brush(QPalette::Inactive, static_cast<QPalette::ColorRole>(r))); |
| 385 | m_palette.setBrush(QPalette::Disabled, static_cast<QPalette::ColorRole>(r), |
| 386 | m_parentPalette.brush(QPalette::Disabled, static_cast<QPalette::ColorRole>(r))); |
| 387 | mask &= ~static_cast<decltype(mask)>((static_cast<decltype(mask)>(1) << static_cast<decltype(mask)>(index.row()))); |
| 388 | } |
| 389 | m_palette. |
| 390 | #if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)) |
| 391 | setResolveMask(mask); |
| 392 | m_palette = m_palette.resolve(m_palette) |
| 393 | #else |
| 394 | resolve(mask) |
| 395 | #endif |
| 396 | ; |
| 397 | emit paletteChanged(m_palette); |
| 398 | const QModelIndex idxEnd = PaletteModel::index(r, 3); |
| 399 | emit dataChanged(index, idxEnd); |
| 400 | return true; |
| 401 | } |
| 402 | return false; |
| 403 | } |
| 404 | |
| 405 | Qt::ItemFlags PaletteModel::flags(const QModelIndex &index) const |
| 406 | { |
| 407 | if (!index.isValid()) |
| 408 | return Qt::ItemIsEnabled; |
| 409 | return Qt::ItemIsEditable | Qt::ItemIsEnabled; |
| 410 | } |
| 411 | |
| 412 | QVariant PaletteModel::headerData(int section, Qt::Orientation orientation, int role) const |
| 413 | { |
| 414 | if (orientation == Qt::Horizontal && role == Qt::DisplayRole) { |
| 415 | if (section == 0) |
| 416 | return tr("Color Role"); |
| 417 | if (section == groupToColumn(QPalette::Active)) |
| 418 | return tr("Active"); |
| 419 | if (section == groupToColumn(QPalette::Inactive)) |
| 420 | return tr("Inactive"); |
| 421 | if (section == groupToColumn(QPalette::Disabled)) |
| 422 | return tr("Disabled"); |
| 423 | } |
| 424 | return QVariant(); |
| 425 | } |
| 426 | |
| 427 | QPalette PaletteModel::getPalette() const |
| 428 | { |
| 429 | return m_palette; |
| 430 | } |
| 431 | |
| 432 | void PaletteModel::setPalette(const QPalette &palette, const QPalette &parentPalette) |
| 433 | { |
| 434 | m_parentPalette = parentPalette; |
| 435 | m_palette = palette; |
| 436 | const QModelIndex idxBegin = index(0, 0); |
| 437 | const QModelIndex idxEnd = index(static_cast<int>(m_roleNames.count()) - 1, 3); |
| 438 | emit dataChanged(idxBegin, idxEnd); |
| 439 | } |
| 440 | |
| 441 | QPalette::ColorGroup PaletteModel::columnToGroup(int index) const |
| 442 | { |
| 443 | if (index == 1) |
| 444 | return QPalette::Active; |
| 445 | if (index == 2) |
| 446 | return QPalette::Inactive; |
| 447 | return QPalette::Disabled; |
| 448 | } |
| 449 | |
| 450 | int PaletteModel::groupToColumn(QPalette::ColorGroup group) const |
| 451 | { |
| 452 | if (group == QPalette::Active) |
| 453 | return 1; |
| 454 | if (group == QPalette::Inactive) |
| 455 | return 2; |
| 456 | return 3; |
| 457 | } |
| 458 | |
| 459 | BrushEditor::BrushEditor(QWidget *parent) |
| 460 | : QWidget(parent) |
| 461 | , m_button(new ColorButton(this)) |
| 462 | , m_changed(false) |
| 463 | { |
| 464 | auto *const layout = new QHBoxLayout(this); |
| 465 | layout->setContentsMargins(0, 0, 0, 0); |
| 466 | layout->addWidget(m_button); |
| 467 | connect(m_button, &ColorButton::colorChanged, this, &BrushEditor::brushChanged); |
| 468 | setFocusProxy(m_button); |
| 469 | } |
| 470 | |
| 471 | void BrushEditor::setBrush(const QBrush &brush) |
| 472 | { |
| 473 | m_button->setColor(brush.color()); |
| 474 | m_changed = false; |
| 475 | } |
| 476 | |
| 477 | QBrush BrushEditor::brush() const |
| 478 | { |
| 479 | return QBrush(m_button->color()); |
| 480 | } |
| 481 | |
| 482 | void BrushEditor::brushChanged() |
| 483 | { |
| 484 | m_changed = true; |
| 485 | emit changed(this); |
| 486 | } |
| 487 | |
| 488 | bool BrushEditor::changed() const |
| 489 | { |
| 490 | return m_changed; |
| 491 | } |
| 492 | |
| 493 | RoleEditor::RoleEditor(QWidget *parent) |
| 494 | : QWidget(parent) |
| 495 | , m_label(new QLabel(this)) |
| 496 | , m_edited(false) |
| 497 | { |
| 498 | QHBoxLayout *layout = new QHBoxLayout(this); |
| 499 | layout->setContentsMargins(0, 0, 0, 0); |
| 500 | layout->setSpacing(0); |
| 501 | |
| 502 | layout->addWidget(m_label); |
| 503 | m_label->setAutoFillBackground(true); |
| 504 | m_label->setIndent(3); // same value as textMargin in QItemDelegate |
| 505 | setFocusProxy(m_label); |
| 506 | |
| 507 | auto *const button = new QToolButton(this); |
| 508 | button->setToolButtonStyle(Qt::ToolButtonIconOnly); |
| 509 | button->setIcon(QIcon::fromTheme(QStringLiteral("edit-clear"))); |
| 510 | button->setIconSize(QSize(8, 8)); |
| 511 | button->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::MinimumExpanding)); |
| 512 | layout->addWidget(button); |
| 513 | connect(button, &QAbstractButton::clicked, this, &RoleEditor::emitResetProperty); |
| 514 | } |
| 515 | |
| 516 | void RoleEditor::setLabel(const QString &label) |
| 517 | { |
| 518 | m_label->setText(label); |
| 519 | } |
| 520 | |
| 521 | void RoleEditor::setEdited(bool on) |
| 522 | { |
| 523 | QFont font; |
| 524 | if (on == true) { |
| 525 | font.setBold(on); |
| 526 | } |
| 527 | m_label->setFont(font); |
| 528 | m_edited = on; |
| 529 | } |
| 530 | |
| 531 | bool RoleEditor::edited() const |
| 532 | { |
| 533 | return m_edited; |
| 534 | } |
| 535 | |
| 536 | void RoleEditor::emitResetProperty() |
| 537 | { |
| 538 | setEdited(false); |
| 539 | emit changed(this); |
| 540 | } |
| 541 | |
| 542 | ColorDelegate::ColorDelegate(QObject *parent) |
| 543 | : QItemDelegate(parent) |
| 544 | { |
| 545 | } |
| 546 | |
| 547 | QWidget *ColorDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &, const QModelIndex &index) const |
| 548 | { |
| 549 | if (index.column() == 0) { |
| 550 | auto *const editor = new RoleEditor(parent); |
| 551 | connect(editor, &RoleEditor::changed, this, &ColorDelegate::commitData); |
| 552 | return editor; |
| 553 | } |
| 554 | |
| 555 | using BrushEditorWidgetSignal = void (BrushEditor::*)(QWidget *); |
| 556 | |
| 557 | auto *const editor = new BrushEditor(parent); |
| 558 | connect(editor, static_cast<BrushEditorWidgetSignal>(&BrushEditor::changed), this, &ColorDelegate::commitData); |
| 559 | editor->setFocusPolicy(Qt::NoFocus); |
| 560 | editor->installEventFilter(const_cast<ColorDelegate *>(this)); |
| 561 | return editor; |
| 562 | } |
| 563 | |
| 564 | void ColorDelegate::setEditorData(QWidget *ed, const QModelIndex &index) const |
| 565 | { |
| 566 | if (index.column() == 0) { |
| 567 | const auto mask = qvariant_cast<bool>(index.model()->data(index, Qt::EditRole)); |
| 568 | auto *const editor = static_cast<RoleEditor *>(ed); |
| 569 | editor->setEdited(mask); |
| 570 | const auto colorName = qvariant_cast<QString>(index.model()->data(index, Qt::DisplayRole)); |
| 571 | editor->setLabel(colorName); |
| 572 | } else { |
| 573 | const auto br = qvariant_cast<QBrush>(index.model()->data(index, BrushRole)); |
| 574 | auto *const editor = static_cast<BrushEditor *>(ed); |
| 575 | editor->setBrush(br); |
| 576 | } |
| 577 | } |
| 578 | |
| 579 | void ColorDelegate::setModelData(QWidget *ed, QAbstractItemModel *model, const QModelIndex &index) const |
| 580 | { |
| 581 | if (index.column() == 0) { |
| 582 | const auto *const editor = static_cast<RoleEditor *>(ed); |
| 583 | const auto mask = editor->edited(); |
| 584 | model->setData(index, mask, Qt::EditRole); |
| 585 | } else { |
| 586 | const auto *const editor = static_cast<BrushEditor *>(ed); |
| 587 | if (editor->changed()) { |
| 588 | QBrush br = editor->brush(); |
| 589 | model->setData(index, br, BrushRole); |
| 590 | } |
| 591 | } |
| 592 | } |
| 593 | |
| 594 | void ColorDelegate::updateEditorGeometry(QWidget *ed, const QStyleOptionViewItem &option, const QModelIndex &index) const |
| 595 | { |
| 596 | QItemDelegate::updateEditorGeometry(ed, option, index); |
| 597 | ed->setGeometry(ed->geometry().adjusted(0, 0, -1, -1)); |
| 598 | } |
| 599 | |
| 600 | void ColorDelegate::paint(QPainter *painter, const QStyleOptionViewItem &opt, const QModelIndex &index) const |
| 601 | { |
| 602 | QStyleOptionViewItem option = opt; |
| 603 | const auto mask = qvariant_cast<bool>(index.model()->data(index, Qt::EditRole)); |
| 604 | if (index.column() == 0 && mask) { |
| 605 | option.font.setBold(true); |
| 606 | } |
| 607 | auto br = qvariant_cast<QBrush>(index.model()->data(index, BrushRole)); |
| 608 | if (br.style() == Qt::LinearGradientPattern || br.style() == Qt::RadialGradientPattern || br.style() == Qt::ConicalGradientPattern) { |
| 609 | painter->save(); |
| 610 | painter->translate(option.rect.x(), option.rect.y()); |
| 611 | painter->scale(option.rect.width(), option.rect.height()); |
| 612 | QGradient gr = *(br.gradient()); |
| 613 | gr.setCoordinateMode(QGradient::LogicalMode); |
| 614 | br = QBrush(gr); |
| 615 | painter->fillRect(0, 0, 1, 1, br); |
| 616 | painter->restore(); |
| 617 | } else { |
| 618 | painter->save(); |
| 619 | painter->setBrushOrigin(option.rect.x(), option.rect.y()); |
| 620 | painter->fillRect(option.rect, br); |
| 621 | painter->restore(); |
| 622 | } |
| 623 | QItemDelegate::paint(painter, option, index); |
| 624 | |
| 625 | const QColor color = static_cast<QRgb>(QApplication::style()->styleHint(QStyle::SH_Table_GridLineColor, &option)); |
| 626 | const QPen oldPen = painter->pen(); |
| 627 | painter->setPen(QPen(color)); |
| 628 | |
| 629 | painter->drawLine(option.rect.right(), option.rect.y(), option.rect.right(), option.rect.bottom()); |
| 630 | painter->drawLine(option.rect.x(), option.rect.bottom(), option.rect.right(), option.rect.bottom()); |
| 631 | painter->setPen(oldPen); |
| 632 | } |
| 633 | |
| 634 | QSize ColorDelegate::sizeHint(const QStyleOptionViewItem &opt, const QModelIndex &index) const |
| 635 | { |
| 636 | return QItemDelegate::sizeHint(opt, index) + QSize(4, 4); |
| 637 | } |
| 638 | |
| 639 | } // namespace QtUtilities |