Kstars

catalogcoloreditor.cpp
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
14CatalogColorEditor::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
35CatalogColorEditor::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,
43};
44
45CatalogColorEditor::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,
53};
54
55void 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
86CatalogColorEditor::~CatalogColorEditor()
87{
88 delete ui;
89}
90
91QColor contrastingColor(QColor color)
92{
93 color.toHsl();
94 color.setHsv(0, 0, color.lightness() < 100 ? 255 : 0);
95
96 return color;
97}
98
99void 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
106void 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
126void 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 {
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};
A form for editing catalog color scheme mappings.
Manages the catalog database and provides an interface to provide an interface to query and modify th...
Definition catalogsdb.h:183
const std::pair< bool, Catalog > get_catalog(const int id)
std::pair< bool, QString > insert_catalog_colors(const int id, const CatalogColorMap &colors)
Saves the configures colors of the catalog with id id in colors into the database.
QString i18n(const char *text, const TYPE &arg...)
KIOCORE_EXPORT CopyJob * move(const QList< QUrl > &src, const QUrl &dest, JobFlags flags=DefaultFlags)
QString name(StandardAction id)
QCA_EXPORT void init()
void clicked(bool checked)
int lightness() const const
QString name(NameFormat format) const const
void setHsv(int h, int s, int v, int a)
QColor toHsl() const const
virtual void accept()
virtual void reject()
void addWidget(QWidget *w)
StandardButton critical(QWidget *parent, const QString &title, const QString &text, StandardButtons buttons, StandardButton defaultButton)
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QString arg(Args &&... args) const const
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
QLayout * layout() const const
void setStyleSheet(const QString &styleSheet)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:16:40 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.