Kstars

ksfilereader.cpp
1 /*
2  SPDX-FileCopyrightText: 2007 James B. Bowlin <[email protected]>
3 
4  SPDX-License-Identifier: GPL-2.0-or-later
5 */
6 
7 #include "ksfilereader.h"
8 
9 #ifndef KSTARS_LITE
10 #include "kstars.h"
11 #endif
12 #include "kstarsdata.h"
13 #include "ksutils.h"
14 
15 #include <QApplication>
16 #include <QDebug>
17 #include <QFile>
18 
19 KSFileReader::KSFileReader(qint64 maxLen) : QTextStream(), m_maxLen(maxLen)
20 {
21 }
22 
23 KSFileReader::KSFileReader(QFile &file, qint64 maxLen)
24  : QTextStream(), m_maxLen(maxLen)
25 {
26  QIODevice *device = (QIODevice *)&file;
28  QTextStream::setCodec("UTF-8");
29 }
30 
31 bool KSFileReader::open(const QString &fname)
32 {
33  if (!KSUtils::openDataFile(m_file, fname))
34  {
35  qWarning() << QString("Couldn't open(%1)").arg(fname);
36  return false;
37  }
38  QTextStream::setDevice(&m_file);
39  QTextStream::setCodec("UTF-8");
40  return true;
41 }
42 
44 {
45  if (!fname.isNull())
46  {
47  m_file.setFileName(fname);
48  if (!m_file.open(QIODevice::ReadOnly))
49  return false;
50  }
51  QTextStream::setDevice(&m_file);
52  QTextStream::setCodec("UTF-8");
53  return true;
54 }
55 
56 void KSFileReader::setProgress(QString label, unsigned int totalLines, unsigned int numUpdates)
57 {
58  m_label = label;
59  m_totalLines = totalLines;
60  if (m_totalLines < 1)
61  m_totalLines = 1;
62  m_targetLine = m_totalLines / 100;
63  m_targetIncrement = m_totalLines / numUpdates;
64 
65  connect(this, SIGNAL(progressText(QString)), KStarsData::Instance(), SIGNAL(progressText(QString)));
66 }
67 
69 {
70  if (m_curLine < m_targetLine)
71  return;
72  if (m_targetLine < m_targetIncrement)
73  m_targetLine = m_targetIncrement;
74  else
75  m_targetLine += m_targetIncrement;
76 
77  int percent = int(.5 + (m_curLine * 100.0) / m_totalLines);
78  //printf("%8d %8d %3d\n", m_curLine, m_totalLines, percent );
79  if (percent > 100)
80  percent = 100;
81  emit progressText(QString("%1 (%2%)").arg(m_label).arg(percent));
82  //#ifdef ANDROID
83  // Can cause crashes on Android
84  qApp->processEvents();
85  //#endif
86 }
bool isNull() const const
bool openFullPath(const QString &fname)
opens the file with full path fname and uses that file for the QTextStream.
virtual bool open(QIODevice::OpenMode mode) override
bool open(const QString &fname)
opens the file fname from the QStandardPaths::AppLocalDataLocation directory and uses that file for t...
KSFileReader(qint64 maxLen=1024)
this is the preferred constructor.
QIODevice * device() const const
void setProgress(QString label, unsigned int lastLine, unsigned int numUpdates=10)
Prepares this instance to emit progress reports on how much of the file has been read (in percent).
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
void setDevice(QIODevice *device)
void setFileName(const QString &name)
void showProgress()
emits progress reports when required and updates bookkeeping for when to send the next report.
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
void setCodec(QTextCodec *codec)
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Fri Aug 12 2022 04:00:54 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.