• 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
  • tools
avtplotwidget.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  avtplotwidget.cpp - description
3  -------------------
4  begin : Sat Nov 10 2007
5  copyright : (C) 2007 by Jason Harris
6  email : kstars@30doradus.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 #include "avtplotwidget.h"
19 
20 #include <QWidget>
21 #include <QMouseEvent>
22 #include <QPainter>
23 #include <QTime>
24 #include <QLinearGradient>
25 
26 #include <KGlobal>
27 #include <KLocale>
28 #include <KPlotObject>
29 #include <kdebug.h>
30 
31 AVTPlotWidget::AVTPlotWidget( QWidget *parent )
32  : KPlotWidget( parent )
33 {
34  setAntialiasing(true);
35 
36  //Default SunRise/SunSet values
37  SunRise = 0.25;
38  SunSet = 0.75;
39 
40  MousePoint = QPoint( -1, -1 );
41 }
42 
43 void AVTPlotWidget::mousePressEvent( QMouseEvent *e ) {
44  mouseMoveEvent( e );
45 }
46 
47 void AVTPlotWidget::mouseDoubleClickEvent( QMouseEvent * ) {
48  MousePoint = QPoint(-1, -1);
49  update();
50 }
51 
52 void AVTPlotWidget::mouseMoveEvent( QMouseEvent *e ) {
53  QRect checkRect( leftPadding(), topPadding(), pixRect().width(), pixRect().height() );
54  int Xcursor = e->x();
55  int Ycursor = e->y();
56 
57  if ( ! checkRect.contains( e->x(), e->y() ) ) {
58  if ( e->x() < checkRect.left() ) Xcursor = checkRect.left();
59  if ( e->x() > checkRect.right() ) Xcursor = checkRect.right();
60  if ( e->y() < checkRect.top() ) Ycursor = checkRect.top();
61  if ( e->y() > checkRect.bottom() ) Ycursor = checkRect.bottom();
62  }
63 
64  Xcursor -= leftPadding();
65  Ycursor -= topPadding();
66 
67  MousePoint = QPoint( Xcursor, Ycursor );
68  update();
69 }
70 
71 void AVTPlotWidget::paintEvent( QPaintEvent *e ) {
72  Q_UNUSED(e)
73 
74  QPainter p;
75 
76  p.begin( this );
77  p.setRenderHint( QPainter::Antialiasing, antialiasing() );
78  p.fillRect( rect(), backgroundColor() );
79  p.translate( leftPadding(), topPadding() );
80 
81  setPixRect();
82  p.setClipRect( pixRect() );
83  p.setClipping( true );
84 
85  int pW = pixRect().width();
86  int pH = pixRect().height();
87 
88  QColor SkyColor( 0, 100, 200 );
89 
90  //draw daytime sky if the Sun rises for the current date/location
91  if ( SunMaxAlt > -18.0 ) {
92  //Display centered on midnight, so need to modulate dawn/dusk by 0.5
93  int rise = int( pW * ( 0.5 + SunRise ) );
94  int set = int( pW * ( SunSet - 0.5 ) );
95  int da = int( pW * ( 0.5 + Dawn ) );
96  int du = int( pW * ( Dusk - 0.5 ) );
97 
98  if ( SunMinAlt > 0.0 ) {
99  // The sun never set and the sky is always blue
100  p.fillRect( rect(), SkyColor );
101  } else if ( SunMaxAlt < 0.0 && SunMinAlt < -18.0 ) {
102  // The sun never rise but the sky is not completely dark
103  QLinearGradient grad = QLinearGradient( QPointF( 0.0, 0.0 ), QPointF( du, 0.0 ) );
104  grad.setColorAt( 0, SkyColor.darker( SunMaxAlt / -18.0 * 1000 ) );
105  grad.setColorAt( 1, Qt::black );
106  p.fillRect( QRectF( 0.0, 0.0, du+20.0, pH ), grad );
107 
108  grad.setStart( QPointF( pW, 0.0 ) );
109  grad.setFinalStop( QPointF( da-20.0, 0.0 ) );
110  p.fillRect( QRectF( da-20.0, 0.0, pW, pH ), grad );
111  } else if ( SunMaxAlt < 0.0 && SunMinAlt > -18.0 ) {
112  // The sun never rise but the sky is NEVER completely dark
113  QLinearGradient grad = QLinearGradient( QPointF( 0.0, 0.0 ), QPointF( pW, 0.0 ) );
114  grad.setColorAt( 0, SkyColor.darker( SunMaxAlt / -18.0 * 1000 ) );
115  grad.setColorAt( 0.5, SkyColor.darker( SunMinAlt / -18.0 * 1000 ) );
116  grad.setColorAt( 1, SkyColor.darker( SunMaxAlt / -18.0 * 1000 ) );
117  p.fillRect( QRectF( 0.0, 0.0, pW, pH ), grad );
118  } else if ( Dawn < 0.0 ) {
119  // The sun sets and rises but the sky is never completely dark
120  p.fillRect( 0, 0, set, int( 0.5 * pH ), SkyColor );
121  p.fillRect( rise, 0, pW, int( 0.5 * pH ), SkyColor );
122 
123  QLinearGradient grad = QLinearGradient( QPointF( set-20.0, 0.0 ), QPointF( rise, 0.0 ) );
124  grad.setColorAt( 0, SkyColor );
125  grad.setColorAt( 0.5, SkyColor.darker( SunMinAlt / -18.0 * 1000 ) );
126  grad.setColorAt( 1, SkyColor );
127  p.fillRect( QRectF( set-20.0, 0.0, rise-set+20.0, pH ), grad );
128  } else {
129  p.fillRect( 0, 0, set, pH, SkyColor );
130  p.fillRect( rise, 0, pW, pH, SkyColor );
131 
132  QLinearGradient grad = QLinearGradient( QPointF( set-20.0, 0.0 ), QPointF( du, 0.0 ) );
133  grad.setColorAt( 0, SkyColor );
134  grad.setColorAt( 1, Qt::black );
135  p.fillRect( QRectF( set-20.0, 0.0, du-set+20.0, pH ), grad );
136 
137  grad.setStart( QPointF( rise+20.0, 0.0 ) );
138  grad.setFinalStop( QPointF( da, 0.0 ) );
139  p.fillRect( QRectF( da, 0.0, rise-da+20.0, pH ), grad );
140  }
141  }
142 
143  //draw ground
144  p.fillRect( 0, int(0.5*pH), pW, int(0.5*pH), QColor( "#002200" ) );
145 
146  foreach( KPlotObject *po, plotObjects() )
147  po->draw( &p, this );
148 
149  p.setClipping( false );
150  drawAxes( &p );
151 
152  //Add vertical line indicating "now"
153  QTime t = QTime::currentTime();
154  double x = 12.0 + t.hour() + t.minute()/60.0 + t.second()/3600.0;
155  while ( x > 24.0 ) x -= 24.0;
156  int ix = int(x*pW/24.0); //convert to screen pixel coords
157  p.setPen( QPen( QBrush("white"), 2.0, Qt::DotLine ) );
158  p.drawLine( ix, 0, ix, pH );
159 
160  //Label this vertical line with the current time
161  p.save();
162  QFont smallFont = p.font();
163  smallFont.setPointSize( smallFont.pointSize() - 2 );
164  p.setFont( smallFont );
165  p.translate( ix + 10, pH - 20 );
166  p.rotate(-90);
167  p.drawText(0, 0, KGlobal::locale()->formatTime( t ) );
168  p.restore();
169 
170  //Draw crosshairs at clicked position
171  if ( MousePoint.x() > 0 ) {
172  p.setPen( QPen( QBrush("gold"), 1.0, Qt::SolidLine ) );
173  p.drawLine( QLineF( MousePoint.x()+0.5, 0.5, MousePoint.x()+0.5, pixRect().height()-0.5 ) );
174  p.drawLine( QLineF( 0.5, MousePoint.y()+0.5, pixRect().width()-0.5, MousePoint.y()+0.5 ) );
175 
176  //Label each crosshair line (time and altitude)
177  p.setFont( smallFont );
178  double a = (pH - MousePoint.y())*180.0/pH - 90.0;
179  p.drawText( 20, MousePoint.y() + 10, QString::number(a,'f',2) + QChar(176) );
180 
181  double h = MousePoint.x()*24.0/pW - 12.0;
182  if ( h < 0.0 ) h += 24.0;
183  t = QTime( int(h), int(60.*(h - int(h))) );
184  p.save();
185  p.translate( MousePoint.x() + 10, pH - 20 );
186  p.rotate(-90);
187  p.drawText( 0, 0, KGlobal::locale()->formatTime( t ) );
188  p.restore();
189  }
190 
191  p.end();
192 }
193 
194 void AVTPlotWidget::setDawnDuskTimes( double da, double du )
195 {
196  Dawn = da;
197  Dusk = du;
198 }
199 
200 void AVTPlotWidget::setMinMaxSunAlt( double min, double max )
201 {
202  SunMinAlt = min;
203  SunMaxAlt = max;
204 }
205 
206 
207 #include "avtplotwidget.moc"
AVTPlotWidget::setMinMaxSunAlt
void setMinMaxSunAlt(double min, double max)
Definition: avtplotwidget.cpp:200
QWidget
AVTPlotWidget::mousePressEvent
void mousePressEvent(QMouseEvent *e)
Simply calls mouseMoveEvent().
Definition: avtplotwidget.cpp:43
KPlotWidget
avtplotwidget.h
AVTPlotWidget::mouseDoubleClickEvent
void mouseDoubleClickEvent(QMouseEvent *e)
Reset the MousePoint to a null value, to erase the crosshairs.
Definition: avtplotwidget.cpp:47
AVTPlotWidget::paintEvent
void paintEvent(QPaintEvent *e)
Redraw the plot.
Definition: avtplotwidget.cpp:71
AVTPlotWidget::setDawnDuskTimes
void setDawnDuskTimes(double da, double du)
Definition: avtplotwidget.cpp:194
AVTPlotWidget::AVTPlotWidget
AVTPlotWidget(QWidget *parent=0)
Constructor.
Definition: avtplotwidget.cpp:31
AVTPlotWidget::mouseMoveEvent
void mouseMoveEvent(QMouseEvent *e)
Handle mouse move events.
Definition: avtplotwidget.cpp:52
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