source: subversion/applications/editors/merkaartor/LayerWidget.cpp @ 9541

Last change on this file since 9541 was 9541, checked in by Chris Browet, 12 years ago

ADD : Render to SVG via Osmarender (requires libxml2/libxslt; inkscape required to generate bitmap). Closes #694
ADD : Render to bitmap via native engine.
ADD : layer menu in menubar
ADD : display viewport coordinates in status bar
ADD : (almost) generalize progress dialogs
ADD : support (initial) of GPS devices / NMEA log replay
ADD : option to show/hide track segments
ADD : option to show/hide non-stylized trackpoints
ADD : option to show/hide street names on the map

File size: 14.4 KB
Line 
1#include "LayerWidget.h"
2#include "LayerDock.h"
3
4#include "MainWindow.h"
5#include "Map/MapDocument.h"
6#include "Map/MapLayer.h"
7#include "MapView.h"
8#include "Preferences/MerkaartorPreferences.h"
9
10#include <QtGui/QMouseEvent>
11#include <QtGui/QPainter>
12
13#define SAFE_DELETE(x) {delete (x); x = NULL;}
14
15#define LINEHEIGHT 25
16
17LayerWidget::LayerWidget(MapLayer* aLayer, QWidget* aParent)
18: QAbstractButton(aParent), theLayer(aLayer), ctxMenu(0), closeAction(0), actZoom(0)
19{
20        setCheckable(true);
21        setAutoExclusive(true) ;
22        setFocusPolicy(Qt::NoFocus);
23        visibleIcon = QPixmap(":Icons/eye.xpm");
24        hiddenIcon = QPixmap(":Icons/empty.xpm");
25
26        associatedMenu = new QMenu(aLayer->name());
27//      initActions();
28}
29
30LayerWidget::~LayerWidget()
31{
32        delete associatedMenu;
33}
34
35QSize LayerWidget::minimumSizeHint () const
36{
37        return QSize(100, LINEHEIGHT);
38}
39
40QSize LayerWidget::sizeHint () const
41{
42        return QSize(100, LINEHEIGHT);
43}
44
45void LayerWidget::paintEvent(QPaintEvent*)
46{
47        if (!theLayer->isEnabled())
48                return;
49
50        QPainter P(this);
51
52        P.drawLine(rect().bottomLeft(), rect().bottomRight());
53        if (isChecked()) {
54                P.fillRect(rect().adjusted(20,0,0,-1),QBrush(palette().highlight()));
55//              P.fillRect(20, 1, width()-19, rect().height()-1, QBrush(palette().highlight()));
56                P.setPen(palette().highlightedText().color());
57                P.drawText(rect().adjusted(23,0,0,-1), Qt::AlignLeft | Qt::AlignVCenter , theLayer->name());
58        } else {
59                P.fillRect(rect().adjusted(0,0,0,-1),backColor);
60                P.setPen(QColor(0,0,0));
61                P.drawText(rect().adjusted(23,0,0,-1), Qt::AlignLeft | Qt::AlignVCenter , theLayer->name());
62        }
63
64        if (theLayer->isVisible())
65                P.drawPixmap(QPoint(2, rect().center().y()-visibleIcon.height()/2), visibleIcon);
66        else
67                P.drawPixmap(QPoint(2, rect().center().y()-hiddenIcon.height()/2), hiddenIcon);
68}
69
70void LayerWidget::mouseReleaseEvent(QMouseEvent* anEvent)
71{
72        if (anEvent->pos().x()<20)
73        {
74                theLayer->setVisible(!theLayer->isVisible());
75                anEvent->ignore();
76                update();
77                emit(layerChanged(this, false));
78        }
79        else
80        {
81                if (!(dynamic_cast<ImageLayerWidget *>(this)))
82                        toggle();
83        }
84}
85
86void LayerWidget::checkStateSet()
87{
88        theLayer->setSelected(isChecked());
89        //emit (layerChanged(this));
90}
91
92void LayerWidget::nextCheckStateSet()
93{
94        setChecked(!isChecked());
95        theLayer->setSelected(isChecked());
96        //emit (layerChanged(this));
97}
98
99MapLayer* LayerWidget::getMapLayer()
100{
101        return theLayer;
102}
103
104void LayerWidget::contextMenuEvent(QContextMenuEvent* anEvent)
105{
106        if (ctxMenu) {
107                if (actZoom)
108                        actZoom->setEnabled(theLayer->size());
109                if (closeAction)
110                        closeAction->setEnabled(theLayer->canDelete());
111                ctxMenu->exec(anEvent->globalPos());
112        }
113}
114
115#define NUMOP 3
116void LayerWidget::initActions()
117{
118        SAFE_DELETE(ctxMenu);
119        ctxMenu = new QMenu(this);
120    static const char *opStr[NUMOP] = {
121        QT_TR_NOOP("Low"), QT_TR_NOOP("High"), QT_TR_NOOP("Opaque")};
122
123        QActionGroup* actgrp = new QActionGroup(this);
124        QMenu* alphaMenu = new QMenu(tr("Opacity"), this);
125        for (int i=0; i<NUMOP; i++) {
126                QAction* act = new QAction(tr(opStr[i]), alphaMenu);
127                actgrp->addAction(act);
128                qreal a = MerkaartorPreferences::instance()->getAlpha(opStr[i]);
129                act->setData(a);
130                act->setCheckable(true);
131                if (theLayer->getAlpha() == a)
132                        act->setChecked(true);
133                alphaMenu->addAction(act);
134        }
135        ctxMenu->addMenu(alphaMenu);
136        connect(alphaMenu, SIGNAL(triggered(QAction*)), this, SLOT(setOpacity(QAction*)));
137
138        associatedMenu->addMenu(alphaMenu);
139}
140
141void LayerWidget::setOpacity(QAction *act)
142{
143        theLayer->setAlpha(act->data().toDouble());
144        emit (layerChanged(this, false));
145}
146
147void LayerWidget::close()
148{
149        emit(layerClosed(theLayer));
150}
151
152void LayerWidget::clear()
153{
154        emit(layerCleared(theLayer));
155}
156
157void LayerWidget::zoomLayer(bool)
158{
159        emit (layerZoom(theLayer));
160}
161
162QMenu* LayerWidget::getAssociatedMenu()
163{
164        return associatedMenu;
165}
166
167
168// DrawingLayerWidget
169
170DrawingLayerWidget::DrawingLayerWidget(DrawingMapLayer* aLayer, QWidget* aParent)
171        : LayerWidget(aLayer, aParent)
172{
173        backColor = QColor(255,255,255);
174        initActions();
175}
176
177void DrawingLayerWidget::initActions()
178{
179        LayerWidget::initActions();
180
181        actZoom = new QAction(tr("Zoom"), ctxMenu);
182        ctxMenu->addAction(actZoom);
183        associatedMenu->addAction(actZoom);
184        connect(actZoom, SIGNAL(triggered(bool)), this, SLOT(zoomLayer(bool)));
185
186        ctxMenu->addSeparator();
187        associatedMenu->addSeparator();
188
189        closeAction = new QAction(tr("Close"), this);
190        connect(closeAction, SIGNAL(triggered()), this, SLOT(close()));
191        ctxMenu->addAction(closeAction);
192        associatedMenu->addAction(closeAction);
193        closeAction->setEnabled(theLayer->canDelete());
194}
195
196// ImageLayerWidget
197
198ImageLayerWidget::ImageLayerWidget(ImageMapLayer* aLayer, QWidget* aParent)
199: LayerWidget(aLayer, aParent), wmsMenu(0) //, actgrWms(0)
200{
201        backColor = QColor(128,128,128);
202        //actgrAdapter = new QActionGroup(this);
203
204        actNone = new QAction(MerkaartorPreferences::instance()->getBgTypes()[Bg_None], this);
205        //actNone->setCheckable(true);
206        actNone->setChecked((MerkaartorPreferences::instance()->getBgType() == Bg_None));
207        connect(actNone, SIGNAL(triggered(bool)), this, SLOT(setNone(bool)));
208
209//      actOSM = new QAction(MerkaartorPreferences::instance()->getBgTypes()[Bg_OSM], this);
210//      //actNone->setCheckable(true);
211//      actOSM->setChecked((MerkaartorPreferences::instance()->getBgType() == Bg_OSM));
212//      connect(actOSM, SIGNAL(triggered(bool)), this, SLOT(setOSM(bool)));
213
214#ifdef YAHOO
215        actLegalYahoo = new QAction(MerkaartorPreferences::instance()->getBgTypes()[Bg_Yahoo], this);
216        //actYahoo->setCheckable(true);
217        actLegalYahoo->setChecked((MerkaartorPreferences::instance()->getBgType() == Bg_Yahoo));
218        connect(actLegalYahoo, SIGNAL(triggered(bool)), this, SLOT(setLegalYahoo(bool)));
219#endif
220#ifdef YAHOO_ILLEGAL
221        actYahoo = new QAction(MerkaartorPreferences::instance()->getBgTypes()[Bg_Yahoo_illegal], this);
222        //actYahoo->setCheckable(true);
223        actYahoo->setChecked((MerkaartorPreferences::instance()->getBgType() == Bg_Yahoo_illegal));
224        connect(actYahoo, SIGNAL(triggered(bool)), this, SLOT(setYahoo(bool)));
225#endif
226#ifdef GOOGLE_ILLEGAL
227        actGoogle = new QAction(MerkaartorPreferences::instance()->getBgTypes()[Bg_Google_illegal], this);
228        //actGoogle->setCheckable(true);
229        actGoogle->setChecked((MerkaartorPreferences::instance()->getBgType() == Bg_Google_illegal));
230        connect(actGoogle, SIGNAL(triggered(bool)), this, SLOT(setGoogle(bool)));
231#endif
232#ifdef MSLIVEMAP_ILLEGAL
233        actVirtEarth = new QAction(MerkaartorPreferences::instance()->getBgTypes()[Bg_MsVirtualEarth_illegal], this);
234        //actVirtEarth->setCheckable(true);
235        actVirtEarth->setChecked((MerkaartorPreferences::instance()->getBgType() == Bg_MsVirtualEarth_illegal));
236        connect(actVirtEarth, SIGNAL(triggered(bool)), this, SLOT(setMsVirtualEarth(bool)));
237#endif
238        initActions();
239}
240
241ImageLayerWidget::~ImageLayerWidget()
242{
243}
244
245void ImageLayerWidget::setWms(QAction* act)
246{
247        WmsServerList* L = MerkaartorPreferences::instance()->getWmsServers();
248        WmsServer S = L->value(act->data().toString());
249        MerkaartorPreferences::instance()->setSelectedWmsServer(S.WmsName);
250
251        ((ImageMapLayer *)theLayer)->setMapAdapter(Bg_Wms);
252        theLayer->setVisible(true);
253
254        this->update(rect());
255        emit (layerChanged(this, true));
256}
257
258void ImageLayerWidget::setTms(QAction* act)
259{
260        TmsServerList* L = MerkaartorPreferences::instance()->getTmsServers();
261        TmsServer S = L->value(act->data().toString());
262        MerkaartorPreferences::instance()->setSelectedTmsServer(S.TmsName);
263
264        ((ImageMapLayer *)theLayer)->setMapAdapter(Bg_Tms);
265        theLayer->setVisible(true);
266
267        this->update(rect());
268        emit (layerChanged(this, true));
269}
270
271#ifdef YAHOO
272void ImageLayerWidget::setLegalYahoo(bool)
273{
274        ((ImageMapLayer *)theLayer)->setMapAdapter(Bg_Yahoo);
275        theLayer->setVisible(true);
276
277        this->update(rect());
278        emit (layerChanged(this, true));
279}
280#endif
281
282#ifdef YAHOO_ILLEGAL
283void ImageLayerWidget::setYahoo(bool)
284{
285        ((ImageMapLayer *)theLayer)->setMapAdapter(Bg_Yahoo_illegal);
286        theLayer->setVisible(true);
287
288        this->update(rect());
289        emit (layerChanged(this, true));
290}
291#endif
292
293#ifdef GOOGLE_ILLEGAL
294void ImageLayerWidget::setGoogle(bool)
295{
296        ((ImageMapLayer *)theLayer)->setMapAdapter(Bg_Google_illegal);
297        theLayer->setVisible(true);
298
299        this->update(rect());
300        emit (layerChanged(this, true));
301}
302#endif
303
304#ifdef MSLIVEMAP_ILLEGAL
305void ImageLayerWidget::setMsVirtualEarth(bool)
306{
307        ((ImageMapLayer *)theLayer)->setMapAdapter(Bg_MsVirtualEarth_illegal);
308        theLayer->setVisible(true);
309
310        this->update(rect());
311        emit (layerChanged(this, true));
312}
313#endif
314
315void ImageLayerWidget::setNone(bool)
316{
317        ((ImageMapLayer *)theLayer)->setMapAdapter(Bg_None);
318
319        this->update(rect());
320        emit (layerChanged(this, true));
321}
322
323/*void ImageLayerWidget::setOSM(bool)
324{
325        ((ImageMapLayer *)theLayer)->setMapAdapter(Bg_OSM);
326        theLayer->setVisible(true);
327
328        this->update(rect());
329        emit (layerChanged(this, true));
330}
331*/
332
333void ImageLayerWidget::initActions()
334{
335        //if (actgrWms)
336        //      delete actgrWms;
337        //actgrWms = new QActionGroup(this);
338
339        LayerWidget::initActions();
340        ctxMenu->addSeparator();
341        associatedMenu->addSeparator();
342
343        wmsMenu = new QMenu(MerkaartorPreferences::instance()->getBgTypes()[Bg_Wms], this);
344        WmsServerList* WmsServers = MerkaartorPreferences::instance()->getWmsServers();
345        WmsServerListIterator wi(*WmsServers);
346        while (wi.hasNext()) {
347                wi.next();
348                WmsServer S = wi.value();
349                QAction* act = new QAction(S.WmsName, wmsMenu);
350                act->setData(S.WmsName);
351                //act->setCheckable(true);
352                wmsMenu->addAction(act);
353                //actgrAdapter->addAction(act);
354                //actgrWms->addAction(act);
355                if (MerkaartorPreferences::instance()->getBgType() == Bg_Wms)
356                        if (S.WmsName == MerkaartorPreferences::instance()->getSelectedWmsServer())
357                                act->setChecked(true);
358        }
359
360        tmsMenu = new QMenu(MerkaartorPreferences::instance()->getBgTypes()[Bg_Tms], this);
361        TmsServerList* TmsServers = MerkaartorPreferences::instance()->getTmsServers();
362        TmsServerListIterator ti(*TmsServers);
363        while (ti.hasNext()) {
364                ti.next();
365                TmsServer S = ti.value();
366                QAction* act = new QAction(S.TmsName, tmsMenu);
367                act->setData(S.TmsName);
368                tmsMenu->addAction(act);
369                if (MerkaartorPreferences::instance()->getBgType() == Bg_Tms)
370                        if (S.TmsName == MerkaartorPreferences::instance()->getSelectedTmsServer())
371                                act->setChecked(true);
372        }
373
374        actNone->setChecked((MerkaartorPreferences::instance()->getBgType() == Bg_None));
375#ifdef YAHOO
376        actLegalYahoo->setChecked((MerkaartorPreferences::instance()->getBgType() == Bg_Yahoo));
377#endif
378#ifdef YAHOO_ILLEGAL
379        actYahoo->setChecked((MerkaartorPreferences::instance()->getBgType() == Bg_Yahoo_illegal));
380#endif
381#ifdef GOOGLE_ILLEGAL
382        actGoogle->setChecked((MerkaartorPreferences::instance()->getBgType() == Bg_Google_illegal));
383#endif
384#ifdef MSLIVEMAP_ILLEGAL
385        actVirtEarth->setChecked((MerkaartorPreferences::instance()->getBgType() == Bg_MsVirtualEarth_illegal));
386#endif
387
388        ctxMenu->addAction(actNone);
389        associatedMenu->addAction(actNone);
390
391        ctxMenu->addMenu(wmsMenu);
392        associatedMenu->addMenu(wmsMenu);
393        connect(wmsMenu, SIGNAL(triggered(QAction*)), this, SLOT(setWms(QAction*)));
394
395        ctxMenu->addMenu(tmsMenu);
396        associatedMenu->addMenu(tmsMenu);
397        connect(tmsMenu, SIGNAL(triggered(QAction*)), this, SLOT(setTms(QAction*)));
398
399//      ctxMenu->addAction(actOSM);
400#ifdef YAHOO
401        ctxMenu->addAction(actLegalYahoo);
402        associatedMenu->addAction(actLegalYahoo);
403#endif
404#ifdef YAHOO_ILLEGAL
405        ctxMenu->addAction(actYahoo);
406        associatedMenu->addAction(actYahoo);
407#endif
408#ifdef GOOGLE_ILLEGAL
409        ctxMenu->addAction(actGoogle);
410        associatedMenu->addAction(actGoogle);
411#endif
412#ifdef MSLIVEMAP_ILLEGAL
413        ctxMenu->addAction(actVirtEarth);
414        associatedMenu->addAction(actVirtEarth);
415#endif
416}
417
418// TrackLayerWidget
419
420TrackLayerWidget::TrackLayerWidget(TrackMapLayer* aLayer, QWidget* aParent)
421        : LayerWidget(aLayer, aParent)
422{
423        backColor = QColor(255,255,255);
424        initActions();
425}
426
427void TrackLayerWidget::initActions()
428{
429        LayerWidget::initActions();
430        ctxMenu->addSeparator();
431        associatedMenu->addSeparator();
432
433        QAction* actExtract = new QAction(tr("Extract Drawing layer"), ctxMenu);
434        ctxMenu->addAction(actExtract);
435        associatedMenu->addAction(actExtract);
436        connect(actExtract, SIGNAL(triggered(bool)), this, SLOT(extractLayer(bool)));
437
438        actZoom = new QAction(tr("Zoom"), ctxMenu);
439        ctxMenu->addAction(actZoom);
440        associatedMenu->addAction(actZoom);
441        connect(actZoom, SIGNAL(triggered(bool)), this, SLOT(zoomLayer(bool)));
442
443        ctxMenu->addSeparator();
444        associatedMenu->addSeparator();
445
446        closeAction = new QAction(tr("Close"), this);
447        connect(closeAction, SIGNAL(triggered()), this, SLOT(close()));
448        ctxMenu->addAction(closeAction);
449        associatedMenu->addAction(closeAction);
450        closeAction->setEnabled(theLayer->canDelete());
451}
452
453TrackLayerWidget::~TrackLayerWidget()
454{
455}
456
457void TrackLayerWidget::extractLayer(bool)
458{
459        ((TrackMapLayer*)theLayer)->extractLayer();
460        emit (layerChanged(this, false));
461}
462
463// DirtyLayerWidget
464
465DirtyLayerWidget::DirtyLayerWidget(DirtyMapLayer* aLayer, QWidget* aParent)
466        : LayerWidget(aLayer, aParent)
467{
468        backColor = QColor(255,255,255);
469        initActions();
470}
471
472void DirtyLayerWidget::initActions()
473{
474        LayerWidget::initActions();
475        ctxMenu->addSeparator();
476        associatedMenu->addSeparator();
477
478        actZoom = new QAction(tr("Zoom"), ctxMenu);
479        ctxMenu->addAction(actZoom);
480        associatedMenu->addAction(actZoom);
481        connect(actZoom, SIGNAL(triggered(bool)), this, SLOT(zoomLayer(bool)));
482}
483
484// UploadLayerWidget
485
486UploadedLayerWidget::UploadedLayerWidget(UploadedMapLayer* aLayer, QWidget* aParent)
487        : LayerWidget(aLayer, aParent)
488{
489        backColor = QColor(255,255,255);
490        initActions();
491}
492
493void UploadedLayerWidget::initActions()
494{
495        LayerWidget::initActions();
496        ctxMenu->addSeparator();
497        associatedMenu->addSeparator();
498
499        actZoom = new QAction(tr("Zoom"), ctxMenu);
500        ctxMenu->addAction(actZoom);
501        associatedMenu->addAction(actZoom);
502        connect(actZoom, SIGNAL(triggered(bool)), this, SLOT(zoomLayer(bool)));
503
504        closeAction = new QAction(tr("Clear"), this);
505        connect(closeAction, SIGNAL(triggered()), this, SLOT(clear()));
506        ctxMenu->addAction(closeAction);
507        associatedMenu->addAction(closeAction);
508        closeAction->setEnabled(theLayer->canDelete());
509}
510
511// ExtractedLayerWidget
512
513ExtractedLayerWidget::ExtractedLayerWidget(ExtractedMapLayer* aLayer, QWidget* aParent)
514        : LayerWidget(aLayer, aParent)
515{
516        backColor = QColor(255,255,255);
517        initActions();
518}
519
520void ExtractedLayerWidget::initActions()
521{
522        LayerWidget::initActions();
523
524        actZoom = new QAction(tr("Zoom"), ctxMenu);
525        ctxMenu->addAction(actZoom);
526        associatedMenu->addAction(actZoom);
527        connect(actZoom, SIGNAL(triggered(bool)), this, SLOT(zoomLayer(bool)));
528
529        ctxMenu->addSeparator();
530        associatedMenu->addSeparator();
531
532        closeAction = new QAction(tr("Close"), this);
533        connect(closeAction, SIGNAL(triggered()), this, SLOT(close()));
534        ctxMenu->addAction(closeAction);
535        associatedMenu->addAction(closeAction);
536        closeAction->setEnabled(theLayer->canDelete());
537}
538
Note: See TracBrowser for help on using the repository browser.