~ubuntu-branches/ubuntu/saucy/merkaartor/saucy

« back to all changes in this revision

Viewing changes to Tools/RegionMapWidget.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Bernd Zeimetz
  • Date: 2009-09-13 00:52:12 UTC
  • mto: (1.2.7 upstream) (0.1.3 upstream) (3.1.7 sid)
  • mto: This revision was merged to the branch mainline in revision 10.
  • Revision ID: james.westby@ubuntu.com-20090913005212-pjecal8zxm07x0fj
ImportĀ upstreamĀ versionĀ 0.14+svnfixes~20090912

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
//
2
 
// C++ Implementation: RegionMapWidget
3
 
//
4
 
// Description:
5
 
//
6
 
//
7
 
// Author: Chris Browet <cbro@semperpax.com>, (C) 2008
8
 
//
9
 
// Copyright: See COPYING file that comes with this distribution
10
 
//
11
 
//
12
 
#include "RegionMapWidget.h"
13
 
#include "Utils/SlippyMapWidget.h"
14
 
 
15
 
#include "ImportExport/ImportExportOsmBin.h"
16
 
 
17
 
#include <QPainter>
18
 
#include <QMouseEvent>
19
 
 
20
 
RegionMapWidget::RegionMapWidget(QWidget* aParent)
21
 
: SlippyMapWidget(aParent), showGrid(false)
22
 
{
23
 
}
24
 
 
25
 
RegionMapWidget::~RegionMapWidget(void)
26
 
{
27
 
}
28
 
 
29
 
void RegionMapWidget::setShowGrid(bool val)
30
 
{
31
 
        showGrid = val;
32
 
}
33
 
 
34
 
bool RegionMapWidget::getShowGrid()
35
 
{
36
 
        return showGrid;
37
 
}
38
 
 
39
 
void RegionMapWidget::paintEvent(QPaintEvent* anEvent)
40
 
{
41
 
        SlippyMapWidget::paintEvent(anEvent);
42
 
 
43
 
        if (!showGrid) return;
44
 
 
45
 
        QPainter P(this);
46
 
        QRect R(viewArea());
47
 
        CoordBox v = CoordBox(Coord(R.y(), R.x()), Coord(R.y()+R.height(), R.x()+R.width()));
48
 
        P.setPen(QPen(Qt::blue,2));
49
 
        P.setBrush(Qt::NoBrush);
50
 
 
51
 
        double sx = (double)width() / v.lonDiff();
52
 
        if (REGION_WIDTH * sx < 6) return;
53
 
 
54
 
        double sy = (double)height() / v.latDiff();
55
 
 
56
 
        qint32 l = v.bottomLeft().lon() / REGION_WIDTH;
57
 
        qint32 t = v.bottomLeft().lat() / REGION_WIDTH;
58
 
        //for (int x = l*REGION_WIDTH; x<v.width(); x+=REGION_WIDTH) {
59
 
        //      for (int y = t*REGION_WIDTH; y<v.height(); y+=REGION_WIDTH) {
60
 
        //              P.drawRect(x * sx, y * sy, REGION_WIDTH * sx, REGION_WIDTH * sy);
61
 
        //      }
62
 
        //}
63
 
        for (qint64 x = l; x<= v.topRight().lon() / REGION_WIDTH; ++x) {
64
 
            P.drawLine(int((x * REGION_WIDTH - v.bottomLeft().lon()) * sx), 0, int((x * REGION_WIDTH - v.bottomLeft().lon()) * sx), height());
65
 
        }
66
 
        for (qint64 y = t; y<= v.topRight().lat() / REGION_WIDTH; ++y) {
67
 
            P.drawLine(0, int(height() - ((y * REGION_WIDTH - v.bottomLeft().lat()) * sy)), width(), int(height() - ((y * REGION_WIDTH - v.bottomLeft().lat()) * sy)));
68
 
        }
69
 
        for (qint64 x = l; x<= v.topRight().lon() / REGION_WIDTH; ++x)
70
 
                for (qint64 y = t; y<= v.topRight().lat() / REGION_WIDTH; ++y) {
71
 
                        quint32 rg = (y + NUM_REGIONS/2)*NUM_REGIONS + (x + NUM_REGIONS/2);
72
 
            QRect rgRect = QRect(int((x * REGION_WIDTH - v.bottomLeft().lon()) * sx),
73
 
                            int(height() - ((y * REGION_WIDTH - v.bottomLeft().lat()) * sy)),
74
 
                            int(REGION_WIDTH * sx), int(-REGION_WIDTH * sy));
75
 
                        //P.setClipRect(rgRect.adjusted(-1, -1, 1, 1));
76
 
                        if (ExistingRegions[rg])
77
 
                P.drawText(int((x * REGION_WIDTH - v.bottomLeft().lon()) * sx),
78
 
                            int(height() - ((y * REGION_WIDTH - v.bottomLeft().lat()) * sy)),
79
 
                                                        QString("%1 - %2").arg(QString::number(rg)).arg(DateRegions[rg].toString("yyyy-MM-dd")));
80
 
                        else
81
 
                P.drawText(int((x * REGION_WIDTH - v.bottomLeft().lon()) * sx),
82
 
                            int(height() - ((y * REGION_WIDTH - v.bottomLeft().lat()) * sy)),
83
 
                                                        QString("%1").arg(QString::number(rg)));
84
 
 
85
 
                        if (ExistingRegions[rg] && !DeleteRegions[rg]) {
86
 
                                //qDebug() << (y + NUM_REGIONS/2)*NUM_REGIONS + (x + NUM_REGIONS/2);
87
 
                                P.save();
88
 
                                P.setPen(Qt::NoPen);
89
 
                                P.setBrush(QBrush(Qt::green, Qt::FDiagPattern));
90
 
 
91
 
                                P.drawRect(rgRect);
92
 
 
93
 
                                P.restore();
94
 
                        }
95
 
                        if (SelectedRegions[rg] && !DeleteRegions[rg]) {
96
 
                                //qDebug() << (y + NUM_REGIONS/2)*NUM_REGIONS + (x + NUM_REGIONS/2);
97
 
                                P.save();
98
 
                                P.setPen(Qt::NoPen);
99
 
                                P.setBrush(QBrush(Qt::blue, Qt::BDiagPattern));
100
 
 
101
 
                                P.drawRect(rgRect);
102
 
 
103
 
                                P.restore();
104
 
                        }
105
 
                }
106
 
 
107
 
 
108
 
}
109
 
 
110
 
void RegionMapWidget::mouseReleaseEvent(QMouseEvent* ev)
111
 
{
112
 
        if (!isDragging()) {
113
 
                QRect R(viewArea());
114
 
                CoordBox v = CoordBox(Coord(R.y(), R.x()), Coord(R.y()+R.height(), R.x()+R.width()));
115
 
                QPointF P = ev->pos();
116
 
 
117
 
        Coord Pt(int(((height()-P.y()) / height() * v.latDiff()) + v.bottomLeft().lat()), int((P.x() / width() * v.lonDiff()) + v.bottomLeft().lon()));
118
 
                int x = int(((qint64)Pt.lon()) / REGION_WIDTH);
119
 
                x = (x < 0) ? x-1 :x;
120
 
                int y = int(((qint64)Pt.lat()) / REGION_WIDTH);
121
 
                y = (y < 0) ? y-1 :y;
122
 
                int rg = (y + NUM_REGIONS/2)*NUM_REGIONS + (x + NUM_REGIONS/2);
123
 
 
124
 
                if (SelectedRegions[rg] && ExistingRegions[rg]) {
125
 
                        SelectedRegions[rg] = false;
126
 
                        DeleteRegions[rg] = true;
127
 
                } else
128
 
                        if (!SelectedRegions[rg] && DeleteRegions[rg])
129
 
                                DeleteRegions[rg] = false;
130
 
                        else
131
 
                                SelectedRegions[rg] = !SelectedRegions[rg];
132
 
 
133
 
                update();
134
 
        }
135
 
 
136
 
        SlippyMapWidget::mouseReleaseEvent(ev);
137
 
}
138