Kstars

mapcanvas.cpp
1/*
2 SPDX-FileCopyrightText: 2001 Jason Harris <jharris@30doradus.org>
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
22{
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
43void 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);
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}
Contains all relevant information for specifying a location on Earth: City Name, State/Province name,...
Definition geolocation.h:28
const CachingDms * lat() const
Definition geolocation.h:70
const CachingDms * lng() const
Definition geolocation.h:64
QList< GeoLocation * > filteredList()
GeoLocation * selectedCity() const
void findCitiesNear(int longitude, int latitude)
Show only cities within 3 degrees of point specified by arguments.
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 paintEvent(QPaintEvent *e) override
Draw the map.
Definition mapcanvas.cpp:67
void convertAndScale(QPoint &o, GeoLocation &g)
Convert geo co-ordinates to a scaled position on the map.
~MapCanvas() override
Destructor (empty)
Definition mapcanvas.cpp:38
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
MapCanvas(QWidget *parent)
Default constructor.
Definition mapcanvas.cpp:21
qsizetype size() const const
int x() const const
int y() const const
bool begin(QPaintDevice *device)
void drawEllipse(const QPoint &center, int rx, int ry)
void drawLine(const QLine &line)
void drawPixmap(const QPoint &point, const QPixmap &pixmap)
void drawPoint(const QPoint &position)
bool end()
void setBrush(Qt::BrushStyle style)
void setPen(Qt::PenStyle style)
int height() const const
QPixmap scaled(const QSize &size, Qt::AspectRatioMode aspectRatioMode, Qt::TransformationMode transformMode) const const
int width() const const
void setX(int x)
void setY(int y)
int x() const const
int y() const const
int height() const const
int width() const const
void setAutoFillBackground(bool enabled)
void setGeometry(const QRect &)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri May 24 2024 11:49:23 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.