1 /***************************************************************************
2  *   copyright       : (C) 2008 by Benito van der Zander                   *
3  *   http://www.xm1math.net/texmaker/                                      *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) any later version.                                   *
9  *                                                                         *
10  ***************************************************************************/
11 
12 #ifndef Header_Latex_Completer
13 #define Header_Latex_Completer
14 
15 #include "mostQtHeaders.h"
16 
17 #include "codesnippet.h"
18 
19 #include "qcodeedit.h"
20 #include "qeditor.h"
21 #include "directoryreader.h"
22 #include "bibtexreader.h"
23 #include <set>
24 
25 class CompletionListModel;
26 class LatexCompleterConfig;
27 class LatexReference;
28 class LatexParser;
29 
30 /*!
31  * \brief Implements the actual completer
32  *
33  * It uses codesnippet, also called completionwords for inserting into text.
34  * \see Codesnippet
35  */
36 class LatexCompleter : public QObject
37 {
38 	Q_OBJECT
39 
40 public:
41 	enum CompletionFlag {CF_FORCE_VISIBLE_LIST = 1, ///< force visible of completer
42 						 CF_NORMAL_TEXT = 2, ///< complete normal text words
43 						 CF_FORCE_REF = 4, ///< completes labels in a reference, e.g. \ref{label}
44 						 CF_OVERRIDEN_BACKSLASH = 8,
45 						 CF_FORCE_GRAPHIC = 16, ///< complete a filename for an image
46 						 CF_FORCE_CITE = 32, ///< complete a citation
47 						 CF_FORCE_PACKAGE = 64, ///< complete a package name
48 						 CF_FORCE_KEYVAL = 128, ///< complete key/value pair
49 						 CF_FORCE_SPECIALOPTION = 256,
50 						 CF_FORCE_LENGTH = 512,
51 						 CF_FORCE_REFLIST = 1024};
52 	Q_DECLARE_FLAGS(CompletionFlags, CompletionFlag)
53 
54     LatexCompleter(const LatexParser &latexParser, QObject *p = nullptr); ///< constructor
55 	virtual ~LatexCompleter();
56 
57 	void complete(QEditor *newEditor, const CompletionFlags &flags); ///< initiate completion with given flags
58 	void setAdditionalWords(const CodeSnippetList &newwords, CompletionType completionType = CT_COMMANDS);
59 	void setAdditionalWords(const QSet<QString> &newwords, CompletionType completionType);
60     void setAdditionalWords(const std::set<QString> &newwords, CompletionType completionType);
61 	void setKeyValWords(const QString &name, const QSet<QString> &newwords);
62 	void setContextWords(const QSet<QString> &newwords, const QString &context);
63 	void updateAbbreviations();
64 
setLatexReference(LatexReference * ref)65 	static void setLatexReference(LatexReference *ref) { latexReference = ref; } ///< set latexreference which is used for showing help per tooltip on selecetd completion commands
getLatexReference()66 	static LatexReference *getLatexReference() { return latexReference; } ///< get used latexreference
67 
68 	bool acceptTriggerString(const QString &trigger);
69 
70 	void setConfig(LatexCompleterConfig *config);
71 	LatexCompleterConfig *getConfig() const;
72 
73     void setPackageList(std::set<QString> *lst); ///< set a list with available latex package names
74 
75 	bool close(); ///< close completer (without insertion)
isVisible()76 	bool isVisible() { return list->isVisible(); }
77 	bool existValues(); ///< are still completion ssuggestions available
78 
setWorkPath(const QString cwd)79 	void setWorkPath(const QString cwd) { workingDir = cwd; }
completingGraphic()80 	bool completingGraphic() { return forcedGraphic; }
completingKey()81 	bool completingKey() { return forcedKeyval; }
82 
83 	int countWords();
84 	void setTab(int index); ///< bring given 'tab' to front
85 
86 	void insertText(QString txt); ///< insert 'txt'
87 
88 	void showTooltip(QString text); ///< show tooltip
89 
90 signals:
91 	void setDirectoryForCompletion(QString fn); ///< set the used directory for filename completion
92 	void searchBibtexSection(QString file, QString bibId);
93 	void showImagePreview(QString fn); ///< show preview of selected image
94 	void showPreview(QString text); ///< show preview of selected item, usually references or citations
95 
96 public slots:
97 
98     void bibtexSectionFound(QString content);
99 
100 private:
101 	friend class CompleterInputBinding;
102 	friend class CompletionListModel;
103 	static LatexCompleterConfig *config;
104 	const LatexParser &latexParser;
105 	int maxWordLen;
106 	QListView *list;
107 	CompletionListModel *listModel;
108 	directoryReader *dirReader;
109 	QEditor *editor;
110 
111     std::set<QString> *packageList;
112 
113 	QWidget *widget;
114 	QTabBar *tbBelow, *tbAbove;
115 
116 	bool editorAutoCloseChars;
117 
118 	void filterList(QString word, int showMostUsed = -1);
119 	bool acceptChar(QChar c, int pos);
120 	void adjustWidget();
121 
122 	static LatexReference *latexReference;
123 
124 	bool forcedRef;
125 	bool forcedGraphic;
126 	bool forcedCite;
127 	bool forcedPackage;
128 	bool forcedKeyval;
129 	bool forcedSpecialOption;
130 	bool forcedLength;
131 	bool startedFromTriggerKey;
132 	QString workingDir;
133 
134 	QPoint lastPos;
135 	bibtexReader *bibReader;
136 
137 private slots:
138 	void cursorPositionChanged();
139 	void selectionChanged(const QModelIndex &index);
140 	void editorDestroyed();
141 	void changeView(int pos);
142 	void listClicked(QModelIndex index);
143 	void directoryLoaded(QString dn, QSet<QString> content);
144 };
145 
146 #endif
147