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

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

FIX : nice panning speedup at low zoom
FIX : OSB fixes

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