skyobjitem.cpp 6.64 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/***************************************************************************
                          skyobjitem.cpp  -  K Desktop Planetarium
                             -------------------
    begin                : 2012/21/06
    copyright            : (C) 2012 by Samikshan Bairagya
    email                : samikshan@gmail.com
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

18
#include "ksfilereader.h"
19
#include "kstarsdata.h"
20 21
#include "deepskyobject.h"
#include "ksplanetbase.h"
22
#include "skyobjitem.h"
23
#include "ksutils.h"
24
#include "kspaths.h"
25

26
SkyObjItem::SkyObjItem(SkyObject * so) : m_Name(so->name()), m_LongName(so->longname()),m_TypeName(so->typeName()), m_So(so), skd(NULL)
27
{
28
    switch (so->type())
29
    {
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
        case SkyObject::PLANET:
            m_Type = Planet;
            break;
        case SkyObject::STAR:
            skd = new SkyObjDescription(m_Name, m_TypeName);
            m_Type = Star;
            break;
        case SkyObject::CONSTELLATION:
            skd = new SkyObjDescription(m_Name, m_TypeName);
            m_Type = Constellation;
            break;
        case SkyObject::GALAXY:
            skd = new SkyObjDescription(m_LongName, "");
            m_Type = Galaxy;
            break;
        case SkyObject::OPEN_CLUSTER:
        case SkyObject::GLOBULAR_CLUSTER:
        case SkyObject::GALAXY_CLUSTER:
            if(m_Name.contains("NGC", Qt::CaseInsensitive))
                skd = new SkyObjDescription(m_Name, "");
            m_Type = Cluster;
            break;
        case SkyObject::PLANETARY_NEBULA:
        case SkyObject::GASEOUS_NEBULA:
        case SkyObject::DARK_NEBULA:
            if(m_Name.contains("NGC", Qt::CaseInsensitive))
                skd = new SkyObjDescription(m_Name, "");
            m_Type = Nebula;
            break;
59 60
    }

61
    setPosition(m_So);
62 63
}

64 65
SkyObjItem::~SkyObjItem()
{
Vijay Dhameliya's avatar
Vijay Dhameliya committed
66 67 68
    delete skd;
}

69 70 71 72 73
QVariant SkyObjItem::data(int role)
{
    switch(role)
    {
        case DispNameRole:
74
            return getLongName();
75 76 77
        case DispImageRole:
            return getImageURL();
        case DispSummaryRole:
78
            return getSummary();
79 80
        case CategoryRole:
            return getType();
81 82
        case CategoryNameRole:
            return getTypeName();
83 84 85 86 87
        default:
            return QVariant();
    }
}

88 89
///Moved to skyobjlistmodel.cpp
/*
90
QHash<int, QByteArray> SkyObjItem::roleNames() const
91
{
92 93
    QHash<int, QByteArray> roles;
    roles[DispNameRole] = "dispName";
94
    roles[CategoryRole] = "type";
95
    roles[CategoryNameRole] = "typeName";
96
    return roles;
97
}
98
*/
99

100
void SkyObjItem::setPosition(SkyObject * so)
101
{
102
    KStarsData * data = KStarsData::Instance();
103
    KStarsDateTime ut = data->geo()->LTtoUT(KStarsDateTime(QDateTime::currentDateTime().toLocalTime()));
104
    SkyPoint sp = so->recomputeCoords(ut, data->geo());
105 106

    //check altitude of object at this time.
107
    sp.EquatorialToHorizontal(data->lst(), data->geo()->lat());
108 109
    double rounded_altitude = (int)(sp.alt().Degrees()/5.0)*5.0;

Jasem Mutlaq's avatar
Jasem Mutlaq committed
110
    m_Position = xi18n("Now visible: About %1 degrees above the %2 horizon", rounded_altitude, KSUtils::toDirectionString( sp.az() ) );
111
}
112

113 114 115 116 117 118 119
QString SkyObjItem::getImageURL() const
{
    if ( m_Type==Star )
    {
        return "";
    }

120 121 122
    QString fname = KSPaths::locate(QStandardPaths::GenericDataLocation, "image_" + m_So->name().toLower().remove( ' ' ) + ".png" ) ;
    if(fname=="")
        fname = KSPaths::locate(QStandardPaths::GenericDataLocation, "thumb-" + m_So->name().toLower().remove( ' ' ) + ".png" ) ;
123
    if(fname=="" && m_Type==Planet){
124
        fname=KSPaths::locate(QStandardPaths::GenericDataLocation, "xplanet/" + m_So->name() + ".png" );
125 126 127 128 129 130
    }

    return fname;

}

131 132 133 134
QString SkyObjItem::getSummary() const
{
    return m_So->typeName()+"\n"+getRADE()+"\n"+getAltAz()+"\n";
}
135

136
QString SkyObjItem::getDesc() const
137
{
138
    if (m_Type == Planet)
139
    {
140
        KSFileReader fileReader;
141
        if (!fileReader.open("PlanetFacts.dat"))
Jasem Mutlaq's avatar
Jasem Mutlaq committed
142
            return xi18n("No Description found for selected sky-object");
143

144
        while (fileReader.hasMoreLines())
145 146
        {
            QString line = fileReader.readLine();
147
            if(line.length() != 0 && line[0] != '#')
148
            {
149 150 151 152 153 154
                QString soname = line.split("::")[0];
                QString desc = line.split("::")[1];
                if (soname == m_Name)
                {
                    return desc;
                }
155
            }
156 157
        }
    }
Vijay Dhameliya's avatar
Vijay Dhameliya committed
158 159

    if(skd)
160
    {
Yuri Chornoivan's avatar
Yuri Chornoivan committed
161
        if(!skd->downloadedData().isEmpty())
Vijay Dhameliya's avatar
Vijay Dhameliya committed
162
            return skd->downloadedData();
163 164
    }

Vijay Dhameliya's avatar
Vijay Dhameliya committed
165
    if(m_Type == Star)
Jasem Mutlaq's avatar
Jasem Mutlaq committed
166
        return xi18n("Bright Star");
Vijay Dhameliya's avatar
Vijay Dhameliya committed
167

168
    return m_So->typeName();
Vijay Dhameliya's avatar
Vijay Dhameliya committed
169

170 171
}

172 173 174 175
QString SkyObjItem::getDescSource()
{
    if (m_Type == Planet)
    {
Jasem Mutlaq's avatar
Jasem Mutlaq committed
176
        return xi18n("(Source: Wikipedia)");
177
    }
Vijay Dhameliya's avatar
Vijay Dhameliya committed
178 179 180

    if(skd)
    {
Vijay Dhameliya's avatar
Vijay Dhameliya committed
181 182
        if(!skd->downloadedData().isEmpty() && !skd->url().isEmpty())
            return "(Source: <a href=\"" + skd->url() + "\">Wikipedia</a>)";
Vijay Dhameliya's avatar
Vijay Dhameliya committed
183 184
    }

Jasem Mutlaq's avatar
Jasem Mutlaq committed
185
    return xi18n("(Source: N/A)");
186 187 188
}


189 190 191 192 193 194 195 196 197
QString SkyObjItem::getSurfaceBrightness() const
{
    /** Surface Brightness is applicable only for extended light sources like
      * Deep-Sky Objects. Here we use the formula SB = m + log10(a*b/4)
      * where m is the magnitude of the sky-object. a and b are the major and minor
      * axis lengths of the objects respectively in arcminutes. SB is the surface
      * brightness obtained in mag * arcminutes^-2
      */

198
    DeepSkyObject * dso = (DeepSkyObject *)m_So;
199 200 201 202
    float SB = m_So->mag() + 2.5 * log10(dso->a() * dso->b() / 4);

    switch(getType())
    {
203 204
        case Galaxy:
        case Nebula:
205
            return QLocale().toString(SB,'f', 2) + "\n   (mag/arcmin^2)";
206 207
        default:
            return QString(" --"); // Not applicable for other sky-objects
208
    }
209
}
210 211 212 213 214

QString SkyObjItem::getSize() const
{
    switch (getType())
    {
215 216 217
        case Galaxy:
        case Cluster:
        case Nebula:
218
            return QLocale().toString(((DeepSkyObject *)m_So)->a(),'f', 2) + "\"";
219
        case Planet:
220
            return QLocale().toString(((KSPlanetBase *)m_So)->angSize(),'f', 2) + "\"";
221 222
        default:
            return QString(" --");
223 224
    }
}