File indexing completed on 2024-09-08 03:29:00
0001 /* 0002 SPDX-FileCopyrightText: 2021 Valentin Boettcher <hiro at protagon.space; @hiro98:tchncs.de> 0003 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #include "catalogcoloreditor.h" 0008 #include "catalogsdb.h" 0009 #include "ui_catalogcoloreditor.h" 0010 #include "kstarsdata.h" 0011 #include <QPushButton> 0012 #include <qcolordialog.h> 0013 0014 CatalogColorEditor::CatalogColorEditor(const int id, QWidget *parent) 0015 : QDialog(parent), ui(new Ui::CatalogColorEditor), m_id{ id } 0016 { 0017 CatalogsDB::DBManager manager{ CatalogsDB::dso_db_path() }; 0018 const auto &cat = manager.get_catalog(m_id); 0019 if (!cat.first) 0020 { 0021 QMessageBox::critical(this, i18n("Critical error"), 0022 i18n("Catalog with id %1 not found.", m_id)); 0023 reject(); 0024 return; 0025 } 0026 0027 m_colors = manager.get_catalog_colors(m_id); 0028 init(); 0029 ui->catalogName->setText(cat.second.name); 0030 0031 connect(ui->buttonBox, &QDialogButtonBox::accepted, this, 0032 &CatalogColorEditor::writeColors); 0033 } 0034 0035 CatalogColorEditor::CatalogColorEditor(const QString &colors, QWidget *parent) 0036 : QDialog(parent), ui(new Ui::CatalogColorEditor), 0037 m_colors{ CatalogsDB::parse_color_string(colors) }, m_id{ -1 } 0038 { 0039 init(); 0040 ui->catalogName->setText(""); 0041 connect(ui->buttonBox, &QDialogButtonBox::accepted, this, 0042 &CatalogColorEditor::accept); 0043 }; 0044 0045 CatalogColorEditor::CatalogColorEditor(color_map colors, QWidget *parent) 0046 : QDialog(parent), 0047 ui(new Ui::CatalogColorEditor), m_colors{ std::move(colors) }, m_id{ -1 } 0048 { 0049 init(); 0050 ui->catalogName->setText(""); 0051 connect(ui->buttonBox, &QDialogButtonBox::accepted, this, 0052 &CatalogColorEditor::accept); 0053 }; 0054 0055 void CatalogColorEditor::init() 0056 { 0057 ui->setupUi(this); 0058 auto *data = KStarsData::Instance(); 0059 auto default_color = m_colors["default"]; 0060 if (!default_color.isValid()) 0061 default_color = data->colorScheme()->colorNamed("DSOColor"); 0062 0063 for (const auto &item : KStarsData::Instance()->color_schemes()) 0064 { 0065 if (item.second != "default" && !data->hasColorScheme(item.second)) 0066 continue; 0067 0068 auto &color = m_colors[item.second]; 0069 if (!color.isValid()) 0070 { 0071 color = m_colors["default"]; 0072 0073 if (!color.isValid()) 0074 { 0075 color = default_color; 0076 } 0077 } 0078 } 0079 0080 for (const auto &item : m_colors) 0081 { 0082 make_color_button(item.first, item.second); 0083 } 0084 }; 0085 0086 CatalogColorEditor::~CatalogColorEditor() 0087 { 0088 delete ui; 0089 } 0090 0091 QColor contrastingColor(QColor color) 0092 { 0093 color.toHsl(); 0094 color.setHsv(0, 0, color.lightness() < 100 ? 255 : 0); 0095 0096 return color; 0097 } 0098 0099 void setButtonStyle(QPushButton *button, const QColor &color) 0100 { 0101 button->setStyleSheet(QString("background-color: %1; color: %2") 0102 .arg(color.name()) 0103 .arg(contrastingColor(color).name())); 0104 } 0105 0106 void CatalogColorEditor::make_color_button(const QString &name, const QColor &color) 0107 { 0108 auto *button = new QPushButton(name == "default" ? 0109 i18n("Default") : 0110 KStarsData::Instance()->colorSchemeName(name)); 0111 0112 auto *layout = ui->colorButtons; 0113 layout->addWidget(button); 0114 setButtonStyle(button, color); 0115 0116 connect(button, &QPushButton::clicked, this, [this, name, button] { 0117 QColorDialog picker{}; 0118 if (picker.exec() != QDialog::Accepted) 0119 return; 0120 0121 m_colors[name] = picker.currentColor(); 0122 setButtonStyle(button, picker.currentColor()); 0123 }); 0124 }; 0125 0126 void CatalogColorEditor::writeColors() 0127 { 0128 if (m_id < 0) 0129 return; 0130 0131 CatalogsDB::DBManager manager{ CatalogsDB::dso_db_path() }; 0132 const auto &insert_success = manager.insert_catalog_colors(m_id, m_colors); 0133 0134 if (!insert_success.first) 0135 { 0136 QMessageBox::critical( 0137 this, i18n("Critical error"), 0138 i18n("Could not insert new colors.<br>", insert_success.second)); 0139 reject(); 0140 return; 0141 } 0142 0143 accept(); 0144 };