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

Last change on this file since 7370 was 7370, checked in by Dirk Stoecker, 12 years ago

fixed i18n error

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