wiview.cpp 8.06 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
#include "QDeclarativeView"
20
#include "QGraphicsObject"
21
#include "wiview.h"
22
#include "skymap.h"
23
#include "dialogs/detaildialog.h"
24

Samikshan Bairagya's avatar
Samikshan Bairagya committed
25
#include "kstandarddirs.h"
26
#include "kdeclarative.h"
Samikshan Bairagya's avatar
Samikshan Bairagya committed
27

28
WIView::WIView(QWidget *parent, ObsConditions *obs) : QWidget(parent), m_Obs(obs), m_CurCategorySelected(-1)
29
{
30
    m_ModManager = new ModelManager(m_Obs);
31

32
    m_BaseView = new QDeclarativeView();
33

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

40
    m_Ctxt = m_BaseView->rootContext();
41

42
    m_BaseView->setSource(KStandardDirs::locate("appdata","tools/whatsinteresting/qml/wiview.qml"));
43

44
    m_BaseObj = dynamic_cast<QObject *>(m_BaseView->rootObject());
45

46
    //soTypeTextObj = m_BaseObj->findChild<QObject *>("soTypeTextObj");
47

48 49
    m_ViewsRowObj = m_BaseObj->findChild<QObject *>("viewsRowObj");
    connect(m_ViewsRowObj, SIGNAL(categorySelected(int)), this, SLOT(onCategorySelected(int)));
50

51 52
    m_SoListObj = m_BaseObj->findChild<QObject *>("soListObj");
    connect(m_SoListObj, SIGNAL(soListItemClicked(int, QString, int)), this, SLOT(onSoListItemClicked(int, QString, int)));
53

54
    m_DetailsViewObj = m_BaseObj->findChild<QObject *>("detailsViewObj");
55

56
    m_NextObj = m_BaseObj->findChild<QObject *>("nextObj");
57 58 59
    connect(m_NextObj, SIGNAL(nextObjClicked()), this, SLOT(onNextObjClicked()));
    m_PrevObj = m_BaseObj->findChild<QObject *>("prevObj");
    connect(m_PrevObj, SIGNAL(prevObjClicked()), this, SLOT(onPrevObjClicked()));
60

61 62 63
    m_SlewButtonObj = m_BaseObj->findChild<QObject *>("slewButtonObj");
    connect(m_SlewButtonObj, SIGNAL(slewButtonClicked()), this, SLOT(onSlewButtonClicked()));

64 65 66
    m_DetailsButtonObj = m_BaseObj->findChild<QObject *>("detailsButtonObj");
    connect(m_DetailsButtonObj, SIGNAL(detailsButtonClicked()), this, SLOT(onDetailsButtonClicked()));

67 68 69
    QObject *settingsIconObj = m_BaseObj->findChild<QObject *>("settingsIconObj");
    connect(settingsIconObj, SIGNAL(settingsIconClicked()), this, SLOT(onSettingsIconClicked()));

70 71 72
    QObject *reloadIconObj = m_BaseObj->findChild<QObject *>("reloadIconObj");
    connect(reloadIconObj, SIGNAL(reloadIconClicked()), this, SLOT(onReloadIconClicked()));

73 74
    m_BaseView->setResizeMode(QDeclarativeView::SizeRootObjectToView);
    m_BaseView->show();
75 76
}

Samikshan Bairagya's avatar
Samikshan Bairagya committed
77 78
WIView::~WIView()
{
79
    delete m_ModManager;
80
    delete m_CurSoItem;
Samikshan Bairagya's avatar
Samikshan Bairagya committed
81
}
82

83
void WIView::onCategorySelected(int type)
84
{
85
    m_CurCategorySelected = type;
86
    m_Ctxt->setContextProperty("soListModel", m_ModManager->returnModel(type));
87
}
88

89
void WIView::onSoListItemClicked(int type, QString typeName, int index)
90
{
Rafal Kulaga's avatar
Rafal Kulaga committed
91 92
    Q_UNUSED(typeName)

93
    SkyObjItem *soitem = m_ModManager->returnModel(type)->getSkyObjItem(index);
94

95 96
//    soTypeTextObj->setProperty("text", typeName);
//    soTypeTextObj->setProperty("visible", true);
97

98
//    soListObj->setProperty("visible", false);
99

100
    loadDetailsView(soitem, index);
101 102
}

103
void WIView::onNextObjClicked()
104
{
105 106
    int modelSize = m_ModManager->returnModel(m_CurSoItem->getType())->rowCount();
    SkyObjItem *nextItem = m_ModManager->returnModel(m_CurSoItem->getType())->getSkyObjItem((m_CurIndex+1)%modelSize);
107
    loadDetailsView(nextItem, (m_CurIndex+1)%modelSize);
Samikshan Bairagya's avatar
Samikshan Bairagya committed
108
}
109 110 111

void WIView::onPrevObjClicked()
{
112 113
    int modelSize = m_ModManager->returnModel(m_CurSoItem->getType())->rowCount();
    SkyObjItem *prevItem = m_ModManager->returnModel(m_CurSoItem->getType())->getSkyObjItem((m_CurIndex-1+modelSize)%modelSize);
114 115
    loadDetailsView(prevItem, (m_CurIndex-1+modelSize)%modelSize);
}
116 117 118 119 120

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

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;
}
139 140 141 142

void WIView::onSettingsIconClicked()
{
    KStars *kstars = KStars::Instance();
143
    kstars->showWISettingsUI();
144
}
145

146 147 148 149 150
void WIView::onReloadIconClicked()
{
    updateModels(m_Obs);
}

151 152 153 154 155 156 157 158 159 160
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));
161 162 163

    if (m_CurCategorySelected >=0 && m_CurCategorySelected <= 5)
        m_Ctxt->setContextProperty("soListModel", m_ModManager->returnModel(m_CurCategorySelected));
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
}

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);

    QObject *nextTextObj = m_NextObj->findChild<QObject *>("nextTextObj");
    nextTextObj->setProperty("text", nextItem->getName());
    QObject *prevTextObj = m_PrevObj->findChild<QObject *>("prevTextObj");
    prevTextObj->setProperty("text", prevItem->getName());

    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)
        magText = i18n("Magnitude:  --");
    else
Pino Toscano's avatar
Pino Toscano committed
197
        magText = i18n("Magnitude: %1 mag", KGlobal::locale()->formatNumber(soitem->getMagnitude(), 2));
198 199
    magTextObj->setProperty("text", magText);

Pino Toscano's avatar
Pino Toscano committed
200
    QString sbText = i18n("Surface Brightness: %1", soitem->getSurfaceBrightness());
201 202
    sbTextObj->setProperty("text", sbText);

Pino Toscano's avatar
Pino Toscano committed
203
    QString sizeText = i18n("Size: %1", soitem->getSize());
204 205
    sizeTextObj->setProperty("text", sizeText);
}