338 lines
11 KiB
C
338 lines
11 KiB
C
|
#ifndef TEXT_PRESENT_H
|
||
|
#define TEXT_PRESENT_H
|
||
|
|
||
|
#include "libtextedit.h"
|
||
|
#include <libConfig.h>
|
||
|
#include <QString>
|
||
|
#include <QFont>
|
||
|
#include <memory>
|
||
|
|
||
|
namespace model_text {
|
||
|
typedef QList<uint32_t> CharStream;
|
||
|
|
||
|
/**
|
||
|
* @brief UTF-16瀛楃闆嗛殣鍠 */
|
||
|
class LIBTEXTEDIT_EXPORT CharSetU16 {
|
||
|
public:
|
||
|
static CharStream combineToU(const QString &buffer);
|
||
|
static QString splitToC(const CharStream &buffer);
|
||
|
static bool contains(uint32_t code);
|
||
|
static uint size();
|
||
|
};
|
||
|
|
||
|
/**
|
||
|
* @brief 璁剧疆鍗曚釜瀛楃鐨勬牸寮忥紝榛樿浠庡乏鍒板彸鎺掑竷
|
||
|
*/
|
||
|
class LIBTEXTEDIT_EXPORT CharFormat {
|
||
|
private:
|
||
|
qulonglong unique_id;
|
||
|
|
||
|
uint32_t point_size;
|
||
|
QString fontfamily_store;
|
||
|
bool bold_store;
|
||
|
bool italic_flag;
|
||
|
bool kerning_flag;
|
||
|
bool overline_flag;
|
||
|
bool underline_flag;
|
||
|
double float_Height;
|
||
|
QFont::Weight weight_store;
|
||
|
|
||
|
public:
|
||
|
CharFormat(qulonglong format_id);
|
||
|
CharFormat(const CharFormat& other);
|
||
|
|
||
|
qulonglong formatID() const;
|
||
|
|
||
|
uint32_t pointSize() const;
|
||
|
QString fontFamily() const;
|
||
|
bool bold() const;
|
||
|
bool italic() const;
|
||
|
bool kerning() const;
|
||
|
bool overline() const;
|
||
|
bool underline() const;
|
||
|
double floatHeight() const;
|
||
|
QFont::Weight weight() const;
|
||
|
|
||
|
void setPointSize(uint32_t val);
|
||
|
void setFontFamily(const QString &name);
|
||
|
void setBold(bool ste);
|
||
|
void setItalic(bool ste);
|
||
|
void setKerning(bool ste);
|
||
|
void setOverline(bool ste);
|
||
|
void setUnderline(bool ste);
|
||
|
void setFloatHeight(double val);
|
||
|
void setWeight(QFont::Weight val);
|
||
|
|
||
|
bool operator==(const CharFormat &other);
|
||
|
CharFormat &operator=(const CharFormat &other);
|
||
|
};
|
||
|
/**
|
||
|
* @brief 瀹氫箟鏂囨。鏍煎紡锛岄粯璁ゆ帓甯冧粠宸﹀埌鍙筹紝浠庝笂鍒颁笅
|
||
|
*/
|
||
|
class LIBTEXTEDIT_EXPORT DocFormat {
|
||
|
private:
|
||
|
std::shared_ptr<CharFormat> char_format;
|
||
|
double line_height = 20;
|
||
|
double indent_char_count = 2;
|
||
|
QMargins margins_store = QMargins(5,5,5,5);
|
||
|
bool visible_of_additional = false;
|
||
|
|
||
|
public:
|
||
|
DocFormat();
|
||
|
DocFormat(const DocFormat &other);
|
||
|
|
||
|
double lineHeight() const;
|
||
|
QMargins margins() const;
|
||
|
double indentChars() const;
|
||
|
bool additionalVisible() const;
|
||
|
std::shared_ptr<CharFormat> defaultCharFormat() const;
|
||
|
|
||
|
void setLineHeight(double val);
|
||
|
void setMargins(const QMargins &val);
|
||
|
void setIndentChars(double n);
|
||
|
void setAdditionalVisible(bool ste);
|
||
|
void setDefaultCharFormat(std::shared_ptr<CharFormat> format);
|
||
|
|
||
|
DocFormat& operator=(const DocFormat &other);
|
||
|
};
|
||
|
|
||
|
class WsBlock;
|
||
|
/**
|
||
|
* @brief 琛屽唴鍏冪礌
|
||
|
*/
|
||
|
class Element {
|
||
|
public:
|
||
|
enum class Type{
|
||
|
Char, Set
|
||
|
};
|
||
|
|
||
|
virtual ~Element() = default;
|
||
|
|
||
|
virtual void refer(std::weak_ptr<WsBlock> ptr) = 0;
|
||
|
virtual std::weak_ptr<WsBlock> blockRefer() const = 0;
|
||
|
|
||
|
/**
|
||
|
* @brief 鑾峰彇鍏冪礌鎵€鍦ㄧ殑娈佃惤绱㈠紩
|
||
|
* @return
|
||
|
*/
|
||
|
virtual uint32_t blockIndex() const = 0;
|
||
|
|
||
|
/**
|
||
|
* @brief 鍏冪礌缁戝畾瀛楃鏍煎紡
|
||
|
* @return
|
||
|
*/
|
||
|
virtual std::shared_ptr<CharFormat> getFormat() const = 0;
|
||
|
/**
|
||
|
* @brief 缁戝畾瀛楃鏍煎紡
|
||
|
* @param format
|
||
|
*/
|
||
|
virtual void bindFormat(std::shared_ptr<CharFormat> format) = 0;
|
||
|
|
||
|
/**
|
||
|
* @brief 鍏冪礌绫诲瀷
|
||
|
* @return
|
||
|
*/
|
||
|
virtual Type type() const = 0;
|
||
|
/**
|
||
|
* @brief 鏂囨。灞傛鍐咃紝鍏ㄥ眬璧峰绱㈠紩
|
||
|
* @return
|
||
|
*/
|
||
|
virtual uint32_t index() const = 0;
|
||
|
/**
|
||
|
* @brief 鑾峰彇鏂囨湰鍐呭琛ㄧず
|
||
|
* @return
|
||
|
*/
|
||
|
virtual QString toText() const = 0;
|
||
|
/**
|
||
|
* @brief 瀛愬厓绱犳暟閲 * @return
|
||
|
*/
|
||
|
virtual uint32_t childCount() const = 0;
|
||
|
};
|
||
|
|
||
|
|
||
|
/**
|
||
|
* @brief 鍗曚釜瀛楃鍐呭瓨妯″瀷
|
||
|
*/
|
||
|
class LIBTEXTEDIT_EXPORT WsChar : public Element {
|
||
|
private:
|
||
|
uint32_t code_store;
|
||
|
std::shared_ptr<CharFormat> char_format_refer;
|
||
|
|
||
|
public:
|
||
|
explicit WsChar(uint32_t code){this->code_store = code;}
|
||
|
virtual ~WsChar() = default;
|
||
|
|
||
|
uint32_t code() const { return code_store; }
|
||
|
|
||
|
static QList<WsChar*> convertFrom(const CharStream &codes);
|
||
|
|
||
|
// Element interface
|
||
|
public:
|
||
|
virtual std::shared_ptr<CharFormat> getFormat() const override { return this->char_format_refer; }
|
||
|
virtual void bindFormat(std::shared_ptr<CharFormat> format) override{this->char_format_refer = format;}
|
||
|
virtual Type type() const override { return Type::Char; }
|
||
|
virtual uint32_t blockIndex() const override;
|
||
|
virtual uint32_t index() const override;
|
||
|
virtual QString toText() const override { return CharSetU16::splitToC(QList<uint32_t>() << code_store); }
|
||
|
virtual uint32_t childCount() const override { return 0; }
|
||
|
virtual void refer(std::weak_ptr<WsBlock> ptr) override;
|
||
|
virtual std::weak_ptr<WsBlock> blockRefer() const override;
|
||
|
};
|
||
|
|
||
|
|
||
|
/**
|
||
|
* @brief 鏂囧瓧鐗囨鍐呭瓨妯″瀷
|
||
|
*/
|
||
|
class LIBTEXTEDIT_EXPORT WsPart : public Element {
|
||
|
public:
|
||
|
/**
|
||
|
* @brief 鎻掑叆鏂囨湰鐗囨
|
||
|
* @param text 鏂囨湰鍐呭
|
||
|
* @param index
|
||
|
*/
|
||
|
void insertChars(const QList<WsChar*> &text, uint32_t offset = -1);
|
||
|
/**
|
||
|
* @brief 鑾峰彇鏂囨湰鐗囨鍐呭
|
||
|
* @param offset 璧峰绱㈠紩
|
||
|
* @param count 鏁伴噺
|
||
|
* @return
|
||
|
*/
|
||
|
QList<WsChar*> getChars(uint32_t offset, uint32_t count) const;
|
||
|
/**
|
||
|
* @brief 鍒犻櫎鏂囨湰鐗囨鍐呭
|
||
|
* @param offset
|
||
|
* @param count
|
||
|
*/
|
||
|
void delChars(uint32_t offset, uint32_t count);
|
||
|
};
|
||
|
|
||
|
/**
|
||
|
* @brief 鏂囨。鍧楋紙娈佃惤锛夊厓绱犲唴瀛樺疄渚 */
|
||
|
class LIBTEXTEDIT_EXPORT WsBlock {
|
||
|
private:
|
||
|
QList<Element*> element_store;
|
||
|
|
||
|
public:
|
||
|
uint32_t blockIndex() const;
|
||
|
/**
|
||
|
* @brief 鎻掑叆鍏冪礌闆嗗悎
|
||
|
* @param text 鏂囨湰鍐呭
|
||
|
* @param index
|
||
|
*/
|
||
|
void addElements(const QList<Element*> &text, uint32_t offset = UINT32_MAX);
|
||
|
/**
|
||
|
* @brief 鑾峰彇鏂囨湰鐗囨鍐呭
|
||
|
* @param offset
|
||
|
* @param count
|
||
|
* @return
|
||
|
*/
|
||
|
QList<Element*> getElements(uint32_t offset, uint32_t count) const;
|
||
|
/**
|
||
|
* @brief 鍒犻櫎鏂囨湰鐗囨鍐呭
|
||
|
* @param offset
|
||
|
* @param count
|
||
|
*/
|
||
|
void delElements(uint32_t offset, uint32_t count);
|
||
|
/**
|
||
|
* @brief 鑾峰彇娈佃惤鐨勬枃鏈〃绀 * @return
|
||
|
*/
|
||
|
QString toText() const;
|
||
|
};
|
||
|
|
||
|
/**
|
||
|
* @brief 鏂囨。鍐呭瓨瀹炰緥
|
||
|
*/
|
||
|
class LIBTEXTEDIT_EXPORT WsDocument : public QObject {
|
||
|
Q_OBJECT
|
||
|
private:
|
||
|
std::shared_ptr<DocFormat> document_format;
|
||
|
QList<std::shared_ptr<WsBlock>> block_store;
|
||
|
|
||
|
public:
|
||
|
/**
|
||
|
* @brief 缁戝畾鏂囨。鏍煎紡
|
||
|
* @param f
|
||
|
*/
|
||
|
void bindFormat(std::shared_ptr<DocFormat> f);
|
||
|
/**
|
||
|
* @brief 鑾峰彇鏂囨。鏍煎紡
|
||
|
* @return
|
||
|
*/
|
||
|
std::shared_ptr<DocFormat> getFormat() const;
|
||
|
|
||
|
// 鍐呭瓨鏂囨。缁撴瀯鎺ュ彛 ===================================================
|
||
|
/**
|
||
|
* @brief 鎻掑叆娈佃惤
|
||
|
* @param blks
|
||
|
* @param offset
|
||
|
*/
|
||
|
void addBlocks(const QList<std::shared_ptr<WsBlock>> &blks, uint32_t offset = UINT32_MAX);
|
||
|
/**
|
||
|
* @brief 娈佃惤鏁伴噺
|
||
|
* @return
|
||
|
*/
|
||
|
uint32_t blockCount() const;
|
||
|
/**
|
||
|
* @brief 鑾峰彇娈佃惤寮曠敤
|
||
|
* @param offset
|
||
|
* @param count
|
||
|
* @return
|
||
|
*/
|
||
|
QList<std::shared_ptr<WsBlock>> getBlocks(uint32_t offset, uint32_t count = 1) const;
|
||
|
/**
|
||
|
* @brief 鍒犻櫎鎸囧畾鑼冨洿鍐呯殑娈佃惤
|
||
|
* @param offset 璧峰绱㈠紩
|
||
|
* @param count 娈佃惤鏁伴噺
|
||
|
*/
|
||
|
void delBlocks(uint32_t offset, uint32_t count = 1);
|
||
|
|
||
|
// // 鍐呭瓨鏂囨湰鎿嶄綔鎺ュ彛 ===================================================
|
||
|
// /**
|
||
|
// * @brief 鎸囧畾浣嶇疆鎻掑叆鏂囨湰
|
||
|
// * @param part 鏂囨湰鐗囨
|
||
|
// * @param index 璧峰绱㈠紩锛宨ndex<0琛ㄧず杩藉姞
|
||
|
// */
|
||
|
// void insertText(QList<WsChar> part, uint32_t index = -1);
|
||
|
// /**
|
||
|
// * @brief 鑾峰彇鎸囧畾浣嶇疆鐨勬枃鏈 // * @param index 璧峰绱㈠紩
|
||
|
// * @param count 鏂囨湰鏁伴噺
|
||
|
// * @return
|
||
|
// */
|
||
|
// QList<WsChar> getText(uint32_t index, uint32_t count);
|
||
|
// /**
|
||
|
// * @brief 鍒犻櫎鎸囧畾浣嶇疆鐨勬枃鏈 // * @param index 璧峰绱㈠紩锛岃秴鍑烘湁鏁堣寖鍥存墽琛屾棤鏁堟灉
|
||
|
// * @param count 鍒犻櫎鏁伴噺
|
||
|
// */
|
||
|
// void delText(uint32_t index, uint32_t count);
|
||
|
|
||
|
// // 閫夋嫨鎿嶄綔鎺ュ彛 ======================================================
|
||
|
// void select(uint32_t index, uint32_t count);
|
||
|
|
||
|
// 鍐呭瓨鏂囨湰瀛樺彇鎺ュ彛 ===================================================
|
||
|
QString toPlainText() const;
|
||
|
void setPlainText(const QString &text);
|
||
|
|
||
|
// signals:
|
||
|
// void blockHasbeenInserted(uint32_t index);
|
||
|
// /**
|
||
|
// * @brief 鐗囨灏嗚琚垹闄 // * @param index
|
||
|
// */
|
||
|
// void blockAboutTobeDelete(uint32_t index);
|
||
|
// /**
|
||
|
// * @brief 鐗囨宸茬粡琚慨鏀 // * @param index
|
||
|
// */
|
||
|
// void blockHasbeenChanged(uint32_t index);
|
||
|
|
||
|
// void redoAvaliable();
|
||
|
// void undoAvaliable();
|
||
|
// /**
|
||
|
// * @brief 鍏夋爣浣嶇疆宸茬粡鍙樺寲
|
||
|
// * @param index 鍏夋爣浣嶇疆
|
||
|
// */
|
||
|
// void cursorPosChanged(uint32_t index);
|
||
|
// void selectionChanged(uint32_t index, uint32_t count);
|
||
|
};
|
||
|
}
|
||
|
|
||
|
#endif // TEXT_PRESENT_H
|