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

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