Kstars

customdrivers.cpp
1/*
2 SPDX-FileCopyrightText: 2018 Jasem Mutlaq <mutlaqja@ikarustech.com>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#include "customdrivers.h"
8
9#include "Options.h"
10
11#include "kstars.h"
12#include "driverinfo.h"
13#include "kspaths.h"
14#include "kstarsdata.h"
15#include "ksnotification.h"
16
17
18CustomDrivers::CustomDrivers(QWidget *parent, const QList<QSharedPointer<DriverInfo>> &driversList) : QDialog(parent),
19 m_DriversList(driversList)
20{
21 setupUi(this);
22
23 auto userdb = QSqlDatabase::database(KStarsData::Instance()->userdb()->connectionName());
24 model = new QSqlTableModel(this, userdb);
25 model->setTable("customdrivers");
26
27 driversView->setModel(model);
28 driversView->hideColumn(0);
29
30 refreshFromDB();
31
32 tipLabel->setPixmap((QIcon::fromTheme("help-hint").pixmap(32, 32)));
33 cautionLabel->setPixmap((QIcon::fromTheme("emblem-warning").pixmap(32, 32)));
34
35 familyCombo->addItems(DeviceFamilyLabels.values());
36
37 for (const auto &oneDriver : driversList)
38 {
39 // MDPD drivers CANNOT have aliases.
40 // JM 2021.10.31 While usually MDPD drivers cannot have aliases
41 // We should add the ability to do so in case only a single device is connected.
42 // if (oneDriver->getAuxInfo().value("mdpd", false).toBool() == true)
43 // continue;
44
45 QString label = oneDriver->getLabel();
46 if (label.isEmpty())
47 continue;
48
49 driverCombo->addItem(label);
50 }
51
52 syncDriver();
53
54 connect(driverCombo, static_cast<void (QComboBox::*)(int)>(&QComboBox::activated),
55 this, &CustomDrivers::syncDriver);
56
57 connect(labelIN, &QLineEdit::textChanged, this, [&]()
58 {
59 addDriverB->setEnabled(labelIN->text().isEmpty() == false);
60 });
61
62 connect(driversView, &QTableView::pressed, this, [&]()
63 {
64 removeDriverB->setEnabled(true);
65 });
66
67 connect(addDriverB, &QPushButton::clicked, this, &CustomDrivers::addDriver);
68 connect(removeDriverB, &QPushButton::clicked, this, &CustomDrivers::removeDriver);
69}
70
71CustomDrivers::~CustomDrivers()
72{
73 userdb.close();
74}
75
76void CustomDrivers::refreshFromDB()
77{
78 KStarsData::Instance()->userdb()->GetAllCustomDrivers(m_CustomDrivers);
79 model->select();
80}
81
82void CustomDrivers::syncDriver()
83{
84 const QString currentDriverLabel = driverCombo->currentText();
85
86 for (auto oneDriver : m_DriversList)
87 {
88 if (currentDriverLabel == oneDriver->getLabel())
89 {
90 familyCombo->setCurrentIndex(oneDriver->getType());
91 execIN->setText(oneDriver->getExecutable());
92 nameIN->setText(oneDriver->getName());
93 manufacturerIN->setText(oneDriver->manufacturer());
94 break;
95 }
96 }
97}
98
99void CustomDrivers::addDriver()
100{
101 // Make sure label is unique in canonical drivers
102 for (auto &oneDriver : m_DriversList)
103 {
104 if (labelIN->text() == oneDriver->getLabel())
105 {
106 KSNotification::error(i18n("Label already exists. Label must be unique."));
107 return;
108 }
109 }
110
111 QVariantMap newDriver;
112
113 newDriver["Label"] = labelIN->text();
114 newDriver["Name"] = nameIN->text();
115 newDriver["Family"] = familyCombo->currentText();
116 newDriver["Manufacturer"] = manufacturerIN->text();
117 newDriver["Exec"] = execIN->text();
118 // TODO Try to make this editable as well
119 newDriver["Version"] = "1.0";
120
121 if (KStarsData::Instance()->userdb()->AddCustomDriver(newDriver) == false)
122 KSNotification::error(i18n("Failed to add new driver. Is the label unique?"));
123
124 refreshFromDB();
125
126}
127
128void CustomDrivers::removeDriver()
129{
130 int row = driversView->currentIndex().row();
131
132 if (row < 0)
133 return;
134
135 QVariantMap oneDriver = m_CustomDrivers[row];
136
137 KStarsData::Instance()->userdb()->DeleteCustomDriver(oneDriver["id"].toString());
138
139 refreshFromDB();
140}
KSUserDB * userdb()
Definition kstarsdata.h:215
QString i18n(const char *text, const TYPE &arg...)
char * toString(const EngineQuery &query)
QString label(StandardShortcut id)
void clicked(bool checked)
void pressed(const QModelIndex &index)
void activated(int index)
QIcon fromTheme(const QString &name)
void textChanged(const QString &text)
QSqlDatabase database(const QString &connectionName, bool open)
bool isEmpty() const const
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 4 2024 16:38:43 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.