7#include "collimationoverlayoptions.h"
8#include <kstars_debug.h>
10#include "kstarsdata.h"
12#include "qmetaobject.h"
15#include <QSqlTableModel>
17#include <basedevice.h>
20CollimationOverlayOptions *CollimationOverlayOptions::m_Instance =
nullptr;
22CollimationOverlayOptions *CollimationOverlayOptions::Instance(
QWidget *parent)
24 if (m_Instance ==
nullptr) {
25 m_Instance =
new CollimationOverlayOptions(
parent);
30void CollimationOverlayOptions::release()
36CollimationOverlayOptions::CollimationOverlayOptions(
QWidget *parent) :
QDialog(parent)
47 updateValue(state,
"Enabled");
53 updateValue(typeComboBox,
"Type");
56 if (typeComboBox->currentIndex() == 0) {
57 sizeXSpinBox->setValue(0);
58 sizeYSpinBox->setValue(0);
59 rotationDoubleSpinBox->setValue(0.0);
60 countSpinBox->setValue(0);
61 pcdSpinBox->setValue(0);
62 thicknessSpinBox->setEnabled(
false);
63 sizeXSpinBox->setEnabled(
false);
64 sizeYSpinBox->setEnabled(
false);
65 rotationDoubleSpinBox->setEnabled(
false);
66 countSpinBox->setEnabled(
false);
67 pcdSpinBox->setEnabled(
false);
68 thicknessSpinBox->setEnabled(
false);
69 colourButton->setColor(
"Black");
70 colourButton->setEnabled(
false);
72 sizeXSpinBox->setEnabled(
true);
73 sizeYSpinBox->setEnabled(
true);
74 rotationDoubleSpinBox->setEnabled(
true);
75 countSpinBox->setEnabled(
true);
76 pcdSpinBox->setEnabled(
true);
77 thicknessSpinBox->setEnabled(
true);
78 colourButton->setEnabled(
true);
82 if (typeComboBox->currentIndex() == 1) {
89 if (typeComboBox->currentIndex() == 3){
90 sizeYSpinBox->setMinimum(0);
92 sizeYSpinBox->setMinimum(1);
98 collimationoverlaytype m_types;
99 const QMetaObject *m_metaobject = m_types.metaObject();
101 for (
int i = 0; i < m_metaEnum.
keyCount(); i++) {
102 typeValues << tr(m_metaEnum.
key(i));
104 typeComboBox->clear();
105 typeComboBox->addItems(typeValues);
106 typeComboBox->setCurrentIndex(0);
111 updateValue(value,
"SizeX");
112 if (linkXYB->icon().name() ==
"document-encrypt") {
113 sizeYSpinBox->setValue(sizeXSpinBox->value());
114 updateValue(value,
"SizeY");
121 updateValue(value,
"SizeY");
127 if (linkXYB->icon().name() ==
"document-decrypt") {
128 sizeYSpinBox->setValue(sizeXSpinBox->value());
130 sizeYSpinBox->setEnabled(
false);
131 }
else if (linkXYB->icon().name() ==
"document-encrypt") {
133 sizeYSpinBox->setEnabled(
true);
140 updateValue(value,
"OffsetX");
146 updateValue(value,
"OffsetY");
152 updateValue(value,
"Count");
155 pcdSpinBox->setEnabled(
false);
156 rotationDoubleSpinBox->setEnabled(
false);
158 pcdSpinBox->setEnabled(
true);
159 rotationDoubleSpinBox->setEnabled(
true);
166 updateValue(value,
"PCD");
171 [
this](
double value) {
172 updateValue(value,
"Rotation");
176 colourButton->setAlphaChannelEnabled(
true);
179 updateValue(value,
"Colour");
185 updateValue(value,
"Thickness");
189 if (addB->icon().name() ==
"dialog-ok-apply") {
190 elementNamesList->clearSelection();
192 selectCollimationOverlayElement(
"");
194 addElement(nameLineEdit->text());
195 m_CollimationOverlayElementsModel->select();
197 elementNamesList->clearSelection();
202 if (elementNamesList->currentItem() !=
nullptr) {
203 removeCollimationOverlayElement(elementNamesList->currentItem()->text());
205 elementNamesList->clearSelection();
213 removeB->setEnabled(
true);
217 selectCollimationOverlayElement(item);
219 if (typeComboBox->currentIndex() == 1) {
227 renameCollimationOverlayElement(nameLineEdit->text());
232 selectCollimationOverlayElement(
"");
238void CollimationOverlayOptions::initModel()
240 m_CollimationOverlayElements.
clear();
242 m_CollimationOverlayElementsModel =
new QSqlTableModel(
this, userdb);
244 m_CollimationOverlayElements.
clear();
245 for (
int i = 0; i < m_CollimationOverlayElementsModel->
rowCount(); ++i) {
246 QVariantMap recordMap;
248 for (
int j = 0; j < record.
count(); j++)
251 m_CollimationOverlayElements.
append(recordMap);
253 m_ElementNames.
clear();
254 for (
auto &oneElement : m_CollimationOverlayElements) {
255 m_ElementNames << oneElement[
"Name"].toString();
257 elementNamesList->clear();
258 elementNamesList->addItems(m_ElementNames);
265void CollimationOverlayOptions::refreshModel()
267 m_CollimationOverlayElements.clear();
269 m_ElementNames.
clear();
270 for (
auto &oneElement : m_CollimationOverlayElements) {
271 m_ElementNames << oneElement[
"Name"].toString();
273 elementNamesList->clear();
274 elementNamesList->addItems(m_ElementNames);
280 element[
"Name"] = uniqueElementName(name, typeComboBox->currentText());
281 element[
"Enabled"] = EnableCheckBox->checkState();
282 element[
"Type"] = typeComboBox->currentText();
283 element[
"SizeX"] = sizeXSpinBox->value();
284 element[
"SizeY"] = sizeYSpinBox->value();
285 element[
"OffsetX"] = offsetXSpinBox->value();
286 element[
"OffsetY"] = offsetYSpinBox->value();
287 element[
"Count"] = countSpinBox->value();
288 element[
"PCD"] = pcdSpinBox->value();
289 element[
"Rotation"] = rotationDoubleSpinBox->value();
290 element[
"Colour"] = colourButton->color();
291 element[
"Thickness"] = thicknessSpinBox->value();
295 return element[
"Name"].toString();
298bool CollimationOverlayOptions::setCollimationOverlayElementValue(
const QString &name,
const QString &field,
const QVariant &value)
300 for (
auto &oneElement : m_CollimationOverlayElements) {
301 if (oneElement[
"Name"].
toString() == name) {
303 if (oneElement[field] == value) {
307 oneElement[field] = value;
316void CollimationOverlayOptions::renameCollimationOverlayElement(
const QString &name)
318 if (m_CurrentElement !=
nullptr && (*m_CurrentElement)[
"Name"] != name) {
319 auto pos = elementNamesList->currentRow();
321 auto unique = uniqueElementName(name, (*m_CurrentElement)[
"Type"].
toString());
323 setCollimationOverlayElementValue((*m_CurrentElement)[
"Name"].
toString(),
"Name", unique);
325 elementNamesList->currentItem()->setText(unique);
329 elementNamesList->setCurrentRow(
pos);
330 selectCollimationOverlayElement(unique);
334bool CollimationOverlayOptions::setCollimationOverlayElement(
const QJsonObject &element)
336 auto oneElement = getCollimationOverlayElement(element[
"id"].toInt());
337 if (!oneElement.empty()) {
345bool CollimationOverlayOptions::removeCollimationOverlayElement(
const QString &name)
347 for (
auto &oneElement : m_CollimationOverlayElements) {
348 if (oneElement[
"Name"].
toString() == name) {
349 auto id = oneElement[
"id"].toInt();
350 KStarsData::Instance()->
userdb()->DeleteCollimationOverlayElement(
id);
369bool CollimationOverlayOptions::selectCollimationOverlayElement(
QListWidgetItem *item)
371 if (item !=
nullptr && selectCollimationOverlayElement(item->
text())) {
377bool CollimationOverlayOptions::selectCollimationOverlayElement(
const QString &name)
379 for (
auto &oneElement : m_CollimationOverlayElements) {
380 if (oneElement[
"Name"].
toString() == name) {
382 m_CurrentElement = &oneElement;
383 nameLineEdit->setText(oneElement[
"Name"].
toString());
384 renameB->setEnabled(
true);
385 EnableCheckBox->setChecked(oneElement[
"Enabled"].toUInt());
386 typeComboBox->setCurrentText(oneElement[
"Type"].
toString());
387 sizeXSpinBox->setValue(oneElement[
"SizeX"].toUInt());
388 sizeYSpinBox->setValue(oneElement[
"SizeY"].toUInt());
389 offsetXSpinBox->setValue(oneElement[
"OffsetX"].toUInt());
390 offsetYSpinBox->setValue(oneElement[
"OffsetY"].toUInt());
391 countSpinBox->setValue(oneElement[
"Count"].toUInt());
392 pcdSpinBox->setValue(oneElement[
"PCD"].toUInt());
393 rotationDoubleSpinBox->setValue(oneElement[
"Rotation"].toDouble());
396 colourButton->setColor(tempColour);
397 thicknessSpinBox->setValue(oneElement[
"Thickness"].toUInt());
398 removeB->setEnabled(m_CollimationOverlayElements.length() > 0);
399 elementConfigBox->setEnabled(
true);
406 nameLineEdit->setText(
"");
407 renameB->setEnabled(
false);
409 typeComboBox->setCurrentText(
"--");
410 sizeXSpinBox->setValue(0);
411 sizeYSpinBox->setValue(0);
412 offsetXSpinBox->setValue(0);
413 offsetYSpinBox->setValue(0);
414 countSpinBox->setValue(0);
415 pcdSpinBox->setValue(0);
416 rotationDoubleSpinBox->setValue(0.0);
419 colourButton->setColor(tempColour);
420 thicknessSpinBox->setValue(1);
422 removeB->setEnabled(
false);
426void CollimationOverlayOptions::openEditor()
432const QVariantMap CollimationOverlayOptions::getCollimationOverlayElement(uint8_t
id)
const
434 for (
auto &oneElement : m_CollimationOverlayElements) {
435 if (oneElement[
"id"].toInt() ==
id)
438 return QVariantMap();
441bool CollimationOverlayOptions::exists(uint8_t
id)
const
443 for (
auto &oneElement : m_CollimationOverlayElements) {
444 if (oneElement[
"id"].toInt() ==
id)
450const QVariantMap CollimationOverlayOptions::getCollimationOverlayElement(
const QString &name)
const
452 for (
auto &oneElement : m_CollimationOverlayElements) {
453 if (oneElement[
"Name"].
toString() == name) {
457 return QVariantMap();
460void CollimationOverlayOptions::refreshElements()
466int CollimationOverlayOptions::id(
const QString &name)
const
468 for (
auto &oneElement : m_CollimationOverlayElements) {
469 if (oneElement[
"Name"].
toString() == name)
470 return oneElement[
"id"].toUInt();
475QString CollimationOverlayOptions::name(
int id)
const
477 for (
auto &oneElement : m_CollimationOverlayElements) {
478 if (oneElement[
"id"].toInt() ==
id)
479 return oneElement[
"name"].toString();
484void CollimationOverlayOptions::updateValue(
QComboBox *cb,
const QString &element)
486 if (elementNamesList->currentItem() !=
nullptr && editing ==
true) {
487 setCollimationOverlayElementValue(elementNamesList->currentItem()->text(), element, cb->
currentText());
491void CollimationOverlayOptions::updateValue(
double value,
const QString &element)
493 if (elementNamesList->currentItem() !=
nullptr && editing ==
true) {
494 setCollimationOverlayElementValue(elementNamesList->currentItem()->text(), element, value);
498void CollimationOverlayOptions::updateValue(
int value,
const QString &element)
500 if (elementNamesList->currentItem() !=
nullptr && editing ==
true) {
501 setCollimationOverlayElementValue(elementNamesList->currentItem()->text(), element, value);
505void CollimationOverlayOptions::updateValue(
QColor value,
const QString &element)
507 if (elementNamesList->currentItem() !=
nullptr && editing ==
true) {
508 setCollimationOverlayElementValue(elementNamesList->currentItem()->text(), element, value);
512void CollimationOverlayOptions::updateValue(
QString value,
const QString &element)
514 if (elementNamesList->currentItem() !=
nullptr && editing ==
true) {
515 setCollimationOverlayElementValue(elementNamesList->currentItem()->text(), element, value);
bool UpdateCollimationOverlayElement(const QVariantMap &oneElement, int id)
Update an existing collimation overlay element.
bool AddCollimationOverlayElement(const QVariantMap &oneElement)
Add a new collimation overlay element to the database.
bool GetCollimationOverlayElements(QList< QVariantMap > &collimationOverlayElements)
Populate the reference passed with all collimation overlay elements.
static KStars * Instance()
KStarsData * data() const
char * toString(const EngineQuery &query)
QString name(GameStandardAction id)
VehicleSection::Type type(QStringView coachNumber, QStringView coachClassification)
void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QList< int > &roles)
void stateChanged(int state)
void setNamedColor(QLatin1StringView name)
void currentIndexChanged(int index)
void valueChanged(double d)
QIcon fromTheme(const QString &name)
QVariantMap toVariantMap() const const
void append(QList< T > &&value)
bool contains(const AT &value) const const
QString text() const const
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QObject * parent() const const
QSqlDatabase database(const QString &connectionName, bool open)
QString fieldName(int index) const const
QVariant value(const QString &name) const const
QSqlRecord record() const const
virtual int rowCount(const QModelIndex &parent) const const override
QString arg(Args &&... args) const const
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)