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

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

FIX : Adding a node to an extracted road must keep the road in extracted
FIX : closed layers must stay closed after loading from an MDC

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