Kstars

pvplotwidget.cpp
1/*
2 SPDX-FileCopyrightText: 2005 Jason Harris <kstars@30doradus.org>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#include "pvplotwidget.h"
8
9#include "planetviewer.h"
10
11#include <KPlotObject>
12#include <KPlotPoint>
13
14#include <QKeyEvent>
15#include <QMouseEvent>
16#include <QWheelEvent>
17
18#include <cmath>
19
20PVPlotWidget::PVPlotWidget(QWidget *parent) : KPlotWidget(parent)
21{
22 setFocusPolicy(Qt::StrongFocus);
23 setMouseTracking(true);
24 setAntialiasing(true);
25 //FIXME: Evil cast!
26 pv = (PlanetViewer *)topLevelWidget();
27}
28
29void PVPlotWidget::keyPressEvent(QKeyEvent *e)
30{
31 double xc = (dataRect().right() + dataRect().x()) * 0.5;
32 double yc = (dataRect().bottom() + dataRect().y()) * 0.5;
33 double xstep = 0.01 * (dataRect().right() - dataRect().x());
34 double ystep = 0.01 * (dataRect().bottom() - dataRect().y());
35 double dx = 0.5 * dataRect().width();
36 double dy = 0.5 * dataRect().height();
37
38 switch (e->key())
39 {
40 case Qt::Key_Left:
41 if (xc - xstep > -AUMAX)
42 {
43 setLimits(dataRect().x() - xstep, dataRect().right() - xstep, dataRect().y(), dataRect().bottom());
44 pv->setCenterPlanet(QString());
45 update();
46 }
47 break;
48
49 case Qt::Key_Right:
50 if (xc + xstep < AUMAX)
51 {
52 setLimits(dataRect().x() + xstep, dataRect().right() + xstep, dataRect().y(), dataRect().bottom());
53 pv->setCenterPlanet(QString());
54 update();
55 }
56 break;
57
58 case Qt::Key_Down:
59 if (yc - ystep > -AUMAX)
60 {
61 setLimits(dataRect().x(), dataRect().right(), dataRect().y() - ystep, dataRect().bottom() - ystep);
62 pv->setCenterPlanet(QString());
63 update();
64 }
65 break;
66
67 case Qt::Key_Up:
68 if (yc + ystep < AUMAX)
69 {
70 setLimits(dataRect().x(), dataRect().right(), dataRect().y() + ystep, dataRect().bottom() + ystep);
71 pv->setCenterPlanet(QString());
72 update();
73 }
74 break;
75
76 case Qt::Key_Plus:
77 case Qt::Key_Equal:
78 {
79 updateFactor(e->modifiers());
80 slotZoomIn();
81 break;
82 }
83
84 case Qt::Key_Minus:
86 {
87 updateFactor(e->modifiers());
88 slotZoomOut();
89 break;
90 }
91
92 case Qt::Key_0: //Sun
93 setLimits(-dx, dx, -dy, dy);
94 pv->setCenterPlanet(i18n("Sun"));
95 update();
96 break;
97
98 case Qt::Key_1: //Mercury
99 {
100 KPlotPoint *p = plotObjects().at(10)->points().at(0);
101 setLimits(p->x() - dx, p->x() + dx, p->y() - dy, p->y() + dy);
102 pv->setCenterPlanet(i18n("Mercury"));
103 update();
104 break;
105 }
106
107 case Qt::Key_2: //Venus
108 {
109 KPlotPoint *p = plotObjects().at(11)->points().at(0);
110 setLimits(p->x() - dx, p->x() + dx, p->y() - dy, p->y() + dy);
111 pv->setCenterPlanet(i18n("Venus"));
112 update();
113 break;
114 }
115
116 case Qt::Key_3: //Earth
117 {
118 KPlotPoint *p = plotObjects().at(12)->points().at(0);
119 setLimits(p->x() - dx, p->x() + dx, p->y() - dy, p->y() + dy);
120 pv->setCenterPlanet(i18n("Earth"));
121 update();
122 break;
123 }
124
125 case Qt::Key_4: //Mars
126 {
127 KPlotPoint *p = plotObjects().at(13)->points().at(0);
128 setLimits(p->x() - dx, p->x() + dx, p->y() - dy, p->y() + dy);
129 pv->setCenterPlanet(i18n("Mars"));
130 update();
131 break;
132 }
133
134 case Qt::Key_5: //Jupiter
135 {
136 KPlotPoint *p = plotObjects().at(14)->points().at(0);
137 setLimits(p->x() - dx, p->x() + dx, p->y() - dy, p->y() + dy);
138 pv->setCenterPlanet(i18n("Jupiter"));
139 update();
140 break;
141 }
142
143 case Qt::Key_6: //Saturn
144 {
145 KPlotPoint *p = plotObjects().at(15)->points().at(0);
146 setLimits(p->x() - dx, p->x() + dx, p->y() - dy, p->y() + dy);
147 pv->setCenterPlanet(i18n("Saturn"));
148 update();
149 break;
150 }
151
152 case Qt::Key_7: //Uranus
153 {
154 KPlotPoint *p = plotObjects().at(16)->points().at(0);
155 setLimits(p->x() - dx, p->x() + dx, p->y() - dy, p->y() + dy);
156 pv->setCenterPlanet("Uranus");
157 update();
158 break;
159 }
160
161 case Qt::Key_8: //Neptune
162 {
163 KPlotPoint *p = plotObjects().at(17)->points().at(0);
164 setLimits(p->x() - dx, p->x() + dx, p->y() - dy, p->y() + dy);
165 pv->setCenterPlanet(i18n("Neptune"));
166 update();
167 break;
168 }
169
170 /*case Qt::Key_9: //Pluto
171 {
172 KPlotPoint *p = plotObjects().at(18)->points().at(0);
173 setLimits( p->x() - dx, p->x() + dx, p->y() - dy, p->y() + dy );
174 pv->setCenterPlanet( "Pluto" );
175 update();
176 break;
177 }*/
178
179 default:
180 e->ignore();
181 break;
182 }
183}
184
185void PVPlotWidget::mousePressEvent(QMouseEvent *e)
186{
187 mouseButtonDown = true;
188 oldx = e->x();
189 oldy = e->y();
190}
191
192void PVPlotWidget::mouseReleaseEvent(QMouseEvent *)
193{
194 mouseButtonDown = false;
195 update();
196}
197
198void PVPlotWidget::mouseMoveEvent(QMouseEvent *e)
199{
200 if (mouseButtonDown)
201 {
202 //Determine how far we've moved
203 double xc = (dataRect().right() + dataRect().x()) * 0.5;
204 double yc = (dataRect().bottom() + dataRect().y()) * 0.5;
205 double xscale = dataRect().width() / (width() - leftPadding() - rightPadding());
206 double yscale = dataRect().height() / (height() - topPadding() - bottomPadding());
207
208 xc += (oldx - e->x()) * xscale;
209 yc -= (oldy - e->y()) * yscale; //Y data axis is reversed...
210
211 if (xc > -AUMAX && xc < AUMAX && yc > -AUMAX && yc < AUMAX)
212 {
213 setLimits(xc - 0.5 * dataRect().width(), xc + 0.5 * dataRect().width(), yc - 0.5 * dataRect().height(),
214 yc + 0.5 * dataRect().height());
215 update();
216 qApp->processEvents();
217 }
218
219 oldx = e->x();
220 oldy = e->y();
221 }
222}
223
224void PVPlotWidget::mouseDoubleClickEvent(QMouseEvent *e)
225{
226 double xscale = dataRect().width() / (width() - leftPadding() - rightPadding());
227 double yscale = dataRect().height() / (height() - topPadding() - bottomPadding());
228
229 double xc = dataRect().x() + xscale * (e->x() - leftPadding());
230 double yc = dataRect().bottom() - yscale * (e->y() - topPadding());
231
232 if (xc > -AUMAX && xc < AUMAX && yc > -AUMAX && yc < AUMAX)
233 {
234 setLimits(xc - 0.5 * dataRect().width(), xc + 0.5 * dataRect().width(), yc - 0.5 * dataRect().height(),
235 yc + 0.5 * dataRect().height());
236 update();
237 }
238
239 pv->setCenterPlanet(QString());
240 for (unsigned int i = 0; i < 9; ++i)
241 {
242 KPlotPoint *point = pv->planetObject(i)->points().at(0);
243 double dx = (point->x() - xc) / xscale;
244 if (dx < 4.0)
245 {
246 double dy = (point->y() - yc) / yscale;
247 if (sqrt(dx * dx + dy * dy) < 4.0)
248 {
249 pv->setCenterPlanet(pv->planetName(i));
250 }
251 }
252 }
253}
254
255void PVPlotWidget::wheelEvent(QWheelEvent *e)
256{
257 updateFactor(e->modifiers());
258 if (e->angleDelta().y() > 0)
259 slotZoomIn();
260 else
261 slotZoomOut();
262}
263
264void PVPlotWidget::slotZoomIn()
265{
266 double size = dataRect().width();
267 if (size > 0.8)
268 {
269 setLimits(dataRect().x() + factor * 0.01 * size, dataRect().right() - factor * 0.01 * size,
270 dataRect().y() + factor * 0.01 * size, dataRect().bottom() - factor * 0.01 * size);
271 update();
272 }
273}
274
275void PVPlotWidget::slotZoomOut()
276{
277 double size = dataRect().width();
278 if ((size) < 100.0)
279 {
280 setLimits(dataRect().x() - factor * 0.01 * size, dataRect().right() + factor * 0.01 * size,
281 dataRect().y() - factor * 0.01 * size, dataRect().bottom() + factor * 0.01 * size);
282 update();
283 }
284}
285
286void PVPlotWidget::updateFactor(const int modifier)
287{
288 factor = (modifier & Qt::ControlModifier) ? 1 : 25;
289}
QList< KPlotPoint * > points() const
double x() const
double y() const
QList< KPlotObject * > plotObjects() const
int bottomPadding() const
int leftPadding() const
QRectF dataRect() const
int topPadding() const
int rightPadding() const
void setLimits(double x1, double x2, double y1, double y2)
Display an overhead view of the solar system.
QString i18n(const char *text, const TYPE &arg...)
void ignore()
Qt::KeyboardModifiers modifiers() const const
int key() const const
Qt::KeyboardModifiers modifiers() const const
int x() const const
int y() const const
int y() const const
qreal bottom() const const
qreal height() const const
qreal right() const const
qreal width() const const
qreal x() const const
qreal y() const const
StrongFocus
Key_Left
ControlModifier
QTextStream & right(QTextStream &stream)
QPoint angleDelta() const const
void update()
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:04:47 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.