99 lines
3.0 KiB
C
99 lines
3.0 KiB
C
|
|
#pragma once
|
||
|
|
|
||
|
|
#include <QString>
|
||
|
|
#include <QStringList>
|
||
|
|
#include <QList>
|
||
|
|
#include <QByteArray>
|
||
|
|
#include <QMap>
|
||
|
|
|
||
|
|
// ============================================================
|
||
|
|
// Структуры данных для элементов документа
|
||
|
|
// ============================================================
|
||
|
|
|
||
|
|
enum class DocElementType {
|
||
|
|
Heading1,
|
||
|
|
Heading2,
|
||
|
|
Heading3,
|
||
|
|
NormalText,
|
||
|
|
Table,
|
||
|
|
Image
|
||
|
|
};
|
||
|
|
|
||
|
|
struct DocStyle {
|
||
|
|
QString font = "Times New Roman";
|
||
|
|
int fontSize = 24; // в половинах пунктов (24 = 12pt)
|
||
|
|
bool bold = false;
|
||
|
|
bool italic = false;
|
||
|
|
bool center = false;
|
||
|
|
};
|
||
|
|
|
||
|
|
struct TableData {
|
||
|
|
QList<QStringList> rows; // rows[i][j] = текст ячейки
|
||
|
|
QStringList headers;
|
||
|
|
};
|
||
|
|
|
||
|
|
struct DocElement {
|
||
|
|
DocElementType type;
|
||
|
|
QString text; // для текста / заголовков
|
||
|
|
DocStyle style;
|
||
|
|
TableData table; // для таблиц
|
||
|
|
QString imagePath; // для картинок
|
||
|
|
int imageWidthEmu = 5400000; // ~6 см
|
||
|
|
int imageHeightEmu = 3600000; // ~4 см
|
||
|
|
};
|
||
|
|
|
||
|
|
// ============================================================
|
||
|
|
// Класс-строитель DOCX-документа
|
||
|
|
// ============================================================
|
||
|
|
|
||
|
|
class DocxBuilder {
|
||
|
|
public:
|
||
|
|
DocxBuilder();
|
||
|
|
|
||
|
|
// --- Добавление элементов ---
|
||
|
|
void addHeading(const QString& text, int level = 1);
|
||
|
|
void addParagraph(const QString& text, const DocStyle& style = {});
|
||
|
|
void addTable(const TableData& table);
|
||
|
|
void addImage(const QString& imagePath,
|
||
|
|
int widthEmu = 5400000,
|
||
|
|
int heightEmu = 3600000);
|
||
|
|
void addPageBreak();
|
||
|
|
|
||
|
|
// --- Сохранение ---
|
||
|
|
bool save(const QString& filePath);
|
||
|
|
|
||
|
|
// --- Очистка ---
|
||
|
|
void clear();
|
||
|
|
|
||
|
|
private:
|
||
|
|
QList<DocElement> m_elements;
|
||
|
|
int m_imageCounter = 0;
|
||
|
|
|
||
|
|
// Генерация XML-частей
|
||
|
|
QString buildDocumentXml();
|
||
|
|
QString buildContentTypesXml();
|
||
|
|
QString buildRelsXml();
|
||
|
|
QString buildDocumentRelsXml();
|
||
|
|
QString buildSettingsXml();
|
||
|
|
QString buildStylesXml();
|
||
|
|
|
||
|
|
// Вспомогательные методы генерации XML
|
||
|
|
QString makeRunProps(const DocStyle& style);
|
||
|
|
QString makeParaProps(const DocStyle& style, const QString& styleId = "");
|
||
|
|
QString makeParagraph(const QString& text,
|
||
|
|
const DocStyle& style,
|
||
|
|
const QString& styleId = "");
|
||
|
|
QString makeTable(const TableData& table);
|
||
|
|
QString makeImageXml(int rId, int widthEmu, int heightEmu, int imgIdx);
|
||
|
|
QString escapeXml(const QString& text);
|
||
|
|
|
||
|
|
// Для хранения изображений (путь → данные)
|
||
|
|
struct ImageEntry {
|
||
|
|
QString sourcePath;
|
||
|
|
QString ext; // png, jpg, …
|
||
|
|
QString rId;
|
||
|
|
int index;
|
||
|
|
};
|
||
|
|
QList<ImageEntry> m_images;
|
||
|
|
};
|