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

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

ADD : GPS record to track layer (+ pause)
ADD : Mobile tweakings

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