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

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

FIX : alpha not properly restored from MDC for default layers
FIX : continue road was active even if not at one end

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