QGCMapToolBar.cc 8.5 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
    ui(new Ui::QGCMapToolBar),
8
    map(NULL),
9 10 11
    optionsMenu(this),
    trailPlotMenu(this),
    updateTimesMenu(this),
Don Gagne's avatar
Don Gagne committed
12
    mapTypesMenu(this),
13
    trailSettingsGroup(new QActionGroup(this)),
Don Gagne's avatar
Don Gagne committed
14
    updateTimesGroup(new QActionGroup(this)),
15 16
    mapTypesGroup(new QActionGroup(this)),
    statusMaxLen(15)
17 18 19 20 21 22 23 24 25 26 27 28
{
    ui->setupUi(this);
}

void QGCMapToolBar::setMap(QGCMapWidget* map)
{
    this->map = map;

    if (map)
    {
        connect(ui->goToButton, SIGNAL(clicked()), map, SLOT(showGoToDialog()));
        connect(ui->goHomeButton, SIGNAL(clicked()), map, SLOT(goHome()));
29
        connect(ui->lastPosButton, SIGNAL(clicked()), map, SLOT(loadSettings()));
lm's avatar
lm committed
30
        connect(ui->clearTrailsButton, SIGNAL(clicked()), map, SLOT(deleteTrails()));
31 32 33 34 35 36 37 38 39 40
        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)));

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

42 43 44 45 46 47 48 49 50
        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
        trailSettingsGroup->setExclusive(true);
        updateTimesGroup->setExclusive(true);
51
        mapTypesGroup->setExclusive(true);
52 53 54 55

        // Build up menu
        trailPlotMenu.setTitle(tr("&Add trail dot every.."));
        updateTimesMenu.setTitle(tr("&Limit map view update rate to.."));
56
        mapTypesMenu.setTitle(tr("&Map type"));
57

58 59 60 61 62 63

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

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

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

85 86 87 88 89 90 91 92
        optionsMenu.addMenu(&mapTypesMenu);


        // FIXME MARK CURRENT VALUES IN MENU
        QAction *defaultTrailAction = trailPlotMenu.addAction(tr("No trail"), this, SLOT(setUAVTrailTime()));
        defaultTrailAction->setData(-1);
        defaultTrailAction->setCheckable(true);
        trailSettingsGroup->addAction(defaultTrailAction);
93 94 95

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

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

125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
        optionsMenu.addMenu(&trailPlotMenu);

        // Add update times menu
        for (int i = 100; i < 5000; i+=400)
        {
            float time = i/1000.0f; // Convert from ms to seconds
            QAction* action = updateTimesMenu.addAction(tr("%1 seconds").arg(time), this, SLOT(setUpdateInterval()));
            action->setData(time);
            action->setCheckable(true);
            if (time == map->getUpdateRateLimit())
            {
                action->blockSignals(true);
                action->setChecked(true);
                action->blockSignals(false);
            }
            updateTimesGroup->addAction(action);
        }

        // If the current time is not part of the menu defaults
        // still add it as new option
        if (!updateTimesGroup->checkedAction())
        {
            float time = map->getUpdateRateLimit();
            QAction* action = updateTimesMenu.addAction(tr("uptate every %1 seconds").arg(time), this, SLOT(setUpdateInterval()));
            action->setData(time);
            action->setCheckable(true);
            action->setChecked(true);
            updateTimesGroup->addAction(action);
        }
        optionsMenu.addMenu(&updateTimesMenu);

        ui->optionsButton->setMenu(&optionsMenu);
157 158 159
    }
}

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

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

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

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

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

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

    if (action)
    {
        bool ok;
        float time = action->data().toFloat(&ok);
208 209 210
        if (ok)
        {
            map->setUpdateRateLimit(time);
211
            setStatusLabelText(tr("Limit: %1 second%2").arg(time).arg((time != 1.0f) ? "s" : ""));
212
        }
213
    }
LM's avatar
LM committed
214 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)
        {
            map->SetMapType((MapType::Types)mapType);
228
            setStatusLabelText(tr("Map: %1").arg(mapType));
229 230 231 232
        }
    }
}

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

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

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

259

260 261 262
QGCMapToolBar::~QGCMapToolBar()
{
    delete ui;
263 264
    delete trailSettingsGroup;
    delete updateTimesGroup;
265
    delete mapTypesGroup;
266
    // FIXME Delete all actions
267
}