LibKEduVocDocument

keduvoccsvwriter.cpp
1/*
2 * export a KEduVocDocument to a delimited text file
3 * SPDX-FileCopyrightText: 1999-2001 Ewald Arnold <kvoctrain@ewald-arnold.de>
4 * SPDX-FileCopyrightText: 2007 Peter Hedlund <peter.hedlund@kdemail.net>
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 */
7
8#include "keduvoccsvwriter.h"
9
10#include <KLocalizedString>
11#include <QFile>
12#include <QTextStream>
13
14#include "keduvocdocument.h"
15#include "keduvocexpression.h"
16#include "keduvoclesson.h"
17
18KEduVocCsvWriter::KEduVocCsvWriter(QFile *file)
19 : m_outputFile(file) // the file must be already open
20 , m_doc(nullptr)
21{
22}
23
24bool KEduVocCsvWriter::writeDoc(KEduVocDocument *doc, const QString &generator)
25{
26 Q_UNUSED(generator);
27
28 m_doc = doc;
29
30 QString separator = m_doc->csvDelimiter();
31
32 QTextStream outputStream;
33 outputStream.setDevice(m_outputFile);
34
35 outputStream << i18nc("@item:intable the title of the document will be written here", "Title:") << separator << m_doc->title() << "\n";
36 outputStream << i18nc("@item:intable the author will be written here", "Author:") << separator << m_doc->author() << "\n";
37
38 KEduVocExpression *expression;
39 int idCount = m_doc->identifierCount();
40 QString currentRow;
41
42 for (int e = 0; e < m_doc->lesson()->entryCount(KEduVocLesson::Recursive); e++) {
43 expression = m_doc->lesson()->entries(KEduVocLesson::Recursive).value(e);
44 currentRow = QLatin1String("");
45 bool sep = false;
46
47 for (int i = 0; i < idCount; i++) {
48 if (!sep)
49 sep = true;
50 else
51 currentRow += separator;
52
53 currentRow += expression->translation(i)->text();
54 }
55
56 if (!currentRow.isEmpty())
57 outputStream << currentRow << "\n";
58 }
59
60 return true;
61}
The primary entry point to the hierarchy of objects describing vocabularies.
QString csvDelimiter() const
Returns the delimiter (separator) used for csv import and export.
KEduVocLesson * lesson() const
return the lesson
KEduVocTranslation * translation(int index)
Get a pointer to the translation.
QList< KEduVocExpression * > entries(EnumEntriesRecursive recursive=NotRecursive) override
get a list of all entries in the lesson
QString text() const
The translation as string (the word itself)
QString i18nc(const char *context, const char *text, const TYPE &arg...)
T value(qsizetype i) const const
bool isEmpty() const const
void setDevice(QIODevice *device)
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri Feb 21 2025 11:49:27 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.