mirror of
https://github.com/JakubMelka/PDF4QT.git
synced 2025-06-05 21:59:17 +02:00
Help tool
This commit is contained in:
@ -23,6 +23,10 @@
|
|||||||
|
|
||||||
#include <stack>
|
#include <stack>
|
||||||
|
|
||||||
|
#ifdef Q_OS_WIN
|
||||||
|
#include "Windows.h"
|
||||||
|
#endif
|
||||||
|
|
||||||
namespace pdftool
|
namespace pdftool
|
||||||
{
|
{
|
||||||
|
|
||||||
@ -49,6 +53,9 @@ public:
|
|||||||
|
|
||||||
/// Get result string in unicode.
|
/// Get result string in unicode.
|
||||||
virtual QString getString() const = 0;
|
virtual QString getString() const = 0;
|
||||||
|
|
||||||
|
/// Ends current line (for formatters, that support it)
|
||||||
|
virtual void endl() { }
|
||||||
};
|
};
|
||||||
|
|
||||||
class PDFTextOutputFormatterImpl : public PDFOutputFormatterImpl
|
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 beginElement(PDFOutputFormatter::Element type, QString name, QString description, Qt::Alignment alignment, int reference) override;
|
||||||
virtual void endElement() override;
|
virtual void endElement() override;
|
||||||
virtual QString getString() const override;
|
virtual QString getString() const override;
|
||||||
|
virtual void endl() override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static constexpr const int INDENT_STEP = 2;
|
static constexpr const int INDENT_STEP = 2;
|
||||||
@ -75,7 +83,6 @@ private:
|
|||||||
|
|
||||||
QString m_string;
|
QString m_string;
|
||||||
QTextStream m_streamWriter;
|
QTextStream m_streamWriter;
|
||||||
int m_depth;
|
|
||||||
int m_indent;
|
int m_indent;
|
||||||
std::stack<PDFOutputFormatter::Element> m_elementStack;
|
std::stack<PDFOutputFormatter::Element> m_elementStack;
|
||||||
std::vector<std::vector<TableCell>> m_table;
|
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 beginElement(PDFOutputFormatter::Element type, QString name, QString description, Qt::Alignment alignment, int reference) override;
|
||||||
virtual void endElement() override;
|
virtual void endElement() override;
|
||||||
virtual QString getString() const override;
|
virtual QString getString() const override;
|
||||||
|
virtual void endl() override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QString m_string;
|
QString m_string;
|
||||||
@ -118,7 +126,6 @@ private:
|
|||||||
PDFTextOutputFormatterImpl::PDFTextOutputFormatterImpl() :
|
PDFTextOutputFormatterImpl::PDFTextOutputFormatterImpl() :
|
||||||
m_string(),
|
m_string(),
|
||||||
m_streamWriter(&m_string, QIODevice::WriteOnly),
|
m_streamWriter(&m_string, QIODevice::WriteOnly),
|
||||||
m_depth(0),
|
|
||||||
m_elementStack()
|
m_elementStack()
|
||||||
{
|
{
|
||||||
|
|
||||||
@ -126,11 +133,14 @@ PDFTextOutputFormatterImpl::PDFTextOutputFormatterImpl() :
|
|||||||
|
|
||||||
void PDFTextOutputFormatterImpl::beginElement(PDFOutputFormatter::Element type, QString name, QString description, Qt::Alignment alignment, int reference)
|
void PDFTextOutputFormatterImpl::beginElement(PDFOutputFormatter::Element type, QString name, QString description, Qt::Alignment alignment, int reference)
|
||||||
{
|
{
|
||||||
|
Q_UNUSED(name);
|
||||||
Q_UNUSED(reference);
|
Q_UNUSED(reference);
|
||||||
|
|
||||||
m_elementStack.push(type);
|
m_elementStack.push(type);
|
||||||
|
|
||||||
switch (type)
|
switch (type)
|
||||||
{
|
{
|
||||||
|
case PDFOutputFormatter::Element::Text:
|
||||||
case PDFOutputFormatter::Element::Root:
|
case PDFOutputFormatter::Element::Root:
|
||||||
{
|
{
|
||||||
m_streamWriter << description << Qt::endl;
|
m_streamWriter << description << Qt::endl;
|
||||||
@ -172,19 +182,16 @@ void PDFTextOutputFormatterImpl::beginElement(PDFOutputFormatter::Element type,
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Increment depth by one
|
|
||||||
++m_depth;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void PDFTextOutputFormatterImpl::endElement()
|
void PDFTextOutputFormatterImpl::endElement()
|
||||||
{
|
{
|
||||||
PDFOutputFormatter::Element type = m_elementStack.top();
|
PDFOutputFormatter::Element type = m_elementStack.top();
|
||||||
m_elementStack.pop();
|
m_elementStack.pop();
|
||||||
--m_depth;
|
|
||||||
|
|
||||||
switch (type)
|
switch (type)
|
||||||
{
|
{
|
||||||
|
case PDFOutputFormatter::Element::Text:
|
||||||
case PDFOutputFormatter::Element::Root:
|
case PDFOutputFormatter::Element::Root:
|
||||||
{
|
{
|
||||||
m_indent -= INDENT_STEP;
|
m_indent -= INDENT_STEP;
|
||||||
@ -269,6 +276,11 @@ QString PDFTextOutputFormatterImpl::getString() const
|
|||||||
return m_string;
|
return m_string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void PDFTextOutputFormatterImpl::endl()
|
||||||
|
{
|
||||||
|
m_streamWriter << Qt::endl;
|
||||||
|
}
|
||||||
|
|
||||||
void PDFTextOutputFormatterImpl::writeIndent()
|
void PDFTextOutputFormatterImpl::writeIndent()
|
||||||
{
|
{
|
||||||
QString str(m_indent, QChar(QChar::Space));
|
QString str(m_indent, QChar(QChar::Space));
|
||||||
@ -351,6 +363,12 @@ void PDFHtmlOutputFormatterImpl::beginElement(PDFOutputFormatter::Element type,
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
case PDFOutputFormatter::Element::Text:
|
||||||
|
{
|
||||||
|
m_streamWriter.writeTextElement("p", description);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
case PDFOutputFormatter::Element::Table:
|
case PDFOutputFormatter::Element::Table:
|
||||||
{
|
{
|
||||||
m_streamWriter.writeStartElement("table");
|
m_streamWriter.writeStartElement("table");
|
||||||
@ -431,6 +449,7 @@ void PDFHtmlOutputFormatterImpl::endElement()
|
|||||||
|
|
||||||
case PDFOutputFormatter::Element::TableHeaderColumn:
|
case PDFOutputFormatter::Element::TableHeaderColumn:
|
||||||
case PDFOutputFormatter::Element::TableColumn:
|
case PDFOutputFormatter::Element::TableColumn:
|
||||||
|
case PDFOutputFormatter::Element::Text:
|
||||||
{
|
{
|
||||||
// Do nothing...
|
// Do nothing...
|
||||||
break;
|
break;
|
||||||
@ -452,6 +471,12 @@ QString PDFHtmlOutputFormatterImpl::getString() const
|
|||||||
return html;
|
return html;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void PDFHtmlOutputFormatterImpl::endl()
|
||||||
|
{
|
||||||
|
m_streamWriter.writeStartElement("br");
|
||||||
|
m_streamWriter.writeEndElement();
|
||||||
|
}
|
||||||
|
|
||||||
PDFXmlOutputFormatterImpl::PDFXmlOutputFormatterImpl() :
|
PDFXmlOutputFormatterImpl::PDFXmlOutputFormatterImpl() :
|
||||||
m_string(),
|
m_string(),
|
||||||
m_streamWriter(&m_string),
|
m_streamWriter(&m_string),
|
||||||
@ -483,6 +508,7 @@ void PDFXmlOutputFormatterImpl::beginElement(PDFOutputFormatter::Element type, Q
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
case PDFOutputFormatter::Element::Text:
|
||||||
case PDFOutputFormatter::Element::TableColumn:
|
case PDFOutputFormatter::Element::TableColumn:
|
||||||
case PDFOutputFormatter::Element::TableHeaderColumn:
|
case PDFOutputFormatter::Element::TableHeaderColumn:
|
||||||
{
|
{
|
||||||
@ -563,9 +589,23 @@ void PDFOutputFormatter::endElement()
|
|||||||
m_impl->endElement();
|
m_impl->endElement();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void PDFOutputFormatter::endl()
|
||||||
|
{
|
||||||
|
m_impl->endl();
|
||||||
|
}
|
||||||
|
|
||||||
QString PDFOutputFormatter::getString() const
|
QString PDFOutputFormatter::getString() const
|
||||||
{
|
{
|
||||||
return m_impl->getString();
|
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
|
} // pdftool
|
||||||
|
@ -44,6 +44,7 @@ public:
|
|||||||
{
|
{
|
||||||
Root, ///< Root element, this must be used only once at start/end of writing
|
Root, ///< Root element, this must be used only once at start/end of writing
|
||||||
Header, ///< Header
|
Header, ///< Header
|
||||||
|
Text, ///< Ordinary text
|
||||||
Table, ///< Table of rows/columns (2D grid)
|
Table, ///< Table of rows/columns (2D grid)
|
||||||
TableHeaderRow, ///< Table header row (consists of columns)
|
TableHeaderRow, ///< Table header row (consists of columns)
|
||||||
TableHeaderColumn, ///< Table header column
|
TableHeaderColumn, ///< Table header column
|
||||||
@ -66,6 +67,20 @@ public:
|
|||||||
/// Ends current element. Must match with a call of \p beginElement
|
/// Ends current element. Must match with a call of \p beginElement
|
||||||
void endElement();
|
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.
|
/// Get result string in unicode.
|
||||||
QString getString() const;
|
QString getString() const;
|
||||||
|
|
||||||
@ -73,6 +88,17 @@ private:
|
|||||||
PDFOutputFormatterImpl* m_impl;
|
PDFOutputFormatterImpl* m_impl;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class PDFConsole
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
|
||||||
|
/// Writes text to the console
|
||||||
|
static void writeText(QString text);
|
||||||
|
|
||||||
|
private:
|
||||||
|
explicit PDFConsole() = delete;
|
||||||
|
};
|
||||||
|
|
||||||
} // namespace pdftool
|
} // namespace pdftool
|
||||||
|
|
||||||
#endif // PDFOUTPUTFORMATTER_H
|
#endif // PDFOUTPUTFORMATTER_H
|
||||||
|
@ -17,6 +17,8 @@
|
|||||||
|
|
||||||
#include "pdftoolabstractapplication.h"
|
#include "pdftoolabstractapplication.h"
|
||||||
|
|
||||||
|
#include <QCommandLineParser>
|
||||||
|
|
||||||
namespace pdftool
|
namespace pdftool
|
||||||
{
|
{
|
||||||
|
|
||||||
@ -27,6 +29,7 @@ public:
|
|||||||
|
|
||||||
virtual QString getStandardString(StandardString standardString) const override;
|
virtual QString getStandardString(StandardString standardString) const override;
|
||||||
virtual int execute(const PDFToolOptions& options) override;
|
virtual int execute(const PDFToolOptions& options) override;
|
||||||
|
virtual Options getOptionsFlags() const override;
|
||||||
};
|
};
|
||||||
|
|
||||||
static PDFToolHelpApplication s_helpApplication;
|
static PDFToolHelpApplication s_helpApplication;
|
||||||
@ -54,7 +57,64 @@ QString PDFToolHelpApplication::getStandardString(StandardString standardString)
|
|||||||
|
|
||||||
int PDFToolHelpApplication::execute(const PDFToolOptions& options)
|
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)
|
PDFToolAbstractApplication::PDFToolAbstractApplication(bool isDefault)
|
||||||
@ -64,13 +124,40 @@ PDFToolAbstractApplication::PDFToolAbstractApplication(bool isDefault)
|
|||||||
|
|
||||||
void PDFToolAbstractApplication::initializeCommandLineParser(QCommandLineParser* parser) const
|
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 PDFToolAbstractApplication::getOptions(QCommandLineParser* parser) const
|
||||||
{
|
{
|
||||||
PDFToolOptions options;
|
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;
|
return options;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -103,6 +190,11 @@ PDFToolAbstractApplication* PDFToolApplicationStorage::getDefaultApplication()
|
|||||||
return getInstance()->m_defaultApplication;
|
return getInstance()->m_defaultApplication;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const std::vector<PDFToolAbstractApplication*>& PDFToolApplicationStorage::getApplications()
|
||||||
|
{
|
||||||
|
return getInstance()->m_applications;
|
||||||
|
}
|
||||||
|
|
||||||
PDFToolApplicationStorage* PDFToolApplicationStorage::getInstance()
|
PDFToolApplicationStorage* PDFToolApplicationStorage::getInstance()
|
||||||
{
|
{
|
||||||
static PDFToolApplicationStorage storage;
|
static PDFToolApplicationStorage storage;
|
||||||
|
@ -18,6 +18,8 @@
|
|||||||
#ifndef PDFTOOLABSTRACTAPPLICATION_H
|
#ifndef PDFTOOLABSTRACTAPPLICATION_H
|
||||||
#define PDFTOOLABSTRACTAPPLICATION_H
|
#define PDFTOOLABSTRACTAPPLICATION_H
|
||||||
|
|
||||||
|
#include "pdfoutputformatter.h"
|
||||||
|
|
||||||
#include <QtGlobal>
|
#include <QtGlobal>
|
||||||
#include <QString>
|
#include <QString>
|
||||||
#include <QCoreApplication>
|
#include <QCoreApplication>
|
||||||
@ -38,6 +40,7 @@ struct PDFToolOptions
|
|||||||
{
|
{
|
||||||
QString document;
|
QString document;
|
||||||
QString password;
|
QString password;
|
||||||
|
PDFOutputFormatter::Style outputStyle = PDFOutputFormatter::Style::Text;
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Base class for all applications
|
/// Base class for all applications
|
||||||
@ -47,6 +50,11 @@ public:
|
|||||||
explicit PDFToolAbstractApplication(bool isDefault = false);
|
explicit PDFToolAbstractApplication(bool isDefault = false);
|
||||||
virtual ~PDFToolAbstractApplication() = default;
|
virtual ~PDFToolAbstractApplication() = default;
|
||||||
|
|
||||||
|
enum ExitCodes
|
||||||
|
{
|
||||||
|
ExitSuccess = EXIT_SUCCESS
|
||||||
|
};
|
||||||
|
|
||||||
enum StandardString
|
enum StandardString
|
||||||
{
|
{
|
||||||
Command, ///< Command, by which is this application invoked
|
Command, ///< Command, by which is this application invoked
|
||||||
@ -54,8 +62,15 @@ public:
|
|||||||
Description ///< Description (what this application does)
|
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 QString getStandardString(StandardString standardString) const = 0;
|
||||||
virtual int execute(const PDFToolOptions& options) = 0;
|
virtual int execute(const PDFToolOptions& options) = 0;
|
||||||
|
virtual Options getOptionsFlags() const = 0;
|
||||||
|
|
||||||
void initializeCommandLineParser(QCommandLineParser* parser) const;
|
void initializeCommandLineParser(QCommandLineParser* parser) const;
|
||||||
PDFToolOptions getOptions(QCommandLineParser* parser) const;
|
PDFToolOptions getOptions(QCommandLineParser* parser) const;
|
||||||
@ -83,6 +98,9 @@ public:
|
|||||||
/// \returns Default application
|
/// \returns Default application
|
||||||
static PDFToolAbstractApplication* getDefaultApplication();
|
static PDFToolAbstractApplication* getDefaultApplication();
|
||||||
|
|
||||||
|
/// Returns a list of available applications
|
||||||
|
static const std::vector<PDFToolAbstractApplication*>& getApplications();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
PDFToolApplicationStorage() = default;
|
PDFToolApplicationStorage() = default;
|
||||||
static PDFToolApplicationStorage* getInstance();
|
static PDFToolApplicationStorage* getInstance();
|
||||||
|
Reference in New Issue
Block a user