diff --git a/po/stellarium/fr.po b/po/stellarium/fr.po index 72d7b43a03558..5ca1b6afd3ace 100644 --- a/po/stellarium/fr.po +++ b/po/stellarium/fr.po @@ -14844,7 +14844,7 @@ msgstr "Exporter la liste" #: src/ui_obsListCreateEditDialog.h:264 msgid "Import list" -msgstr "Exporter la liste" +msgstr "Importer la liste" #: src/ui_obsListCreateEditDialog.h:265 msgid "Save and Close " diff --git a/src/gui/ObsListCreateEditDialog.cpp b/src/gui/ObsListCreateEditDialog.cpp index 411b164e8d325..36cc55f5f2513 100644 --- a/src/gui/ObsListCreateEditDialog.cpp +++ b/src/gui/ObsListCreateEditDialog.cpp @@ -17,720 +17,1090 @@ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ -#include -#include -#include #include -#include +#include #include +#include +#include +#include +#include +#include +#include +#include +#include "NebulaMgr.hpp" #include "StelCore.hpp" #include "StelFileMgr.hpp" -#include "StelObjectMgr.hpp" +#include "StelJsonParser.hpp" #include "StelModuleMgr.hpp" -#include "NebulaMgr.hpp" #include "StelMovementMgr.hpp" +#include "StelObjectMgr.hpp" +#include "LandscapeMgr.hpp" #include "StelUtils.hpp" -#include "StelJsonParser.hpp" #include "ObsListCreateEditDialog.hpp" #include "ui_obsListCreateEditDialog.h" -ObsListCreateEditDialog * ObsListCreateEditDialog::m_instance = nullptr; - -ObsListCreateEditDialog::ObsListCreateEditDialog ( std::string listUuid ) -{ - listUuid_ = listUuid; +ObsListCreateEditDialog *ObsListCreateEditDialog::m_instance = nullptr; + +ObsListCreateEditDialog::ObsListCreateEditDialog(std::string listUuid) : ui(new Ui_obsListCreateEditDialogForm()), + obsListListModel( + new QStandardItemModel(0, + ColumnCount)), + core(StelApp::getInstance().getCore()), + listOlud_(std::move(listUuid)), sorting("") { + objectMgr = GETSTELMODULE(StelObjectMgr); + landscapeMgr = GETSTELMODULE(LandscapeMgr); + observingListJsonPath = + StelFileMgr::findFile("data", + static_cast(StelFileMgr::Directory | StelFileMgr::Writable)) + + "/" + + QString(JSON_FILE_NAME); - ui = new Ui_obsListCreateEditDialogForm(); - core = StelApp::getInstance().getCore(); - objectMgr = GETSTELMODULE ( StelObjectMgr ); - obsListListModel = new QStandardItemModel ( 0,ColumnCount ); - observingListJsonPath = StelFileMgr::findFile ( "data", static_cast( StelFileMgr::Directory|StelFileMgr::Writable ) ) + "/" + QString ( JSON_FILE_NAME ); - sorting = ""; } -ObsListCreateEditDialog::~ObsListCreateEditDialog() -{ - delete ui; - delete obsListListModel; +ObsListCreateEditDialog::~ObsListCreateEditDialog() { + delete ui; + delete obsListListModel; + ui = Q_NULLPTR; + obsListListModel = Q_NULLPTR; } /** * Get instance of class -*/ -ObsListCreateEditDialog * ObsListCreateEditDialog::Instance ( std::string listUuid ) -{ - if ( m_instance == nullptr ) { - m_instance = new ObsListCreateEditDialog ( listUuid ); - } - - return m_instance; + */ +auto ObsListCreateEditDialog::Instance(std::string listUuid) -> ObsListCreateEditDialog * { + if (m_instance == nullptr) { + m_instance = new ObsListCreateEditDialog(std::move(listUuid)); + } + return m_instance; } - /* * Initialize the dialog widgets and connect the signals/slots. -*/ -void ObsListCreateEditDialog::createDialogContent() -{ - ui->setupUi ( dialog ); - - //Signals and slots - connect ( &StelApp::getInstance(), SIGNAL ( languageChanged() ), this, SLOT ( retranslate() ) ); - connect ( ui->closeStelWindow, SIGNAL ( clicked() ), this, SLOT ( close() ) ); - - connect ( ui->obsListAddObjectButton, SIGNAL ( clicked() ), this, SLOT ( obsListAddObjectButtonPressed() ) ); - connect ( ui->obsListExitButton, SIGNAL ( clicked() ), this, SLOT ( obsListExitButtonPressed() ) ); - connect ( ui->obsListSaveButton, SIGNAL ( clicked() ), this, SLOT ( obsListSaveButtonPressed() ) ); - connect ( ui->obsListRemoveObjectButton, SIGNAL ( clicked() ), this, SLOT ( obsListRemoveObjectButtonPressed() ) ); - connect ( ui->obsListImportListButton, SIGNAL ( clicked() ), this, SLOT ( obsListImportListButtonPresssed() ) ); - connect ( ui->obsListExportListButton, SIGNAL ( clicked() ), this, SLOT ( obsListExportListButtonPressed() ) ); - - //Initializing the list of observing list - obsListListModel->setColumnCount ( ColumnCount ); - setObservingListHeaderNames(); - - ui->obsListCreationEditionTreeView->setModel ( obsListListModel ); - ui->obsListCreationEditionTreeView->header()->setSectionsMovable ( false ); - ui->obsListCreationEditionTreeView->header()->setSectionResizeMode ( ColumnName, QHeaderView::ResizeToContents ); - ui->obsListCreationEditionTreeView->header()->setSectionResizeMode ( ColumnType, QHeaderView::ResizeToContents ); - ui->obsListCreationEditionTreeView->header()->setSectionResizeMode ( ColumnRa, QHeaderView::ResizeToContents ); - ui->obsListCreationEditionTreeView->header()->setSectionResizeMode ( ColumnDec, QHeaderView::ResizeToContents ); - ui->obsListCreationEditionTreeView->header()->setSectionResizeMode ( ColumnConstellation, QHeaderView::ResizeToContents ); - ui->obsListCreationEditionTreeView->header()->setSectionResizeMode ( ColumnMagnitude, QHeaderView::ResizeToContents ); - ui->obsListCreationEditionTreeView->header()->setStretchLastSection ( true ); - ui->obsListCreationEditionTreeView->hideColumn ( ColumnUUID ); - ui->obsListCreationEditionTreeView->hideColumn ( ColumnNameI18n ); - ui->obsListCreationEditionTreeView->hideColumn ( ColumnJD ); - ui->obsListCreationEditionTreeView->hideColumn ( ColumnLocation ); - //Enable the sort for columns - ui->obsListCreationEditionTreeView->setSortingEnabled ( true ); - - QHeaderView * header = ui->obsListCreationEditionTreeView->header(); - connect ( header, SIGNAL ( sectionClicked ( int ) ), this, SLOT ( headerClicked ( int ) ) ); - - if ( listUuid_.size() == 0 ) - { - // case of creation mode - isCreationMode = true; - ui->stelWindowTitle->setText ( "Observing list creation mode" ); - } - else - { - // case of edit mode - isCreationMode = false; - ui->stelWindowTitle->setText ( "Observing list modification mode" ); - loadObservingList(); - } + */ +void ObsListCreateEditDialog::createDialogContent() { + ui->setupUi(dialog); + + // Signals and slots + connect(&StelApp::getInstance(), SIGNAL(languageChanged()), this, SLOT(retranslate())); + connect(ui->closeStelWindow, SIGNAL(clicked()), this, SLOT(close())); + + connect(ui->obsListAddObjectButton, &QPushButton::clicked, this, + &ObsListCreateEditDialog::obsListAddObjectButtonPressed); + connect(ui->obsListExitButton, &QPushButton::clicked, this, &ObsListCreateEditDialog::obsListExitButtonPressed); + connect(ui->obsListSaveButton, &QPushButton::clicked, this, &ObsListCreateEditDialog::obsListSaveButtonPressed); + connect(ui->obsListRemoveObjectButton, &QPushButton::clicked, this, + &ObsListCreateEditDialog::obsListRemoveObjectButtonPressed); + connect(ui->obsListImportListButton, &QPushButton::clicked, this, + &ObsListCreateEditDialog::obsListImportListButtonPresssed); + connect(ui->obsListExportListButton, &QPushButton::clicked, this, + &ObsListCreateEditDialog::obsListExportListButtonPressed); + connect(ui->nameOfListLineEdit, &QLineEdit::textChanged, this, &ObsListCreateEditDialog::nameOfListTextChange); + + // Initializing the list of observing list + obsListListModel->setColumnCount(ColumnCount); + setObservingListHeaderNames(); + + ui->obsListCreationEditionTreeView->setModel(obsListListModel); + ui->obsListCreationEditionTreeView->header()->setSectionsMovable(false); + ui->obsListCreationEditionTreeView->header()->setSectionResizeMode(ColumnName, QHeaderView::ResizeToContents); + ui->obsListCreationEditionTreeView->header()->setSectionResizeMode(ColumnNameI18n, QHeaderView::ResizeToContents); + ui->obsListCreationEditionTreeView->header()->setSectionResizeMode(ColumnType, QHeaderView::ResizeToContents); + ui->obsListCreationEditionTreeView->header()->setSectionResizeMode(ColumnRa, QHeaderView::ResizeToContents); + ui->obsListCreationEditionTreeView->header()->setSectionResizeMode(ColumnDec, QHeaderView::ResizeToContents); + ui->obsListCreationEditionTreeView->header()->setSectionResizeMode(ColumnConstellation, + QHeaderView::ResizeToContents); + ui->obsListCreationEditionTreeView->header()->setSectionResizeMode(ColumnMagnitude, QHeaderView::ResizeToContents); + ui->obsListCreationEditionTreeView->header()->setStretchLastSection(true); + ui->obsListCreationEditionTreeView->hideColumn(ColumnUUID); + ui->obsListCreationEditionTreeView->hideColumn(ColumnDate); + ui->obsListCreationEditionTreeView->hideColumn(ColumnLocation); + // Enable the sort for columns + ui->obsListCreationEditionTreeView->setSortingEnabled(true); + + QHeaderView *header = ui->obsListCreationEditionTreeView->header(); + connect(header, SIGNAL(sectionClicked(int)), this, SLOT(headerClicked(int))); + + // We hide the closeStelWindow to have only two possibilities to close the dialog: + // Save and close and Exit + ui->closeStelWindow->setHidden(true); + + initErrorMessage(); + ui->obsListLandscapeCheckBox->setChecked(false); + + // In case of creation the nameOfListLineEdit is empty and button save/close must be disabled + // In case on edition the nameOfListLineEdit is not empty and the button save/close must be enable + // delete with space -> no list with only white space as name + ui->nameOfListLineEdit->setText(QString(ui->nameOfListLineEdit->text()).remove(QRegularExpression("([ ]+)$"))); + if (ui->nameOfListLineEdit->text().isEmpty()) { + ui->obsListSaveButton->setEnabled(false); + } else { + ui->obsListSaveButton->setEnabled(true); + } + + if (listOlud_.empty()) { + // case of creation mode + isCreationMode = true; + ui->stelWindowTitle->setText("Observing list creation mode"); + ui->obsListImportListButton->setHidden(false); + } else { + // case of edit mode + isCreationMode = false; + ui->stelWindowTitle->setText("Observing list editor mode"); + ui->obsListImportListButton->setHidden(true); + loadObservingList(); + } } /* * Retranslate dialog -*/ -void ObsListCreateEditDialog::retranslate() -{ - if ( dialog ) - ui->retranslateUi ( dialog ); + */ +void ObsListCreateEditDialog::retranslate() { + if (dialog != nullptr) { + ui->retranslateUi(dialog); + } } /* * Style changed -*/ -void ObsListCreateEditDialog::styleChanged() -{ - // Nothing for now + */ +void ObsListCreateEditDialog::styleChanged() { + // Nothing for now } /* * Set the header for the observing list table * (obsListTreeVView) -*/ -void ObsListCreateEditDialog::setObservingListHeaderNames() -{ - const QStringList headerStrings = { - "UUID", // Hided column - q_( "Object name" ), - q_( "Localized Object Name" ), // Hided column - q_( "Type" ), - q_( "Right ascension" ), - q_( "Declination" ), - q_( "Magnitude" ), - q_( "Constellation" ), - q_( "Date" ), // Hided column - q_( "Location" ) // Hided column - }; - - obsListListModel->setHorizontalHeaderLabels ( headerStrings ); + */ +void ObsListCreateEditDialog::setObservingListHeaderNames() { + const QStringList headerStrings = { + "UUID", // Hidden column + q_("Object designation"), + q_("Object name"), + q_("Type"), + q_("Right ascension"), + q_("Declination"), + q_("Magnitude"), + q_("Constellation"), + q_("Date"), // Hided column + q_("Location") // Hided column + }; + + obsListListModel->setHorizontalHeaderLabels(headerStrings); } - /* * Add row in the obsListListModel -*/ -void ObsListCreateEditDialog::addModelRow ( int number, QString uuid, QString name, QString nameI18n, QString type, QString ra, QString dec, QString magnitude, QString constellation ) -{ - QStandardItem* item = Q_NULLPTR; - - item = new QStandardItem ( uuid ); - item->setEditable ( false ); - obsListListModel->setItem ( number, ColumnUUID, item ); - - item = new QStandardItem ( name ); - item->setEditable ( false ); - obsListListModel->setItem ( number, ColumnName, item ); - - item = new QStandardItem ( nameI18n ); - item->setEditable ( false ); - obsListListModel->setItem ( number, ColumnNameI18n, item ); - - item = new QStandardItem ( type ); - item->setEditable ( false ); - obsListListModel->setItem ( number, ColumnType, item ); - - item = new QStandardItem ( ra ); - item->setEditable ( false ); - obsListListModel->setItem ( number, ColumnRa, item ); - - item = new QStandardItem ( dec ); - item->setEditable ( false ); - obsListListModel->setItem ( number, ColumnDec, item ); - - item = new QStandardItem ( magnitude ); - item->setEditable ( false ); - obsListListModel->setItem ( number, ColumnMagnitude, item ); - - item = new QStandardItem ( constellation ); - item->setEditable ( false ); - obsListListModel->setItem ( number, ColumnConstellation, item ); - - for ( int i = 0; i < ColumnCount; ++i ) - ui->obsListCreationEditionTreeView->resizeColumnToContents ( i ); + */ +void ObsListCreateEditDialog::addModelRow(int number, + const QString &olud, + const QString &name, + const QString &nameI18n, + const QString &objtype, + const QString &ra, + const QString &dec, + const QString &magnitude, + const QString &constellation) { + QStandardItem *item = Q_NULLPTR; + + // olud + item = new QStandardItem(olud); + item->setEditable(false); + obsListListModel->setItem(number, ColumnUUID, item); + + // name + item = new QStandardItem(name); + item->setEditable(false); + obsListListModel->setItem(number, ColumnName, item); + + // nameI18n + item = new QStandardItem(nameI18n); + item->setEditable(false); + obsListListModel->setItem(number, ColumnNameI18n, item); + + // objtype (object type) + item = new QStandardItem(objtype); + item->setEditable(false); + obsListListModel->setItem(number, ColumnType, item); + + // ra + item = new QStandardItem(ra); + item->setEditable(false); + obsListListModel->setItem(number, ColumnRa, item); + + // dec + item = new QStandardItem(dec); + item->setEditable(false); + obsListListModel->setItem(number, ColumnDec, item); + + // magnitude + item = new QStandardItem(magnitude); + item->setEditable(false); + obsListListModel->setItem(number, ColumnMagnitude, item); + + // constellation + item = new QStandardItem(constellation); + item->setEditable(false); + obsListListModel->setItem(number, ColumnConstellation, item); + + for (int i = 0; i < ColumnCount; ++i) { + ui->obsListCreationEditionTreeView->resizeColumnToContents(i); + } } - /* - * Slot for button obsListAddObjectButton -*/ -void ObsListCreateEditDialog::obsListAddObjectButtonPressed() -{ - const QList& selectedObject = objectMgr->getSelectedObject(); - if ( !selectedObject.isEmpty() ) - { - // No duplicate item in the same list - bool is_already_in_list = false; - QHash::const_iterator i; - for ( i = observingListItemCollection.constBegin(); i != observingListItemCollection.constEnd(); i++ ) - { - if ( i.value().name.compare ( selectedObject[0]->getEnglishName() ) == 0 ) - { - is_already_in_list = true; - break; - } - } - - if ( !is_already_in_list ) - { - int lastRow = obsListListModel->rowCount(); - QString objectUuid = QUuid::createUuid().toString(); - - QString objectName = selectedObject[0]->getEnglishName(); - QString objectNameI18n = selectedObject[0]->getNameI18n(); - if ( selectedObject[0]->getType() =="Nebula" ) - objectName = GETSTELMODULE ( NebulaMgr )->getLatestSelectedDSODesignation(); - - QString objectRaStr = "", objectDecStr = ""; - bool visibleFlag = false; - double fov = -1.0; - - QString objectType = selectedObject[0]->getType(); - - double ra, dec; - StelUtils::rectToSphe ( &ra, &dec, selectedObject[0]->getJ2000EquatorialPos ( core ) ); - objectRaStr = StelUtils::radToHmsStr ( ra, false ).trimmed(); - objectDecStr = StelUtils::radToDmsStr ( dec, false ).trimmed(); - if ( objectName.contains ( "marker", Qt::CaseInsensitive ) ) - visibleFlag = true; - - if ( objectName.isEmpty() ) - { - objectName = QString ( "%1, %2" ).arg ( objectRaStr, objectDecStr ); - objectNameI18n = q_( "Unnamed object" ); - fov = GETSTELMODULE ( StelMovementMgr )->getCurrentFov(); - } - - float objectMagnitude = selectedObject[0]->getVMagnitude ( core ); - QString objectMagnitudeStr = QString::number ( objectMagnitude ); - - QVariantMap objectMap = selectedObject[0]->getInfoMap ( core ); - QVariant objectConstellationVariant = objectMap["iauConstellation"]; - QString objectConstellation ( "unknown" ); - if ( objectConstellationVariant.canConvert() ) - objectConstellation = objectConstellationVariant.value(); - - QString JDs = ""; - double JD = core->getJD(); - JDs = StelUtils::julianDayToISO8601String ( JD + core->getUTCOffset ( JD ) /24. ).replace ( "T", " " ); - - QString Location = ""; - StelLocation loc = core->getCurrentLocation(); - if ( loc.name.isEmpty() ) - Location = QString ( "%1, %2" ).arg ( loc.latitude ).arg ( loc.longitude ); - else - Location = QString ( "%1, %2" ).arg ( loc.name, loc.region ); - - addModelRow ( lastRow,objectUuid,objectName, objectNameI18n, objectType, objectRaStr, objectDecStr, objectMagnitudeStr, objectConstellation ); - - observingListItem item; - item.name = objectName; - item.nameI18n = objectNameI18n; - if ( !objectType.isEmpty() ) - item.type = objectType; - - if ( !objectRaStr.isEmpty() ) - item.ra = objectRaStr; - - if ( !objectDecStr.isEmpty() ) - item.dec = objectDecStr; - - if ( !objectMagnitudeStr.isEmpty() ) - item.magnitude = objectMagnitudeStr; - - if ( !objectConstellation.isEmpty() ) - item.constellation = objectConstellation; - - if ( !JDs.isEmpty() ) - item.jd = QString::number ( JD, 'f', 6 ); - - if ( !Location.isEmpty() ) - { - // FIXME: gcc warned about an indentation problem. I added the brackets here. But what did you intend to do with the iterator? - //QHash::iterator i; - item.location = Location; - } - - if ( !visibleFlag ) - item.isVisibleMarker = visibleFlag; - - if ( fov > 0.0 ) - item.fov = fov; - - observingListItemCollection.insert ( objectUuid,item ); - } - } - else - qWarning() << "selected object is empty !"; + * Slot for button obsListAddObjectButton. + * Save selected object into the list of observed objects. + */ +void ObsListCreateEditDialog::obsListAddObjectButtonPressed() { + initErrorMessage(); + const QList &selectedObject = objectMgr->getSelectedObject(); + + if (!selectedObject.isEmpty()) { + + // No duplicate item in the same list + bool is_already_in_list = false; + QHash::iterator i; + for (i = observingListItemCollection.begin(); i != observingListItemCollection.end(); i++) { + if (i.value().name.compare(selectedObject[0]->getEnglishName()) == 0) { + is_already_in_list = true; + break; + } + } + + if (!is_already_in_list) { + const int lastRow = obsListListModel->rowCount(); + const QString objectOlud = QUuid::createUuid().toString(); + + // Object name (designation) and object name I18n + QString objectName = selectedObject[0]->getEnglishName(); + QString objectNameI18n = selectedObject[0]->getNameI18n(); + + if(objectNameI18n.isEmpty()){ + objectNameI18n = dash; + } + + StelObjectP object = objectMgr->searchByNameI18n(objectNameI18n); + if (selectedObject[0]->getType() == "Nebula" && objectName.isEmpty()) { + objectName = GETSTELMODULE(NebulaMgr)->getLatestSelectedDSODesignation(); + } + + // Type + QString type = selectedObject[0]->getType(); + + // Object Type + QString objectType = selectedObject[0]->getObjectType(); + + // Ra & Dec + float ra = 0.0; + float dec = 0.0; + QString objectRaStr = ""; + QString objectDecStr = ""; + bool visibleFlag = false; + StelUtils::rectToSphe(&ra, &dec, selectedObject[0]->getJ2000EquatorialPos(core)); + + if (ui->obsListCoordinatesCheckBox->isChecked() || objectType == CUSTOM_OBJECT || objectName.isEmpty()) { + objectRaStr = StelUtils::radToHmsStr(ra, false).trimmed(); + objectDecStr = StelUtils::radToDmsStr(dec, false).trimmed(); + } + + // Fov + double fov = -1.0; + if (ui->obsListFovCheckBox->isChecked() || objectType == CUSTOM_OBJECT || objectName.isEmpty()) { + fov = GETSTELMODULE(StelMovementMgr)->getCurrentFov(); + } + + // Visible flag + if (objectName.contains("marker", Qt::CaseInsensitive)) { + visibleFlag = true; + } + + // Magnitude + QString objectMagnitudeStr = util.getMagnitue(selectedObject, core); + + // Constallation + QVariantMap objectMap = selectedObject[0]->getInfoMap(core); + QVariant objectConstellationVariant = objectMap["iauConstellation"]; + QString objectConstellation("unknown"); + if (objectConstellationVariant.canConvert()) { + objectConstellation = objectConstellationVariant.value(); + } + + // JD + QString JDs = ""; + double JD = core->getJD(); + JDs = StelUtils::julianDayToISO8601String(JD + core->getUTCOffset(JD) / 24.).replace("T", " "); + + // Location + QString Location; + StelLocation loc = core->getCurrentLocation(); + if (loc.name.isEmpty()) { + Location = ""; + } else { + Location = QString("%1, %2").arg(loc.name, loc.region); + } + + // Check if the object name is empty. + if (objectName.isEmpty()) { + objectName = q_("Unnamed object"); + if (objectNameI18n.isEmpty()) { + objectNameI18n = q_("Unnamed object"); + } + } + + // Add objects in row model + addModelRow(lastRow, + objectOlud, + objectName, + objectNameI18n, + objectType, + objectRaStr, + objectDecStr, + objectMagnitudeStr, + objectConstellation); + + observingListItem item; + util.initItem(item); + item.name = objectName; + item.nameI18n = objectNameI18n; + + // Type + if (!type.isEmpty()) { + item.type = type; + } + // Object Type + if (!objectType.isEmpty()) { + item.objtype = objectType; + } + // Ra + if (!objectRaStr.isEmpty()) { + item.ra = objectRaStr; + } + // Dec + if (!objectDecStr.isEmpty()) { + item.dec = objectDecStr; + } + // Magnitude + if (!objectMagnitudeStr.isEmpty()) { + item.magnitude = objectMagnitudeStr; + } + // Constellation + if (!objectConstellation.isEmpty()) { + item.constellation = objectConstellation; + } + // JD + if (!JDs.isEmpty()) { + item.jd = JD; + } + // Location + if (!Location.isEmpty()) { + item.location = Location; + } + // Visible Flag + if (!visibleFlag) { + item.isVisibleMarker = visibleFlag; + } + // Fov + if (fov > 0.0) { + item.fov = fov; + } + observingListItemCollection.insert(objectOlud, item); + } + } else { + qWarning() << "Selected object is empty !"; + } } /* * Slot for button obsListRemoveObjectButton -*/ -void ObsListCreateEditDialog::obsListRemoveObjectButtonPressed() -{ - int number = ui->obsListCreationEditionTreeView->currentIndex().row(); - QString uuid = obsListListModel->index ( number, ColumnUUID ).data().toString(); - obsListListModel->removeRow ( number ); - observingListItemCollection.remove ( uuid ); + */ +void ObsListCreateEditDialog::obsListRemoveObjectButtonPressed() { + initErrorMessage(); + int number = ui->obsListCreationEditionTreeView->currentIndex().row(); + QString uuid = obsListListModel->index(number, ColumnUUID).data().toString(); + obsListListModel->removeRow(number); + observingListItemCollection.remove(uuid); } /* - * Save observed object into json file -*/ -void ObsListCreateEditDialog::saveObservedObject() -{ - QString listName = ui->nameOfListLineEdit->text(); - if ( observingListJsonPath.isEmpty() || listName.isEmpty() ) - { - qWarning() << "[ObservingList Creation/Edition] Error saving observing list"; - return; - } - - QFile jsonFile ( observingListJsonPath ); - if ( !jsonFile.open ( QIODevice::ReadWrite|QIODevice::Text ) ) - { - qWarning() << "[ObservingList Creation/Edition] observing list can not be saved. A file can not be open for reading and writing:" - << QDir::toNativeSeparators ( observingListJsonPath ); - return; - } - - try - { - QVariantMap mapFromJsonFile; - QVariantMap allListsMap; - if ( jsonFile.size() > 0 ) - { - mapFromJsonFile = StelJsonParser::parse ( jsonFile.readAll() ).toMap(); - allListsMap = mapFromJsonFile.value ( QString ( KEY_OBSERVING_LISTS ) ).toMap(); - } - - QVariantMap observingListDataList; - - // Description - QString description = ui->descriptionLineEdit->text(); - observingListDataList.insert ( QString ( KEY_DESCRIPTION ), description ); - - // Julian day - QString JDString = ""; - double JD = core->getJD(); - JDString = QString::number ( JD, 'f', 6 ); - - - // No JD modifications in modification mode - if ( !isCreationMode ) - { - QString uuidQs = QString::fromStdString ( this->listUuid_ ); - QVariantMap currentList = allListsMap.value ( uuidQs ).toMap(); - QVariant existingJD = currentList.value ( QString ( KEY_JD ) ); - QString existingJDs = existingJD.toString(); - if ( existingJDs.isEmpty() ) - observingListDataList.insert ( QString ( KEY_JD ), JDString ); - else - observingListDataList.insert ( QString ( KEY_JD ), existingJDs ); - } - else - observingListDataList.insert ( QString ( KEY_JD ), JDString ); - - // Location - QString Location = ""; - StelLocation loc = core->getCurrentLocation(); - if ( loc.name.isEmpty() ) - Location = QString ( "%1, %2" ).arg ( loc.latitude ).arg ( loc.longitude ); - else - Location = QString ( "%1, %2" ).arg ( loc.name, loc.region ); - - observingListDataList.insert ( QString ( KEY_LOCATION ), Location ); - - // Name of the liste - QString name = ui->nameOfListLineEdit->text(); - observingListDataList.insert ( QString ( KEY_NAME ), name ); - - // List of objects - QVariantList listOfObjects; - QHashIterator i ( observingListItemCollection ); - while ( i.hasNext() ) - { - i.next(); - - observingListItem item = i.value(); - QVariantMap obl; - QString objectName = item.name; - obl.insert ( QString ( KEY_DESIGNATION ), objectName ); - listOfObjects.push_back ( obl ); - } - - observingListDataList.insert ( QString ( KEY_OBJECTS ), listOfObjects ); - observingListDataList.insert ( QString ( KEY_SORTING ), sorting ); - - QString oblListUuid; - if ( isCreationMode ) - oblListUuid = QUuid::createUuid().toString(); - else - oblListUuid = QString::fromStdString ( listUuid_ ); - - if ( ui->obsListDefaultListCheckBox->isChecked() ) - mapFromJsonFile.insert ( KEY_DEFAULT_LIST_UUID, oblListUuid ); - else - { - QString defaultListUuid = mapFromJsonFile.value ( KEY_DEFAULT_LIST_UUID ).toString(); - if ( defaultListUuid == oblListUuid ) - mapFromJsonFile.insert ( KEY_DEFAULT_LIST_UUID, "" ); - } - - mapFromJsonFile.insert ( KEY_VERSION, "1.0" ); - mapFromJsonFile.insert ( KEY_SHORT_NAME, "Observing lists for Stellarium" ); - - allListsMap.insert ( oblListUuid, observingListDataList ); - mapFromJsonFile.insert ( QString ( KEY_OBSERVING_LISTS ), allListsMap ); - - jsonFile.resize ( 0 ); - StelJsonParser::write ( mapFromJsonFile, &jsonFile ); - jsonFile.flush(); - jsonFile.close(); - } - catch ( std::runtime_error &e ) - { - qCritical() << "[ObservingList Creation/Edition] File format is wrong! Error: " << e.what(); - return; - } + * Save observed objects and the list into Json file + */ +void ObsListCreateEditDialog::saveObservedObjectsInJsonFile() { + + if (observingListJsonPath.isEmpty()) { + qWarning() << "[ObservingList Creation/Edition] Error saving observing list"; + return; + } + + QFile jsonFile(observingListJsonPath); + if (!jsonFile.open(QIODevice::ReadWrite | QIODevice::Text)) { + qWarning() << "[ObservingList Creation/Edition] observing list can not be saved. A file can not be open for " + "reading and writing:" + << QDir::toNativeSeparators(observingListJsonPath); + return; + } + + // Name of the list + const QString listName = ui->nameOfListLineEdit->text(); + + // Creation date + double JD = core->getJD(); + QString listCreationDate = StelUtils::julianDayToISO8601String(JD + core->getUTCOffset(JD) / 24.).replace("T", " "); + + try { + QVariantMap mapFromJsonFile; + QVariantMap allListsMap; + const QString oludQs = QString::fromStdString(this->listOlud_); + const QVariantMap currentList = allListsMap.value(oludQs).toMap(); + if (jsonFile.size() > 0) { + mapFromJsonFile = StelJsonParser::parse(jsonFile.readAll()).toMap(); + allListsMap = mapFromJsonFile.value(QString(KEY_OBSERVING_LISTS)).toMap(); + } + + QVariantMap observingListDataList; + + // Name of the liste + observingListDataList.insert(QString(KEY_NAME), listName); + + // Description of the list + QString description = ui->descriptionLineEdit->text(); + observingListDataList.insert(QString(KEY_DESCRIPTION), description); + + // Creation date + observingListDataList.insert(QString(KEY_CREATION_DATE), listCreationDate); + + // Landscape + if (ui->obsListLandscapeCheckBox->isChecked()) { + QString landscapeId = landscapeMgr->getCurrentLandscapeID(); + observingListDataList.insert(QString(KEY_LANDSCAPE_ID), landscapeId); + } else { + observingListDataList.insert(QString(KEY_LANDSCAPE_ID), ""); + } + + // List of objects + QVariantList listOfObjects; + QHashIterator i(observingListItemCollection); + while (i.hasNext()) { + i.next(); + observingListItem item = i.value(); + QVariantMap obl; + + // Designation (name) + obl.insert(QString(KEY_DESIGNATION), item.name); + + // NameI18n + obl.insert(QString(KEY_NAME_I18N), item.nameI18n); + + // Fov + obl.insert(QString(KEY_FOV), item.fov); + + // Jd + obl.insert(QString(KEY_JD), item.jd); + + // Location + obl.insert(QString(KEY_LOCATION), item.location); + + // Ra + obl.insert(QString(KEY_RA), item.ra); + + // Dec + obl.insert(QString(KEY_DEC), item.dec); + + // Type + obl.insert(QString(KEY_TYPE), item.type); + + // Object type + obl.insert(QString(KEY_OBJECTS_TYPE), item.objtype); + + // Magnitude + obl.insert(QString(KEY_MAGNITUDE), item.magnitude); + + // Constellation + obl.insert(QString(KEY_CONSTELLATION), item.constellation); + + // Visible marker + obl.insert(QString(KEY_IS_VISIBLE_MARKER), item.isVisibleMarker); + + listOfObjects.push_back(obl); + } + observingListDataList.insert(QString(KEY_OBJECTS), listOfObjects); + + // Sorting + QString existingSorting; + if (!isCreationMode) { + existingSorting = currentList.value(QString(KEY_SORTING)).toString(); + } + if (sorting.isEmpty()) { + observingListDataList.insert(QString(KEY_SORTING), existingSorting); + } else { + observingListDataList.insert(QString(KEY_SORTING), sorting); + } + + // Olud + QString oblListOlud; + if (isCreationMode || isSaveAs) { + oblListOlud = QUuid::createUuid().toString(); + } else { + oblListOlud = QString::fromStdString(listOlud_); + } + + // Default list + if (ui->obsListDefaultListCheckBox->isChecked()) { + mapFromJsonFile.insert(KEY_DEFAULT_LIST_OLUD, oblListOlud); + } else { + QString defaultListUuid = mapFromJsonFile.value(KEY_DEFAULT_LIST_OLUD).toString(); + if (defaultListUuid.isEmpty()) { + mapFromJsonFile.insert(KEY_DEFAULT_LIST_OLUD, ""); + } else { + int compareResult = QString::compare(defaultListUuid, QString::fromStdString(listOlud_), + Qt::CaseSensitive); + if (compareResult == 0) { + mapFromJsonFile.insert(KEY_DEFAULT_LIST_OLUD, ""); + } + } + } + + // Version + mapFromJsonFile.insert(KEY_VERSION, FILE_VERSION); + // Short name + mapFromJsonFile.insert(KEY_SHORT_NAME, SHORT_NAME_VALUE); + + allListsMap.insert(oblListOlud, observingListDataList); + mapFromJsonFile.insert(QString(KEY_OBSERVING_LISTS), allListsMap); + + jsonFile.resize(0); + StelJsonParser::write(mapFromJsonFile, &jsonFile); + jsonFile.flush(); + jsonFile.close(); + + } catch (std::runtime_error &e) { + qCritical() << "[ObservingList Creation/Edition] File format is wrong! Error: " << e.what(); + return; + } } /* * Slot for button obsListExportListButton -*/ -void ObsListCreateEditDialog::obsListExportListButtonPressed() -{ - QString originalobservingListJsonPath = observingListJsonPath; - - QString filter = "JSON (*.json)"; - observingListJsonPath = QFileDialog::getSaveFileName ( Q_NULLPTR, - q_ ( "Export observing list as..." ), - QDir::homePath() + "/" + JSON_FILE_NAME, - filter ); - saveObservedObject(); - observingListJsonPath = originalobservingListJsonPath; + */ +void ObsListCreateEditDialog::obsListExportListButtonPressed() { + initErrorMessage(); + QString originalobservingListJsonPath = observingListJsonPath; + + QString filter = "JSON (*.json)"; + observingListJsonPath = QFileDialog::getSaveFileName(Q_NULLPTR, q_("Export observing list as..."), + QDir::homePath() + "/" + JSON_FILE_NAME, filter); + saveObservedObjectsInJsonFile(); + observingListJsonPath = originalobservingListJsonPath; } /* * Slot for button obsListImportListButton -*/ -void ObsListCreateEditDialog::obsListImportListButtonPresssed() -{ - QString originalobservingListJsonPath = observingListJsonPath; - - QString filter = "JSON (*.json)"; - observingListJsonPath = QFileDialog::getOpenFileName ( Q_NULLPTR, q_ ( "Import observing list" ), QDir::homePath(), filter ); - - - QVariantMap map; - QFile jsonFile ( observingListJsonPath ); - if ( !jsonFile.open ( QIODevice::ReadOnly ) ) - qWarning() << "[ObservingList Creation/Edition] cannot open" << QDir::toNativeSeparators ( JSON_FILE_NAME ); - else - { - try - { - map = StelJsonParser::parse ( jsonFile.readAll() ).toMap(); - jsonFile.close(); - QVariantMap observingListMap = map.value ( QString ( KEY_OBSERVING_LISTS ) ).toMap(); - - if ( observingListMap.size() == 1 ) - { - QMap::const_iterator i; - listUuid_ = i.value().toString().toStdString(); - } - else - { - // define error message if needed - return; - } - } - catch ( std::runtime_error &e ) - { - qWarning() << "[ObservingList Creation/Edition] File format is wrong! Error: " << e.what(); - return; - } - loadObservingList(); - observingListJsonPath = originalobservingListJsonPath; - saveObservedObject(); - } + */ +void ObsListCreateEditDialog::obsListImportListButtonPresssed() { + + QString filter = "JSON (*.json)"; + QString fileToImportJsonPath = QFileDialog::getOpenFileName(Q_NULLPTR, q_("Import observing list"), + QDir::homePath(), + filter); + QVariantMap map; + QFile jsonFile(fileToImportJsonPath); + if (!jsonFile.open(QIODevice::ReadOnly)) { + qWarning() << "[ObservingList Creation/Edition import] cannot open" + << QDir::toNativeSeparators(jsonFile.fileName()); + } else { + + try { + initErrorMessage(); + map = StelJsonParser::parse(jsonFile.readAll()).toMap(); + jsonFile.close(); + + if (map.contains(KEY_OBSERVING_LISTS)) { // Case of observingList import + qDebug() << "ObservingList import"; + + QVariantMap observingListMap = map.value(QString(KEY_OBSERVING_LISTS)).toMap(); + if (!observingListMap.isEmpty() && observingListMap.size() == 1) { + listOlud_ = observingListMap.keys().at(0).toStdString(); + } else { + qWarning() + << "[ObservingList Creation/Edition import] there is no list or more than one list."; + displayErrorMessage("Error: here is no list or more than one list."); + return; + } + + QString originalobservingListJsonPath = observingListJsonPath; + observingListJsonPath = fileToImportJsonPath; + loadObservingList(); + observingListJsonPath = originalobservingListJsonPath; + } else if (map.contains(KEY_BOOKMARKS)) { // Case of legacy bookmarks import + qDebug() << "Legacy bookmarks import"; + QVariantMap bookmarksListMap = map.value(QString(KEY_BOOKMARKS)).toMap(); + if (!bookmarksListMap.isEmpty()) { + listOlud_ = bookmarksListMap.keys().at(0).toStdString(); + QString originalobservingListJsonPath = observingListJsonPath; + observingListJsonPath = fileToImportJsonPath; + loadBookmarksInObservingList(); + observingListJsonPath = originalobservingListJsonPath; + } else { + qWarning() + << "[ObservingList Creation/Edition import] the file is empty or doesn't contains legacy bookmarks."; + displayErrorMessage("Error: the file is empty or doesn't contains legacy bookmarks."); + return; + } + } + } catch (std::runtime_error &e) { + qWarning() << "[ObservingList Creation/Edition] File format is wrong! Error: " << e.what(); + displayErrorMessage("File format is wrong!"); + return; + } + } } /* * Slot for button obsListSaveButton -*/ -void ObsListCreateEditDialog::obsListSaveButtonPressed() -{ - saveObservedObject(); - this->close(); - emit exitButtonClicked(); + */ +void ObsListCreateEditDialog::obsListSaveButtonPressed() { + initErrorMessage(); + QString listName = ui->nameOfListLineEdit->text(); + + //delete with space at the end of the name + listName = QString(listName).remove(QRegularExpression("([ ]+)$")); + + bool isListAlreadyExists = !this->listNames_.isEmpty() && this->listNames_.contains(listName) && + (isCreationMode || (listName.compare(currentListName) != 0 && !isCreationMode)); + + if (isListAlreadyExists) { + QString errorMessage; + errorMessage.append("Error: a list with the name ") + .append(ui->nameOfListLineEdit->text()) + .append(" already exists !"); + qWarning() << "[ObservingList Creation/Edition] Error: a list with the name " << ui->nameOfListLineEdit->text() + << " already exists !"; + std::string errorMessage_str = errorMessage.toStdString(); + displayErrorMessage(errorMessage_str.c_str()); + } else if (ui->nameOfListLineEdit->text().isEmpty()) { + qWarning() << "[ObservingList Creation/Edition] Error: the list name is empty."; + displayErrorMessage("Error: the list name is empty."); + } else { + isSaveAs = listName.compare(currentListName) != 0 && !isCreationMode; + saveObservedObjectsInJsonFile(); + this->close(); + emit exitButtonClicked(); + } } /* * Slot for button obsListExitButton -*/ -void ObsListCreateEditDialog::obsListExitButtonPressed() -{ - this->close(); - emit exitButtonClicked(); + */ +void ObsListCreateEditDialog::obsListExitButtonPressed() { + ui->obsListErrorMessage->setHidden(true); + ui->obsListErrorMessage->clear(); + this->close(); } /* * Overload StelDialog::close() -*/ -void ObsListCreateEditDialog::close() -{ - this->setVisible ( false ); - emit this->exitButtonClicked(); + */ +void ObsListCreateEditDialog::close() { + this->setVisible(false);; + emit this->exitButtonClicked(); } /* * Slot for obsListCreationEditionTreeView header -*/ -void ObsListCreateEditDialog::headerClicked ( int index ) -{ - switch ( index ) - { - case ColumnName: - sorting = QString ( SORTING_BY_NAME ); - break; - case ColumnType: - sorting = QString ( SORTING_BY_TYPE ); - break; - case ColumnRa: - sorting = QString ( SORTING_BY_RA ); - break; - case ColumnDec: - sorting = QString ( SORTING_BY_DEC ); - break; - case ColumnMagnitude: - sorting = QString ( SORTING_BY_MAGNITUDE ); - break; - case ColumnConstellation: - sorting = QString ( SORTING_BY_CONSTELLATION ); - break; - default: - sorting = ""; - break; - } - qDebug() << "Sorting = " << sorting; + */ +void ObsListCreateEditDialog::headerClicked(int index) { + switch (index) { + case ColumnName: + sorting = QString(SORTING_BY_NAME); + break; + case ColumnNameI18n: + sorting = QString(SORTING_BY_NAMEI18N); + break; + case ColumnType: + sorting = QString(SORTING_BY_TYPE); + break; + case ColumnRa: + sorting = QString(SORTING_BY_RA); + break; + case ColumnDec: + sorting = QString(SORTING_BY_DEC); + break; + case ColumnMagnitude: + sorting = QString(SORTING_BY_MAGNITUDE); + break; + case ColumnConstellation: + sorting = QString(SORTING_BY_CONSTELLATION);; + break; + default: + sorting = ""; + break; + } + qDebug() << "Sorting = " << sorting; } /* * Load the observing list in case of edit mode + */ +void ObsListCreateEditDialog::loadObservingList() { + + QVariantMap map; + QFile jsonFile(observingListJsonPath); + if (!jsonFile.open(QIODevice::ReadOnly)) { + qWarning() << "[ObservingList Creation/Edition] cannot open" << QDir::toNativeSeparators(JSON_FILE_NAME); + } else { + try { + map = StelJsonParser::parse(jsonFile.readAll()).toMap(); + jsonFile.close(); + + // Get the default list uuid + QString defaultListOlud = map.value(KEY_DEFAULT_LIST_OLUD).toString(); + if (defaultListOlud.toStdString() == listOlud_) { + ui->obsListDefaultListCheckBox->setChecked(true); + } + + observingListItemCollection.clear(); + const QString keyOlud = QString::fromStdString(listOlud_); + QVariantMap observingListMap = map.value(QString(KEY_OBSERVING_LISTS)).toMap().value(keyOlud).toMap(); + QVariantList listOfObjects; + + auto listName = observingListMap.value(QString(KEY_NAME)).value(); + + // List name + currentListName = listName; + ui->nameOfListLineEdit->setText(listName); + + // List description + auto listDescription = observingListMap.value(QString(KEY_DESCRIPTION)).value(); + ui->descriptionLineEdit->setText(listDescription); + + // Landscape + auto landscape = observingListMap.value(QString(KEY_LANDSCAPE_ID)).value(); + if (!landscape.isEmpty()) { + ui->obsListLandscapeCheckBox->setChecked(true); + } + + if (observingListMap.value(QString(KEY_OBJECTS)).canConvert()) { + QVariant data = observingListMap.value(QString(KEY_OBJECTS)); + listOfObjects = data.value(); + } else { + qCritical() << "[ObservingList Creation/Edition] conversion error"; + return; + } + + for (const QVariant &object: listOfObjects) { + QVariantMap objectMap; + if (object.canConvert()) { + objectMap = object.value(); + int lastRow = obsListListModel->rowCount(); + QString objectOlud = QUuid::createUuid().toString(); + + // Name + auto objectName = objectMap.value(QString(KEY_DESIGNATION)).value(); + + // NameI18n + auto objectNameI18n = objectMap.value(QString(KEY_NAME_I18N)).value(); + + // Fov + auto fov = objectMap.value(QString(KEY_FOV)).value(); + + // Object type + auto type = objectMap.value(QString(KEY_TYPE)).value(); + + // Object type + auto objectType = objectMap.value(QString(KEY_OBJECTS_TYPE)).value(); + + // Ra & dec + auto objectRaStr = objectMap.value(QString(KEY_RA)).value(); + auto objectDecStr = objectMap.value(QString(KEY_DEC)).value(); + + // Magnitude + auto objectMagnitudeStr = objectMap.value(QString(KEY_MAGNITUDE)).value(); + + // Constellation + auto objectConstellation = objectMap.value(QString(KEY_CONSTELLATION)).value(); + + // Julian Day + auto JDs = objectMap.value(QString(KEY_JD)).value(); + + // Location + auto location = objectMap.value(QString(KEY_LOCATION)).value(); + + // Visible flag + bool visibleFlag = objectMap.value(QString(KEY_IS_VISIBLE_MARKER)).value(); + + + + // Add data into model row + addModelRow(lastRow, + objectOlud, + objectName, + objectNameI18n, + objectType, + objectRaStr, + objectDecStr, + objectMagnitudeStr, + objectConstellation); + + + observingListItem item; + util.initItem(item); + item.name = objectName; + item.nameI18n = objectNameI18n; + + // Type + if (!type.isEmpty()) { + item.type = type; + } + + // Object type + if (!objectType.isEmpty()) { + item.objtype = objectType; + } + + // Ra + if (!objectRaStr.isEmpty()) { + item.ra = objectRaStr; + } + + // Dec + if (!objectDecStr.isEmpty()) { + item.dec = objectDecStr; + } + + // Magnitude + if (!objectMagnitudeStr.isEmpty()) { + item.magnitude = objectMagnitudeStr; + } + + // Constellation + if (!objectConstellation.isEmpty()) { + item.constellation = objectConstellation; + } + + // JD + if (!JDs.isEmpty()) { + item.jd = JDs.toDouble(); + } + + // Location + if (!location.isEmpty()) { + item.location = location; + } + + // Flag + if (!visibleFlag) { + item.isVisibleMarker = visibleFlag; + } + + // Fov + if (fov > 0.0) { + item.fov = fov; + } + observingListItemCollection.insert(objectOlud, item); + + } else { + qCritical() << "[ObservingList Creation/Edition] conversion error"; + return; + } + } + + objectMgr->unSelect(); + + } catch (std::runtime_error &e) { + qWarning() << "[ObservingList Creation/Edition] File format is wrong! Error: " << e.what(); + return; + } + } +} + +/* +* Load the bookmarks of bookmarks.json file into observing lists file +* For no regression with must take into account the legacy bookmarks.json file */ -void ObsListCreateEditDialog::loadObservingList() -{ - QVariantMap map; - QFile jsonFile ( observingListJsonPath ); - if ( !jsonFile.open ( QIODevice::ReadOnly ) ) - qWarning() << "[ObservingList Creation/Edition] cannot open" << QDir::toNativeSeparators ( JSON_FILE_NAME ); - else - { - try - { - map = StelJsonParser::parse ( jsonFile.readAll() ).toMap(); - jsonFile.close(); - - // Get the default list uuid - QString defaultListUuid = map.value ( KEY_DEFAULT_LIST_UUID ).toString(); - if ( defaultListUuid.toStdString() == listUuid_ ) - ui->obsListDefaultListCheckBox->setChecked ( true ); - - observingListItemCollection.clear(); - const QString keyUuid = QString::fromStdString ( listUuid_ ); - QVariantMap observingListMap = map.value ( QString ( KEY_OBSERVING_LISTS ) ).toMap().value ( keyUuid ).toMap(); - QVariantList listOfObjects; - - QString listeName = observingListMap.value ( QString ( KEY_NAME ) ).value(); - ui->nameOfListLineEdit->setText ( listeName ); - QString listDescription = observingListMap.value ( QString ( KEY_DESCRIPTION ) ).value(); - ui->descriptionLineEdit->setText ( listDescription ); - - if ( observingListMap.value ( QString ( KEY_OBJECTS ) ).canConvert() ) - { - QVariant data = observingListMap.value ( QString ( KEY_OBJECTS ) ); - listOfObjects = data.value(); - } - else - { - qCritical() << "[ObservingList Creation/Edition] conversion error"; - return; - } - - for ( const QVariant &object: qAsConst(listOfObjects) ) - { - QVariantMap objectMap; - if ( object.canConvert() ) - { - objectMap = object.value(); - QString objectName = objectMap.value ( QString ( KEY_DESIGNATION ) ).value(); - - if ( objectMgr->findAndSelect ( objectName ) ) - { - const QList& selectedObject = objectMgr->getSelectedObject(); - if ( !selectedObject.isEmpty() ) - { - int lastRow = obsListListModel->rowCount(); - QString objectUuid = QUuid::createUuid().toString(); - QString objectNameI18n = selectedObject[0]->getNameI18n(); - QString objectRaStr = "", objectDecStr = ""; - bool visibleFlag = false; - double fov = -1.0; - - QString objectType = selectedObject[0]->getType(); - - double ra, dec; - StelUtils::rectToSphe ( &ra, &dec, selectedObject[0]->getJ2000EquatorialPos ( core ) ); - objectRaStr = StelUtils::radToHmsStr ( ra, false ).trimmed(); - objectDecStr = StelUtils::radToDmsStr ( dec, false ).trimmed(); - if ( objectName.contains ( "marker", Qt::CaseInsensitive ) ) - visibleFlag = true; - - float objectMagnitude = selectedObject[0]->getVMagnitude ( core ); - QString objectMagnitudeStr = QString::number ( objectMagnitude ); - - QVariantMap objectMap = selectedObject[0]->getInfoMap ( core ); - QVariant objectConstellationVariant = objectMap["iauConstellation"]; - QString objectConstellation ( "unknown" ); - if ( objectConstellationVariant.canConvert() ) - objectConstellation = objectConstellationVariant.value(); - - QString JDs = ""; - double JD = core->getJD(); - - JDs = StelUtils::julianDayToISO8601String ( JD + core->getUTCOffset ( JD ) /24. ).replace ( "T", " " ); - - QString Location = ""; - StelLocation loc = core->getCurrentLocation(); - if ( loc.name.isEmpty() ) - Location = QString ( "%1, %2" ).arg ( loc.latitude ).arg ( loc.longitude ); - else - Location = QString ( "%1, %2" ).arg ( loc.name, loc.region ); - - addModelRow ( lastRow,objectUuid,objectName, objectNameI18n, objectType, objectRaStr, objectDecStr, objectMagnitudeStr, objectConstellation ); - - observingListItem item; - item.name = objectName; - item.nameI18n = objectNameI18n; - if ( !objectType.isEmpty() ) - item.type = objectType; - - if ( !objectRaStr.isEmpty() ) - item.ra = objectRaStr; - - if ( !objectDecStr.isEmpty() ) - item.dec = objectDecStr; - - if ( !objectMagnitudeStr.isEmpty() ) - item.magnitude = objectMagnitudeStr; - - if ( !objectConstellation.isEmpty() ) - item.constellation = objectConstellation; - - if ( !JDs.isEmpty() ) - item.jd = JDs; - - if ( !Location.isEmpty() ) - item.location = Location; - - if ( !visibleFlag ) - item.isVisibleMarker = visibleFlag; - - if ( fov > 0.0 ) - item.fov = fov; - - observingListItemCollection.insert ( objectUuid,item ); - } - else - qWarning() << "[ObservingList Creation/Edition] selected object is empty !"; - } - else - qWarning() << "[ObservingList Creation/Edition] object: " << objectName << " not found !" ; - } - else - { - qCritical() << "[ObservingList Creation/Edition] conversion error"; - return; - } - } - objectMgr->unSelect(); - } - catch ( std::runtime_error &e ) - { - qWarning() << "[ObservingList Creation/Edition] File format is wrong! Error: " << e.what(); - return; - } - } +void ObsListCreateEditDialog::loadBookmarksInObservingList() { + QVariantMap map; + + QFile jsonFile(observingListJsonPath); + if (!jsonFile.open(QIODevice::ReadOnly)) { + qWarning() << "[ObservingList] cannot open" << QDir::toNativeSeparators(observingListJsonPath); + } else { + + try { + + map = StelJsonParser::parse(jsonFile.readAll()).toMap(); + jsonFile.close(); + QVariantMap bookmarksMap = map.value(KEY_BOOKMARKS).toMap(); + observingListItemCollection.clear(); + + for (const auto &bookmarkKey: bookmarksMap.keys()) { + + QVariantMap bookmarkData = bookmarksMap.value(bookmarkKey).toMap(); + observingListItem item; + util.initItem(item); + QString objectUuid = QUuid::createUuid().toString(); + int lastRow = obsListListModel->rowCount(); + + // Name + QString objectName = bookmarkData.value(KEY_NAME).toString(); + item.name = objectName; + + // We need to select the object to add additional information that is not in the Bookmark file + if (objectMgr->findAndSelect(item.name) && !objectMgr->getSelectedObject().isEmpty()) { + const QList &selectedObject = objectMgr->getSelectedObject(); + + // Ra & Dec - ra and dec are not empty in case of Custom Object + QString raStr = bookmarkData.value(KEY_RA).toString(); + QString decStr = bookmarkData.value(KEY_DEC).toString(); + if (raStr.isEmpty() || decStr.isEmpty()) { + float ra = 0.0; + float dec = 0.0; + StelUtils::rectToSphe(&ra, &dec, selectedObject[0]->getJ2000EquatorialPos(core)); + raStr = StelUtils::radToHmsStr(ra, false).trimmed(); + decStr = StelUtils::radToDmsStr(dec, false).trimmed(); + } + + if (!raStr.isEmpty()) { + item.ra = raStr; + } else { + item.ra = ""; + } + + if (!decStr.isEmpty()) { + item.dec = decStr; + } else { + item.dec = ""; + } + + // NameI18n + QString nameI18n = bookmarkData.value(KEY_NAME_I18N).toString(); + if (!nameI18n.isEmpty()) { + item.nameI18n = nameI18n; + } else { + item.nameI18n = ""; + } + + // JDs + QString JDs = bookmarkData.value(KEY_JD).toString(); + if (!JDs.isEmpty()) { + item.jd = JDs.toDouble(); + } else { + item.jd = 0.0; + } + + // Location + QString location = bookmarkData.value(KEY_LOCATION).toString(); + if (!location.isEmpty()) { + item.location = location; + } else { + item.location = ""; + } + + // Constallation + QVariantMap objectMap = selectedObject[0]->getInfoMap(core); + QVariant objectConstellationVariant = objectMap["iauConstellation"]; + QString objectConstellation("unknown"); + if (objectConstellationVariant.canConvert()) { + objectConstellation = objectConstellationVariant.value(); + } + if (!objectConstellation.isEmpty()) { + item.constellation = objectConstellation; + } + + // Type + QString type = selectedObject[0]->getType(); + if (!type.isEmpty()) { + item.type = type; + } + + // Object Type + QString objectType = selectedObject[0]->getObjectType(); + if (!objectType.isEmpty()) { + item.objtype = objectType; + } + + // Magnitude + QString objectMagnitudeStr = util.getMagnitue(selectedObject, core); + if (!objectMagnitudeStr.isEmpty()) { + item.magnitude = objectMagnitudeStr; + } + + // Fov + double fov = bookmarkData.value(KEY_FOV).toDouble(); + if (fov > 0.0) { + item.fov = fov; + } + + // Visible marker + item.isVisibleMarker = bookmarkData.value(KEY_IS_VISIBLE_MARKER, false).toBool(); + + // Add data into model row + addModelRow(lastRow, + objectUuid, + objectName, + nameI18n, + objectType, + raStr, + decStr, + objectMagnitudeStr, + objectConstellation); + + observingListItemCollection.insert(objectUuid, item); + } + } + objectMgr->unSelect(); + } catch (std::runtime_error &e) { + qWarning() << "[ObservingList] Load bookmarks in observing list: File format is wrong! Error: " << e.what(); + return; + } + } +} + +/* + * Called when the text of the nameOfListLineEdit change + */ +void ObsListCreateEditDialog::nameOfListTextChange() { + ui->obsListErrorMessage->setHidden(true); + //delete with space -> no list with only white space as name + QString listeName = QString(ui->nameOfListLineEdit->text()).remove(QRegularExpression("([ ]+)$")); + if (listeName.isEmpty()) { + ui->obsListSaveButton->setEnabled(false); + } else { + ui->obsListSaveButton->setEnabled(true); + } +} + +/* + * Setter for listName + */ +void ObsListCreateEditDialog::setListName(QList listName) { + this->listNames_ = std::move(listName); +} + +/* + * Initialize error mssage + */ +void ObsListCreateEditDialog::initErrorMessage() { + ui->obsListErrorMessage->setHidden(true); + ui->obsListErrorMessage->clear(); +} + +/* + * Display error message + */ +void ObsListCreateEditDialog::displayErrorMessage(const char *message) { + QString errorMessage; + errorMessage.append(q_(message)); + ui->obsListErrorMessage->setHidden(false); + ui->obsListErrorMessage->setText(errorMessage); } /* * Destructor of singleton -*/ -void ObsListCreateEditDialog::kill() -{ - if ( m_instance != nullptr ) - { - delete m_instance; - m_instance = nullptr; - } + */ +void ObsListCreateEditDialog::kill() { + if (m_instance != nullptr) { + delete m_instance; + m_instance = nullptr; + } } diff --git a/src/gui/ObsListCreateEditDialog.hpp b/src/gui/ObsListCreateEditDialog.hpp index 1b5b2fc785348..18b17f361a168 100644 --- a/src/gui/ObsListCreateEditDialog.hpp +++ b/src/gui/ObsListCreateEditDialog.hpp @@ -30,79 +30,118 @@ class Ui_obsListCreateEditDialogForm; -class ObsListCreateEditDialog : public StelDialog -{ - Q_OBJECT +class ObsListCreateEditDialog : public StelDialog { +Q_OBJECT public: - static ObsListCreateEditDialog * Instance ( std::string listUuid ); - static void kill(); + static auto Instance(std::string listUuid) -> ObsListCreateEditDialog *; - //! Notify that the application style changed - void styleChanged() Q_DECL_OVERRIDE; + static void kill(); - //! called when click on button close in top right corner - void close() Q_DECL_OVERRIDE; + //! Notify that the application style changed + void styleChanged() override; + + //! called when click on button close in top right corner + void close() override; + + void setListName(QList listName); protected: - static ObsListCreateEditDialog * m_instance; - Ui_obsListCreateEditDialogForm *ui; - //! Initialize the dialog widgets and connect the signals/slots. - virtual void createDialogContent() Q_DECL_OVERRIDE; + //! Initialize the dialog widgets and connect the signals/slots. + void createDialogContent() override; private: - //! To know if the dialog is open in creation mode or editionn mode - // if true we are in creation mode otherwise in edition mode - bool isCreationMode; - QStandardItemModel * obsListListModel; - class StelCore* core; - class StelObjectMgr* objectMgr; - std::string listUuid_; - QString observingListJsonPath; - QHash observingListItemCollection; - - //! Sorting of the list ex: right ascencion - QString sorting; - - //! Set header names for observing list table - void setObservingListHeaderNames(); - - //! Add row in the obsListListModel - //! @param number row number - //! @param uuid id of the record - //! @param name name or the designation of the object - //! @param type type of the object - //! @param ra right ascencion of the object - //! @param dec declination of the object - //! @param magnitude magnitude of the object - //! @param constellation constellation in which the object is located - void addModelRow ( int number, QString uuid, QString name, QString nameI18n, QString type, QString ra, QString dec, QString magnitude, QString constellation ); - - //! Save the object informations into json file - void saveObservedObject(); - - //! Load the observing liste in case of edit mode - void loadObservingList(); - - //Private constructor and destructor - ObsListCreateEditDialog ( std::string listUuid ); - virtual ~ObsListCreateEditDialog() Q_DECL_OVERRIDE; + static ObsListCreateEditDialog *m_instance; + Ui_obsListCreateEditDialogForm *ui; + //! To know if the dialog is open in creation mode or editionn mode + // if true we are in creation mode otherwise in edition mode + bool isCreationMode{}; + bool isSaveAs{}; + QStandardItemModel *obsListListModel; + + class StelCore *core; + + class StelObjectMgr *objectMgr; + + class LandscapeMgr *landscapeMgr; + + std::string listOlud_; + QString observingListJsonPath; + ObservingListUtil util; + + // Data for observed objects + QHash observingListItemCollection; + + //List names + QList listNames_; + + //Current list name + QString currentListName; + + //! Sorting of the list ex: right ascension + QString sorting; + + //! Set header names for observing list table + void setObservingListHeaderNames(); + + //! Add row in the obsListListModel + //! @param number row number + //! @param olud id of the record + //! @param name name or the designation of the object + //! @param type type of the object + //! @param ra right ascension of the object + //! @param dec declination of the object + //! @param magnitude magnitude of the object + //! @param constellation constellation in which the object is located + void addModelRow(int number, const QString &olud, const QString &name, const QString &nameI18n, const QString &type, + const QString &ra, const QString &dec, const QString &magnitude, const QString &constellation); + + //! Save the object informations into json file + void saveObservedObjectsInJsonFile(); + + //! Load the observing liste in case of edit mode + void loadObservingList(); + + //! Load bookmark in observing list for import. + void loadBookmarksInObservingList(); + //! Initialize the error message (obsListErrorMessage). + void initErrorMessage(); + + //! Display the error message. + void displayErrorMessage(const char *message); + + //Private constructor and destructor + explicit ObsListCreateEditDialog(std::string listUuid); + + ~ObsListCreateEditDialog() override; public slots: - void retranslate() Q_DECL_OVERRIDE; + + void retranslate() override; + private slots: - void obsListAddObjectButtonPressed(); - void obsListRemoveObjectButtonPressed(); - void obsListExportListButtonPressed(); - void obsListImportListButtonPresssed(); - void obsListSaveButtonPressed(); - void obsListExitButtonPressed(); - void headerClicked ( int index ); + + void obsListAddObjectButtonPressed(); + + void obsListRemoveObjectButtonPressed(); + + void obsListExportListButtonPressed(); + + void obsListImportListButtonPresssed(); + + void obsListSaveButtonPressed(); + + void obsListExitButtonPressed(); + + void headerClicked(int index); + + void nameOfListTextChange(); signals: - //To notified that the exit button is clicked - void exitButtonClicked(); + + //To notified that the exit button is clicked + void exitButtonClicked(); }; #endif // OBSLISTCREATEEDITDIALOG_H diff --git a/src/gui/ObsListDialog.cpp b/src/gui/ObsListDialog.cpp index 373f2fefc7781..f7290d0ffcb1b 100644 --- a/src/gui/ObsListDialog.cpp +++ b/src/gui/ObsListDialog.cpp @@ -28,692 +28,1138 @@ #include "StelFileMgr.hpp" #include "StelMovementMgr.hpp" #include "CustomObjectMgr.hpp" +#include "LandscapeMgr.hpp" #include "HighlightMgr.hpp" #include "StarMgr.hpp" #include "StelJsonParser.hpp" #include "StelUtils.hpp" #include "ObsListDialog.hpp" #include "LabelMgr.hpp" +#include "Planet.hpp" #include "ui_obsListDialog.h" +#include +#include + +using namespace std; + + +ObsListDialog::ObsListDialog(QObject *parent) : StelDialog("Observing list", parent), ui(new Ui_obsListDialogForm()), + obsListListModel(new QStandardItemModel(0, ColumnCount)), + core(StelApp::getInstance().getCore()), + currentJd(core->getJD()), createEditDialog_instance(Q_NULLPTR) { + + objectMgr = GETSTELMODULE (StelObjectMgr); + labelMgr = GETSTELMODULE (LabelMgr); + landscapeMgr = GETSTELMODULE(LandscapeMgr); + + observingListJsonPath = + StelFileMgr::findFile("data", + static_cast(StelFileMgr::Directory | StelFileMgr::Writable)) + + "/" + + QString(JSON_FILE_NAME); + bookmarksJsonPath = + StelFileMgr::findFile("data", + static_cast(StelFileMgr::Directory | StelFileMgr::Writable)) + + "/" + + QString(JSON_BOOKMARKS_FILE_NAME); -ObsListDialog::ObsListDialog ( QObject* parent ) : StelDialog ( "Observing list", parent ) -{ - ui = new Ui_obsListDialogForm(); - core = StelApp::getInstance().getCore(); - objectMgr = GETSTELMODULE ( StelObjectMgr ); - labelMgr = GETSTELMODULE ( LabelMgr ); - obsListListModel = new QStandardItemModel ( 0,ColumnCount ); - observingListJsonPath = StelFileMgr::findFile ( "data", static_cast( StelFileMgr::Directory|StelFileMgr::Writable ) ) + "/" + QString ( JSON_FILE_NAME ); - createEditDialog_instance = Q_NULLPTR; - defaultListUuid_ = ""; } -ObsListDialog::~ObsListDialog() -{ - delete ui; - delete obsListListModel; +ObsListDialog::~ObsListDialog() { + delete ui; + delete obsListListModel; + ui = Q_NULLPTR; + obsListListModel = Q_NULLPTR; } /* * Initialize the dialog widgets and connect the signals/slots. */ -void ObsListDialog::createDialogContent() -{ - ui->setupUi ( dialog ); - - //Signals and slots - connect ( &StelApp::getInstance(), SIGNAL ( languageChanged() ), this, SLOT ( retranslate() ) ); - connect ( ui->closeStelWindow, SIGNAL ( clicked() ), this, SLOT ( close() ) ); - - connect ( ui->obsListNewListButton, SIGNAL ( clicked() ), this, SLOT ( obsListNewListButtonPressed() ) ); - connect ( ui->obsListEditListButton, SIGNAL ( clicked() ), this, SLOT ( obsListEditButtonPressed() ) ); - connect ( ui->obsListClearHighlightButton, SIGNAL ( clicked() ), this, SLOT ( obsListClearHighLightButtonPressed() ) ); - connect ( ui->obsListHighlightAllButton, SIGNAL ( clicked() ), this, SLOT ( obsListHighLightAllButtonPressed() ) ); - connect ( ui->obsListExitButton, SIGNAL ( clicked() ), this, SLOT ( obsListExitButtonPressed() ) ); - connect ( ui->obsListDeleteButton, SIGNAL ( clicked() ), this, SLOT ( obsListDeleteButtonPressed() ) ); - connect ( ui->obsListTreeView, SIGNAL ( doubleClicked ( QModelIndex ) ), this, SLOT ( selectAndGoToObject ( QModelIndex ) ) ); - - //BookmarksListCombo - connect ( ui->obsListComboBox, SIGNAL ( activated ( int ) ), this, SLOT ( loadSelectedObservingList ( int ) ) ); - - //Initializing the list of observing list - obsListListModel->setColumnCount ( ColumnCount ); - setObservingListHeaderNames(); - - ui->obsListTreeView->setModel ( obsListListModel ); - ui->obsListTreeView->header()->setSectionsMovable ( false ); - ui->obsListTreeView->header()->setSectionResizeMode ( ColumnName, QHeaderView::ResizeToContents ); - ui->obsListTreeView->header()->setSectionResizeMode ( ColumnType, QHeaderView::ResizeToContents ); - ui->obsListTreeView->header()->setSectionResizeMode ( ColumnRa, QHeaderView::ResizeToContents ); - ui->obsListTreeView->header()->setSectionResizeMode ( ColumnDec, QHeaderView::ResizeToContents ); - ui->obsListTreeView->header()->setSectionResizeMode ( ColumnConstellation, QHeaderView::ResizeToContents ); - ui->obsListTreeView->header()->setSectionResizeMode ( ColumnMagnitude, QHeaderView::ResizeToContents ); - ui->obsListTreeView->header()->setStretchLastSection ( true ); - ui->obsListTreeView->hideColumn ( ColumnUUID ); - ui->obsListTreeView->hideColumn ( ColumnNameI18n ); - ui->obsListTreeView->hideColumn ( ColumnJD ); - ui->obsListTreeView->hideColumn ( ColumnLocation ); - //Enable the sort for columns - ui->obsListTreeView->setSortingEnabled ( true ); - - //First item of the combobox is an empty headerStrings - ui->obsListComboBox->addItem ( "" ); - //By default buttons are disable - ui->obsListEditListButton->setEnabled ( false ); - ui->obsListHighlightAllButton->setEnabled ( false ); - ui->obsListClearHighlightButton->setEnabled ( false ); - ui->obsListDeleteButton->setEnabled(false); - - QFile jsonFile ( observingListJsonPath ); - if ( jsonFile.exists() ) - { - loadListsName(); - loadDefaultList(); - } +void ObsListDialog::createDialogContent() { + ui->setupUi(dialog); + + //Signals and slots + connect(&StelApp::getInstance(), SIGNAL (languageChanged()), this, SLOT (retranslate())); + + connect(ui->obsListNewListButton, SIGNAL (clicked()), this, SLOT (obsListNewListButtonPressed())); + connect(ui->obsListEditListButton, SIGNAL (clicked()), this, SLOT (obsListEditButtonPressed())); + connect(ui->obsListClearHighlightButton, SIGNAL (clicked()), this, SLOT (obsListClearHighLightButtonPressed())); + connect(ui->obsListHighlightAllButton, SIGNAL (clicked()), this, SLOT (obsListHighLightAllButtonPressed())); + connect(ui->obsListExitButton, SIGNAL (clicked()), this, SLOT (obsListExitButtonPressed())); + connect(ui->obsListDeleteButton, SIGNAL (clicked()), this, SLOT (obsListDeleteButtonPressed())); + connect(ui->obsListTreeView, SIGNAL (doubleClicked(QModelIndex)), this, SLOT (selectAndGoToObject(QModelIndex))); + + //obsListCombo settings + connect(ui->obsListComboBox, SIGNAL (activated(int)), this, SLOT (loadSelectedObservingList(int))); + + //Initialize the list of observing lists + obsListListModel->setColumnCount(ColumnCount); + setObservingListHeaderNames(); + + ui->obsListTreeView->setModel(obsListListModel); + ui->obsListTreeView->header()->setSectionsMovable(false); + ui->obsListTreeView->header()->setSectionResizeMode(ColumnName, QHeaderView::ResizeToContents); + ui->obsListTreeView->header()->setSectionResizeMode(ColumnNameI18n, QHeaderView::ResizeToContents); + ui->obsListTreeView->header()->setSectionResizeMode(ColumnType, QHeaderView::ResizeToContents); + ui->obsListTreeView->header()->setSectionResizeMode(ColumnRa, QHeaderView::ResizeToContents); + ui->obsListTreeView->header()->setSectionResizeMode(ColumnDec, QHeaderView::ResizeToContents); + ui->obsListTreeView->header()->setSectionResizeMode(ColumnConstellation, QHeaderView::ResizeToContents); + ui->obsListTreeView->header()->setSectionResizeMode(ColumnMagnitude, QHeaderView::ResizeToContents); + ui->obsListTreeView->header()->setSectionResizeMode(ColumnDate, QHeaderView::ResizeToContents); + ui->obsListTreeView->header()->setSectionResizeMode(ColumnLocation, QHeaderView::ResizeToContents); + ui->obsListTreeView->header()->setStretchLastSection(true); + ui->obsListTreeView->hideColumn(ColumnUUID); + + //Enable the sort for columns + ui->obsListTreeView->setSortingEnabled(true); + + //By default buttons are disable + ui->obsListEditListButton->setEnabled(false); + ui->obsListHighlightAllButton->setEnabled(false); + ui->obsListClearHighlightButton->setEnabled(false); + ui->obsListDeleteButton->setEnabled(false); + + ui->obsListJdCheckBox->setChecked(false); + ui->obsListLocationCheckBox->setChecked(false); + + // We hide the closeStelWindow to have only two possibilities to close the dialog: + // Exit + ui->closeStelWindow->setHidden(true); + + + // For no regression we must take into account the legacy bookmarks file + QFile jsonBookmarksFile(bookmarksJsonPath); + if (jsonBookmarksFile.exists()) { + loadBookmarksInObservingList(); + } + + QFile jsonFile(observingListJsonPath); + if (jsonFile.exists()) { + loadListsNameFromJsonFile(); + defaultListOlud_ = extractDefaultListOludFromJsonFile(); + loadDefaultList(); + } + } /* * Retranslate dialog */ -void ObsListDialog::retranslate() -{ - if ( dialog ) - { - ui->retranslateUi ( dialog ); - setObservingListHeaderNames(); - } +void ObsListDialog::retranslate() { + if (dialog != nullptr) { + ui->retranslateUi(dialog); + setObservingListHeaderNames(); + } } /* * Style changed */ -void ObsListDialog::styleChanged() -{ - // Nothing for now +void ObsListDialog::styleChanged() { + // Nothing for now } /* * Set the header for the observing list table * (obsListTreeVView) */ -void ObsListDialog::setObservingListHeaderNames() -{ - const QStringList headerStrings = { - "UUID", // Hided column - q_( "Object name" ), - q_( "Localized Object Name" ), // Hided column - q_( "Type" ), - q_( "Right ascension" ), - q_( "Declination" ), - q_( "Magnitude" ), - q_( "Constellation" ), - q_( "Date" ), // Hided column - q_( "Location" ) // Hided column - }; - - obsListListModel->setHorizontalHeaderLabels ( headerStrings ); +void ObsListDialog::setObservingListHeaderNames() { + const QStringList headerStrings = { + "UUID", // Hidden column + q_ ("Object designation"), + q_ ("Object name"), + q_ ("Type"), + q_ ("Right ascension"), + q_ ("Declination"), + q_ ("Magnitude"), + q_ ("Constellation"), + q_ ("Date"), + q_ ("Location") + }; + + obsListListModel->setHorizontalHeaderLabels(headerStrings); } /* * Add row in the obsListListModel */ -void ObsListDialog::addModelRow ( int number, QString uuid, QString name, QString nameI18n, QString type, QString ra, QString dec, QString magnitude, QString constellation ) -{ - QStandardItem* item = Q_NULLPTR; - - item = new QStandardItem ( uuid ); - item->setEditable ( false ); - obsListListModel->setItem ( number, ColumnUUID, item ); - - item = new QStandardItem ( name ); - item->setEditable ( false ); - obsListListModel->setItem ( number, ColumnName, item ); - - item = new QStandardItem ( nameI18n ); - item->setEditable ( false ); - obsListListModel->setItem ( number, ColumnNameI18n, item ); - - item = new QStandardItem ( type ); - item->setEditable ( false ); - obsListListModel->setItem ( number, ColumnType, item ); - - item = new QStandardItem ( ra ); - item->setEditable ( false ); - obsListListModel->setItem ( number, ColumnRa, item ); - - item = new QStandardItem ( dec ); - item->setEditable ( false ); - obsListListModel->setItem ( number, ColumnDec, item ); - - item = new QStandardItem ( magnitude ); - item->setEditable ( false ); - obsListListModel->setItem ( number, ColumnMagnitude, item ); - - item = new QStandardItem ( constellation ); - item->setEditable ( false ); - obsListListModel->setItem ( number, ColumnConstellation, item ); - - for ( int i = 0; i < ColumnCount; ++i ) { - ui->obsListTreeView->resizeColumnToContents ( i ); - } +void ObsListDialog::addModelRow(int number, const QString &olud, const QString &name, const QString &nameI18n, + const QString &type, const QString &ra, + const QString &dec, const QString &magnitude, const QString &constellation, + const QString &date, const QString &location) { + QStandardItem *item = Q_NULLPTR; + + item = new QStandardItem(olud); + item->setEditable(false); + obsListListModel->setItem(number, ColumnUUID, item); + + item = new QStandardItem(name); + item->setEditable(false); + obsListListModel->setItem(number, ColumnName, item); + + item = new QStandardItem(nameI18n); + item->setEditable(false); + obsListListModel->setItem(number, ColumnNameI18n, item); + + item = new QStandardItem(type); + item->setEditable(false); + obsListListModel->setItem(number, ColumnType, item); + + item = new QStandardItem(ra); + item->setEditable(false); + obsListListModel->setItem(number, ColumnRa, item); + + item = new QStandardItem(dec); + item->setEditable(false); + obsListListModel->setItem(number, ColumnDec, item); + + item = new QStandardItem(magnitude); + item->setEditable(false); + obsListListModel->setItem(number, ColumnMagnitude, item); + + item = new QStandardItem(constellation); + item->setEditable(false); + obsListListModel->setItem(number, ColumnConstellation, item); + + item = new QStandardItem(date); + item->setEditable(false); + obsListListModel->setItem(number, ColumnDate, item); + + item = new QStandardItem(location); + item->setEditable(false); + obsListListModel->setItem(number, ColumnLocation, item); + + for (int i = 0; i < ColumnCount; ++i) { + ui->obsListTreeView->resizeColumnToContents(i); + } } /* * Slot for button obsListHighLightAllButton */ -void ObsListDialog::obsListHighLightAllButtonPressed() -{ - QList highlights; - highlights.clear(); - obsListClearHighLightButtonPressed(); // Enable fool protection - int fontSize = StelApp::getInstance().getScreenFontSize(); - HighlightMgr* hlMgr = GETSTELMODULE ( HighlightMgr ); - QString color = hlMgr->getColor().toHtmlColor(); - float distance = hlMgr->getMarkersSize(); - - for ( const auto &item : qAsConst(observingListItemCollection) ) - { - QString name = item.name; - QString raStr = item.ra.trimmed(); - QString decStr = item.dec.trimmed(); - - Vec3d pos; - bool status = false; - if ( !raStr.isEmpty() && !decStr.isEmpty() ) - { - StelUtils::spheToRect ( StelUtils::getDecAngle ( raStr ), StelUtils::getDecAngle ( decStr ), pos ); - status = true; - } - else - { - status = objectMgr->findAndSelect ( name ); - const QList& selected = objectMgr->getSelectedObject(); - if ( !selected.isEmpty() ) - pos = selected[0]->getJ2000EquatorialPos ( core ); - } - - if ( status ) - highlights.append ( pos ); - - objectMgr->unSelect(); - // Add labels for named highlights (name in top right corner) - highlightLabelIDs.append ( labelMgr->labelObject ( name, name, true, fontSize, color, "NE", distance ) ); - } - - hlMgr->fillHighlightList ( highlights ); +void ObsListDialog::obsListHighLightAllButtonPressed() { + QList highlights; + highlights.clear(); + obsListClearHighLightButtonPressed(); // Enable fool protection + int fontSize = StelApp::getInstance().getScreenFontSize(); + auto *hlMgr = GETSTELMODULE (HighlightMgr); + QString color = hlMgr->getColor().toHtmlColor(); + float distance = hlMgr->getMarkersSize(); + + for (const auto &item: observingListItemCollection) { + QString name = item.name; + QString raStr = item.ra.trimmed(); + QString decStr = item.dec.trimmed(); + + Vec3d pos; + bool status; + if (!raStr.isEmpty() && !decStr.isEmpty()) { + StelUtils::spheToRect(StelUtils::getDecAngle(raStr), StelUtils::getDecAngle(decStr), pos); + status = true; + } else { + status = objectMgr->findAndSelect(name); + const QList &selected = objectMgr->getSelectedObject(); + if (!selected.isEmpty()) { + pos = selected[0]->getJ2000EquatorialPos(core); + } + } + + if (status) { + highlights.append(pos); + } + + objectMgr->unSelect(); + // Add labels for named highlights (name in top right corner) + highlightLabelIDs.append(labelMgr->labelObject(name, name, true, fontSize, color, "NE", distance)); + } + + hlMgr->fillHighlightList(highlights); } /* * Slot for button obsListClearHighLightButton */ -void ObsListDialog::obsListClearHighLightButtonPressed() -{ - objectMgr->unSelect(); - GETSTELMODULE ( HighlightMgr )->cleanHighlightList(); - // Clear labels - for ( auto l : qAsConst(highlightLabelIDs) ) - labelMgr->deleteLabel ( l ); - - highlightLabelIDs.clear(); +void ObsListDialog::obsListClearHighLightButtonPressed() { + clearHighlight(); } /* - * Slot for button obsListNewListButton + * Clear highlight */ -void ObsListDialog::obsListNewListButtonPressed() -{ - std::string listUuid; - invokeObsListCreateEditDialog ( listUuid ); +void ObsListDialog::clearHighlight() { + objectMgr->unSelect(); + GETSTELMODULE (HighlightMgr)->cleanHighlightList(); + // Clear labels + for (auto l: highlightLabelIDs) { + labelMgr->deleteLabel(l); + } + highlightLabelIDs.clear(); } +/* + * Slot for button obsListNewListButton +*/ +void ObsListDialog::obsListNewListButtonPressed() { + string olud = string(); + invokeObsListCreateEditDialog(olud); +} /* * Slot for button obsListEditButton */ -void ObsListDialog::obsListEditButtonPressed() -{ - if ( !selectedObservingListUuid.empty() ) - invokeObsListCreateEditDialog ( selectedObservingListUuid ); - else - qWarning() << "The selected observing list uuid is empty"; +void ObsListDialog::obsListEditButtonPressed() { + if (!selectedObservingListUuid.empty()) { + invokeObsListCreateEditDialog(selectedObservingListUuid); + } else { + qWarning() << "The selected observing list olud is empty"; + } } /** * Open the observing list create/edit dialog */ -void ObsListDialog::invokeObsListCreateEditDialog ( std::string listUuid ) -{ - createEditDialog_instance = ObsListCreateEditDialog::Instance ( listUuid ); - connect ( createEditDialog_instance, SIGNAL ( exitButtonClicked() ), this, SLOT ( obsListCreateEditDialogClosed() ) ); - createEditDialog_instance->setVisible ( true ); +void ObsListDialog::invokeObsListCreateEditDialog(string listOlud) { + createEditDialog_instance = ObsListCreateEditDialog::Instance(std::move(listOlud)); + connect(createEditDialog_instance, SIGNAL (exitButtonClicked()), this, SLOT (obsListCreateEditDialogClosed())); + createEditDialog_instance->setListName(listName_); + createEditDialog_instance->setVisible(true); +} + +/* + * Load the lists names from Json file + * to populate the combo box and get the default list uuid +*/ +void ObsListDialog::loadListsNameFromJsonFile() { + QVariantMap map; + QFile jsonFile(observingListJsonPath); + if (!jsonFile.open(QIODevice::ReadOnly)) { + qWarning() << "[ObservingList] cannot open" << QDir::toNativeSeparators(JSON_FILE_NAME); + + } else { + try { + map = StelJsonParser::parse(jsonFile.readAll()).toMap(); + jsonFile.close(); + + // init combo box + ui->obsListComboBox->clear(); + + QVariantMap observingListsMap = map.value(QString(KEY_OBSERVING_LISTS)).toMap(); + populateListNameInComboBox(observingListsMap); + populateDataInComboBox(observingListsMap, defaultListOlud_); + + } catch (std::runtime_error &e) { + qWarning() << "[ObservingList] File format is wrong! Error: " << e.what(); + return; + } + } +} + + +/* + * Populate the list names into combo box +*/ +void ObsListDialog::populateListNameInComboBox(QVariantMap map) { + QMap::iterator i; + ui->obsListComboBox->clear(); + listNamesModel.clear(); + listName_.clear(); + for (i = map.begin(); i != map.end(); ++i) { + if (i.value().canConvert()) { + QVariant var = i.value(); + auto data = var.value(); + auto listName = data.value(KEY_NAME).value(); + listNamesModel.append(listName); + listName_.append(listName); + } + } + listNamesModel.sort(Qt::CaseInsensitive); + ui->obsListComboBox->addItems(listNamesModel); } /* - * Load the lists names for populate the combo box and get the default list uuid + * Populate data into combo box */ -void ObsListDialog::loadListsName() -{ - QVariantMap map; - QFile jsonFile ( observingListJsonPath ); - if ( !jsonFile.open ( QIODevice::ReadOnly ) ) - qWarning() << "[ObservingList] cannot open" << QDir::toNativeSeparators ( JSON_FILE_NAME ); - else - { - try { - map = StelJsonParser::parse ( jsonFile.readAll() ).toMap(); - jsonFile.close(); - - // init combo box - ui->obsListComboBox->clear(); - ui->obsListComboBox->addItem ( "" ); - - // Get the default list uuid - QString defaultListUuid = map.value ( KEY_DEFAULT_LIST_UUID ).toString(); - - QVariantMap observingListsMap = map.value ( QString ( KEY_OBSERVING_LISTS ) ).toMap(); - - QMap::iterator i; - for ( i = observingListsMap.begin(); i != observingListsMap.end(); ++i ) - { - QString listUuid = i.key(); - - if ( i.value().canConvert() ) - { - QVariant var = i.value(); - QVariantMap data = var.value(); - QString listName = data.value ( KEY_NAME ).value(); - ui->obsListComboBox->addItem ( listName, listUuid ); - if ( defaultListUuid == listUuid ) - defaultListUuid_ = defaultListUuid; - } - } - } - catch ( std::runtime_error &e ) - { - qWarning() << "[ObservingList] File format is wrong! Error: " << e.what(); - return; - } - } +void ObsListDialog::populateDataInComboBox(QVariantMap map, const QString &defaultListOlud) { + QMap::iterator i; + for (i = map.begin(); i != map.end(); ++i) { + const QString &listUuid = i.key(); + if (i.value().canConvert()) { + QVariant var = i.value(); + auto data = var.value(); + auto listName = data.value(KEY_NAME).value(); + + foreach (QString str, listNamesModel) { + if (QString::compare(str, listName) == 0) { + int index = listNamesModel.indexOf(listName, 0); + ui->obsListComboBox->setItemData(index, listUuid); + break; + } + } + } + if (defaultListOlud == listUuid) { + defaultListOlud_ = defaultListOlud; + } + } } + /* * Load the default list */ -void ObsListDialog::loadDefaultList() -{ - if ( defaultListUuid_ != "" ) - { - int index = ui->obsListComboBox->findData ( defaultListUuid_ ); - if ( index != -1 ) - { - ui->obsListComboBox->setCurrentIndex ( index ); - ui->obsListEditListButton->setEnabled ( true ); - selectedObservingListUuid = defaultListUuid_.toStdString(); - loadObservingList ( defaultListUuid_ ); - } - } +void ObsListDialog::loadDefaultList() { + + if (defaultListOlud_ != "") { + int index = ui->obsListComboBox->findData(defaultListOlud_); + if (index != -1) { + ui->obsListComboBox->setCurrentIndex(index); + ui->obsListEditListButton->setEnabled(true); + selectedObservingListUuid = defaultListOlud_.toStdString(); + loadSelectedObservingListFromJsonFile(defaultListOlud_); + } + } else { + // If there is no default list we load the first list in the combo box + const int currentIndex = ui->obsListComboBox->currentIndex(); + if (currentIndex != -1) { + ui->obsListComboBox->setCurrentIndex(0); + loadSelectedObservingList(0); + } + } +} + +/* + * Load the selected observing list in the combo box, from Json file into dialog. +*/ +void ObsListDialog::loadSelectedObservingListFromJsonFile(const QString &listOlud) { + QVariantMap map; + QVariantList listOfObjects; + QFile jsonFile(observingListJsonPath); + if (!jsonFile.open(QIODevice::ReadOnly)) { + qWarning() << "[ObservingList] cannot open" << QDir::toNativeSeparators(JSON_FILE_NAME); + } else { + try { + + map = StelJsonParser::parse(jsonFile.readAll()).toMap(); + QVariantMap observingListMap = map.value(QString(KEY_OBSERVING_LISTS)).toMap().value(listOlud).toMap(); + + // Check or not default list checkbox information + if (listOlud.compare(defaultListOlud_, Qt::CaseSensitive) == 0) { + ui->obsListIsDefaultListCheckBox->setChecked(true); + } else { + ui->obsListIsDefaultListCheckBox->setChecked(false); + } + + // Landscape + QString landscapeId = observingListMap.value(KEY_LANDSCAPE_ID).toString(); + if (!landscapeId.isEmpty()) { + landscapeMgr->setCurrentLandscapeID(landscapeId); + } + + listOfObjects = loadListFromJson(map, listOlud); + + ui->obsListDeleteButton->setEnabled(true); + + if (!listOfObjects.isEmpty()) { + ui->obsListHighlightAllButton->setEnabled(true); + ui->obsListClearHighlightButton->setEnabled(true); + //ui->obsListDeleteButton->setEnabled(true); + + for (const QVariant &object: listOfObjects) { + QVariantMap objectMap; + if (object.canConvert()) { + objectMap = object.value(); + auto objectName = objectMap.value(QString(KEY_DESIGNATION)).value(); + + int lastRow = obsListListModel->rowCount(); + QString objectOlud = QUuid::createUuid().toString(); + auto objectNameI18n = objectMap.value(QString(KEY_NAME_I18N)).value(); + double fov = -1.0; + + // Type + auto objectType = objectMap.value(QString(KEY_OBJECTS_TYPE)).value(); + + // RA & DEC + auto objectRaStr = objectMap.value(QString(KEY_RA)).value(); + auto objectDecStr = objectMap.value(QString(KEY_DEC)).value(); + + if (objectMgr->findAndSelect(objectName) && !objectMgr->getSelectedObject().isEmpty()) { + const QList &selectedObject = objectMgr->getSelectedObject(); + float ra = 0.0; + float dec = 0.0; + StelUtils::rectToSphe(&ra, &dec, selectedObject[0]->getJ2000EquatorialPos(core)); + + if (objectRaStr.isEmpty()) { + objectRaStr = StelUtils::radToHmsStr(ra, false).trimmed(); + } + + if (objectDecStr.isEmpty()) { + objectDecStr = StelUtils::radToDmsStr(dec, false).trimmed(); + } + } else { + qWarning() << "[ObservingList] object: " << objectName << " not found or empty !"; + } + + // Magnitude + auto objectMagnitudeStr = objectMap.value(QString(KEY_MAGNITUDE)).value(); + + // Constellation + auto objectConstellation = objectMap.value(QString(KEY_CONSTELLATION)).value(); + + // Julian Day / Date + auto JDs = objectMap.value(QString(KEY_JD)).value(); + QString date = ""; + if (JDs.isEmpty() || QString::compare(JDs, "0", Qt::CaseSensitive) == 0) { + double JD = core->getJD(); + JDs = QString::number(JD, 'f', 6); + } else { + double JD = JDs.toDouble(); + date = StelUtils::julianDayToISO8601String(JD + core->getUTCOffset(JD) / 24.).replace( + "T", " "); + } + + + // Location + auto Location = objectMap.value(QString(KEY_LOCATION)).value(); + if (Location.isEmpty()) { + StelLocation loc = core->getCurrentLocation(); + if (loc.name.isEmpty()) { + Location = q_("Location not found"); + } else { + Location = QString("%1, %2").arg(loc.name).arg(loc.region); + } + } + + // Fov + auto fovStr = objectMap.value(QString(KEY_FOV)).value(); + if (!fovStr.isEmpty()) { + fov = fovStr.toDouble(); + } + + // Visible flag + bool visibleFlag = objectMap.value(QString(KEY_IS_VISIBLE_MARKER)).value(); + + addModelRow(lastRow, objectOlud, objectName, objectNameI18n, objectType, objectRaStr, + objectDecStr, objectMagnitudeStr, objectConstellation, date, Location); + + observingListItem item; + item.name = objectName; + item.nameI18n = objectNameI18n; + if (!objectType.isEmpty()) { + item.type = objectType; + } + if (!objectRaStr.isEmpty()) { + item.ra = objectRaStr; + } + if (!objectDecStr.isEmpty()) { + item.dec = objectDecStr; + } + if (!objectMagnitudeStr.isEmpty()) { + item.magnitude = objectMagnitudeStr; + } + if (!objectConstellation.isEmpty()) { + item.constellation = objectConstellation; + } + if (!JDs.isEmpty()) { + item.jd = JDs.toDouble(); + } + if (!Location.isEmpty()) { + item.location = Location; + } + if (!visibleFlag) { + item.isVisibleMarker = visibleFlag; + } + if (fov > 0.0) { + item.fov = fov; + } + + observingListItemCollection.insert(objectOlud, item); + + } else { + qCritical() << "[ObservingList] conversion error"; + return; + } + } + } else { + ui->obsListHighlightAllButton->setEnabled(false); + ui->obsListClearHighlightButton->setEnabled(false); + //ui->obsListDeleteButton->setEnabled(false); + } + + objectMgr->unSelect(); + // Sorting for the objects list. + QString sortingBy = observingListMap.value(KEY_SORTING).toString(); + if (!sortingBy.isEmpty()) { + sortObsListTreeViewByColumnName(sortingBy); + } + jsonFile.close(); + } catch (std::runtime_error &e) { + qWarning() << "[ObservingList] Load selected observing list: File format is wrong! Error: " << e.what(); + jsonFile.close(); + return; + } + } +} + +/* + * Load the list from JSON file QVariantMap map +*/ +auto ObsListDialog::loadListFromJson(const QVariantMap &map, const QString& listOlud) -> QVariantList { + + observingListItemCollection.clear(); + QVariantMap observingListMap = map.value(QString(KEY_OBSERVING_LISTS)).toMap().value(listOlud).toMap(); + QVariantList listOfObjects; + + auto listDescription = observingListMap.value(QString(KEY_DESCRIPTION)).value(); + ui->obsListDescriptionTextEdit->setPlainText(listDescription); + + // Displaying the creation date + auto listCreationDate = observingListMap.value(QString(KEY_CREATION_DATE)).value(); + ui->obsListCreationDateLineEdit->setText(listCreationDate); + + if (observingListMap.value(QString(KEY_OBJECTS)).canConvert()) { + QVariant data = observingListMap.value(QString(KEY_OBJECTS)); + listOfObjects = data.value(); + } else { + qCritical() << "[ObservingList] conversion error"; + } + + // Clear model + obsListListModel->removeRows(0, obsListListModel->rowCount()); + return listOfObjects; +} + + +/* + * Load the bookmarks of bookmarks.json file into observing lists file + * For no regression with must take into account the legacy bookmarks.json file +*/ +void ObsListDialog::loadBookmarksInObservingList() { + QHash bookmarksCollection; + QVariantMap map; + + QFile jsonFile(bookmarksJsonPath); + if (!jsonFile.open(QIODevice::ReadOnly)) { + qWarning() << "[ObservingList] cannot open" << QDir::toNativeSeparators(bookmarksJsonPath); + } else { + + try { + + map = StelJsonParser::parse(jsonFile.readAll()).toMap(); + jsonFile.close(); + QVariantMap bookmarksMap = map.value(KEY_BOOKMARKS).toMap(); + + for (const auto &bookmarkKey: bookmarksMap.keys()) { + + QVariantMap bookmarkData = bookmarksMap.value(bookmarkKey).toMap(); + observingListItem item; + util.initItem(item); + + // Name + item.name = bookmarkData.value(KEY_NAME).toString(); + + // We need to select the object to add additional information that is not in the Bookmark file + if (objectMgr->findAndSelect(item.name) && !objectMgr->getSelectedObject().isEmpty()) { + const QList &selectedObject = objectMgr->getSelectedObject(); + + // Ra & Dec - ra and dec are not empty in case of Custom Object + QString raStr = bookmarkData.value(KEY_RA).toString(); + QString decStr = bookmarkData.value(KEY_DEC).toString(); + if (raStr.isEmpty() || decStr.isEmpty()) { + float ra = 0.0; + float dec = 0.0; + StelUtils::rectToSphe(&ra, &dec, selectedObject[0]->getJ2000EquatorialPos(core)); + raStr = StelUtils::radToHmsStr(ra, false).trimmed(); + decStr = StelUtils::radToDmsStr(dec, false).trimmed(); + } + + if (!raStr.isEmpty()) { + item.ra = raStr; + } else { + item.ra = ""; + } + + if (!decStr.isEmpty()) { + item.dec = decStr; + } else { + item.dec = ""; + } + + // NameI18n + QString nameI18n = bookmarkData.value(KEY_NAME_I18N).toString(); + if (!nameI18n.isEmpty()) { + item.nameI18n = nameI18n; + } else { + item.nameI18n = dash; + } + + // JDs + QString JDs = bookmarkData.value(KEY_JD).toString(); + if (!JDs.isEmpty()) { + item.jd = JDs.toDouble(); + } else { + item.jd = 0.0; + } + + // Location + QString location = bookmarkData.value(KEY_LOCATION).toString(); + if (!location.isEmpty()) { + item.location = location; + } else { + item.location = ""; + } + + // Constallation + QVariantMap objectMap = selectedObject[0]->getInfoMap(core); + QVariant objectConstellationVariant = objectMap["iauConstellation"]; + QString objectConstellation("unknown"); + if (objectConstellationVariant.canConvert()) { + objectConstellation = objectConstellationVariant.value(); + } + if (!objectConstellation.isEmpty()) { + item.constellation = objectConstellation; + } + + // Type + QString type = selectedObject[0]->getType(); + if (!type.isEmpty()) { + item.type = type; + } + + // Object Type + QString objectType = selectedObject[0]->getObjectType(); + if (!objectType.isEmpty()) { + item.objtype = objectType; + } + + // Magnitude + QString objectMagnitudeStr = util.getMagnitue(selectedObject, core); + if (!objectMagnitudeStr.isEmpty()) { + item.magnitude = objectMagnitudeStr; + } + + // Fov + double fov = bookmarkData.value(KEY_FOV).toDouble(); + if (fov > 0.0) { + item.fov = fov; + } + + // Visible marker + item.isVisibleMarker = bookmarkData.value(KEY_IS_VISIBLE_MARKER, false).toBool(); + + bookmarksCollection.insert(bookmarkKey, item); + } + } + + saveBookmarksInObsListJsonFile(bookmarksCollection); + objectMgr->unSelect(); + + } catch (std::runtime_error &e) { + qWarning() << "[ObservingList] Load bookmarks in observing list: File format is wrong! Error: " << e.what(); + return; + } + } } + /* - * Load selected observing list + * Save the bookmarks into observing list file */ -void ObsListDialog::loadObservingList ( QString listUuid ) -{ - QVariantList listOfObjects; - QFile jsonFile ( observingListJsonPath ); - if ( !jsonFile.open ( QIODevice::ReadOnly ) ) - qWarning() << "[ObservingList] cannot open" << QDir::toNativeSeparators ( JSON_FILE_NAME ); - else { - try { - listOfObjects = loadListFromJson(jsonFile, listUuid); - - if ( listOfObjects.size() > 0 ) - { - ui->obsListHighlightAllButton->setEnabled ( true ); - ui->obsListClearHighlightButton->setEnabled ( true ); - ui->obsListDeleteButton->setEnabled(true); - - for ( const QVariant &object: qAsConst(listOfObjects) ) - { - QVariantMap objectMap; - if ( object.canConvert() ) - { - objectMap = object.value(); - QString objectName = objectMap.value ( QString ( KEY_DESIGNATION ) ).value(); - - if ( objectMgr->findAndSelect ( objectName ) ) - { - const QList& selectedObject = objectMgr->getSelectedObject(); - if ( !selectedObject.isEmpty() ) - { - int lastRow = obsListListModel->rowCount(); - QString objectUuid = QUuid::createUuid().toString(); - QString objectNameI18n = selectedObject[0]->getNameI18n(); - QString objectRaStr = "", objectDecStr = ""; - bool visibleFlag = false; - double fov = -1.0; - - QString objectType = selectedObject[0]->getType(); - - double ra, dec; - StelUtils::rectToSphe ( &ra, &dec, selectedObject[0]->getJ2000EquatorialPos ( core ) ); - objectRaStr = StelUtils::radToHmsStr ( ra, false ).trimmed(); - objectDecStr = StelUtils::radToDmsStr ( dec, false ).trimmed(); - if ( objectName.contains ( "marker", Qt::CaseInsensitive ) ) - visibleFlag = true; - - float objectMagnitude = selectedObject[0]->getVMagnitude ( core ); - QString objectMagnitudeStr = QString::number ( objectMagnitude ); - - QVariantMap objectMap = selectedObject[0]->getInfoMap ( core ); - QVariant objectConstellationVariant = objectMap["iauConstellation"]; - QString objectConstellation ( "unknown" ); - if ( objectConstellationVariant.canConvert() ) - objectConstellation = objectConstellationVariant.value(); - - QString JDs = ""; - double JD = core->getJD(); - - JDs = StelUtils::julianDayToISO8601String ( JD + core->getUTCOffset ( JD ) /24. ).replace ( "T", " " ); - - QString Location = ""; - StelLocation loc = core->getCurrentLocation(); - if ( loc.name.isEmpty() ) - Location = QString ( "%1, %2" ).arg ( loc.latitude ).arg ( loc.longitude ); - else - Location = QString ( "%1, %2" ).arg ( loc.name, loc.region ); - - addModelRow ( lastRow,objectUuid,objectName, objectNameI18n, objectType, objectRaStr, objectDecStr, objectMagnitudeStr, objectConstellation ); - - observingListItem item; - item.name = objectName; - item.nameI18n = objectNameI18n; - if ( !objectType.isEmpty() ) - item.type = objectType; - - if ( !objectRaStr.isEmpty() ) - item.ra = objectRaStr; - - if ( !objectDecStr.isEmpty() ) - item.dec = objectDecStr; - - if ( !objectMagnitudeStr.isEmpty() ) - item.magnitude = objectMagnitudeStr; - - if ( !objectConstellation.isEmpty() ) - item.constellation = objectConstellation; - - if ( !JDs.isEmpty() ) - item.jd = QString::number ( JD, 'f', 6 ); - - if ( !Location.isEmpty() ) - item.location = Location; - - if ( !visibleFlag ) - item.isVisibleMarker = visibleFlag; - - if ( fov > 0.0 ) - item.fov = fov; - - observingListItemCollection.insert ( objectUuid,item ); - } - else - qWarning() << "[ObservingList] selected object is empty !"; - } - else - qWarning() << "[ObservingList] object: " << objectName << " not found !" ; - } - else - { - qCritical() << "[ObservingList] conversion error"; - return; - } - } - } - else - { - ui->obsListHighlightAllButton->setEnabled ( false ); - ui->obsListClearHighlightButton->setEnabled ( false ); - ui->obsListDeleteButton->setEnabled(false); - } - - objectMgr->unSelect(); - } - catch ( std::runtime_error &e ) - { - qWarning() << "[ObservingList] File format is wrong! Error: " << e.what(); - return; - } - } +void ObsListDialog::saveBookmarksInObsListJsonFile(const QHash &bookmarksCollection) { + + QFile jsonFile(observingListJsonPath); + if (!jsonFile.open(QIODevice::ReadWrite | QIODevice::Text)) { + qWarning() << "[ObservingList] bookmarks list can not be saved. A file can not be open for reading and writing:" + << QDir::toNativeSeparators(observingListJsonPath); + return; + } + + try { + + QVariantMap mapFromJsonFile; + QVariantMap allListsMap; + QVariantMap observingListDataList; + if (jsonFile.size() > 0) { + mapFromJsonFile = StelJsonParser::parse(jsonFile.readAll()).toMap(); + allListsMap = mapFromJsonFile.value(QString(KEY_OBSERVING_LISTS)).toMap(); + } + + QString defaultListValue = mapFromJsonFile.value(QString(KEY_DEFAULT_LIST_OLUD)).toString(); + if (defaultListValue.isEmpty()) { + mapFromJsonFile.insert(KEY_DEFAULT_LIST_OLUD, ""); + } + + if (checkIfBookmarksListExists(allListsMap)) { + //the bookmarks file is already loaded + return; + } + + // Description + QString description = QString(BOOKMARKS_LIST_DESCRIPTION); + observingListDataList.insert(QString(KEY_DESCRIPTION), description); + + // Creation date + double JD = core->getJD(); + QString listCreationDate = StelUtils::julianDayToISO8601String(JD + core->getUTCOffset(JD) / 24.).replace("T", + " "); + observingListDataList.insert(QString(KEY_CREATION_DATE), listCreationDate); + + // Landscape + QString landscapeId = landscapeMgr->getCurrentLandscapeID(); + observingListDataList.insert(QString(KEY_LANDSCAPE_ID), landscapeId); + + // Name of the liste + observingListDataList.insert(QString(KEY_NAME), BOOKMARKS_LIST_NAME); + + // List of objects + QVariantList listOfObjects; + QHashIterator i(bookmarksCollection); + while (i.hasNext()) { + i.next(); + + observingListItem item = i.value(); + QVariantMap obl; + + // Designation (name) + obl.insert(QString(KEY_DESIGNATION), item.name); + + // NameI18 + obl.insert(QString(KEY_NAME_I18N), item.nameI18n); + + // Fov + obl.insert(QString(KEY_FOV), item.fov); + + // JD + obl.insert(QString(KEY_JD), item.jd); + + // Location + obl.insert(QString(KEY_LOCATION), item.location); + + // Magnitude + obl.insert(QString(KEY_MAGNITUDE), item.magnitude); + + // Constellation + obl.insert(QString(KEY_CONSTELLATION), item.constellation); + + // Ra + obl.insert(QString(KEY_RA), item.ra); + + // Dec + obl.insert(QString(KEY_DEC), item.dec); + + // Type + obl.insert(QString(KEY_TYPE), item.type); + + // Object type + obl.insert(QString(KEY_OBJECTS_TYPE), item.objtype); + + // Visible marker + obl.insert(QString(KEY_IS_VISIBLE_MARKER), item.isVisibleMarker); + + listOfObjects.push_back(obl); + + } + + observingListDataList.insert(QString(KEY_OBJECTS), listOfObjects); + observingListDataList.insert(QString(KEY_SORTING), SORTING_BY_NAME); + + QList keys = bookmarksCollection.keys(); + QString oblListUuid; + if (keys.empty()) { + oblListUuid = QUuid::createUuid().toString(); + } else { + oblListUuid = keys.at(0); + } + + mapFromJsonFile.insert(KEY_VERSION, FILE_VERSION); + mapFromJsonFile.insert(KEY_SHORT_NAME, SHORT_NAME_VALUE); + + allListsMap.insert(oblListUuid, observingListDataList); + mapFromJsonFile.insert(QString(KEY_OBSERVING_LISTS), allListsMap); + + jsonFile.resize(0); + StelJsonParser::write(mapFromJsonFile, &jsonFile); + jsonFile.flush(); + jsonFile.close(); + + } catch (std::runtime_error &e) { + qCritical() << "[ObservingList] File format is wrong! Error: " << e.what(); + return; + } } + /* - * Load the list from JSON file + * Check if bookmarks list already exists in observing list file, + * in fact if the file of bookarks has already be loaded. */ -QVariantList ObsListDialog::loadListFromJson(QFile &jsonFile, QString listUuid) -{ - QVariantMap map; - map = StelJsonParser::parse ( jsonFile.readAll() ).toMap(); - jsonFile.close(); - - observingListItemCollection.clear(); - QVariantMap observingListMap = map.value ( QString ( KEY_OBSERVING_LISTS ) ).toMap().value ( listUuid ).toMap(); - QVariantList listOfObjects; - - QString listDescription = observingListMap.value ( QString ( KEY_DESCRIPTION ) ).value(); - ui->obsListDescriptionTextEdit->setPlainText ( listDescription ); - - // Displaying the creation date - QString JDs = observingListMap.value ( QString ( KEY_JD ) ).value(); - double JD = JDs.toDouble(); - QString listCreationDate = JDs = StelUtils::julianDayToISO8601String ( JD + core->getUTCOffset ( JD ) /24. ).replace ( "T", " " ); - ui->obsListCreationDateLineEdit->setText ( listCreationDate ); - - if ( observingListMap.value ( QString ( KEY_OBJECTS ) ).canConvert() ) - { - QVariant data = observingListMap.value ( QString ( KEY_OBJECTS ) ); - listOfObjects = data.value(); - } - else - qCritical() << "[ObservingList] conversion error"; - - // Clear model - obsListListModel->removeRows ( 0,obsListListModel->rowCount() ); - return listOfObjects; +auto ObsListDialog::checkIfBookmarksListExists(const QVariantMap &allListsMap) -> bool { + + for (const auto& bookmarkKey: allListsMap.keys()) { + + QVariantMap bookmarkData = allListsMap.value(bookmarkKey).toMap(); + QString listName = bookmarkData.value(KEY_NAME).toString(); + + if (QString::compare(QString(BOOKMARKS_LIST_NAME), listName) == 0) { + return true; + } + } + + return false; } /* * Select and go to object */ -void ObsListDialog::selectAndGoToObject ( QModelIndex index ) -{ - QStandardItem * selectedItem = obsListListModel->itemFromIndex ( index ); - int rawNumber = selectedItem->row(); - - QStandardItem * uuidItem = obsListListModel->item ( rawNumber, ColumnUUID ); - QString itemUuid = uuidItem->text(); - observingListItem item = observingListItemCollection.value ( itemUuid ); - - if ( !item.jd.isEmpty() ) - core->setJD ( item.jd.toDouble() ); - - if ( !item.location.isEmpty() ) - { - StelLocationMgr* locationMgr = &StelApp::getInstance().getLocationMgr(); - core->moveObserverTo ( locationMgr->locationForString ( item.location ) ); - } - - StelMovementMgr* mvmgr = GETSTELMODULE ( StelMovementMgr ); - objectMgr->unSelect(); - - bool status = objectMgr->findAndSelect ( item.name ); - float amd = mvmgr->getAutoMoveDuration(); - if ( !item.ra.isEmpty() && !item.dec.isEmpty() && !status ) - { - Vec3d pos; - StelUtils::spheToRect ( StelUtils::getDecAngle ( item.ra.trimmed() ), StelUtils::getDecAngle ( item.dec.trimmed() ), pos ); - if ( item.name.contains ( "marker", Qt::CaseInsensitive ) ) - { - // Add a custom object on the sky - GETSTELMODULE ( CustomObjectMgr )->addCustomObject ( item.name, pos, item.isVisibleMarker ); - status = objectMgr->findAndSelect ( item.name ); - } - else - { - // The unnamed stars - StelObjectP sobj; - const StelProjectorP prj = core->getProjection ( StelCore::FrameJ2000 ); - double fov = 5.0; - if ( item.fov > 0.0 ) - fov = item.fov; - - mvmgr->zoomTo ( fov, 0.0 ); - mvmgr->moveToJ2000 ( pos, mvmgr->mountFrameToJ2000 ( Vec3d ( 0., 0., 1. ) ), 0.0 ); - - QList candidates = GETSTELMODULE ( StarMgr )->searchAround ( pos, 0.5, core ); - if ( candidates.empty() ) - { // The FOV is too big, let's reduce it - mvmgr->zoomTo ( 0.5*fov, 0.0 ); - candidates = GETSTELMODULE ( StarMgr )->searchAround ( pos, 0.5, core ); - } - - double best_object_value = M_PI; - for ( const auto& obj : qAsConst(candidates) ) - { - double distance=pos.angle(obj->getJ2000EquatorialPos(core)); - if ( distance < best_object_value ) - { - best_object_value = distance; - sobj = obj; - } - } - - if ( sobj ) - status = objectMgr->setSelectedObject ( sobj ); - } - } - - if ( status ) - { - const QList newSelected = objectMgr->getSelectedObject(); - if ( !newSelected.empty() ) - { - mvmgr->moveToObject ( newSelected[0], amd ); - mvmgr->setFlagTracking ( true ); - } - } +void ObsListDialog::selectAndGoToObject(QModelIndex index) { + QStandardItem *selectedItem = obsListListModel->itemFromIndex(index); + int rawNumber = selectedItem->row(); + + QStandardItem *uuidItem = obsListListModel->item(rawNumber, ColumnUUID); + QString itemUuid = uuidItem->text(); + observingListItem item = observingListItemCollection.value(itemUuid); + + + // We use jd if the checkbox JD is checked. + if (ui->obsListJdCheckBox->isChecked() && item.jd != 0.0) { + core->setJD(item.jd); + } else { + core->setJD(currentJd); + } + + // We use location if the checkbox Location is checked. + if (ui->obsListLocationCheckBox->isChecked() && !item.location.isEmpty()) { + StelLocationMgr *locationMgr = &StelApp::getInstance().getLocationMgr(); + core->moveObserverTo(locationMgr->locationForString(item.location)); + } + + auto *mvmgr = GETSTELMODULE (StelMovementMgr); + objectMgr->unSelect(); + + bool status = objectMgr->findAndSelect(item.name); + float amd = mvmgr->getAutoMoveDuration(); + if (!item.ra.isEmpty() && !item.dec.isEmpty() && !status) { + Vec3d pos; + StelUtils::spheToRect(StelUtils::getDecAngle(item.ra.trimmed()), StelUtils::getDecAngle(item.dec.trimmed()), + pos); + if (item.name.contains("marker", Qt::CaseInsensitive)) { + // Add a custom object on the sky + GETSTELMODULE (CustomObjectMgr)->addCustomObject(item.name, pos, item.isVisibleMarker); + status = objectMgr->findAndSelect(item.name); + } else { + // The unnamed stars + StelObjectP sobj; + const StelProjectorP prj = core->getProjection(StelCore::FrameJ2000); + double fov = 5.0; + if (item.fov > 0.0) { + fov = item.fov; + } + + mvmgr->zoomTo(fov, 0.0); + mvmgr->moveToJ2000(pos, mvmgr->mountFrameToJ2000(Vec3d(0., 0., 1.)), 0.0); + + QList candidates = GETSTELMODULE (StarMgr)->searchAround(pos, 0.5, core); + if (candidates.empty()) { // The FOV is too big, let's reduce it + mvmgr->zoomTo(0.5 * fov, 0.0); + candidates = GETSTELMODULE (StarMgr)->searchAround(pos, 0.5, core); + } + + Vec3d winpos; + prj->project(pos, winpos); + double xpos = winpos[0]; + double ypos = winpos[1]; + double best_object_value = 1000.F; + for (const auto &obj: candidates) { + prj->project(obj->getJ2000EquatorialPos(core), winpos); + double distance = std::sqrt( + (xpos - winpos[0]) * (xpos - winpos[0]) + (ypos - winpos[1]) * (ypos - winpos[1])); + if (distance < best_object_value) { + best_object_value = distance; + sobj = obj; + } + } + + if (sobj != nullptr) { + status = objectMgr->setSelectedObject(sobj); + } + } + } + + if (status) { + const QList newSelected = objectMgr->getSelectedObject(); + if (!newSelected.empty()) { + mvmgr->moveToObject(newSelected[0], amd); + mvmgr->setFlagTracking(true); + } + } } /* * Method called when a list name is selected in the combobox */ -void ObsListDialog::loadSelectedObservingList ( int selectedIndex ) -{ - if ( selectedIndex > 0 ) - { - ui->obsListEditListButton->setEnabled ( true ); - ui->obsListDeleteButton->setEnabled(true); - QString listUuid = ui->obsListComboBox->itemData ( selectedIndex ).toString(); - selectedObservingListUuid = listUuid.toStdString(); - loadObservingList ( listUuid ); - } - else - { - selectedObservingListUuid = ""; - // Button obsListEditListButton, obsListHighlightAllButton and - // obsListClearHighlightButtonmust be disable if no list is selected - ui->obsListEditListButton->setEnabled ( false ); - ui->obsListHighlightAllButton->setEnabled ( false ); - ui->obsListClearHighlightButton->setEnabled ( false ); - ui->obsListDeleteButton->setEnabled(false); - // Clear list description - ui->obsListDescriptionTextEdit->setPlainText ( "" ); - // Clear model - obsListListModel->removeRows ( 0,obsListListModel->rowCount() ); - qWarning() << "[ObservingList] No list selected !"; - } +void ObsListDialog::loadSelectedObservingList(int selectedIndex) { + ui->obsListEditListButton->setEnabled(true); + ui->obsListDeleteButton->setEnabled(true); + QString listUuid = ui->obsListComboBox->itemData(selectedIndex).toString(); + selectedObservingListUuid = listUuid.toStdString(); + loadSelectedObservingListFromJsonFile(listUuid); } /* * Delete the selected list */ -void ObsListDialog::obsListDeleteButtonPressed() -{ - qDebug() << QString::fromStdString(selectedObservingListUuid); - QVariantMap map; - QFile jsonFile ( observingListJsonPath ); - if ( !jsonFile.open ( QIODevice::ReadWrite ) ) - qWarning() << "[ObservingList] cannot open" << QDir::toNativeSeparators ( JSON_FILE_NAME ); - else - { - try - { - QVariantMap newMap; - QVariantMap newObsListMap; - map = StelJsonParser::parse ( jsonFile.readAll() ).toMap(); - - newMap.insert(QString(KEY_DEFAULT_LIST_UUID), map.value(QString(KEY_DEFAULT_LIST_UUID))); - QVariantMap obsListMap = map.value(QString(KEY_OBSERVING_LISTS)).toMap(); - - QMap::iterator i; - for(i = obsListMap.begin(); i != obsListMap.end(); ++i) - { - if(i.key().compare(QString::fromStdString(selectedObservingListUuid))!=0) - newObsListMap.insert(i.key(),i.value()); - } - - newMap.insert(QString(KEY_OBSERVING_LISTS),newObsListMap); - newMap.insert(QString(KEY_SHORT_NAME), map.value(QString(KEY_SHORT_NAME))); - newMap.insert(QString(KEY_VERSION), map.value(QString(KEY_VERSION))); - objectMgr->unSelect(); - observingListItemCollection.clear(); - // Clear model - obsListListModel->removeRows ( 0,obsListListModel->rowCount() ); - ui->obsListCreationDateLineEdit->setText(""); - int currentIndex = ui->obsListComboBox->currentIndex(); - ui->obsListComboBox->removeItem(currentIndex); - - int index = ui->obsListComboBox->findData(""); - ui->obsListComboBox->setCurrentIndex(index); - ui->obsListDescriptionTextEdit->setPlainText(""); - - jsonFile.resize ( 0 ); - StelJsonParser::write ( newMap, &jsonFile ); - jsonFile.flush(); - jsonFile.close(); - - } - catch ( std::runtime_error &e ) - { - qWarning() << "[ObservingList] File format is wrong! Error: " << e.what(); - return; - } - } +void ObsListDialog::obsListDeleteButtonPressed() { + bool res = askConfirmation(); + + if (res) { + QVariantMap map; + QFile jsonFile(observingListJsonPath); + if (!jsonFile.open(QIODevice::ReadWrite)) { + qWarning() << "[ObservingList] cannot open" << QDir::toNativeSeparators(JSON_FILE_NAME); + } else { + try { + QVariantMap newMap; + QVariantMap newObsListMap; + map = StelJsonParser::parse(jsonFile.readAll()).toMap(); + + newMap.insert(QString(KEY_DEFAULT_LIST_OLUD), map.value(QString(KEY_DEFAULT_LIST_OLUD))); + QVariantMap obsListMap = map.value(QString(KEY_OBSERVING_LISTS)).toMap(); + + QMap::iterator i; + for (i = obsListMap.begin(); i != obsListMap.end(); ++i) { + if (i.key().compare(QString::fromStdString(selectedObservingListUuid)) != 0) { + newObsListMap.insert(i.key(), i.value()); + } + } + + newMap.insert(QString(KEY_OBSERVING_LISTS), newObsListMap); + newMap.insert(QString(KEY_SHORT_NAME), map.value(QString(KEY_SHORT_NAME))); + newMap.insert(QString(KEY_VERSION), map.value(QString(KEY_VERSION))); + objectMgr->unSelect(); + observingListItemCollection.clear(); + + // Clear row in model + obsListListModel->removeRows(0, obsListListModel->rowCount()); + ui->obsListCreationDateLineEdit->setText(""); + ui->obsListDescriptionTextEdit->setPlainText(""); + int currentIndex = ui->obsListComboBox->currentIndex(); + ui->obsListComboBox->removeItem(currentIndex); + + selectedObservingListUuid = ""; + + jsonFile.resize(0); + StelJsonParser::write(newMap, &jsonFile); + jsonFile.flush(); + jsonFile.close(); + + clearHighlight(); + loadListsNameFromJsonFile(); + + if (ui->obsListComboBox->count() > 0) { + ui->obsListComboBox->setCurrentIndex(0); + loadSelectedObservingList(0); + } + + } catch (std::runtime_error &e) { + qWarning() << "[ObservingList] File format is wrong! Error: " << e.what(); + return; + } + } + } + + } /* * Slot for button obsListExitButton */ -void ObsListDialog::obsListExitButtonPressed() -{ - this->close(); +void ObsListDialog::obsListExitButtonPressed() { + this->close(); +} + +/* + * Slot to manage the close of obsListCreateEditDialog. +*/ +void ObsListDialog::obsListCreateEditDialogClosed() { + + // The defaultListOlud may have changed in the create dialog. + // The defaultListOlud needs to be updated. + defaultListOlud_ = extractDefaultListOludFromJsonFile(); + + // We must reload the list name + loadListsNameFromJsonFile(); + int index = 0; + if (!selectedObservingListUuid.empty()) { + index = ui->obsListComboBox->findData(QString::fromStdString(selectedObservingListUuid)); + } + + // Reload of the selected observing list. + if (index != -1) { + ui->obsListComboBox->setCurrentIndex(index); + loadSelectedObservingList(index); + } + + ObsListCreateEditDialog::kill(); + createEditDialog_instance = Q_NULLPTR; } /* - * Slot to manage the close of obsListCreateEditDialog + * Extract the defaultListOlud from the Json file. */ -void ObsListDialog::obsListCreateEditDialogClosed() -{ - // We must reload the list of list name - loadListsName(); - int index = ui->obsListComboBox->findData ( QString::fromStdString ( selectedObservingListUuid ) ) ; - if ( index != -1 ) - { - ui->obsListComboBox->setCurrentIndex ( index ); - loadSelectedObservingList ( index ); - } - - ObsListCreateEditDialog::kill(); - createEditDialog_instance = Q_NULLPTR; +auto ObsListDialog::extractDefaultListOludFromJsonFile() -> QString { + QFile jsonFile(observingListJsonPath); + if (!jsonFile.open(QIODevice::ReadOnly)) { + qWarning() << "[ObservingList] cannot open" << QDir::toNativeSeparators(JSON_FILE_NAME); + } else { + try { + QVariantMap map = StelJsonParser::parse(jsonFile.readAll()).toMap(); + jsonFile.close(); + + // Get the default list uuid + QString defaultListOlud = map.value(KEY_DEFAULT_LIST_OLUD).toString(); + return defaultListOlud; + } catch (std::runtime_error &e) { + qWarning() << "[ObservingList] File format is wrong! Error: ()()" << e.what(); + return ""; + } + } + return {}; } + +/* + * Override of the StelDialog::setVisible((bool) methode + * We need to load the default liste when opening the obsListDialog +*/ +void ObsListDialog::setVisible(bool v) { + if (v) { + StelDialog::setVisible(true); + this->loadDefaultList(); + } else { + StelDialog::setVisible(false); + } +} + +/* + * Sort the obsListTreeView by the column name given in parameter +*/ +void ObsListDialog::sortObsListTreeViewByColumnName(const QString &columnName) { + if (QString::compare(columnName, SORTING_BY_NAME) == 0) { + obsListListModel->sort(COLUMN_NUMBER_NAME, Qt::AscendingOrder); + } else if (QString::compare(columnName, SORTING_BY_NAMEI18N) == 0) { + obsListListModel->sort(COLUMN_NUMBER_NAMEI18N, Qt::AscendingOrder); + } else if (QString::compare(columnName, SORTING_BY_TYPE) == 0) { + obsListListModel->sort(COLUMN_NUMBER_TYPE, Qt::AscendingOrder); + } else if (QString::compare(columnName, SORTING_BY_RA) == 0) { + obsListListModel->sort(COLUMN_NUMBER_RA, Qt::AscendingOrder); + } else if (QString::compare(columnName, SORTING_BY_DEC) == 0) { + obsListListModel->sort(COLUMN_NUMBER_DEC, Qt::AscendingOrder); + } else if (QString::compare(columnName, SORTING_BY_MAGNITUDE) == 0) { + obsListListModel->sort(COLUMN_NUMBER_MAGNITUDE, Qt::AscendingOrder); + } else if (QString::compare(columnName, SORTING_BY_CONSTELLATION) == 0) { + obsListListModel->sort(COLUMN_NUMBER_CONSTELLATION, Qt::AscendingOrder); + } +} + + + + + diff --git a/src/gui/ObsListDialog.hpp b/src/gui/ObsListDialog.hpp index cdb078a97afc5..c312ed2393b68 100644 --- a/src/gui/ObsListDialog.hpp +++ b/src/gui/ObsListDialog.hpp @@ -22,6 +22,7 @@ #include #include +#include #include "StelDialog.hpp" #include "StelCore.hpp" @@ -30,84 +31,131 @@ class Ui_obsListDialogForm; -class ObsListDialog : public StelDialog -{ - Q_OBJECT +class ObsListDialog : public StelDialog { +Q_OBJECT public: - ObsListDialog ( QObject* parent ); - virtual ~ObsListDialog() Q_DECL_OVERRIDE; + explicit ObsListDialog(QObject *parent); - //! Notify that the application style changed - virtual void styleChanged() Q_DECL_OVERRIDE; + ~ObsListDialog() override; + + //! Notify that the application style changed + void styleChanged() override; + + void setVisible(bool v) override; protected: - Ui_obsListDialogForm *ui; - //! Initialize the dialog widgets and connect the signals/slots. - virtual void createDialogContent() Q_DECL_OVERRIDE; + Ui_obsListDialogForm *ui; + + //! Initialize the dialog widgets and connect the signals/slots. + void createDialogContent() override; private: - QStandardItemModel * obsListListModel; - class StelCore* core; - class StelObjectMgr* objectMgr; - class LabelMgr* labelMgr; - std::string selectedObservingListUuid; - QString observingListJsonPath; - QHash observingListItemCollection; - QList highlightLabelIDs; - QString defaultListUuid_; + QStandardItemModel *obsListListModel; + + class StelCore *core; + + class StelObjectMgr *objectMgr; + + class LandscapeMgr *landscapeMgr; + + class LabelMgr *labelMgr; + + std::string selectedObservingListUuid; + QString observingListJsonPath; + QString bookmarksJsonPath; + QHash observingListItemCollection; + QList highlightLabelIDs; + QString defaultListOlud_; + QList listName_; + QStringList listNamesModel; + double currentJd; + ObservingListUtil util; + + //! Set header names for observing list table + void setObservingListHeaderNames(); + + void invokeObsListCreateEditDialog(std::string listOlud); + + ObsListCreateEditDialog *createEditDialog_instance; + + //! Add row in the obsListListModel + //! @param number row number + //! @param olud id of the record + //! @param name name or the designation of the object + //! @param type type of the object + //! @param ra right assencion of the object + //! @param dec declination of the object + //! @param magnitude magnitude of the object + //! @param constellation constellation in which the object is located + void addModelRow(int number, const QString &olud, const QString &name, const QString &nameI18n, const QString &type, + const QString &ra, const QString &dec, const QString &magnitude, const QString &constellation, + const QString &date, const QString &location); + + //! Load the selected observing list from Json file into dialog. + //! @param listOlud the olud (id) of the list + void loadSelectedObservingListFromJsonFile(const QString &listOlud); + + //! Load the lists names from Json file to populate the combo box and get the default list olud + void loadListsNameFromJsonFile(); - //! Set header names for observing list table - void setObservingListHeaderNames(); + //! Load the default list + void loadDefaultList(); - void invokeObsListCreateEditDialog ( std::string listUuid ); + //! Load the bookmarks of bookmarks.json file into observing lists file + void loadBookmarksInObservingList(); - ObsListCreateEditDialog * createEditDialog_instance; + void saveBookmarksInObsListJsonFile(const QHash &bookmarksCollection); - //! Add row in the obsListListModel - //! @param number row number - //! @param uuid id of the record - //! @param name name or the designation of the object - //! @param type type of the object - //! @param ra right ascencion of the object - //! @param dec declination of the object - //! @param magnitude magnitude of the object - //! @param constellation constellation in which the object is located - void addModelRow ( int number, QString uuid, QString name, QString nameI18n, QString type, QString ra, QString dec, QString magnitude, QString constellation ); + static auto checkIfBookmarksListExists(const QVariantMap &allListsMap) -> bool; - //! Load the selected observing list - //! @param listUuid the uuid of the list - void loadObservingList ( QString listUuid ); + //! Load list from JSON file + auto loadListFromJson(const QVariantMap &map, const QString& listOlud) -> QVariantList; - //! Load the lists names for populate the combo box and get the default list uuid - void loadListsName(); + //! Populate list names into combo box + void populateListNameInComboBox(QVariantMap map); - //! Load the default list - void loadDefaultList(); + //! Populate data into combo box + void populateDataInComboBox(QVariantMap map, const QString &defaultListOlud); - //! Load list from JSON file - QVariantList loadListFromJson(QFile &jsonFile, QString listUuid); + //! Sort the obsListTreeView by the column name given in parameter + void sortObsListTreeViewByColumnName(const QString &columnName); + + //! Clear highlights + void clearHighlight(); + + //! get the defaultListOlud from Json file + QString extractDefaultListOludFromJsonFile(); public slots: - virtual void retranslate() Q_DECL_OVERRIDE; + + void retranslate(); private slots: - void obsListHighLightAllButtonPressed(); - void obsListClearHighLightButtonPressed(); - void obsListNewListButtonPressed(); - void obsListEditButtonPressed(); - void obsListCreateEditDialogClosed(); - void obsListExitButtonPressed(); - void obsListDeleteButtonPressed(); - - //! Method called when a list name is selected in the combobox - //! @param selectedIndex the index of the list name in the combo box - void loadSelectedObservingList ( int selectedIndex ); - - //! Select and go to object - //! @param index the QModelIndex of the list - void selectAndGoToObject ( QModelIndex index ); + + void obsListHighLightAllButtonPressed(); + + void obsListClearHighLightButtonPressed(); + + void obsListNewListButtonPressed(); + + void obsListEditButtonPressed(); + + void obsListCreateEditDialogClosed(); + + void obsListExitButtonPressed(); + + void obsListDeleteButtonPressed(); + + + //! Method called when a list name is selected in the combobox + //! @param selectedIndex the index of the list name in the combo box + void loadSelectedObservingList(int selectedIndex); + + //! Select and go to object + //! @param index the QModelIndex of the list + void selectAndGoToObject(QModelIndex index); }; #endif // OBSLISTDIALOG_H diff --git a/src/gui/ObservingListCommon.hpp b/src/gui/ObservingListCommon.hpp index 1d3b85de41a36..24d1e3dd4045f 100644 --- a/src/gui/ObservingListCommon.hpp +++ b/src/gui/ObservingListCommon.hpp @@ -20,58 +20,141 @@ #ifndef OBSERVINGLISTCOMMON_H #define OBSERVINGLISTCOMMON_H -#include +#include "Nebula.hpp" +#include "StelTranslator.hpp" +#include struct observingListItem { QString name; QString nameI18n; QString type; + QString objtype; QString ra; QString dec; QString magnitude; QString constellation; - QString jd; + double jd; QString location; double fov; bool isVisibleMarker; }; -Q_DECLARE_METATYPE ( observingListItem ) + +Q_DECLARE_METATYPE (observingListItem) enum ObsListColumns { - ColumnUUID, //! UUID of object - ColumnName, //! Name or designation of object - ColumnNameI18n, //! Localized name of object - ColumnType, //! Type of the object - ColumnRa, //! Right ascencion of the object - ColumnDec, //! Declination of the object - ColumnMagnitude, //! Magnitude of the object - ColumnConstellation, //! Constellation in which the object is located - ColumnJD, //! Date in julian day - ColumnLocation, //! Location where the object is observed - ColumnCount //! Total number of columns + ColumnUUID, //! UUID of object + ColumnName, //! Name or designation of object + ColumnNameI18n, //! Localized name of object + ColumnType, //! Type of the object + ColumnRa, //! Right ascension of the object + ColumnDec, //! Declination of the object + ColumnMagnitude, //! Magnitude of the object + ColumnConstellation, //! Constellation in which the object is located + ColumnDate, //! Date + ColumnLocation, //! Location where the object is observed + ColumnCount //! Total number of columns }; -static constexpr char const * JSON_FILE_NAME = "observingList.json"; -static constexpr char const * FILE_VERSION = "1.0"; - -static constexpr char const * KEY_DEFAULT_LIST_UUID = "defaultListUuid"; -static constexpr char const * KEY_OBSERVING_LISTS = "observingLists"; -static constexpr char const * KEY_NAME = "name"; -static constexpr char const * KEY_JD = "jd"; -static constexpr char const * KEY_DESCRIPTION = "description"; -static constexpr char const * KEY_OBJECTS = "objects"; -static constexpr char const * KEY_DESIGNATION = "designation"; -static constexpr char const * KEY_SORTING = "sorting"; -static constexpr char const * KEY_LOCATION = "location"; -static constexpr char const * KEY_VERSION = "version"; -static constexpr char const * KEY_SHORT_NAME = "shortName"; - -static constexpr char const * SORTING_BY_NAME = "name"; -static constexpr char const * SORTING_BY_TYPE = "type"; -static constexpr char const * SORTING_BY_RA = "right ascension"; -static constexpr char const * SORTING_BY_DEC = "declination"; -static constexpr char const * SORTING_BY_MAGNITUDE = "magnitude"; -static constexpr char const * SORTING_BY_CONSTELLATION = "constellation"; +static constexpr char const *JSON_FILE_NAME = "observingList.json"; +static constexpr char const *FILE_VERSION = "2.0"; + +static constexpr char const *JSON_BOOKMARKS_FILE_NAME = "bookmarks.json"; +static constexpr char const *BOOKMARKS_LIST_NAME = "bookmarks list"; +static constexpr char const *BOOKMARKS_LIST_DESCRIPTION = "Bookmarks of previous Stellarium version."; +static constexpr char const *SHORT_NAME_VALUE = "Observing list for Stellarium"; + +static constexpr char const *KEY_DEFAULT_LIST_OLUD = "defaultListOlud"; +static constexpr char const *KEY_OBSERVING_LISTS = "observingLists"; +static constexpr char const *KEY_CREATION_DATE = "creation date"; +static constexpr char const *KEY_BOOKMARKS = "bookmarks"; +static constexpr char const *KEY_NAME = "name"; +static constexpr char const *KEY_NAME_I18N = "nameI18n"; +static constexpr char const *KEY_JD = "jd"; +static constexpr char const *KEY_RA = "ra"; +static constexpr char const *KEY_DEC = "dec"; +static constexpr char const *KEY_FOV = "fov"; +static constexpr char const *KEY_DESCRIPTION = "description"; +static constexpr char const *KEY_LANDSCAPE_ID = "landscape id"; +static constexpr char const *KEY_OBJECTS = "objects"; +static constexpr char const *KEY_OBJECTS_TYPE = "objtype"; +static constexpr char const *KEY_TYPE = "type"; +static constexpr char const *KEY_DESIGNATION = "designation"; +static constexpr char const *KEY_SORTING = "sorting"; +static constexpr char const *KEY_LOCATION = "location"; +static constexpr char const *KEY_MAGNITUDE = "magnitude"; +static constexpr char const *KEY_CONSTELLATION = "constellation"; +static constexpr char const *KEY_VERSION = "version"; +static constexpr char const *KEY_SHORT_NAME = "shortName"; +static constexpr char const *KEY_IS_VISIBLE_MARKER = "isVisibleMarker"; + +static constexpr char const *SORTING_BY_NAME = "name"; +static constexpr char const *SORTING_BY_NAMEI18N = "nameI18n"; +static constexpr char const *SORTING_BY_TYPE = "type"; +static constexpr char const *SORTING_BY_RA = "right ascension"; +static constexpr char const *SORTING_BY_DEC = "declination"; +static constexpr char const *SORTING_BY_MAGNITUDE = "magnitude"; +static constexpr char const *SORTING_BY_CONSTELLATION = "constellation"; + +static constexpr int COLUMN_NUMBER_NAME = 1; +static constexpr int COLUMN_NUMBER_NAMEI18N = 2; +static constexpr int COLUMN_NUMBER_TYPE = 3; +static constexpr int COLUMN_NUMBER_RA = 4; +static constexpr int COLUMN_NUMBER_DEC = 5; +static constexpr int COLUMN_NUMBER_MAGNITUDE = 6; +static constexpr int COLUMN_NUMBER_CONSTELLATION = 7; + +static constexpr char const *CUSTOM_OBJECT = "CustomObject"; + +static const QString dash = QChar(0x2014); +class ObservingListUtil { +public: + + // Init item + void initItem(observingListItem &item) { + + item.jd = 0.0; + item.type = ""; + item.objtype = ""; + item.ra = ""; + item.dec = ""; + item.name = ""; + item.nameI18n = ""; + item.location = ""; + item.fov = 0.0; + item.constellation = ""; + item.magnitude = ""; + item.isVisibleMarker = false; + } + + // Get the magnitude form selected object + QString getMagnitue(const QList &selectedObject, StelCore *core) { + + if (core == nullptr) { + return ""; + } + + QString objectMagnitudeStr; + QString type = selectedObject[0]->getType(); + float objectMagnitude = selectedObject[0]->getVMagnitude(core); + if (objectMagnitude > 98.f) { + if (QString::compare(type, "Nebula", Qt::CaseSensitive) == 0) { + auto &r_nebula = dynamic_cast(*selectedObject[0]); + float mB = r_nebula.getBMagnitude(core); + if (mB > 98.f) { + objectMagnitudeStr = dash; + } else { + objectMagnitudeStr = QString::number(mB); + } + } else { + objectMagnitudeStr = dash; + } + } else { + objectMagnitudeStr = QString::number(objectMagnitude, 'f', 2); + } + + return objectMagnitudeStr; + } +}; #endif // OBSERVINGLISTCOMMON_H diff --git a/src/gui/obsListCreateEditDialog.ui b/src/gui/obsListCreateEditDialog.ui index 93b85b6421531..73ec927e81c00 100644 --- a/src/gui/obsListCreateEditDialog.ui +++ b/src/gui/obsListCreateEditDialog.ui @@ -82,7 +82,7 @@ - Observing Lists creation/edition + Observing Lists editor @@ -127,7 +127,37 @@ - + + + + + + Description: + + + + + + + Qt::Horizontal + + + QSizePolicy::Fixed + + + + 62 + 20 + + + + + + + + + + @@ -135,6 +165,9 @@ 0 + + false + QFrame::StyledPanel @@ -143,20 +176,46 @@ - + + + + + Add Ra & Dec coordinates + + + 3 + + + Coordinates (RA and DEC) + + + - + - Set the list as default + Add field of view 3 - Set list as default + Fov + + + + Qt::Horizontal + + + + 40 + 20 + + + + @@ -193,7 +252,7 @@ - Create a new observing list + Export list 3 @@ -206,7 +265,7 @@ - Edit the selected list + Import list 3 @@ -223,7 +282,7 @@ - Save and Close + Save and close @@ -252,54 +311,81 @@ - - + + - + + + QLabel {color : red;} + - Name of the list: + TextLabel + + + + + + -1 + + + + + - + + + Set the list as default + + + 3 + + + Set list as default + + + + + Qt::Horizontal - - QSizePolicy::Fixed - - 39 + 40 20 - + + + Add landscape + + + 3 + + + Landscape + + - - - - -1 - - - - - + + - + - Description: + Name of the list: - + Qt::Horizontal @@ -308,14 +394,14 @@ - 67 + 39 20 - + diff --git a/src/gui/obsListDialog.ui b/src/gui/obsListDialog.ui index 302851f49ac9e..ff16e7f66b5a8 100644 --- a/src/gui/obsListDialog.ui +++ b/src/gui/obsListDialog.ui @@ -2,6 +2,9 @@ obsListDialogForm + + true + 0 @@ -138,7 +141,7 @@ - List(s): + List: @@ -147,43 +150,7 @@ - - - - Observing list description: - - - - - - - -1 - - - - - - - true - - - - 0 - 0 - - - - - 16777215 - 40 - - - - true - - - - + @@ -297,8 +264,82 @@ + + + + Observing list description: + + + + + + + -1 + + + + + + + false + + + + + + + + 35 + 38 + 39 + + + + + + + + + 35 + 38 + 39 + + + + + + + + + 0 + 0 + 0 + + + + + + + + Defined as default list + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + @@ -313,6 +354,53 @@ + + + + + + true + + + + 0 + 0 + + + + + 16777215 + 40 + + + + true + + + + + + + + + Use: + + + + + + + Jd + + + + + + + Location + + + diff --git a/stellarium.kdev4 b/stellarium.kdev4 new file mode 100644 index 0000000000000..b4a438370b981 --- /dev/null +++ b/stellarium.kdev4 @@ -0,0 +1,4 @@ +[Project] +CreatedFrom=CMakeLists.txt +Manager=KDevCMakeManager +Name=stellarium