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

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

ADD : Readonly layers + Preferences to default track layers to readonly (fixes #1460)
FIX : Make upload dialog modal

File size: 16.0 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 YAHOO
258        actLegalYahoo = new QAction(MerkaartorPreferences::instance()->getBgTypes()[Bg_Yahoo], this);
259        //actYahoo->setCheckable(true);
260        actLegalYahoo->setChecked((MerkaartorPreferences::instance()->getBgType() == Bg_Yahoo));
261        connect(actLegalYahoo, SIGNAL(triggered(bool)), this, SLOT(setLegalYahoo(bool)));
262#endif
263#ifdef YAHOO_ILLEGAL
264        actYahoo = new QAction(MerkaartorPreferences::instance()->getBgTypes()[Bg_Yahoo_illegal], this);
265        //actYahoo->setCheckable(true);
266        actYahoo->setChecked((MerkaartorPreferences::instance()->getBgType() == Bg_Yahoo_illegal));
267        connect(actYahoo, SIGNAL(triggered(bool)), this, SLOT(setYahoo(bool)));
268#endif
269#ifdef GOOGLE_ILLEGAL
270        actGoogle = new QAction(MerkaartorPreferences::instance()->getBgTypes()[Bg_Google_illegal], this);
271        //actGoogle->setCheckable(true);
272        actGoogle->setChecked((MerkaartorPreferences::instance()->getBgType() == Bg_Google_illegal));
273        connect(actGoogle, SIGNAL(triggered(bool)), this, SLOT(setGoogle(bool)));
274#endif
275#ifdef MSLIVEMAP_ILLEGAL
276        actVirtEarth = new QAction(MerkaartorPreferences::instance()->getBgTypes()[Bg_MsVirtualEarth_illegal], this);
277        //actVirtEarth->setCheckable(true);
278        actVirtEarth->setChecked((MerkaartorPreferences::instance()->getBgType() == Bg_MsVirtualEarth_illegal));
279        connect(actVirtEarth, SIGNAL(triggered(bool)), this, SLOT(setMsVirtualEarth(bool)));
280#endif
281        initActions();
282}
283
284ImageLayerWidget::~ImageLayerWidget()
285{
286}
287
288void ImageLayerWidget::setWms(QAction* act)
289{
290        WmsServerList L = MerkaartorPreferences::instance()->getWmsServers();
291        WmsServer S = L.value(act->data().toString());
292        MerkaartorPreferences::instance()->setSelectedWmsServer(S.WmsName);
293
294        ((ImageMapLayer *)theLayer)->setMapAdapter(Bg_Wms);
295        theLayer->setVisible(true);
296
297        this->update(rect());
298        emit (layerChanged(this, true));
299}
300
301void ImageLayerWidget::setTms(QAction* act)
302{
303        TmsServerList L = MerkaartorPreferences::instance()->getTmsServers();
304        TmsServer S = L.value(act->data().toString());
305        MerkaartorPreferences::instance()->setSelectedTmsServer(S.TmsName);
306
307        ((ImageMapLayer *)theLayer)->setMapAdapter(Bg_Tms);
308        theLayer->setVisible(true);
309
310        this->update(rect());
311        emit (layerChanged(this, true));
312}
313
314#ifdef YAHOO
315void ImageLayerWidget::setLegalYahoo(bool)
316{
317        ((ImageMapLayer *)theLayer)->setMapAdapter(Bg_Yahoo);
318        theLayer->setVisible(true);
319
320        this->update(rect());
321        emit (layerChanged(this, true));
322}
323#endif
324
325#ifdef YAHOO_ILLEGAL
326void ImageLayerWidget::setYahoo(bool)
327{
328        ((ImageMapLayer *)theLayer)->setMapAdapter(Bg_Yahoo_illegal);
329        theLayer->setVisible(true);
330
331        this->update(rect());
332        emit (layerChanged(this, true));
333}
334#endif
335
336#ifdef GOOGLE_ILLEGAL
337void ImageLayerWidget::setGoogle(bool)
338{
339        ((ImageMapLayer *)theLayer)->setMapAdapter(Bg_Google_illegal);
340        theLayer->setVisible(true);
341
342        this->update(rect());
343        emit (layerChanged(this, true));
344}
345#endif
346
347#ifdef MSLIVEMAP_ILLEGAL
348void ImageLayerWidget::setMsVirtualEarth(bool)
349{
350        ((ImageMapLayer *)theLayer)->setMapAdapter(Bg_MsVirtualEarth_illegal);
351        theLayer->setVisible(true);
352
353        this->update(rect());
354        emit (layerChanged(this, true));
355}
356#endif
357
358void ImageLayerWidget::setNone(bool)
359{
360        ((ImageMapLayer *)theLayer)->setMapAdapter(Bg_None);
361
362        this->update(rect());
363        emit (layerChanged(this, true));
364}
365
366/*void ImageLayerWidget::setOSM(bool)
367{
368        ((ImageMapLayer *)theLayer)->setMapAdapter(Bg_OSM);
369        theLayer->setVisible(true);
370
371        this->update(rect());
372        emit (layerChanged(this, true));
373}
374*/
375
376void ImageLayerWidget::initActions()
377{
378        //if (actgrWms)
379        //      delete actgrWms;
380        //actgrWms = new QActionGroup(this);
381
382        LayerWidget::initActions();
383
384        actReadonly->setVisible(false);
385
386        ctxMenu->addSeparator();
387        associatedMenu->addSeparator();
388
389        wmsMenu = new QMenu(MerkaartorPreferences::instance()->getBgTypes()[Bg_Wms], this);
390        WmsServerList WmsServers = MerkaartorPreferences::instance()->getWmsServers();
391        WmsServerListIterator wi(WmsServers);
392        while (wi.hasNext()) {
393                wi.next();
394                WmsServer S = wi.value();
395                QAction* act = new QAction(S.WmsName, wmsMenu);
396                act->setData(S.WmsName);
397                //act->setCheckable(true);
398                wmsMenu->addAction(act);
399                //actgrAdapter->addAction(act);
400                //actgrWms->addAction(act);
401                if (MerkaartorPreferences::instance()->getBgType() == Bg_Wms)
402                        if (S.WmsName == MerkaartorPreferences::instance()->getSelectedWmsServer())
403                                act->setChecked(true);
404        }
405
406        tmsMenu = new QMenu(MerkaartorPreferences::instance()->getBgTypes()[Bg_Tms], this);
407        TmsServerList TmsServers = MerkaartorPreferences::instance()->getTmsServers();
408        TmsServerListIterator ti(TmsServers);
409        while (ti.hasNext()) {
410                ti.next();
411                TmsServer S = ti.value();
412                QAction* act = new QAction(S.TmsName, tmsMenu);
413                act->setData(S.TmsName);
414                tmsMenu->addAction(act);
415                if (MerkaartorPreferences::instance()->getBgType() == Bg_Tms)
416                        if (S.TmsName == MerkaartorPreferences::instance()->getSelectedTmsServer())
417                                act->setChecked(true);
418        }
419
420        actNone->setChecked((MerkaartorPreferences::instance()->getBgType() == Bg_None));
421#ifdef YAHOO
422        actLegalYahoo->setChecked((MerkaartorPreferences::instance()->getBgType() == Bg_Yahoo));
423#endif
424#ifdef YAHOO_ILLEGAL
425        actYahoo->setChecked((MerkaartorPreferences::instance()->getBgType() == Bg_Yahoo_illegal));
426#endif
427#ifdef GOOGLE_ILLEGAL
428        actGoogle->setChecked((MerkaartorPreferences::instance()->getBgType() == Bg_Google_illegal));
429#endif
430#ifdef MSLIVEMAP_ILLEGAL
431        actVirtEarth->setChecked((MerkaartorPreferences::instance()->getBgType() == Bg_MsVirtualEarth_illegal));
432#endif
433
434        ctxMenu->addAction(actNone);
435        associatedMenu->addAction(actNone);
436
437        ctxMenu->addMenu(wmsMenu);
438        associatedMenu->addMenu(wmsMenu);
439        connect(wmsMenu, SIGNAL(triggered(QAction*)), this, SLOT(setWms(QAction*)));
440
441        ctxMenu->addMenu(tmsMenu);
442        associatedMenu->addMenu(tmsMenu);
443        connect(tmsMenu, SIGNAL(triggered(QAction*)), this, SLOT(setTms(QAction*)));
444
445//      ctxMenu->addAction(actOSM);
446#ifdef YAHOO
447        ctxMenu->addAction(actLegalYahoo);
448        associatedMenu->addAction(actLegalYahoo);
449#endif
450#ifdef YAHOO_ILLEGAL
451        ctxMenu->addAction(actYahoo);
452        associatedMenu->addAction(actYahoo);
453#endif
454#ifdef GOOGLE_ILLEGAL
455        ctxMenu->addAction(actGoogle);
456        associatedMenu->addAction(actGoogle);
457#endif
458#ifdef MSLIVEMAP_ILLEGAL
459        ctxMenu->addAction(actVirtEarth);
460        associatedMenu->addAction(actVirtEarth);
461#endif
462}
463
464// TrackLayerWidget
465
466TrackLayerWidget::TrackLayerWidget(TrackMapLayer* aLayer, QWidget* aParent)
467        : LayerWidget(aLayer, aParent)
468{
469        backColor = QColor(122,204,166);
470        initActions();
471}
472
473void TrackLayerWidget::initActions()
474{
475        LayerWidget::initActions();
476        ctxMenu->addSeparator();
477        associatedMenu->addSeparator();
478
479        QAction* actExtract = new QAction(tr("Extract Drawing layer"), ctxMenu);
480        ctxMenu->addAction(actExtract);
481        associatedMenu->addAction(actExtract);
482        connect(actExtract, SIGNAL(triggered(bool)), this, SLOT(extractLayer(bool)));
483
484        actZoom = new QAction(tr("Zoom"), ctxMenu);
485        ctxMenu->addAction(actZoom);
486        associatedMenu->addAction(actZoom);
487        connect(actZoom, SIGNAL(triggered(bool)), this, SLOT(zoomLayer(bool)));
488
489        ctxMenu->addSeparator();
490        associatedMenu->addSeparator();
491
492        closeAction = new QAction(tr("Close"), this);
493        connect(closeAction, SIGNAL(triggered()), this, SLOT(close()));
494        ctxMenu->addAction(closeAction);
495        associatedMenu->addAction(closeAction);
496        closeAction->setEnabled(theLayer->canDelete());
497}
498
499TrackLayerWidget::~TrackLayerWidget()
500{
501}
502
503void TrackLayerWidget::extractLayer(bool)
504{
505        ((TrackMapLayer*)theLayer)->extractLayer();
506        emit (layerChanged(this, false));
507}
508
509// DirtyLayerWidget
510
511DirtyLayerWidget::DirtyLayerWidget(DirtyMapLayer* aLayer, QWidget* aParent)
512        : LayerWidget(aLayer, aParent)
513{
514        backColor = QColor(200,200,200);
515        initActions();
516}
517
518void DirtyLayerWidget::initActions()
519{
520        LayerWidget::initActions();
521        ctxMenu->addSeparator();
522        associatedMenu->addSeparator();
523
524        actZoom = new QAction(tr("Zoom"), ctxMenu);
525        ctxMenu->addAction(actZoom);
526        associatedMenu->addAction(actZoom);
527        connect(actZoom, SIGNAL(triggered(bool)), this, SLOT(zoomLayer(bool)));
528}
529
530// UploadLayerWidget
531
532UploadedLayerWidget::UploadedLayerWidget(UploadedMapLayer* aLayer, QWidget* aParent)
533        : LayerWidget(aLayer, aParent)
534{
535        backColor = QColor(200,200,200);
536        initActions();
537}
538
539void UploadedLayerWidget::initActions()
540{
541        LayerWidget::initActions();
542        ctxMenu->addSeparator();
543        associatedMenu->addSeparator();
544
545        actZoom = new QAction(tr("Zoom"), ctxMenu);
546        ctxMenu->addAction(actZoom);
547        associatedMenu->addAction(actZoom);
548        connect(actZoom, SIGNAL(triggered(bool)), this, SLOT(zoomLayer(bool)));
549
550        closeAction = new QAction(tr("Clear"), this);
551        connect(closeAction, SIGNAL(triggered()), this, SLOT(clear()));
552        ctxMenu->addAction(closeAction);
553        associatedMenu->addAction(closeAction);
554        closeAction->setEnabled(theLayer->canDelete());
555}
556
557// ExtractedLayerWidget
558
559ExtractedLayerWidget::ExtractedLayerWidget(ExtractedMapLayer* aLayer, QWidget* aParent)
560        : LayerWidget(aLayer, aParent)
561{
562        backColor = QColor(165,209,255);
563        initActions();
564}
565
566void ExtractedLayerWidget::initActions()
567{
568        LayerWidget::initActions();
569
570        actZoom = new QAction(tr("Zoom"), ctxMenu);
571        ctxMenu->addAction(actZoom);
572        associatedMenu->addAction(actZoom);
573        connect(actZoom, SIGNAL(triggered(bool)), this, SLOT(zoomLayer(bool)));
574
575        ctxMenu->addSeparator();
576        associatedMenu->addSeparator();
577
578        closeAction = new QAction(tr("Close"), this);
579        connect(closeAction, SIGNAL(triggered()), this, SLOT(close()));
580        ctxMenu->addAction(closeAction);
581        associatedMenu->addAction(closeAction);
582        closeAction->setEnabled(theLayer->canDelete());
583}
584
585// OsbLayerWidget
586
587OsbLayerWidget::OsbLayerWidget(OsbMapLayer* aLayer, QWidget* aParent)
588        : LayerWidget(aLayer, aParent)
589{
590        backColor = QColor(165,209,192);
591        initActions();
592}
593
594void OsbLayerWidget::initActions()
595{
596        LayerWidget::initActions();
597
598        ctxMenu->addSeparator();
599        associatedMenu->addSeparator();
600
601        closeAction = new QAction(tr("Close"), this);
602        connect(closeAction, SIGNAL(triggered()), this, SLOT(close()));
603        ctxMenu->addAction(closeAction);
604        associatedMenu->addAction(closeAction);
605        closeAction->setEnabled(theLayer->canDelete());
606}
607
Note: See TracBrowser for help on using the repository browser.