Skip to content

Commit

Permalink
New feature: Export as localized text
Browse files Browse the repository at this point in the history
  • Loading branch information
Isarhamster committed Jun 4, 2024
1 parent b92de92 commit d41e886
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 11 deletions.
19 changes: 11 additions & 8 deletions src/database/output.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ Output::Output(OutputType output, BoardRenderingFunc renderer, const QString& pa
case Html:
m_options.createDefaultOptions("");
break;
case LocalPgn:
case Pgn:
m_options.createDefaultOptions("");
break;
Expand All @@ -66,7 +67,7 @@ Output::~Output()

void Output::initialize()
{
if(m_outputType == Pgn)
if(isPgnType(m_outputType))
{
m_newlineChar = "\n";
}
Expand Down Expand Up @@ -233,9 +234,10 @@ QMap<Output::OutputType, QString>& Output::getFormats()
{
m_outputMap.clear();
m_outputMap[Html] = "Html Output";
m_outputMap[Pgn] = "Pgn Output";
m_outputMap[Pgn] = "PGN Output";
m_outputMap[Latex] = "Latex Output";
m_outputMap[NotationWidget] = "Notation Widget Output";
m_outputMap[LocalPgn] = "Local PGN Output";
return m_outputMap;
}

Expand Down Expand Up @@ -299,10 +301,10 @@ QString Output::writeMove(MoveToWrite moveToWrite)
}
// Read comments
if(m_game.canHaveStartAnnotation(moveId))
precommentString = (m_outputType == Pgn) ? m_game.annotation(moveId, GameX::BeforeMove) :
precommentString = isPgnType(m_outputType) ? m_game.annotation(moveId, GameX::BeforeMove) :
m_game.textAnnotation(moveId, GameX::BeforeMove, m_game.textFilter2());

QString commentString = (m_outputType == Pgn) ? m_game.annotation(moveId) :
QString commentString = isPgnType(m_outputType) ? m_game.annotation(moveId) :
m_game.textAnnotation(moveId, GameX::AfterMove, m_game.textFilter2());

// Write precomment if any
Expand All @@ -325,7 +327,7 @@ QString Output::writeMove(MoveToWrite moveToWrite)

// *** Determine actual san
QString san;
GameX::MoveStringFlags flags = (m_outputType == NotationWidget) ? GameX::TranslatePiece : GameX::MoveOnly;
GameX::MoveStringFlags flags = isLocalized(m_outputType) ? GameX::TranslatePiece : GameX::MoveOnly;
if(moveToWrite == NextMove)
{
san = m_game.moveToSan(flags);
Expand Down Expand Up @@ -446,7 +448,7 @@ QString Output::writeMove(MoveToWrite moveToWrite)
if (imageString.isEmpty() && commentString.isEmpty() && !san.isEmpty())
{
if((!((m_options.getOptionAsBool("ColumnStyle")) &&
(m_currentVariationLevel == 0))) || (m_outputType == Pgn))
(m_currentVariationLevel == 0))) || isPgnType(m_outputType))
{
if (m_game.hasNextMove())
{
Expand Down Expand Up @@ -803,7 +805,7 @@ QString Output::outputGame(const GameX* g, bool upToCurrentMove)
text += m_startTagMap[MarkupColumnStyleMainline];
}

QString gameComment = (m_outputType == Pgn) ? m_game.annotation(0) : m_game.textAnnotation(0, GameX::AfterMove, m_game.textFilter2());
QString gameComment = isPgnType(m_outputType) ? m_game.annotation(0) : m_game.textAnnotation(0, GameX::AfterMove, m_game.textFilter2());
text += writeGameComment(gameComment);

text += writeMainLine(mainId);
Expand Down Expand Up @@ -1043,7 +1045,7 @@ void Output::output(const QString& filename, Database& database)
return;
}
if((m_outputType == Html) || (m_outputType == NotationWidget) ||
(m_outputType == Pgn && database.isUtf8()))
(isPgnType(m_outputType) && database.isUtf8()))
{
QTextStream out(&f);
outputUtf8(out, database);
Expand Down Expand Up @@ -1132,6 +1134,7 @@ void Output::setTemplateFile(QString filename)
filename = DEFAULT_NOTATION_TEMPLATE;
break;
case Pgn:
case LocalPgn:
filename = DEFAULT_PGN_TEMPLATE;
break;
default :
Expand Down
6 changes: 5 additions & 1 deletion src/database/output.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,12 @@ class Output : public QObject
Html, /**< Exports the game in Html format */
Pgn, /**< Exports the game in PGN format */
Latex, /**< Exports the game in Latex format */
NotationWidget /**< Exports the game in format appropriate for the notation widget */
NotationWidget, /**< Exports the game in format appropriate for the notation widget */
LocalPgn
};
inline bool isPgnType(OutputType o) const { return ((o==Pgn) || (o==LocalPgn)); }
inline bool isLocalized(OutputType o) const { return ((o==NotationWidget) || (o==LocalPgn)); }

enum MoveToWrite
{
PreviousMove,
Expand Down
7 changes: 6 additions & 1 deletion src/gui/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1461,7 +1461,8 @@ QString MainWindow::exportFileName(int& format)
QStringList filters;
filters << tr("PGN file (*.pgn)")
<< tr("HTML page (*.html)")
<< tr("LaTeX document (*.tex)");
<< tr("LaTeX document (*.tex)")
<< tr("Localized PGN (*.txt)");
fd.setNameFilters(filters);
if(fd.exec() != QDialog::Accepted)
{
Expand All @@ -1475,6 +1476,10 @@ QString MainWindow::exportFileName(int& format)
{
format = Output::Html;
}
else if(fd.selectedNameFilter().contains("*.txt"))
{
format = Output::LocalPgn;
}
else
{
format = Output::Pgn;
Expand Down
12 changes: 11 additions & 1 deletion src/gui/testadapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ bool TestAdapter::dispatchTests()
QCommandLineOption enOption(QStringList() << "en" << "eco-no-classify",
QCoreApplication::translate("TestAdapter", "Never classify games."));
parser.addOption(enOption);
QCommandLineOption lcOption(QStringList() << "lc" << "locale",
QCoreApplication::translate("TestAdapter", "Output Locale."),
QCoreApplication::translate("TestAdapter", "Locale"));

// Do not start the GUI after processing the files
QCommandLineOption exitOption(QStringList() << "x" << "exit",
Expand All @@ -67,6 +70,7 @@ bool TestAdapter::dispatchTests()

QString inputFile = parser.value(inputOption);
QString outputFile = parser.value(outputOption);
QString lc = parser.value(lcOption);

bool ec = parser.isSet("ec");
bool ek = parser.isSet("ek");
Expand All @@ -81,6 +85,11 @@ bool TestAdapter::dispatchTests()
AppSettings->setValue("/General/preserveECO", ek);
}

if (!lc.isEmpty())
{
AppSettings->setValue("/GameText/PieceString", lc);
}

if (!inputFile.isEmpty())
{
if (outputFile.isEmpty())
Expand All @@ -106,6 +115,7 @@ void TestAdapter::convertPgn(const QString& filename, const QString& outfile, QC
bool rv = parser.isSet("rv");
bool rc = parser.isSet("rc");
bool rt = parser.isSet("rt");
bool lc = parser.isSet("lc");

if (db.open(filename, true) && ((Database*)(&db))->parseFile())
{
Expand All @@ -122,7 +132,7 @@ void TestAdapter::convertPgn(const QString& filename, const QString& outfile, QC
}
}

Output output(Output::Pgn);
Output output(lc ? Output::LocalPgn : Output::Pgn);
output.outputLatin1(outfile, db);
}
}
Expand Down

0 comments on commit d41e886

Please sign in to comment.