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

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

ADD : Merkaartor document (*.mdc). fixes #768.
For the moment, the document contains the layouts structure (and their contents as OSM or GPX), the document history (=undo) and the viewport.
This is a biggie and there are probably bugs inside. Please test and report.

File size: 9.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)
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                ctxMenu->exec(anEvent->globalPos());
92}
93
94void LayerWidget::initActions()
95{
96        SAFE_DELETE(ctxMenu);
97        ctxMenu = new QMenu(this);
98
99        QStringList opStr;
100        opStr << "Low" << "High" << "Opaque";
101
102        QActionGroup* actgrp = new QActionGroup(this);
103        QMenu* alphaMenu = new QMenu("Opacity", this);
104        for (int i=0; i<opStr.size(); i++) {
105                QAction* act = new QAction(opStr[i], alphaMenu);
106                actgrp->addAction(act);
107                qreal a = MerkaartorPreferences::instance()->getAlpha(opStr[i]);
108                act->setData(a);
109                act->setCheckable(true);
110                if (theLayer->getAlpha() == a)
111                        act->setChecked(true);
112                alphaMenu->addAction(act);
113        }
114        ctxMenu->addMenu(alphaMenu);
115        connect(alphaMenu, SIGNAL(triggered(QAction*)), this, SLOT(setOpacity(QAction*)));
116}
117
118void LayerWidget::setOpacity(QAction *act)
119{
120        theLayer->setAlpha(act->data().toDouble());
121        emit (layerChanged(this, false));
122}
123
124// DrawingLayerWidget
125
126DrawingLayerWidget::DrawingLayerWidget(DrawingMapLayer* aLayer, QWidget* aParent)
127        : LayerWidget(aLayer, aParent)
128{
129        backColor = QColor(255,255,255);
130}
131
132// ImageLayerWidget
133
134ImageLayerWidget::ImageLayerWidget(ImageMapLayer* aLayer, QWidget* aParent)
135: LayerWidget(aLayer, aParent), wmsMenu(0) //, actgrWms(0)
136{
137        backColor = QColor(128,128,128);
138        //actgrAdapter = new QActionGroup(this);
139
140        actNone = new QAction(MerkaartorPreferences::instance()->getBgTypes()[Bg_None], this);
141        //actNone->setCheckable(true);
142        actNone->setChecked((MerkaartorPreferences::instance()->getBgType() == Bg_None));
143        connect(actNone, SIGNAL(triggered(bool)), this, SLOT(setNone(bool)));
144
145//      actOSM = new QAction(MerkaartorPreferences::instance()->getBgTypes()[Bg_OSM], this);
146//      //actNone->setCheckable(true);
147//      actOSM->setChecked((MerkaartorPreferences::instance()->getBgType() == Bg_OSM));
148//      connect(actOSM, SIGNAL(triggered(bool)), this, SLOT(setOSM(bool)));
149
150#ifdef YAHOO
151        actLegalYahoo = new QAction(MerkaartorPreferences::instance()->getBgTypes()[Bg_Yahoo], this);
152        //actYahoo->setCheckable(true);
153        actLegalYahoo->setChecked((MerkaartorPreferences::instance()->getBgType() == Bg_Yahoo));
154        connect(actLegalYahoo, SIGNAL(triggered(bool)), this, SLOT(setLegalYahoo(bool)));
155#endif
156#ifdef YAHOO_ILLEGAL
157        actYahoo = new QAction(MerkaartorPreferences::instance()->getBgTypes()[Bg_Yahoo_illegal], this);
158        //actYahoo->setCheckable(true);
159        actYahoo->setChecked((MerkaartorPreferences::instance()->getBgType() == Bg_Yahoo_illegal));
160        connect(actYahoo, SIGNAL(triggered(bool)), this, SLOT(setYahoo(bool)));
161#endif
162#ifdef GOOGLE_ILLEGAL
163        actGoogle = new QAction(MerkaartorPreferences::instance()->getBgTypes()[Bg_Google_illegal], this);
164        //actGoogle->setCheckable(true);
165        actGoogle->setChecked((MerkaartorPreferences::instance()->getBgType() == Bg_Google_illegal));
166        connect(actGoogle, SIGNAL(triggered(bool)), this, SLOT(setGoogle(bool)));
167#endif
168        initActions();
169}
170
171ImageLayerWidget::~ImageLayerWidget()
172{
173}
174
175void ImageLayerWidget::setWms(QAction* act)
176{
177        WmsServerList* L = MerkaartorPreferences::instance()->getWmsServers();
178        WmsServer S = L->value(act->data().toString());
179        MerkaartorPreferences::instance()->setSelectedWmsServer(S.WmsName);
180
181        ((ImageMapLayer *)theLayer)->setMapAdapter(Bg_Wms);
182        theLayer->setVisible(true);
183
184        this->update(rect());
185        emit (layerChanged(this, true));
186}
187
188void ImageLayerWidget::setTms(QAction* act)
189{
190        TmsServerList* L = MerkaartorPreferences::instance()->getTmsServers();
191        TmsServer S = L->value(act->data().toString());
192        MerkaartorPreferences::instance()->setSelectedTmsServer(S.TmsName);
193
194        ((ImageMapLayer *)theLayer)->setMapAdapter(Bg_Tms);
195        theLayer->setVisible(true);
196
197        this->update(rect());
198        emit (layerChanged(this, true));
199}
200
201#ifdef YAHOO
202void ImageLayerWidget::setLegalYahoo(bool)
203{
204        ((ImageMapLayer *)theLayer)->setMapAdapter(Bg_Yahoo);
205        theLayer->setVisible(true);
206
207        this->update(rect());
208        emit (layerChanged(this, true));
209}
210#endif
211
212#ifdef YAHOO_ILLEGAL
213void ImageLayerWidget::setYahoo(bool)
214{
215        ((ImageMapLayer *)theLayer)->setMapAdapter(Bg_Yahoo_illegal);
216        theLayer->setVisible(true);
217
218        this->update(rect());
219        emit (layerChanged(this, true));
220}
221#endif
222
223#ifdef GOOGLE_ILLEGAL
224void ImageLayerWidget::setGoogle(bool)
225{
226        ((ImageMapLayer *)theLayer)->setMapAdapter(Bg_Google_illegal);
227        theLayer->setVisible(true);
228
229        this->update(rect());
230        emit (layerChanged(this, true));
231}
232#endif
233
234void ImageLayerWidget::setNone(bool)
235{
236        ((ImageMapLayer *)theLayer)->setMapAdapter(Bg_None);
237
238        this->update(rect());
239        emit (layerChanged(this, true));
240}
241
242/*void ImageLayerWidget::setOSM(bool)
243{
244        ((ImageMapLayer *)theLayer)->setMapAdapter(Bg_OSM);
245        theLayer->setVisible(true);
246
247        this->update(rect());
248        emit (layerChanged(this, true));
249}
250*/
251
252void ImageLayerWidget::initActions()
253{
254        //if (actgrWms)
255        //      delete actgrWms;
256        //actgrWms = new QActionGroup(this);
257
258        LayerWidget::initActions();
259        ctxMenu->addSeparator();
260
261        wmsMenu = new QMenu(MerkaartorPreferences::instance()->getBgTypes()[Bg_Wms], this);
262        WmsServerList* WmsServers = MerkaartorPreferences::instance()->getWmsServers();
263        WmsServerListIterator wi(*WmsServers);
264        while (wi.hasNext()) {
265                wi.next();
266                WmsServer S = wi.value();
267                QAction* act = new QAction(S.WmsName, wmsMenu);
268                act->setData(S.WmsName);
269                //act->setCheckable(true);
270                wmsMenu->addAction(act);
271                //actgrAdapter->addAction(act);
272                //actgrWms->addAction(act);
273                if (MerkaartorPreferences::instance()->getBgType() == Bg_Wms)
274                        if (S.WmsName == MerkaartorPreferences::instance()->getSelectedWmsServer())
275                                act->setChecked(true);
276        }
277
278        tmsMenu = new QMenu(MerkaartorPreferences::instance()->getBgTypes()[Bg_Tms], this);
279        TmsServerList* TmsServers = MerkaartorPreferences::instance()->getTmsServers();
280        TmsServerListIterator ti(*TmsServers);
281        while (ti.hasNext()) {
282                ti.next();
283                TmsServer S = ti.value();
284                QAction* act = new QAction(S.TmsName, tmsMenu);
285                act->setData(S.TmsName);
286                tmsMenu->addAction(act);
287                if (MerkaartorPreferences::instance()->getBgType() == Bg_Tms)
288                        if (S.TmsName == MerkaartorPreferences::instance()->getSelectedTmsServer())
289                                act->setChecked(true);
290        }
291
292        actNone->setChecked((MerkaartorPreferences::instance()->getBgType() == Bg_None));
293#ifdef YAHOO
294        actLegalYahoo->setChecked((MerkaartorPreferences::instance()->getBgType() == Bg_Yahoo));
295#endif
296#ifdef YAHOO_ILLEGAL
297        actYahoo->setChecked((MerkaartorPreferences::instance()->getBgType() == Bg_Yahoo_illegal));
298#endif
299#ifdef GOOGLE_ILLEGAL
300        actGoogle->setChecked((MerkaartorPreferences::instance()->getBgType() == Bg_Google_illegal));
301#endif
302
303        ctxMenu->addAction(actNone);
304
305        ctxMenu->addMenu(wmsMenu);
306        connect(wmsMenu, SIGNAL(triggered(QAction*)), this, SLOT(setWms(QAction*)));
307
308        ctxMenu->addMenu(tmsMenu);
309        connect(tmsMenu, SIGNAL(triggered(QAction*)), this, SLOT(setTms(QAction*)));
310
311//      ctxMenu->addAction(actOSM);
312#ifdef YAHOO
313        ctxMenu->addAction(actLegalYahoo);
314#endif
315#ifdef YAHOO_ILLEGAL
316        ctxMenu->addAction(actYahoo);
317#endif
318#ifdef GOOGLE_ILLEGAL
319        ctxMenu->addAction(actGoogle);
320#endif
321}
322
323// TrackLayerWidget
324
325TrackLayerWidget::TrackLayerWidget(TrackMapLayer* aLayer, QWidget* aParent)
326        : LayerWidget(aLayer, aParent)
327{
328        backColor = QColor(255,255,255);
329}
330
331void TrackLayerWidget::initActions()
332{
333        LayerWidget::initActions();
334        ctxMenu->addSeparator();
335
336        QAction* actExtract = new QAction("Extract Drawing layer", ctxMenu);
337        ctxMenu->addAction(actExtract);
338        connect(actExtract, SIGNAL(triggered(bool)), this, SLOT(extractLayer(bool)));
339}
340
341TrackLayerWidget::~TrackLayerWidget()
342{
343}
344
345void TrackLayerWidget::extractLayer(bool)
346{
347        ((TrackMapLayer*)theLayer)->extractLayer();
348        emit (layerChanged(this, false));
349}
350
351
Note: See TracBrowser for help on using the repository browser.