File: tablemanipulation.h

package info (click to toggle)
texstudio 2.11.2%2Bdebian-1
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 41,292 kB
  • ctags: 12,405
  • sloc: cpp: 93,072; xml: 10,217; ansic: 4,153; sh: 145; makefile: 56
file content (108 lines) | stat: -rw-r--r-- 3,972 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#ifndef TABLEMANIPULATION_H
#define TABLEMANIPULATION_H

#include "mostQtHeaders.h"
#include "qdocument.h"

class QEditor;
class LatexEditorView;

class LatexTables
{
public:
	static void addRow(QDocumentCursor &c, const int numberOfColumns );
	static void addColumn(QDocument *doc, const int lineNumber, const int afterColumn, QStringList *cutBuffer = 0);
	static void removeColumn(QDocument *doc, const int lineNumber, const int column, QStringList *cutBuffer = 0);
	static void removeRow(QDocumentCursor &c);
	static int findNextToken(QDocumentCursor &cur, QStringList tokens, bool keepAnchor = false, bool backwards = false);
	static int getColumn(QDocumentCursor &cur);
	static QString getDef(QDocumentCursor &cur);
	static QString getSimplifiedDef(QDocumentCursor &cur);
	static int getNumberOfColumns(QDocumentCursor &cur);
	static int getNumberOfColumns(QStringList values);
	static bool inTableEnv(QDocumentCursor &cur);
	static int getNumOfColsInMultiColumn(const QString &str, QString *outAlignment = 0, QString *outText = 0);
	static int incNumOfColsInMultiColumn(const QString &str, int add);
	static void addHLine(QDocumentCursor &c, const int numberOfLines = -1, const bool remove = false);
	static QStringList splitColDef(QString def);
	static void simplifyColDefs(QStringList &colDefs);
	static void executeScript(QString script, LatexEditorView *edView);
	static void generateTableFromTemplate(LatexEditorView *edView, QString templateFileName, QString def, QList<QStringList> table, QString env, QString width);
	static QString getTableText(QDocumentCursor &cur);
	static void alignTableCols(QDocumentCursor &cur);

	static QStringList tabularNames;
	static QStringList tabularNamesWithOneOption;
	static QStringList mathTables;
};


class LatexTableLine : public QObject
{
	Q_OBJECT

public:
	LatexTableLine(QObject *parent = 0);

	enum MultiColFlag {MCNone, MCStart, MCMid, MCEnd};

	void setMetaLine(const QString line) { metaLine = line; }
	void setColLine(const QString line);
	void setLineBreakOption(const QString opt) { lineBreakOption = opt; }

	int colWidth(int col) const { return cols.at(col).length(); }
	int colCount() const { return cols.count(); }
	MultiColFlag multiColState(int col) { return mcFlag.at(col); }
	QChar multiColAlign(int col) {  return mcAlign.at(col); }
	int multiColStart(int col) {
		for (; col >= 0; col--) if (mcFlag.at(col) == MCStart) return col;
		return -1;
	}
	QString toMetaLine() const { return metaLine; }
	QString toColLine() const { return colLine; }
	QString toLineBreakOption() const { return lineBreakOption; }
	QString colText(int col) const { return cols.at(col); }
	QString colText(int col, int width, const QChar &alignment);

private:
	void appendCol(const QString &col);

	QString colLine;
	QString metaLine;
	QString lineBreakOption;

	QStringList cols;
	QList<MultiColFlag> mcFlag;
	QList<QChar> mcAlign;
};
Q_DECLARE_METATYPE(LatexTableLine::MultiColFlag)


class LatexTableModel : public QAbstractTableModel
{
	Q_OBJECT

#ifndef QT_NO_DEBUG
	friend class TableManipulationTest;
#endif
public:
	LatexTableModel(QObject *parent = 0);


	void setContent(const QString &text);
	QStringList getAlignedLines(const QStringList alignment, const QString &rowIndent = "\t") const;

	int rowCount(const QModelIndex &parent = QModelIndex()) const { Q_UNUSED(parent) return lines.count(); }
	int columnCount(const QModelIndex &parent = QModelIndex()) const { Q_UNUSED(parent) return (lines.count() > 0) ? lines.at(0)->colCount() : 0; }
	QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;

private:
	int findRowBreak(const QString &text, int startCol) const;
	LatexTableLine *parseNextLine(const QString &text, int &startCol);

	QStringList metaLineCommands;
	int metaLineCommandPos;
	QList<LatexTableLine *> lines;
	QStringList parseErrors;
};
#endif // TABLEMANIPULATION_H