1 /**********************************************************************************************
2     Copyright (C) 2017 Oliver Eichler <oliver.eichler@gmx.de>
3 
4     This program is free software: you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation, either version 3 of the License, or
7     (at your option) any later version.
8 
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13 
14     You should have received a copy of the GNU General Public License
15     along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 
17 **********************************************************************************************/
18 
19 #include "CMainWindow.h"
20 #include "gis/CGisListWks.h"
21 #include "gis/ovl/CGisItemOvlArea.h"
22 #include "gis/qlb/CQlbProject.h"
23 #include "gis/rte/CGisItemRte.h"
24 #include "gis/trk/CGisItemTrk.h"
25 #include "gis/wpt/CGisItemWpt.h"
26 #include "qlgt/CQlb.h"
27 #include "qlgt/CQlgtDiary.h"
28 #include "qlgt/CQlgtRoute.h"
29 #include "qlgt/CQlgtTrack.h"
30 #include "qlgt/CQlgtWpt.h"
31 #include "qlgt/IQlgtOverlay.h"
32 
33 #include <QtWidgets>
34 
CQlbProject(const QString & filename,CGisListWks * parent)35 CQlbProject::CQlbProject(const QString& filename, CGisListWks* parent)
36     : IGisProject(eTypeQlb, filename, parent)
37 {
38     setIcon(CGisListWks::eColumnIcon, QIcon("://icons/32x32/QlbProject.png"));
39 
40     // create file instance
41     QFile file(filename);
42 
43     // if the file does not exist, the filename is assumed to be a name for a new project
44     if(!file.exists() || QFileInfo(filename).suffix().toLower() != "qlb")
45     {
46         IGisProject::filename.clear();
47         setupName(filename);
48         setToolTip(CGisListWks::eColumnName, getInfo());
49         valid = true;
50         return;
51     }
52 
53     if(!file.open(QIODevice::ReadOnly))
54     {
55         QMessageBox::critical(CMainWindow::getBestWidgetForParent(), tr("Failed to open..."), tr("Failed to open %1").arg(filename), QMessageBox::Abort);
56         return;
57     }
58 
59     load(filename);
60 
61     markAsSaved();
62     setupName(QFileInfo(filename).completeBaseName().replace("_", " "));
63     setToolTip(CGisListWks::eColumnName, getInfo());
64     updateItems();
65     valid = true;
66 }
67 
load(const QString & filename)68 void CQlbProject::load(const QString& filename)
69 {
70     CQlb qlb(nullptr);
71     qlb.load(filename);
72 
73     loadWpts(qlb.waypoints());
74     loadTrks(qlb.tracks());
75     loadRtes(qlb.routes());
76     loadOvls(qlb.overlays());
77 }
78 
loadWpts(QByteArray & array)79 void CQlbProject::loadWpts(QByteArray& array)
80 {
81     QDataStream stream(&array, QIODevice::ReadOnly);
82     stream.setVersion(QDataStream::Qt_4_5);
83 
84     while(!stream.atEnd())
85     {
86         CQlgtWpt wpt(0, nullptr);
87         stream >> wpt;
88         new CGisItemWpt(wpt, this);
89     }
90 }
91 
loadTrks(QByteArray & array)92 void CQlbProject::loadTrks(QByteArray& array)
93 {
94     QDataStream stream(&array, QIODevice::ReadOnly);
95     stream.setVersion(QDataStream::Qt_4_5);
96 
97     while(!stream.atEnd())
98     {
99         CQlgtTrack trk(0, nullptr);
100         stream >> trk;
101         new CGisItemTrk(trk, this);
102     }
103 }
104 
loadRtes(QByteArray & array)105 void CQlbProject::loadRtes(QByteArray& array)
106 {
107     QDataStream stream(&array, QIODevice::ReadOnly);
108     stream.setVersion(QDataStream::Qt_4_5);
109 
110     while(!stream.atEnd())
111     {
112         CQlgtRoute rte(0, nullptr);
113         stream >> rte;
114         new CGisItemRte(rte, this);
115     }
116 }
117 
loadOvls(QByteArray & array)118 void CQlbProject::loadOvls(QByteArray& array)
119 {
120     QDataStream stream(&array, QIODevice::ReadOnly);
121     stream.setVersion(QDataStream::Qt_4_5);
122 
123     bool warningDone = false;
124     while(!stream.atEnd())
125     {
126         IQlgtOverlay ovl(0, nullptr);
127         stream >> ovl;
128         if(ovl.type == "Area")
129         {
130             new CGisItemOvlArea(ovl, this);
131         }
132         else if(ovl.type == "Distance")
133         {
134             new CGisItemTrk(ovl, this);
135         }
136         else if(!warningDone)
137         {
138             QMessageBox::warning(CMainWindow::self().getBestWidgetForParent(), tr("Could not convert...")
139                                  , tr("The file contains overlays that can not be converted. This is because "
140                                       "QMapShack does not support all overlay types of QLandkarte."), QMessageBox::Ok);
141             warningDone = true;
142         }
143     }
144 }
145 
146