source: ntrip/trunk/BNC/src/bncmap_svg.cpp @ 8231

Last change on this file since 8231 was 8231, checked in by wiese, 21 months ago

CHANGE: #105 QT5

File size: 7.8 KB
Line 
1// Part of BNC, a utility for retrieving decoding and
2// converting GNSS data streams from NTRIP broadcasters.
3//
4// Copyright (C) 2007
5// German Federal Agency for Cartography and Geodesy (BKG)
6// http://www.bkg.bund.de
7// Czech Technical University Prague, Department of Geodesy
8// http://www.fsv.cvut.cz
9//
10// Email: euref-ip@bkg.bund.de
11//
12// This program is free software; you can redistribute it and/or
13// modify it under the terms of the GNU General Public License
14// as published by the Free Software Foundation, version 2.
15//
16// This program is distributed in the hope that it will be useful,
17// but WITHOUT ANY WARRANTY; without even the implied warranty of
18// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19// GNU General Public License for more details.
20//
21// You should have received a copy of the GNU General Public License
22// along with this program; if not, write to the Free Software
23// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24
25/* -------------------------------------------------------------------------
26 * BKG NTRIP Client
27 * -------------------------------------------------------------------------
28 *
29 * Class:      t_bncMap
30 *
31 * Purpose:    Plot map of stations from NTRIP source table
32 *
33 * Author:     L. Mervart
34 *
35 * Created:    02-Sep-2012
36 *
37 * Changes:
38 *
39 * -----------------------------------------------------------------------*/
40
41#include <QtSvg>
42#include <QtPrintSupport/QPrinter>
43#include <QtPrintSupport/QPrintDialog>
44
45#include <qwt_symbol.h>
46#include <qwt_plot.h>
47#include <qwt_plot_svgitem.h>
48#include <qwt_plot_curve.h>
49#include <qwt_plot_marker.h>
50#include <qwt_plot_canvas.h>
51#include <qwt_plot_zoomer.h>
52#include <qwt_plot_renderer.h>
53
54#include "bncmap.h"
55
56// Constructor
57/////////////////////////////////////////////////////////////////////////////
58t_bncMap::t_bncMap(QWidget* parent) : QDialog(parent) {
59
60  // Map in Scalable Vector Graphics (svg) Format
61  // --------------------------------------------
62  _mapPlot = new QwtPlot();
63
64  _mapPlot->setAxisScale(QwtPlot::xBottom, -180.0, 180.0);
65  _mapPlot->setAxisScale(QwtPlot::yLeft,    -90.0,  90.0);
66
67  _mapPlotZoomer = new QwtPlotZoomer(_mapPlot->canvas());
68
69  _mapPlot->canvas()->setFocusPolicy(Qt::WheelFocus);
70
71  QwtPlotSvgItem* mapItem = new QwtPlotSvgItem();
72  mapItem->loadFile(QRectF(-180.0, -90.0, 360.0, 180.0), ":world.svg");
73  mapItem->attach(_mapPlot);
74
75  // Buttons
76  // -------
77  int ww = QFontMetrics(font()).width('w');
78
79  _buttonClose = new QPushButton(tr("Close"), this);
80  _buttonClose->setMaximumWidth(10*ww);
81  connect(_buttonClose, SIGNAL(clicked()), this, SLOT(slotClose()));
82
83  _buttonPrint = new QPushButton(tr("Print"), this);
84  _buttonPrint->setMaximumWidth(10*ww);
85  connect(_buttonPrint, SIGNAL(clicked()), this, SLOT(slotPrint()));
86
87  _buttonWhatsThis = new QPushButton(tr("Help=Shift+F1"), this);
88  _buttonWhatsThis->setMaximumWidth(14*ww);
89  connect(_buttonWhatsThis, SIGNAL(clicked()), this, SLOT(slotWhatsThis()));
90
91  // Layout
92  // ------
93  QHBoxLayout* buttonLayout = new QHBoxLayout;
94  buttonLayout->addWidget(_buttonClose);
95  buttonLayout->addWidget(_buttonPrint);
96  buttonLayout->addWidget(_buttonWhatsThis);
97
98  QVBoxLayout* mainLayout = new QVBoxLayout(this);
99  mainLayout->addWidget(_mapPlot);
100  mainLayout->addLayout(buttonLayout);
101
102  // WhatsThis, Map
103  // --------------
104  _buttonClose->setWhatsThis(tr("<p>Close window.</p>"));
105  _buttonPrint->setWhatsThis(tr("<p>Print stream distribution map.</p>"));
106
107  // Minimal and Maximal Coordinates
108  // -------------------------------
109  _minPointLat = 0.0;
110  _maxPointLat = 0.0;
111  _minPointLon = 0.0;
112  _maxPointLon = 0.0;
113
114  // Important
115  // ---------
116  _mapPlot->replot();
117}
118
119// Destructor
120/////////////////////////////////////////////////////////////////////////////
121t_bncMap::~t_bncMap() {
122  delete _mapPlot;
123  delete _buttonWhatsThis;
124}
125
126//
127/////////////////////////////////////////////////////////////////////////////
128void t_bncMap::slotNewPoint(const QString& name, double latDeg, double lonDeg) {
129
130  if (lonDeg > 180.0) lonDeg -= 360.0;
131
132  QColor red(220,20,60);
133  QwtSymbol* symbol = new QwtSymbol(QwtSymbol::Rect, QBrush(red),
134                                    QPen(red), QSize(2,2));
135  QwtPlotMarker* marker = new QwtPlotMarker();
136  marker->setValue(lonDeg, latDeg);
137  if (lonDeg > 170.0) {
138    marker->setLabelAlignment(Qt::AlignLeft);
139  }
140  else {
141    marker->setLabelAlignment(Qt::AlignRight);
142  }
143  QwtText text(name.left(4));
144  QFont   font = text.font();
145  font.setPointSize(int(font.pointSize()*0.8));
146  text.setFont(font);
147  marker->setLabel(text);
148  marker->setSymbol(symbol);
149  marker->attach(_mapPlot);
150
151  // Remeber minimal and maximal coordinates
152  // ---------------------------------------
153  if (_minPointLat == 0.0 && _maxPointLat == 0.0 &&
154      _minPointLon == 0.0 && _maxPointLon == 0.0) {
155    _minPointLat = latDeg;
156    _maxPointLat = latDeg;
157    _minPointLon = lonDeg;
158    _maxPointLon = lonDeg;
159  }
160  else {
161    if      (_maxPointLat < latDeg) {
162      _maxPointLat = latDeg;
163    }
164    else if (_minPointLat > latDeg) {
165      _minPointLat = latDeg;
166    }
167    if      (_maxPointLon < lonDeg) {
168      _maxPointLon = lonDeg;
169    }
170    else if (_minPointLon > lonDeg) {
171      _minPointLon = lonDeg;
172    }
173  }
174}
175
176// Close
177////////////////////////////////////////////////////////////////////////////
178void t_bncMap::slotClose() {
179  done(0);
180}
181
182// Close Dialog gracefully
183////////////////////////////////////////////////////////////////////////////
184void t_bncMap::closeEvent(QCloseEvent* event) {
185  QDialog::closeEvent(event);
186}
187
188//
189////////////////////////////////////////////////////////////////////////////
190void t_bncMap::showEvent(QShowEvent* event) {
191  double width  = _maxPointLon - _minPointLon;
192  double height = _maxPointLat - _minPointLat;
193  if (width > 0 && height > 0) {
194
195    // Extend plot area by 10 percent
196    // ------------------------------
197    double eps = 0.1;
198    double epsLon    = eps * (_maxPointLon - _minPointLon);
199    double epsLat    = eps * (_maxPointLat - _minPointLat);
200    double widthExt  = width  + 2 * epsLon;
201    double heightExt = height + 2 * epsLat;
202    double minLon    = _minPointLon - epsLon;
203    double minLat    = _minPointLat - epsLat;
204
205    // Keep lat/lon relations
206    // ----------------------
207    double widthBorder = widthExt;
208    double heightBorder = heightExt;
209    double scale = widthExt/heightExt/2.;
210    if ( scale < 1.) {
211      widthBorder = widthExt / scale;
212      minLon = minLon - (widthBorder - widthExt)/2.;
213    }
214    else {
215      heightBorder = heightExt * scale;
216      minLat = minLat - (heightBorder - heightExt)/2.;
217    }
218
219    // Borders shall not exceed min or max values
220    // ------------------------------------------
221    if (minLon < -180.) minLon = -180.;
222    if (minLat <  -90.) minLat =  -90.;
223    double maxLat = minLat + heightBorder;
224    if ( maxLat >  90) minLat = minLat - (maxLat -  90.);
225    double maxLon = minLon + widthBorder;
226    if ( maxLon > 180) minLon = minLon - (maxLon - 180.);
227
228    // Area large enough to justify world map
229    // --------------------------------------
230    if (widthBorder < 270.0 && heightBorder < 135.0) {
231      QRectF rect(minLon, minLat, widthBorder, heightBorder);
232      _mapPlotZoomer->zoom(rect);
233    }
234  }
235  QDialog::showEvent(event);
236}
237
238// Print the widget
239////////////////////////////////////////////////////////////////////////////
240void t_bncMap::slotPrint() {
241
242  QPrinter printer;
243  QPrintDialog* dialog = new QPrintDialog(&printer, this);
244  dialog->setWindowTitle(tr("Print Map"));
245  if (dialog->exec() != QDialog::Accepted) {
246    return;
247  }
248  else {
249    QwtPlotRenderer renderer;
250    renderer.setDiscardFlag(QwtPlotRenderer::DiscardBackground, false);
251    //renderer.setLayoutFlag(QwtPlotRenderer::KeepFrames, true);
252    renderer.renderTo(_mapPlot, printer);
253  }
254}
255
256// Whats This Help
257////////////////////////////////////////////////////////////////////////////
258void t_bncMap::slotWhatsThis() {
259QWhatsThis::enterWhatsThisMode();
260}
261
Note: See TracBrowser for help on using the repository browser.