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

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

ADD : Create one layer per track during GPX import (Refs #1054)
ADD : Info dock for layers

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