Help tool

This commit is contained in:
Jakub Melka 2020-09-26 17:10:23 +02:00
parent 6e212d89f0
commit 4c763277fc
4 changed files with 183 additions and 7 deletions

View File

@ -23,6 +23,10 @@
#include <stack>
#ifdef Q_OS_WIN
#include "Windows.h"
#endif
namespace pdftool
{
@ -49,6 +53,9 @@ public:
/// Get result string in unicode.
virtual QString getString() const = 0;
/// Ends current line (for formatters, that support it)
virtual void endl() { }
};
class PDFTextOutputFormatterImpl : public PDFOutputFormatterImpl
@ -59,6 +66,7 @@ public:
virtual void beginElement(PDFOutputFormatter::Element type, QString name, QString description, Qt::Alignment alignment, int reference) override;
virtual void endElement() override;
virtual QString getString() const override;
virtual void endl() override;
private:
static constexpr const int INDENT_STEP = 2;
@ -75,7 +83,6 @@ private:
QString m_string;
QTextStream m_streamWriter;
int m_depth;
int m_indent;
std::stack<PDFOutputFormatter::Element> m_elementStack;
std::vector<std::vector<TableCell>> m_table;
@ -106,6 +113,7 @@ public:
virtual void beginElement(PDFOutputFormatter::Element type, QString name, QString description, Qt::Alignment alignment, int reference) override;
virtual void endElement() override;
virtual QString getString() const override;
virtual void endl() override;
private:
QString m_string;
@ -118,7 +126,6 @@ private:
PDFTextOutputFormatterImpl::PDFTextOutputFormatterImpl() :
m_string(),
m_streamWriter(&m_string, QIODevice::WriteOnly),
m_depth(0),
m_elementStack()
{
@ -126,11 +133,14 @@ PDFTextOutputFormatterImpl::PDFTextOutputFormatterImpl() :
void PDFTextOutputFormatterImpl::beginElement(PDFOutputFormatter::Element type, QString name, QString description, Qt::Alignment alignment, int reference)
{
Q_UNUSED(name);
Q_UNUSED(reference);
m_elementStack.push(type);
switch (type)
{
case PDFOutputFormatter::Element::Text:
case PDFOutputFormatter::Element::Root:
{
m_streamWriter << description << Qt::endl;
@ -172,19 +182,16 @@ void PDFTextOutputFormatterImpl::beginElement(PDFOutputFormatter::Element type,
break;
}
}
// Increment depth by one
++m_depth;
}
void PDFTextOutputFormatterImpl::endElement()
{
PDFOutputFormatter::Element type = m_elementStack.top();
m_elementStack.pop();
--m_depth;
switch (type)
{
case PDFOutputFormatter::Element::Text:
case PDFOutputFormatter::Element::Root:
{
m_indent -= INDENT_STEP;
@ -269,6 +276,11 @@ QString PDFTextOutputFormatterImpl::getString() const
return m_string;
}
void PDFTextOutputFormatterImpl::endl()
{
m_streamWriter << Qt::endl;
}
void PDFTextOutputFormatterImpl::writeIndent()
{
QString str(m_indent, QChar(QChar::Space));
@ -351,6 +363,12 @@ void PDFHtmlOutputFormatterImpl::beginElement(PDFOutputFormatter::Element type,
break;
}
case PDFOutputFormatter::Element::Text:
{
m_streamWriter.writeTextElement("p", description);
break;
}
case PDFOutputFormatter::Element::Table:
{
m_streamWriter.writeStartElement("table");
@ -431,6 +449,7 @@ void PDFHtmlOutputFormatterImpl::endElement()
case PDFOutputFormatter::Element::TableHeaderColumn:
case PDFOutputFormatter::Element::TableColumn:
case PDFOutputFormatter::Element::Text:
{
// Do nothing...
break;
@ -452,6 +471,12 @@ QString PDFHtmlOutputFormatterImpl::getString() const
return html;
}
void PDFHtmlOutputFormatterImpl::endl()
{
m_streamWriter.writeStartElement("br");
m_streamWriter.writeEndElement();
}
PDFXmlOutputFormatterImpl::PDFXmlOutputFormatterImpl() :
m_string(),
m_streamWriter(&m_string),
@ -483,6 +508,7 @@ void PDFXmlOutputFormatterImpl::beginElement(PDFOutputFormatter::Element type, Q
break;
}
case PDFOutputFormatter::Element::Text:
case PDFOutputFormatter::Element::TableColumn:
case PDFOutputFormatter::Element::TableHeaderColumn:
{
@ -563,9 +589,23 @@ void PDFOutputFormatter::endElement()
m_impl->endElement();
}
void PDFOutputFormatter::endl()
{
m_impl->endl();
}
QString PDFOutputFormatter::getString() const
{
return m_impl->getString();
}
void PDFConsole::writeText(QString text)
{
#ifdef Q_OS_WIN
WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), text.utf16(), text.size(), nullptr, nullptr);
#else
QTextStream(stdout) << text;
#endif
}
} // pdftool

View File

@ -44,6 +44,7 @@ public:
{
Root, ///< Root element, this must be used only once at start/end of writing
Header, ///< Header
Text, ///< Ordinary text
Table, ///< Table of rows/columns (2D grid)
TableHeaderRow, ///< Table header row (consists of columns)
TableHeaderColumn, ///< Table header column
@ -66,6 +67,20 @@ public:
/// Ends current element. Must match with a call of \p beginElement
void endElement();
inline void beginDocument(QString name, QString description) { beginElement(Element::Root, name, description); }
inline void endDocument() { endElement(); }
inline void beginTable(QString name, QString description) { beginElement(Element::Table, name, description); }
inline void endTable() { endElement(); }
inline void beginTableHeaderRow(QString name) { beginElement(Element::TableHeaderRow, name); }
inline void endTableHeaderRow() { endElement(); }
inline void beginTableRow(QString name) { beginElement(Element::TableRow, name); }
inline void endTableRow() { endElement(); }
inline void writeTableHeaderColumn(QString name, QString description, Qt::Alignment alignment = Qt::AlignCenter) { beginElement(Element::TableHeaderColumn, name, description, alignment); endElement(); }
inline void writeTableColumn(QString name, QString description, Qt::Alignment alignment = Qt::AlignLeft) { beginElement(Element::TableColumn, name, description, alignment); endElement(); }
/// Ends current line
void endl();
/// Get result string in unicode.
QString getString() const;
@ -73,6 +88,17 @@ private:
PDFOutputFormatterImpl* m_impl;
};
class PDFConsole
{
public:
/// Writes text to the console
static void writeText(QString text);
private:
explicit PDFConsole() = delete;
};
} // namespace pdftool
#endif // PDFOUTPUTFORMATTER_H

View File

@ -17,6 +17,8 @@
#include "pdftoolabstractapplication.h"
#include <QCommandLineParser>
namespace pdftool
{
@ -27,6 +29,7 @@ public:
virtual QString getStandardString(StandardString standardString) const override;
virtual int execute(const PDFToolOptions& options) override;
virtual Options getOptionsFlags() const override;
};
static PDFToolHelpApplication s_helpApplication;
@ -54,7 +57,64 @@ QString PDFToolHelpApplication::getStandardString(StandardString standardString)
int PDFToolHelpApplication::execute(const PDFToolOptions& options)
{
return EXIT_SUCCESS;
PDFOutputFormatter formatter(options.outputStyle);
formatter.beginDocument("help", PDFToolTranslationContext::tr("PDFTool help"));
formatter.endl();
formatter.beginTable("commands", PDFToolTranslationContext::tr("List of available commands"));
// Table header
formatter.beginTableHeaderRow("header");
formatter.writeTableHeaderColumn("command", PDFToolTranslationContext::tr("Command"));
formatter.writeTableHeaderColumn("tool", PDFToolTranslationContext::tr("Tool"));
formatter.writeTableHeaderColumn("description", PDFToolTranslationContext::tr("Description"));
formatter.endTableHeaderRow();
struct Info
{
bool operator<(const Info& other) const
{
return command < other.command;
}
QString command;
QString name;
QString description;
};
std::vector<Info> infos;
for (PDFToolAbstractApplication* application : PDFToolApplicationStorage::getApplications())
{
Info info;
info.command = application->getStandardString(Command);
info.name = application->getStandardString(Name);
info.description = application->getStandardString(Description);
infos.emplace_back(qMove(info));
}
qSort(infos);
for (const Info& info : infos)
{
formatter.beginTableRow("command");
formatter.writeTableColumn("command", info.command);
formatter.writeTableColumn("name", info.name);
formatter.writeTableColumn("description", info.description);
formatter.endTableRow();
}
formatter.endTable();
formatter.endDocument();
PDFConsole::writeText(formatter.getString());
return ExitSuccess;
}
PDFToolAbstractApplication::Options PDFToolHelpApplication::getOptionsFlags() const
{
return ConsoleFormat;
}
PDFToolAbstractApplication::PDFToolAbstractApplication(bool isDefault)
@ -64,13 +124,40 @@ PDFToolAbstractApplication::PDFToolAbstractApplication(bool isDefault)
void PDFToolAbstractApplication::initializeCommandLineParser(QCommandLineParser* parser) const
{
Options optionFlags = getOptionsFlags();
if (optionFlags.testFlag(ConsoleFormat))
{
parser->addOption(QCommandLineOption("console-format", "Console output text format (valid values: text|xml|html).", "console-format", "text"));
}
}
PDFToolOptions PDFToolAbstractApplication::getOptions(QCommandLineParser* parser) const
{
PDFToolOptions options;
Options optionFlags = getOptionsFlags();
if (optionFlags.testFlag(ConsoleFormat))
{
QString consoleFormat = parser->value("console-format");
if (consoleFormat == "text")
{
options.outputStyle = PDFOutputFormatter::Style::Text;
}
else if (consoleFormat == "xml")
{
options.outputStyle = PDFOutputFormatter::Style::Xml;
}
else if (consoleFormat == "html")
{
options.outputStyle = PDFOutputFormatter::Style::Html;
}
else
{
options.outputStyle = PDFOutputFormatter::Style::Text;
}
}
return options;
}
@ -103,6 +190,11 @@ PDFToolAbstractApplication* PDFToolApplicationStorage::getDefaultApplication()
return getInstance()->m_defaultApplication;
}
const std::vector<PDFToolAbstractApplication*>& PDFToolApplicationStorage::getApplications()
{
return getInstance()->m_applications;
}
PDFToolApplicationStorage* PDFToolApplicationStorage::getInstance()
{
static PDFToolApplicationStorage storage;

View File

@ -18,6 +18,8 @@
#ifndef PDFTOOLABSTRACTAPPLICATION_H
#define PDFTOOLABSTRACTAPPLICATION_H
#include "pdfoutputformatter.h"
#include <QtGlobal>
#include <QString>
#include <QCoreApplication>
@ -38,6 +40,7 @@ struct PDFToolOptions
{
QString document;
QString password;
PDFOutputFormatter::Style outputStyle = PDFOutputFormatter::Style::Text;
};
/// Base class for all applications
@ -47,6 +50,11 @@ public:
explicit PDFToolAbstractApplication(bool isDefault = false);
virtual ~PDFToolAbstractApplication() = default;
enum ExitCodes
{
ExitSuccess = EXIT_SUCCESS
};
enum StandardString
{
Command, ///< Command, by which is this application invoked
@ -54,8 +62,15 @@ public:
Description ///< Description (what this application does)
};
enum Option
{
ConsoleFormat = 0x0001, ///< Set format of console output (text, xml or html)
};
Q_DECLARE_FLAGS(Options, Option)
virtual QString getStandardString(StandardString standardString) const = 0;
virtual int execute(const PDFToolOptions& options) = 0;
virtual Options getOptionsFlags() const = 0;
void initializeCommandLineParser(QCommandLineParser* parser) const;
PDFToolOptions getOptions(QCommandLineParser* parser) const;
@ -83,6 +98,9 @@ public:
/// \returns Default application
static PDFToolAbstractApplication* getDefaultApplication();
/// Returns a list of available applications
static const std::vector<PDFToolAbstractApplication*>& getApplications();
private:
PDFToolApplicationStorage() = default;
static PDFToolApplicationStorage* getInstance();