wiview.cpp 10.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/***************************************************************************
                          wiview.cpp  -  K Desktop Planetarium
                             -------------------
    begin                : 2012/26/05
    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

19 20 21 22
#include <QtQuick/QQuickView>
#include <QtQuick/QQuickItem>
#include <QStandardPaths>
#include <QGraphicsObject>
23
#include "wiview.h"
24
#include "skymap.h"
25
#include "dialogs/detaildialog.h"
26
#include <klocalizedcontext.h>
27
#include "kspaths.h"
28

29 30 31 32 33 34 35
#ifdef HAVE_INDI
#include <basedevice.h>
#include "indi/indilistener.h"
#include "indi/indistd.h"
#include "indi/driverinfo.h"
#endif

36

37
WIView::WIView(QWidget * parent, ObsConditions * obs) : QWidget(parent), m_Obs(obs), m_CurCategorySelected(-1)
38
{
39 40


41
    m_ModManager = new ModelManager(m_Obs);
42

43
    m_BaseView = new QQuickView();
44

45
    ///To use i18n() instead of qsTr() in qml/wiview.qml for translation
46
    //KDeclarative kd;
47
    // kd.setDeclarativeEngine(m_BaseView->engine());
48 49
    //kd.initialize();
    //kd.setupBindings();
50

51
    m_Ctxt = m_BaseView->rootContext();
52

53 54 55
    ///Use instead of KDeclarative
    m_Ctxt->setContextObject(new KLocalizedContext(m_BaseView));

56
    m_BaseView->setSource(QUrl::fromLocalFile(KSPaths::locate(QStandardPaths::AppDataLocation, "tools/whatsinteresting/qml/wiview.qml")));
57

58
    m_BaseObj = m_BaseView->rootObject();
59

60
    //soTypeTextObj = m_BaseObj->findChild<QObject *>("soTypeTextObj");
61

62
    m_ViewsRowObj = m_BaseObj->findChild<QQuickItem *>(QString("viewsRowObj"));
63
    connect(m_ViewsRowObj, SIGNAL(categorySelected(int)), this, SLOT(onCategorySelected(int)));
64

65
    m_SoListObj = m_BaseObj->findChild<QQuickItem *>("soListObj");
66
    connect(m_SoListObj, SIGNAL(soListItemClicked(int, QString, int)), this, SLOT(onSoListItemClicked(int, QString, int)));
67

68
    m_DetailsViewObj = m_BaseObj->findChild<QQuickItem *>("detailsViewObj");
69

70
    m_NextObj = m_BaseObj->findChild<QQuickItem *>("nextObj");
71
    connect(m_NextObj, SIGNAL(nextObjClicked()), this, SLOT(onNextObjClicked()));
72
    m_PrevObj = m_BaseObj->findChild<QQuickItem *>("prevObj");
73
    connect(m_PrevObj, SIGNAL(prevObjClicked()), this, SLOT(onPrevObjClicked()));
74

75
    m_SlewButtonObj = m_BaseObj->findChild<QQuickItem *>("slewButtonObj");
76 77
    connect(m_SlewButtonObj, SIGNAL(slewButtonClicked()), this, SLOT(onSlewButtonClicked()));

78 79 80
    m_SlewTelescopeButtonObj = m_BaseObj->findChild<QQuickItem *>("slewTelescopeButtonObj");
    connect(m_SlewTelescopeButtonObj, SIGNAL(slewTelescopeButtonClicked()), this, SLOT(onSlewTelescopeButtonClicked()));

81
    m_DetailsButtonObj = m_BaseObj->findChild<QQuickItem *>("detailsButtonObj");
82 83
    connect(m_DetailsButtonObj, SIGNAL(detailsButtonClicked()), this, SLOT(onDetailsButtonClicked()));

84
    QObject * settingsIconObj = m_BaseObj->findChild<QQuickItem *>("settingsIconObj");
85 86
    connect(settingsIconObj, SIGNAL(settingsIconClicked()), this, SLOT(onSettingsIconClicked()));

87
    QObject * reloadIconObj = m_BaseObj->findChild<QQuickItem *>("reloadIconObj");
88 89
    connect(reloadIconObj, SIGNAL(reloadIconClicked()), this, SLOT(onReloadIconClicked()));

90
    m_BaseView->setResizeMode(QQuickView::SizeRootObjectToView);
91
    m_BaseView->show();
92 93
}

Samikshan Bairagya's avatar
Samikshan Bairagya committed
94 95
WIView::~WIView()
{
96
    delete m_ModManager;
97
    delete m_CurSoItem;
Samikshan Bairagya's avatar
Samikshan Bairagya committed
98
}
99

100
void WIView::onCategorySelected(int type)
101
{
102
    m_CurCategorySelected = type;
103
    m_Ctxt->setContextProperty("soListModel", m_ModManager->returnModel(type));
104
}
105

106
void WIView::onSoListItemClicked(int type, QString typeName, int index)
107
{
Rafal Kulaga's avatar
Rafal Kulaga committed
108 109
    Q_UNUSED(typeName)

110
    SkyObjItem * soitem = m_ModManager->returnModel(type)->getSkyObjItem(index);
111

112 113
//    soTypeTextObj->setProperty("text", typeName);
//    soTypeTextObj->setProperty("visible", true);
114

115
//    soListObj->setProperty("visible", false);
116

117
    loadDetailsView(soitem, index);
118 119
}

120
void WIView::onNextObjClicked()
121
{
122
    int modelSize = m_ModManager->returnModel(m_CurSoItem->getType())->rowCount();
123
    SkyObjItem * nextItem = m_ModManager->returnModel(m_CurSoItem->getType())->getSkyObjItem((m_CurIndex+1)%modelSize);
124
    loadDetailsView(nextItem, (m_CurIndex+1)%modelSize);
Samikshan Bairagya's avatar
Samikshan Bairagya committed
125
}
126 127 128

void WIView::onPrevObjClicked()
{
129
    int modelSize = m_ModManager->returnModel(m_CurSoItem->getType())->rowCount();
130
    SkyObjItem * prevItem = m_ModManager->returnModel(m_CurSoItem->getType())->getSkyObjItem((m_CurIndex-1+modelSize)%modelSize);
131 132
    loadDetailsView(prevItem, (m_CurIndex-1+modelSize)%modelSize);
}
133 134 135 136

void WIView::onSlewButtonClicked()
{
    ///Slew map to selected sky-object
137 138
    SkyObject * so = m_CurSoItem->getSkyObject();
    KStars * kstars = KStars::Instance();
139 140
    if (so != 0)
    {
141 142 143
        kstars->map()->setFocusPoint(so);
        kstars->map()->setFocusObject(so);
        kstars->map()->setDestination(*kstars->map()->focusPoint());
144 145
    }
}
146

147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
void WIView::onSlewTelescopeButtonClicked()
{

    if(KMessageBox::Continue==KMessageBox::warningContinueCancel(NULL, "Are you sure you want your telescope to slew to this object?",
            i18n("Continue Slew"),  KStandardGuiItem::cont(), KStandardGuiItem::cancel(), "continue_wi_slew_warning"))
    {

#ifdef HAVE_INDI

    if (INDIListener::Instance()->size() == 0)
    {
        KMessageBox::sorry(0, i18n("KStars did not find any active telescopes."));
        return;
    }

    foreach(ISD::GDInterface * gd, INDIListener::Instance()->getDevices())
    {
        INDI::BaseDevice * bd = gd->getBaseDevice();

        if (gd->getType() != KSTARS_TELESCOPE)
            continue;

        if (bd == NULL)
            continue;

        if (bd->isConnected() == false)
        {
            KMessageBox::error(0, i18n("Telescope %1 is offline. Please connect and retry again.", gd->getDeviceName()));
            return;
        }


        ISD::GDSetCommand SlewCMD(INDI_SWITCH, "ON_COORD_SET", "TRACK", ISS_ON, this);

        gd->setProperty(&SlewCMD);
        gd->runCommand(INDI_SEND_COORDS, m_CurSoItem->getSkyObject());

        ///Slew map to selected sky-object
        onSlewButtonClicked();

        return;

    }

    KMessageBox::sorry(0, i18n("KStars did not find any active telescopes."));

#endif
    }


}

199 200 201
void WIView::onDetailsButtonClicked()
{
    ///Code taken from WUTDialog::slotDetails()
202 203 204
    KStars * kstars = KStars::Instance();
    SkyObject * so = m_CurSoItem->getSkyObject();
    DetailDialog * detail = new DetailDialog(so, kstars->data()->lt(), kstars->data()->geo(), kstars);
205 206 207
    detail->exec();
    delete detail;
}
208 209 210

void WIView::onSettingsIconClicked()
{
211
    KStars * kstars = KStars::Instance();
212
    kstars->showWISettingsUI();
213
}
214

215 216 217 218 219
void WIView::onReloadIconClicked()
{
    updateModels(m_Obs);
}

220
void WIView::updateModels(ObsConditions * obs)
221 222 223 224 225 226 227 228 229
{
    m_Obs = obs;
    m_ModManager->updateModels(m_Obs);
    m_Ctxt->setContextProperty("soListModel", m_ModManager->returnModel(0));
    m_Ctxt->setContextProperty("soListModel", m_ModManager->returnModel(1));
    m_Ctxt->setContextProperty("soListModel", m_ModManager->returnModel(2));
    m_Ctxt->setContextProperty("soListModel", m_ModManager->returnModel(3));
    m_Ctxt->setContextProperty("soListModel", m_ModManager->returnModel(4));
    m_Ctxt->setContextProperty("soListModel", m_ModManager->returnModel(5));
230 231 232

    if (m_CurCategorySelected >=0 && m_CurCategorySelected <= 5)
        m_Ctxt->setContextProperty("soListModel", m_ModManager->returnModel(m_CurCategorySelected));
233 234
}

235
void WIView::loadDetailsView(SkyObjItem * soitem, int index)
236 237 238 239 240
{
    m_CurSoItem = soitem;
    m_CurIndex = index;

    int modelSize = m_ModManager->returnModel(m_CurSoItem->getType())->rowCount();
241 242
    SkyObjItem * nextItem = m_ModManager->returnModel(m_CurSoItem->getType())->getSkyObjItem((m_CurIndex+1)%modelSize);
    SkyObjItem * prevItem = m_ModManager->returnModel(m_CurSoItem->getType())->getSkyObjItem((m_CurIndex-1+modelSize)%modelSize);
243

Vijay Dhameliya's avatar
Vijay Dhameliya committed
244 245 246 247 248 249 250 251 252
    if(modelSize <= 1)
    {
        m_NextObj->setProperty("visible", "false");
        m_PrevObj->setProperty("visible", "false");
    }
    else
    {
        m_NextObj->setProperty("visible", "true");
        m_PrevObj->setProperty("visible", "true");
253
        QObject * nextTextObj = m_NextObj->findChild<QObject *>("nextTextObj");
Vijay Dhameliya's avatar
Vijay Dhameliya committed
254
        nextTextObj->setProperty("text", nextItem->getName());
255
        QObject * prevTextObj = m_PrevObj->findChild<QObject *>("prevTextObj");
Vijay Dhameliya's avatar
Vijay Dhameliya committed
256 257
        prevTextObj->setProperty("text", prevItem->getName());
    }
258

259 260
    QObject * sonameObj = m_DetailsViewObj->findChild<QObject *>("sonameObj");
    QObject * posTextObj = m_DetailsViewObj->findChild<QObject *>("posTextObj");
261
    QObject * detailImage = m_DetailsViewObj->findChild<QObject *>("detailImage");
262 263 264 265 266
    QObject * descTextObj = m_DetailsViewObj->findChild<QObject *>("descTextObj");
    QObject * descSrcTextObj = m_DetailsViewObj->findChild<QObject *>("descSrcTextObj");
    QObject * magTextObj = m_DetailsViewObj->findChild<QObject *>("magTextObj");
    QObject * sbTextObj = m_DetailsViewObj->findChild<QObject *>("sbTextObj");
    QObject * sizeTextObj = m_DetailsViewObj->findChild<QObject *>("sizeTextObj");
267 268 269

    sonameObj->setProperty("text", soitem->getLongName());
    posTextObj->setProperty("text", soitem->getPosition());
270
    detailImage->setProperty("refreshableSource", soitem->getImageURL());
271 272 273 274 275
    descTextObj->setProperty("text", soitem->getDesc());
    descSrcTextObj->setProperty("text", soitem->getDescSource());

    QString magText;
    if (soitem->getType() == SkyObjItem::Constellation)
Jasem Mutlaq's avatar
Jasem Mutlaq committed
276
        magText = xi18n("Magnitude:  --");
277
    else
Jasem Mutlaq's avatar
Jasem Mutlaq committed
278
        magText = xi18n("Magnitude: %1 mag", QLocale().toString(soitem->getMagnitude(), 2));
279 280
    magTextObj->setProperty("text", magText);

Jasem Mutlaq's avatar
Jasem Mutlaq committed
281
    QString sbText = xi18n("Surface Brightness: %1", soitem->getSurfaceBrightness());
282 283
    sbTextObj->setProperty("text", sbText);

Jasem Mutlaq's avatar
Jasem Mutlaq committed
284
    QString sizeText = xi18n("Size: %1", soitem->getSize());
285 286
    sizeTextObj->setProperty("text", sizeText);
}