Subversion Repositories Projects

Rev

Rev 750 | Blame | Compare with Previous | Last modification | View Log | RSS feed

/***************************************************************************
 *   Copyright (C) 2009 by Manuel Schrape                                  *
 *   manuel.schrape@gmx.de                                                 *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License.        *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/


#include <QCryptographicHash>
#include <QMessageBox>
#include <QDomDocument>
#include <QFile>

#include "dlg_Main.h"
#include "dlg_MapPos.h"

// Konstruktor Main-Form
dlg_Main::dlg_Main()
{
    setupUi(this);

    o_Settings = new cSettings();

    wg_Connection->set_Client(ID_MAPS, QA_NAME + " " + QA_VERSION, DataFields);

    o_Map = new MapControl(QSize(25,25));
    l_RouteWP = new LineString();
    init_Directorys();
    init_GUI();
    init_Connections();
}

// Grafische Oberfläche initialisieren
void dlg_Main::init_GUI()
{
    setWindowTitle(QA_NAME + " " + QA_VERSION);

    resize(o_Settings->GUI.Size);
    move(o_Settings->GUI.Point);

    if (o_Settings->GUI.isMax)
    {
        showMaximized();
    }

    wg_Connection->set_SelectVisible(false);

    cb_CenterPos->setChecked(o_Settings->CONFIG.cb_CenterPos);
    cb_ShowRoute->setChecked(o_Settings->CONFIG.cb_ShowRoute);
    cb_ShowWPs->setChecked(o_Settings->CONFIG.cb_ShowWPs);
    cb_Goto->setChecked(o_Settings->CONFIG.cb_Goto);

//    tb_More->addWidget(cb_Maps);
    cb_Maps->setVisible(false);

    ac_Toolbar->setChecked(o_Settings->GUI.Toolbar);

    if (ac_Toolbar->isChecked())
    {
        wg_Connection->set_ButtonVisible(false);
    }
    else
    {
        ToolBar->setVisible(false);
        tb_More->setVisible(false);
    }
}

// Signale mit Slots verbinden
void dlg_Main::init_Connections()
{
    connect(ac_Connect, SIGNAL(triggered()), wg_Connection, SLOT(slot_btn_Connect()));
    connect(ac_Toolbar, SIGNAL(triggered()), this, SLOT(slot_ac_Toolbar()));

    connect(sl_Zoom,   SIGNAL(valueChanged(int)), this, SLOT(slot_Zoom(int)));
    connect(cb_Maps,   SIGNAL(currentIndexChanged(int)), this, SLOT(slot_ChangeMap(int)));

    connect(cb_ShowWPs, SIGNAL(toggled(bool)), this, SLOT(slot_ShowWayPoints(bool)));

    connect(btn_WPAdd,    SIGNAL(clicked()), this, SLOT(slot_btn_WPAdd()));
    connect(btn_WPFly,    SIGNAL(clicked()), this, SLOT(slot_btn_WPFly()));
    connect(btn_WPDelete, SIGNAL(clicked()), this, SLOT(slot_btn_WPDelete()));

    connect(ac_LoadRoute, SIGNAL(triggered()), this, SLOT(slot_ac_LoadWayPoints()));
    connect(ac_SaveRoute, SIGNAL(triggered()), this, SLOT(slot_ac_SaveRoute()));

    connect(ac_LoadMap,     SIGNAL(triggered()), this, SLOT(slot_ac_LoadMapPic()));
    connect(ac_Record,      SIGNAL(triggered()), this, SLOT(slot_ac_Record()));
    connect(ac_RouteDelete, SIGNAL(triggered()), this, SLOT(slot_ac_RouteDelete()));

    // About QMK-Kernel & About-QT Dialog einfügen
    connect(ac_About, SIGNAL(triggered()), this, SLOT(slot_ac_About()));
    menu_Help->addAction(trUtf8("Über &Qt"), qApp, SLOT(aboutQt()));

    connect(wg_Connection, SIGNAL(sig_Status(int)), this, SLOT(slot_ConnectionStatus(int)));
    connect(wg_Connection, SIGNAL(sig_MK_Version(s_Hardware)), this, SLOT(slot_MK_Version(s_Hardware)));
    connect(wg_Connection, SIGNAL(sig_MK_NaviData(s_MK_NaviData)), this, SLOT(slot_MK_NaviData(s_MK_NaviData)));
    connect(wg_Connection, SIGNAL(sig_MK_WayPoint(int)), this, SLOT(slot_MK_WayPoint(int)));
}

void dlg_Main::init_Directorys()
{
    QDir *t_Dir = new QDir();

    s_Dir.MainData = QDir::homePath() + "/QMK-Data";
    if (!t_Dir->exists(s_Dir.MainData))
    {
        t_Dir->mkdir(s_Dir.MainData);
    }

    s_Dir.MapCache = s_Dir.MainData + "/Map-Cache";
    if (!t_Dir->exists(s_Dir.MapCache))
    {
        t_Dir->mkdir(s_Dir.MapCache);
    }

    s_Dir.WPRoutes = s_Dir.MainData + "/WP-Routes";
    if (!t_Dir->exists(s_Dir.WPRoutes))
    {
        t_Dir->mkdir(s_Dir.WPRoutes);
    }

    s_Dir.MapPics = s_Dir.MainData + "/Map-Pics";
    if (!t_Dir->exists(s_Dir.MapPics))
    {
        t_Dir->mkdir(s_Dir.MapPics);
    }

    s_Dir.Flights = s_Dir.MainData + "/Flights";
    if (!t_Dir->exists(s_Dir.Flights))
    {
        t_Dir->mkdir(s_Dir.Flights);
    }
}

///////////////////////////////////////////////////////////////////
// QMK-Maps                                                      //
///////////////////////////////////////////////////////////////////

void dlg_Main::create_Map()
{
    o_Map->resize(w_Map->size() - QSize(25,50));
    o_Map->enablePersistentCache(s_Dir.MapCache);
    o_Map->showScale(true);

    o_Adapter = new OSMMapAdapter();

    o_Layer   = new MapLayer("MapLayer", o_Adapter);
    o_Click   = new GeometryLayer("Click", o_Adapter);
    o_Info    = new GeometryLayer("Poute", o_Adapter);
    o_RouteWP = new GeometryLayer("Route-WayPoint", o_Adapter);
    o_RouteFL = new GeometryLayer("Route-Flight", o_Adapter);

    o_Map->addLayer(o_Layer);
    o_Map->addLayer(o_Click);
    o_Map->addLayer(o_Info);
    o_Map->addLayer(o_RouteWP);
    o_Map->addLayer(o_RouteFL);

    o_Map->setZoom(17);
    o_Map->setView(QPointF(o_Settings->NAVI.Longitude,o_Settings->NAVI.Latitude));
//    o_Map->setView(QPointF(13.5,52.5));
//    o_Map->setView(QPointF(13.419805,52.431787));
    connect(o_Map, SIGNAL(mouseEventCoordinate(const QMouseEvent*, const QPointF)), this, SLOT(slot_Click(const QMouseEvent*, const QPointF)));

    l_Map->addWidget(o_Map);

    sl_Zoom->setValue(17);

    // Flight
    Pen[0] = new QPen(QColor(0,0,255,255));
    Pen[0]->setWidth(2);
    Pen[1] = new QPen(QColor(0,0,255,255));
    Pen[1]->setWidth(1);
    // WayPoint
    Pen[2] = new QPen(QColor(255,0,0,255));
    Pen[2]->setWidth(2);
    Pen[3] = new QPen(QColor(255,0,0,255));
    Pen[3]->setWidth(1);
    // Info
    Pen[4] = new QPen(QColor(0,128,128,255));
    Pen[4]->setWidth(2);
    Pen[5] = new QPen(QColor(0,128,128,255));
    Pen[5]->setWidth(1);
}

// auf Veränderung der Fenstergröße reagieren
void dlg_Main::resizeEvent ( QResizeEvent * event )
{
    event = event;
    o_Map->resize(w_Map->size() - QSize(25,25));
}

void dlg_Main::wheelEvent(QWheelEvent *event)
{
    int zoomValue = sl_Zoom->value();
    int numDegrees = event->delta() / 8;
    int numSteps = numDegrees / 15;
    zoomValue += numSteps;
    if (zoomValue < 0) { zoomValue = 0;}
    if (zoomValue > 18) { zoomValue = 18;}
    sl_Zoom->setValue(zoomValue);
}

QList<sWayPoint> dlg_Main::parse_WayPointKML(QString s_File)
{
    QList<sWayPoint> tmp_WayPoints;
    sWayPoint tmp_WayPoint;

    QFile f_KML(s_File);
    f_KML.open(QIODevice::ReadOnly | QIODevice::Text);

    QByteArray s_KML;

    while (!f_KML.atEnd())
    {
        s_KML.append(f_KML.readLine());
     }

    f_KML.close();

    QDomDocument *UserXML;
    UserXML = new QDomDocument;

    UserXML->setContent(s_KML);

    QDomElement Root       = UserXML->firstChildElement("kml");
    QDomElement Document   = Root.firstChildElement("Document");
    QDomElement Placemark  = Document.firstChildElement("Placemark");
    QDomElement Linestring = Placemark.firstChildElement("LineString");

    QString Name = Placemark.firstChildElement("name").toElement().text();

    QString Route = Linestring.firstChildElement("coordinates").toElement().text();

    QStringList s_Points = Route.split(" ");

    QStringList Position;

    for (int z = 0; z < s_Points.count() - 1; z++)
    {
        if (z != 20)
        {
           Position = s_Points[z].split(",");
           tmp_WayPoint.Longitude = Position[0].toDouble();
           tmp_WayPoint.Latitude  = Position[1].toDouble();
           tmp_WayPoint.Altitude  = Position[2].toDouble();
           tmp_WayPoint.Time      = sb_Time->value();

           tmp_WayPoints.append(tmp_WayPoint);
        }
        else
        {
            QMessageBox::warning(this, QA_NAME,trUtf8("Die Wegpunkt-Liste umfasst mehr als 20 Einträge. Es werden nur die ersten 20 Einträge übernommen."), QMessageBox::Ok);

            btn_WPAdd->setEnabled(false);

            z = s_Points.count();
        }
    }
    return tmp_WayPoints;
}

QList<sWayPoint> dlg_Main::parse_WayPointMKW(QString s_File)
{
    QList<sWayPoint> tmp_WayPoints;
    sWayPoint tmp_WayPoint;

    QFile f_MKW(s_File);
    f_MKW.open(QIODevice::ReadOnly | QIODevice::Text);

    QString s_MKW;

    while (!f_MKW.atEnd())
    {
        s_MKW.append(f_MKW.readLine());
     }

    f_MKW.close();

    QStringList s_Points = s_MKW.split(" ");

    QStringList Position;

    for (int z = 0; z < s_Points.count() - 1; z++)
    {
        if (z != 20)
        {
           Position = s_Points[z].split(",");
           tmp_WayPoint.Longitude = Position[0].toDouble();
           tmp_WayPoint.Latitude  = Position[1].toDouble();
           tmp_WayPoint.Altitude  = Position[2].toDouble();
           tmp_WayPoint.Time      = Position[3].toInt();

           tmp_WayPoints.append(tmp_WayPoint);
        }
        else
        {
            QMessageBox::warning(this, QA_NAME,trUtf8("Die Wegpunkt-Liste umfasst mehr als 20 Einträge. Es werden nur die ersten 20 Einträge übernommen."), QMessageBox::Ok);

            btn_WPAdd->setEnabled(false);

            z = s_Points.count();
        }
    }
    return tmp_WayPoints;
}

// Waypoint-Route anzeigen in Karte
void dlg_Main::show_WayPoints(QList<sWayPoint> WayPoints)
{
    Point* p_Point;

    o_RouteWP->removeGeometry(l_RouteWP);
    p_RouteWP.clear();
    l_WayPoints.clear();

    l_WayPoints = WayPoints;

    for (int z = 0; z < WayPoints.count(); z++)
    {
        p_Point = new Point(WayPoints[z].Longitude, WayPoints[z].Latitude);

        p_RouteWP.append(p_Point);
    }

    l_RouteWP = new LineString(p_RouteWP, "", Pen[3]);
    o_RouteWP->addGeometry(l_RouteWP);

    o_Map->setView(p_Point);

    o_Map->updateRequestNew();
}

// Waypoint-Liste speichern
void dlg_Main::save_WayPointsMKW(QString s_File)
{
    QFile *f_MKW = new QFile(s_File);

    f_MKW->open(QIODevice::ReadWrite | QIODevice::Text);

    QTextStream out(f_MKW);

    out.setRealNumberPrecision(9);

    for (int z = 0; z < l_WayPoints.count(); z++)
    {
        out << l_WayPoints[z].Longitude << "," << l_WayPoints[z].Latitude << "," << l_WayPoints[z].Altitude << "," << l_WayPoints[z].Time << " \n";
    }

    f_MKW->close();
}

// Aktuelle MK-Position hinzufügen
void dlg_Main::add_Position(s_MK_NaviData t_NaviData)
{
    sWayPoint WayPoint;

    WayPoint.Longitude = HandlerMK::Int2Double(t_NaviData.CurrentPosition.Longitude, 7);
    WayPoint.Latitude = HandlerMK::Int2Double(t_NaviData.CurrentPosition.Latitude, 7);
    WayPoint.Altitude = HandlerMK::Int2Double(t_NaviData.CurrentPosition.Altitude, 3);

    o_Settings->NAVI.Latitude  = WayPoint.Latitude;
    o_Settings->NAVI.Longitude = WayPoint.Longitude;

    l_Track.append(WayPoint);

    o_RouteFL->removeGeometry(l_RouteFL);
    p_RouteFL.append(new Point(WayPoint.Longitude, WayPoint.Latitude));

    o_Click->removeGeometry(LastPos);

    Point* P = new CirclePoint(WayPoint.Longitude, WayPoint.Latitude, "P1", Point::Middle, Pen[0]);
    LastPos = P;
//    P->setBaselevel(17);
    o_Click->addGeometry(P);

    // Target anzeigen
    o_Click->removeGeometry(Target);

    Target = new ImagePoint( HandlerMK::Int2Double(t_NaviData.TargetPosition.Longitude, 7), HandlerMK::Int2Double(t_NaviData.TargetPosition.Latitude, 7), ":/Flags/Global/Images/Flags/Target.png", "Start");
    Target->setBaselevel(o_Adapter->adaptedZoom());
    o_Click->addGeometry(Target);

    if (cb_CenterPos->isChecked())
    {
        o_Map->setView(QPointF(WayPoint.Longitude, WayPoint.Latitude));
    }

    if (cb_ShowRoute->isChecked())
    {
        l_RouteFL = new LineString(p_RouteFL, "", Pen[1]);

        o_RouteFL->addGeometry(l_RouteFL);
    }
    o_Map->updateRequestNew();

    if (ac_Record->isChecked())
    {
        QTextStream Out(o_Record);

        Out.setRealNumberPrecision(9);

        Out << WayPoint.Longitude << ", " << WayPoint.Latitude << ", " << WayPoint.Altitude << "\n";
    }
}

// Ein Ziel anfliegen.
// TODO: Check auf  Entfernung zur IST-Position.
void dlg_Main::send_Target(Point *t_Target)
{
    if (l_Track.count() > 0)
    {
        s_MK_WayPoint s_WayPoint;

        s_WayPoint.Position.Latitude  = int32_t(t_Target->latitude() * 10000000+0.5);
        s_WayPoint.Position.Longitude = int32_t(t_Target->longitude() * 10000000+0.5);
        s_WayPoint.Position.Altitude  = 0;
        s_WayPoint.Position.Status    = NEWDATA;
        s_WayPoint.Heading            = -1;
        s_WayPoint.ToleranceRadius    = 5;
        s_WayPoint.HoldTime           = sb_Time->value();
        s_WayPoint.Event_Flag         = 0;
        s_WayPoint.Index              = 1;
        s_WayPoint.reserve[0]         = 0; // reserve
        s_WayPoint.reserve[1]         = 0; // reserve
        s_WayPoint.reserve[2]         = 0; // reserve
        s_WayPoint.reserve[3]         = 0;

        memcpy((unsigned char *)&c_Data, (unsigned char *)&s_WayPoint, sizeof(s_WayPoint));
        wg_Connection->send_Data(HandlerMK::make_Frame('s', ADDRESS_NC, c_Data, sizeof(s_WayPoint)).toLatin1().data());
    }
    else
    {
        QMessageBox::warning(this, QA_NAME,tr("Es wurden noch keine aktuellen Positionsdaten vom Mikrokopter empfangen."), QMessageBox::Ok);
    }
}

//Waypoint-Liste zum MK senden
void dlg_Main::send_WayPoints(QList<sWayPoint> t_WayPoints, int t_Pos)
{
    s_MK_WayPoint s_WayPoint;
    double Longitude, Latitude;

    if (t_Pos == 0)
    {   // Waypoint-Liste löschen
        s_WayPoint.Position.Status = INVALID;

        memcpy((unsigned char *)&c_Data, (unsigned char *)&s_WayPoint, sizeof(s_WayPoint));
        wg_Connection->send_Data(HandlerMK::make_Frame('w', ADDRESS_NC, c_Data, sizeof(s_WayPoint)).toLatin1().data(), DATA_WRITE_WAYPOINT);
    }
    else
    {
        int z = t_Pos - 1;
        Longitude = t_WayPoints[z].Longitude;
        Latitude  = t_WayPoints[z].Latitude;

        if (Longitude < 100)
            Longitude *= 10000000+0.5;

        if (Latitude < 100)
            Latitude *= 10000000+0.5;

        //fülle Wegpunkt-Daten
        s_WayPoint.Position.Altitude = 0;
        s_WayPoint.Position.Longitude = int32_t(Longitude);
        s_WayPoint.Position.Latitude =  int32_t(Latitude);
        s_WayPoint.Position.Status = NEWDATA;
        s_WayPoint.Heading = -1;
        s_WayPoint.ToleranceRadius = 5;
        s_WayPoint.HoldTime = t_WayPoints[z].Time;
        s_WayPoint.Event_Flag = 0;
        s_WayPoint.Index      = t_Pos;
        s_WayPoint.reserve[0] = 0; // reserve
        s_WayPoint.reserve[1] = 0; // reserve
        s_WayPoint.reserve[2] = 0; // reserve
        s_WayPoint.reserve[3] = 0; // reserve

        memcpy((unsigned char *)&c_Data, (unsigned char *)&s_WayPoint, sizeof(s_WayPoint));
        wg_Connection->send_Data(HandlerMK::make_Frame('w', ADDRESS_NC, c_Data, sizeof(s_WayPoint)).toLatin1().data(), DATA_WRITE_WAYPOINT);
    }
}

///////////
// Slots //
///////////

// About-Dialog
void dlg_Main::slot_ac_About()
{
    QMessageBox::about(this, trUtf8(("Über ")) + QA_NAME, QA_ABOUT);
}

void dlg_Main::slot_ac_Toolbar()
{
    if (ac_Toolbar->isChecked())
    {
        wg_Connection->set_ButtonVisible(false);
        ToolBar->setVisible(true);
        tb_More->setVisible(true);
    }
    else
    {
        wg_Connection->set_ButtonVisible(true);
        ToolBar->setVisible(false);
        tb_More->setVisible(false);
    }
}

void dlg_Main::slot_ConnectionStatus(int li_Status)
{
    if (li_Status)
    {
        ac_Connect->setChecked(true);
        ac_Connect->setText(tr("Trennen"));
    }
    else
    {
        ac_Connect->setChecked(false);
        ac_Connect->setText(tr("Verbinden"));
    }
}

void dlg_Main::slot_MK_Version(s_Hardware ls_Version)
{
    gs_Version = ls_Version;
    setWindowTitle(QA_NAME + " " + QA_VERSION + " - " + ls_Version.Hardware + " " + ls_Version.Version);
}

void dlg_Main::slot_MK_NaviData(s_MK_NaviData ps_MK_NaviData)
{
    QString Mode = "NC-Flags : ";
    if (ps_MK_NaviData.NCFlags &  0x08) {le_Mode->setText(tr("Range Limit")); Mode += "R";}
    if (ps_MK_NaviData.NCFlags &  0x10) {le_Mode->setText(tr("Serial Error")); Mode += "S";}
    if (ps_MK_NaviData.NCFlags &  0x20) {le_Mode->setText(tr("Target reached")); Mode += "T";}
    if (ps_MK_NaviData.NCFlags &  0x40) {le_Mode->setText(tr("Manual Control")); Mode += "M";}
    if (ps_MK_NaviData.NCFlags &  0x01) {le_Mode->setText(tr("Free")); Mode += "F";}
    if (ps_MK_NaviData.NCFlags &  0x02) {le_Mode->setText(tr("Position Hold")); Mode += "P";}
    if (ps_MK_NaviData.NCFlags &  0x04) {le_Mode->setText(tr("Coming Home")); Mode += "C";}

    le_WP->setText(QString("%1/").arg(ps_MK_NaviData.WaypointIndex) + QString("%1").arg(ps_MK_NaviData.WaypointNumber));
    le_WPTime->setText(QString("%1:").arg(ps_MK_NaviData.TargetHoldTime / 60) + (QString("%1").arg(ps_MK_NaviData.TargetHoldTime % 60)).rightJustified(2, '0'));
    le_WPDist->setText(QString("%1m").arg(ps_MK_NaviData.TargetPositionDeviation.Distance / 10));
    le_HomeDist->setText(QString("%1m").arg(ps_MK_NaviData.HomePositionDeviation.Distance / 10));
    le_Sats->setText(QString("%1").arg(ps_MK_NaviData.SatsInUse));

    add_Position(ps_MK_NaviData);
}

void dlg_Main::slot_MK_WayPoint(int pi_WayPointID)
{
    if (pi_WayPointID < l_WayPoints.count())
    {
        send_WayPoints(l_WayPoints, pi_WayPointID + 1);
    }
}
///////////////////////////////////////////////////////////////////
// QMK-Maps                                                      //
///////////////////////////////////////////////////////////////////

// Zoom der Karte ändern
void dlg_Main::slot_Zoom(int t_Zoom)
{
    o_Map->setZoom(t_Zoom);
}

// Karte wechseln
void dlg_Main::slot_ChangeMap(int t_Set)
{
    int zoom = o_Adapter->adaptedZoom();
    QPointF a = o_Map->currentCoordinate();

    o_Map->setZoom(0);

    switch(t_Set)
    {
        case 0 : // OpenStreetMap
        {
            o_Adapter = new OSMMapAdapter();
        }
        break;
        case 1 : // Yahoo Sat
        {
            o_Adapter = new TileMapAdapter("tile.openaerialmap.org", "/tiles/1.0.0/openaerialmap-900913/%1/%2/%3.png", 256, 0, 17);
        }
        break;
        case 2 : // Google Maps
        {
            o_Adapter = new GoogleMapAdapter();
        }
        break;
        case 3 : // Google Sat
        {
            o_Adapter = new GoogleSatMapAdapter();
        }
        break;
        case 4 : // Yahoo Maps
        {
            o_Adapter = new YahooMapAdapter();
        }
        break;
        case 5 : // Yahoo Sat
        {
            o_Adapter = new YahooMapAdapter("us.maps3.yimg.com", "/aerial.maps.yimg.com/png?v=1.7&t=a&s=256&x=%2&y=%3&z=%1");
        }
        break;
    }

    o_Layer->setMapAdapter(o_Adapter);
    o_Click->setMapAdapter(o_Adapter);
    o_Info->setMapAdapter(o_Adapter);
    o_RouteWP->setMapAdapter(o_Adapter);
    o_RouteFL->setMapAdapter(o_Adapter);

    o_Map->updateRequestNew();
    o_Map->setZoom(zoom);
}

// Click in die Karte
void dlg_Main::slot_Click(const QMouseEvent* Event, const QPointF Coord)
{
    if ((Event->type() == QEvent::MouseButtonPress) && ((Event->button() == Qt::RightButton) || (Event->button() == Qt::MidButton)))
    {
        sl_Zoom->setValue(o_Adapter->adaptedZoom());
    }

    // Überwachen ob Karte verschoben wird
    if ((Event->type() == QEvent::MouseButtonPress) && (Event->button() == Qt::LeftButton))
    {
        MapCenter = o_Map->currentCoordinate();
    }

    // Nur wenn nicht Verschoben dann einen Punkt setzen
    if ((Event->type() == QEvent::MouseButtonRelease) && (Event->button() == Qt::LeftButton))
    {
        if (o_Map->currentCoordinate() == MapCenter)
        {
            if (l_WayPoints.count() < 20)
            {
                btn_WPAdd->setEnabled(true);
            }

            o_Click->removeGeometry(ClickPoint);

            ClickPoint = new CirclePoint(Coord.x(), Coord.y(), 6, "P1", Point::Middle, Pen[2]);


            LastClick = new Point(Coord.x(), Coord.y());

            ClickPoint->setBaselevel(o_Adapter->adaptedZoom());
            o_Click->addGeometry(ClickPoint);

//            o_Click->removeGeometry(Flag);
//            Flag = new ImagePoint(Coord.x(), Coord.y(), ":/Flags/Global/Images/Flags/Target.png", "Start");
//            Flag->setBaselevel(o_Adapter->adaptedZoom());
//            o_Click->addGeometry(Flag);


            if (cb_Goto->isChecked())
            {
                send_Target(LastClick);
            }
        }
    }

    o_Map->updateRequestNew();
//    qDebug(QString("%1").arg(Coord.x()).toLatin1().data());
//    qDebug(QString("%1").arg(Coord.y()).toLatin1().data());
}

// WayPoint zur Liste hinzufügen
void dlg_Main::slot_btn_WPAdd()
{
    cb_ShowWPs->setChecked(true);

    sWayPoint WayPoint;

    WayPoint.Longitude = LastClick->longitude();
    WayPoint.Latitude = LastClick->latitude();
    WayPoint.Time = sb_Time->value();

    l_WayPoints.append(WayPoint);

    o_RouteWP->removeGeometry(l_RouteWP);

    p_RouteWP.append(LastClick);
    l_RouteWP = new LineString(p_RouteWP, "", Pen[3]);

    o_RouteWP->addGeometry(l_RouteWP);
    o_Map->updateRequestNew();

    btn_WPFly->setEnabled(true);

    if (l_WayPoints.count() == 20)
    {
        QMessageBox::warning(this, QA_NAME,trUtf8("Wegpunkt-Liste ist voll. Es können maximal 20 Wegpunkte benutzt werden."), QMessageBox::Ok);
        btn_WPAdd->setEnabled(false);
    }
}

// WayPoint-Liste übertragen
void dlg_Main::slot_btn_WPFly()
{
    send_WayPoints(l_WayPoints, 0);
}

// WayPpoint-Liste löschen
void dlg_Main::slot_btn_WPDelete()
{
    p_RouteWP.clear();
    l_WayPoints.clear();
    o_RouteWP->clearGeometries();
    l_RouteWP = new LineString(p_RouteWP, "", Pen[0]);
//    o_RouteWP->addGeometry(l_RouteWP);
//    o_RouteWP->removeGeometry(l_RouteWP);

    o_Map->updateRequestNew();

    btn_WPFly->setEnabled(false);
    btn_WPAdd->setEnabled(true);
}

// WayPoint-Liste laden
void dlg_Main::slot_ac_LoadWayPoints()
{
    QString Filename = QFileDialog::getOpenFileName(this, "WayPoint-Route laden",  s_Dir.WPRoutes, "Mikrokopter WayPoints(*.mkw);;KML-Datei(*.kml);;Alle Dateien (*)");

    if (!Filename.isEmpty())
    {
        if (Filename.endsWith(".kml", Qt::CaseInsensitive))
        {
            cb_ShowWPs->setChecked(true);
            btn_WPFly->setEnabled(true);

            show_WayPoints(parse_WayPointKML(Filename));
        }
        if (Filename.endsWith(".mkw", Qt::CaseInsensitive))
        {
            cb_ShowWPs->setChecked(true);
            btn_WPFly->setEnabled(true);

            show_WayPoints(parse_WayPointMKW(Filename));
        }
    }
}

// WayPoint-Liste speichern
void dlg_Main::slot_ac_SaveRoute()
{
    QString Filename = QFileDialog::getSaveFileName(this, "WayPoint-Route speichern",  s_Dir.WPRoutes, "Mikrokopter WayPoints(*.mkw);;Alle Dateien (*)");

    if (!Filename.isEmpty())
    {
        if (!(Filename.endsWith(".mkw", Qt::CaseInsensitive)))
        {
            Filename = Filename + QString(".mkw");
        }

        save_WayPointsMKW(Filename);
    }
}

// Route anzeigen / ausblenden
void dlg_Main::slot_ShowWayPoints(bool Show)
{
    if (Show == true)
    {
        if (l_RouteWP->hasPoints())
        {
            o_RouteWP->addGeometry(l_RouteWP);
            o_Map->updateRequestNew();
        }
    }
    else
    {
        o_RouteWP->removeGeometry(l_RouteWP);
        o_Map->updateRequestNew();
    }
}

// Bilddatei als Karteladen.
void dlg_Main::slot_ac_LoadMapPic()
{
    QString Filename = QFileDialog::getOpenFileName(this, "Bild als Karte",  s_Dir.MapPics, "Bilddatei(*.jpg *.png *.gif);;Alle Dateien (*)");

    if (!Filename.isEmpty())
    {
        QFile *f_Points = new QFile(Filename + ".pos");

        if (f_Points->exists())
        {
            f_Points->open(QIODevice::ReadOnly | QIODevice::Text);

            QString s_Points;

            while (!f_Points->atEnd())
            {
                s_Points.append(f_Points->readLine());
            }

            f_Points->close();

            QStringList s_Pos = s_Points.split(",");

            FixedImageOverlay* fip = new FixedImageOverlay(s_Pos[0].toDouble(), s_Pos[1].toDouble(), s_Pos[2].toDouble(), s_Pos[3].toDouble(), Filename);

            o_Layer->addGeometry(fip);
            o_Map->setView(QPointF(((s_Pos[0].toDouble() + s_Pos[2].toDouble()) / 2),((s_Pos[1].toDouble() + s_Pos[3].toDouble()) / 2)));

            o_Map->updateRequestNew();
        }
        else
        {
            dlg_MapPos *f_MapPos = new dlg_MapPos(this);

            if (f_MapPos->exec()==QDialog::Accepted)
            {
                QString Data = f_MapPos->get_Data();

                f_Points->open(QIODevice::ReadWrite | QIODevice::Text);

                QTextStream out(f_Points);

                out.setRealNumberPrecision(9);

                out << Data << "\n";

                f_Points->close();

                QStringList s_Pos = Data.split(",");

                FixedImageOverlay* fip = new FixedImageOverlay(s_Pos[0].toDouble(), s_Pos[1].toDouble(), s_Pos[2].toDouble(), s_Pos[3].toDouble(), Filename);

                o_Layer->addGeometry(fip);
                o_Map->updateRequestNew();
            }

        }
    }
}

// Flug als KML aufzeichnen
void dlg_Main::slot_ac_Record()
{
    if (ac_Record->isChecked())
    {
        QString f_Name = s_Dir.Flights + "/" + QDate::currentDate().toString(("yyyy-MM-dd")) + "_" + QTime::currentTime().toString("hh-mm");

        ac_Record->setText(tr("Stoppen"));

        o_Record = new QFile(f_Name + ".kml");
        o_Record->open(QIODevice::Append | QIODevice::Text);

        QTextStream Out(o_Record);
        Out << HandlerKML::get_Header(tr("Mikrokopter Flugaufzeichnung")) << "\n";

    }
    else
    {
        QTextStream Out(o_Record);
        Out << HandlerKML::get_Footer() << "\n";
        o_Record->close();
        ac_Record->setText(tr("Aufzeichnen"));
    }
}

// WayPpoint-Liste löschen
void dlg_Main::slot_ac_RouteDelete()
{
    p_RouteFL.clear();
    l_Track.clear();
    o_RouteFL->clearGeometries();
    l_RouteFL = new LineString(p_RouteFL, "", Pen[0]);
//    o_RouteFL->removeGeometry(l_RouteFL);

    o_Map->updateRequestNew();
}

// Programm Ende
dlg_Main::~dlg_Main()
{
    o_Settings->GUI.isMax       = isMaximized();
    o_Settings->GUI.Size        = size();
    o_Settings->GUI.Point       = pos();
    o_Settings->GUI.Toolbar     = ac_Toolbar->isChecked();

    o_Settings->CONFIG.cb_CenterPos = cb_CenterPos->isChecked();
    o_Settings->CONFIG.cb_ShowRoute = cb_ShowRoute->isChecked();
    o_Settings->CONFIG.cb_ShowWPs   = cb_ShowWPs->isChecked();
    o_Settings->CONFIG.cb_Goto      = cb_Goto->isChecked();

    o_Settings->NAVI.StayTime = sb_Time->value();

    o_Settings->write_Settings();
 
//    qDebug("Ende.");
}