• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdeedu API Reference
  • KDE Home
  • Contact Us
 

kstars

  • sources
  • kde-4.12
  • kdeedu
  • kstars
  • kstars
  • skycomponents
flagcomponent.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  flagcomponent.cpp - K Desktop Planetarium
3  -------------------
4  begin : Fri 16 Jan 2009
5  copyright : (C) 2009 by Jerome SONRIER
6  email : jsid@emor3j.fr.eu.org
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 
19 #include "flagcomponent.h"
20 
21 #include <kio/job.h>
22 #include <kstandarddirs.h>
23 #include <kfileitem.h>
24 #include <klocale.h>
25 #include <qmath.h>
26 
27 #include "Options.h"
28 #include "kstarsdata.h"
29 #include "skymap.h"
30 #include "skyobjects/skypoint.h"
31 #include "ksfilereader.h"
32 #include "skypainter.h"
33 #include "kstars/projections/projector.h"
34 
35 FlagComponent::FlagComponent( SkyComposite *parent )
36  : PointListComponent(parent)
37 {
38  // List user's directory
39  m_Job = KIO::listDir( KUrl( KStandardDirs::locateLocal("appdata", ".") ), KIO::HideProgressInfo, false );
40  connect( m_Job,SIGNAL( entries(KIO::Job*, const KIO::UDSEntryList& ) ),
41  SLOT( slotLoadImages( KIO::Job*, const KIO::UDSEntryList& ) ) );
42  connect( m_Job, SIGNAL( result( KJob * ) ), this, SLOT( slotInit( KJob * ) ) );
43 }
44 
45 
46 FlagComponent::~FlagComponent()
47 {}
48 
49 void FlagComponent::draw( SkyPainter *skyp ) {
50  // Return if flags must not be draw
51  if( ! selected() )
52  return;
53 
54  // Return if no images are available
55  if( m_Names.size() < 1 )
56  return;
57 
58  // Draw all flags
59  skyp->drawFlags();
60 }
61 
62 bool FlagComponent::selected() {
63  return Options::showFlags();
64 }
65 
66 void FlagComponent::loadFromFile() {
67  bool imageFound = false;
68 
69  QList<QStringList> flagList=KStarsData::Instance()->userdb()->ReturnAllFlags();
70  for (int i=0; i<flagList.size(); ++i){
71  QStringList flagEntry = flagList.at(i);
72 
73  // Read coordinates
74  dms r( flagEntry.at( 0 ) );
75  dms d( flagEntry.at( 1 ) );
76  SkyPoint* flagPoint = new SkyPoint( r, d );
77  pointList().append( flagPoint );
78 
79  // Read epoch
80  m_Epoch.append( flagEntry.at( 2 ) );
81 
82  // Read image name
83  QString str = flagEntry.at( 3 );
84  str = str.replace( '_', ' ');
85  for(int i = 0; i < m_Names.size(); ++i ) {
86  if ( str == m_Names.at( i ) ) {
87  m_FlagImages.append( i );
88  imageFound = true;
89  }
90  }
91 
92  // If the image sprecified in db does not exist,
93  // use the default one
94  if ( ! imageFound )
95  m_FlagImages.append( 0 );
96 
97  imageFound = false;
98 
99  // If there is no label, use an empty string, red color and continue.
100  m_Labels.append(flagEntry.at(4));
101 
102  // color label
103 
104  QRegExp rxLabelColor( "^#[a-fA-F0-9]{6}$" );
105  if ( rxLabelColor.exactMatch( flagEntry.at(5) ) ) {
106  m_LabelColors.append( QColor( flagEntry.at(5) ) );
107  } else {
108  m_LabelColors.append( QColor( "red" ) );
109  }
110 
111  }
112 }
113 
114 void FlagComponent::saveToFile() {
115  /*
116  TODO: This is a really bad way of storing things. Adding one flag shouldn't
117  involve writing a new file/table every time. Needs fixing.
118  */
119  KStarsData::Instance()->userdb()->EraseAllFlags();
120 
121  for ( int i=0; i < size(); ++i ) {
122  KStarsData::Instance()->userdb()->AddFlag(QString::number( pointList().at( i )->ra0().Degrees() ),
123  QString::number( pointList().at( i )->dec0().Degrees() ),
124  epoch ( i ),
125  imageName( i ).replace( ' ', '_' ),
126  label( i ),
127  labelColor( i ).name());
128  }
129 }
130 
131 void FlagComponent::add( SkyPoint* flagPoint, QString epoch, QString image, QString label, QColor labelColor ) {
132  pointList().append( flagPoint );
133  m_Epoch.append( epoch );
134 
135  for(int i = 0; i<m_Names.size(); i++ ) {
136  if( image == m_Names.at( i ) )
137  m_FlagImages.append( i );
138  }
139 
140  m_Labels.append( label );
141  m_LabelColors.append( labelColor );
142 }
143 
144 void FlagComponent::remove( int index ) {
145  // check if flag of required index exists
146  if ( index > pointList().size() - 1 ) {
147  return;
148  }
149 
150  pointList().removeAt( index );
151  m_Epoch.removeAt( index );
152  m_FlagImages.removeAt( index );
153  m_Labels.removeAt( index );
154  m_LabelColors.removeAt( index );
155 
156  // request SkyMap update
157  SkyMap::Instance()->forceUpdate();
158 }
159 
160 void FlagComponent::updateFlag( int index, SkyPoint *flagPoint, QString epoch, QString image, QString label, QColor labelColor ) {
161  if ( index > pointList().size() -1 ) {
162  return;
163  }
164  delete pointList().at( index );
165  pointList().replace( index, flagPoint);
166 
167  m_Epoch.replace( index, epoch );
168 
169  for(int i = 0; i<m_Names.size(); i++ ) {
170  if( image == m_Names.at( i ) )
171  m_FlagImages.replace( index, i );
172  }
173 
174  m_Labels.replace( index, label );
175  m_LabelColors.replace( index, labelColor );
176 }
177 
178 void FlagComponent::slotLoadImages( KIO::Job*, const KIO::UDSEntryList& list ) {
179  // Add the default flag images to available images list
180  m_Names.append( i18n( "No icon" ) );
181  m_Images.append( QImage() );
182  m_Names.append( i18n( "Default" ) );
183  m_Images.append( QImage( KStandardDirs::locate( "appdata", "defaultflag.gif" ) ));
184 
185  // Add all other images found in user appdata directory
186  foreach( KIO::UDSEntry entry, list) {
187  KFileItem item(entry, m_Job->url(), false, true);
188  if( item.name().startsWith( "_flag" ) ) {
189  QString fileName = item.name()
190  .replace(QRegExp("\\.[^.]*$"), QString())
191  .replace(QRegExp("^_flag"), QString())
192  .replace('_',' ');
193  m_Names.append( fileName );
194  m_Images.append( QImage( item.localPath() ));
195  }
196  }
197 }
198 
199 void FlagComponent::slotInit( KJob *job ) {
200  Q_UNUSED (job)
201 
202  loadFromFile();
203 
204  // Redraw Skymap
205  SkyMap::Instance()->forceUpdate(false);
206 }
207 
208 
209 QStringList FlagComponent::getNames() {
210  return m_Names;
211 }
212 
213 int FlagComponent::size() {
214  return pointList().size();
215 }
216 
217 QString FlagComponent::epoch( int index ) {
218  if ( index > m_Epoch.size() - 1 ) {
219  return QString();
220  }
221 
222  return m_Epoch.at( index );
223 }
224 
225 QString FlagComponent::label( int index ) {
226  if ( index > m_Labels.size() - 1 ) {
227  return QString();
228  }
229 
230  return m_Labels.at( index );
231 }
232 
233 QColor FlagComponent::labelColor( int index ) {
234  if ( index > m_LabelColors.size() -1 ) {
235  return QColor();
236  }
237 
238  return m_LabelColors.at( index );
239 }
240 
241 QImage FlagComponent::image( int index ) {
242  if ( index > m_FlagImages.size() - 1 ) {
243  return QImage();
244  }
245 
246  if ( m_FlagImages.at( index ) > m_Images.size() - 1 ) {
247  return QImage();
248  }
249 
250  return m_Images.at( m_FlagImages.at( index ) );
251 }
252 
253 QString FlagComponent::imageName( int index ) {
254  if ( index > m_FlagImages.size() - 1 ) {
255  return QString();
256  }
257 
258  if ( m_FlagImages.at( index ) > m_Names.size() - 1 ) {
259  return QString();
260  }
261 
262  return m_Names.at( m_FlagImages.at( index ) );
263 }
264 
265 QList<QImage> FlagComponent::imageList() {
266  return m_Images;
267 }
268 
269 QList<int> FlagComponent::getFlagsNearPix ( SkyPoint *point, int pixelRadius )
270 {
271  const Projector *proj = SkyMap::Instance()->projector();
272  QPointF pos = proj->toScreen(point);
273  QList<int> retVal;
274 
275  int ptr = 0;
276  foreach ( SkyPoint *cp, pointList() ) {
277  QPointF pos2 = proj->toScreen(cp);
278  int dx = (pos2 - pos).x();
279  int dy = (pos2 - pos).y();
280 
281  if ( qSqrt( dx * dx + dy * dy ) <= pixelRadius ) {
282  //point is inside pixelRadius circle
283  retVal.append(ptr);
284  }
285 
286  ptr++;
287  }
288 
289  return retVal;
290 }
291 
292 QImage FlagComponent::imageList( int index ) {
293  if ( index > m_Images.size() - 1 ) {
294  return QImage();
295  }
296 
297  return m_Images.at( index );
298 }
FlagComponent::size
int size()
Return the numbers of flags.
Definition: flagcomponent.cpp:213
FlagComponent::selected
virtual bool selected()
Definition: flagcomponent.cpp:62
FlagComponent::imageName
QString imageName(int index)
Get image name.
Definition: flagcomponent.cpp:253
FlagComponent::getNames
QStringList getNames()
Return image names.
Definition: flagcomponent.cpp:209
FlagComponent
Represents a flag on the sky map.
Definition: flagcomponent.h:44
KSUserDB::ReturnAllFlags
QList< QStringList > ReturnAllFlags()
Returns a QList populated with all stored flags Order: const QString &ra, const QString &dec...
Definition: ksuserdb.cpp:288
Options::showFlags
static bool showFlags()
Get Draw flags in the sky map?
Definition: Options.h:1778
KStarsData::Instance
static KStarsData * Instance()
Definition: kstarsdata.h:92
SkyMap::forceUpdate
void forceUpdate(bool now=false)
Recalculates the positions of objects in the sky, and then repaints the sky map.
Definition: skymap.cpp:985
skypainter.h
FlagComponent::updateFlag
void updateFlag(int index, SkyPoint *flagPoint, QString epoch, QString image, QString label, QColor labelColor)
Update a flag.
Definition: flagcomponent.cpp:160
KSUserDB::EraseAllFlags
void EraseAllFlags()
Erases all the flags from the database.
Definition: ksuserdb.cpp:254
SkyPainter::drawFlags
virtual void drawFlags()=0
Draw flags.
FlagComponent::draw
virtual void draw(SkyPainter *skyp)
Draw the object on the SkyMap skyp a pointer to the SkyPainter to use.
Definition: flagcomponent.cpp:49
FlagComponent::image
QImage image(int index)
Get image.
Definition: flagcomponent.cpp:241
SkyPoint
The sky coordinates of a point in the sky.
Definition: skypoint.h:50
Projector
This class serves as an interface to handle projections.
Definition: projector.h:49
Projector::toScreen
QPointF toScreen(const SkyPoint *o, bool oRefract=true, bool *onVisibleHemisphere=0) const
This is exactly the same as toScreenVec but it returns a QPointF.
Definition: projector.cpp:93
FlagComponent::loadFromFile
void loadFromFile()
Load flags from flags.dat file.
Definition: flagcomponent.cpp:66
FlagComponent::label
QString label(int index)
Get label.
Definition: flagcomponent.cpp:225
FlagComponent::add
void add(SkyPoint *flagPoint, QString epoch, QString image, QString label, QColor labelColor)
Add a flag.
Definition: flagcomponent.cpp:131
skymap.h
dms
An angle, stored as degrees, but expressible in many ways.
Definition: dms.h:42
FlagComponent::~FlagComponent
virtual ~FlagComponent()
Destructor.
Definition: flagcomponent.cpp:46
SkyComposite
SkyComposite is a kind of container class for SkyComponent objects.
Definition: skycomposite.h:43
skypoint.h
PointListComponent::pointList
QList< SkyPoint * > & pointList()
Definition: pointlistcomponent.h:58
FlagComponent::saveToFile
void saveToFile()
Save flags to flags.dat file.
Definition: flagcomponent.cpp:114
Options.h
PointListComponent
An abstract parent class, to be inherited by SkyComponents that store a QList of SkyPoints.
Definition: pointlistcomponent.h:37
ksfilereader.h
NaN::d
const double d
Definition: nan.h:35
FlagComponent::labelColor
QColor labelColor(int index)
Get label color.
Definition: flagcomponent.cpp:233
SkyMap
This is the canvas on which the sky is painted.
Definition: skymap.h:72
projector.h
SkyMap::Instance
static SkyMap * Instance()
Definition: skymap.cpp:141
kstarsdata.h
KStarsData::userdb
KSUserDB * userdb()
Definition: kstarsdata.h:152
KSUserDB::AddFlag
void AddFlag(const QString &ra, const QString &dec, const QString &epoch, const QString &image_name, const QString &label, const QString &labelColor)
Add a new Flag with given parameters.
Definition: ksuserdb.cpp:267
FlagComponent::remove
void remove(int index)
Remove a flag.
Definition: flagcomponent.cpp:144
SkyMap::projector
const Projector * projector() const
Get the current projector.
Definition: skymap.h:264
SkyPainter
Draws things on the sky, without regard to backend.
Definition: skypainter.h:47
flagcomponent.h
FlagComponent::imageList
QList< QImage > imageList()
Get images.
Definition: flagcomponent.cpp:265
FlagComponent::epoch
QString epoch(int index)
Get epoch.
Definition: flagcomponent.cpp:217
QList
FlagComponent::FlagComponent
FlagComponent(SkyComposite *)
Constructor.
Definition: flagcomponent.cpp:35
FlagComponent::getFlagsNearPix
QList< int > getFlagsNearPix(SkyPoint *point, int pixelRadius)
Get list of flag indexes near specified SkyPoint with radius specified in pixels. ...
Definition: flagcomponent.cpp:269
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:36:19 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kstars

Skip menu "kstars"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdeedu API Reference

Skip menu "kdeedu API Reference"
  • Analitza
  •     lib
  • kalgebra
  • kalzium
  •   libscience
  • kanagram
  • kig
  •   lib
  • klettres
  • kstars
  • libkdeedu
  •   keduvocdocument
  • marble
  • parley
  • rocs
  •   App
  •   RocsCore
  •   VisualEditor
  •   stepcore

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal