Skip to content

Commit

Permalink
Introduced Ts=>Xlsx conversion
Browse files Browse the repository at this point in the history
Signed-off-by: Federico Guerinoni <[email protected]>
  • Loading branch information
guerinoni committed Feb 24, 2020
1 parent 6f26079 commit 8a5848a
Show file tree
Hide file tree
Showing 13 changed files with 120 additions and 6 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[submodule "3rd-party/qtcsv"]
path = 3rd-party/qtcsv
url = https://github.com/iamantony/qtcsv.git
[submodule "3rd-party/qtxlsx"]
path = 3rd-party/qtxlsx
url = https://github.com/VSRonin/QtXlsxWriter.git
1 change: 1 addition & 0 deletions 3rd-party/qtxlsx
Submodule qtxlsx added at d3bd83
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ cmake_minimum_required(VERSION 3.10)
project(qt-ts-csv LANGUAGES CXX VERSION "3.1.0")

add_subdirectory(3rd-party/qtcsv)
add_subdirectory(3rd-party/qtxlsx)
add_subdirectory(src)
add_subdirectory(tests)
5 changes: 4 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)

include_directories(${PROJECT_SOURCE_DIR}/3rd-party/qtcsv)
include_directories(${PROJECT_SOURCE_DIR}/3rd-party/qtxlsx/src)
find_package(Qt5 COMPONENTS Core Gui Quick Xml REQUIRED)

add_executable(${CMAKE_PROJECT_NAME}
Expand All @@ -14,12 +15,14 @@ add_executable(${CMAKE_PROJECT_NAME}
CsvBuilder.cpp CsvBuilder.hpp
CsvParser.cpp CsvParser.hpp
Ts2CsvConverter.cpp Ts2CsvConverter.hpp
Ts2XlsxConverter.cpp Ts2XlsxConverter.hpp
TsBuilder.cpp TsBuilder.hpp
TsParser.cpp TsParser.hpp
XlsxBuilder.cpp XlsxBuilder.hpp
main.cpp
qml.qrc)

target_link_libraries(${CMAKE_PROJECT_NAME} PRIVATE Qt5::Core Qt5::Gui Qt5::Quick Qt5::Xml
PUBLIC qtcsv)
PUBLIC qtcsv QtXlsxWriter)

add_definitions(-DVERSION=\"${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}\")
6 changes: 6 additions & 0 deletions src/ConverterFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "Csv2TsConverter.hpp"
#include "Ts2CsvConverter.hpp"
#include "Ts2XlsxConverter.hpp"

#include <cassert>

Expand All @@ -22,6 +23,11 @@ ConverterFactory::make_converter(ConverterFactory::ConversionType type,
string_sep);
break;

case ConversionType::Ts2Xlsx:
return std::make_unique<Ts2XlsxConverter>(in, out, field_sep,
string_sep);
break;

default:
break;
}
Expand Down
2 changes: 1 addition & 1 deletion src/ConverterFactory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
class ConverterFactory
{
public:
enum ConversionType { Ts2Csv, Csv2Ts, Dummy };
enum ConversionType { Ts2Csv, Csv2Ts, Ts2Xlsx, Dummy };
static std::unique_ptr<Converter>
make_converter(ConversionType type, const std::string &in,
const std::string &out, const std::string &field_sep,
Expand Down
5 changes: 3 additions & 2 deletions src/ConverterGuiProxy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ class ConverterGuiProxy : public QObject
explicit ConverterGuiProxy(QObject *parent = nullptr);

enum QConversionType {
Ts2Csv = ConverterFactory::Ts2Csv,
Csv2Ts = ConverterFactory::Csv2Ts,
Ts2Csv = ConverterFactory::Ts2Csv,
Csv2Ts = ConverterFactory::Csv2Ts,
Ts2Xlsx = ConverterFactory::Ts2Xlsx,
Dummy
};
Q_ENUM(QConversionType)
Expand Down
22 changes: 22 additions & 0 deletions src/Ts2XlsxConverter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include "Ts2XlsxConverter.hpp"

#include "XlsxBuilder.hpp"

Ts2XlsxConverter::Ts2XlsxConverter(const std::string &input,
const std::string &output,
const std::string &field_sep,
const std::string &string_sep) :
Converter{ input, output }
{
m_builder = std::make_shared<XlsxBuilder>();
}

void Ts2XlsxConverter::process() const
{
if (m_inputFile.empty() || m_outputDir.empty()) {
return;
}

auto trs = m_parser.parse(m_inputFile);
m_builder->build(m_outputDir + "/output.xlsx", trs);
}
23 changes: 23 additions & 0 deletions src/Ts2XlsxConverter.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#pragma once

#include "Converter.hpp"
#include "TsParser.hpp"

#include <memory>

class XlsxBuilder;

class Ts2XlsxConverter : public Converter
{
public:
explicit Ts2XlsxConverter(const std::string &input,
const std::string &output,
const std::string &field_sep,
const std::string &string_sep);

void process() const override;

private:
TsParser m_parser;
std::shared_ptr<XlsxBuilder> m_builder;
};
38 changes: 38 additions & 0 deletions src/XlsxBuilder.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include "XlsxBuilder.hpp"

#include <QFile>
#include <QTextStream>
#include <QtDebug>
#include <xlsx/xlsxdocument.h>

void XlsxBuilder::build(const std::string &output_filename,
Translations trs) const
{
QXlsx::Document xlsx;
xlsx.write(1, 1, "Context");
xlsx.write(1, 2, "Source");
xlsx.write(1, 3, "Translation");
xlsx.write(1, 4, "Location");

int row{ 2 };
int col{ 1 };
for (const auto &tr : trs) {
for (const auto &msg : tr.messages) {
xlsx.write(row, col++, tr.name);
xlsx.write(row, col++, msg.source);
xlsx.write(row, col++, msg.translation);

for (const auto &loc : msg.locations) {
xlsx.write(
row, col++,
QString(loc.first + " - " + QString::number(loc.second)));
}
++row;
col = 1;
}
}

if (!xlsx.saveAs(output_filename.c_str())) {
qWarning() << "error writing file";
}
}
9 changes: 9 additions & 0 deletions src/XlsxBuilder.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#pragma once

#include "TranslationObject.hpp"

class XlsxBuilder
{
public:
void build(const std::string &output_filename, Translations trs) const;
};
8 changes: 7 additions & 1 deletion src/main.qml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ Window {
visible: ApplicationWindow.Windowed

property bool choosingFile: true
readonly property bool isCsvFormat: comboType.currentIndex === 0
|| comboType.currentIndex === 1

GridLayout {
anchors {
Expand Down Expand Up @@ -73,14 +75,16 @@ Window {

ComboBox {
id: comboType
model: ["TS => CSV", "CSV => TS"]
model: ["TS => CSV", "CSV => TS", "TS => XLSX"]
}

Text {
text: qsTr("Field separator:")
visible: isCsvFormat
}

Rectangle {
visible: isCsvFormat
border.width: 0.5
border.color: "black"
color: "transparent"
Expand All @@ -96,9 +100,11 @@ Window {

Text {
text: qsTr("String separator:")
visible: isCsvFormat
}

Rectangle {
visible: isCsvFormat
border.width: 0.5
border.color: "black"
color: "transparent"
Expand Down
3 changes: 2 additions & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)

include_directories(${PROJECT_SOURCE_DIR}/3rd-party/qtcsv)
include_directories(${PROJECT_SOURCE_DIR}/3rd-party/qtxlsx/src)
find_package(Qt5 COMPONENTS Core Gui Quick Xml Test REQUIRED)

add_executable(tests
Expand All @@ -20,5 +21,5 @@ add_executable(tests
main.cpp)

target_link_libraries(tests PRIVATE Qt5::Core Qt5::Xml Qt5::Test
PUBLIC qtcsv)
PUBLIC qtcsv QtXlsxWriter)
add_definitions(-DFILESPATH=\"${CMAKE_CURRENT_SOURCE_DIR}/files\")

0 comments on commit 8a5848a

Please sign in to comment.