2#include "opsastrometryindexfiles.h"
8#include "ksnotification.h"
10#include <KConfigDialog>
15OpsAstrometryIndexFiles::OpsAstrometryIndexFiles(Align *parent) :
QDialog(
KStars::Instance())
20 actualdownloadSpeed = downloadSpeed;
24 indexURL->setText(
"http://broiler.astrometry.net/~dstn/");
28 connect(openIndexFileDirectory, SIGNAL(clicked()),
this, SLOT(slotOpenIndexFileDirectory()));
35 astrometryIndex[2.8] =
"00";
36 astrometryIndex[4.0] =
"01";
37 astrometryIndex[5.6] =
"02";
38 astrometryIndex[8] =
"03";
39 astrometryIndex[11] =
"04";
40 astrometryIndex[16] =
"05";
41 astrometryIndex[22] =
"06";
42 astrometryIndex[30] =
"07";
43 astrometryIndex[42] =
"08";
44 astrometryIndex[60] =
"09";
45 astrometryIndex[85] =
"10";
46 astrometryIndex[120] =
"11";
47 astrometryIndex[170] =
"12";
48 astrometryIndex[240] =
"13";
49 astrometryIndex[340] =
"14";
50 astrometryIndex[480] =
"15";
51 astrometryIndex[680] =
"16";
52 astrometryIndex[1000] =
"17";
53 astrometryIndex[1400] =
"18";
54 astrometryIndex[2000] =
"19";
59 &OpsAstrometryIndexFiles::slotUpdate);
61 for (
auto &checkBox : checkboxes)
70 for (
auto &bar : progressBars)
72 if(bar->objectName().contains(
"progress"))
74 bar->setVisible(
false);
75 bar->setTextVisible(
false);
79 for (
auto &button : qButtons)
81 if(button->objectName().contains(
"cancel"))
83 button->setVisible(
false);
87 for (
QLabel * label : qLabels)
91 label->setVisible(
false);
103 addDirectoryToList(dir);
107 if(indexLocations->currentIndex() != 0)
108 removeDirectoryFromList(indexLocations->currentText());
114void OpsAstrometryIndexFiles::showEvent(
QShowEvent *)
116 updateIndexDirectoryList();
120void OpsAstrometryIndexFiles::updateIndexDirectoryList()
124 QStringList astrometryDataDirs = KSUtils::getAstrometryDataDirs();
126 indexLocations->clear();
127 if(astrometryDataDirs.
count() > 1)
128 indexLocations->addItem(
"All Sources");
129 indexLocations->addItems(astrometryDataDirs);
133void OpsAstrometryIndexFiles::addDirectoryToList(
QString directory)
140 QStringList indexFileDirs = Options::astrometryIndexFolderList();
141 if(indexFileDirs.
contains(directoryPath))
143 indexFileDirs.
append(directoryPath);
144 Options::setAstrometryIndexFolderList(indexFileDirs);
145 updateIndexDirectoryList();
148void OpsAstrometryIndexFiles::removeDirectoryFromList(
QString directory)
150 QStringList indexFileDirs = Options::astrometryIndexFolderList();
151 if(indexFileDirs.
contains(directory))
154 Options::setAstrometryIndexFolderList(indexFileDirs);
155 updateIndexDirectoryList();
159void OpsAstrometryIndexFiles::slotUpdate()
163 for (
auto &checkBox : checkboxes)
165 checkBox->setChecked(
false);
168 if(indexLocations->count() == 0)
171 double fov_w, fov_h, fov_pixscale;
174 alignModule->getFOVScale(fov_w, fov_h, fov_pixscale);
176 double fov_check = qMax(fov_w, fov_h);
182 QStringList astrometryDataDirs = Options::astrometryIndexFolderList();
184 bool allDirsSelected = (indexLocations->currentIndex() == 0 && astrometryDataDirs.
count() > 1);
185 bool folderIsWriteable;
191 folderDetails->setText(
i18n(
"Downloads Disabled, this is not a directory, it is a list of all index files."));
192 folderIsWriteable =
false;
193 astrometryDataDirsToIndex = astrometryDataDirs;
194 openIndexFileDirectory->setEnabled(
false);
198 QString folderPath = indexLocations->currentText();
200 if(folderIsWriteable)
201 folderDetails->setText(
i18n(
"Downloads Enabled, the directory exists and is writeable."));
203 folderDetails->setText(
i18n(
"Downloads Disabled, directory permissions issue."));
205 folderDetails->setText(
i18n(
"Downloads Disabled, directory does not exist."));
206 astrometryDataDirsToIndex << folderPath;
207 openIndexFileDirectory->setEnabled(
true);
209 folderDetails->setCursorPosition(0);
213 for(
auto &astrometryDataDir : astrometryDataDirsToIndex)
215 QDir directory(astrometryDataDir);
216 QStringList indexList = directory.entryList(nameFilter);
218 for (
auto &indexName : indexList)
220 if (fileCountMatches(directory, indexName))
223 QCheckBox *indexCheckBox = findChild<QCheckBox *>(indexName);
230 for (
auto &checkBox : checkboxes)
232 checkBox->setEnabled(folderIsWriteable);
233 checkBox->setIcon(
QIcon(
":/icons/astrometry-optional.svg"));
234 checkBox->setToolTip(
i18n(
"Optional"));
235 checkBox->setStyleSheet(
"");
238 float last_skymarksize = 2;
240 for (
auto &skymarksize : astrometryIndex.keys())
242 QString indexName1 =
"index_41" + astrometryIndex.value(skymarksize);
243 QString indexName2 =
"index_42" + astrometryIndex.value(skymarksize);
244 QString indexName3 =
"index_52" + astrometryIndex.value(skymarksize);
245 QCheckBox *indexCheckBox1 = findChild<QCheckBox *>(indexName1);
246 QCheckBox *indexCheckBox2 = findChild<QCheckBox *>(indexName2);
247 QCheckBox *indexCheckBox3 = findChild<QCheckBox *>(indexName3);
248 if ((skymarksize >= 0.40 * fov_check && skymarksize <= 0.9 * fov_check) ||
249 (fov_check > last_skymarksize && fov_check < skymarksize))
253 indexCheckBox1->
setIcon(
QIcon(
":/icons/astrometry-required.svg"));
258 indexCheckBox2->
setIcon(
QIcon(
":/icons/astrometry-required.svg"));
263 indexCheckBox3->
setIcon(
QIcon(
":/icons/astrometry-required.svg"));
267 else if (skymarksize >= 0.10 * fov_check && skymarksize <= fov_check)
271 indexCheckBox1->
setIcon(
QIcon(
":/icons/astrometry-recommended.svg"));
276 indexCheckBox2->
setIcon(
QIcon(
":/icons/astrometry-recommended.svg"));
281 indexCheckBox3->
setIcon(
QIcon(
":/icons/astrometry-recommended.svg"));
286 last_skymarksize = skymarksize;
292 for(
QString astrometryDataDir : astrometryDataDirs)
294 QDir directory(astrometryDataDir);
295 QStringList indexList = directory.entryList(nameFilter);
297 for (
auto &indexName : indexList)
299 if (fileCountMatches(directory, indexName))
302 QCheckBox *indexCheckBox = findChild<QCheckBox *>(indexName);
304 indexCheckBox->
setStyleSheet(
"QCheckBox{font-weight: bold; color:green}");
310int OpsAstrometryIndexFiles::indexFileCount(
QString indexName)
325bool OpsAstrometryIndexFiles::fileCountMatches(
QDir directory,
QString indexName)
327 QString indexNameMatch = indexName.
left(10) +
"*.fits";
329 return list.
count() == indexFileCount(indexName);
332void OpsAstrometryIndexFiles::slotOpenIndexFileDirectory()
334 if(indexLocations->count() == 0)
340bool OpsAstrometryIndexFiles::astrometryIndicesAreAvailable()
342 QUrl indexUrl =
QUrl(this->indexURL->text());
345 timeout.setInterval(5000);
346 timeout.setSingleShot(
true);
350 if (!timeout.isActive())
355 qApp->processEvents();
362 return wasSuccessful;
366 int currentIndex,
int maxIndex,
double fileSize)
369 downloadTime.
start();
372 if (currentIndex < 10)
373 indexString =
'0' + indexString;
375 QString indexSeriesName = checkBox->
text().remove(
'&');
376 QProgressBar *indexDownloadProgress = findChild<QProgressBar *>(indexSeriesName.
replace(
'-',
'_').
left(10) +
"_progress");
377 QLabel *indexDownloadInfo = findChild<QLabel *>(indexSeriesName.
replace(
'-',
'_').
left(10) +
"_info");
378 QPushButton *indexDownloadCancel = findChild<QPushButton *>(indexSeriesName.
replace(
'-',
'_').
left(10) +
"_cancel");
379 QLabel *indexDownloadPerc = findChild<QLabel *>(indexSeriesName.
replace(
'-',
'_').
left(10) +
"_perc");
381 setDownloadInfoVisible(indexSeriesName, checkBox,
true);
383 if(indexDownloadInfo)
385 if (indexDownloadProgress && maxIndex > 0)
386 indexDownloadProgress->
setValue(currentIndex * 100 / maxIndex);
392 indexURL.
replace(
'*', indexString);
401 if(fileSize > 4000000)
402 timeout = fileSize / downloadSpeed;
409 if(indexDownloadPerc)
412 [ = ](qint64 bytesReceived, qint64 bytesTotal)
414 if (indexDownloadProgress)
416 indexDownloadProgress->
setValue(bytesReceived);
417 indexDownloadProgress->
setMaximum(bytesTotal);
424 timeoutTimer.disconnect();
427 KSNotification::error(
428 i18n(
"Download Timed out. Either the network is not fast enough, the file is not accessible, or you are not connected."));
429 disconnectDownload(cancelConnection, replyConnection, percentConnection);
435 setDownloadInfoVisible(indexSeriesName, checkBox,
false);
437 timeoutTimer.start(timeout);
442 qDebug() << Q_FUNC_INFO <<
"Download Cancelled.";
444 disconnectDownload(cancelConnection, replyConnection, percentConnection);
450 setDownloadInfoVisible(indexSeriesName, checkBox,
false);
459 disconnectDownload(cancelConnection, replyConnection, percentConnection);
460 setDownloadInfoVisible(indexSeriesName, checkBox,
false);
468 indexFileN.
replace(
'*', indexString);
470 QFile file(indexFileN);
475 KSNotification::error(
i18n(
"File Write Error"));
481 file.write(responseData.
data(), responseData.
size());
484 int dtime = downloadTime.
elapsed();
485 actualdownloadSpeed = (actualdownloadSpeed + (downloadedFileSize / dtime)) / 2;
486 qDebug() << Q_FUNC_INFO <<
"Filesize: " << downloadedFileSize <<
", time: " << dtime <<
", inst speed: " <<
487 downloadedFileSize / dtime <<
488 ", averaged speed: " << actualdownloadSpeed;
494 KSNotification::error(
i18n(
"Astrometry Folder Permissions Error"));
497 if (currentIndex == maxIndex)
502 downloadIndexFile(URL, fileN, checkBox, currentIndex + 1, maxIndex, fileSize);
507void OpsAstrometryIndexFiles::setDownloadInfoVisible(
QString indexSeriesName,
QCheckBox *checkBox,
bool set)
511 QProgressBar *indexDownloadProgress = findChild<QProgressBar *>(indexSeriesName.
replace(
'-',
'_').
left(10) +
"_progress");
512 QLabel *indexDownloadInfo = findChild<QLabel *>(indexSeriesName.
replace(
'-',
'_').
left(10) +
"_info");
513 QPushButton *indexDownloadCancel = findChild<QPushButton *>(indexSeriesName.
replace(
'-',
'_').
left(10) +
"_cancel");
514 QLabel *indexDownloadPerc = findChild<QLabel *>(indexSeriesName.
replace(
'-',
'_').
left(10) +
"_perc");
515 if (indexDownloadProgress)
517 if (indexDownloadInfo)
519 if (indexDownloadCancel)
521 if (indexDownloadPerc)
528 disconnect(*cancelConnection);
530 disconnect(*replyConnection);
531 if(percentConnection)
532 disconnect(*percentConnection);
535void OpsAstrometryIndexFiles::downloadOrDeleteIndexFiles(
bool checked)
539 if (indexLocations->count() == 0)
542 QString astrometryDataDir = indexLocations->currentText();
545 KSNotification::sorry(
546 i18n(
"The selected Index File directory does not exist. Please either create it or choose another."));
552 QString indexSeriesName = checkBox->
text().remove(
'&');
553 QString filePath = astrometryDataDir +
'/' + indexSeriesName;
554 QString fileNumString = indexSeriesName.
mid(8, 2);
561 nullptr,
i18n(
"The file %1 already exists in another directory. Are you sure you want to download it to this directory as well?",
571 if (astrometryIndicesAreAvailable())
576 if (this->indexURL->text().
endsWith(
"/"))
578 BASE_URL = this->indexURL->text();
582 BASE_URL = this->indexURL->text() +
"/";
586 URL = BASE_URL +
"4100/" + indexSeriesName;
587 else if (indexSeriesName.startsWith(
QLatin1String(
"index-42")))
588 URL = BASE_URL +
"4200/" + indexSeriesName;
589 else if (indexSeriesName.startsWith(
QLatin1String(
"index-52")))
590 URL =
"https://portal.nersc.gov/project/cosmo/temp/dstn/index-5200/LITE/" + indexSeriesName;
592 int maxIndex = indexFileCount(indexSeriesName) - 1;
594 double fileSize = 1E11 * qPow(astrometryIndex.key(fileNumString),
597 fileSize /= maxIndex;
598 downloadIndexFile(URL, filePath, checkBox, 0, maxIndex, fileSize);
602 KSNotification::sorry(
i18n(
"Could not contact Astrometry Index Server."));
608 nullptr,
i18n(
"Are you sure you want to delete these index files? %1", indexSeriesName),
612 if (
QFileInfo(astrometryDataDir).isWritable())
615 QDir directory(astrometryDataDir);
617 for (
auto &fileName : indexList)
619 if (fileName.contains(indexSeriesName.left(10)))
621 if (!directory.
remove(fileName))
623 KSNotification::error(
i18n(
"File Delete Error"));
633 KSNotification::error(
i18n(
"Astrometry Folder Permissions Error"));
This is the main window for KStars.
QString i18n(const char *text, const TYPE &arg...)
Ekos is an advanced Astrophotography tool for Linux.
QString path(const QString &relativePath)
ButtonCode warningContinueCancel(QWidget *parent, const QString &text, const QString &title=QString(), const KGuiItem &buttonContinue=KStandardGuiItem::cont(), const KGuiItem &buttonCancel=KStandardGuiItem::cancel(), const QString &dontAskAgainName=QString(), Options options=Notify)
KIOCORE_EXPORT QString dir(const QString &fileClass)
KIOCORE_EXPORT QStringList list(const QString &fileClass)
QString label(StandardShortcut id)
qsizetype size() const const
void currentIndexChanged(int index)
bool openUrl(const QUrl &url)
QStringList entryList(Filters filters, SortFlags sort) const const
bool remove(const QString &fileName)
qint64 elapsed() const const
QString getExistingDirectory(QWidget *parent, const QString &caption, const QString &dir, Options options)
bool exists() const const
bool isWritable() const const
qint64 size() const const
void setText(const QString &)
void textChanged(const QString &text)
void append(QList< T > &&value)
qsizetype count() const const
bool removeOne(const AT &t)
void downloadProgress(qint64 bytesReceived, qint64 bytesTotal)
NetworkError error() const const
bool isFinished() const const
QObject * sender() const const
void setMaximum(int maximum)
bool contains(QChar ch, Qt::CaseSensitivity cs) const const
bool endsWith(QChar c, Qt::CaseSensitivity cs) const const
bool isEmpty() const const
QString left(qsizetype n) const const
QString mid(qsizetype position, qsizetype n) const const
QString number(double n, char format, int precision)
QString & replace(QChar before, QChar after, Qt::CaseSensitivity cs)
bool contains(QLatin1StringView str, Qt::CaseSensitivity cs) const const
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
QUrl fromLocalFile(const QString &localFile)
QString url(FormattingOptions options) const const