Warning, file /education/kstars/kstars/dialogs/catalogdetails.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002     SPDX-FileCopyrightText: 2021 Valentin Boettcher <hiro at protagon.space; @hiro98:tchncs.de>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include <QMessageBox>
0008 #include "catalogdetails.h"
0009 #include "detaildialog.h"
0010 #include "kstarsdata.h"
0011 #include "kstars.h"
0012 #include "ui_catalogdetails.h"
0013 #include "catalogeditform.h"
0014 #include "addcatalogobject.h"
0015 #include "catalogcsvimport.h"
0016 #include "skymapcomposite.h"
0017 #include "catalogscomponent.h"
0018 
0019 constexpr int CatalogDetails::list_size;
0020 
0021 CatalogDetails::CatalogDetails(QWidget *parent, const QString &db_path,
0022                                const int catalog_id)
0023     : QDialog(parent), ui(new Ui::CatalogDetails), m_manager{ db_path },
0024       m_catalog_id{ catalog_id }, m_timer{ new QTimer(this) }
0025 {
0026     ui->setupUi(this);
0027     reload_catalog();
0028     reload_objects();
0029     ui->name_filter->setPlaceholderText(i18n("Showing <= %1 entries. Enter a name (case "
0030                                              "sensitive) to narrow down the search.",
0031                                              list_size));
0032 
0033     m_timer->setInterval(200);
0034     connect(m_timer, &QTimer::timeout, this, &CatalogDetails::reload_objects);
0035 
0036     connect(ui->name_filter, &QLineEdit::textChanged, this,
0037             [&](const auto) { m_timer->start(); });
0038 
0039     ui->object_table->setModel(&m_model);
0040     ui->object_table->setSelectionBehavior(QAbstractItemView::SelectRows);
0041     ui->object_table->adjustSize();
0042     ui->object_table->horizontalHeader()->setSectionResizeMode(
0043         QHeaderView::ResizeMode::ResizeToContents);
0044 
0045     connect(ui->object_table, &QTableView::doubleClicked, this,
0046             &CatalogDetails::show_object_details);
0047 
0048     connect(ui->object_table, &QTableView::clicked, [&](const auto) {
0049         if (!m_catalog.mut)
0050             return;
0051 
0052         ui->remove_object->setEnabled(true);
0053         ui->edit_object->setEnabled(true);
0054         ui->object_group->setEnabled(true);
0055     });
0056 
0057     connect(ui->edit, &QPushButton::clicked, this, &CatalogDetails::edit_catalog_meta);
0058 
0059     connect(ui->add_object, &QPushButton::clicked, this, &CatalogDetails::add_object);
0060 
0061     connect(ui->remove_object, &QPushButton::clicked, this,
0062             &CatalogDetails::remove_objects);
0063 
0064     connect(ui->edit_object, &QPushButton::clicked, this, &CatalogDetails::edit_objects);
0065 
0066     connect(ui->import_csv, &QPushButton::clicked, this, &CatalogDetails::import_csv);
0067 }
0068 
0069 CatalogDetails::~CatalogDetails()
0070 {
0071     delete ui;
0072 }
0073 
0074 void CatalogDetails::reload_catalog()
0075 {
0076     const auto &found = m_manager.get_catalog(m_catalog_id);
0077     if (!found.first)
0078     {
0079         QMessageBox::warning(this, i18n("Warning"),
0080                              i18n("Could not load the catalog with id=%1", m_catalog_id));
0081         reject();
0082     }
0083 
0084     m_catalog = found.second;
0085 
0086     ui->catalogInfo->setTitle(m_catalog.name);
0087     ui->id->setText(QString::number(m_catalog.id));
0088     ui->precedence->setText(QString::number(m_catalog.precedence));
0089     ui->author->setText(m_catalog.author);
0090     ui->maintainer->setText(m_catalog.maintainer);
0091     ui->source->setText(m_catalog.source);
0092     ui->description->setText(m_catalog.description);
0093     ui->version->setText(QString::number(m_catalog.version));
0094     ui->license->setText(m_catalog.license);
0095 
0096     ui->edit->setEnabled(m_catalog.mut);
0097     ui->add_object->setEnabled(m_catalog.mut);
0098     ui->import_csv->setEnabled(m_catalog.mut);
0099 }
0100 
0101 void CatalogDetails::reload_objects()
0102 {
0103     const auto objects = m_manager.find_objects_by_name(
0104         m_catalog.id, ui->name_filter->displayText(), list_size);
0105 
0106     m_model.setObjects({ objects.cbegin(), objects.cend() });
0107     m_timer->stop();
0108 }
0109 
0110 void CatalogDetails::show_object_details(const QModelIndex &index)
0111 {
0112     const auto &obj = m_model.getObject(index);
0113     auto &inserted_obj =
0114         KStarsData::Instance()->skyComposite()->catalogsComponent()->insertStaticObject(
0115             obj);
0116 
0117     auto *dialog = new DetailDialog(&inserted_obj, KStarsData::Instance()->lt(),
0118                                     KStarsData::Instance()->geo(), this);
0119     connect(this, &QDialog::finished, dialog, &QDialog::done);
0120     dialog->show();
0121 }
0122 
0123 void CatalogDetails::edit_catalog_meta()
0124 {
0125     auto *dialog = new CatalogEditForm(this, m_catalog, 0, false);
0126 
0127     if (dialog->exec() != QDialog::Accepted)
0128         return;
0129 
0130     const auto &success = m_manager.update_catalog_meta(dialog->getCatalog());
0131     if (!success.first)
0132         QMessageBox::warning(this, i18n("Warning"),
0133                              i18n("Could not update the catalog.<br>%1", success.second));
0134 
0135     reload_catalog();
0136 }
0137 
0138 void CatalogDetails::add_object()
0139 {
0140     auto *dialog = new AddCatalogObject(this, {});
0141 
0142     if (dialog->exec() != QDialog::Accepted)
0143         return;
0144 
0145     const auto &success = m_manager.add_object(m_catalog.id, dialog->get_object());
0146     if (!success.first)
0147         QMessageBox::warning(this, i18n("Warning"),
0148                              i18n("Could not add the object.<br>%1", success.second));
0149 
0150     reload_objects();
0151 }
0152 
0153 void CatalogDetails::remove_objects()
0154 {
0155     const auto &items = ui->object_table->selectionModel()->selectedRows();
0156 
0157     if (items.length() == 0 || !m_catalog.mut)
0158         return;
0159 
0160     for (const auto &index : items)
0161     {
0162         const auto &success =
0163             m_manager.remove_object(m_catalog.id, m_model.getObject(index).getObjectId());
0164         if (!success.first)
0165             QMessageBox::warning(
0166                 this, i18n("Warning"),
0167                 i18n("Could not remove the object.<br>%1", success.second));
0168     }
0169 
0170     reload_objects();
0171 }
0172 
0173 void CatalogDetails::edit_objects()
0174 {
0175     const auto &items = ui->object_table->selectionModel()->selectedRows();
0176 
0177     if (items.length() == 0 || !m_catalog.mut)
0178         return;
0179 
0180     for (const auto &index : items)
0181     {
0182         const auto &obj = m_model.getObject(index);
0183         auto *dialog    = new AddCatalogObject(this, obj);
0184 
0185         if (dialog->exec() != QDialog::Accepted)
0186             continue;
0187 
0188         const auto &success = m_manager.remove_object(m_catalog.id, obj.getObjectId());
0189         if (!success.first)
0190             QMessageBox::warning(this, i18n("Warning"),
0191                                  i18n("Could not remove the object.<br>%1", success.second));
0192 
0193         const auto &success_add =
0194             m_manager.add_object(m_catalog.id, dialog->get_object());
0195         if (!success_add.first)
0196             QMessageBox::warning(this, i18n("Warning"),
0197                                  i18n("Could not add the object.<br>%1", success_add.second));
0198     }
0199 
0200     reload_objects();
0201 }
0202 
0203 void CatalogDetails::import_csv()
0204 {
0205     CatalogCSVImport dialog{};
0206 
0207     if (dialog.exec() != QDialog::Accepted)
0208         return;
0209 
0210     const auto &success_add = m_manager.add_objects(m_catalog.id, dialog.get_objects());
0211 
0212     if (!success_add.first)
0213         QMessageBox::warning(this, i18n("Warning"),
0214                              i18n("Could not add the objects.<br>%1", success_add.second));
0215 
0216     reload_objects();
0217 };