summaryrefslogtreecommitdiff
path: root/src/settingsdialoggeneral.cpp
diff options
context:
space:
mode:
authorisanae <14251494+isanae@users.noreply.github.com>2019-09-27 15:20:02 -0400
committerisanae <14251494+isanae@users.noreply.github.com>2019-09-27 15:20:02 -0400
commit51d664d76ce6b611e7a7585b209bad9d68fe65d7 (patch)
treebfb235aab1f5c7b5cd6a8622739a166ad5f1c4fa /src/settingsdialoggeneral.cpp
parent5d74bd3789515a3e04e54267f1cdfe8f5397f6df (diff)
moved color stuff to ColorTable, now shows sample text and icons
Diffstat (limited to 'src/settingsdialoggeneral.cpp')
-rw-r--r--src/settingsdialoggeneral.cpp235
1 files changed, 20 insertions, 215 deletions
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 <questionboxmemory.h>
-using MOBase::QuestionBoxMemory;
-
-
-class ColorItem : public QTableWidgetItem
-{
-public:
- ColorItem(
- const QColor& defaultColor,
- std::function<QColor ()> get,
- std::function<void (const QColor&)> 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<QColor ()> m_get;
- std::function<void (const QColor&)> 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<ColorItem*>(
- m_table->item(index.row(), index.column()));
-
- if (!item) {
- return false;
- }
-
- p->save();
- p->fillRect(option.rect, item->get());
- p->restore();
-
- return true;
- }
-};
-
-
-template <class F>
-void forEachColorItem(QTableWidget* table, F&& f)
-{
- const auto rowCount = table->rowCount();
-
- for (int i=0; i<rowCount; ++i) {
- if (auto* item=dynamic_cast<ColorItem*>(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->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->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<QColor ()> get,
- std::function<void (const QColor&)> 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<ColorItem*>(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()