1 |
|
---|
2 | /* -------------------------------------------------------------------------
|
---|
3 | * RTNet GUI
|
---|
4 | * -------------------------------------------------------------------------
|
---|
5 | *
|
---|
6 | * Class: t_svgMap
|
---|
7 | *
|
---|
8 | * Purpose: Plot map of stations/satellites
|
---|
9 | *
|
---|
10 | * Author: L. Mervart
|
---|
11 | *
|
---|
12 | * Created: 05-Jan-2013
|
---|
13 | *
|
---|
14 | * Changes:
|
---|
15 | *
|
---|
16 | * -----------------------------------------------------------------------*/
|
---|
17 |
|
---|
18 | #include <QtSvg>
|
---|
19 |
|
---|
20 | #include <qwt_symbol.h>
|
---|
21 | #include <qwt_plot.h>
|
---|
22 | #include <qwt_plot_svgitem.h>
|
---|
23 | #include <qwt_plot_curve.h>
|
---|
24 | #include <qwt_plot_marker.h>
|
---|
25 | #include <qwt_plot_canvas.h>
|
---|
26 | #include <qwt_plot_zoomer.h>
|
---|
27 | #include <qwt_plot_renderer.h>
|
---|
28 |
|
---|
29 | #include "svgmap.h"
|
---|
30 |
|
---|
31 | using namespace std;
|
---|
32 | using namespace GnssCenter;
|
---|
33 |
|
---|
34 | ///Q_EXPORT_PLUGIN2(svgMap, t_svgMap)
|
---|
35 |
|
---|
36 | // Constructor
|
---|
37 | /////////////////////////////////////////////////////////////////////////////
|
---|
38 | t_svgMap::t_svgMap() : QDialog(), t_pluginInterface() {
|
---|
39 |
|
---|
40 | // Map in Scalable Vector Graphics (svg) Format
|
---|
41 | // --------------------------------------------
|
---|
42 | _mapPlot = new QwtPlot();
|
---|
43 |
|
---|
44 | _mapPlot->setAxisScale(QwtPlot::xBottom, -180.0, 180.0);
|
---|
45 | _mapPlot->setAxisScale(QwtPlot::yLeft, -90.0, 90.0);
|
---|
46 |
|
---|
47 | _mapPlotZoomer = new QwtPlotZoomer(_mapPlot->canvas());
|
---|
48 |
|
---|
49 | _mapPlot->canvas()->setFocusPolicy(Qt::WheelFocus);
|
---|
50 |
|
---|
51 | QwtPlotSvgItem* mapItem = new QwtPlotSvgItem();
|
---|
52 | mapItem->loadFile(QRectF(-180.0, -90.0, 360.0, 180.0), ":world.svg");
|
---|
53 | mapItem->attach(_mapPlot);
|
---|
54 |
|
---|
55 | // Buttons
|
---|
56 | // -------
|
---|
57 | int ww = QFontMetrics(font()).width('w');
|
---|
58 |
|
---|
59 | _buttonClose = new QPushButton(tr("Close"), this);
|
---|
60 | _buttonClose->setMaximumWidth(10*ww);
|
---|
61 | connect(_buttonClose, SIGNAL(clicked()), this, SLOT(slotClose()));
|
---|
62 |
|
---|
63 | _buttonPrint = new QPushButton(tr("Print"), this);
|
---|
64 | _buttonPrint->setMaximumWidth(10*ww);
|
---|
65 | connect(_buttonPrint, SIGNAL(clicked()), this, SLOT(slotPrint()));
|
---|
66 |
|
---|
67 | _buttonWhatsThis = new QPushButton(tr("Help=Shift+F1"), this);
|
---|
68 | _buttonWhatsThis->setMaximumWidth(10*ww);
|
---|
69 | connect(_buttonWhatsThis, SIGNAL(clicked()), this, SLOT(slotWhatsThis()));
|
---|
70 |
|
---|
71 | // Layout
|
---|
72 | // ------
|
---|
73 | QHBoxLayout* buttonLayout = new QHBoxLayout;
|
---|
74 | buttonLayout->addWidget(_buttonClose);
|
---|
75 | buttonLayout->addWidget(_buttonPrint);
|
---|
76 | buttonLayout->addWidget(_buttonWhatsThis);
|
---|
77 |
|
---|
78 | QVBoxLayout* mainLayout = new QVBoxLayout(this);
|
---|
79 | mainLayout->addWidget(_mapPlot);
|
---|
80 | mainLayout->addLayout(buttonLayout);
|
---|
81 |
|
---|
82 | // WhatsThis
|
---|
83 | // ---------
|
---|
84 | _buttonClose->setWhatsThis(tr("<p>Close window.</p>"));
|
---|
85 | _buttonPrint->setWhatsThis(tr("<p>Print stream distribution map.</p>"));
|
---|
86 |
|
---|
87 | // Minimal and Maximal Coordinates
|
---|
88 | // -------------------------------
|
---|
89 | _minPointLat = 0.0;
|
---|
90 | _maxPointLat = 0.0;
|
---|
91 | _minPointLon = 0.0;
|
---|
92 | _maxPointLon = 0.0;
|
---|
93 |
|
---|
94 | // Important
|
---|
95 | // ---------
|
---|
96 | _mapPlot->replot();
|
---|
97 | }
|
---|
98 |
|
---|
99 | // Destructor
|
---|
100 | /////////////////////////////////////////////////////////////////////////////
|
---|
101 | t_svgMap::~t_svgMap() {
|
---|
102 | delete _mapPlot;
|
---|
103 | delete _buttonWhatsThis;
|
---|
104 | }
|
---|
105 |
|
---|
106 | //
|
---|
107 | /////////////////////////////////////////////////////////////////////////////
|
---|
108 | void t_svgMap::slotNewPoint(const QString& name, double latDeg, double lonDeg) {
|
---|
109 |
|
---|
110 | if (lonDeg > 180.0) lonDeg -= 360.0;
|
---|
111 |
|
---|
112 | QColor red(220,20,60);
|
---|
113 | QwtSymbol* symbol = new QwtSymbol(QwtSymbol::Rect, QBrush(red),
|
---|
114 | QPen(red), QSize(2,2));
|
---|
115 | QwtPlotMarker* marker = new QwtPlotMarker();
|
---|
116 | marker->setValue(lonDeg, latDeg);
|
---|
117 | if (lonDeg > 170.0) {
|
---|
118 | marker->setLabelAlignment(Qt::AlignLeft);
|
---|
119 | }
|
---|
120 | else {
|
---|
121 | marker->setLabelAlignment(Qt::AlignRight);
|
---|
122 | }
|
---|
123 | QwtText text(name.left(4));
|
---|
124 | QFont font = text.font();
|
---|
125 | font.setPointSize(font.pointSize()*0.8);
|
---|
126 | text.setFont(font);
|
---|
127 | marker->setLabel(text);
|
---|
128 | marker->setSymbol(symbol);
|
---|
129 | marker->attach(_mapPlot);
|
---|
130 |
|
---|
131 | // Remeber minimal and maximal coordinates
|
---|
132 | // ---------------------------------------
|
---|
133 | if (_minPointLat == 0.0 && _maxPointLat == 0.0 &&
|
---|
134 | _minPointLon == 0.0 && _maxPointLon == 0.0) {
|
---|
135 | _minPointLat = latDeg;
|
---|
136 | _maxPointLat = latDeg;
|
---|
137 | _minPointLon = lonDeg;
|
---|
138 | _maxPointLon = lonDeg;
|
---|
139 | }
|
---|
140 | else {
|
---|
141 | if (_maxPointLat < latDeg) {
|
---|
142 | _maxPointLat = latDeg;
|
---|
143 | }
|
---|
144 | else if (_minPointLat > latDeg) {
|
---|
145 | _minPointLat = latDeg;
|
---|
146 | }
|
---|
147 | if (_maxPointLon < lonDeg) {
|
---|
148 | _maxPointLon = lonDeg;
|
---|
149 | }
|
---|
150 | else if (_minPointLon > lonDeg) {
|
---|
151 | _minPointLon = lonDeg;
|
---|
152 | }
|
---|
153 | }
|
---|
154 | }
|
---|
155 |
|
---|
156 | // Close
|
---|
157 | ////////////////////////////////////////////////////////////////////////////
|
---|
158 | void t_svgMap::slotClose() {
|
---|
159 | done(0);
|
---|
160 | }
|
---|
161 |
|
---|
162 | // Close Dialog gracefully
|
---|
163 | ////////////////////////////////////////////////////////////////////////////
|
---|
164 | void t_svgMap::closeEvent(QCloseEvent* event) {
|
---|
165 | QDialog::closeEvent(event);
|
---|
166 | }
|
---|
167 |
|
---|
168 | //
|
---|
169 | ////////////////////////////////////////////////////////////////////////////
|
---|
170 | void t_svgMap::showEvent(QShowEvent* event) {
|
---|
171 | double width = _maxPointLon - _minPointLon;
|
---|
172 | double height = _maxPointLat - _minPointLat;
|
---|
173 | if (width > 0 && height > 0) {
|
---|
174 |
|
---|
175 | // Extend plot area by 10 percent
|
---|
176 | // ------------------------------
|
---|
177 | double eps = 0.1;
|
---|
178 | double epsLon = eps * (_maxPointLon - _minPointLon);
|
---|
179 | double epsLat = eps * (_maxPointLat - _minPointLat);
|
---|
180 | double widthExt = width + 2 * epsLon;
|
---|
181 | double heightExt = height + 2 * epsLat;
|
---|
182 | double minLon = _minPointLon - epsLon;
|
---|
183 | double minLat = _minPointLat - epsLat;
|
---|
184 |
|
---|
185 | // Keep lat/lon relations
|
---|
186 | // ----------------------
|
---|
187 | double widthBorder = widthExt;
|
---|
188 | double heightBorder = heightExt;
|
---|
189 | double scale = widthExt/heightExt/2.;
|
---|
190 | if ( scale < 1.) {
|
---|
191 | widthBorder = widthExt / scale;
|
---|
192 | minLon = minLon - (widthBorder - widthExt)/2.;
|
---|
193 | }
|
---|
194 | else {
|
---|
195 | heightBorder = heightExt * scale;
|
---|
196 | minLat = minLat - (heightBorder - heightExt)/2.;
|
---|
197 | }
|
---|
198 |
|
---|
199 | // Borders shall not exceed min or max values
|
---|
200 | // ------------------------------------------
|
---|
201 | if (minLon < -180.) minLon = -180.;
|
---|
202 | if (minLat < -90.) minLat = -90.;
|
---|
203 | double maxLat = minLat + heightBorder;
|
---|
204 | if ( maxLat > 90) minLat = minLat - (maxLat - 90.);
|
---|
205 | double maxLon = minLon + widthBorder;
|
---|
206 | if ( maxLon > 180) minLon = minLon - (maxLon - 180.);
|
---|
207 |
|
---|
208 | // Area large enough to justify world map
|
---|
209 | // --------------------------------------
|
---|
210 | if (widthBorder < 270.0 && heightBorder < 135.0) {
|
---|
211 | QRectF rect(minLon, minLat, widthBorder, heightBorder);
|
---|
212 | _mapPlotZoomer->zoom(rect);
|
---|
213 | }
|
---|
214 | }
|
---|
215 | QDialog::showEvent(event);
|
---|
216 | }
|
---|
217 |
|
---|
218 | // Print the widget
|
---|
219 | ////////////////////////////////////////////////////////////////////////////
|
---|
220 | void t_svgMap::slotPrint() {
|
---|
221 |
|
---|
222 | QPrinter printer;
|
---|
223 | QPrintDialog* dialog = new QPrintDialog(&printer, this);
|
---|
224 | dialog->setWindowTitle(tr("Print Map"));
|
---|
225 | if (dialog->exec() != QDialog::Accepted) {
|
---|
226 | return;
|
---|
227 | }
|
---|
228 | else {
|
---|
229 | QwtPlotRenderer renderer;
|
---|
230 | renderer.setDiscardFlag(QwtPlotRenderer::DiscardBackground, false);
|
---|
231 | renderer.setLayoutFlag(QwtPlotRenderer::KeepFrames, true);
|
---|
232 | renderer.renderTo(_mapPlot, printer);
|
---|
233 | }
|
---|
234 | }
|
---|
235 |
|
---|
236 | // Whats This Help
|
---|
237 | ////////////////////////////////////////////////////////////////////////////
|
---|
238 | void t_svgMap::slotWhatsThis() {
|
---|
239 | QWhatsThis::enterWhatsThisMode();
|
---|
240 | }
|
---|
241 |
|
---|