Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion projects/gui/src/movelist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
#include <QAction>
#include <chessgame.h>
#include <QMenu>

#include <QClipboard>
#include "cutechessapp.h"
#include <QTextStream>

MoveList::MoveList(QWidget* parent)
: QWidget(parent),
Expand Down Expand Up @@ -90,6 +92,21 @@ MoveList::MoveList(QWidget* parent)
toggleCommentAct->setEnabled(true);
});
});
QAction* copyListAct = new QAction("Copy with PGN header", m_moveList);
m_moveList->addAction(copyListAct);
connect(copyListAct, &QAction::triggered, this, [=]()
{
if (m_game.isNull() && m_pgn == nullptr)
return;

PgnGame::PgnMode mode = m_showComments ?PgnGame::PgnMode::Verbose:
PgnGame::PgnMode::Minimal;
QClipboard* cb = CuteChessApplication::clipboard();
QString str;
QTextStream s(&str);
m_pgn->write(s, mode, true);
cb->setText(s.readAll());
});
m_moveList->setContextMenuPolicy(Qt::ContextMenuPolicy::CustomContextMenu);

connect(m_moveList, SIGNAL(customContextMenuRequested(const QPoint&)),
Expand Down
15 changes: 11 additions & 4 deletions projects/lib/src/pgngame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ bool PgnGame::read(PgnStream& in, int maxMoves, bool addEco)
return true;
}

bool PgnGame::write(QTextStream& out, PgnMode mode) const
bool PgnGame::write(QTextStream& out, PgnMode mode, bool aligned) const
{
if (m_tags.isEmpty())
return false;
Expand Down Expand Up @@ -310,11 +310,18 @@ bool PgnGame::write(QTextStream& out, PgnMode mode) const
str = QString::number(++movenum) + ". ";

str += data.moveString;
if (aligned && (mode == Verbose || side == Chess::Side::White))
str = QString("%1").arg(str, -12);

if (mode == Verbose && !data.comment.isEmpty())
str += QString(" {%1}").arg(data.comment);
if (mode == Verbose && aligned && side == Chess::Side::White)
str = QString("%1").arg(str, -36);

// Limit the lines to 80 characters
if (lineLength == 0 || lineLength + str.size() >= 80)
if (lineLength == 0 || lineLength + str.size() >= 80
// or list one full move per line for listed mode.
|| (aligned && side == Chess::Side::White))
{
out << "\n" << str;
lineLength = str.size();
Expand All @@ -340,14 +347,14 @@ bool PgnGame::write(QTextStream& out, PgnMode mode) const
return (out.status() == QTextStream::Ok);
}

bool PgnGame::write(const QString& filename, PgnMode mode) const
bool PgnGame::write(const QString& filename, PgnMode mode, bool aligned) const
{
QFile file(filename);
if (!file.open(QIODevice::WriteOnly | QIODevice::Append))
return false;

QTextStream out(&file);
return write(out, mode);
return write(out, mode, aligned);
}

bool PgnGame::isStandard() const
Expand Down
12 changes: 8 additions & 4 deletions projects/lib/src/pgngame.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,19 +114,23 @@ class LIB_EXPORT PgnGame
bool read(PgnStream& in, int maxMoves = INT_MAX - 1,
bool addEco = true);
/*!
* Writes the game to a text stream.
* Writes the game to a text stream. If \a aligned is true,
* then only one full move per line is listed.
*
* Returns true if successful; otherwise returns false.
*/
bool write(QTextStream& out, PgnMode mode = Verbose) const;
bool write(QTextStream& out, PgnMode mode = Verbose,
bool aligned = false) const;
/*!
* Writes the game to a file.
* If the file already exists, the game will be appended
* to the end of the file.
* to the end of the file. If \a aligned is true,
* then only one full move per line is listed.
*
* Returns true if successful; otherwise returns false.
*/
bool write(const QString& filename, PgnMode mode = Verbose) const;
bool write(const QString& filename, PgnMode mode = Verbose,
bool aligned = false) const;

/*!
* Returns true if the game's variant is "standard" and it's
Expand Down