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

Last change on this file since 13954 was 13502, checked in by Chris Browet, 11 years ago

ADD : Preference to enable/disable Shape background (less startup time, less memory consumption)

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