Subversion Repositories public

Rev

Rev 283 | Rev 285 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
283 andreas 1
//
2
// C++ Implementation:
3
//
4
// Description:
5
//
6
//
7
// Author: Andreas Theofilu <andreas@theosys.at>, (C) 2009
8
//
9
// Copyright: See COPYING file that comes with this distribution
10
//
11
//
12
 
13
#include <iostream>
14
 
15
#include <QPixmap>
16
#include <QBitmap>
17
#include <QDir>
18
#include <QFile>
19
#include <QImage>
20
#include <QtNetwork>
21
#include <QListWidget>
22
#include <QPainter>
23
#include <QCheckBox>
284 andreas 24
#include <QSlider>
283 andreas 25
#include <KStandardDirs>
26
#include <KConfig>
27
#include <KLocale>
28
#include <KMessageBox>
29
#include <KGlobalSettings>
30
#include <KConfigGroup>
31
#include <KIconLoader>
32
#include <KUrl>
33
#include <KUrlRequester>
34
#include <KFile>
35
 
36
#include "config.h"
37
 
38
#if defined HAVE_GDAL
39
   #include <gdal/gdal_priv.h>
40
   #include <gdal/ogr_spatialref.h>
41
   #include <gdal/ogrsf_frmts.h>
42
   #include <gdal/gdalwarper.h>
43
   #include <gdal/ogrsf_frmts.h>
44
   #include "GDALError.h"
45
#endif
46
 
47
#include "wmsselectorwidget.h"
48
#include "ui_authenticationdialog.h"
49
#include "wmscapabilitieswidget.h"
50
 
51
// The capital cities from all countries of the world
52
// Source: Wikipedia, Google
53
CITIES Cities[] = {
284 andreas 54
	{ " - - - - - - -", 0.0, 0.0 },
283 andreas 55
	{ "Abidjan", 5.3363, -4.0277 },
56
	{ "Abu Dhabi", 24.4758, 54.3714 },
57
	{ "Abuja", 9.0578, 7.4892 },
58
	{ "Accora", 5.5555, -0.196 },
59
	{ "Addis Abeba", 9.0164, 38.7579 },
60
	{ "Algier", 36.7527, 3.0422 },
61
	{ "Amman", 31.9565, 35.9457 },
62
	{ "Amsterdam", 52.3737, 4.8911 },
63
	{ "Andorra la Vella", 42.5074, 1.5219 },
64
	{ "Ankara", 39.9438, 32.856 },
65
	{ "Antananarivo", -18.915, 47.5318 },
66
	{ "Apia", -13.8314, -171.7516 },
67
	{ "Aschgabat", 37.9501, 58.3802 },
68
	{ "Asmara", 15.3324, 38.9261 },
69
	{ "Astana", 51.1798, 71.4466 },
70
	{ "Asuncion", -25.3003, -57.6363 },
71
	{ "Athen", 37.9792, 23.7166 },
72
	{ "Bagdad", 33.3158, 44.3921 },
73
	{ "Baku", 40.4167, 49.8236 },
74
	{ "Bamako", 12.6529, -7.9865 },
75
	{ "Bandar Seri Begawan", 4.9431, 114.9424 },
76
	{ "Bankok", 13.7211, 100.4765 },
77
	{ "Bangui", 4.3616, 18.556 },
78
	{ "Banjul", 13.4548, -16.579 },
79
	{ "Basseterre", 17.2968, -62.7137 },
80
	{ "Beirut", 33.8886, 35.4954 },
81
	{ "Belgrad", 44.8024, 20.4651 },
82
	{ "Belmopan", 17.2617, -88.7785 },
83
	{ "Berlin", 52.5234, 13.4105 },
84
	{ "Bern", 46.9479, 7.4479 },
85
	{ "Bratislava", 48.1483, 17.1068 },
86
	{ "Bridgetown", 13.0935, -59.6106 },
284 andreas 87
	{ "Bruessel", 50.8462, 4.3545 },
283 andreas 88
	{ "Budapest", 47.4984, 19.0403 },
89
	{ "Buenos Aires", -34.6084, -58.3733 },
90
	{ "Bukarest", 44.4304, 26.1225 },
91
	{ "Den Haag", 52.0782, 4.3134 },
92
	{ "Dublin", 53.3441, -6.2677 },
93
	{ "Helsinki", 60.1698, 24.9379 },
94
	{ "Kopenhagen", 55.6762, 12.5678 },
95
	{ "Lissabon", 38.7071, -9.1357 },
96
	{ "Ljubljana", 46.0514, 14.5055 },
97
	{ "London", 51.5001, -0.1262 },
98
	{ "Luxemburg", 49.8152, 6.1286 },
99
	{ "Madrid", 40.4167, -3.7036 },
100
	{ "Mexiko City", 19.427, -99.128 },
101
	{ "Minsk", 53.8996, 27.5753 },
102
	{ "Monaco", 43.7326, 7.4189 },
103
	{ "Moskau", 55.7554, 37.6203 },
104
	{ "Oslo", 59.9136, 10.7394 },
105
	{ "Ottawa", 45.4218, -75.6964 },
106
	{ "Paris", 48.856, 2.3515 },
107
	{ "Peking", 39.9077, 116.3994 },
108
	{ "Prag", 50.088, 14.4217 },
109
	{ "Reykjavik", 64.1352, -21.8951 },
110
	{ "Riga", 56.9462, 24.1049 },
111
	{ "Rom", 41.8945, 12.4826 },
112
	{ "San Marino", 43.9322, 12.4484 },
113
	{ "Sofia", 42.6963, 23.3236 },
114
	{ "Stockholm", 59.3329, 18.0652 },
115
	{ "Tirana", 41.3309, 19.8325 },
116
	{ "Tokio", 35.688, 139.6932 },
117
	{ "Vaduz", 47.1407, 9.5218 },
118
	{ "Valletta", 35.9009, 14.5154 },
119
	{ "Vilnius", 54.6888, 25.2801 },
120
	{ "Warschau", 52.2291, 21.0123 },
121
	{ "Washington D.C.", 38.8917, -77.0239 },
122
	{ "Wien", 48.2091, 16.3727 },
123
	{ "Zagreb", 45.8149, 15.9784 },
124
	{ "", 0.0, 0.0 }
125
};
126
 
284 andreas 127
ZOOM zoom[] = {
128
	{ 0.014108, 0.003554 },
129
	{ 0.028216, 0.007108 },
130
	{ 0.056432, 0.014216 },
131
	{ 0.112864, 0.028432 },
132
	{ 0.225728, 0.056864 },
133
	{ 0.451456, 0.113728 },
134
	{ 0.902912, 0.227456 },
135
	{ 1.805824, 0.454912 },
136
	{ 3.611648, 0.909824 },
137
	{ 7.223296, 1.819648 }
138
};
139
 
283 andreas 140
wmsSelectorWidget::wmsSelectorWidget (QWidget* parent, Qt::WFlags fl)
141
		: QDialog (parent, fl), Ui::wmsSelectorWidgetBase()
142
{
143
	// Initialize some variables
144
	http = 0;
145
	httpRequestAborted = false;
146
	progressDialog = 0;
147
	id = 0;
148
	Layer = 0;
149
	firstLayer = 0;
284 andreas 150
	zLevel = 0;
283 andreas 151
	initializeParser();
152
	// Initialize the GUI elements
153
	setupUi (this);
154
	// Initialize the environment
155
	urlWMS->setMode(KFile::File | KFile::ExistingOnly);
156
	KConfig cfg (QString("sportwatcher.rc"), KConfig::SimpleConfig);
157
	KConfigGroup ic (&cfg, "WMS");
158
	urlWMS->setUrl (ic.readEntry("ServerURL", "http://onearth.jpl.nasa.gov/wms.cgi"));
159
	Data = ic.readEntry("Data", QDir::home().absolutePath() + "/.sportwatcher");
160
	MAP = ic.readEntry("MAP", QDir::home().absolutePath() + "/.sportwatcher/track.wms");
161
	file.setFileName(Data + "/capabilities.xml");	// File to save WMS capabilities
162
	edBands->setValue(ic.readEntry("Bands", 3));
284 andreas 163
	Layers = ic.readEntry("Layer", QString("modis,global_mosaic"));
283 andreas 164
	StyleCommas = true;
165
	cbStyles->setChecked(StyleCommas);
166
	// Fill the cities combo box
167
	int i = 0;
168
 
169
	while (!Cities[i].city.isEmpty())
170
	{
171
	   cbCities->addUrl(KUrl(Cities[i].city));
172
	   i++;
173
	}
174
 
175
	// Connect the http module
176
	progressDialog = new KProgressDialog(this);
177
	http = new QHttp(this);
178
	connect(http, SIGNAL(requestFinished(int, bool)), this, SLOT(WMSrequestFinished(int, bool)));
179
	connect(http, SIGNAL(dataReadProgress(int, int)), this, SLOT(updateDataReadProgress(int, int)));
180
	connect(http, SIGNAL(responseHeaderReceived(const QHttpResponseHeader &)), this, SLOT(readResponseHeader(const QHttpResponseHeader &)));
181
	connect(http, SIGNAL(authenticationRequired(const QString &, quint16, QAuthenticator *)),
182
		this, SLOT(slotAuthenticationRequired(const QString &, quint16, QAuthenticator *)));
183
	connect(progressDialog, SIGNAL(cancelClicked()), this, SLOT(cancelDownload()));
184
#ifndef QT_NO_OPENSSL
185
	connect(http, SIGNAL(sslErrors(const QList<QSslError> &)), this, SLOT(sslErrors(const QList<QSslError> &)));
186
#endif
187
	getCapabilities();
284 andreas 188
	Init = true;
283 andreas 189
}
190
 
191
wmsSelectorWidget::~wmsSelectorWidget()
192
{
193
	if (file.exists())
194
	   file.remove();
195
 
196
	destroyErrors();
197
}
198
 
199
/*$SPECIALIZATION$*/
200
void wmsSelectorWidget::slotGetCapabilities()
201
{
202
	actionLayer->availableListWidget()->clear();
203
	actionLayer->selectedListWidget()->clear();
204
	getCapabilities();
205
}
206
 
207
void wmsSelectorWidget::slotDetails()
208
{
209
	wmsCapabilitiesWidget *dlg = new wmsCapabilitiesWidget(this);
210
	dlg->setPerson(Service.ContactPerson);
211
	dlg->setOrg(Service.ContactOrganisation);
212
	dlg->setPhone(Service.ContactVoiceTelephone);
213
	dlg->setFax(Service.ContactFacsimileTelephone);
214
	dlg->setMail(Service.ContactElectronicMailAddress);
215
	dlg->setCapabilitiesGet(Request.GetCapabilities.Get);
216
	dlg->setCapabilitiesPost(Request.GetCapabilities.Post);
217
 
218
	if (Request.GetCapabilities.Format.size() > 0)
219
	   dlg->setFormat(Request.GetCapabilities.Format.at(0));
220
 
221
	dlg->setMapGet(Request.GetMap.Get);
222
	dlg->setMapPost(Request.GetMap.Post);
223
 
224
	for (int i = 0; i < Request.GetMap.Format.size(); ++i)
225
	   dlg->addFormats(Request.GetMap.Format.at(i));
226
 
227
	dlg->exec();
228
	delete dlg;
229
}
230
 
231
void wmsSelectorWidget::slotAdded(QListWidgetItem *item)
232
{
233
double minx, miny, maxx, maxy;
234
 
235
	setDimension(minx, miny, maxx, maxy, item);
236
	grabPicture(minx, miny, maxx, maxy);
237
}
238
 
239
void wmsSelectorWidget::slotRemoved(QListWidgetItem *item)
240
{
241
double minx, miny, maxx, maxy;
242
 
243
	setDimension(minx, miny, maxx, maxy, item);
244
	grabPicture(minx, miny, maxx, maxy);
245
}
246
 
247
void wmsSelectorWidget::slotUp(QListWidgetItem *item)
248
{
249
double minx, miny, maxx, maxy;
250
 
251
	setDimension(minx, miny, maxx, maxy, item);
252
	grabPicture(minx, miny, maxx, maxy);
253
}
254
 
255
void wmsSelectorWidget::slotDown(QListWidgetItem *item)
256
{
257
double minx, miny, maxx, maxy;
258
 
259
	setDimension(minx, miny, maxx, maxy, item);
260
	grabPicture(minx, miny, maxx, maxy);
261
}
262
 
263
void wmsSelectorWidget::slotCities(QString city)
264
{
265
int i = 0;
266
double minx, miny, maxx, maxy;
267
 
268
	while (!Cities[i].city.isEmpty())
269
	{
270
	   if (Cities[i].city == city)
271
	   {
272
	      edLat->setValue(Cities[i].lat);
273
	      edLon->setValue(Cities[i].lon);
274
	      break;
275
	   }
276
 
277
	   i++;
278
	}
279
 
280
	if (actionLayer->selectedListWidget()->count() > 0)
281
	{
282
	   setDimension(minx, miny, maxx, maxy, 0);
283
	   grabPicture(minx, miny, maxx, maxy);
284
	}
285
}
286
 
287
void wmsSelectorWidget::slotStyles(bool mode)
288
{
289
	StyleCommas = mode;
290
}
291
 
284 andreas 292
void wmsSelectorWidget::slotSliderPressed()
293
{
294
	sliderPressed = true;
295
}
296
 
297
void wmsSelectorWidget::slotSliderReleased()
298
{
299
double minx, miny, maxx, maxy;
300
 
301
	sliderPressed = false;
302
 
303
	if (!sliderPressed && actionLayer->selectedListWidget()->count() > 0)
304
	{
305
	   setDimension(minx, miny, maxx, maxy, 0);
306
	   grabPicture(minx, miny, maxx, maxy);
307
	}
308
}
309
 
310
void wmsSelectorWidget::slotValueChanged(int val)
311
{
312
double minx, miny, maxx, maxy;
313
 
314
	zLevel = val;
315
 
316
	if (!sliderPressed && actionLayer->selectedListWidget()->count() > 0)
317
	{
318
	   setDimension(minx, miny, maxx, maxy, 0);
319
	   grabPicture(minx, miny, maxx, maxy);
320
	}
321
}
322
 
283 andreas 323
/*
324
 * This functions try to get a small example picture from the WMS server
325
 */
326
bool wmsSelectorWidget::grabPicture(double minx, double miny, double maxx, double maxy)
327
{
328
#if defined HAVE_GDAL
329
QPixmap pm;
330
GDALDataset *poDataset = 0;
331
GDALRasterBand *poBand = 0;
332
unsigned char *pafScanline = 0;
333
unsigned char *pafScanlineRed = 0;
334
unsigned char *pafScanlineGreen = 0;
335
unsigned char *pafScanlineBlue = 0;
336
unsigned char *pafScanlineAlpha = 0;
337
int nXSize, nYSize;
338
int width, height;
339
bool Fgeo = false;
340
QPainter paint;
341
 
342
	QApplication::setOverrideCursor (QCursor(Qt::WaitCursor));
343
	width = lbPreview->width();
284 andreas 344
	height = lbPreview->height() - 6;	// necessary to prevent growing of dialog window
283 andreas 345
	nXSize = nYSize = 0;
346
 
347
	if (!writeWMSTag(minx, miny, maxx, maxy, width, height))
348
	{
349
	   QApplication::restoreOverrideCursor();
350
	   KMessageBox::error(this, i18n("Error writing a tag file!"));
351
	   return false;
352
	}
353
 
354
	// set the dimensions of the pixmap
355
	pm = QPixmap(width, height);
356
	// start painting the map
357
	paint.begin(&pm);
358
	QColor background(220, 220, 220);
359
	paint.fillRect(0, 0, width, height, background);
360
 
361
	if ((poDataset = (GDALDataset *)GDALOpen (MAP.toAscii().constData(), GA_ReadOnly)) != NULL)
362
	{
363
	   int nRasterCount = poDataset->GetRasterCount();
364
	   int nXBlock, nYBlock;
365
	   GDALColorTable *pCT, *pCTb, *pCTr, *pCTg, *pCTa;
366
 
367
	   int             bGotMin, bGotMax;
368
	   int             tTypeLen, tColor, tColorEntrys;
369
	   GDALDataType    tRasterType;
370
	   double          adfMinMax[2];
371
 
372
	   pCT = pCTb = pCTr = pCTg = pCTa = 0;
373
	   tTypeLen = 0;
374
	   pafScanlineRed = pafScanlineGreen = pafScanlineBlue = pafScanlineAlpha = 0;
375
	   Fgeo = true;
376
 
377
	   for (int a = 1; a <= nRasterCount; a++)
378
	   {
379
	      if (!Fgeo)
380
	        break;
381
 
382
	      if (!(poBand = poDataset->GetRasterBand (a)))
383
	      {
384
		 paint.end();
385
		 QApplication::restoreOverrideCursor();
386
		 KMessageBox::error(this, i18n("Error getting a raster band:\n%1").arg(catGDALError()));
387
 
388
		 if (pafScanlineRed)
389
		    delete pafScanlineRed;
390
 
391
		 if (pafScanlineGreen)
392
		    delete pafScanlineGreen;
393
 
394
		 if (pafScanlineBlue)
395
		    delete pafScanlineBlue;
396
 
397
		 if (pafScanlineAlpha)
398
		    delete pafScanlineAlpha;
399
 
400
		 GDALClose (poDataset);
401
		    poDataset = 0;
402
 
403
		 return false;
404
	      }
405
 
406
	      poBand->GetBlockSize (&nXBlock, &nYBlock);
407
	      nXSize = poBand->GetXSize();
408
	      nYSize = poBand->GetYSize();
409
	      tRasterType = poBand->GetRasterDataType ();
410
	      tTypeLen = GDALGetDataTypeSize (tRasterType) / 8;	// We need Bytes not Bits!
411
	      tColor = poBand->GetColorInterpretation ();
412
 
413
	      adfMinMax[0] = poBand->GetMinimum (&bGotMin);
414
	      adfMinMax[1] = poBand->GetMaximum (&bGotMax);
415
 
416
	      if (!(bGotMin && bGotMax))
417
		 GDALComputeRasterMinMax ((GDALRasterBandH)poBand, TRUE, adfMinMax);
418
 
419
	      if ((pCT = poBand->GetColorTable()) != NULL)
420
		 tColorEntrys = poBand->GetColorTable()->GetColorEntryCount();
421
 
422
	      switch (a)
423
	      {
424
		 case 1: pafScanlineRed   = new unsigned char[tTypeLen * nXSize * nYSize]; pafScanline = pafScanlineRed; pCTr = pCT; break;
425
		 case 2: pafScanlineGreen = new unsigned char[tTypeLen * nXSize * nYSize]; pafScanline = pafScanlineGreen; pCTg = pCT; break;
426
		 case 3: pafScanlineBlue  = new unsigned char[tTypeLen * nXSize * nYSize]; pafScanline = pafScanlineBlue; pCTb = pCT; break;
427
		 case 4: pafScanlineAlpha  = new unsigned char[tTypeLen * nXSize * nYSize]; pafScanline = pafScanlineAlpha; pCTa = pCT; break;
428
	      }
429
 
430
	      if (!pafScanline)
431
	      {
432
		 paint.end();
433
		 QApplication::restoreOverrideCursor();
434
		 KMessageBox::error(this, i18n("Not enough memory for a raster operation!"));
435
 
436
		 if (pafScanlineRed)
437
		    delete pafScanlineRed;
438
 
439
		 if (pafScanlineGreen)
440
		    delete pafScanlineGreen;
441
 
442
		 if (pafScanlineBlue)
443
		    delete pafScanlineBlue;
444
 
445
		 if (pafScanlineAlpha)
446
		    delete pafScanlineAlpha;
447
 
448
		 GDALClose (poDataset);
449
		    poDataset = 0;
450
 
451
		 return false;
452
	      }
453
 
454
	      memset (pafScanline, 0, tTypeLen * nXSize * nYSize);
455
 
456
	      /*
457
	       * Get the image (from the server) and put the tiles together.
458
	       *
459
	       * The function reads only one raster band. This is,
460
	       * because the function is called for every raster band and
461
	       * every raster band is stored into a separate array.
462
	       */
463
	      if (poBand->RasterIO (GF_Read, 0, 0, nXSize, nYSize, pafScanline, nXSize, nYSize, tRasterType, 0, 0) == CE_Failure)
464
	      {
465
		 paint.end();
466
		 QApplication::restoreOverrideCursor();
467
		 KMessageBox::error(this, i18n("Error reading a raster band:\n%1").arg(catGDALError()));
468
		 QApplication::setOverrideCursor (QCursor(Qt::WaitCursor));
469
		 paint.begin(&pm);
470
		 Fgeo = false;
471
		 break;
472
	      }
473
	      else
474
		 Fgeo = true;
475
	   }
476
 
477
	   /*
478
	    * Only if Fgeo is TRUE, we've read successfully all raster
479
	    * bands. Now we have to put the bands together to get
480
	    * an image.
481
	    */
482
	   if (Fgeo)
483
	   {
484
	   unsigned char *pCombinedBytes = new unsigned char[(tTypeLen * nXSize * nYSize * nRasterCount)];
485
	   unsigned char *ptr_dest, *ptr_src;
486
	   int j, x1, y1, a;
487
 
488
	      ptr_dest = ptr_src = 0;
489
 
490
	      /*
491
	       * We need two nested loops to set the pixels in the wanted
492
	       * order.
493
	       */
494
	      for (a = 0, j = 0; a < (nXSize * nYSize * nRasterCount); a += nRasterCount, j++)
495
	      {
496
		 int k = a;
497
 
498
		 for (int m = nRasterCount - 1; m >= 0; m--, k++)
499
		 {
500
		    unsigned char *pBytes = 0;
501
 
502
		    switch (m)
503
		    {
504
		       case 3: pBytes = pafScanlineAlpha; pCT = pCTa; break;
505
		       case 2: pBytes = pafScanlineBlue; pCT = pCTb; break;
506
		       case 1: pBytes = pafScanlineGreen; pCT = pCTg; break;
507
		       default: pBytes = pafScanlineRed; pCT = pCTr;
508
		    }
509
 
510
		    ptr_dest = pCombinedBytes + k;
511
		    unsigned char b = pBytes[j];
512
 
513
		    /*
514
		     * If we have a color table, the pixels are pointers
515
		     * to the color table. We need to convert them into
516
		     * 24 bit pixels plus an optional alpha channel.
517
		     */
518
		    if (pCT != NULL)
519
		    {
520
		       GDALColorEntry ce;
521
		       unsigned int c = (unsigned int)b;
522
		       c = pCT->GetColorEntryAsRGB (c, &ce);
523
 
524
		       if  (m == 0) c = ce.c1;
525
		       if  (m == 1) c = ce.c2;
526
		       if  (m == 2) c = ce.c3;
527
		       if  (m == 3) c = ce.c4;
528
 
529
		       b = (unsigned char)c;
530
		    }
531
 
532
		    ptr_src = &b;
533
		    memcpy (ptr_dest, ptr_src, 1);
534
		 }
535
	      }
536
 
537
	      x1 = y1 = 0;
538
 
539
	      /*
540
	       * The following loop is QT specific! It sets the pixels
541
	       * of the raw image, pixel by pixel. This may be slow, but
542
	       * everything else didn't work :-(
543
	       *
544
	       * FIXME: We need a more effective routine to put the
545
	       *        raw image into QT's "painter" class.
546
	       */
547
	      for (a = 0; a < (nXSize * nYSize * nRasterCount); a += nRasterCount)
548
	      {
549
		 if (x1 < width && y1 < height)
550
		 {
551
		    if (nRasterCount == 3)
552
		       paint.setPen (QPen(QColor((int)pCombinedBytes[a+2], (int)pCombinedBytes[a+1], (int)pCombinedBytes[a]), Qt::SolidLine));
553
		    else if (nRasterCount > 3)
554
		       paint.setPen (QPen(QColor(qRgba((int)pCombinedBytes[a+3], (int)pCombinedBytes[a+2], (int)pCombinedBytes[a+1], (int)pCombinedBytes[a])), Qt::SolidLine));
555
		    else if (nRasterCount == 2)
556
		       paint.setPen (QPen(QColor((int)pCombinedBytes[a+1], (int)pCombinedBytes[a], (int)pCombinedBytes[a+1]), Qt::SolidLine));
557
		    else if (nRasterCount == 1)
558
		       paint.setPen (QPen(QColor((int)pCombinedBytes[a], (int)pCombinedBytes[a], (int)pCombinedBytes[a]), Qt::SolidLine));
559
 
560
		    paint.drawPoint(x1, y1);
561
		 }
562
 
563
		 x1++;
564
 
565
		 if (x1 >= nXSize)
566
		 {
567
		    x1 = 0;
568
		    y1++;
569
		 }
570
	      }
571
 
572
	      delete pCombinedBytes;
573
	      pCombinedBytes = 0;
574
	   }
575
 
576
	   if (pafScanlineRed)
577
	      delete pafScanlineRed;
578
 
579
	   if (pafScanlineGreen)
580
	      delete pafScanlineGreen;
581
 
582
	   if (pafScanlineBlue)
583
	      delete pafScanlineBlue;
584
 
585
	   if (pafScanlineAlpha)
586
	      delete pafScanlineAlpha;
587
 
588
	   GDALClose (poDataset);
589
	      poDataset = 0;
590
	}
591
	else
592
	{
593
	   paint.end();
594
	   lbPreview->setPixmap(pm);
595
	   QApplication::restoreOverrideCursor();
596
	   KMessageBox::error(this, i18n("Error initializing GDAL:\n%1").arg(catGDALError()));
597
	   return false;
598
	}
599
 
600
	paint.end();
601
	lbPreview->setPixmap(pm);
602
	QApplication::restoreOverrideCursor();
603
#endif
604
	return true;
605
}
606
 
607
/*
608
 * This part is a parser, able to read an XML-file produced by a WMS-server.
609
 * The file contains informations about the layers and styles the server
610
 * supports.
611
 */
612
bool wmsSelectorWidget::startDocument()
613
{
614
	initializeParser();
615
	Exception.clear();
616
	WMSVersion.clear();
617
	element = el2 = el_none;
618
	attribute = at_none;
619
	line = 0;
620
	lcount = 0;
621
	return true;
622
}
623
 
624
bool wmsSelectorWidget::startElement( const QString&, const QString&,
625
                                    const QString& qName,
626
                                    const QXmlAttributes& att)
627
{
628
int index;
629
 
630
	attribute = at_none;
631
 
632
	if (qName.toLower() == QString("wmt_ms_capabilities"))
633
	{
634
	   element = el_WMT_MS_Capabilities;
635
 
636
	   if ((index = att.index(QString("version"))) != -1)
637
	      WMSVersion = att.value(index);
638
	}
639
	else if (qName.toLower() == QString("service"))
640
	   element = el_Service;
641
	else if (qName.toLower() == QString("name"))
642
	   attribute = at_Name;
643
	else if (qName.toLower() == QString("title"))
644
	   attribute = at_Title;
645
	else if (qName.toLower() == QString("onlineresource"))
646
	{
647
	   if (element == el_Service)
648
	   {
649
	      if ((index = att.index(QString("href"))) != -1)
650
		 Service.OnlineResource = att.value(index);
651
 
652
	      if ((index = att.index(QString("type"))) != -1)
653
		 Service.Type = att.value(index);
654
	   }
655
	   else if (element == el_GetCapabilities && el2 == el_Get)
656
	   {
657
	      if ((index = att.index(QString("href"))) != -1)
658
		 Request.GetCapabilities.Get = att.value(index);
659
	   }
660
	   else if (element == el_GetCapabilities && el2 == el_Post)
661
	   {
662
	      if ((index = att.index(QString("href"))) != -1)
663
		 Request.GetCapabilities.Post = att.value(index);
664
	   }
665
	   else if (element == el_GetMap && el2 == el_Get)
666
	   {
667
	      if ((index = att.index(QString("href"))) != -1)
668
		 Request.GetMap.Get = att.value(index);
669
	   }
670
	   else if (element == el_GetMap && el2 == el_Post)
671
	   {
672
	      if ((index = att.index(QString("href"))) != -1)
673
		 Request.GetMap.Post = att.value(index);
674
	   }
675
	   else if (element == el_GetFeatureInfo && el2 == el_Get)
676
	   {
677
	      if ((index = att.index(QString("href"))) != -1)
678
		 Request.GetFeatureInfo.Get = att.value(index);
679
	   }
680
	   else if (element == el_GetFeatureInfo && el2 == el_Post)
681
	   {
682
	      if ((index = att.index(QString("href"))) != -1)
683
		 Request.GetFeatureInfo.Post = att.value(index);
684
	   }
685
	}
686
	else if (qName.toLower() == QString("contactinformation"))
687
	   element = el_ContactInformation;
688
	else if (qName.toLower() == QString("contactpersonprimary"))
689
	   element = el_ContactPersonPrimary;
690
	else if (qName.toLower() == QString("contactperson"))
691
	   attribute = at_ContactPerson;
692
	else if (qName.toLower() == QString("contactorganisation"))
693
	   attribute = at_ContactOrganisation;
694
	else if (qName.toLower() == QString("contactposition"))
695
	   attribute = at_ContactPosition;
696
	else if (qName.toLower() == QString("contactaddress"))
697
	   element = el_ContactAddress;
698
	else if (qName.toLower() == QString("addresstype"))
699
	   attribute = at_AddressType;
700
	else if (qName.toLower() == QString("address"))
701
	   attribute = at_Address;
702
	else if (qName.toLower() == QString("city"))
703
	   attribute = at_City;
704
	else if (qName.toLower() == QString("stateorprovince"))
705
	   attribute = at_StateOrProvince;
706
	else if (qName.toLower() == QString("postcode"))
707
	   attribute = at_PostCode;
708
	else if (qName.toLower() == QString("country"))
709
	   attribute = at_Country;
710
	else if (qName.toLower() == QString("contactvoicetelephone"))
711
	   attribute = at_ContactVoiceTelephone;
712
	else if (qName.toLower() == QString("contactfacsimiletelephone"))
713
	   attribute = at_ContactFacsimileTelephone;
714
	else if (qName.toLower() == QString("contactelectronicmailaddress"))
715
	   attribute = at_ContactElectronicMailAddress;
716
	else if (qName.toLower() == QString("fees"))
717
	   attribute = at_Fees;
718
	else if (qName.toLower() == QString("accessconstraints"))
719
	   attribute = at_AccessConstraints;
720
	else if (qName.toLower() == QString("capability"))
721
	   element = el_Capability;
722
	else if (qName.toLower() == QString("request"))
723
	   element = el_Request;
724
	else if (qName.toLower() == QString("getcapabilities"))
725
	   element = el_GetCapabilities;
726
	else if (qName.toLower() == QString("format"))
727
	   attribute = at_Format;
728
	else if (qName.toLower() == QString("dcptype"))
729
	   el2 = el_DCPType;
730
	else if (qName.toLower() == QString("http"))
731
	   el2 = el_HTTP;
732
	else if (qName.toLower() == QString("get"))
733
	   el2 = el_Get;
734
	else if (qName.toLower() == QString("post"))
735
	   el2 = el_Post;
736
	else if (qName.toLower() == QString("getmap"))
737
	   element = el_GetMap;
738
	else if (qName.toLower() == QString("getfeatureinfo"))
739
	   element = el_GetFeatureInfo;
740
	else if (qName.toLower() == QString("exception"))
741
	   element = el_Exception;
742
	else if (qName.toLower() == QString("layer"))
743
	{
744
	LAYER *akt;
745
 
746
	   element = el_Layer;
747
	   lcount++;
748
	   akt = allocateLayer();
749
 
750
	   if (firstLayer == 0)
751
	      firstLayer = akt;
752
 
753
	   if ((index = att.index(QString("opaque"))) != -1)
754
	      akt->opaque = (att.value(index).toInt() == 0) ? false : true;
755
 
756
	   if ((index = att.index(QString("noSubsets"))) != -1)
757
	      akt->noSubsets = (att.value(index).toInt() == 0) ? false : true;
758
 
759
	   if ((index = att.index(QString("queryable"))) != -1)
760
	      akt->queryable = (att.value(index).toInt() == 0) ? false : true;
761
 
762
	   if ((index = att.index(QString("cascaded"))) != -1)
763
	      akt->cascaded = (att.value(index).toInt() == 0) ? false : true;
764
	}
765
	else if (qName.toLower() == QString("srs"))
766
	   attribute = at_SRS;
767
	else if (qName.toLower() == QString("crs"))
768
	   attribute = at_CRS;
769
	else if (qName.toLower() == QString("abstract"))
770
	   attribute = at_Abstract;
771
	else if (qName.toLower() == QString("latlonboundingbox"))
772
	{
773
	   if (lcount < 1)
774
	   {
775
	      KMessageBox::error(this, i18n("Error at line %1: Element LatLonBoundingBox was found outside of a Layer element!").arg(line));
776
	      return false;
777
	   }
778
 
779
	   if (!Layer || !firstLayer)
780
	   {
781
	      KMessageBox::error(this, i18n("Fatal error at line %1: Layer was not initialized!").arg(line));
782
	      return false;
783
	   }
784
 
785
	   if ((index = att.index(QString("maxx"))) != -1)
786
	      Layer->LatLon.maxx = att.value(index).toDouble();
787
 
788
	   if ((index = att.index(QString("maxy"))) != -1)
789
	      Layer->LatLon.maxy = att.value(index).toDouble();
790
 
791
	   if ((index = att.index(QString("minx"))) != -1)
792
	      Layer->LatLon.minx = att.value(index).toDouble();
793
 
794
	   if ((index = att.index(QString("miny"))) != -1)
795
	      Layer->LatLon.miny = att.value(index).toDouble();
796
	}
797
	else if (qName.toLower() == QString("boundingbox"))
798
	{
799
	   if (lcount < 1)
800
	   {
801
	      KMessageBox::error(this, i18n("Error at line %1: Element BoundingBox was found outside of a Layer element!").arg(line));
802
	      return false;
803
	   }
804
 
805
	   if (!Layer || !firstLayer)
806
	   {
807
	      KMessageBox::error(this, i18n("Fatal error at line %1: Layer was not initialized!").arg(line));
808
	      return false;
809
	   }
810
 
811
	   if ((index = att.index(QString("maxx"))) != -1)
812
	      Layer->Bounding.maxx = att.value(index).toDouble();
813
 
814
	   if ((index = att.index(QString("maxy"))) != -1)
815
	      Layer->Bounding.maxy = att.value(index).toDouble();
816
 
817
	   if ((index = att.index(QString("minx"))) != -1)
818
	      Layer->Bounding.minx = att.value(index).toDouble();
819
 
820
	   if ((index = att.index(QString("miny"))) != -1)
821
	      Layer->Bounding.miny = att.value(index).toDouble();
822
 
823
	   if ((index = att.index(QString("srs"))) != -1)
824
	      Layer->Bounding.SRS = att.value(index);
825
	}
826
	else if (qName.toLower() == QString("metadataurl"))
827
	   element = el_MetadataURL;
828
	else if (qName.toLower() == QString("legendurl"))
829
	   element = el_LegendURL;
830
	else if (qName.toLower() == QString("style"))
831
	{
832
	   element = el_Style;
833
 
834
	   if (!Layer || !firstLayer)
835
	   {
836
	      KMessageBox::error(this, i18n("Fatal error at line %1: Layer was not initialized!").arg(line));
837
	      return false;
838
	   }
839
 
840
	   if (!Layer->style)
841
	      Layer->style = allocateStyle();
842
	   else
843
	   {
844
	   STYLE *akt = findLastStyle(Layer);
845
 
846
	      akt->next = allocateStyle();
847
	   }
848
	}
849
	else if (qName.toLower() == QString("scalehint"))
850
	{
851
	   if (lcount < 1)
852
	   {
853
	      KMessageBox::error(this, i18n("Error at line %1: Element ScaleHint was found outside of a Layer element!").arg(line));
854
	      return false;
855
	   }
856
 
857
	   if (!Layer || !firstLayer)
858
	   {
859
	      KMessageBox::error(this, i18n("Fatal error at line %1: Layer was not initialized!").arg(line));
860
	      return false;
861
	   }
862
 
863
	   if ((index = att.index(QString("min"))) != -1)
864
	      Layer->scaleMin = att.value(index).toDouble();
865
 
866
	   if ((index = att.index(QString("max"))) != -1)
867
	      Layer->scaleMax = att.value(index).toDouble();
868
	}
869
 
870
	return true;
871
}
872
 
873
bool wmsSelectorWidget::endElement( const QString&, const QString&, const QString& qName)
874
{
875
	attribute = at_none;
876
 
877
	if (qName.toLower() == QString("wmt_ms_Capabilities"))
878
	   element = el_none;
879
	else if (qName.toLower() == QString("service"))
880
	   element = el_WMT_MS_Capabilities;
881
	else if (qName.toLower() == QString("keywordlist"))
882
	   element = el_Service;
883
	else if (qName.toLower() == QString("contactinformation"))
884
	   element = el_Service;
885
	else if (qName.toLower() == QString("contactpersonprimary"))
886
	   element = el_ContactInformation;
887
	else if (qName.toLower() == QString("contactaddress"))
888
	   element = el_ContactInformation;
889
	else if (qName.toLower() == QString("capability"))
890
	   element = el_WMT_MS_Capabilities;
891
	else if (qName.toLower() == QString("request"))
892
	   element = el_Capability;
893
	else if (qName.toLower() == QString("getcapabilities"))
894
	   element = el_Request;
895
	else if (qName.toLower() == QString("dcptype"))
896
	   el2 = el_none;
897
	else if (qName.toLower() == QString("http"))
898
	   el2 = el_DCPType;
899
	else if (qName.toLower() == QString("get"))
900
	   el2 = el_HTTP;
901
	else if (qName.toLower() == QString("post"))
902
	   el2 = el_HTTP;
903
	else if (qName.toLower() == QString("getmap"))
904
	   element = el_Request;
905
	else if (qName.toLower() == QString("getfeatureinfo"))
906
	   element = el_Request;
907
	else if (qName.toLower() == QString("exception"))
908
	   element = el_Capability;
909
	else if (qName.toLower() == QString("layer"))
910
	{
911
	   lcount--;
912
 
913
	   if (lcount == 0)
914
	      element = el_Capability;
915
	   else
916
	      element = el_Layer;
917
	}
918
	else if (qName.toLower() == QString("metadataurl"))
919
	   element = el_Layer;
920
	else if (qName.toLower() == QString("legendurl"))
921
	   element = el_Layer;
922
	else if (qName.toLower() == QString("style"))
923
	   element = el_Layer;
924
 
925
	return true;
926
}
927
 
928
bool wmsSelectorWidget::characters (const QString& chraw)
929
{
930
	if (chraw.at(0) == QChar('\n'))
931
	{
932
	   line++;
933
	   return true;
934
	}
935
 
936
	QString ch = chraw.trimmed();
937
 
938
	if (ch.length() == 0)
939
	   return true;
940
 
941
	if (element == el_Service || element == el_ContactInformation ||
942
	    element == el_ContactPersonPrimary || element == el_ContactAddress)
943
	{
944
	   if (attribute == at_Name)
945
	      Service.Name = ch;
946
	   else if (attribute == at_Title)
947
	      Service.Title = ch;
948
	   else if (attribute == at_Abstract)
949
	      Service.Abstract = ch;
950
	   else if (attribute == at_ContactPerson)
951
	      Service.ContactPerson = ch;
952
	   else if (attribute == at_ContactOrganisation)
953
	      Service.ContactOrganisation = ch;
954
	   else if (attribute == at_ContactElectronicMailAddress)
955
	      Service.ContactElectronicMailAddress = ch;
956
	   else if (attribute == at_ContactPosition)
957
	      Service.ContactPosition = ch;
958
	   else if (attribute == at_AddressType)
959
	      Service.AddressType = ch;
960
	   else if (attribute == at_Address)
961
	      Service.Address = ch;
962
	   else if (attribute == at_City)
963
	      Service.City = ch;
964
	   else if (attribute == at_StateOrProvince)
965
	      Service.StateOrProvince = ch;
966
	   else if (attribute == at_PostCode)
967
	      Service.PostCode = ch;
968
	   else if (attribute == at_Country)
969
	      Service.Country = ch;
970
	   else if (attribute == at_ContactVoiceTelephone)
971
	      Service.ContactVoiceTelephone = ch;
972
	   else if (attribute == at_ContactFacsimileTelephone)
973
	      Service.ContactFacsimileTelephone = ch;
974
	   else if (attribute == at_Fees)
975
	      Service.Fees = ch;
976
	   else if (attribute == at_AccessConstraints)
977
	      Service.AccessConstraints = ch;
978
	}
979
	else if (element == el_GetCapabilities)
980
	{
981
	   if (attribute == at_Format)
982
	      Request.GetCapabilities.Format << ch;
983
	}
984
	else if (element == el_GetMap)
985
	{
986
	   if (attribute == at_Format)
987
	      Request.GetMap.Format << ch;
988
	}
989
	else if (element == el_GetFeatureInfo)
990
	{
991
	   if (attribute == at_Format)
992
	      Request.GetFeatureInfo.Format << ch;
993
	}
994
	else if (element == el_Exception)
995
	{
996
	   if (attribute == at_Format)
997
	      Exception << ch;
998
	}
999
	else if (element == el_Layer)
1000
	{
1001
	   if (attribute == at_Name)
1002
	      Layer->Name = ch;
1003
	   else if (attribute == at_Title)
1004
	      Layer->Title = ch;
1005
	   else if (attribute == at_Abstract)
1006
	      Layer->Abstract = ch;
1007
	   else if (attribute == at_SRS)
1008
	      Layer->SRS << ch;
1009
	   else if (attribute == at_CRS)
1010
	      Layer->CRS << ch;
1011
	}
1012
	else if (element == el_Style)
1013
	{
1014
	STYLE *akt = findLastStyle(Layer);
1015
 
1016
	   if (!Layer->style)
1017
	   {
1018
	      Layer->style = allocateStyle();
1019
	      akt = Layer->style;
1020
	   }
1021
 
1022
	   if (!akt)
1023
	   {
1024
	      KMessageBox::error(this, i18n("Fatal error at line %1: No, or unable to allocate memory for a style!").arg(line));
1025
	      return false;
1026
	   }
1027
 
1028
	   if (attribute == at_Name)
1029
	      akt->Name = ch;
1030
	   else if (attribute == at_Title)
1031
	      akt->Title = ch;
1032
	}
1033
 
1034
	return true;
1035
}
1036
 
1037
void wmsSelectorWidget::getCapabilities()
1038
{
1039
	if (file.exists())
1040
	   file.remove();
1041
 
1042
	if (!file.open(QIODevice::WriteOnly))
1043
	{
1044
	   KMessageBox::information(this,
1045
		i18n("Unable to save the file %1: %2.").arg(file.fileName()).arg(file.errorString()),
1046
		i18n("WMS capabilities"));
1047
 
1048
	   return;
1049
	}
1050
 
1051
#ifndef QT_NO_OPENSSL
1052
	QHttp::ConnectionMode mode = (urlWMS->url().protocol().toLower() == "https") ? QHttp::ConnectionModeHttps : QHttp::ConnectionModeHttp;
1053
#else
1054
	QHttp::ConnectionMode mode = QHttp::ConnectionModeHttp;
1055
#endif
1056
	http->setHost(urlWMS->url().host(), mode, (urlWMS->url().port() == -1) ? 0 : urlWMS->url().port());
1057
 
1058
	if (!urlWMS->url().user().isEmpty())
1059
	   http->setUser(urlWMS->url().user(), urlWMS->url().pass());
1060
 
1061
	httpRequestAborted = false;
1062
	id = http->get(urlWMS->url().url() + "?VERSION=1.1.1&REQUEST=GetCapabilities&SERVICE=WMS", &file);
1063
	// Show a progress dialog
1064
	progressDialog->setVisible(true);
1065
	progressDialog->setWindowTitle(i18n("Getting capabilities"));
1066
	progressDialog->setLabelText(i18n("Downloading capabilities."));
1067
}
1068
 
1069
bool wmsSelectorWidget::parseWMS()
1070
{
1071
QXmlSimpleReader reader;
1072
LAYER *aktLayer;
1073
 
1074
	QXmlInputSource source (&file);
1075
	reader.setContentHandler (this);
1076
	reader.parse (source);
1077
 
1078
	// Put the result into the elements of the mask
1079
	aktLayer = firstLayer;
1080
 
1081
	while (aktLayer)
1082
	{
1083
	   if (!aktLayer->Name.isEmpty())	// Do we have a valid layer?
1084
	      actionLayer->availableListWidget()->addItem(aktLayer->Name);
1085
 
1086
	   aktLayer = aktLayer->next;
1087
	}
1088
 
1089
	return true;
1090
}
1091
 
284 andreas 1092
QString wmsSelectorWidget::getLayers()
1093
{
1094
QString la;
1095
QListWidget *lw = actionLayer->selectedListWidget();
1096
 
1097
	for (int i = 0; i < lw->count(); i++)
1098
	{
1099
	   QListWidgetItem *item = lw->item(i);
1100
	   la += item->text();
1101
 
1102
	   if (i < lw->count())
1103
	      la += ",";
1104
	}
1105
 
1106
	return la;
1107
}
1108
 
1109
QString wmsSelectorWidget::getStyles()
1110
{
1111
QString la;
1112
QListWidget *lw = actionLayer->selectedListWidget();
1113
int anz = lw->count();
1114
 
1115
	if (cbStyles->isChecked() && anz > 1)
1116
	   la.fill(',', anz - 1);
1117
 
1118
	return la;
1119
}
1120
 
283 andreas 1121
void wmsSelectorWidget::cancelDownload()
1122
{
1123
	httpRequestAborted = true;
1124
	http->abort();
1125
}
1126
 
1127
void wmsSelectorWidget::WMSrequestFinished(int requestId, bool error)
1128
{
1129
	if (requestId != id)
1130
           return;
1131
 
1132
	if (httpRequestAborted)
1133
	{
1134
	   file.close();
1135
	   file.remove();
1136
	   progressDialog->hide();
1137
	   return;
1138
	}
1139
 
1140
	progressDialog->hide();
1141
	file.close();
1142
 
1143
	if (error)
1144
	{
1145
	   file.remove();
1146
	   KMessageBox::information(this, i18n("Download failed: %1.").arg(http->errorString()), i18n("WMS capabilities"));
1147
	}
1148
	else
1149
	{
1150
	   parseWMS();
284 andreas 1151
 
1152
	   if (Init)
1153
	   {
1154
	      // Set the already selected layers to the selected box
1155
	      // This will be done only, if the box with the available layers
1156
	      // is not empty. Otherwise we've to assume, that there was an
1157
	      // error getting the capabilities of the server.
1158
	      if (actionLayer->availableListWidget()->count() > 0)
1159
	      {
1160
	      QListWidget *item = actionLayer->selectedListWidget();
1161
	      QListWidget *av = actionLayer->availableListWidget();
1162
	      QStringList laList = Layers.split(',');
1163
 
1164
		 for (int i = 0; i < laList.size(); i++)
1165
		 {
1166
		    QList<QListWidgetItem*> qlwi = av->findItems(laList.at(i), Qt::MatchExactly);
1167
 
1168
		    if (qlwi.size() == 1)
1169
		    {
1170
		       item->addItem(laList.at(i));
1171
		       av->takeItem(av->row(qlwi.at(0)));
1172
		    }
1173
		 }
1174
	      }
1175
 
1176
	      Init = false;
1177
	   }
283 andreas 1178
	}
1179
}
1180
 
1181
void wmsSelectorWidget::readResponseHeader(const QHttpResponseHeader &responseHeader)
1182
{
1183
	switch (responseHeader.statusCode())
1184
	{
1185
	   case 200:                   // Ok
1186
	   case 301:                   // Moved Permanently
1187
	   case 302:                   // Found
1188
	   case 303:                   // See Other
1189
	   case 307:                   // Temporary Redirect
1190
		// these are not error conditions
1191
	   break;
1192
 
1193
	   default:
1194
	      KMessageBox::information(this,
1195
	        i18n("Download failed: %1.").arg(responseHeader.reasonPhrase()),
1196
		i18n("WMS capabilities"));
1197
		httpRequestAborted = true;
1198
		progressDialog->hide();
1199
		http->abort();
1200
	}
1201
}
1202
 
1203
void wmsSelectorWidget::updateDataReadProgress(int bytesRead, int totalBytes)
1204
 {
1205
	if (httpRequestAborted)
1206
	   return;
1207
 
1208
	progressDialog->progressBar()->setMaximum(totalBytes);
1209
	progressDialog->progressBar()->setValue(bytesRead);
1210
 }
1211
 
1212
void wmsSelectorWidget::slotAuthenticationRequired(const QString &hostName, quint16, QAuthenticator *authenticator)
1213
{
1214
QDialog dlg;
1215
Ui::Dialog ui;
1216
 
1217
	ui.setupUi(&dlg);
1218
	dlg.adjustSize();
1219
	ui.siteDescription->setText(i18n("%1 at %2").arg(authenticator->realm()).arg(hostName));
1220
 
1221
	if (dlg.exec() == QDialog::Accepted)
1222
	{
1223
	   authenticator->setUser(ui.userEdit->text());
1224
	   authenticator->setPassword(ui.passwordEdit->text());
1225
	}
1226
}
1227
 
1228
#ifndef QT_NO_OPENSSL
1229
void wmsSelectorWidget::sslErrors(const QList<QSslError> &errors)
1230
{
1231
QString errorString;
1232
 
1233
	foreach (const QSslError &error, errors)
1234
	{
1235
	   if (!errorString.isEmpty())
1236
	      errorString += ", ";
1237
 
1238
	   errorString += error.errorString();
1239
	}
1240
 
1241
	if (KMessageBox::warningContinueCancel(this,
1242
		i18n("One or more SSL errors has occurred: %1").arg(errorString),
1243
		i18n("WMS capabilities")) == KMessageBox::Continue)
1244
	{
1245
           http->ignoreSslErrors();
1246
	}
1247
}
1248
#endif
1249
 
1250
void wmsSelectorWidget::initializeParser()
1251
{
1252
LAYER *aktLayer;
1253
 
1254
	Service.Name.clear();
1255
	Service.Title.clear();
1256
	Service.Abstract.clear();
1257
	Service.OnlineResource.clear();
1258
	Service.Type.clear();
1259
	Service.ContactPerson.clear();
1260
	Service.ContactOrganisation.clear();
1261
	Service.ContactPosition.clear();
1262
	Service.AddressType.clear();
1263
	Service.Address.clear();
1264
	Service.City.clear();
1265
	Service.StateOrProvince.clear();
1266
	Service.PostCode.clear();
1267
	Service.Country.clear();
1268
	Service.ContactVoiceTelephone.clear();
1269
	Service.ContactFacsimileTelephone.clear();
1270
	Service.ContactElectronicMailAddress.clear();
1271
	Service.Fees.clear();
1272
	Service.AccessConstraints.clear();
1273
 
1274
	Request.GetCapabilities.Format.clear();
1275
	Request.GetCapabilities.Get.clear();
1276
	Request.GetCapabilities.Post.clear();
1277
 
1278
	Request.GetMap.Format.clear();
1279
	Request.GetMap.Get.clear();
1280
	Request.GetMap.Post.clear();
1281
 
1282
	Request.GetFeatureInfo.Format.clear();
1283
	Request.GetFeatureInfo.Get.clear();
1284
	Request.GetFeatureInfo.Post.clear();
1285
 
1286
	if (Layer)
1287
	{
1288
	LAYER *nl;
1289
 
1290
	   aktLayer = Layer;
1291
 
1292
	   while (aktLayer)
1293
	   {
1294
	      aktLayer->opaque = false;
1295
	      aktLayer->noSubsets = false;
1296
	      aktLayer->queryable = false;
1297
	      aktLayer->cascaded = false;
1298
	      aktLayer->Name.clear();
1299
	      aktLayer->Title.clear();
1300
	      aktLayer->Abstract.clear();
1301
	      aktLayer->SRS.clear();
1302
	      aktLayer->LatLon.maxx = 0.0;
1303
	      aktLayer->LatLon.maxy = 0.0;
1304
	      aktLayer->LatLon.minx = 0.0;
1305
	      aktLayer->LatLon.miny = 0.0;
1306
	      aktLayer->Bounding.maxx = 0.0;
1307
	      aktLayer->Bounding.maxy = 0.0;
1308
	      aktLayer->Bounding.minx = 0.0;
1309
	      aktLayer->Bounding.miny = 0.0;
1310
	      aktLayer->scaleMin = 0.0;
1311
	      aktLayer->scaleMax = 0.0;
1312
 
1313
	      if (aktLayer->style)
1314
	      {
1315
	      STYLE *ns, *akt;
1316
 
1317
		 akt = aktLayer->style;
1318
 
1319
		 while (akt)
1320
		 {
1321
		    akt->Name.clear();
1322
		    akt->Title.clear();
1323
		    akt->Format.clear();
1324
		    akt->LegendURL.clear();
1325
		    ns = akt->next;
1326
		    delete akt;
1327
		    akt = ns;
1328
		 }
1329
 
1330
		 aktLayer->style = 0;
1331
	      }
1332
 
1333
	      nl = aktLayer->next;
1334
	      delete aktLayer;
1335
	      aktLayer = nl;
1336
	   }
1337
 
1338
	   Layer = 0;
1339
	   firstLayer = 0;
1340
	}
1341
}
1342
 
1343
LAYER *wmsSelectorWidget::allocateLayer()
1344
{
1345
LAYER *aktLayer, *last;
1346
 
1347
	if (Layer)
1348
	{
1349
	   last = Layer;
1350
 
1351
	   while (last)
1352
	   {
1353
	      if (!last->next)
1354
		 break;
1355
 
1356
	      last = last->next;
1357
	   }
1358
 
1359
	   aktLayer = new LAYER;
1360
	   aktLayer->next = 0;
1361
	   last->next = aktLayer;
1362
	   Layer = aktLayer;
1363
	}
1364
	else
1365
	{
1366
	   aktLayer = new LAYER;
1367
	   aktLayer->next = 0;
1368
	   Layer = last = firstLayer = aktLayer;
1369
	}
1370
 
1371
	aktLayer->opaque = false;
1372
	aktLayer->noSubsets = false;
1373
	aktLayer->queryable = false;
1374
	aktLayer->cascaded = false;
1375
	aktLayer->Name.clear();
1376
	aktLayer->Title.clear();
1377
	aktLayer->Abstract.clear();
1378
	aktLayer->SRS.clear();
1379
	aktLayer->LatLon.maxx = 0.0;
1380
	aktLayer->LatLon.maxy = 0.0;
1381
	aktLayer->LatLon.minx = 0.0;
1382
	aktLayer->LatLon.miny = 0.0;
1383
	aktLayer->LatLon.SRS.clear();
1384
	aktLayer->Bounding.maxx = 0.0;
1385
	aktLayer->Bounding.maxy = 0.0;
1386
	aktLayer->Bounding.minx = 0.0;
1387
	aktLayer->Bounding.miny = 0.0;
1388
	aktLayer->Bounding.SRS.clear();
1389
	aktLayer->scaleMin = 0.0;
1390
	aktLayer->scaleMax = 0.0;
1391
	aktLayer->style = 0;
1392
	return aktLayer;
1393
}
1394
 
1395
STYLE *wmsSelectorWidget::allocateStyle()
1396
{
1397
STYLE *akt;
1398
 
1399
	akt = new STYLE;
1400
	akt->Name.clear();
1401
	akt->Title.clear();
1402
	akt->Format.clear();
1403
	akt->LegendURL.clear();
1404
	akt->next = 0;
1405
	return akt;
1406
}
1407
 
1408
STYLE *wmsSelectorWidget::findLastStyle(LAYER *l)
1409
{
1410
STYLE *akt;
1411
 
1412
	if (!l)
1413
	   return 0;
1414
 
1415
	akt = l->style;
1416
 
1417
	while (akt)
1418
	{
1419
	   if (!akt->next)
1420
	      return akt;
1421
 
1422
	   akt = akt->next;
1423
	}
1424
 
1425
	return 0;
1426
}
1427
 
1428
bool wmsSelectorWidget::writeWMSTag(double llat, double llon, double rlat, double rlon, int width, int height)
1429
{
1430
#if defined HAVE_GDAL
1431
QFile fl(MAP);
1432
QString xml, s, srs, crs, styles, bSize, ext;
1433
QDir dir = QDir::home();
1434
QString path = dir.absolutePath();
284 andreas 1435
//int item;
283 andreas 1436
double _llat, _llon, _rlat, _rlon;
1437
 
1438
	if (!fl.open(QIODevice::ReadWrite | QIODevice::Truncate))
1439
	{
1440
	   KMessageBox::error (this, i18n("Error opening or creating the WMS tag file!\nPlease check file name and/or permissions."));
1441
	   return false;
1442
	}
1443
 
1444
	KConfig cfg (QString("sportwatcher.rc"), KConfig::SimpleConfig);
1445
	KConfigGroup wms (&cfg, "WMS");
1446
 
1447
	xml = "<GDAL_WMS>\n";
1448
	xml += "   <Service name=\"WMS\">\n";
284 andreas 1449
 
1450
	if (!WMSVersion.isEmpty())
1451
	   xml += "      <Version>" + WMSVersion + "</Version>\n";
1452
	else
1453
	   xml += "      <Version>1.1.1</Version>\n";
1454
 
283 andreas 1455
	xml += "      <ServerURL>" + urlWMS->url().url() + "?</serverURL>\n";
1456
	_llon = llon;
1457
	_llat = llat;
1458
	_rlon = rlon;
1459
	_rlat = rlat;
1460
 
1461
	if (firstLayer && firstLayer->SRS.size())
1462
	   srs = firstLayer->SRS.at(0);
1463
	else
1464
	   srs = QString("EPSG:4326");
1465
 
1466
	xml += "      <SRS>" + srs + "</SRS>\n";
1467
 
1468
	if (firstLayer && firstLayer->CRS.size())
1469
	   crs = firstLayer->CRS.at(0);
1470
	else
1471
	   crs = QString("EPSG:4326");
1472
 
1473
	xml += "      <CRS>" + crs + "</CRS>\n";
1474
	xml += "      <ImageFormat>image/";
1475
	xml += "png";
1476
	ext = QString(".png");
1477
	xml += "</ImageFormat>\n";
1478
 
1479
	xml += "      <Layers>";
1480
	QListWidget *lw = actionLayer->selectedListWidget();
1481
	s.clear();
1482
 
1483
	for (int i = 0; i < lw->count(); i++)
1484
	{
1485
	QListWidgetItem *lwi;
1486
 
1487
	   if (i > 0 && i < lw->count())
1488
	   {
1489
	      xml += ",";
1490
	      s += ",";
1491
	   }
1492
 
1493
	   lwi = lw->item(i);
1494
	   xml += lwi->text();
1495
	}
1496
 
1497
	xml += "</Layers>\n";
1498
 
1499
	if (StyleCommas)
1500
	   xml += "      <Styles>" + s + "</Styles>\n";
1501
	else
1502
	   xml += "      <Styles></Styles>\n";
1503
 
1504
	xml += "      <BBoxOrder>xyXY</BBoxOrder>\n";
1505
	xml += "   </Service>\n";
1506
	xml += "   <DataWindow>\n";
1507
	s.sprintf ("%f", _llat);
1508
	xml += "      <UpperLeftX>" + s + "</UpperLeftX>\n";
1509
	s.sprintf ("%f", _llon);
1510
	xml += "      <UpperLeftY>" + s + "</UpperLeftY>\n";
1511
	s.sprintf ("%f", _rlat);
1512
	xml += "      <LowerRightX>" + s + "</LowerRightX>\n";
1513
	s.sprintf ("%f", _rlon);
1514
	xml += "      <LowerRightY>" + s + "</LowerRightY>\n";
1515
	s.sprintf ("%d", width);
1516
	xml += "      <SizeX>" + s + "</SizeX>\n";
1517
	s.sprintf ("%d", height);
1518
	xml += "      <SizeY>" + s + "</SizeY>\n";
1519
	xml += "   </DataWindow>\n";
1520
 
1521
	xml += "   <Projection>" + srs + "</Projection>\n";
1522
	xml += "   <BandsCount>" + QString("%1").arg(edBands->value()) + "</BandsCount>\n";
284 andreas 1523
/*	item = wms.readEntry("Tile", 2);
283 andreas 1524
 
1525
	switch (item)
1526
	{
1527
	   case 0: bSize = QString("64"); break;
1528
	   case 1: bSize = QString("128"); break;
1529
	   case 2: bSize = QString("256"); break;
1530
	   case 3: bSize = QString("512"); break;
1531
	   case 4: bSize = QString("1024"); break;
1532
	   default: bSize = QString("256");
1533
	}
284 andreas 1534
*/
1535
	bSize = QString("256");
283 andreas 1536
	xml += "   <BlockSizeX>" + bSize + "</BlockSizeX>\n";
1537
	xml += "   <BlockSizeY>" + bSize + "</BlockSizeY>\n";
1538
	xml += "   <OverviewCount>" + wms.readEntry("Overview", QString("10")) + "</OverviewCount>\n";
1539
	xml += "   <Cache>\n";
1540
	xml += "      <Path>" + path + "/.gdalwmscache" + "</Path>\n";
1541
	xml += "      <Depth>" + wms.readEntry("Depth", QString("2")) + "</Depth>\n";
1542
	xml += "      <Extension>" + ext + "</Extension>\n";
1543
	xml += "   </Cache>\n";
1544
	QString adv((wms.readEntry("Advice", true)) ? "true" : "false");
1545
	QString ver((wms.readEntry("Verify", false)) ? "true" : "false");
1546
 
1547
	xml += "   <OfflineMode>false</OfflineMode>\n";
1548
	xml += "   <AdviseRead>" + adv + "</AdviseRead>\n";
1549
	xml += "   <VerifyAdviseRead>" + ver + "</VerifyAdviseRead>\n";
1550
	xml += "</GDAL_WMS>\n";
1551
 
1552
	write (fl.handle(), xml.toAscii().data(), strlen (xml.toAscii().data()));
1553
	fl.close();
1554
#endif
1555
	return true;
1556
}
1557
 
1558
void wmsSelectorWidget::setDimension(double &minx, double &miny, double &maxx, double &maxy, QListWidgetItem *item = 0)
1559
{
284 andreas 1560
double cx, cy, levX, levY;
1561
LAYER *akt;
283 andreas 1562
 
1563
	cx = cy = 0.0;
1564
	akt = firstLayer;
1565
 
284 andreas 1566
	if (zLevel >= 0 && zLevel < 10)
283 andreas 1567
	{
284 andreas 1568
	   levX = zoom[zLevel].x;
1569
	   levY = zoom[zLevel].y;
1570
	}
1571
	else
1572
	{
1573
	   levX = zoom[0].x;
1574
	   levY = zoom[0].y;
1575
	}
283 andreas 1576
 
284 andreas 1577
	cx = edLat->value();
1578
	cy = edLon->value();
283 andreas 1579
 
284 andreas 1580
	if (cx == 0.0 && cy == 0.0)
1581
	{
1582
	   while (akt)
1583
	   {
1584
	      cx = akt->LatLon.minx + (akt->LatLon.maxx - akt->LatLon.minx) / 2.0;
1585
	      cy = akt->LatLon.miny + (akt->LatLon.maxy - akt->LatLon.miny) / 2.0;
283 andreas 1586
 
284 andreas 1587
	      if ((cx != 0.0 || cy != 0.0) && !item)
1588
	         break;
283 andreas 1589
 
284 andreas 1590
	      if (item && item->text() == akt->Name && cx != 0.0 && cy != 0.0)
1591
	         break;
1592
 
1593
	      akt = akt->next;
1594
	   }
1595
 
1596
	   if (cx == 0.0 && cy == 0.0)
1597
	   {	// Schloss Schoenbrunn in Vienna
1598
	      cx = 16.298223 + (0.014108 / 2.0);
1599
	      cy = 48.177103 + (0.003554 / 2.0);
1600
	   }
1601
 
1602
	   edLat->setValue(cx);
1603
	   edLon->setValue(cy);
283 andreas 1604
	}
1605
 
284 andreas 1606
	minx = cx - (levX / 2.0);
1607
	maxx = cx + (levX / 2.0);
1608
	miny = cy + (levY / 2.0);
1609
	maxy = cy - (levY / 2.0);
283 andreas 1610
}
1611
 
1612
#include "wmsselectorwidget.moc"
1613