Kstars

mapcanvas.cpp
1 /*
2  SPDX-FileCopyrightText: 2001 Jason Harris <[email protected]>
3 
4  SPDX-License-Identifier: GPL-2.0-or-later
5 */
6 
7 #include "mapcanvas.h"
8 #include <cstdlib>
9 
10 #include <QPainter>
11 #include <QPixmap>
12 #include <QMouseEvent>
13 #include <QPaintEvent>
14 #include <QStandardPaths>
15 #include "kspaths.h"
16 
17 #include "dialogs/locationdialog.h"
18 #include "kstars.h"
19 #include "kstarsdata.h"
20 
21 MapCanvas::MapCanvas(QWidget *parent) : QFrame(parent), ld(nullptr)
22 {
23  setAutoFillBackground(false);
24 
25  QString bgFile = KSPaths::locate(QStandardPaths::AppLocalDataLocation, "geomap.jpg");
26  bgImage = new QPixmap(bgFile);
27  xsize= width();
28  ysize = height();
29  ximage = bgImage->width();
30  yimage = bgImage->height();
31  ratio = ximage / yimage;
32  xscale = xsize / 360;
33  yscale = ysize / (360 / ratio);
34  origin.setX(width() / 2);
35  origin.setY(height() / 2);
36 }
37 
39 {
40  delete bgImage;
41 }
42 
43 void MapCanvas::setGeometry(int x, int y, int w, int h)
44 {
45  QWidget::setGeometry(x, y, w, h);
46  origin.setX(w / 2);
47  origin.setY(h / 2);
48 }
49 
51 {
53  origin.setX(r.width() / 2);
54  origin.setY(r.height() / 2);
55 }
56 
58 {
59  //Determine Lat/Long corresponding to event press
60  int lng = ((e->x() - origin.x()) / xscale);
61  int lat = ((origin.y() - e->y()) / yscale);
62 
63  if (ld)
64  ld->findCitiesNear(lng, lat);
65 }
66 
68 {
69  QPainter p;
70 
71  xsize= width();
72  ysize = height();
73  ximage = bgImage->width();
74  yimage = bgImage->height();
75  ratio = ximage / yimage;
76  xscale = xsize / 360;
77  yscale = ysize / (360 / ratio);
78  origin.setX(width() / 2);
79  origin.setY(height() / 2);
80 
81  //prepare the canvas
82  p.begin(this);
83  p.drawPixmap(0, 0, bgImage->scaled(size()));
84  p.setPen(QPen(QColor("SlateGrey")));
85 
86  //Draw cities
87  QPoint o;
88 
89  foreach (GeoLocation *g, KStarsData::Instance()->getGeoList())
90  {
91  convertAndScale(o, *g);
92 
93  if (o.x() >= 0 && o.x() <= width() && o.y() >= 0 && o.y() <= height())
94  {
95  p.drawPoint(o.x(), o.y());
96  }
97  }
98 
99  // FIXME: there must be better way to this. Without bothering LocationDialog
100  if (ld)
101  {
102  //redraw the cities that appear in the filtered list, with a white pen
103  //If the list has not been filtered, skip the redraw.
104  if (ld->filteredList().size())
105  {
106  p.setPen(Qt::white);
107  foreach (GeoLocation *g, ld->filteredList())
108  {
109  convertAndScale(o, *g);
110 
111  if (o.x() >= 0 && o.x() <= width() && o.y() >= 0 && o.y() <= height())
112  {
113  p.drawPoint(o.x(), o.y());
114  }
115  }
116  }
117 
118  GeoLocation *g = ld->selectedCity();
119  if (g)
120  {
121  convertAndScale(o, *g);
122 
123  p.setPen(Qt::red);
124  p.setBrush(Qt::red);
125  p.drawEllipse(o.x() - 3, o.y() - 3, 6, 6);
126  p.drawLine(o.x() - 16, o.y(), o.x() - 8, o.y());
127  p.drawLine(o.x() + 8, o.y(), o.x() + 16, o.y());
128  p.drawLine(o.x(), o.y() - 16, o.x(), o.y() - 8);
129  p.drawLine(o.x(), o.y() + 8, o.x(), o.y() + 16);
130  p.setPen(Qt::white);
131  p.setBrush(Qt::white);
132  }
133  }
134  p.end();
135 }
136 
138 {
139  int xpos = g.lng()->Degrees();
140  int ypos = g.lat()->Degrees();
141  o.setX((xpos * xscale) + origin.x());
142  o.setY(height() - ((ypos * yscale) + origin.y()));
143 }
void setPen(const QColor &color)
QList< GeoLocation * > & getGeoList()
Definition: kstarsdata.h:235
void drawPixmap(const QRectF &target, const QPixmap &pixmap, const QRectF &source)
void drawEllipse(const QRectF &rectangle)
const CachingDms * lng() const
Definition: geolocation.h:64
void mousePressEvent(QMouseEvent *e) override
Trim the list of cities so that only those within 2 degrees of the mouse click are shown in the list.
Definition: mapcanvas.cpp:57
int width() const const
MapCanvas(QWidget *parent)
Default constructor.
Definition: mapcanvas.cpp:21
int x() const const
int y() const const
bool begin(QPaintDevice *device)
bool end()
const CachingDms * lat() const
Definition: geolocation.h:70
void setX(int x)
void setY(int y)
int x() const const
int y() const const
~MapCanvas() override
Destructor (empty)
Definition: mapcanvas.cpp:38
int height() const const
virtual void setGeometry(int x, int y, int w, int h)
Set the geometry of the map widget (overloaded from QWidget).
Definition: mapcanvas.cpp:43
void setBrush(const QBrush &brush)
void setGeometry(int x, int y, int w, int h)
QPixmap scaled(int width, int height, Qt::AspectRatioMode aspectRatioMode, Qt::TransformationMode transformMode) const const
int height() const const
void drawPoint(const QPointF &position)
const double & Degrees() const
Definition: dms.h:141
void drawLine(const QLineF &line)
void setAutoFillBackground(bool enabled)
void convertAndScale(QPoint &o, GeoLocation &g)
Convert geo co-ordinates to a scaled position on the map.
Definition: mapcanvas.cpp:137
void paintEvent(QPaintEvent *e) override
Draw the map.
Definition: mapcanvas.cpp:67
int width() const const
Relevant data about an observing location on Earth.
Definition: geolocation.h:27
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Fri Aug 12 2022 04:00:55 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.