Kstars

catalogdetails.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 <QMessageBox>
8 #include "catalogdetails.h"
9 #include "detaildialog.h"
10 #include "kstarsdata.h"
11 #include "kstars.h"
12 #include "ui_catalogdetails.h"
13 #include "catalogeditform.h"
14 #include "addcatalogobject.h"
15 #include "catalogcsvimport.h"
16 #include "skymapcomposite.h"
17 #include "catalogscomponent.h"
18 
19 constexpr int CatalogDetails::list_size;
20 
22  const int catalog_id)
23  : QDialog(parent), ui(new Ui::CatalogDetails), m_manager{ db_path },
24  m_catalog_id{ catalog_id }, m_timer{ new QTimer(this) }
25 {
26  ui->setupUi(this);
27  reload_catalog();
28  reload_objects();
29  ui->name_filter->setPlaceholderText(i18n("Showing <= %1 entries. Enter a name (case "
30  "sensitive) to narrow down the search.",
31  list_size));
32 
33  m_timer->setInterval(200);
34  connect(m_timer, &QTimer::timeout, this, &CatalogDetails::reload_objects);
35 
36  connect(ui->name_filter, &QLineEdit::textChanged, this,
37  [&](const auto) { m_timer->start(); });
38 
39  ui->object_table->setModel(&m_model);
40  ui->object_table->setSelectionBehavior(QAbstractItemView::SelectRows);
41  ui->object_table->adjustSize();
42  ui->object_table->horizontalHeader()->setSectionResizeMode(
43  QHeaderView::ResizeMode::ResizeToContents);
44 
45  connect(ui->object_table, &QTableView::doubleClicked, this,
46  &CatalogDetails::show_object_details);
47 
48  connect(ui->object_table, &QTableView::clicked, [&](const auto) {
49  if (!m_catalog.mut)
50  return;
51 
52  ui->remove_object->setEnabled(true);
53  ui->edit_object->setEnabled(true);
54  ui->object_group->setEnabled(true);
55  });
56 
57  connect(ui->edit, &QPushButton::clicked, this, &CatalogDetails::edit_catalog_meta);
58 
59  connect(ui->add_object, &QPushButton::clicked, this, &CatalogDetails::add_object);
60 
61  connect(ui->remove_object, &QPushButton::clicked, this,
62  &CatalogDetails::remove_objects);
63 
64  connect(ui->edit_object, &QPushButton::clicked, this, &CatalogDetails::edit_objects);
65 
66  connect(ui->import_csv, &QPushButton::clicked, this, &CatalogDetails::import_csv);
67 }
68 
69 CatalogDetails::~CatalogDetails()
70 {
71  delete ui;
72 }
73 
74 void CatalogDetails::reload_catalog()
75 {
76  const auto &found = m_manager.get_catalog(m_catalog_id);
77  if (!found.first)
78  {
79  QMessageBox::warning(this, i18n("Warning"),
80  i18n("Could not load the catalog with id=%1", m_catalog_id));
81  reject();
82  }
83 
84  m_catalog = found.second;
85 
86  ui->catalogInfo->setTitle(m_catalog.name);
87  ui->id->setText(QString::number(m_catalog.id));
88  ui->precedence->setText(QString::number(m_catalog.precedence));
89  ui->author->setText(m_catalog.author);
90  ui->maintainer->setText(m_catalog.maintainer);
91  ui->source->setText(m_catalog.source);
92  ui->description->setText(m_catalog.description);
93  ui->version->setText(QString::number(m_catalog.version));
94  ui->license->setText(m_catalog.license);
95 
96  ui->edit->setEnabled(m_catalog.mut);
97  ui->add_object->setEnabled(m_catalog.mut);
98  ui->import_csv->setEnabled(m_catalog.mut);
99 }
100 
101 void CatalogDetails::reload_objects()
102 {
103  const auto objects = m_manager.find_objects_by_name(
104  m_catalog.id, ui->name_filter->displayText(), list_size);
105 
106  m_model.setObjects({ objects.cbegin(), objects.cend() });
107  m_timer->stop();
108 }
109 
110 void CatalogDetails::show_object_details(const QModelIndex &index)
111 {
112  const auto &obj = m_model.getObject(index);
113  auto &inserted_obj =
114  KStarsData::Instance()->skyComposite()->catalogsComponent()->insertStaticObject(
115  obj);
116 
117  auto *dialog = new DetailDialog(&inserted_obj, KStarsData::Instance()->lt(),
118  KStarsData::Instance()->geo(), this);
119  connect(this, &QDialog::finished, dialog, &QDialog::done);
120  dialog->show();
121 }
122 
123 void CatalogDetails::edit_catalog_meta()
124 {
125  auto *dialog = new CatalogEditForm(this, m_catalog, 0, false);
126 
127  if (dialog->exec() != QDialog::Accepted)
128  return;
129 
130  const auto &success = m_manager.update_catalog_meta(dialog->getCatalog());
131  if (!success.first)
132  QMessageBox::warning(this, i18n("Warning"),
133  i18n("Could not update the catalog.<br>%1", success.second));
134 
135  reload_catalog();
136 }
137 
138 void CatalogDetails::add_object()
139 {
140  auto *dialog = new AddCatalogObject(this, {});
141 
142  if (dialog->exec() != QDialog::Accepted)
143  return;
144 
145  const auto &success = m_manager.add_object(m_catalog.id, dialog->get_object());
146  if (!success.first)
147  QMessageBox::warning(this, i18n("Warning"),
148  i18n("Could not add the object.<br>%1", success.second));
149 
150  reload_objects();
151 }
152 
153 void CatalogDetails::remove_objects()
154 {
155  const auto &items = ui->object_table->selectionModel()->selectedRows();
156 
157  if (items.length() == 0 || !m_catalog.mut)
158  return;
159 
160  for (const auto &index : items)
161  {
162  const auto &success =
163  m_manager.remove_object(m_catalog.id, m_model.getObject(index).getObjectId());
164  if (!success.first)
166  this, i18n("Warning"),
167  i18n("Could not remove the object.<br>%1", success.second));
168  }
169 
170  reload_objects();
171 }
172 
173 void CatalogDetails::edit_objects()
174 {
175  const auto &items = ui->object_table->selectionModel()->selectedRows();
176 
177  if (items.length() == 0 || !m_catalog.mut)
178  return;
179 
180  for (const auto &index : items)
181  {
182  const auto &obj = m_model.getObject(index);
183  auto *dialog = new AddCatalogObject(this, obj);
184 
185  if (dialog->exec() != QDialog::Accepted)
186  continue;
187 
188  const auto &success = m_manager.remove_object(m_catalog.id, obj.getObjectId());
189  if (!success.first)
190  QMessageBox::warning(this, i18n("Warning"),
191  i18n("Could not remove the object.<br>%1", success.second));
192 
193  const auto &success_add =
194  m_manager.add_object(m_catalog.id, dialog->get_object());
195  if (!success_add.first)
196  QMessageBox::warning(this, i18n("Warning"),
197  i18n("Could not add the object.<br>%1", success_add.second));
198  }
199 
200  reload_objects();
201 }
202 
203 void CatalogDetails::import_csv()
204 {
205  CatalogCSVImport dialog{};
206 
207  if (dialog.exec() != QDialog::Accepted)
208  return;
209 
210  const auto &success_add = m_manager.add_objects(m_catalog.id, dialog.get_objects());
211 
212  if (!success_add.first)
213  QMessageBox::warning(this, i18n("Warning"),
214  i18n("Could not add the objects.<br>%1", success_add.second));
215 
216  reload_objects();
217 };
QString maintainer
The catalog maintainer.
Definition: catalogsdb.h:100
void doubleClicked(const QModelIndex &index)
QString number(int n, int base)
std::pair< bool, QString > add_object(const int catalog_id, const SkyObject::TYPE t, const CachingDms &r, const CachingDms &d, const QString &n, const float m=NaN::f, const QString &lname=QString(), const QString &catalog_identifier=QString(), const float a=0.0, const float b=0.0, const double pa=0.0, const float flux=0)
Add a CatalogObject to a table with catalog_id.
Definition: catalogsdb.cpp:677
virtual void reject()
QString author
The author of the catalog.
Definition: catalogsdb.h:58
std::pair< bool, QString > add_objects(const int catalog_id, const CatalogObjectVector &objects)
Add the objects to a table with catalog_id.
int id
The catalog id.
Definition: catalogsdb.h:40
QString license
The catalog license.
Definition: catalogsdb.h:95
A simple data entry form for creating/editing catalog meta information.
void clicked(bool checked)
const std::pair< bool, Catalog > get_catalog(const int id)
Definition: catalogsdb.cpp:376
A dialog that shows catalog information and provides facilities to edit catalog meta information and ...
QMessageBox::StandardButton warning(QWidget *parent, const QString &title, const QString &text, QMessageBox::StandardButtons buttons, QMessageBox::StandardButton defaultButton)
CatalogDetails(QWidget *parent, const QString &db_path, const int catalog_id)
static constexpr int list_size
How many catalog entries to show in the list.
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
std::pair< bool, QString > remove_object(const int catalog_id, const CatalogObject::oid &id)
Remove the catalog object with the oid from the catalog with the catalog_id.
Definition: catalogsdb.cpp:713
QString i18n(const char *text, const TYPE &arg...)
CatalogObjectList find_objects_by_name(const QString &name, const int limit=-1, const bool exactMatchOnly=false)
Find an objects by name.
Definition: catalogsdb.cpp:488
QString source
The catalog source.
Definition: catalogsdb.h:63
CatalogObject & insertStaticObject(const CatalogObject &obj)
Insert an object obj into m_static_objects and return a reference to the newly inserted object.
std::pair< bool, QString > update_catalog_meta(const Catalog &cat)
Update the metatadata catalog.
Definition: catalogsdb.cpp:922
void textChanged(const QString &text)
void timeout()
double precedence
The precedence level of a catalog.
Definition: catalogsdb.h:53
GeoCoordinates geo(const QVariant &location)
virtual void done(int r)
bool mut
Wether the catalog is mutable.
Definition: catalogsdb.h:74
int version
The catalog version.
Definition: catalogsdb.h:84
SkyMapComposite * skyComposite()
Definition: kstarsdata.h:165
QString name
The catalog mame.
Definition: catalogsdb.h:45
void stop()
A simple data entry dialog to create and edit objects in CatalogDB catalogs.
QString description
A (short) description for the catalog.
Definition: catalogsdb.h:69
void clicked(const QModelIndex &index)
void finished(int result)
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Fri Aug 19 2022 03:57:49 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.