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

Last change on this file since 7056 was 7056, checked in by bvh, 12 years ago

FIX : memory leak (by Daniel van Gerpen)

File size: 5.7 KB
Line 
1#include "LayerWidget.h"
2
3#include "MainWindow.h"
4#include "Map/MapDocument.h"
5#include "Map/MapLayer.h"
6#include "MapView.h"
7#include "Preferences/MerkaartorPreferences.h"
8
9#include <QtGui/QMouseEvent>
10#include <QtGui/QPainter>
11
12#define SAFE_DELETE(x) {delete (x); x = NULL;}
13
14#define LINEHEIGHT 20
15
16LayerWidget::LayerWidget(QWidget* aParent)
17: QAbstractButton(aParent)
18{
19        setCheckable(true);
20        setAutoExclusive(true) ;
21        setFocusPolicy(Qt::NoFocus);
22        visibleIcon = QPixmap(":Icons/eye.xpm");
23        hiddenIcon = QPixmap(":Icons/empty.xpm");
24}
25
26QSize LayerWidget::minimumSizeHint () const
27{
28        return QSize(100, LINEHEIGHT);
29}
30
31QSize LayerWidget::sizeHint () const
32{
33        return QSize(100, LINEHEIGHT);
34}
35
36void LayerWidget::paintEvent(QPaintEvent*)
37{
38        QPainter P(this);
39
40        P.drawLine(rect().bottomLeft(), rect().bottomRight());
41        if (theLayer->isSelected()) {
42                P.fillRect(rect().adjusted(20,0,0,-1),QBrush(palette().highlight()));
43//              P.fillRect(20, 1, width()-19, rect().height()-1, QBrush(palette().highlight()));
44                P.setPen(palette().highlightedText().color());
45                P.drawText(rect().adjusted(23,0,0,-1), Qt::AlignLeft | Qt::AlignVCenter , theLayer->name());
46        } else {
47                P.fillRect(rect().adjusted(0,0,0,-1),backColor);
48                P.setPen(QColor(0,0,0));
49                P.drawText(rect().adjusted(23,0,0,-1), Qt::AlignLeft | Qt::AlignVCenter , theLayer->name());
50        }
51
52        if (theLayer->isVisible())
53                P.drawPixmap(QPoint(2, rect().center().y()-visibleIcon.height()/2), visibleIcon);
54        else
55                P.drawPixmap(QPoint(2, rect().center().y()-hiddenIcon.height()/2), hiddenIcon);
56}
57
58void LayerWidget::mouseReleaseEvent(QMouseEvent* anEvent)
59{
60        if (anEvent->pos().x()<20)
61        {
62                theLayer->setVisible(!theLayer->isVisible());
63                anEvent->ignore();
64                update();
65                emit(layerChanged(this, false));
66        }
67        else
68        {
69                if (!(dynamic_cast<ImageLayerWidget *>(this)))
70                        toggle();
71        }
72}
73
74void LayerWidget::checkStateSet()
75{
76        theLayer->setSelected(isChecked());
77        //emit (layerChanged(this));
78}
79
80MapLayer* LayerWidget::getMapLayer()
81{
82        return theLayer;
83}
84
85// DrawingLayerWidget
86
87DrawingLayerWidget::DrawingLayerWidget(DrawingMapLayer* aLayer, QWidget* aParent)
88: LayerWidget(aParent)
89{
90        theLayer = aLayer;
91        backColor = QColor(255,255,255);
92}
93
94// ImageLayerWidget
95
96ImageLayerWidget::ImageLayerWidget(ImageMapLayer* aLayer, QWidget* aParent)
97: LayerWidget(aParent), actgrWms(0), imageMenu(0)
98{
99        theLayer = aLayer;
100        backColor = QColor(128,128,128);
101        //actgrAdapter = new QActionGroup(this);
102
103        actNone = new QAction(MerkaartorPreferences::instance()->getBgTypes()[Bg_None], this);
104        //actNone->setCheckable(true);
105        actNone->setChecked((MerkaartorPreferences::instance()->getBgType() == Bg_None));
106        connect(actNone, SIGNAL(triggered(bool)), this, SLOT(setNone(bool)));
107
108        actOSM = new QAction(MerkaartorPreferences::instance()->getBgTypes()[Bg_OSM], this);
109        //actNone->setCheckable(true);
110        actOSM->setChecked((MerkaartorPreferences::instance()->getBgType() == Bg_OSM));
111        connect(actOSM, SIGNAL(triggered(bool)), this, SLOT(setOSM(bool)));
112
113#ifdef yahoo_illegal
114        actYahoo = new QAction(MerkaartorPreferences::instance()->getBgTypes()[Bg_Yahoo_illegal], this);
115        //actYahoo->setCheckable(true);
116        actYahoo->setChecked((MerkaartorPreferences::instance()->getBgType() == Bg_Yahoo_illegal));
117        connect(actYahoo, SIGNAL(triggered(bool)), this, SLOT(setYahoo(bool)));
118#endif
119        initWmsActions();
120}
121
122ImageLayerWidget::~ImageLayerWidget()
123{
124}
125
126void ImageLayerWidget::setWms(QAction* act)
127{
128        QStringList server = act->data().toStringList();
129        MerkaartorPreferences::instance()->setSelectedWmsServer(server[6].toInt());
130
131        ((ImageMapLayer *)theLayer)->setMapAdapter(Bg_Wms);
132        theLayer->setVisible(true);
133
134        this->update(rect());
135        emit (layerChanged(this, true));
136}
137
138#ifdef yahoo_illegal
139void ImageLayerWidget::setYahoo(bool)
140{
141        ((ImageMapLayer *)theLayer)->setMapAdapter(Bg_Yahoo_illegal);
142        theLayer->setVisible(true);
143
144        this->update(rect());
145        emit (layerChanged(this, true));
146}
147#endif
148
149void ImageLayerWidget::setNone(bool)
150{
151        ((ImageMapLayer *)theLayer)->setMapAdapter(Bg_None);
152
153        this->update(rect());
154        emit (layerChanged(this, true));
155}
156
157void ImageLayerWidget::setOSM(bool)
158{
159        ((ImageMapLayer *)theLayer)->setMapAdapter(Bg_OSM);
160        theLayer->setVisible(true);
161
162        this->update(rect());
163        emit (layerChanged(this, true));
164}
165
166void ImageLayerWidget::initWmsActions()
167{
168        //if (actgrWms)
169        //      delete actgrWms;
170        //actgrWms = new QActionGroup(this);
171
172        SAFE_DELETE(imageMenu);
173
174        wmsMenu = new QMenu(MerkaartorPreferences::instance()->getBgTypes()[Bg_Wms], this);
175
176        QStringList Servers = MerkaartorPreferences::instance()->getWmsServers();
177        for (int i=0; i<Servers.size(); i+=6) {
178                QStringList oneServer;
179                oneServer.append(Servers[i]);
180                oneServer.append(Servers[i+1]);
181                oneServer.append(Servers[i+2]);
182                oneServer.append(Servers[i+3]);
183                oneServer.append(Servers[i+4]);
184                oneServer.append(Servers[i+5]);
185                oneServer.append(QString().setNum(int(i/6)));
186
187               QAction* act = new QAction(Servers[i], wmsMenu);
188                act->setData(oneServer);
189                //act->setCheckable(true);
190                wmsMenu->addAction(act);
191                //actgrAdapter->addAction(act);
192                //actgrWms->addAction(act);
193                if (MerkaartorPreferences::instance()->getBgType() == Bg_Wms)
194                        if (int(i/6) == MerkaartorPreferences::instance()->getSelectedWmsServer())
195                                act->setChecked(true);
196        }
197        actNone->setChecked((MerkaartorPreferences::instance()->getBgType() == Bg_None));
198#ifdef yahoo_illegal
199        actYahoo->setChecked((MerkaartorPreferences::instance()->getBgType() == Bg_Yahoo_illegal));
200#endif
201
202        imageMenu = new QMenu(this);
203        imageMenu->addAction(actNone);
204
205        imageMenu->addMenu(wmsMenu);
206        connect(wmsMenu, SIGNAL(triggered(QAction*)), this, SLOT(setWms(QAction*)));
207
208        imageMenu->addAction(actOSM);
209#ifdef yahoo_illegal
210        imageMenu->addAction(actYahoo);
211#endif
212}
213
214void ImageLayerWidget::contextMenuEvent(QContextMenuEvent* anEvent)
215{
216        imageMenu->exec(anEvent->globalPos());
217}
218
219
Note: See TracBrowser for help on using the repository browser.