QGCMapToolBar.cc 8.55 KB
Newer Older
1 2 3 4 5 6
#include "QGCMapToolBar.h"
#include "QGCMapWidget.h"
#include "ui_QGCMapToolBar.h"

QGCMapToolBar::QGCMapToolBar(QWidget *parent) :
    QWidget(parent),
7 8 9 10 11 12 13 14 15 16
    _ui(new Ui::QGCMapToolBar),
    _map(NULL),
    _optionsMenu(new QMenu(this)),
    _trailPlotMenu(new QMenu(this)),
    _updateTimesMenu(new QMenu(this)),
    _mapTypesMenu(new QMenu(this)),
    _trailSettingsGroup(new QActionGroup(this)),
    _updateTimesGroup(new QActionGroup(this)),
    _mapTypesGroup(new QActionGroup(this)),
    _statusMaxLen(15)
17
{
18
    _ui->setupUi(this);
19 20 21 22
}

void QGCMapToolBar::setMap(QGCMapWidget* map)
{
23
    _map = map;
24

25
    if (_map)
26
    {
27 28 29 30 31 32 33 34 35 36 37 38
        connect(_ui->goToButton, SIGNAL(clicked()), _map, SLOT(showGoToDialog()));
        connect(_ui->goHomeButton, SIGNAL(clicked()), _map, SLOT(goHome()));
        connect(_ui->lastPosButton, SIGNAL(clicked()), _map, SLOT(loadSettings()));
        connect(_ui->clearTrailsButton, SIGNAL(clicked()), _map, SLOT(deleteTrails()));
        connect(_ui->lockCheckBox, SIGNAL(clicked(bool)), _map, SLOT(setZoomBlocked(bool)));
        connect(_map, SIGNAL(OnTileLoadStart()), this, SLOT(tileLoadStart()));
        connect(_map, SIGNAL(OnTileLoadComplete()), this, SLOT(tileLoadEnd()));
        connect(_map, SIGNAL(OnTilesStillToLoad(int)), this, SLOT(tileLoadProgress(int)));
        connect(_ui->ripMapButton, SIGNAL(clicked()), _map, SLOT(cacheVisibleRegion()));

        _ui->followCheckBox->setChecked(_map->getFollowUAVEnabled());
        connect(_ui->followCheckBox, SIGNAL(clicked(bool)), _map, SLOT(setFollowUAVEnabled(bool)));
39 40

        // Edit mode handling
41
        _ui->editButton->hide();
LM's avatar
LM committed
42

43 44 45 46 47 48 49
        const int uavTrailTimeList[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};                      // seconds
        const int uavTrailTimeCount = 10;

        const int uavTrailDistanceList[] = {1, 2, 5, 10, 20, 50, 100, 200, 500};             // meters
        const int uavTrailDistanceCount = 9;

        // Set exclusive items
50 51 52
        _trailSettingsGroup->setExclusive(true);
        _updateTimesGroup->setExclusive(true);
        _mapTypesGroup->setExclusive(true);
53 54

        // Build up menu
55 56 57
        _trailPlotMenu->setTitle(tr("&Add trail dot every.."));
        _updateTimesMenu->setTitle(tr("&Limit map view update rate to.."));
        _mapTypesMenu->setTitle(tr("&Map type"));
58

59 60 61

        //setup the mapTypesMenu
        QAction* action;
62
        action =  _mapTypesMenu->addAction(tr("Bing Hybrid"),this,SLOT(setMapType()));
63 64
        action->setData(MapType::BingHybrid);
        action->setCheckable(true);
65 66 67
#ifdef MAP_DEFAULT_TYPE_BING
        action->setChecked(true);
#endif
68
        _mapTypesGroup->addAction(action);
69

70
        action =  _mapTypesMenu->addAction(tr("Google Hybrid"),this,SLOT(setMapType()));
71 72
        action->setData(MapType::GoogleHybrid);
        action->setCheckable(true);
73 74 75
#ifdef MAP_DEFAULT_TYPE_GOOGLE
        action->setChecked(true);
#endif
76
        _mapTypesGroup->addAction(action);
77

78
        action =  _mapTypesMenu->addAction(tr("OpenStreetMap"),this,SLOT(setMapType()));
79
        action->setData(MapType::OpenStreetMap);
80
        action->setCheckable(true);
81 82 83
#ifdef MAP_DEFAULT_TYPE_OSM
        action->setChecked(true);
#endif
84
        _mapTypesGroup->addAction(action);
85

86
        _optionsMenu->addMenu(_mapTypesMenu);
87 88 89


        // FIXME MARK CURRENT VALUES IN MENU
90
        QAction *defaultTrailAction = _trailPlotMenu->addAction(tr("No trail"), this, SLOT(setUAVTrailTime()));
91 92
        defaultTrailAction->setData(-1);
        defaultTrailAction->setCheckable(true);
93
        _trailSettingsGroup->addAction(defaultTrailAction);
94 95 96

        for (int i = 0; i < uavTrailTimeCount; ++i)
        {
97
            action = _trailPlotMenu->addAction(tr("%1 second%2").arg(uavTrailTimeList[i]).arg((uavTrailTimeList[i] > 1) ? "s" : ""), this, SLOT(setUAVTrailTime()));
98 99
            action->setData(uavTrailTimeList[i]);
            action->setCheckable(true);
100 101
            _trailSettingsGroup->addAction(action);
            if (static_cast<mapcontrol::UAVTrailType::Types>(map->getTrailType()) == mapcontrol::UAVTrailType::ByTimeElapsed && _map->getTrailInterval() == uavTrailTimeList[i])
102 103 104 105
            {
                // This is the current active time, set the action checked
                action->setChecked(true);
            }
106 107 108
        }
        for (int i = 0; i < uavTrailDistanceCount; ++i)
        {
109
            action = _trailPlotMenu->addAction(tr("%1 meter%2").arg(uavTrailDistanceList[i]).arg((uavTrailDistanceList[i] > 1) ? "s" : ""), this, SLOT(setUAVTrailDistance()));
110 111
            action->setData(uavTrailDistanceList[i]);
            action->setCheckable(true);
112 113
            _trailSettingsGroup->addAction(action);
            if (static_cast<mapcontrol::UAVTrailType::Types>(_map->getTrailType()) == mapcontrol::UAVTrailType::ByDistance && _map->getTrailInterval() == uavTrailDistanceList[i])
114 115 116 117
            {
                // This is the current active time, set the action checked
                action->setChecked(true);
            }
118
        }
119 120

        // Set no trail checked if no action is checked yet
121
        if (!_trailSettingsGroup->checkedAction())
122
        {
123
            defaultTrailAction->setChecked(true);
124 125
        }

126
        _optionsMenu->addMenu(_trailPlotMenu);
127 128 129 130 131

        // Add update times menu
        for (int i = 100; i < 5000; i+=400)
        {
            float time = i/1000.0f; // Convert from ms to seconds
132
            QAction* action = _updateTimesMenu->addAction(tr("%1 seconds").arg(time), this, SLOT(setUpdateInterval()));
133 134
            action->setData(time);
            action->setCheckable(true);
135
            if (time == _map->getUpdateRateLimit())
136 137 138 139 140
            {
                action->blockSignals(true);
                action->setChecked(true);
                action->blockSignals(false);
            }
141
            _updateTimesGroup->addAction(action);
142 143 144 145
        }

        // If the current time is not part of the menu defaults
        // still add it as new option
146
        if (!_updateTimesGroup->checkedAction())
147
        {
148 149
            float time = _map->getUpdateRateLimit();
            QAction* action = _updateTimesMenu->addAction(tr("uptate every %1 seconds").arg(time), this, SLOT(setUpdateInterval()));
150 151 152
            action->setData(time);
            action->setCheckable(true);
            action->setChecked(true);
153
            _updateTimesGroup->addAction(action);
154
        }
155
        _optionsMenu->addMenu(_updateTimesMenu);
156

157
        _ui->optionsButton->setMenu(_optionsMenu);
158 159 160
    }
}

LM's avatar
LM committed
161 162
void QGCMapToolBar::setUAVTrailTime()
{
163 164
    QObject* sender = QObject::sender();
    QAction* action = qobject_cast<QAction*>(sender);
LM's avatar
LM committed
165

166 167 168 169 170 171
    if (action)
    {
        bool ok;
        int trailTime = action->data().toInt(&ok);
        if (ok)
        {
172
            (_map->setTrailModeTimed(trailTime));
173
            setStatusLabelText(tr("Trail mode: Every %1 second%2").arg(trailTime).arg((trailTime > 1) ? "s" : ""));
174 175
        }
    }
LM's avatar
LM committed
176 177
}

178 179
void QGCMapToolBar::setStatusLabelText(const QString &text)
{
180
    _ui->posLabel->setText(text.leftJustified(_statusMaxLen, QChar('.'), true));
181 182
}

LM's avatar
LM committed
183 184
void QGCMapToolBar::setUAVTrailDistance()
{
185 186
    QObject* sender = QObject::sender();
    QAction* action = qobject_cast<QAction*>(sender);
LM's avatar
LM committed
187

188 189 190 191 192 193
    if (action)
    {
        bool ok;
        int trailDistance = action->data().toInt(&ok);
        if (ok)
        {
194
            _map->setTrailModeDistance(trailDistance);
195
            setStatusLabelText(tr("Trail mode: Every %1 meter%2").arg(trailDistance).arg((trailDistance == 1) ? "s" : ""));
196 197 198 199 200 201 202 203 204 205 206 207 208
        }
    }
}

void QGCMapToolBar::setUpdateInterval()
{
    QObject* sender = QObject::sender();
    QAction* action = qobject_cast<QAction*>(sender);

    if (action)
    {
        bool ok;
        float time = action->data().toFloat(&ok);
209 210
        if (ok)
        {
211
            _map->setUpdateRateLimit(time);
212
            setStatusLabelText(tr("Limit: %1 second%2").arg(time).arg((time != 1.0f) ? "s" : ""));
213
        }
214
    }
LM's avatar
LM committed
215 216
}

217 218 219 220 221 222 223 224 225 226 227
void QGCMapToolBar::setMapType()
{
    QObject* sender = QObject::sender();
    QAction* action = qobject_cast<QAction*>(sender);

    if (action)
    {
        bool ok;
        int mapType = action->data().toInt(&ok);
        if (ok)
        {
228
            _map->SetMapType((MapType::Types)mapType);
229
            setStatusLabelText(tr("Map: %1").arg(mapType));
230 231 232 233
        }
    }
}

234 235
void QGCMapToolBar::tileLoadStart()
{
236
    setStatusLabelText(tr("Loading"));
237 238 239 240
}

void QGCMapToolBar::tileLoadEnd()
{
241
    setStatusLabelText(tr("Finished"));
242 243 244 245 246 247
}

void QGCMapToolBar::tileLoadProgress(int progress)
{
    if (progress == 1)
    {
248
        setStatusLabelText(tr("1 tile"));
249 250 251
    }
    else if (progress > 0)
    {
252
        setStatusLabelText(tr("%1 tile").arg(progress));
253 254 255 256
    }
    else
    {
        tileLoadEnd();
257 258
    }
}