skyobjitem.cpp 8.32 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
        case DispImageRole:
76
            return getImageURL(true);
77
        case DispSummaryRole:
78
            return getSummary(true);
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;

110 111 112 113
    if(rounded_altitude<=0)
        m_Position = "<span style='color:red'>" + xi18n("NOT VISIBLE: About %1 degrees below the %2 horizon", -rounded_altitude, KSUtils::toDirectionString( sp.az() ) ) + "</span>";
    else
        m_Position = "<span style='color:yellow'>" + xi18n("Now visible: About %1 degrees above the %2 horizon", rounded_altitude, KSUtils::toDirectionString( sp.az() ) ) + "</span>";
114
}
115

116
QString SkyObjItem::getImageURL(bool preferThumb) const
117 118 119 120 121
{
    if ( m_Type==Star )
    {
        return "";
    }
122 123
    QString thumbName = KSPaths::locate(QStandardPaths::GenericDataLocation, "thumb-" + m_So->name().toLower().remove( ' ' ) + ".png" ) ;
    QString fullSizeName = KSPaths::locate(QStandardPaths::GenericDataLocation, "Image_" + m_So->name().toLower().remove( ' ' ) + ".png" ) ;
124

125 126 127 128 129 130 131 132
    //First try to return the preferred file
    if(thumbName!=""&&preferThumb)
        return thumbName;
    if(fullSizeName!=""&&(!preferThumb))
        return fullSizeName;

    //If that fails, try to return the large image first, then the thumb and then if it is a planet, the xplanet image.
    QString fname = KSPaths::locate(QStandardPaths::GenericDataLocation, "Image_" + m_So->name().toLower().remove( ' ' ) + ".png" ) ;
133 134
    if(fname=="")
        fname = KSPaths::locate(QStandardPaths::GenericDataLocation, "thumb-" + m_So->name().toLower().remove( ' ' ) + ".png" ) ;
135
    if(fname=="" && m_Type==Planet){
136
        fname=KSPaths::locate(QStandardPaths::GenericDataLocation, "xplanet/" + m_So->name() + ".png" );
137 138 139 140 141 142
    }

    return fname;

}

143
QString SkyObjItem::getSummary(bool includeDescription) const
144
{
145 146 147 148
    if(includeDescription)
        return m_So->typeName() + "<BR>" + getRADE() + "<BR>" + getAltAz()+"<BR><BR>" + loadObjectDescription();
    else
        return m_So->typeName() + "<BR>" + getRADE() + "<BR>" + getAltAz();
149
}
150

151
QString SkyObjItem::getDesc() const
152
{
153
    if (m_Type == Planet)
154
    {
155
        KSFileReader fileReader;
156
        if (!fileReader.open("PlanetFacts.dat"))
Jasem Mutlaq's avatar
Jasem Mutlaq committed
157
            return xi18n("No Description found for selected sky-object");
158

159
        while (fileReader.hasMoreLines())
160 161
        {
            QString line = fileReader.readLine();
162
            if(line.length() != 0 && line[0] != '#')
163
            {
164 165 166 167 168 169
                QString soname = line.split("::")[0];
                QString desc = line.split("::")[1];
                if (soname == m_Name)
                {
                    return desc;
                }
170
            }
171 172
        }
    }
Vijay Dhameliya's avatar
Vijay Dhameliya committed
173 174

    if(skd)
175
    {
Yuri Chornoivan's avatar
Yuri Chornoivan committed
176
        if(!skd->downloadedData().isEmpty())
Vijay Dhameliya's avatar
Vijay Dhameliya committed
177
            return skd->downloadedData();
178 179
    }

Vijay Dhameliya's avatar
Vijay Dhameliya committed
180
    if(m_Type == Star)
Jasem Mutlaq's avatar
Jasem Mutlaq committed
181
        return xi18n("Bright Star");
Vijay Dhameliya's avatar
Vijay Dhameliya committed
182

183
    return m_So->typeName();
Vijay Dhameliya's avatar
Vijay Dhameliya committed
184

185 186
}

187 188 189 190
QString SkyObjItem::getDescSource()
{
    if (m_Type == Planet)
    {
Jasem Mutlaq's avatar
Jasem Mutlaq committed
191
        return xi18n("(Source: Wikipedia)");
192
    }
Vijay Dhameliya's avatar
Vijay Dhameliya committed
193 194 195

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

Jasem Mutlaq's avatar
Jasem Mutlaq committed
200
    return xi18n("(Source: N/A)");
201 202 203
}


204 205 206 207 208 209 210 211 212
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
      */

213
    DeepSkyObject * dso = (DeepSkyObject *)m_So;
214 215 216 217
    float SB = m_So->mag() + 2.5 * log10(dso->a() * dso->b() / 4);

    switch(getType())
    {
218 219
        case Galaxy:
        case Nebula:
220
            return QLocale().toString(SB,'f', 2) + "<BR>   (mag/arcmin^2)";
221 222
        default:
            return QString(" --"); // Not applicable for other sky-objects
223
    }
224
}
225 226 227 228 229

QString SkyObjItem::getSize() const
{
    switch (getType())
    {
230 231 232
        case Galaxy:
        case Cluster:
        case Nebula:
233
            return QLocale().toString(((DeepSkyObject *)m_So)->a(),'f', 2) + "\"";
234
        case Planet:
235
            return QLocale().toString(((KSPlanetBase *)m_So)->angSize(),'f', 2) + "\"";
236 237
        default:
            return QString(" --");
238 239
    }
}
240 241 242 243 244 245 246 247 248 249 250 251

inline QString SkyObjItem::loadObjectDescription() const{
    QFile file;
    QString fname = "description-" + getName().toLower().remove( ' ' ) + ".html";
    file.setFileName( KSPaths::writableLocation(QStandardPaths::GenericDataLocation) + "descriptions/" + fname ) ; //determine filename in local user KDE directory tree.

    if(file.exists())
    {
        if(file.open(QIODevice::ReadOnly))
        {
            QTextStream in(&file);
            QString line;
252
            line = in.readLine();//This should only read the description since the source is on the next line
253 254 255 256 257 258
            file.close();
            return line;
        }
    }
    return "";
}