• 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
pvplotwidget.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  pvplotwidget.cpp
3  -------------------
4  begin : Sat 17 Dec 2005
5  copyright : (C) 2005 by Jason Harris
6  email : kstars@30doradus.org
7  ***************************************************************************/
8 /***************************************************************************
9  * *
10  * This program is free software; you can redistribute it and/or modify *
11  * it under the terms of the GNU General Public License as published by *
12  * the Free Software Foundation; either version 2 of the License, or *
13  * (at your option) any later version. *
14  * *
15  ***************************************************************************/
16 
17 #include "pvplotwidget.h"
18 
19 #include <cmath> //for sqrt()
20 
21 #include <QApplication>
22 #include <KPlotObject>
23 #include <KPlotPoint>
24 
25 #include "planetviewer.h"
26 
27 PVPlotWidget::PVPlotWidget( QWidget *parent ) :
28  KPlotWidget( parent ),
29  mouseButtonDown(false), oldx(0), oldy(0)
30 {
31  setFocusPolicy( Qt::StrongFocus );
32  setMouseTracking (true);
33  setAntialiasing(true);
34  //FIXME: Evil cast!
35  pv = (PlanetViewer*)topLevelWidget();
36 }
37 
38 PVPlotWidget::~ PVPlotWidget() {}
39 
40 void PVPlotWidget::keyPressEvent( QKeyEvent *e ) {
41  double xc = (dataRect().right() + dataRect().x())*0.5;
42  double yc = (dataRect().bottom() + dataRect().y())*0.5;
43  double xstep = 0.01*(dataRect().right() - dataRect().x());
44  double ystep = 0.01*(dataRect().bottom() - dataRect().y());
45  double dx = 0.5*dataRect().width();
46  double dy = 0.5*dataRect().height();
47 
48  switch ( e->key() ) {
49  case Qt::Key_Left:
50  if ( xc - xstep > -AUMAX ) {
51  setLimits( dataRect().x() - xstep, dataRect().right() - xstep, dataRect().y(), dataRect().bottom() );
52  pv->setCenterPlanet(QString());
53  update();
54  }
55  break;
56 
57  case Qt::Key_Right:
58  if ( xc + xstep < AUMAX ) {
59  setLimits( dataRect().x() + xstep, dataRect().right() + xstep, dataRect().y(), dataRect().bottom() );
60  pv->setCenterPlanet(QString());
61  update();
62  }
63  break;
64 
65  case Qt::Key_Down:
66  if ( yc - ystep > -AUMAX ) {
67  setLimits( dataRect().x(), dataRect().right(), dataRect().y() - ystep, dataRect().bottom() - ystep );
68  pv->setCenterPlanet(QString());
69  update();
70  }
71  break;
72 
73  case Qt::Key_Up:
74  if ( yc + ystep < AUMAX ) {
75  setLimits( dataRect().x(), dataRect().right(), dataRect().y() + ystep, dataRect().bottom() + ystep );
76  pv->setCenterPlanet(QString());
77  update();
78  }
79  break;
80 
81  case Qt::Key_Plus:
82  case Qt::Key_Equal:
83  slotZoomIn();
84  break;
85 
86  case Qt::Key_Minus:
87  case Qt::Key_Underscore:
88  slotZoomOut();
89  break;
90 
91  case Qt::Key_0: //Sun
92  setLimits( -dx, dx, -dy, dy );
93  pv->setCenterPlanet( "Sun" );
94  update();
95  break;
96 
97  case Qt::Key_1: //Mercury
98  {
99  KPlotPoint *p = plotObjects().at(10)->points().at(0);
100  setLimits( p->x() - dx, p->x() + dx, p->y() - dy, p->y() + dy );
101  pv->setCenterPlanet( "Mercury" );
102  update();
103  break;
104  }
105 
106  case Qt::Key_2: //Venus
107  {
108  KPlotPoint *p = plotObjects().at(11)->points().at(0);
109  setLimits( p->x() - dx, p->x() + dx, p->y() - dy, p->y() + dy );
110  pv->setCenterPlanet( "Venus" );
111  update();
112  break;
113  }
114 
115  case Qt::Key_3: //Earth
116  {
117  KPlotPoint *p = plotObjects().at(12)->points().at(0);
118  setLimits( p->x() - dx, p->x() + dx, p->y() - dy, p->y() + dy );
119  pv->setCenterPlanet( "Earth" );
120  update();
121  break;
122  }
123 
124  case Qt::Key_4: //Mars
125  {
126  KPlotPoint *p = plotObjects().at(13)->points().at(0);
127  setLimits( p->x() - dx, p->x() + dx, p->y() - dy, p->y() + dy );
128  pv->setCenterPlanet( "Mars" );
129  update();
130  break;
131  }
132 
133  case Qt::Key_5: //Jupiter
134  {
135  KPlotPoint *p = plotObjects().at(14)->points().at(0);
136  setLimits( p->x() - dx, p->x() + dx, p->y() - dy, p->y() + dy );
137  pv->setCenterPlanet( "Jupiter" );
138  update();
139  break;
140  }
141 
142  case Qt::Key_6: //Saturn
143  {
144  KPlotPoint *p = plotObjects().at(15)->points().at(0);
145  setLimits( p->x() - dx, p->x() + dx, p->y() - dy, p->y() + dy );
146  pv->setCenterPlanet( "Saturn" );
147  update();
148  break;
149  }
150 
151  case Qt::Key_7: //Uranus
152  {
153  KPlotPoint *p = plotObjects().at(16)->points().at(0);
154  setLimits( p->x() - dx, p->x() + dx, p->y() - dy, p->y() + dy );
155  pv->setCenterPlanet( "Uranus" );
156  update();
157  break;
158  }
159 
160  case Qt::Key_8: //Neptune
161  {
162  KPlotPoint *p = plotObjects().at(17)->points().at(0);
163  setLimits( p->x() - dx, p->x() + dx, p->y() - dy, p->y() + dy );
164  pv->setCenterPlanet( "Neptune" );
165  update();
166  break;
167  }
168 
169  case Qt::Key_9: //Pluto
170  {
171  KPlotPoint *p = plotObjects().at(18)->points().at(0);
172  setLimits( p->x() - dx, p->x() + dx, p->y() - dy, p->y() + dy );
173  pv->setCenterPlanet( "Pluto" );
174  update();
175  break;
176  }
177 
178  default:
179  e->ignore();
180  break;
181  }
182 }
183 
184 void PVPlotWidget::mousePressEvent( QMouseEvent *e ) {
185  mouseButtonDown = true;
186  oldx = e->x();
187  oldy = e->y();
188 }
189 
190 void PVPlotWidget::mouseReleaseEvent( QMouseEvent * ) {
191  mouseButtonDown = false;
192  update();
193 }
194 
195 void PVPlotWidget::mouseMoveEvent( QMouseEvent *e ) {
196  if ( mouseButtonDown ) {
197  //Determine how far we've moved
198  double xc = (dataRect().right() + dataRect().x())*0.5;
199  double yc = (dataRect().bottom() + dataRect().y())*0.5;
200  double xscale = dataRect().width()/( width() - leftPadding() - rightPadding() );
201  double yscale = dataRect().height()/( height() - topPadding() - bottomPadding() );
202 
203  xc += ( oldx - e->x() )*xscale;
204  yc -= ( oldy - e->y() )*yscale; //Y data axis is reversed...
205 
206  if ( xc > -AUMAX && xc < AUMAX && yc > -AUMAX && yc < AUMAX ) {
207  setLimits( xc - 0.5*dataRect().width(), xc + 0.5*dataRect().width(),
208  yc - 0.5*dataRect().height(), yc + 0.5*dataRect().height() );
209  update();
210  qApp->processEvents();
211  }
212 
213  oldx = e->x();
214  oldy = e->y();
215  }
216 }
217 
218 void PVPlotWidget::mouseDoubleClickEvent( QMouseEvent *e ) {
219  double xscale = dataRect().width()/( width() - leftPadding() - rightPadding() );
220  double yscale = dataRect().height()/( height() - topPadding() - bottomPadding() );
221 
222  double xc = dataRect().x() + xscale*( e->x() - leftPadding() );
223  double yc = dataRect().bottom() - yscale*( e->y() - topPadding() );
224 
225  if ( xc > -AUMAX && xc < AUMAX && yc > -AUMAX && yc < AUMAX ) {
226  setLimits( xc - 0.5*dataRect().width(), xc + 0.5*dataRect().width(),
227  yc - 0.5*dataRect().height(), yc + 0.5*dataRect().height() );
228  update();
229  }
230 
231  pv->setCenterPlanet( QString() );
232  for ( unsigned int i=0; i<9; ++i ) {
233  KPlotPoint *point = pv->planetObject(i)->points().at(0);
234  double dx = ( point->x() - xc )/xscale;
235  if ( dx < 4.0 ) {
236  double dy = ( point->y() - yc )/yscale;
237  if ( sqrt( dx*dx + dy*dy ) < 4.0 ) {
238  pv->setCenterPlanet( pv->planetName(i) );
239  }
240  }
241  }
242 }
243 
244 void PVPlotWidget::wheelEvent( QWheelEvent *e ) {
245  if ( e->delta() > 0 ) slotZoomIn();
246  else slotZoomOut();
247 }
248 
249 void PVPlotWidget::slotZoomIn() {
250  double size = dataRect().width();
251  if ( size > 0.8 ) {
252  setLimits( dataRect().x() + 0.02*size, dataRect().right() - 0.02*size, dataRect().y() + 0.02*size, dataRect().bottom() - 0.02*size );
253  update();
254  }
255 }
256 
257 void PVPlotWidget::slotZoomOut() {
258  double size = dataRect().width();
259  if ( (size) < 100.0 ) {
260  setLimits( dataRect().x() - 0.02*size, dataRect().right() + 0.02*size, dataRect().y() - 0.02*size, dataRect().bottom() + 0.02*size );
261  update();
262  }
263 }
264 
265 #include "pvplotwidget.moc"
PVPlotWidget::slotZoomOut
void slotZoomOut()
Definition: pvplotwidget.cpp:257
PVPlotWidget::PVPlotWidget
PVPlotWidget(QWidget *parent=0)
Definition: pvplotwidget.cpp:27
PVPlotWidget::mouseDoubleClickEvent
virtual void mouseDoubleClickEvent(QMouseEvent *e)
Definition: pvplotwidget.cpp:218
PVPlotWidget::slotZoomIn
void slotZoomIn()
Definition: pvplotwidget.cpp:249
PVPlotWidget::mouseReleaseEvent
virtual void mouseReleaseEvent(QMouseEvent *)
Definition: pvplotwidget.cpp:190
QWidget
PVPlotWidget::mousePressEvent
virtual void mousePressEvent(QMouseEvent *e)
Definition: pvplotwidget.cpp:184
PlanetViewer::planetName
QString planetName(uint i) const
Definition: planetviewer.cpp:123
PVPlotWidget::~PVPlotWidget
~PVPlotWidget()
Definition: pvplotwidget.cpp:38
PlanetViewer::setCenterPlanet
void setCenterPlanet(const QString &cp)
Definition: planetviewer.h:56
AUMAX
#define AUMAX
Definition: planetviewer.h:33
planetviewer.h
PlanetViewer::planetObject
KPlotObject * planetObject(uint i) const
Definition: planetviewer.h:58
KPlotWidget
PlanetViewer
Display an overhead view of the solar system.
Definition: planetviewer.h:48
pvplotwidget.h
PVPlotWidget::keyPressEvent
virtual void keyPressEvent(QKeyEvent *e)
Definition: pvplotwidget.cpp:40
PVPlotWidget::wheelEvent
virtual void wheelEvent(QWheelEvent *e)
Definition: pvplotwidget.cpp:244
PVPlotWidget::mouseMoveEvent
virtual void mouseMoveEvent(QMouseEvent *e)
Definition: pvplotwidget.cpp:195
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