From 51d664d76ce6b611e7a7585b209bad9d68fe65d7 Mon Sep 17 00:00:00 2001 From: isanae <14251494+isanae@users.noreply.github.com> Date: Fri, 27 Sep 2019 15:20:02 -0400 Subject: moved color stuff to ColorTable, now shows sample text and icons --- src/settingsdialoggeneral.cpp | 235 ++++-------------------------------------- 1 file changed, 20 insertions(+), 215 deletions(-) (limited to 'src/settingsdialoggeneral.cpp') diff --git a/src/settingsdialoggeneral.cpp b/src/settingsdialoggeneral.cpp index 0dfd3a08..a9ec5cae 100644 --- a/src/settingsdialoggeneral.cpp +++ b/src/settingsdialoggeneral.cpp @@ -2,114 +2,9 @@ #include "ui_settingsdialog.h" #include "appconfig.h" #include "categoriesdialog.h" +#include "colortable.h" #include -using MOBase::QuestionBoxMemory; - - -class ColorItem : public QTableWidgetItem -{ -public: - ColorItem( - const QColor& defaultColor, - std::function get, - std::function commit) - : m_default(defaultColor), m_get(get), m_commit(commit) - { - set(get()); - } - - QColor get() const - { - return m_temp; - } - - bool set(const QColor& c) - { - if (m_temp != c) { - m_temp = c; - return true; - } - - return false; - } - - void commit() - { - m_commit(m_temp); - } - - bool reset() - { - return set(m_default); - } - -private: - const QColor m_default; - std::function m_get; - std::function m_commit; - QColor m_temp; -}; - - -class ColorDelegate : public QStyledItemDelegate -{ -public: - ColorDelegate(QTableWidget* table) - : m_table(table) - { - } - -protected: - void paint( - QPainter* p, const QStyleOptionViewItem& option, - const QModelIndex& index) const override - { - if (!paintColor(p, option, index)) { - QStyledItemDelegate::paint(p, option, index); - } - } - -private: - QTableWidget* m_table; - - bool paintColor( - QPainter* p, const QStyleOptionViewItem& option, - const QModelIndex& index) const - { - if (index.column() != 1) { - return false; - } - - const auto* item = dynamic_cast( - m_table->item(index.row(), index.column())); - - if (!item) { - return false; - } - - p->save(); - p->fillRect(option.rect, item->get()); - p->restore(); - - return true; - } -}; - - -template -void forEachColorItem(QTableWidget* table, F&& f) -{ - const auto rowCount = table->rowCount(); - - for (int i=0; i(table->item(i, 1))) { - f(item); - } - } -} - - GeneralSettingsTab::GeneralSettingsTab(Settings& s, SettingsDialog& d) : SettingsTab(s, d) { @@ -119,17 +14,26 @@ GeneralSettingsTab::GeneralSettingsTab(Settings& s, SettingsDialog& d) addStyles(); selectStyle(); - setColorTable(); + ui->colorTable->load(s); - QObject::connect(ui->categoriesBtn, &QPushButton::clicked, [&]{ on_categoriesBtn_clicked(); }); - QObject::connect(ui->resetColorsBtn, &QPushButton::clicked, [&]{ on_resetColorsBtn_clicked(); }); - QObject::connect(ui->resetDialogsButton, &QPushButton::clicked, [&]{ on_resetDialogsButton_clicked(); }); + QObject::connect( + ui->categoriesBtn, &QPushButton::clicked, + [&]{ on_categoriesBtn_clicked(); }); + + QObject::connect( + ui->resetColorsBtn, &QPushButton::clicked, + [&]{ on_resetColorsBtn_clicked(); }); + + QObject::connect( + ui->resetDialogsButton, &QPushButton::clicked, + [&]{ on_resetDialogsButton_clicked(); }); } void GeneralSettingsTab::update() { const QString oldLanguage = settings().interface().language(); - const QString newLanguage = ui->languageBox->itemData(ui->languageBox->currentIndex()).toString(); + const QString newLanguage = ui->languageBox->itemData( + ui->languageBox->currentIndex()).toString(); if (newLanguage != oldLanguage) { settings().interface().setLanguage(newLanguage); @@ -137,7 +41,9 @@ void GeneralSettingsTab::update() } const QString oldStyle = settings().interface().styleName().value_or(""); - const QString newStyle = ui->styleBox->itemData(ui->styleBox->currentIndex()).toString(); + const QString newStyle = ui->styleBox->itemData( + ui->styleBox->currentIndex()).toString(); + if (oldStyle != newStyle) { settings().interface().setStyleName(newStyle); emit settings().styleChanged(newStyle); @@ -149,9 +55,7 @@ void GeneralSettingsTab::update() settings().setCheckForUpdates(ui->checkForUpdates->isChecked()); settings().setUsePrereleases(ui->usePrereleaseBox->isChecked()); - forEachColorItem(ui->colorTable, [](auto* item) { - item->commit(); - }); + ui->colorTable->commitColors(); settings().colors().setColorSeparatorScrollbar(ui->colorSeparatorsBox->isChecked()); } @@ -258,113 +162,14 @@ void GeneralSettingsTab::selectStyle() } } -void GeneralSettingsTab::setColorTable() -{ - ui->colorTable->setColumnCount(2); - ui->colorTable->setHorizontalHeaderLabels({ - QObject::tr("Item"), QObject::tr("Color") - }); - - ui->colorTable->setItemDelegate(new ColorDelegate(ui->colorTable)); - - addColor( - QObject::tr("Is overwritten (loose files)"), - QColor(0, 255, 0, 64), - [this]{ return settings().colors().modlistOverwrittenLoose(); }, - [this](auto&& v){ settings().colors().setModlistOverwrittenLoose(v); }); - - addColor( - QObject::tr("Is overwriting (loose files)"), - QColor(255, 0, 0, 64), - [this]{ return settings().colors().modlistOverwritingLoose(); }, - [this](auto&& v){ settings().colors().setModlistOverwritingLoose(v); }); - - addColor( - QObject::tr("Is overwritten (archives)"), - QColor(0, 255, 255, 64), - [this]{ return settings().colors().modlistOverwrittenArchive(); }, - [this](auto&& v){ settings().colors().setModlistOverwrittenArchive(v); }); - - addColor( - QObject::tr("Is overwriting (archives)"), - QColor(255, 0, 255, 64), - [this]{ return settings().colors().modlistOverwritingArchive(); }, - [this](auto&& v){ settings().colors().setModlistOverwritingArchive(v); }); - - addColor( - QObject::tr("Mod contains selected plugin"), - QColor(0, 0, 255, 64), - [this]{ return settings().colors().modlistContainsPlugin(); }, - [this](auto&& v){ settings().colors().setModlistContainsPlugin(v); }); - - addColor( - QObject::tr("Plugin is contained in selected mod"), - QColor(0, 0, 255, 64), - [this]{ return settings().colors().pluginListContained(); }, - [this](auto&& v){ settings().colors().setPluginListContained(v); }); - - QObject::connect( - ui->colorTable, &QTableWidget::cellActivated, - [&]{ onColorActivated(); }); -} - -void GeneralSettingsTab::addColor( - const QString& text, const QColor& defaultColor, - std::function get, - std::function commit) -{ - const auto r = ui->colorTable->rowCount(); - ui->colorTable->setRowCount(r + 1); - - ui->colorTable->setItem(r, 0, new QTableWidgetItem(text)); - ui->colorTable->setItem(r, 1, new ColorItem(defaultColor, get, commit)); - - ui->colorTable->resizeColumnsToContents(); -} - void GeneralSettingsTab::resetDialogs() { settings().widgets().resetQuestionButtons(); } -void GeneralSettingsTab::onColorActivated() -{ - const auto rows = ui->colorTable->selectionModel()->selectedRows(); - if (rows.isEmpty()) { - return; - } - - const auto row = rows[0].row(); - - const auto text = ui->colorTable->item(row, 0)->text(); - auto* item = dynamic_cast(ui->colorTable->item(row, 1)); - - if (!item) { - return; - } - - const QColor result = QColorDialog::getColor( - item->get(), &dialog(), text, QColorDialog::ShowAlphaChannel); - - if (result.isValid()) { - item->set(result); - ui->colorTable->update(ui->colorTable->model()->index(row, 1)); - } -} - void GeneralSettingsTab::on_resetColorsBtn_clicked() { - bool changed = false; - - forEachColorItem(ui->colorTable, [&](auto* item) { - if (item->reset()) { - changed = true; - } - }); - - if (changed) { - ui->colorTable->update(); - } + ui->colorTable->resetColors(); } void GeneralSettingsTab::on_resetDialogsButton_clicked() -- cgit v1.3.1