• 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
  • printing
legend.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  legend.cpp - K Desktop Planetarium
3  -------------------
4  begin : Thu May 26 2011
5  copyright : (C) 2011 by Rafał Kułaga
6  email : rl.kulaga@gmail.com
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 #include "legend.h"
19 #include "skyqpainter.h"
20 #include "skymap.h"
21 #include "kstarsdata.h"
22 #include "Options.h"
23 #include "colorscheme.h"
24 
25 #include <QBrush>
26 
27 namespace
28 {
29  const int symbolSize = 15;
30  const int bRectWidth = 100;
31  const int bRectHeight = 45;
32  const int maxHScalePixels = 200;
33  const int maxVScalePixels = 100;
34  const int xSymbolSpacing = 100;
35  const int ySymbolSpacing = 70;
36 }
37 
38 Legend::Legend(LEGEND_ORIENTATION orientation, LEGEND_POSITION pos)
39  : m_Painter(0), m_SkyMap(SkyMap::Instance()), m_DeletePainter(false), m_Type(LT_FULL),
40  m_Orientation(orientation), m_Position(pos), m_PositionFloating(QPoint(0, 0)),
41  m_cScheme(KStarsData::Instance()->colorScheme()), m_DrawFrame(false), m_SymbolSize(symbolSize),
42  m_BRectWidth(bRectWidth), m_BRectHeight(bRectHeight), m_MaxHScalePixels(maxHScalePixels),
43  m_MaxVScalePixels(maxVScalePixels), m_XSymbolSpacing(xSymbolSpacing), m_YSymbolSpacing(ySymbolSpacing)
44 {
45  m_BgColor = m_cScheme->colorNamed("SkyColor");
46 }
47 
48 Legend::~Legend()
49 {
50  if(m_Painter && m_DeletePainter)
51  {
52  delete m_Painter;
53  }
54 }
55 
56 QSize Legend::calculateSize()
57 {
58  int width = 0;
59  int height = 0;
60 
61  switch(m_Orientation)
62  {
63  case LO_HORIZONTAL:
64  {
65  switch(m_Type)
66  {
67  case LT_SCALE_ONLY:
68  {
69  width = 40 + m_MaxHScalePixels;
70  height = 60;
71  break;
72  }
73 
74  case LT_MAGNITUDES_ONLY:
75  {
76  width = 140;
77  height = 70;
78  break;
79  }
80 
81  case LT_SYMBOLS_ONLY:
82  {
83  width = 7 * m_XSymbolSpacing;
84  height = 20 + m_SymbolSize + m_BRectHeight;
85  break;
86  }
87 
88  case LT_SCALE_MAGNITUDES:
89  {
90  width = 160 + m_MaxHScalePixels;
91  height = 70;
92  break;
93  }
94 
95  case LT_FULL:
96  {
97  width = 7 * m_XSymbolSpacing;
98  height = 20 + m_SymbolSize + m_BRectHeight + 70;
99  break;
100  }
101 
102  default: break; // should never happen
103  }
104  }
105 
106  break;
107 
108  case LO_VERTICAL:
109  {
110  switch(m_Type)
111  {
112  case LT_SCALE_ONLY:
113  {
114  width = 120;
115  height = 40 + m_MaxVScalePixels;
116  break;
117  }
118 
119  case LT_MAGNITUDES_ONLY:
120  {
121  width = 140;
122  height = 70;
123  break;
124  }
125 
126  case LT_SYMBOLS_ONLY:
127  {
128  width = 120;
129  height = 7 * m_YSymbolSpacing;
130  break;
131  }
132 
133  case LT_SCALE_MAGNITUDES:
134  {
135  width = 120;
136  height = 100 + m_MaxVScalePixels;
137  break;
138  }
139 
140  case LT_FULL:
141  {
142  width = 120;
143  height = 100 + 7 * m_YSymbolSpacing + m_MaxVScalePixels;
144  break;
145  }
146 
147  default: break; // should never happen
148  }
149 
150  break;
151  }
152 
153  default:
154  {
155  return QSize();
156  }
157  }
158 
159  return QSize(width, height);
160 }
161 
162 void Legend::paintLegend(QPaintDevice *pd)
163 {
164  if(m_Painter)
165  {
166  delete m_Painter;
167  }
168 
169  m_Painter = new SkyQPainter(m_SkyMap, pd);
170  m_DeletePainter = true;
171  m_Painter->begin();
172 
173  paintLegend(m_Painter);
174 
175  m_Painter->end();
176 }
177 
178 void Legend::paintLegend(SkyQPainter *painter)
179 {
180  if(!m_DeletePainter)
181  {
182  m_Painter = painter;
183  }
184 
185  if(m_Position != LP_FLOATING)
186  {
187  m_PositionFloating = positionToDeviceCoord(painter->device());
188  }
189 
190  m_Painter->translate(m_PositionFloating.x(), m_PositionFloating.y());
191 
192  m_Painter->setFont(m_Font);
193 
194  QBrush backgroundBrush(m_BgColor, Qt::SolidPattern);
195  QPen backgroundPen(m_cScheme->colorNamed("SNameColor"));
196  backgroundPen.setStyle(Qt::SolidLine);
197 
198  // set brush & pen
199  m_Painter->setBrush(backgroundBrush);
200  m_Painter->setPen(backgroundPen);
201 
202  QSize size = calculateSize();
203  if(m_DrawFrame)
204  {
205  m_Painter->drawRect(1, 1, size.width(), size.height());
206  }
207 
208  else
209  {
210  QPen noLinePen;
211  noLinePen.setStyle(Qt::NoPen);
212 
213  m_Painter->setPen(noLinePen);
214  m_Painter->drawRect(1, 1, size.width(), size.height());
215 
216  m_Painter->setPen(backgroundPen);
217  }
218 
219  switch(m_Orientation)
220  {
221  case LO_HORIZONTAL:
222  {
223  switch(m_Type)
224  {
225  case LT_SCALE_ONLY:
226  {
227  paintScale(QPointF(20, 20));
228  break;
229  }
230 
231  case LT_MAGNITUDES_ONLY:
232  {
233  paintMagnitudes(QPointF(20, 20));
234  break;
235  }
236 
237  case LT_SYMBOLS_ONLY:
238  {
239  paintSymbols(QPointF(20, 20));
240  break;
241  }
242 
243  case LT_SCALE_MAGNITUDES:
244  {
245  paintMagnitudes(QPointF(20, 20));
246  paintScale(QPointF(150, 20));
247  break;
248  }
249 
250  case LT_FULL:
251  {
252  paintSymbols(QPointF(20, 20));
253  paintMagnitudes(QPointF(10, 40 + m_SymbolSize + m_BRectHeight));
254  paintScale(QPointF(200, 40 + m_SymbolSize + m_BRectHeight));
255  break;
256  }
257 
258  default: break; // should never happen
259  }
260 
261  break;
262  }
263 
264  case LO_VERTICAL:
265  {
266  switch(m_Type)
267  {
268  case LT_SCALE_ONLY:
269  {
270  paintScale(QPointF(20, 20));
271  break;
272  }
273 
274  case LT_MAGNITUDES_ONLY:
275  {
276  paintMagnitudes(QPointF(20, 20));
277  break;
278  }
279 
280  case LT_SYMBOLS_ONLY:
281  {
282  paintSymbols(QPointF(20, 20));
283  break;
284  }
285 
286  case LT_SCALE_MAGNITUDES:
287  {
288  paintMagnitudes(QPointF(7, 20));
289  paintScale(QPointF(20, 80));
290  break;
291  }
292 
293  case LT_FULL:
294  {
295  paintSymbols(QPointF(30, 20));
296  paintMagnitudes(QPointF(7, 30 + 7 * m_YSymbolSpacing));
297  paintScale(QPointF(20, 90 + 7 * m_YSymbolSpacing));
298  break;
299  }
300 
301  default: break; // should never happen
302  }
303 
304  break;
305  }
306 
307  default: break; // should never happen
308  }
309 }
310 
311 void Legend::paintLegend(QPaintDevice *pd, LEGEND_TYPE type, LEGEND_POSITION pos)
312 {
313  LEGEND_TYPE prevType = m_Type;
314  LEGEND_POSITION prevPos = m_Position;
315 
316  m_Type = type;
317  m_Position = pos;
318 
319  paintLegend(pd);
320 
321  m_Type = prevType;
322  m_Position = prevPos;
323 }
324 
325 void Legend::paintLegend(SkyQPainter *painter, LEGEND_TYPE type, LEGEND_POSITION pos)
326 {
327  LEGEND_TYPE prevType = m_Type;
328  LEGEND_POSITION prevPos = m_Position;
329 
330  m_Type = type;
331  m_Position = pos;
332 
333  paintLegend(painter);
334 
335  m_Type = prevType;
336  m_Position = prevPos;
337 }
338 
339 void Legend::paintSymbols(QPointF pos)
340 {
341  qreal x = pos.x();
342  qreal y = pos.y();
343 
344  x += 30;
345 
346  switch(m_Orientation)
347  {
348  case Legend::LO_HORIZONTAL :
349  {
350  // paint Open Cluster/Asterism symbol
351  QString label1 = i18n("Open Cluster") + "\n" + i18n("Asterism");
352  paintSymbol(QPointF(x, y), 3, 1, 0, label1);
353  x += m_XSymbolSpacing;
354 
355  // paint Globular Cluster symbol
356  paintSymbol(QPointF(x, y), 4, 1, 0, i18n("Globular Cluster"));
357  x += m_XSymbolSpacing;
358 
359  // paint Gaseous Nebula/Dark Nebula symbol
360  QString label3 = i18n("Gaseous Nebula") + "\n" + i18n("Dark Nebula");
361  paintSymbol(QPointF(x, y), 5, 1, 0, label3);
362  x += m_XSymbolSpacing;
363 
364  // paint Planetary Nebula symbol
365  paintSymbol(QPointF(x, y), 6, 1, 0, i18n("Planetary Nebula"));
366  x += m_XSymbolSpacing;
367 
368  // paint Supernova Remnant
369  paintSymbol(QPointF(x, y), 7, 1, 0, i18n("Supernova Remnant"));
370  x += m_XSymbolSpacing;
371 
372  // paint Galaxy/Quasar
373  QString label6 = i18n("Galaxy") + "\n" + i18n("Quasar");
374  paintSymbol(QPointF(x, y), 8, 0.5, 60, label6);
375  x += m_XSymbolSpacing;
376 
377  // paint Galaxy Cluster
378  paintSymbol(QPointF(x, y), 14, 1, 0, i18n("Galactic Cluster"));
379 
380  break;
381  }
382 
383  case Legend::LO_VERTICAL :
384  {
385  // paint Open Cluster/Asterism symbol
386  QString label1 = i18n("Open Cluster") + "\n" + i18n("Asterism");
387  paintSymbol(QPointF(x, y), 3, 1, 0, label1);
388  y += m_YSymbolSpacing;
389 
390  // paint Globular Cluster symbol
391  paintSymbol(QPointF(x, y), 4, 1, 0, i18n("Globular Cluster"));
392  y += m_YSymbolSpacing;
393 
394  // paint Gaseous Nebula/Dark Nebula symbol
395  QString label3 = i18n("Gaseous Nebula") + "\n" + i18n("Dark Nebula");
396  paintSymbol(QPointF(x, y), 5, 1, 0, label3);
397  y += m_YSymbolSpacing;
398 
399  // paint Planetary Nebula symbol
400  paintSymbol(QPointF(x, y), 6, 1, 0, i18n("Planetary Nebula"));
401  y += m_YSymbolSpacing;
402 
403  // paint Supernova Remnant
404  paintSymbol(QPointF(x, y), 7, 1, 0, i18n("Supernova Remnant"));
405  y += m_YSymbolSpacing;
406 
407  // paint Galaxy/Quasar
408  QString label6 = i18n("Galaxy") + "\n" + i18n("Quasar");
409  paintSymbol(QPointF(x, y), 8, 0.5, 60, label6);
410  y += m_YSymbolSpacing;
411 
412  // paint Galaxy Cluster
413  paintSymbol(QPointF(x, y), 14, 1, 0, i18n("Galactic Cluster"));
414 
415  break;
416  }
417  default : return; // should never happen
418  }
419 }
420 
421 void Legend::paintSymbol(QPointF pos, int type, float e, float angle, QString label)
422 {
423  qreal x = pos.x();
424  qreal y = pos.y();
425  qreal bRectHalfWidth = m_BRectWidth / 2;
426 
427  // paint symbol
428  m_Painter->drawDeepSkySymbol(pos, type, m_SymbolSize, e, angle);
429  QRectF bRect(QPoint(x - bRectHalfWidth, y + m_SymbolSize), QPoint(x + bRectHalfWidth, y + m_SymbolSize + m_BRectHeight));
430  //m_Painter->drawRect(bRect);
431  // paint label
432  m_Painter->drawText(bRect, label, QTextOption(Qt::AlignHCenter));
433 }
434 
435 void Legend::paintMagnitudes(QPointF pos)
436 {
437  qreal x = pos.x();
438  qreal y = pos.y();
439 
440  m_Painter->drawText(x, y, i18n("Star Magnitudes:"));
441  y += 15;
442 
443  for(int i = 1; i <= 9; i += 2)
444  {
445  m_Painter->drawPointSource(QPointF(x + i * 10, y), m_Painter->starWidth(i));
446  m_Painter->drawText(x + i * 10 - 4, y + 20, QString::number(i));
447  }
448 }
449 
450 void Legend::paintScale(QPointF pos)
451 {
452  qreal maxScalePixels;
453 
454  switch(m_Orientation)
455  {
456  case LO_HORIZONTAL:
457  {
458  maxScalePixels = m_MaxHScalePixels;
459  break;
460  }
461 
462  case LO_VERTICAL:
463  {
464  maxScalePixels = m_MaxVScalePixels;
465  break;
466  }
467 
468  default: return; // should never happen
469  }
470 
471  qreal maxArcsec = maxScalePixels * 57.3 * 3600 / Options::zoomFactor();
472 
473  int deg = 0;
474  int arcmin = 0;
475  int arcsec = 0;
476 
477  QString lab;
478  if(maxArcsec >= 3600)
479  {
480  deg = maxArcsec / 3600;
481  lab = QString::number(deg) + QString::fromWCharArray(L"\u00B0");
482  }
483 
484  else if(maxArcsec >= 60)
485  {
486  arcmin = maxArcsec / 60;
487  lab = QString::number(arcmin) + '\'';
488  }
489 
490  else
491  {
492  arcsec = maxArcsec;
493  lab = QString::number(arcsec) + "\"";
494  }
495 
496  int actualArcsec = 3600 * deg + 60 * arcmin + arcsec;
497 
498  qreal size = actualArcsec * Options::zoomFactor() / 57.3 / 3600;
499 
500  qreal x = pos.x();
501  qreal y = pos.y();
502 
503  switch(m_Orientation)
504  {
505  case LO_HORIZONTAL:
506  {
507  m_Painter->drawText(pos, i18n("Chart Scale:"));
508  y += 15;
509 
510  m_Painter->drawLine(x, y, x + size, y);
511  // paint line endings
512  m_Painter->drawLine(x, y - 5, x, y + 5);
513  m_Painter->drawLine(x + size, y - 5, x + size, y + 5);
514 
515  // paint scale text
516  QRectF bRect(QPoint(x, y), QPoint(x + size, y + 20));
517  m_Painter->drawText(bRect, lab, QTextOption(Qt::AlignHCenter));
518 
519  break;
520  }
521 
522  case LO_VERTICAL:
523  {
524  m_Painter->drawText(pos, i18n("Chart Scale:"));
525  y += 10;
526  x += 40;
527 
528  m_Painter->drawLine(x, y, x, y + size);
529  // paint line endings
530  m_Painter->drawLine(x - 5, y, x + 5, y);
531  m_Painter->drawLine(x - 5, y + size, x + 5, y + size);
532 
533  // paint scale text
534  QRectF bRect(QPoint(x + 5, y), QPoint(x + 20, y + size));
535  //m_Painter->drawRect(bRect);
536  m_Painter->drawText(bRect, lab, QTextOption(Qt::AlignVCenter));
537 
538  break;
539  }
540 
541  default: return; // should never happen
542  }
543 }
544 
545 QPoint Legend::positionToDeviceCoord(QPaintDevice *pd)
546 {
547  QSize legendSize = calculateSize();
548 
549  switch(m_Position)
550  {
551  case LP_UPPER_LEFT: // position: upper left corner
552  {
553  return QPoint(0, 0);
554  }
555 
556  case LP_UPPER_RIGHT: // position: upper right corner
557  {
558  return QPoint(pd->width() - legendSize.width(), 0);
559  }
560 
561  case LP_LOWER_LEFT: // position: lower left corner
562  {
563  return QPoint(0, pd->height() - legendSize.height());
564  }
565 
566  case LP_LOWER_RIGHT: // position: lower right corner
567  {
568  return QPoint(pd->width() - legendSize.width(), pd->height() - legendSize.height());
569  }
570 
571  default: // legend is floating
572  {
573  return QPoint();
574  }
575  }
576 }
577 
578 Legend::Legend( const Legend &o ) :
579  m_Painter( 0 ), m_SkyMap( o.m_SkyMap ), m_DeletePainter( o.m_DeletePainter ),
580  m_Type( o.m_Type ),m_Orientation( o.m_Orientation ),m_Position( o.m_Position ),
581  m_PositionFloating( o.m_PositionFloating ), m_cScheme( o.m_cScheme ),m_Font( o.m_Font ),
582  m_BgColor( o.m_BgColor ),m_DrawFrame( o.m_DrawFrame ), m_SymbolSize( o.m_SymbolSize ),
583  m_BRectWidth( o.m_BRectWidth ),m_BRectHeight( o.m_BRectHeight ),
584  m_MaxHScalePixels( o.m_MaxHScalePixels ),m_MaxVScalePixels( o.m_MaxVScalePixels ),
585  m_XSymbolSpacing( o.m_XSymbolSpacing ),m_YSymbolSpacing( o.m_YSymbolSpacing ) {
586 
587 }
588 
SkyQPainter::drawPointSource
virtual bool drawPointSource(SkyPoint *loc, float mag, char sp= 'A')
Draw a point source (e.g., a star).
Definition: skyqpainter.cpp:371
KStarsData
KStarsData is the backbone of KStars.
Definition: kstarsdata.h:66
legend.h
Legend::LT_FULL
Definition: legend.h:54
Legend::LP_LOWER_LEFT
Definition: legend.h:77
Legend::calculateSize
QSize calculateSize()
Calculates size of legend that will be painted using current settings.
Definition: legend.cpp:56
ColorScheme::colorNamed
QColor colorNamed(const QString &name) const
Retrieve a color by name.
Definition: colorscheme.cpp:97
SkyQPainter::setBrush
virtual void setBrush(const QBrush &brush)
Set the brush of the painter.
Definition: skyqpainter.cpp:135
SkyQPainter::begin
virtual void begin()
Begin painting.
Definition: skyqpainter.cpp:110
SkyQPainter
The QPainter-based painting backend.
Definition: skyqpainter.h:32
Legend::LP_LOWER_RIGHT
Definition: legend.h:78
SkyPainter::starWidth
float starWidth(float mag) const
Get the width of a star of magnitude mag.
Definition: skypainter.cpp:50
Legend::LP_UPPER_RIGHT
Definition: legend.h:76
Legend::LEGEND_POSITION
LEGEND_POSITION
Legend position enumeration.
Definition: legend.h:73
Legend::LO_HORIZONTAL
Definition: legend.h:66
Legend::LP_UPPER_LEFT
Definition: legend.h:75
skymap.h
Legend::LEGEND_TYPE
LEGEND_TYPE
Legend type enumeration.
Definition: legend.h:52
Legend::LT_SCALE_MAGNITUDES
Definition: legend.h:55
Legend::~Legend
~Legend()
Destructor.
Definition: legend.cpp:48
Legend::LEGEND_ORIENTATION
LEGEND_ORIENTATION
Legend orientation enumeration.
Definition: legend.h:64
Options.h
Legend
Legend class is used for painting legends on class inheriting QPaintDevice.
Definition: legend.h:45
SkyQPainter::setPen
virtual void setPen(const QPen &pen)
Set the pen of the painter.
Definition: skyqpainter.cpp:130
SkyQPainter::drawDeepSkySymbol
virtual void drawDeepSkySymbol(const QPointF &pos, int type, float size, float e, float positionAngle)
Definition: skyqpainter.cpp:468
Options::zoomFactor
static double zoomFactor()
Get Zoom Factor, in pixels per radian.
Definition: Options.h:2531
skyqpainter.h
Legend::LP_FLOATING
Definition: legend.h:79
SkyMap
This is the canvas on which the sky is painted.
Definition: skymap.h:72
Legend::LT_MAGNITUDES_ONLY
Definition: legend.h:57
Legend::LT_SCALE_ONLY
Definition: legend.h:56
Legend::LT_SYMBOLS_ONLY
Definition: legend.h:58
Legend::paintLegend
void paintLegend(QPaintDevice *pd)
Paint legend on passed QPaintDevice at selected position.
Definition: legend.cpp:162
kstarsdata.h
colorscheme.h
Legend::LO_VERTICAL
Definition: legend.h:67
SkyQPainter::end
virtual void end()
End and finalize painting.
Definition: skyqpainter.cpp:119
Legend::Legend
Legend(LEGEND_ORIENTATION orientation=LO_HORIZONTAL, LEGEND_POSITION pos=LP_FLOATING)
Constructor.
Definition: legend.cpp:38
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:36:20 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