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 <KLocalizedString>
16
17#include <QApplication>
18#include <QDebug>
19#include <QFile>
20
21KSFileReader::KSFileReader(qint64 maxLen) : QTextStream(), m_maxLen(maxLen)
22{
23}
24
25KSFileReader::KSFileReader(QFile &file, qint64 maxLen)
26 : QTextStream(), m_maxLen(maxLen)
27{
28 QIODevice *device = (QIODevice *)&file;
30
31 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
33 #else
34 QTextStream::setCodec("UTF-8");
35 #endif
36
37
38}
39
40bool KSFileReader::open(const QString &fname)
41{
42 if (!KSUtils::openDataFile(m_file, fname))
43 {
44 qWarning() << QString("Couldn't open(%1)").arg(fname);
45 return false;
46 }
48 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
50 #else
51 QTextStream::setCodec("UTF-8");
52 #endif
53 return true;
54}
55
57{
58 if (!fname.isNull())
59 {
60 m_file.setFileName(fname);
61 if (!m_file.open(QIODevice::ReadOnly))
62 return false;
63 }
65 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
67 #else
68 QTextStream::setCodec("UTF-8");
69 #endif
70 return true;
71}
72
73void KSFileReader::setProgress(QString label, unsigned int totalLines, unsigned int numUpdates)
74{
75 m_label = label;
76 m_totalLines = totalLines;
77 if (m_totalLines < 1)
78 m_totalLines = 1;
79 m_targetLine = m_totalLines / 100;
80 m_targetIncrement = m_totalLines / numUpdates;
81
82 connect(this, SIGNAL(progressText(QString)), KStarsData::Instance(), SIGNAL(progressText(QString)));
83}
84
86{
87 if (m_curLine < m_targetLine)
88 return;
89 if (m_targetLine < m_targetIncrement)
90 m_targetLine = m_targetIncrement;
91 else
92 m_targetLine += m_targetIncrement;
93
94 int percent = int(.5 + (m_curLine * 100.0) / m_totalLines);
95 //printf("%8d %8d %3d\n", m_curLine, m_totalLines, percent );
96 if (percent > 100)
97 percent = 100;
98 emit progressText(i18nc("%2 is the value, % is the percent sign", "%1 (%2%)", m_label, percent));
99 //#ifdef ANDROID
100 // Can cause crashes on Android
101 qApp->processEvents();
102 //#endif
103}
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.
QString i18nc(const char *context, const char *text, const TYPE &arg...)
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
bool isNull() const const
QIODevice * device() const const
void setDevice(QIODevice *device)
void setEncoding(QStringConverter::Encoding encoding)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Oct 11 2024 12:15:10 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.