00001 #include <qradiobutton.h>
00002 #include <klistview.h>
00003 #include <kpopupmenu.h>
00004 #include <klocale.h>
00005 #include <qstatusbar.h>
00006
00007 #include <vector>
00008
00009 #include "celestiacore.h"
00010 #include "celengine/simulation.h"
00011 #include "celengine/stardb.h"
00012 #include "celengine/starbrowser.h"
00013 #include "celengine/selection.h"
00014
00015 #include "celestialbrowser.h"
00016 #include "cellistviewitem.h"
00017 #include "selectionpopup.h"
00018 #include "celutil/utf8.h"
00019
00020
00021
00022
00023
00024
00025
00026
00027 CelestialBrowser::CelestialBrowser( QWidget* parent, CelestiaCore* appCore)
00028 : CelestialBrowserBase( parent, i18n("Celestial Browser"))
00029 {
00030 this->parent = dynamic_cast<KdeApp*>(parent);
00031 this->appCore = appCore;
00032 this->appSim = appCore->getSimulation();
00033 listStars->setAllColumnsShowFocus(true);
00034 listStars->setRootIsDecorated(true);
00035 listStars->setColumnAlignment(1, Qt::AlignRight);
00036 listStars->setColumnAlignment(2, Qt::AlignRight);
00037 listStars->setColumnAlignment(3, Qt::AlignRight);
00038 listStars->setShowSortIndicator(true);
00039
00040 sbrowser.setSimulation(appSim);
00041 radioNearest->setChecked(true);
00042 statusBar()->hide();
00043 }
00044
00045
00046
00047
00048 CelestialBrowser::~CelestialBrowser()
00049 {
00050
00051 }
00052
00053
00054
00055
00056 void CelestialBrowser::slotNearest(bool)
00057 {
00058 sbrowser.setPredicate(StarBrowser::NearestStars);
00059 listStars->setSorting(1);
00060 slotRefresh();
00061 }
00062
00063
00064
00065
00066 void CelestialBrowser::slotBrightest(bool)
00067 {
00068 sbrowser.setPredicate(StarBrowser::BrightestStars);
00069 listStars->setSorting(3);
00070 slotRefresh();
00071 }
00072
00073
00074
00075
00076 void CelestialBrowser::slotBrighter(bool)
00077 {
00078 sbrowser.setPredicate(StarBrowser::BrighterStars);
00079 listStars->setSorting(2);
00080 slotRefresh();
00081 }
00082
00083
00084
00085
00086 void CelestialBrowser::slotWithPlanets(bool)
00087 {
00088 sbrowser.setPredicate(StarBrowser::StarsWithPlanets);
00089 listStars->setSorting(1);
00090 slotRefresh();
00091 }
00092
00093
00094
00095
00096 void CelestialBrowser::slotRefresh()
00097 {
00098 StarDatabase* stardb = appSim->getUniverse()->getStarCatalog();
00099 sbrowser.refresh();
00100
00101 std::vector<const Star*> *stars = sbrowser.listStars(100);
00102
00103 listStars->clear();
00104 browserSel.obj = const_cast<Star*>((*stars)[0]);
00105 browserSel.type = Selection::Type_Star;
00106
00107 SolarSystemCatalog* solarSystemCatalog = appSim->getUniverse()->getSolarSystemCatalog();
00108
00109 UniversalCoord ucPos = appSim->getObserver().getPosition();
00110 Point3f obsPos( (double)ucPos.x * 1e-6,
00111 (double)ucPos.y * 1e-6,
00112 (double)ucPos.z * 1e-6);
00113
00114 for (std::vector<const Star*>::iterator i = stars->begin();
00115 i != stars->end() ;
00116 i++ )
00117 {
00118 const Star *star=(Star *)(*i);
00119
00120 Point3f starPos = star->getPosition();
00121 Vec3d v(starPos.x - obsPos.x,
00122 starPos.y - obsPos.y,
00123 starPos.z - obsPos.z);
00124 double dist = v.length();
00125
00126 QString starClass(star->getSpectralType());
00127 CelListViewItem *starItem = new CelListViewItem(listStars, stardb->getStarName(*star), dist, "ly",
00128 astro::absToAppMag(star->getAbsoluteMagnitude(), dist),
00129 star->getAbsoluteMagnitude(), starClass);
00130
00131 SolarSystemCatalog::iterator iter = solarSystemCatalog->find(star->getCatalogNumber());
00132 if (iter != solarSystemCatalog->end())
00133 {
00134 const PlanetarySystem* planets = 0;
00135 SolarSystem* solarSys = iter->second;
00136 planets = solarSys->getPlanets();
00137
00138 for (int i = 0; i < solarSys->getPlanets()->getSystemSize(); i++)
00139 {
00140 Body* body = solarSys->getPlanets()->getBody(i);
00141
00142 Point3d bodyPos = body->getHeliocentricPosition(appSim->getTime());
00143 double starBodyDist = bodyPos.distanceFromOrigin();
00144
00145 CelListViewItem *planetItem = new CelListViewItem(starItem, body->getName(true),
00146 starBodyDist / KM_PER_AU, "au",
00147 0, 0, getClassification(body->getClassification()));
00148
00149 const PlanetarySystem* satellites = body->getSatellites();
00150 if (satellites != NULL && satellites->getSystemSize() != 0)
00151 {
00152 for (int i = 0; i < satellites->getSystemSize(); i++)
00153 {
00154 Body* sat = satellites->getBody(i);
00155
00156 Point3d satPos = sat->getHeliocentricPosition(appSim->getTime());
00157 Vec3d bodySatVec(bodyPos.x - satPos.x, bodyPos.y - satPos.y, bodyPos.z - satPos.z);
00158 double bodySatDist = bodySatVec.length();
00159
00160 new CelListViewItem(planetItem, sat->getName(true),
00161 bodySatDist, "km", 0, 0, getClassification(sat->getClassification()));
00162
00163 }
00164 }
00165 }
00166 }
00167 }
00168 delete(stars);
00169 }
00170
00171
00172 QString CelestialBrowser::getClassification(int c) const{
00173 QString cl;
00174 switch(c)
00175 {
00176 case Body::Planet:
00177 cl = i18n("Planet");
00178 break;
00179 case Body::Moon:
00180 cl = i18n("Moon");
00181 break;
00182 case Body::Asteroid:
00183 cl = i18n("Asteroid");
00184 break;
00185 case Body::Comet:
00186 cl = i18n("Comet");
00187 break;
00188 case Body::Spacecraft:
00189 cl = i18n("Spacecraft");
00190 break;
00191 case Body::Unknown:
00192 default:
00193 cl = i18n("-");
00194 break;
00195 }
00196 return cl;
00197 }
00198
00199
00200 void CelestialBrowser::slotRightClickOnStar(QListViewItem* item, const QPoint& p, int col) {
00201 CelListViewItem *i = dynamic_cast<CelListViewItem*>(item);
00202 std::string name = i->getName();
00203 while ( (i = dynamic_cast<CelListViewItem*>(i->parent())) ) {
00204 name = i->getName() + "/" + name;
00205 }
00206 Selection sel = appSim->findObjectFromPath(name, true);
00207
00208 SelectionPopup popup(this, appCore, sel);
00209 popup.init();
00210 int id = popup.exec(p);
00211 popup.process(id);
00212 }
00213
00214
00215 CelListViewItem::CelListViewItem( QListView * parent, std::string _name, double _dist, const char* _dist_unit, double _app_mag, double _abs_mag, QString _type )
00216 : QListViewItem(parent),
00217 name(_name),
00218 dist(_dist),
00219 dist_unit(_dist_unit),
00220 app_mag(_app_mag),
00221 abs_mag(_abs_mag),
00222 type(_type)
00223 {
00224 char buf[20];
00225 QString label;
00226 this->setText(0, QString::fromUtf8(ReplaceGreekLetterAbbr(_name).c_str()));
00227
00228 sprintf(buf, " %.2f %s", _dist, _dist_unit);
00229 label = buf;
00230 this->setText(1, label);
00231
00232 if (_app_mag != 0) {
00233 sprintf(buf, " %.2f ", _app_mag);
00234 label = buf;
00235 this->setText(2, label);
00236 }
00237
00238 if (_abs_mag != 0) {
00239 sprintf(buf, " %.2f ", _abs_mag);
00240 label = buf;
00241 this->setText(3, label);
00242 }
00243
00244 this->setText(4, _type);
00245 }
00246
00247 CelListViewItem::CelListViewItem( QListViewItem * parent, std::string _name, double _dist, const char* _dist_unit, double _app_mag, double _abs_mag, QString _type )
00248 : QListViewItem(parent),
00249 name(_name),
00250 dist(_dist),
00251 dist_unit(_dist_unit),
00252 app_mag(_app_mag),
00253 abs_mag(_abs_mag),
00254 type(_type)
00255 {
00256 char buf[20];
00257 QString label;
00258
00259 this->setText(0, QString::fromUtf8(ReplaceGreekLetterAbbr(_name).c_str()));
00260
00261 sprintf(buf, " %.2f %s", _dist, _dist_unit);
00262 label = buf;
00263 this->setText(1, label);
00264
00265 if (_app_mag != 0) {
00266 sprintf(buf, " %.2f ", _app_mag);
00267 label = buf;
00268 this->setText(2, label);
00269 }
00270
00271 if (_abs_mag != 0) {
00272 sprintf(buf, " %.2f ", _abs_mag);
00273 label = buf;
00274 this->setText(3, label);
00275 }
00276
00277 this->setText(4, _type);
00278 }
00279
00280 CelListViewItem::~CelListViewItem() {
00281
00282 }
00283
00284 int CelListViewItem::compare ( QListViewItem * i, int col, bool ascending ) const {
00285 if (col == 0 || col > 3) {
00286 return key(col, ascending).localeAwareCompare(i->key(col, ascending));
00287 } else {
00288 if (col == 1) return (dist > dynamic_cast<CelListViewItem*>(i)->getDist()) * 2 - 1;
00289 if (col == 2) return (app_mag > dynamic_cast<CelListViewItem*>(i)->getAppMag()) * 2 - 1;
00290 if (col == 3) return (abs_mag > dynamic_cast<CelListViewItem*>(i)->getAbsMag()) * 2 - 1;
00291 }
00292 }
00293
00294
00295
00296
00297