• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Stellarium / stellarium / 6685397774

29 Oct 2023 07:37PM UTC coverage: 11.735% (-0.002%) from 11.737%
6685397774

push

github

10110111
Deduplicate title bar implementation

131 of 131 new or added lines in 56 files covered. (100.0%)

14842 of 126472 relevant lines covered (11.74%)

21617.74 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/plugins/Satellites/src/gui/SatellitesImportDialog.cpp
1
/*
2
 * Stellarium Satellites Plug-in: satellites import feature
3
 * Copyright (C) 2012 Bogdan Marinov
4
 * 
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 * 
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 * 
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18
*/
19

20
#include "SatellitesImportDialog.hpp"
21
#include "ui_satellitesImportDialog.h"
22

23
#include "StelApp.hpp"
24
#include "StelGui.hpp"
25
#include "StelModuleMgr.hpp" // for the GETSTELMODULE macro :(
26
#include "StelTranslator.hpp"
27
#include "StelProgressController.hpp"
28

29
#include <QDesktopServices>
30
#include <QFileDialog>
31
#include <QFileInfo>
32
#include <QNetworkReply>
33
#include <QProgressBar>
34
#include <QSortFilterProxyModel>
35
#include <QStandardItemModel>
36
#include <QTemporaryFile>
37
#include <QDir>
38
#include <QStandardPaths>
39

40
SatellitesImportDialog::SatellitesImportDialog()
×
41
        : StelDialog("SatellitesImport")
42
        , isGettingData(false)
×
43
        , numberDownloadsComplete(0)
×
44
        , downloadMgr(nullptr)
×
45
        , progressBar(nullptr)
×
46
        , filterProxyModel(nullptr)
×
47
{
48
        ui = new Ui_satellitesImportDialog;
×
49
        newSatellitesModel = new QStandardItemModel(this);
×
50
}
×
51

52
SatellitesImportDialog::~SatellitesImportDialog()
×
53
{
54
        delete ui;
×
55
        
56
        // Do I need to explicitly delete this?
57
        if (progressBar)
×
58
        {
59
                StelApp::getInstance().removeProgressBar(progressBar);
×
60
                progressBar = nullptr;
×
61
        }
62
        
63
        if (newSatellitesModel)
×
64
        {
65
                newSatellitesModel->clear();
×
66
                delete newSatellitesModel;
×
67
                newSatellitesModel = nullptr;
×
68
        }
69
}
×
70

71
void SatellitesImportDialog::retranslate()
×
72
{
73
        if (dialog)
×
74
        {
75
                ui->retranslateUi(dialog);
×
76
        }
77
}
×
78

79
void SatellitesImportDialog::setVisible(bool visible)
×
80
{
81
        StelDialog::setVisible(visible);
×
82
        if (!isGettingData)
×
83
                getData();
×
84
}
×
85

86
void SatellitesImportDialog::createDialogContent()
×
87
{
88
        ui->setupUi(dialog);
×
89

90
        // Kinetic scrolling
91
        kineticScrollingList << ui->listView;
×
92
        StelGui* gui= dynamic_cast<StelGui*>(StelApp::getInstance().getGui());
×
93
        if (gui)
×
94
        {
95
                enableKineticScrolling(gui->getFlagUseKineticScrolling());
×
96
                connect(gui, SIGNAL(flagUseKineticScrollingChanged(bool)), this, SLOT(enableKineticScrolling(bool)));
×
97
        }
98

99
        connect(ui->titleBar, &TitleBar::closeClicked, this, &StelDialog::close);
×
100
        connect(ui->titleBar, SIGNAL(movedTo(QPoint)), this, SLOT(handleMovedTo(QPoint)));
×
101

102
        connect(ui->pushButtonGetData, SIGNAL(clicked()),
×
103
                this, SLOT(getData()));
104
        connect(ui->pushButtonAbort, SIGNAL(clicked()),
×
105
                this, SLOT(abortDownloads()));
106
        connect(ui->pushButtonAdd, SIGNAL(clicked()),
×
107
                this, SLOT(acceptNewSatellites()));
108
        connect(ui->pushButtonDiscard, SIGNAL(clicked()),
×
109
                this, SLOT(discardNewSatellites()));
110
        connect(ui->pushButtonMarkAll, SIGNAL(clicked()),
×
111
                this, SLOT(markAll()));
112
        connect(ui->pushButtonMarkNone, SIGNAL(clicked()),
×
113
                this, SLOT(markNone()));
114
        
115
        filterProxyModel = new QSortFilterProxyModel(this);
×
116
        filterProxyModel->setSourceModel(newSatellitesModel);
×
117
        filterProxyModel->setFilterCaseSensitivity(Qt::CaseInsensitive);
×
118
        ui->listView->setModel(filterProxyModel);
×
119
        connect(ui->lineEditSearch, SIGNAL(textChanged(const QString&)),
×
120
                filterProxyModel, SLOT(setFilterFixedString(const QString&)));
×
121
        
122
        reset();
×
123
}
×
124

125
void SatellitesImportDialog::getData()
×
126
{
127
        if (isGettingData)
×
128
                return;
×
129
        isGettingData = true;
×
130
        
131
        if (!downloadMgr)
×
132
        {
133
                downloadMgr = StelApp::getInstance().getNetworkAccessManager();
×
134
                connect(downloadMgr, SIGNAL(finished(QNetworkReply*)),
×
135
                        this, SLOT(receiveDownload(QNetworkReply*)));
136
        }
137
        Satellites* satMgr = GETSTELMODULE(Satellites);
×
138
        
139
        if (satMgr->getUpdatesEnabled())
×
140
        {
141
                sourceUrls = satMgr->getTleSources();                
×
142
                qDeleteAll(sourceFiles);
×
143
                sourceFiles.clear();
×
144
                numberDownloadsComplete = 0;
×
145
                
146
                // Reusing some code from Satellites::updateTLEs()
147
                if (progressBar == nullptr)
×
148
                        progressBar = StelApp::getInstance().addProgressBar();
×
149
                progressBar->setValue(0);
×
150
                progressBar->setRange(0, sourceUrls.size());
×
151
                // TRANSLATORS: The full phrase is 'Loading TLE %VALUE%/%MAX%' in progress bar
152
                progressBar->setFormat(QString("%1 %v/%m").arg(q_("Loading TLE")));
×
153
                
154
                ui->pushButtonGetData->setVisible(false);
×
155
                ui->pushButtonAbort->setVisible(true);
×
156
                ui->groupBoxWorking->setTitle(q_("Downloading data..."));
×
157
                displayMessage(q_("Stellarium is downloading satellite data from the update sources. Please wait..."));
×
158
                
159
                for (int i = 0; i < sourceUrls.size(); i++)
×
160
                {
161
                        QString urlData = sourceUrls.at(i);
×
162
                        QUrl url(urlData.remove("1,", Qt::CaseInsensitive));
×
163
                        QNetworkReply* reply = downloadMgr->get(QNetworkRequest(url));
×
164
                        activeDownloads.append(reply);
×
165
                }
×
166
        }
167
        else
168
        {
169
                QStringList sourceFilePaths;
×
170
                // XXX: we should check that there is at least one home location.
171
                QString homeDirPath = QStandardPaths::standardLocations(QStandardPaths::HomeLocation).constFirst();
×
172
                sourceFilePaths = QFileDialog::getOpenFileNames(
×
173
                                      nullptr,
174
                                      q_("Select TLE source file(s)..."),
×
175
                                      homeDirPath, "*.*");
×
176
                if (sourceFilePaths.isEmpty())
×
177
                        return;
×
178
                for (auto &filePath : sourceFilePaths)
×
179
                {
180
                        QFileInfo fileInfo(filePath);
×
181
                        if (fileInfo.exists() && fileInfo.isReadable())
×
182
                        {
183
                                QFile* file = new QFile(filePath);
×
184
                                sourceFiles.append(file);
×
185
                        }
186
                }
×
187
                ui->pushButtonGetData->setVisible(false);
×
188
                ui->pushButtonAbort->setVisible(false);
×
189
                ui->groupBoxWorking->setTitle(q_("Processing data..."));
×
190
                displayMessage(q_("Processing data..."));
×
191
                populateList();
×
192
        }
×
193
}
194

195
void SatellitesImportDialog::receiveDownload(QNetworkReply* networkReply)
×
196
{
197
        Q_ASSERT(networkReply);
×
198
        
199
        // First, check if this one is one of ours
200
        QString url = networkReply->request().url().toString();
×
201
        if (!activeDownloads.contains(networkReply))
×
202
        {
203
                qDebug() << "Satellites: Received URL not in the source list:" << url;
×
204
                return;
×
205
        }
206
        
207
        // An error is a completed download, isn't it?
208
        activeDownloads.removeAll(networkReply);
×
209
        numberDownloadsComplete++;
×
210
        if (progressBar)
×
211
                progressBar->setValue(numberDownloadsComplete);
×
212
        
213
        // Then, see if there was an error...
214
        if (networkReply->error() != QNetworkReply::NoError || networkReply->bytesAvailable()==0)
×
215
        {
216
                qWarning() << "Satellites: failed to download " << url
×
217
                           << networkReply->errorString();
×
218
                return;
×
219
        }
220
        
221
        QTemporaryFile* tmpFile = new QTemporaryFile();
×
222
        if (tmpFile->open())
×
223
        {
224
                tmpFile->write(networkReply->readAll());
×
225
                tmpFile->close();
×
226
                sourceFiles.append(tmpFile);
×
227
        }
228
        else
229
        {
230
                qWarning() << "Satellites: could not save to file" << url;
×
231
        }
232
        
233
        if (numberDownloadsComplete >= sourceUrls.count())
×
234
        {
235
                if (progressBar)
×
236
                {
237
                        StelApp::getInstance().removeProgressBar(progressBar);
×
238
                        progressBar = nullptr;
×
239
                }
240
                
241
                if (sourceFiles.isEmpty())
×
242
                {
243
                        reset();
×
244
                        displayMessage(q_("No data could be downloaded. Try again later."));
×
245
                }
246
                else
247
                {
248
                        ui->pushButtonAbort->setVisible(false);
×
249
                        ui->groupBoxWorking->setTitle(q_("Processing data..."));
×
250
                        displayMessage(q_("Processing data..."));
×
251
                        populateList();
×
252
                }
253
        }
254
        
255
        networkReply->deleteLater();
×
256
}
×
257

258
void SatellitesImportDialog::abortDownloads()
×
259
{
260
        for (int i = 0; i < activeDownloads.count(); i++)
×
261
        {
262
                activeDownloads[i]->abort();
×
263
                activeDownloads[i]->deleteLater();
×
264
        }
265
        reset();
×
266
        displayMessage(q_("Download aborted."));
×
267
}
×
268

269
void SatellitesImportDialog::acceptNewSatellites()
×
270
{
271
        TleDataList satellitesToAdd;
×
272
        for (int row = 0; row < newSatellitesModel->rowCount(); row++)
×
273
        {
274
                QStandardItem* item = newSatellitesModel->item(row);
×
275
                if (item->checkState() == Qt::Checked)
×
276
                {
277
                        QString id = item->data(Qt::UserRole).toString();
×
278
                        satellitesToAdd.append(newSatellites.value(id));
×
279
                }
×
280
        }
281
        emit satellitesAccepted(satellitesToAdd);
×
282
        reset();
×
283
        close();
×
284
}
×
285

286
void SatellitesImportDialog::discardNewSatellites()
×
287
{
288
        reset();
×
289
        close();
×
290
}
×
291

292
void SatellitesImportDialog::markAll()
×
293
{
294
        setCheckState(Qt::Checked);
×
295
}
×
296

297
void SatellitesImportDialog::markNone()
×
298
{
299
        setCheckState(Qt::Unchecked);
×
300
}
×
301

302
void SatellitesImportDialog::reset()
×
303
{
304
        // Assuming that everything that needs to be stopped is stopped
305
        isGettingData = false;
×
306
        ui->stackedWidget->setCurrentIndex(0);
×
307
        ui->pushButtonGetData->setVisible(true);
×
308
        ui->pushButtonAbort->setVisible(false);
×
309
        ui->labelMessage->setVisible(false);
×
310
        ui->labelMessage->clear();
×
311
        ui->groupBoxWorking->setTitle(q_("Get data"));
×
312
        newSatellitesModel->clear();
×
313
        ui->lineEditSearch->clear();
×
314
        
315
        newSatellites.clear();
×
316
        sourceUrls.clear();
×
317
        
318
        qDeleteAll(activeDownloads);
×
319
        activeDownloads.clear();
×
320
        
321
        qDeleteAll(sourceFiles);
×
322
        sourceFiles.clear();
×
323
        
324
        numberDownloadsComplete = 0;
×
325
        if (progressBar)
×
326
        {
327
                StelApp::getInstance().removeProgressBar(progressBar);
×
328
                progressBar = nullptr;
×
329
        }
330
}
×
331

332
void SatellitesImportDialog::populateList()
×
333
{
334
        newSatellites.clear();
×
335
        newSatellitesModel->clear();
×
336
        Satellites* satMgr = GETSTELMODULE(Satellites);
×
337
        
338
        // Load ALL two-line element sets...
339
        for (int f = 0; f < sourceFiles.count(); f++)
×
340
        {
341
                bool open = false;
×
342
                QTemporaryFile* tempFile = dynamic_cast<QTemporaryFile*>(sourceFiles[f]);
×
343
                if (tempFile)
×
344
                        open = tempFile->open();
×
345
                else
346
                        open = sourceFiles[f]->open(QFile::ReadOnly);
×
347
                if (open)
×
348
                {
349
                        satMgr->parseTleFile(*sourceFiles[f], newSatellites);
×
350
                        sourceFiles[f]->close();
×
351
                }
352
                else
353
                {
354
                        qDebug() << "Satellites: cannot open file"
×
355
                                 << QDir::toNativeSeparators(sourceFiles[f]->fileName());
×
356
                }
357
        }
358
        // Clear the disk...
359
        qDeleteAll(sourceFiles);
×
360
        sourceFiles.clear();
×
361
        
362
        QStringList existingIDs = satMgr->listAllIds();
×
363
        QHashIterator<QString,TleData> i(newSatellites);
×
364
        while (i.hasNext())
×
365
        {
366
                i.next();
×
367
                
368
                // Skip duplicates
369
                if (existingIDs.contains(i.key()))
×
370
                        continue;
×
371
                
372
                TleData tle = i.value();                
×
373
                QStandardItem* newItem = new QStandardItem(QString("%1 (NORAD %2)").arg(tle.name, tle.id)); // Available to search via NORAD number
×
374
                newItem->setFlags(Qt::ItemIsEnabled | Qt::ItemIsUserCheckable);
×
375
                newItem->setCheckState(Qt::Unchecked);
×
376
                newItem->setData(tle.id, Qt::UserRole);                
×
377
                newItem->setToolTip(QString(q_("Catalog Number: %1")).arg(tle.id));
×
378
                newSatellitesModel->appendRow(newItem);
×
379
        }
×
380
        existingIDs.clear();
×
381
        newSatellitesModel->sort(0);
×
382
        ui->listView->scrollToTop();
×
383
        ui->stackedWidget->setCurrentIndex(1);
×
384
}
×
385

386
void SatellitesImportDialog::displayMessage(const QString& message)
×
387
{
388
        if (message.isEmpty())
×
389
                return;
×
390
        
391
        ui->labelMessage->setText(message);
×
392
        ui->labelMessage->setVisible(true);
×
393
}
394

395
void SatellitesImportDialog::setCheckState(Qt::CheckState state)
×
396
{
397
        Q_ASSERT(filterProxyModel);
×
398
        
399
        int rowCount = filterProxyModel->rowCount();
×
400
        if (rowCount < 1)
×
401
                return;
×
402

403
        for (int row = 0; row < rowCount; row++)
×
404
        {
405
                QModelIndex proxyIndex = filterProxyModel->index(row, 0);
×
406
                QModelIndex index = filterProxyModel->mapToSource(proxyIndex);
×
407
                QStandardItem * item = newSatellitesModel->itemFromIndex(index);
×
408
                if (item)
×
409
                {
410
                        item->setCheckState(state);
×
411
                }
412
        }
413
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc