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

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

FIX : Layer widgets management
ADD : Background tiles caching (closes #747)
FIX : setViewport without downloading every intermediate zoom tiles
ADD : Initial track to road (#748)
ADD : Initial osmarender rendering (#694)

File size: 6.9 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(QWidget* aParent)
18: QAbstractButton(aParent), 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
27QSize LayerWidget::minimumSizeHint () const
28{
29        return QSize(100, LINEHEIGHT);
30}
31
32QSize LayerWidget::sizeHint () const
33{
34        return QSize(100, LINEHEIGHT);
35}
36
37void LayerWidget::paintEvent(QPaintEvent*)
38{
39        QPainter P(this);
40
41        P.drawLine(rect().bottomLeft(), rect().bottomRight());
42        if (theLayer->isSelected()) {
43                P.fillRect(rect().adjusted(20,0,0,-1),QBrush(palette().highlight()));
44//              P.fillRect(20, 1, width()-19, rect().height()-1, QBrush(palette().highlight()));
45                P.setPen(palette().highlightedText().color());
46                P.drawText(rect().adjusted(23,0,0,-1), Qt::AlignLeft | Qt::AlignVCenter , theLayer->name());
47        } else {
48                P.fillRect(rect().adjusted(0,0,0,-1),backColor);
49                P.setPen(QColor(0,0,0));
50                P.drawText(rect().adjusted(23,0,0,-1), Qt::AlignLeft | Qt::AlignVCenter , theLayer->name());
51        }
52
53        if (theLayer->isVisible())
54                P.drawPixmap(QPoint(2, rect().center().y()-visibleIcon.height()/2), visibleIcon);
55        else
56                P.drawPixmap(QPoint(2, rect().center().y()-hiddenIcon.height()/2), hiddenIcon);
57}
58
59void LayerWidget::mouseReleaseEvent(QMouseEvent* anEvent)
60{
61        if (anEvent->pos().x()<20)
62        {
63                theLayer->setVisible(!theLayer->isVisible());
64                anEvent->ignore();
65                update();
66                emit(layerChanged(this, false));
67        }
68        else
69        {
70                if (!(dynamic_cast<ImageLayerWidget *>(this)))
71                        toggle();
72        }
73}
74
75void LayerWidget::checkStateSet()
76{
77        theLayer->setSelected(isChecked());
78        //emit (layerChanged(this));
79}
80
81MapLayer* LayerWidget::getMapLayer()
82{
83        return theLayer;
84}
85
86void LayerWidget::contextMenuEvent(QContextMenuEvent* anEvent)
87{
88        if (ctxMenu)
89                ctxMenu->exec(anEvent->globalPos());
90}
91
92
93// DrawingLayerWidget
94
95DrawingLayerWidget::DrawingLayerWidget(DrawingMapLayer* aLayer, QWidget* aParent)
96: LayerWidget(aParent)
97{
98        theLayer = aLayer;
99        backColor = QColor(255,255,255);
100}
101
102// ImageLayerWidget
103
104ImageLayerWidget::ImageLayerWidget(ImageMapLayer* aLayer, QWidget* aParent)
105: LayerWidget(aParent), actgrWms(0)
106{
107        theLayer = aLayer;
108        backColor = QColor(128,128,128);
109        //actgrAdapter = new QActionGroup(this);
110
111        actNone = new QAction(MerkaartorPreferences::instance()->getBgTypes()[Bg_None], this);
112        //actNone->setCheckable(true);
113        actNone->setChecked((MerkaartorPreferences::instance()->getBgType() == Bg_None));
114        connect(actNone, SIGNAL(triggered(bool)), this, SLOT(setNone(bool)));
115
116        actOSM = new QAction(MerkaartorPreferences::instance()->getBgTypes()[Bg_OSM], this);
117        //actNone->setCheckable(true);
118        actOSM->setChecked((MerkaartorPreferences::instance()->getBgType() == Bg_OSM));
119        connect(actOSM, SIGNAL(triggered(bool)), this, SLOT(setOSM(bool)));
120
121#ifdef yahoo_illegal
122        actYahoo = new QAction(MerkaartorPreferences::instance()->getBgTypes()[Bg_Yahoo_illegal], this);
123        //actYahoo->setCheckable(true);
124        actYahoo->setChecked((MerkaartorPreferences::instance()->getBgType() == Bg_Yahoo_illegal));
125        connect(actYahoo, SIGNAL(triggered(bool)), this, SLOT(setYahoo(bool)));
126#endif
127#ifdef google_illegal
128        actGoogle = new QAction(MerkaartorPreferences::instance()->getBgTypes()[Bg_Google_illegal], this);
129        //actGoogle->setCheckable(true);
130        actGoogle->setChecked((MerkaartorPreferences::instance()->getBgType() == Bg_Google_illegal));
131        connect(actGoogle, SIGNAL(triggered(bool)), this, SLOT(setGoogle(bool)));
132#endif
133        initWmsActions();
134}
135
136ImageLayerWidget::~ImageLayerWidget()
137{
138}
139
140void ImageLayerWidget::setWms(QAction* act)
141{
142        QStringList server = act->data().toStringList();
143        MerkaartorPreferences::instance()->setSelectedWmsServer(server[6].toInt());
144
145        ((ImageMapLayer *)theLayer)->setMapAdapter(Bg_Wms);
146        theLayer->setVisible(true);
147
148        this->update(rect());
149        emit (layerChanged(this, true));
150}
151
152#ifdef yahoo_illegal
153void ImageLayerWidget::setYahoo(bool)
154{
155        ((ImageMapLayer *)theLayer)->setMapAdapter(Bg_Yahoo_illegal);
156        theLayer->setVisible(true);
157
158        this->update(rect());
159        emit (layerChanged(this, true));
160}
161#endif
162
163#ifdef google_illegal
164void ImageLayerWidget::setGoogle(bool)
165{
166        ((ImageMapLayer *)theLayer)->setMapAdapter(Bg_Google_illegal);
167        theLayer->setVisible(true);
168
169        this->update(rect());
170        emit (layerChanged(this, true));
171}
172#endif
173
174void ImageLayerWidget::setNone(bool)
175{
176        ((ImageMapLayer *)theLayer)->setMapAdapter(Bg_None);
177
178        this->update(rect());
179        emit (layerChanged(this, true));
180}
181
182void ImageLayerWidget::setOSM(bool)
183{
184        ((ImageMapLayer *)theLayer)->setMapAdapter(Bg_OSM);
185        theLayer->setVisible(true);
186
187        this->update(rect());
188        emit (layerChanged(this, true));
189}
190
191void ImageLayerWidget::initWmsActions()
192{
193        //if (actgrWms)
194        //      delete actgrWms;
195        //actgrWms = new QActionGroup(this);
196
197        SAFE_DELETE(ctxMenu);
198
199        wmsMenu = new QMenu(MerkaartorPreferences::instance()->getBgTypes()[Bg_Wms], this);
200
201        QStringList Servers = MerkaartorPreferences::instance()->getWmsServers();
202        for (int i=0; i<Servers.size(); i+=6) {
203                QStringList oneServer;
204                oneServer.append(Servers[i]);
205                oneServer.append(Servers[i+1]);
206                oneServer.append(Servers[i+2]);
207                oneServer.append(Servers[i+3]);
208                oneServer.append(Servers[i+4]);
209                oneServer.append(Servers[i+5]);
210                oneServer.append(QString().setNum(int(i/6)));
211
212                QAction* act = new QAction(Servers[i], wmsMenu);
213                act->setData(oneServer);
214                //act->setCheckable(true);
215                wmsMenu->addAction(act);
216                //actgrAdapter->addAction(act);
217                //actgrWms->addAction(act);
218                if (MerkaartorPreferences::instance()->getBgType() == Bg_Wms)
219                        if (int(i/6) == MerkaartorPreferences::instance()->getSelectedWmsServer())
220                                act->setChecked(true);
221        }
222        actNone->setChecked((MerkaartorPreferences::instance()->getBgType() == Bg_None));
223#ifdef yahoo_illegal
224        actYahoo->setChecked((MerkaartorPreferences::instance()->getBgType() == Bg_Yahoo_illegal));
225#endif
226#ifdef google_illegal
227        actGoogle->setChecked((MerkaartorPreferences::instance()->getBgType() == Bg_Google_illegal));
228#endif
229
230        ctxMenu = new QMenu(this);
231        ctxMenu->addAction(actNone);
232
233        ctxMenu->addMenu(wmsMenu);
234        connect(wmsMenu, SIGNAL(triggered(QAction*)), this, SLOT(setWms(QAction*)));
235
236        ctxMenu->addAction(actOSM);
237#ifdef yahoo_illegal
238        ctxMenu->addAction(actYahoo);
239#endif
240#ifdef google_illegal
241        ctxMenu->addAction(actGoogle);
242#endif
243}
244
245// TrackLayerWidget
246
247TrackLayerWidget::TrackLayerWidget(TrackMapLayer* aLayer, QWidget* /* aParent */)
248{
249        theLayer = aLayer;
250        backColor = QColor(255,255,255);
251
252        ctxMenu = new QMenu(this);
253
254        QAction* actExtract = new QAction("Extract Drawing layer", ctxMenu);
255        ctxMenu->addAction(actExtract);
256        connect(actExtract, SIGNAL(triggered(bool)), this, SLOT(extractLayer(bool)));
257}
258
259TrackLayerWidget::~TrackLayerWidget()
260{
261}
262
263void TrackLayerWidget::extractLayer(bool)
264{
265        ((TrackMapLayer*)theLayer)->extractLayer();
266        emit (layerChanged(this, false));
267}
Note: See TracBrowser for help on using the repository browser.