File indexing completed on 2024-03-24 15:14:55

0001 /*
0002     SPDX-FileCopyrightText: 2007 James B. Bowlin <bowlin@mindspring.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "ksfilereader.h"
0008 
0009 #ifndef KSTARS_LITE
0010 #include "kstars.h"
0011 #endif
0012 #include "kstarsdata.h"
0013 #include "ksutils.h"
0014 
0015 #include <QApplication>
0016 #include <QDebug>
0017 #include <QFile>
0018 
0019 KSFileReader::KSFileReader(qint64 maxLen) : QTextStream(), m_maxLen(maxLen)
0020 {
0021 }
0022 
0023 KSFileReader::KSFileReader(QFile &file, qint64 maxLen)
0024     : QTextStream(), m_maxLen(maxLen)
0025 {
0026     QIODevice *device = (QIODevice *)&file;
0027     QTextStream::setDevice(device);
0028     QTextStream::setCodec("UTF-8");
0029 }
0030 
0031 bool KSFileReader::open(const QString &fname)
0032 {
0033     if (!KSUtils::openDataFile(m_file, fname))
0034     {
0035         qWarning() << QString("Couldn't open(%1)").arg(fname);
0036         return false;
0037     }
0038     QTextStream::setDevice(&m_file);
0039     QTextStream::setCodec("UTF-8");
0040     return true;
0041 }
0042 
0043 bool KSFileReader::openFullPath(const QString &fname)
0044 {
0045     if (!fname.isNull())
0046     {
0047         m_file.setFileName(fname);
0048         if (!m_file.open(QIODevice::ReadOnly))
0049             return false;
0050     }
0051     QTextStream::setDevice(&m_file);
0052     QTextStream::setCodec("UTF-8");
0053     return true;
0054 }
0055 
0056 void KSFileReader::setProgress(QString label, unsigned int totalLines, unsigned int numUpdates)
0057 {
0058     m_label      = label;
0059     m_totalLines = totalLines;
0060     if (m_totalLines < 1)
0061         m_totalLines = 1;
0062     m_targetLine      = m_totalLines / 100;
0063     m_targetIncrement = m_totalLines / numUpdates;
0064 
0065     connect(this, SIGNAL(progressText(QString)), KStarsData::Instance(), SIGNAL(progressText(QString)));
0066 }
0067 
0068 void KSFileReader::showProgress()
0069 {
0070     if (m_curLine < m_targetLine)
0071         return;
0072     if (m_targetLine < m_targetIncrement)
0073         m_targetLine = m_targetIncrement;
0074     else
0075         m_targetLine += m_targetIncrement;
0076 
0077     int percent = int(.5 + (m_curLine * 100.0) / m_totalLines);
0078     //printf("%8d %8d %3d\n", m_curLine, m_totalLines, percent );
0079     if (percent > 100)
0080         percent = 100;
0081     emit progressText(QString("%1 (%2%)").arg(m_label).arg(percent));
0082     //#ifdef ANDROID
0083     // Can cause crashes on Android
0084     qApp->processEvents();
0085     //#endif
0086 }