Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

selectionpopup.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002                           selectionpopup.cpp  -  description
00003                              -------------------
00004     begin                : 2003-05-06
00005     copyright            : (C) 2002 by Christophe Teyssier
00006     email                : chris@teyssier.org
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  *                                                                         *
00011  *   This program is free software; you can redistribute it and/or modify  *
00012  *   it under the terms of the GNU General Public License as published by  *
00013  *   the Free Software Foundation; either version 2 of the License, or     *
00014  *   (at your option) any later version.                                   *
00015  *                                                                         *
00016  ***************************************************************************/
00017 
00018 
00019 #include <sstream>
00020 #include <fstream>
00021 
00022 #include "selectionpopup.h"
00023 
00024 #include <qlabel.h>
00025 
00026 #include <klocale.h>
00027 #include <kurl.h>
00028 #include <krun.h>
00029 
00030 #include "celutil/utf8.h"
00031 
00032 SelectionPopup::SelectionPopup(QWidget* parent, CelestiaCore* _appCore, Selection _sel)
00033 :KPopupMenu(parent),
00034  appCore(_appCore),
00035  sel(_sel)
00036 {
00037 
00038 }
00039 
00040 void SelectionPopup::init()
00041 {
00042     QLabel *lab = new QLabel("", this);
00043     QPalette pal( lab->palette() );
00044     pal.setColor( QPalette::Normal, QColorGroup::Background, QColor( "White" ) );
00045     pal.setColor( QPalette::Normal, QColorGroup::Foreground, QColor( "Black" ) );
00046     pal.setColor( QPalette::Inactive, QColorGroup::Foreground, QColor( "Black" ) );
00047 
00048     QFont rsFont = lab->font();
00049     rsFont.setPointSize( rsFont.pointSize() - 2 );
00050 
00051     Simulation *sim = appCore->getSimulation();
00052     Vec3d v = sel.getPosition(sim->getTime()) - sim->getObserver().getPosition();
00053 
00054     if (sel.body() != NULL)
00055     {
00056         insertTitle(QString::fromUtf8(sel.body()->getName(true).c_str()), 0, 0);
00057         insert(this, sel, MENUMAXSIZE);
00058     }
00059     else if (sel.star() != NULL)
00060     {
00061         std::string name = sim->getUniverse()->getStarCatalog()->getStarName(*sel.star());
00062 
00063         double distance = v.length() * 1e-6;
00064         char buff[50];
00065 
00066         ostringstream o;
00067 
00068         if (abs(distance) >= astro::AUtoLightYears(1000.0f))
00069             sprintf(buff, "%.3f ly", distance);
00070         else if (abs(distance) >= astro::kilometersToLightYears(10000000.0))
00071             sprintf(buff, "%.3f au", astro::lightYearsToAU(distance));
00072         else if (abs(distance) > astro::kilometersToLightYears(1.0f))
00073             sprintf(buff, "%.3f km", astro::lightYearsToKilometers(distance));
00074         else
00075             sprintf(buff, "%.3f m", astro::lightYearsToKilometers(distance) * 1000.0f);
00076 
00077         o << i18n("Distance: ") << buff << "\n";
00078 
00079         o << i18n("Abs (app) mag: ");
00080 
00081         sprintf(buff, "%.2f (%.2f)",
00082                    sel.star()->getAbsoluteMagnitude(),
00083                    astro::absToAppMag(sel.star()->getAbsoluteMagnitude(),
00084                                       (float) distance));
00085         o << buff << "\n";
00086 
00087         o << i18n("Class: ");
00088         sprintf(buff, "%s", sel.star()->getSpectralType());
00089         o << buff << "\n";
00090 
00091         o << i18n("Surface Temp: ");
00092         sprintf(buff, "%.0f K", sel.star()->getTemperature());
00093         o << buff << "\n";
00094 
00095         o << i18n("Radius: ");
00096         sprintf(buff, "%.2f Rsun", sel.star()->getRadius() / 696000.0f);
00097         o << buff;
00098 
00099         QLabel *starDetails = new QLabel(QString(o.str().c_str()), this);
00100         starDetails->setPalette(pal);
00101         starDetails->setFont(rsFont);
00102 
00103         insertTitle(QString::fromUtf8(ReplaceGreekLetterAbbr(name).c_str()), 0, 0);
00104         insertItem(starDetails);
00105         insertSeparator();
00106         insert(this, sel, MENUMAXSIZE);
00107     }
00108     else if (sel.deepsky() != NULL)
00109     {
00110         insertTitle(QString::fromUtf8(sim->getUniverse()->getDSOCatalog()->getDSOName(sel.deepsky()).c_str()), 0);
00111         insert(this, sel, MENUMAXSIZE);
00112     }
00113 
00114     if (sim->getUniverse() != NULL)
00115     {
00116         MarkerList* markers = sim->getUniverse()->getMarkers();
00117         if (markers->size() > 0)
00118         {
00119             KPopupMenu *markMenu = new KPopupMenu(this);
00120             int j=1;
00121             for (std::vector<Marker>::iterator i = markers->begin(); i < markers->end() && j < MENUMAXSIZE; i++)
00122             {
00123                 KPopupMenu *objMenu = new KPopupMenu(this);
00124                 insert(objMenu, (*i).getObject(), (2 * MENUMAXSIZE + j) * MENUMAXSIZE, false);
00125                 markMenu->insertItem(QString::fromUtf8(getSelectionName((*i).getObject())), objMenu);
00126                 j++;
00127             }
00128             insertItem(i18n("Marked objects"), markMenu);
00129         }
00130     }
00131 }
00132 
00133 void SelectionPopup::process(int id)
00134 {
00135     Simulation *sim = appCore->getSimulation();
00136 
00137     int actionId = id;
00138     actionId = id - (id / MENUMAXSIZE) * MENUMAXSIZE;
00139 
00140     int subId = id;
00141     int level = 1;
00142     while (id > MENUMAXSIZE) {
00143         id /= MENUMAXSIZE;
00144         level *= MENUMAXSIZE;
00145     }
00146     subId -= id * level;
00147 
00148     if (id == 1)
00149     {
00150         int selId = subId / MENUMAXSIZE;
00151         sel = getSelectionFromId(sel, selId);
00152     }
00153     else if (id == 2) // marked objects sub-menu
00154     {
00155         int subsubId = subId;
00156         int level = 1;
00157         while (subId > MENUMAXSIZE) {
00158             subId /= MENUMAXSIZE;
00159             level *= MENUMAXSIZE;
00160         }
00161         subsubId -= subId * level;
00162         MarkerList* markers = sim->getUniverse()->getMarkers();
00163         sel = (*markers)[subId-1].getObject();
00164         int selId = subsubId / MENUMAXSIZE;
00165         sel = getSelectionFromId(sel, selId);
00166     }
00167 
00168     if (actionId == 1) {
00169         sim->setSelection(sel);
00170         return;
00171     }
00172     if (actionId == 2) {
00173         sim->setSelection(sel);
00174         appCore->charEntered('c');
00175         return;
00176     }
00177     if (actionId == 3) {
00178         sim->setSelection(sel);
00179         appCore->charEntered('g');
00180         return;
00181     }
00182     if (actionId == 4) {
00183         sim->setSelection(sel);
00184         appCore->charEntered('f');
00185         return;
00186     }
00187     if (actionId == 5) {
00188         sim->setSelection(sel);
00189         appCore->charEntered('y');
00190         return;
00191     }
00192     if (actionId == 6) {
00193         QString url;
00194         if (sel.body() != NULL)
00195         {
00196             url = QString(sel.body()->getInfoURL().c_str());
00197             if (url == "")
00198             {
00199                 QString name = QString(sel.body()->getName().c_str()).lower();
00200                 url = QString("http://www.nineplanets.org/") + name + ".html";
00201             }
00202         }
00203         else if (sel.star() != NULL)
00204         {
00205             if (sel.star()->getCatalogNumber() != 0)
00206                 url = QString("http://simbad.u-strasbg.fr/sim-id.pl?protocol=html&Ident=HIP %1")
00207                       .arg(sel.star()->getCatalogNumber() & ~0xf0000000);
00208             else
00209                 url = QString("http://www.nineplanets.org/sun.html");
00210         }
00211         else if (sel.deepsky() != NULL)
00212         {
00213                 url = QString("http://simbad.u-strasbg.fr/sim-id.pl?protocol=html&Ident=%1")
00214                       .arg(sim->getUniverse()->getDSOCatalog()->getDSOName(sel.deepsky()).c_str());
00215         }
00216         KRun::runURL(url, "text/html");
00217         return;
00218     }
00219     if (actionId == 7)
00220     {
00221         if (sim->getUniverse() != NULL)
00222             sim->getUniverse()->unmarkObject(sel, 1);
00223         return;
00224     }
00225     if (actionId == 8)
00226     {
00227         sim->getUniverse()->unmarkAll();
00228         return;
00229     }
00230     if (actionId >= 10 && actionId <= 14)
00231     {
00232         if (sim->getUniverse() != NULL)
00233         {
00234             sim->getUniverse()->markObject(sel,
00235                                            10.0f,
00236                                            Color(0.0f, 1.0f, 0.0f, 0.9f),
00237                                            (Marker::Symbol)(actionId - 10),
00238                                            1);
00239         }
00240         return;
00241     }
00242     if (actionId == 20) {
00243         sim->getActiveObserver()->setDisplayedSurface("");
00244         return;
00245     }
00246     if (actionId > 20) {
00247         std::vector<std::string>* altSurfaces = sel.body()->getAlternateSurfaceNames();
00248         if (altSurfaces != NULL && altSurfaces->size() > actionId - 21)
00249         {
00250             sim->getActiveObserver()->setDisplayedSurface((*altSurfaces)[actionId - 21]);
00251         }
00252     }
00253 }
00254 
00255 const char* SelectionPopup::getSelectionName(const Selection& sel) const
00256 {
00257     if (sel.body() != NULL)
00258         return sel.body()->getName(true).c_str();
00259     else if (sel.star() != NULL)
00260         return appCore->getSimulation()->getUniverse()->getStarCatalog()->getStarName(*(sel.star())).c_str();
00261     else if (sel.deepsky() != NULL)
00262         return appCore->getSimulation()->getUniverse()->getDSOCatalog()->getDSOName(sel.deepsky()).c_str();
00263 
00264     return "";
00265 }
00266 
00267 Selection SelectionPopup::getSelectionFromId(Selection sel, int id) const
00268 {
00269     if (id == 0) return sel;
00270 
00271     int subId = id;
00272     int level = 1;
00273     while (id > MENUMAXSIZE) {
00274         id /= MENUMAXSIZE;
00275         level *= MENUMAXSIZE;
00276     }
00277     subId -= id * level;
00278     if (subId < 0) subId = 0;
00279 
00280     if (sel.body() != NULL)
00281     {
00282         const PlanetarySystem* satellites = sel.body()->getSatellites();
00283         if (satellites != NULL && satellites->getSystemSize() != 0)
00284         {
00285             Body* body = satellites->getBody(id - 1);
00286             Selection satSel(body);
00287             return getSelectionFromId(satSel, subId);
00288         }
00289     }
00290     else if (sel.star() != NULL)
00291     {
00292         Simulation *sim = appCore->getSimulation();
00293         SolarSystemCatalog* solarSystemCatalog = sim->getUniverse()->getSolarSystemCatalog();
00294         SolarSystemCatalog::iterator iter = solarSystemCatalog->find(sel.star()->getCatalogNumber());
00295         if (iter != solarSystemCatalog->end())
00296         {
00297             SolarSystem* solarSys = iter->second;
00298             Body* body = solarSys->getPlanets()->getBody(id - 1);
00299             Selection satSel(body);
00300             return getSelectionFromId(satSel, subId);
00301         }
00302     }
00303 
00304     return sel;
00305 }
00306 
00307 void SelectionPopup::insert(KPopupMenu *popup, Selection sel, int baseId, bool showSubObjects)
00308 {
00309     popup->insertItem(i18n("&Select"), baseId + 1);
00310     popup->insertItem(i18n("&Center"), baseId + 2);
00311     popup->insertItem(i18n("&Goto"), baseId + 3);
00312     popup->insertItem(i18n("&Follow"), baseId + 4);
00313     popup->insertItem(i18n("S&ynch Orbit"), baseId + 5);
00314     popup->insertItem(i18n("&Info"), baseId + 6);
00315     popup->insertItem(i18n("Unmark &All"), baseId + 8);
00316     if (appCore->getSimulation()->getUniverse()->isMarked(sel, 1))
00317     {
00318         popup->insertItem(i18n("&Unmark"), baseId + 7);
00319     }
00320     else
00321     {
00322         KPopupMenu *markMenu = new KPopupMenu(this);
00323         markMenu->insertItem(i18n("Diamond"), baseId + 10);
00324         markMenu->insertItem(i18n("Triangle"), baseId + 11);
00325         markMenu->insertItem(i18n("Square"), baseId + 12);
00326         markMenu->insertItem(i18n("Plus"), baseId + 13);
00327         markMenu->insertItem(i18n("X"), baseId + 14);
00328         popup->insertItem(i18n("&Mark"), markMenu);
00329     }
00330 
00331     if (showSubObjects && sel.body() != NULL)
00332     {
00333         std::vector<std::string>* altSurfaces = sel.body()->getAlternateSurfaceNames();
00334         if (altSurfaces != NULL)
00335         {
00336             if (altSurfaces->size() != NULL)
00337             {
00338                 KPopupMenu *surfaces = new KPopupMenu(this);
00339                 surfaces->insertItem(i18n("Normal"), baseId + 20);
00340                 int j=0;
00341                 for (std::vector<std::string>::const_iterator i = altSurfaces->begin();
00342                      i < altSurfaces->end() && j < MENUMAXSIZE - 1; i++, j++)
00343                 {
00344                     surfaces->insertItem(QString((*i).c_str()), baseId + 21 + j);
00345                 }
00346                 popup->insertItem(i18n("&Alternate Surfaces"), surfaces);
00347             }
00348             delete altSurfaces;
00349         }
00350 
00351         const PlanetarySystem* satellites = sel.body()->getSatellites();
00352         if (satellites != NULL && satellites->getSystemSize() != 0)
00353         {
00354             popup->insertSeparator();
00355             KPopupMenu *planetaryMenu = new KPopupMenu(this);
00356             for (int i = 0; i < satellites->getSystemSize() && i < MENUMAXSIZE; i++)
00357             {
00358                 Body* body = satellites->getBody(i);
00359                 Selection satSel(body);
00360                 KPopupMenu *satMenu = new KPopupMenu(this);
00361                 insert(satMenu, satSel, baseId * MENUMAXSIZE + (i + 1) * MENUMAXSIZE);
00362                 planetaryMenu->insertItem(QString::fromUtf8(body->getName(true).c_str()), satMenu);
00363             }
00364             popup->insertItem(i18n("Satellites"), planetaryMenu);
00365         }
00366     }
00367     else if (showSubObjects && sel.star() != NULL)
00368     {
00369         popup->setItemEnabled(baseId + 5, false);
00370         Simulation *sim = appCore->getSimulation();
00371         SolarSystemCatalog* solarSystemCatalog = sim->getUniverse()->getSolarSystemCatalog();
00372         SolarSystemCatalog::iterator iter = solarSystemCatalog->find(sel.star()->getCatalogNumber());
00373         if (iter != solarSystemCatalog->end())
00374         {
00375             popup->insertSeparator();
00376             SolarSystem* solarSys = iter->second;
00377             KPopupMenu* planetsMenu = new KPopupMenu(this);
00378             for (int i = 0; i < solarSys->getPlanets()->getSystemSize() && i < MENUMAXSIZE; i++)
00379             {
00380                 Body* body = solarSys->getPlanets()->getBody(i);
00381                 Selection satSel(body);
00382                 KPopupMenu *satMenu = new KPopupMenu(this);
00383                 insert(satMenu, satSel, baseId * MENUMAXSIZE + (i + 1) * MENUMAXSIZE);
00384                 planetsMenu->insertItem(QString::fromUtf8(body->getName(true).c_str()), satMenu);
00385             }
00386             popup->insertItem(i18n("Planets"), planetsMenu);
00387         }
00388     }
00389     else if (sel.deepsky() != NULL)
00390     {
00391         popup->setItemEnabled(baseId + 5, false);
00392     }
00393 }
00394 

Generated on Sat Jan 14 22:30:31 2006 for Celestia by  doxygen 1.4.1