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

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

FIX : reinstate the possibility to delete layers

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