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 QTextStream::setCodec("UTF-8");
31}
32
33bool KSFileReader::open(const QString &fname)
34{
35 if (!KSUtils::openDataFile(m_file, fname))
36 {
37 qWarning() << QString("Couldn't open(%1)").arg(fname);
38 return false;
39 }
41 QTextStream::setCodec("UTF-8");
42 return true;
43}
44
46{
47 if (!fname.isNull())
48 {
49 m_file.setFileName(fname);
50 if (!m_file.open(QIODevice::ReadOnly))
51 return false;
52 }
54 QTextStream::setCodec("UTF-8");
55 return true;
56}
57
58void KSFileReader::setProgress(QString label, unsigned int totalLines, unsigned int numUpdates)
59{
60 m_label = label;
61 m_totalLines = totalLines;
62 if (m_totalLines < 1)
63 m_totalLines = 1;
64 m_targetLine = m_totalLines / 100;
65 m_targetIncrement = m_totalLines / numUpdates;
66
67 connect(this, SIGNAL(progressText(QString)), KStarsData::Instance(), SIGNAL(progressText(QString)));
68}
69
71{
72 if (m_curLine < m_targetLine)
73 return;
74 if (m_targetLine < m_targetIncrement)
75 m_targetLine = m_targetIncrement;
76 else
77 m_targetLine += m_targetIncrement;
78
79 int percent = int(.5 + (m_curLine * 100.0) / m_totalLines);
80 //printf("%8d %8d %3d\n", m_curLine, m_totalLines, percent );
81 if (percent > 100)
82 percent = 100;
83 emit progressText(i18nc("%2 is the value, % is the percent sign", "%1 (%2%)", m_label, percent));
84 //#ifdef ANDROID
85 // Can cause crashes on Android
86 qApp->processEvents();
87 //#endif
88}
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)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Jul 26 2024 11:59:50 by doxygen 1.11.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.