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

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

FIX : bug-fixes + adaptations for mobile

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