| 1 |
/*************************************************************************** |
| 2 |
* Copyright (c) 2004 Werner Mayer <wmayer[at]users.sourceforge.net> * |
| 3 |
* * |
| 4 |
* This file is part of the FreeCAD CAx development system. * |
| 5 |
* * |
| 6 |
* This library is free software; you can redistribute it and/or * |
| 7 |
* modify it under the terms of the GNU Library General Public * |
| 8 |
* License as published by the Free Software Foundation; either * |
| 9 |
* version 2 of the License, or (at your option) any later version. * |
| 10 |
* * |
| 11 |
* This library is distributed in the hope that it will be useful, * |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 14 |
* GNU Library General Public License for more details. * |
| 15 |
* * |
| 16 |
* You should have received a copy of the GNU Library General Public * |
| 17 |
* License along with this library; see the file COPYING.LIB. If not, * |
| 18 |
* write to the Free Software Foundation, Inc., 59 Temple Place, * |
| 19 |
* Suite 330, Boston, MA 02111-1307, USA * |
| 20 |
* * |
| 21 |
***************************************************************************/ |
| 22 |
|
| 23 |
/* Text completion mechanism */ |
| 24 |
|
| 25 |
#ifndef GUI_TEXTEDIT_H |
| 26 |
#define GUI_TEXTEDIT_H |
| 27 |
|
| 28 |
#include <QListWidget> |
| 29 |
#include <QPlainTextEdit> |
| 30 |
|
| 31 |
#include "CallTips.h" |
| 32 |
#include "Window.h" |
| 33 |
|
| 34 |
|
| 35 |
namespace Gui |
| 36 |
{ |
| 37 |
class CompletionBox; |
| 38 |
class SyntaxHighlighter; |
| 39 |
|
| 40 |
/** |
| 41 |
* Completion is a means by which an editor automatically completes words that the user is typing. |
| 42 |
* For example, in a code editor, a programmer might type "sur", then Tab, and the editor will |
| 43 |
* complete the word the programmer was typing so that "sur" is replaced by "surnameLineEdit". This |
| 44 |
* is very useful for text that contains long words or variable names. The completion mechanism |
| 45 |
* usually works by looking at the existing text to see if any words begin with what the user has |
| 46 |
* typed, and in most editors completion is invoked by a special key sequence. |
| 47 |
* |
| 48 |
* TextEdit can detect a special key sequence to invoke the completion mechanism, and can handle |
| 49 |
* three different situations: |
| 50 |
* \li There are no possible completions. |
| 51 |
* \li There is a single possible completion. |
| 52 |
* \li There are two or more possible completions. |
| 53 |
* |
| 54 |
* \remark The original sources are taken from Qt Quarterly (Customizing for Completion). |
| 55 |
* @author Werner Mayer |
| 56 |
*/ |
| 57 |
class CompletionList; |
| 58 |
class GuiExport TextEdit: public QPlainTextEdit |
| 59 |
{ |
| 60 |
Q_OBJECT |
| 61 |
|
| 62 |
public: |
| 63 |
explicit TextEdit(QWidget* parent = nullptr); |
| 64 |
~TextEdit() override; |
| 65 |
|
| 66 |
//! Get the cursor position of the current line being edited |
| 67 |
virtual int getInputStringPosition(); |
| 68 |
//! Get the text of the current line being edited |
| 69 |
virtual QString getInputString(); |
| 70 |
|
| 71 |
private Q_SLOTS: |
| 72 |
void complete(); |
| 73 |
|
| 74 |
Q_SIGNALS: |
| 75 |
void showSearchBar(); |
| 76 |
void findNext(); |
| 77 |
void findPrevious(); |
| 78 |
|
| 79 |
protected: |
| 80 |
void keyPressEvent(QKeyEvent*) override; |
| 81 |
void wheelEvent(QWheelEvent* e) override; |
| 82 |
CallTipsList* callTipsList = nullptr; |
| 83 |
|
| 84 |
private: |
| 85 |
void createListBox(); |
| 86 |
|
| 87 |
private: |
| 88 |
QString wordPrefix; |
| 89 |
int cursorPosition; |
| 90 |
CompletionList* listBox; |
| 91 |
}; |
| 92 |
|
| 93 |
class SyntaxHighlighter; |
| 94 |
class GuiExport TextEditor: public TextEdit, public WindowParameter |
| 95 |
{ |
| 96 |
Q_OBJECT |
| 97 |
|
| 98 |
public: |
| 99 |
explicit TextEditor(QWidget* parent = nullptr); |
| 100 |
~TextEditor() override; |
| 101 |
void setSyntaxHighlighter(SyntaxHighlighter*); |
| 102 |
|
| 103 |
void OnChange(Base::Subject<const char*>& rCaller, const char* rcReason) override; |
| 104 |
|
| 105 |
/** Draw a beam in the line where the cursor is. */ |
| 106 |
void lineNumberAreaPaintEvent(QPaintEvent*); |
| 107 |
int lineNumberAreaWidth(); |
| 108 |
void setVisibleLineNumbers(bool value); |
| 109 |
bool isVisibleLineNumbers() const; |
| 110 |
void setEnabledHighlightCurrentLine(bool value); |
| 111 |
bool isEnabledHighlightCurrentLine() const; |
| 112 |
|
| 113 |
private Q_SLOTS: |
| 114 |
void updateLineNumberAreaWidth(int newBlockCount); |
| 115 |
void updateLineNumberArea(const QRect&, int); |
| 116 |
void highlightCurrentLine(); |
| 117 |
|
| 118 |
protected: |
| 119 |
void resizeEvent(QResizeEvent* e) override; |
| 120 |
QWidget* getMarker() const |
| 121 |
{ |
| 122 |
return lineNumberArea; |
| 123 |
} |
| 124 |
virtual void drawMarker(int line, int x, int y, QPainter*); |
| 125 |
|
| 126 |
private: |
| 127 |
SyntaxHighlighter* highlighter; |
| 128 |
QWidget* lineNumberArea; |
| 129 |
struct TextEditorP* d; |
| 130 |
|
| 131 |
friend class SyntaxHighlighter; |
| 132 |
}; |
| 133 |
|
| 134 |
/** subclass of TextEditor that serves as base class for the |
| 135 |
* python editor and the python console where we handle |
| 136 |
* the tab key conversion to spaces, depending on user settings |
| 137 |
*/ |
| 138 |
class GuiExport PythonTextEditor: public TextEditor |
| 139 |
{ |
| 140 |
Q_OBJECT |
| 141 |
public: |
| 142 |
explicit PythonTextEditor(QWidget* parent = nullptr); |
| 143 |
~PythonTextEditor() override; |
| 144 |
|
| 145 |
public Q_SLOTS: |
| 146 |
/** Inserts \a str at the beginning of each selected line or the current line if |
| 147 |
* nothing is selected |
| 148 |
*/ |
| 149 |
void prepend(const QString& str); |
| 150 |
/** Removes \a str from the beginning of each selected line or the current line if |
| 151 |
* nothing is selected |
| 152 |
*/ |
| 153 |
void remove(const QString& str); |
| 154 |
|
| 155 |
protected: |
| 156 |
void keyPressEvent(QKeyEvent*) override; |
| 157 |
}; |
| 158 |
|
| 159 |
|
| 160 |
class LineMarker: public QWidget |
| 161 |
{ |
| 162 |
Q_OBJECT |
| 163 |
|
| 164 |
public: |
| 165 |
explicit LineMarker(TextEditor* editor); |
| 166 |
~LineMarker() override; |
| 167 |
|
| 168 |
QSize sizeHint() const override; |
| 169 |
|
| 170 |
protected: |
| 171 |
void paintEvent(QPaintEvent*) override; |
| 172 |
|
| 173 |
private: |
| 174 |
TextEditor* textEditor; |
| 175 |
}; |
| 176 |
|
| 177 |
/** |
| 178 |
* The CompletionList class provides a list box that pops up in a text edit if the user has pressed |
| 179 |
* an accelerator to complete the current word they are typing in. |
| 180 |
* @author Werner Mayer |
| 181 |
*/ |
| 182 |
class CompletionList: public QListWidget |
| 183 |
{ |
| 184 |
Q_OBJECT |
| 185 |
|
| 186 |
public: |
| 187 |
/// Construction |
| 188 |
explicit CompletionList(QPlainTextEdit* parent); |
| 189 |
/// Destruction |
| 190 |
~CompletionList() override; |
| 191 |
|
| 192 |
void findCurrentWord(const QString&); |
| 193 |
|
| 194 |
protected: |
| 195 |
bool eventFilter(QObject*, QEvent*) override; |
| 196 |
|
| 197 |
private Q_SLOTS: |
| 198 |
void completionItem(QListWidgetItem* item); |
| 199 |
|
| 200 |
private: |
| 201 |
QPlainTextEdit* textEdit; |
| 202 |
}; |
| 203 |
|
| 204 |
} // namespace Gui |
| 205 |
|
| 206 |
#endif // GUI_TEXTEDIT_H |