Kstars

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

KDE's Doxygen guidelines are available online.