wiview.cpp 8.52 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
WIView::WIView(QWidget *parent, ObsConditions *obs) : QWidget(parent), m_Obs(obs), m_CurCategorySelected(-1)
31
{
32
    m_ModManager = new ModelManager(m_Obs);
33

34
    m_BaseView = new QQuickView();
35

36
    ///To use i18n() instead of qsTr() in qml/wiview.qml for translation
37 38 39 40
    //KDeclarative kd;
   // kd.setDeclarativeEngine(m_BaseView->engine());
    //kd.initialize();
    //kd.setupBindings();
41

42
    m_Ctxt = m_BaseView->rootContext();
43

44 45 46
    ///Use instead of KDeclarative
    m_Ctxt->setContextObject(new KLocalizedContext(m_BaseView));

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

49
    m_BaseObj = m_BaseView->rootObject();
50

51
    //soTypeTextObj = m_BaseObj->findChild<QObject *>("soTypeTextObj");
52

53
    m_ViewsRowObj = m_BaseObj->findChild<QQuickItem*>(QString("viewsRowObj"));
54
    connect(m_ViewsRowObj, SIGNAL(categorySelected(int)), this, SLOT(onCategorySelected(int)));
55

56
    m_SoListObj = m_BaseObj->findChild<QQuickItem *>("soListObj");
57
    connect(m_SoListObj, SIGNAL(soListItemClicked(int, QString, int)), this, SLOT(onSoListItemClicked(int, QString, int)));
58

59
    m_DetailsViewObj = m_BaseObj->findChild<QQuickItem *>("detailsViewObj");
60

61
    m_NextObj = m_BaseObj->findChild<QQuickItem *>("nextObj");
62
    connect(m_NextObj, SIGNAL(nextObjClicked()), this, SLOT(onNextObjClicked()));
63
    m_PrevObj = m_BaseObj->findChild<QQuickItem *>("prevObj");
64
    connect(m_PrevObj, SIGNAL(prevObjClicked()), this, SLOT(onPrevObjClicked()));
65

66
    m_SlewButtonObj = m_BaseObj->findChild<QQuickItem*>("slewButtonObj");
67 68
    connect(m_SlewButtonObj, SIGNAL(slewButtonClicked()), this, SLOT(onSlewButtonClicked()));

69
    m_DetailsButtonObj = m_BaseObj->findChild<QQuickItem *>("detailsButtonObj");
70 71
    connect(m_DetailsButtonObj, SIGNAL(detailsButtonClicked()), this, SLOT(onDetailsButtonClicked()));

72
    QObject *settingsIconObj = m_BaseObj->findChild<QQuickItem *>("settingsIconObj");
73 74
    connect(settingsIconObj, SIGNAL(settingsIconClicked()), this, SLOT(onSettingsIconClicked()));

75
    QObject *reloadIconObj = m_BaseObj->findChild<QQuickItem*>("reloadIconObj");
76 77
    connect(reloadIconObj, SIGNAL(reloadIconClicked()), this, SLOT(onReloadIconClicked()));

78
    m_BaseView->setResizeMode(QQuickView::SizeRootObjectToView);
79
    m_BaseView->show();
80 81
}

Samikshan Bairagya's avatar
Samikshan Bairagya committed
82 83
WIView::~WIView()
{
84
    delete m_ModManager;
85
    delete m_CurSoItem;
Samikshan Bairagya's avatar
Samikshan Bairagya committed
86
}
87

88
void WIView::onCategorySelected(int type)
89
{
90
    m_CurCategorySelected = type;
91
    m_Ctxt->setContextProperty("soListModel", m_ModManager->returnModel(type));
92
}
93

94
void WIView::onSoListItemClicked(int type, QString typeName, int index)
95
{
Rafal Kulaga's avatar
Rafal Kulaga committed
96 97
    Q_UNUSED(typeName)

98
    SkyObjItem *soitem = m_ModManager->returnModel(type)->getSkyObjItem(index);
99

100 101
//    soTypeTextObj->setProperty("text", typeName);
//    soTypeTextObj->setProperty("visible", true);
102

103
//    soListObj->setProperty("visible", false);
104

105
    loadDetailsView(soitem, index);
106 107
}

108
void WIView::onNextObjClicked()
109
{
110 111
    int modelSize = m_ModManager->returnModel(m_CurSoItem->getType())->rowCount();
    SkyObjItem *nextItem = m_ModManager->returnModel(m_CurSoItem->getType())->getSkyObjItem((m_CurIndex+1)%modelSize);
112
    loadDetailsView(nextItem, (m_CurIndex+1)%modelSize);
Samikshan Bairagya's avatar
Samikshan Bairagya committed
113
}
114 115 116

void WIView::onPrevObjClicked()
{
117 118
    int modelSize = m_ModManager->returnModel(m_CurSoItem->getType())->rowCount();
    SkyObjItem *prevItem = m_ModManager->returnModel(m_CurSoItem->getType())->getSkyObjItem((m_CurIndex-1+modelSize)%modelSize);
119 120
    loadDetailsView(prevItem, (m_CurIndex-1+modelSize)%modelSize);
}
121 122 123 124 125

void WIView::onSlewButtonClicked()
{
    ///Slew map to selected sky-object
    SkyObject* so = m_CurSoItem->getSkyObject();
126
    KStars* kstars = KStars::Instance();
127 128
    if (so != 0)
    {
129 130 131
        kstars->map()->setFocusPoint(so);
        kstars->map()->setFocusObject(so);
        kstars->map()->setDestination(*kstars->map()->focusPoint());
132 133
    }
}
134 135 136 137 138 139 140 141 142 143

void WIView::onDetailsButtonClicked()
{
    ///Code taken from WUTDialog::slotDetails()
    KStars *kstars = KStars::Instance();
    SkyObject* so = m_CurSoItem->getSkyObject();
    DetailDialog *detail = new DetailDialog(so, kstars->data()->lt(), kstars->data()->geo(), kstars);
    detail->exec();
    delete detail;
}
144 145 146 147

void WIView::onSettingsIconClicked()
{
    KStars *kstars = KStars::Instance();
148
    kstars->showWISettingsUI();
149
}
150

151 152 153 154 155
void WIView::onReloadIconClicked()
{
    updateModels(m_Obs);
}

156 157 158 159 160 161 162 163 164 165
void WIView::updateModels(ObsConditions* obs)
{
    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));
166 167 168

    if (m_CurCategorySelected >=0 && m_CurCategorySelected <= 5)
        m_Ctxt->setContextProperty("soListModel", m_ModManager->returnModel(m_CurCategorySelected));
169 170 171 172 173 174 175 176 177 178 179
}

void WIView::loadDetailsView(SkyObjItem *soitem, int index)
{
    m_CurSoItem = soitem;
    m_CurIndex = index;

    int modelSize = m_ModManager->returnModel(m_CurSoItem->getType())->rowCount();
    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);

Vijay Dhameliya's avatar
Vijay Dhameliya committed
180 181 182 183 184 185 186 187 188 189 190 191 192 193
    if(modelSize <= 1)
    {
        m_NextObj->setProperty("visible", "false");
        m_PrevObj->setProperty("visible", "false");
    }
    else
    {
        m_NextObj->setProperty("visible", "true");
        m_PrevObj->setProperty("visible", "true");
        QObject *nextTextObj = m_NextObj->findChild<QObject *>("nextTextObj");
        nextTextObj->setProperty("text", nextItem->getName());
        QObject *prevTextObj = m_PrevObj->findChild<QObject *>("prevTextObj");
        prevTextObj->setProperty("text", prevItem->getName());
    }
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209

    QObject *sonameObj = m_DetailsViewObj->findChild<QObject *>("sonameObj");
    QObject *posTextObj = m_DetailsViewObj->findChild<QObject *>("posTextObj");
    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");

    sonameObj->setProperty("text", soitem->getLongName());
    posTextObj->setProperty("text", soitem->getPosition());
    descTextObj->setProperty("text", soitem->getDesc());
    descSrcTextObj->setProperty("text", soitem->getDescSource());

    QString magText;
    if (soitem->getType() == SkyObjItem::Constellation)
Jasem Mutlaq's avatar
Jasem Mutlaq committed
210
        magText = xi18n("Magnitude:  --");
211
    else
Jasem Mutlaq's avatar
Jasem Mutlaq committed
212
        magText = xi18n("Magnitude: %1 mag", QLocale().toString(soitem->getMagnitude(), 2));
213 214
    magTextObj->setProperty("text", magText);

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

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