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

0001 /*
0002     SPDX-FileCopyrightText: 2016 Artem Fedoskin <afedoskin3@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "finddialoglite.h"
0008 
0009 #include "catalogentrydata.h"
0010 #include "deepskyobject.h"
0011 #include "kstarsdata.h"
0012 #include "kstarslite.h"
0013 #include "skymaplite.h"
0014 #include "skyobjectlistmodel.h"
0015 #include "skycomponents/skymapcomposite.h"
0016 #include "skyobjects/skyobject.h"
0017 #include "tools/nameresolver.h"
0018 
0019 #include <QQmlContext>
0020 #include <QSortFilterProxyModel>
0021 #include <QTimer>
0022 
0023 FindDialogLite::FindDialogLite()
0024 {
0025     m_filterModel.append(i18n("Any"));
0026     m_filterModel.append(i18n("Stars"));
0027     m_filterModel.append(i18n("Solar System"));
0028     m_filterModel.append(i18n("Open Clusters"));
0029     m_filterModel.append(i18n("Globular Clusters"));
0030     m_filterModel.append(i18n("Gaseous Nebulae"));
0031     m_filterModel.append(i18n("Planetary Nebulae"));
0032     m_filterModel.append(i18n("Galaxies"));
0033     m_filterModel.append(i18n("Comets"));
0034     m_filterModel.append(i18n("Asteroids"));
0035     m_filterModel.append(i18n("Constellations"));
0036     m_filterModel.append(i18n("Supernovae"));
0037     m_filterModel.append(i18n("Satellites"));
0038     emit filterModelChanged();
0039 
0040     fModel      = new SkyObjectListModel(this);
0041     m_sortModel = new QSortFilterProxyModel(this);
0042     m_sortModel->setFilterCaseSensitivity(Qt::CaseInsensitive);
0043     m_sortModel->setSourceModel(fModel);
0044     m_sortModel->setSortRole(Qt::DisplayRole);
0045     m_sortModel->setFilterRole(Qt::DisplayRole);
0046     m_sortModel->setDynamicSortFilter(true);
0047     KStarsLite::Instance()->qmlEngine()->rootContext()->setContextProperty("SortModel", m_sortModel);
0048     m_sortModel->sort(0);
0049 }
0050 
0051 FindDialogLite::~FindDialogLite()
0052 {
0053 }
0054 
0055 void FindDialogLite::filterByType(uint typeIndex)
0056 {
0057     KStarsData *data = KStarsData::Instance();
0058     m_typeIndex      = typeIndex;
0059 
0060     switch (typeIndex)
0061     {
0062         case 0: // All object types
0063         {
0064             QVector<QPair<QString, const SkyObject *>> allObjects;
0065             foreach (int type, data->skyComposite()->objectLists().keys())
0066             {
0067                 allObjects.append(data->skyComposite()->objectLists(SkyObject::TYPE(type)));
0068             }
0069             fModel->setSkyObjectsList(allObjects);
0070             break;
0071         }
0072         case 1: //Stars
0073         {
0074             QVector<QPair<QString, const SkyObject *>> starObjects;
0075             starObjects.append(data->skyComposite()->objectLists(SkyObject::STAR));
0076             starObjects.append(data->skyComposite()->objectLists(SkyObject::CATALOG_STAR));
0077             fModel->setSkyObjectsList(starObjects);
0078             break;
0079         }
0080         case 2: //Solar system
0081         {
0082             QVector<QPair<QString, const SkyObject *>> ssObjects;
0083             ssObjects.append(data->skyComposite()->objectLists(SkyObject::PLANET));
0084             ssObjects.append(data->skyComposite()->objectLists(SkyObject::COMET));
0085             ssObjects.append(data->skyComposite()->objectLists(SkyObject::ASTEROID));
0086             ssObjects.append(data->skyComposite()->objectLists(SkyObject::MOON));
0087 
0088             fModel->setSkyObjectsList(ssObjects);
0089             break;
0090         }
0091         case 3: //Open Clusters
0092             fModel->setSkyObjectsList(data->skyComposite()->objectLists(SkyObject::OPEN_CLUSTER));
0093             break;
0094         case 4: //Globular Clusters
0095             fModel->setSkyObjectsList(data->skyComposite()->objectLists(SkyObject::GLOBULAR_CLUSTER));
0096             break;
0097         case 5: //Gaseous nebulae
0098             fModel->setSkyObjectsList(data->skyComposite()->objectLists(SkyObject::GASEOUS_NEBULA));
0099             break;
0100         case 6: //Planetary nebula
0101             fModel->setSkyObjectsList(data->skyComposite()->objectLists(SkyObject::PLANETARY_NEBULA));
0102             break;
0103         case 7: //Galaxies
0104             fModel->setSkyObjectsList(data->skyComposite()->objectLists(SkyObject::GALAXY));
0105             break;
0106         case 8: //Comets
0107             fModel->setSkyObjectsList(data->skyComposite()->objectLists(SkyObject::COMET));
0108             break;
0109         case 9: //Asteroids
0110             fModel->setSkyObjectsList(data->skyComposite()->objectLists(SkyObject::ASTEROID));
0111             break;
0112         case 10: //Constellations
0113             fModel->setSkyObjectsList(data->skyComposite()->objectLists(SkyObject::CONSTELLATION));
0114             break;
0115         case 11: //Supernovae
0116             fModel->setSkyObjectsList(data->skyComposite()->objectLists(SkyObject::SUPERNOVA));
0117             break;
0118         case 12: //Satellites
0119             fModel->setSkyObjectsList(data->skyComposite()->objectLists(SkyObject::SATELLITE));
0120             break;
0121     }
0122 }
0123 
0124 void FindDialogLite::filterList(QString searchQuery)
0125 {
0126     m_searchQuery = searchQuery;
0127     setIsResolveEnabled(!isInList(searchQuery));
0128     QString SearchText = processSearchText(searchQuery);
0129     m_sortModel->setFilterFixedString(SearchText);
0130     listFiltered = true;
0131 }
0132 
0133 void FindDialogLite::selectObject(int index)
0134 {
0135     QVariant sObj     = m_sortModel->data(m_sortModel->index(index, 0), SkyObjectListModel::SkyObjectRole);
0136     SkyObject *skyObj = (SkyObject *)sObj.value<void *>();
0137     SkyMapLite::Instance()->slotSelectObject(skyObj);
0138 }
0139 
0140 // Process the search box text to replace equivalent names like "m93" with "m 93"
0141 QString FindDialogLite::processSearchText(QString text)
0142 {
0143     QRegExp re;
0144     QString searchtext = text;
0145 
0146     re.setCaseSensitivity(Qt::CaseInsensitive);
0147 
0148     // If it is an NGC/IC/M catalog number, as in "M 76" or "NGC 5139", check for absence of the space
0149     re.setPattern("^(m|ngc|ic)\\s*\\d*$");
0150     if (text.contains(re))
0151     {
0152         re.setPattern("\\s*(\\d+)");
0153         searchtext.replace(re, " \\1");
0154         re.setPattern("\\s*$");
0155         searchtext.remove(re);
0156         re.setPattern("^\\s*");
0157         searchtext.remove(re);
0158     }
0159 
0160     // TODO after KDE 4.1 release:
0161     // If it is a IAU standard three letter abbreviation for a constellation, then go to that constellation
0162     // Check for genetive names of stars. Example: alp CMa must go to alpha Canis Majoris
0163 
0164     return searchtext;
0165 }
0166 
0167 bool FindDialogLite::isInList(QString searchQuery)
0168 {
0169     if (searchQuery.isEmpty())
0170         return false;
0171 
0172     int size         = m_sortModel->rowCount(m_sortModel->index(0, 0));
0173     QString stripped = searchQuery.remove(' ');
0174 
0175     for (int i = 0; i < size; ++i)
0176     {
0177         QString s = m_sortModel->data(m_sortModel->index(i, 0), Qt::DisplayRole).toString();
0178 
0179         if (s == searchQuery || s.remove(' ') == stripped)
0180             return true;
0181     }
0182     return false;
0183 }
0184 
0185 void FindDialogLite::resolveInInternet(QString searchQuery)
0186 {
0187     emit notifyMessage(i18n("Not Implemented.", searchQuery));
0188 }
0189 
0190 void FindDialogLite::setIsResolveEnabled(bool isResolveEnabled)
0191 {
0192     if (m_isResolveEnabled != isResolveEnabled)
0193     {
0194         m_isResolveEnabled = isResolveEnabled;
0195         emit isResolveEnabledChanged(isResolveEnabled);
0196     }
0197 }