Commit 7553ba2c authored by Samikshan Bairagya's avatar Samikshan Bairagya

Code improvements: Use enumns with switch-case instead of matching strings in if-else.

parent 443786b6
......@@ -118,13 +118,12 @@ Rectangle {
objectName: "soListObj"
anchors.fill: parent
signal soListItemClicked( string type, int curIndex )
signal soListItemClicked( int type, string typeName, int curIndex )
clip: true
visible: false
delegate: Item {
id: soListItem
objectName: type
x: 5
height: 40
Text {
......@@ -138,7 +137,7 @@ Rectangle {
anchors.fill: parent
onClicked: {
soListView.currentIndex = index
soListView.soListItemClicked( soListItem.objectName , soListView.currentIndex)
soListView.soListItemClicked(type, typeName, soListView.currentIndex)
}
}
}
......
......@@ -37,9 +37,10 @@ WIView::WIView ( QObject *parent, ObsConditions *obs) : QObject(parent)
catListObj = baseObj->findChild<QObject *>("catListObj");
connect(catListObj, SIGNAL(catListItemClicked(QString)), this, SLOT(onCatListItemClicked(QString)));
soListObj = baseObj->findChild<QObject *>("soListObj");
connect(soListObj, SIGNAL(soListItemClicked(QString, int)),
this, SLOT(onSoListItemClicked(QString, int)));
connect(soListObj, SIGNAL(soListItemClicked(int, QString, int)), this, SLOT(onSoListItemClicked(int, QString, int)));
detailsViewObj = baseObj->findChild<QObject *>("detailsViewObj");
nextObj = baseObj->findChild<QObject *>("nextObj");
connect(nextObj, SIGNAL(nextObjTextClicked()), this, SLOT(onNextObjTextClicked()));
......@@ -51,6 +52,7 @@ WIView::WIView ( QObject *parent, ObsConditions *obs) : QObject(parent)
WIView::~WIView()
{
delete m;
delete curSoItem;
}
void WIView::onCatListItemClicked(QString category)
......@@ -117,22 +119,44 @@ void WIView::onCatListItemClicked(QString category)
}
}
void WIView::onSoListItemClicked(QString type, int index)
void WIView::onSoListItemClicked(int type, QString typeName, int index)
{
SkyObjItem *soitem;
if (type == "Star")
kDebug()<<type;
switch (type)
{
case 0:
soitem = m->returnModel(ModelManager::Planets)->getSkyObjItem(index);
break;
case 1:
soitem = m->returnModel(ModelManager::Stars)->getSkyObjItem(index);
else if (type == "Constellation")
break;
case 2:
soitem = m->returnModel(ModelManager::Galaxies)->getSkyObjItem(index);
break;
case 3:
soitem = m->returnModel(ModelManager::Constellations)->getSkyObjItem(index);
else if (type == "Planet")
soitem = m->returnModel(ModelManager::Planets)->getSkyObjItem(index);
else
{
break;
default:
kDebug()<<"Nothing for DSOs right now";
return;
}
// if (type == "Star")
// soitem = m->returnModel(ModelManager::Stars)->getSkyObjItem(index);
// else if (type == "Constellation")
// soitem = m->returnModel(ModelManager::Constellations)->getSkyObjItem(index);
// else if (type == "Planet")
// soitem = m->returnModel(ModelManager::Planets)->getSkyObjItem(index);
// else
// {
// kDebug()<<"Nothing for DSOs right now";
// return;
// }
kDebug()<<soitem->getName()<<soitem->getType();
soTypeTextObj->setProperty("text", type);
soTypeTextObj->setProperty("text", typeName);
soTypeTextObj->setProperty("visible", true);
soListObj->setProperty("visible", false);
......@@ -167,7 +191,7 @@ void WIView::loadDetailsView(SkyObjItem* soitem, int index)
void WIView::onNextObjTextClicked()
{
int modelSize = m->returnModel(curSoItem->getType())->rowCount();
SkyObjItem *nextItem = m->returnModel(curSoItem->getType())->getSkyObjItem((curIndex+1)%modelSize);
int modelSize = m->returnModel(curSoItem->getTypeName())->rowCount();
SkyObjItem *nextItem = m->returnModel(curSoItem->getTypeName())->getSkyObjItem((curIndex+1)%modelSize);
loadDetailsView(nextItem, (curIndex+1)%modelSize);
}
......@@ -29,13 +29,13 @@ class WIView : public QObject
public:
WIView(QObject *parent = 0, ObsConditions *obs = 0);
~WIView();
void loadDetailsView( SkyObjItem* soitem, int index);
void loadDetailsView(SkyObjItem* soitem, int index);
public slots:
// void loadModel ( int TYPE );
// void skyObjectItemClicked(QModelIndex index);
void onCatListItemClicked(QString);
void onSoListItemClicked(QString, int index);
void onSoListItemClicked(int type, QString typeName, int index);
void onNextObjTextClicked();
private:
QObject *baseObj, *catListObj, *soListObj, *detailsViewObj, *nextObj, *soTypeTextObj;
......@@ -50,4 +50,4 @@ private:
ModelManager *m;
SkyObjItem *curSoItem;
int curIndex;
};
\ No newline at end of file
};
......@@ -19,12 +19,29 @@
#include "kstarsdata.h"
#include "skyobjitem.h"
SkyObjItem::SkyObjItem(SkyObject* sobj, QObject* parent) : QObject(parent)
SkyObjItem::SkyObjItem(SkyObject* sobj, QObject* parent) : QObject(parent),
m_Name(sobj->name()), m_TypeName(sobj->typeName())
{
so = sobj;
name = sobj->name();
type = sobj->typeName();
setPosition(so);
switch (sobj->type())
{
case SkyObject::PLANET:
m_Type = Planet;
break;
case SkyObject::STAR:
m_Type = Star;
break;
case SkyObject::CONSTELLATION:
m_Type = Constellation;
break;
case SkyObject::OPEN_CLUSTER:
m_Type = Star_Cluster;
break;
case SkyObject::PLANETARY_NEBULA:
m_Type = Planetary_Nebula;
break;
}
setPosition(so=sobj);
}
QVariant SkyObjItem::data(int role)
......@@ -35,6 +52,8 @@ QVariant SkyObjItem::data(int role)
return getName();
case CategoryRole:
return getType();
case CategoryNameRole:
return getTypeName();
default:
return QVariant();
}
......@@ -45,12 +64,13 @@ QHash< int, QByteArray > SkyObjItem::roleNames() const
QHash<int, QByteArray > roles;
roles[DispNameRole]="dispName";
roles[CategoryRole] = "type";
roles[CategoryNameRole] = "typeName";
return roles;
}
void SkyObjItem::setPosition(SkyObject* so)
{
QString cardinals[] = {
const QString cardinals[] = {
"N", "NNE", "NE", "ENE",
"E", "ESE", "SE", "SSE",
"S", "SSW", "SW", "WSW",
......@@ -65,12 +85,13 @@ void SkyObjItem::setPosition(SkyObject* so)
double rounded_altitude = (int)(sp.alt().Degrees()/5.0)*5.0;
int rounded_azimuth = (int)(sp.az().Degrees()/22.5);
position = QString("Now visible: ").append(QString::number(rounded_altitude)).append(" degrees above the ").append(cardinals[rounded_azimuth]).append(" horizon ");
m_Position = QString("Now visible: ")+(QString::number(rounded_altitude))+(" degrees above the ")+(cardinals[rounded_azimuth])+(" horizon ");
}
QString SkyObjItem::getDesc()
QString SkyObjItem::getDesc() const
{
if (type == "Planet"){
if (m_TypeName == "Planet")
{
KSFileReader fileReader;
if ( !fileReader.open("PlanetFacts.dat") )
return QString("No Description found for selected sky-object");
......@@ -78,15 +99,15 @@ QString SkyObjItem::getDesc()
while ( fileReader.hasMoreLines() )
{
QString line = fileReader.readLine();
if (line.split("::")[0] == name)
if (line.split("::")[0] == m_Name)
return line.split("::")[1];
}
}
else if (type == "Star")
else if (m_TypeName == "Star")
{
return "Bright Star";
}
else if (type == "Constellation")
else if (m_TypeName == "Constellation")
{
return "Constellation";
}
......@@ -94,9 +115,7 @@ QString SkyObjItem::getDesc()
return QString("No Description found for selected sky-object");
}
QString SkyObjItem::getMagnitude()
QString SkyObjItem::getMagnitude() const
{
QString magtext = "Magnitude : ";
magtext.append(QString::number(so->mag()));
return magtext;
return QString("Magnitude : ")+(QString::number(so->mag()));
}
......@@ -26,29 +26,25 @@ class SkyObjItem : public QObject
{
Q_OBJECT
public:
enum SkyObjectRoles {DispNameRole = Qt::UserRole + 1 , CategoryRole };
enum Positions {
N, NNE, NE, ENE,
E, ESE, SE, SSE,
S, SSW, SW, WSW,
W, WNW, NW, NNW
};
//explicit SkyObjItem( SkyObject *so=0, QString soname = QString(), QString sotype = QString(), QObject *parent = 0);
enum SkyObjectRoles {DispNameRole = Qt::UserRole + 1 , CategoryRole, CategoryNameRole };
enum Type {Planet, Star, Galaxy, Constellation, Star_Cluster, Planetary_Nebula};
explicit SkyObjItem( SkyObject *sobj=0, QObject *parent = 0);
QVariant data(int role);
QHash<int, QByteArray> roleNames() const;
inline QString getName() { return name; }
inline QString getType() { return type; }
inline QString getPosition() { return position; }
inline QString getName() const { return m_Name; }
inline Type getType() const { return m_Type; }
inline QString getTypeName() const { return m_TypeName; }
inline QString getPosition() const { return m_Position; }
inline SkyObject* getSkyObject() { return so; }
QString getDesc();
QString getMagnitude();
QString getDesc() const;
QString getMagnitude() const;
void setPosition(SkyObject* so);
private:
QString name;
QString type;
QString position;
QString m_Name;
QString m_TypeName;
QString m_Position;
Type m_Type;
SkyObject* so;
};
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment