1 /*
2     SPDX-FileCopyrightText: 2021 Valentin Boettcher <hiro at protagon.space; @hiro98:tchncs.de>
3 
4     SPDX-License-Identifier: GPL-2.0-or-later
5 */
6 
7 #include "catalogcoloreditor.h"
8 #include "catalogsdb.h"
9 #include "ui_catalogcoloreditor.h"
10 #include "kstarsdata.h"
11 #include <QPushButton>
12 #include <qcolordialog.h>
13 
CatalogColorEditor(const int id,QWidget * parent)14 CatalogColorEditor::CatalogColorEditor(const int id, QWidget *parent)
15     : QDialog(parent), ui(new Ui::CatalogColorEditor), m_id{ id }
16 {
17     CatalogsDB::DBManager manager{ CatalogsDB::dso_db_path() };
18     const auto &cat = manager.get_catalog(m_id);
19     if (!cat.first)
20     {
21         QMessageBox::critical(this, i18n("Critical error"),
22                               i18n("Catalog with id %1 not found.", m_id));
23         reject();
24         return;
25     }
26 
27     m_colors = manager.get_catalog_colors(m_id);
28     init();
29     ui->catalogName->setText(cat.second.name);
30 
31     connect(ui->buttonBox, &QDialogButtonBox::accepted, this,
32             &CatalogColorEditor::writeColors);
33 }
34 
CatalogColorEditor(const QString & colors,QWidget * parent)35 CatalogColorEditor::CatalogColorEditor(const QString &colors, QWidget *parent)
36     : QDialog(parent), ui(new Ui::CatalogColorEditor),
37       m_colors{ CatalogsDB::parse_color_string(colors) }, m_id{ -1 }
38 {
39     init();
40     ui->catalogName->setText("");
41     connect(ui->buttonBox, &QDialogButtonBox::accepted, this,
42             &CatalogColorEditor::accept);
43 };
44 
CatalogColorEditor(color_map colors,QWidget * parent)45 CatalogColorEditor::CatalogColorEditor(color_map colors, QWidget *parent)
46     : QDialog(parent),
47       ui(new Ui::CatalogColorEditor), m_colors{ std::move(colors) }, m_id{ -1 }
48 {
49     init();
50     ui->catalogName->setText("");
51     connect(ui->buttonBox, &QDialogButtonBox::accepted, this,
52             &CatalogColorEditor::accept);
53 };
54 
init()55 void CatalogColorEditor::init()
56 {
57     ui->setupUi(this);
58     auto *data         = KStarsData::Instance();
59     auto default_color = m_colors["default"];
60     if (!default_color.isValid())
61         default_color = data->colorScheme()->colorNamed("DSOColor");
62 
63     for (const auto &item : KStarsData::Instance()->color_schemes())
64     {
65         if (item.second != "default" && !data->hasColorScheme(item.second))
66             continue;
67 
68         auto &color = m_colors[item.second];
69         if (!color.isValid())
70         {
71             color = m_colors["default"];
72 
73             if (!color.isValid())
74             {
75                 color = default_color;
76             }
77         }
78     }
79 
80     for (const auto &item : m_colors)
81     {
82         make_color_button(item.first, item.second);
83     }
84 };
85 
~CatalogColorEditor()86 CatalogColorEditor::~CatalogColorEditor()
87 {
88     delete ui;
89 }
90 
contrastingColor(QColor color)91 QColor contrastingColor(QColor color)
92 {
93     color.toHsl();
94     color.setHsv(0, 0, color.lightness() < 100 ? 255 : 0);
95 
96     return color;
97 }
98 
setButtonStyle(QPushButton * button,const QColor & color)99 void setButtonStyle(QPushButton *button, const QColor &color)
100 {
101     button->setStyleSheet(QString("background-color: %1; color: %2")
102                               .arg(color.name())
103                               .arg(contrastingColor(color).name()));
104 }
105 
make_color_button(const QString & name,const QColor & color)106 void CatalogColorEditor::make_color_button(const QString &name, const QColor &color)
107 {
108     auto *button = new QPushButton(name == "default" ?
109                                        i18n("Default") :
110                                        KStarsData::Instance()->colorSchemeName(name));
111 
112     auto *layout = ui->colorButtons;
113     layout->addWidget(button);
114     setButtonStyle(button, color);
115 
116     connect(button, &QPushButton::clicked, this, [this, name, button] {
117         QColorDialog picker{};
118         if (picker.exec() != QDialog::Accepted)
119             return;
120 
121         m_colors[name] = picker.currentColor();
122         setButtonStyle(button, picker.currentColor());
123     });
124 };
125 
writeColors()126 void CatalogColorEditor::writeColors()
127 {
128     if (m_id < 0)
129         return;
130 
131     CatalogsDB::DBManager manager{ CatalogsDB::dso_db_path() };
132     const auto &insert_success = manager.insert_catalog_colors(m_id, m_colors);
133 
134     if (!insert_success.first)
135     {
136         QMessageBox::critical(
137             this, i18n("Critical error"),
138             i18n("Could not insert new colors.<br>", insert_success.second));
139         reject();
140         return;
141     }
142 
143     accept();
144 };
145