[2753] | 1 | // ------------
|
---|
| 2 | // author: jan dousa (jan.dousa@pecny.cz)
|
---|
| 3 | // ------------
|
---|
| 4 |
|
---|
| 5 | #include <iostream>
|
---|
| 6 | #include "bncmap.h"
|
---|
| 7 |
|
---|
| 8 |
|
---|
| 9 | // ------------
|
---|
| 10 | bncMap::bncMap(QWidget* parent) : QDialog(parent)
|
---|
| 11 | {
|
---|
[2796] | 12 | _scale = 2.8; // scale the map
|
---|
| 13 | _LaOff = 25; // shift longitude
|
---|
[2753] | 14 | _mapScen = new QGraphicsScene();
|
---|
[2796] | 15 | _mapView = new BncMapView();
|
---|
[2753] | 16 | _mapView->setScene(_mapScen);
|
---|
[2796] | 17 | _mapView->setMatrix(QMatrix(_scale,0,0,_scale,0,0));
|
---|
[2753] | 18 | slotReadMap();
|
---|
| 19 | slotCreateMap();
|
---|
[2796] | 20 | _mapScen->setSceneRect(QRect(0,-90,360,180));
|
---|
[2753] | 21 |
|
---|
[2796] | 22 | setWindowTitle(tr("Source-Table Map [*]"));
|
---|
[2753] | 23 |
|
---|
| 24 | /* close button */
|
---|
| 25 | QPushButton* buttClose = new QPushButton("Close");
|
---|
| 26 | connect(buttClose, SIGNAL(clicked()), this, SLOT(close()));
|
---|
[2796] | 27 |
|
---|
| 28 | /* rescale button */
|
---|
| 29 | QPushButton* buttClean = new QPushButton("Clean");
|
---|
| 30 | connect(buttClean, SIGNAL(clicked()), this, SLOT(slotCleanMap()));
|
---|
[2753] | 31 |
|
---|
| 32 | /* reset button */
|
---|
| 33 | QPushButton* buttReset = new QPushButton("Reset");
|
---|
| 34 | connect(buttReset, SIGNAL(clicked()), this, SLOT(slotResetMap()));
|
---|
| 35 |
|
---|
[2796] | 36 | /* zoom button */
|
---|
| 37 | QPushButton* buttZoomIn = new QPushButton("Zoom +");
|
---|
| 38 | connect(buttZoomIn, SIGNAL(clicked()), this, SLOT(slotZoomIn()));
|
---|
| 39 |
|
---|
| 40 | /* zoom button */
|
---|
| 41 | QPushButton* buttZoomOut = new QPushButton("Zoom -");
|
---|
| 42 | connect(buttZoomOut, SIGNAL(clicked()), this, SLOT(slotZoomOut()));
|
---|
| 43 |
|
---|
| 44 | /* fit button */
|
---|
| 45 | QPushButton* buttFit = new QPushButton("Fit");
|
---|
| 46 | connect(buttFit, SIGNAL(clicked()), this, SLOT(slotFitMap()));
|
---|
| 47 |
|
---|
[2753] | 48 | /* layout */
|
---|
[2796] | 49 | QVBoxLayout* MapLayout = new QVBoxLayout;
|
---|
| 50 | QHBoxLayout* ButLayout = new QHBoxLayout;
|
---|
| 51 |
|
---|
| 52 | ButLayout->addWidget(buttZoomIn);
|
---|
| 53 | ButLayout->addWidget(buttZoomOut);
|
---|
| 54 | ButLayout->addWidget(buttClean);
|
---|
| 55 | ButLayout->addWidget(buttReset);
|
---|
| 56 | ButLayout->addWidget(buttFit);
|
---|
| 57 | ButLayout->addWidget(buttClose);
|
---|
| 58 |
|
---|
| 59 | MapLayout->addWidget(_mapView);
|
---|
| 60 | MapLayout->addLayout(ButLayout);
|
---|
| 61 |
|
---|
| 62 | setLayout(MapLayout);
|
---|
| 63 |
|
---|
[2753] | 64 | this->show();
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 |
|
---|
| 68 | // ------------
|
---|
| 69 | bncMap::~bncMap(){
|
---|
| 70 | delete _mapView;
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 |
|
---|
| 74 | // ------------
|
---|
| 75 | void bncMap::slotReadMap()
|
---|
| 76 | {
|
---|
[2754] | 77 | QFile world(":worldmap.dat");
|
---|
[2753] | 78 | float fi, la;
|
---|
| 79 |
|
---|
[2754] | 80 | world.open(QIODevice::ReadOnly | QIODevice::Text);
|
---|
[2753] | 81 |
|
---|
[2754] | 82 | QTextStream in(&world);
|
---|
| 83 | in.setRealNumberNotation(QTextStream::FixedNotation);
|
---|
[2753] | 84 |
|
---|
[2754] | 85 | while( ! in.atEnd() ){
|
---|
[2753] | 86 |
|
---|
[2754] | 87 | in >> la >> fi;
|
---|
[2796] | 88 |
|
---|
| 89 | // la = 0-360
|
---|
| 90 | while( la < 0 ){ la += 360; }
|
---|
| 91 | while( la >= 360 ){ la -= 360; }
|
---|
| 92 |
|
---|
| 93 | // fi opposite
|
---|
[2754] | 94 | _worldMap << QPointF( la, -fi );
|
---|
[2796] | 95 |
|
---|
[2753] | 96 | }
|
---|
| 97 | world.close();
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 |
|
---|
| 101 | // ------------
|
---|
| 102 | void bncMap::slotCreateMap()
|
---|
| 103 | {
|
---|
[2796] | 104 | // _mapScen->setForegroundBrush(QBrush(Qt::lightGray, Qt::CrossPattern)); // grid
|
---|
[2753] | 105 |
|
---|
| 106 | int begIdx = 0;
|
---|
| 107 | int endIdx = 0;
|
---|
| 108 | for( int i=0; i < _worldMap.size(); i++ ){
|
---|
| 109 | if( _worldMap.at(i).x() == 0.0 and _worldMap.at(i).y() == 0.0 ){
|
---|
| 110 | if( i > 0 ){
|
---|
[2796] | 111 | endIdx = i-1;
|
---|
[2753] | 112 | while( begIdx < endIdx ){
|
---|
| 113 |
|
---|
[2796] | 114 | int l1 = 0;
|
---|
| 115 | int l2 = 0;
|
---|
[2753] | 116 |
|
---|
| 117 | float la1 = _worldMap.at(begIdx+0).x() + _LaOff;
|
---|
| 118 | float fi1 = _worldMap.at(begIdx+0).y();
|
---|
| 119 | float la2 = _worldMap.at(begIdx+1).x() + _LaOff;
|
---|
| 120 | float fi2 = _worldMap.at(begIdx+1).y();
|
---|
| 121 | begIdx++;
|
---|
[2796] | 122 |
|
---|
| 123 | while( la1 < 0 ){ la1 += 360; l1++; }
|
---|
| 124 | while( la1 >= 360 ){ la1 -= 360; l1--; }
|
---|
| 125 | while( la2 < 0 ){ la2 += 360; l2++; }
|
---|
| 126 | while( la2 >= 360 ){ la2 -= 360; l2--; }
|
---|
[2753] | 127 |
|
---|
[2796] | 128 | if( l1 != 0 and l2 == 0 ){ continue; } // break this line
|
---|
| 129 | if( l2 != 0 and l1 == 0 ){ continue; } // break this line
|
---|
[2753] | 130 |
|
---|
[2796] | 131 | _mapScen->addLine(la1, fi1, la2, fi2, QPen(QBrush(Qt::gray),0.3));
|
---|
[2753] | 132 | }
|
---|
| 133 | }
|
---|
| 134 | if( i+1 < _worldMap.size() ) begIdx = i+1;
|
---|
| 135 | }
|
---|
| 136 | }
|
---|
| 137 | }
|
---|
| 138 |
|
---|
| 139 |
|
---|
| 140 | // ------------
|
---|
[2796] | 141 | void bncMap::slotCleanMap()
|
---|
[2753] | 142 | {
|
---|
| 143 | QMutexLocker locker(&_mutexMap);
|
---|
| 144 | _mapScen->clear();
|
---|
| 145 | slotCreateMap();
|
---|
[2796] | 146 | slotResetMap();
|
---|
[2753] | 147 | }
|
---|
| 148 |
|
---|
| 149 |
|
---|
| 150 | // ------------
|
---|
[2796] | 151 | void bncMap::slotResetMap()
|
---|
| 152 | {
|
---|
| 153 | _mapView->setMatrix(QMatrix(_scale,0,0,_scale,0,0));
|
---|
| 154 | }
|
---|
| 155 |
|
---|
| 156 |
|
---|
| 157 | // ------------
|
---|
| 158 | void bncMap::slotZoomIn()
|
---|
| 159 | {
|
---|
| 160 | _mapView->scale( 1.2, 1.2 );
|
---|
| 161 | }
|
---|
| 162 |
|
---|
| 163 |
|
---|
| 164 | // ------------
|
---|
| 165 | void bncMap::slotZoomOut()
|
---|
| 166 | {
|
---|
| 167 | _mapView->scale( 1/1.2, 1/1.2 );
|
---|
| 168 | }
|
---|
| 169 |
|
---|
| 170 |
|
---|
| 171 | // ------------
|
---|
| 172 | void bncMap::slotFitMap()
|
---|
| 173 | {
|
---|
| 174 | QRectF reg = _allPoints.boundingRect().adjusted(-10,-10,10,10);
|
---|
| 175 |
|
---|
| 176 | _mapView->updateSceneRect(reg);
|
---|
| 177 | _mapView->centerOn(reg.center());
|
---|
| 178 | _mapView->fitInView(reg,Qt::KeepAspectRatio);
|
---|
| 179 | }
|
---|
| 180 |
|
---|
| 181 |
|
---|
| 182 | // ------------
|
---|
[2753] | 183 | void bncMap::slotNewPoint(QPointF point, QString name, QPen pen)
|
---|
| 184 | {
|
---|
[2796] | 185 | float la = point.x() + _LaOff;
|
---|
| 186 | float fi = - point.y();
|
---|
[2753] | 187 |
|
---|
| 188 | while( la < 0 ){ la += 360; }
|
---|
| 189 | while( la >= 360 ){ la -= 360; }
|
---|
[2796] | 190 |
|
---|
| 191 | _allPoints << QPointF(la, fi);
|
---|
| 192 | _mapScen->addEllipse( la, fi, 1.5, 1.5, pen );
|
---|
[2753] | 193 |
|
---|
| 194 | if( ! name.isEmpty() ){
|
---|
| 195 | QGraphicsTextItem* nameItem = new QGraphicsTextItem( name );
|
---|
[2796] | 196 | nameItem->setPos( QPointF(la-1, fi-2));
|
---|
| 197 | nameItem->setFont( QFont("Arial", 2, 1) );
|
---|
[2753] | 198 |
|
---|
| 199 | _mapScen->addItem( nameItem );
|
---|
| 200 | }
|
---|
[2805] | 201 | }
|
---|