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

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

ADD : delete layer command (not undo-able)
FIX : document load/save

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