Kstars

ksfilereader.cpp
1/*
2 SPDX-FileCopyrightText: 2007 James B. Bowlin <bowlin@mindspring.com>
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
20{
21}
22
24 : QTextStream(), m_maxLen(maxLen)
25{
26 QIODevice *device = (QIODevice *)&file;
28 QTextStream::setCodec("UTF-8");
29}
30
32{
33 if (!KSUtils::openDataFile(m_file, fname))
34 {
35 qWarning() << QString("Couldn't open(%1)").arg(fname);
36 return false;
37 }
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 }
52 QTextStream::setCodec("UTF-8");
53 return true;
54}
55
56void 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 openFullPath(const QString &fname)
opens the file with full path fname and uses that file for the QTextStream.
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).
KSFileReader(qint64 maxLen=1024)
this is the preferred constructor.
bool open(const QString &fname)
opens the file fname from the QStandardPaths::AppLocalDataLocation directory and uses that file for t...
void showProgress()
emits progress reports when required and updates bookkeeping for when to send the next report.
bool open(FILE *fh, OpenMode mode, FileHandleFlags handleFlags)
void setFileName(const QString &name)
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QString arg(Args &&... args) const const
QIODevice * device() const const
void setDevice(QIODevice *device)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:19:01 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.