| 1 |
/*************************************************************************** |
| 2 |
* Copyright (c) 2015 Eivind Kvedalen <[email protected]> * |
| 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 |
#ifndef EXPRESSIONCOMPLETER_H |
| 24 |
#define EXPRESSIONCOMPLETER_H |
| 25 |
|
| 26 |
#include <QCompleter> |
| 27 |
#include <QLineEdit> |
| 28 |
#include <QObject> |
| 29 |
#include <QPlainTextEdit> |
| 30 |
#include <QValidator> |
| 31 |
#include <App/DocumentObserver.h> |
| 32 |
#include <App/ExpressionTokenizer.h> |
| 33 |
|
| 34 |
class QStandardItem; |
| 35 |
|
| 36 |
namespace App |
| 37 |
{ |
| 38 |
class Document; |
| 39 |
class DocumentObject; |
| 40 |
class Property; |
| 41 |
class ObjectIdentifier; |
| 42 |
} // namespace App |
| 43 |
|
| 44 |
namespace Gui |
| 45 |
{ |
| 46 |
|
| 47 |
enum class ActivationMode |
| 48 |
{ |
| 49 |
Highlighted, // browsing with arrow keys |
| 50 |
Activated // confirmed selection (Enter/Click) |
| 51 |
}; |
| 52 |
|
| 53 |
class GuiExport ExpressionValidator: public QValidator |
| 54 |
{ |
| 55 |
Q_OBJECT |
| 56 |
|
| 57 |
public: |
| 58 |
explicit ExpressionValidator(QObject* parent = nullptr); |
| 59 |
void fixup(QString& input) const override; |
| 60 |
QValidator::State validate(QString& input, int& pos) const override; |
| 61 |
}; |
| 62 |
|
| 63 |
/** |
| 64 |
* @brief The ExpressionCompleter class extends the QCompleter class to provide a completer model of |
| 65 |
* documentobject names and properties. |
| 66 |
*/ |
| 67 |
|
| 68 |
class GuiExport ExpressionCompleter: public QCompleter |
| 69 |
{ |
| 70 |
Q_OBJECT |
| 71 |
public: |
| 72 |
ExpressionCompleter( |
| 73 |
const App::DocumentObject* currentDocObj, |
| 74 |
QObject* parent = nullptr, |
| 75 |
bool noProperty = false, |
| 76 |
bool checkInList = true |
| 77 |
); |
| 78 |
|
| 79 |
void getPrefixRange(int& start, int& end) const |
| 80 |
{ |
| 81 |
tokenizer.getPrefixRange(start, end); |
| 82 |
} |
| 83 |
|
| 84 |
void updatePrefixEnd(int end) |
| 85 |
{ |
| 86 |
tokenizer.updatePrefixEnd(end); |
| 87 |
} |
| 88 |
|
| 89 |
void setDocumentObject(const App::DocumentObject*, bool checkInList = true); |
| 90 |
|
| 91 |
void setNoProperty(bool enabled = true); |
| 92 |
|
| 93 |
public Q_SLOTS: |
| 94 |
void slotUpdate(const QString& prefix, int pos); |
| 95 |
|
| 96 |
Q_SIGNALS: |
| 97 |
void completerSlotUpdated(); |
| 98 |
|
| 99 |
private: |
| 100 |
void init(); |
| 101 |
QString pathFromIndex(const QModelIndex& index) const override; |
| 102 |
QStringList splitPath(const QString& input) const override; |
| 103 |
|
| 104 |
App::DocumentObjectT currentObj; |
| 105 |
App::ExpressionTokenizer tokenizer; |
| 106 |
bool noProperty; |
| 107 |
bool checkInList; |
| 108 |
}; |
| 109 |
|
| 110 |
class GuiExport ExpressionLineEdit: public QLineEdit |
| 111 |
{ |
| 112 |
Q_OBJECT |
| 113 |
Q_PROPERTY(ExpressionCompleter* completer READ getCompleter) |
| 114 |
public: |
| 115 |
ExpressionLineEdit( |
| 116 |
QWidget* parent = nullptr, |
| 117 |
bool noProperty = false, |
| 118 |
char checkPrefix = 0, |
| 119 |
bool checkInList = true |
| 120 |
); |
| 121 |
void setDocumentObject(const App::DocumentObject* currentDocObj, bool checkInList = true); |
| 122 |
void setPrefix(char prefix); |
| 123 |
bool completerActive() const; |
| 124 |
void hideCompleter(); |
| 125 |
void setNoProperty(bool enabled = true); |
| 126 |
void setExactMatch(bool enabled = true); |
| 127 |
ExpressionCompleter* getCompleter(void); |
| 128 |
Q_SIGNALS: |
| 129 |
void textChanged2(QString text, int pos); |
| 130 |
public Q_SLOTS: |
| 131 |
void slotTextChanged(const QString& text); |
| 132 |
void slotCompleteText(const QString& completionPrefix, ActivationMode mode); |
| 133 |
void slotCompleteTextHighlighted(const QString& completionPrefix); |
| 134 |
void slotCompleteTextSelected(const QString& completionPrefix); |
| 135 |
|
| 136 |
protected: |
| 137 |
void keyPressEvent(QKeyEvent* event) override; |
| 138 |
void contextMenuEvent(QContextMenuEvent* event) override; |
| 139 |
|
| 140 |
private: |
| 141 |
ExpressionCompleter* completer; |
| 142 |
bool block; |
| 143 |
bool noProperty; |
| 144 |
bool exactMatch; |
| 145 |
bool checkInList; |
| 146 |
char checkPrefix; |
| 147 |
}; |
| 148 |
|
| 149 |
class GuiExport ExpressionTextEdit: public QPlainTextEdit |
| 150 |
{ |
| 151 |
Q_OBJECT |
| 152 |
public: |
| 153 |
ExpressionTextEdit(QWidget* parent = nullptr); |
| 154 |
void setDocumentObject(const App::DocumentObject* currentDocObj); |
| 155 |
bool completerActive() const; |
| 156 |
void hideCompleter(); |
| 157 |
void setExactMatch(bool enabled = true); |
| 158 |
QSize sizeHint() const override; |
| 159 |
|
| 160 |
protected: |
| 161 |
void keyPressEvent(QKeyEvent* event) override; |
| 162 |
void contextMenuEvent(QContextMenuEvent* event) override; |
| 163 |
Q_SIGNALS: |
| 164 |
void textChanged2(QString text, int pos); |
| 165 |
public Q_SLOTS: |
| 166 |
void slotTextChanged(); |
| 167 |
void slotCompleteText(const QString& completionPrefix, ActivationMode mode); |
| 168 |
void slotCompleteTextHighlighted(const QString& completionPrefix); |
| 169 |
void slotCompleteTextSelected(const QString& completionPrefix); |
| 170 |
void adjustCompleterToCursor(); |
| 171 |
|
| 172 |
private: |
| 173 |
ExpressionCompleter* completer; |
| 174 |
bool block; |
| 175 |
bool exactMatch; |
| 176 |
}; |
| 177 |
|
| 178 |
class GuiExport ExpressionParameter |
| 179 |
{ |
| 180 |
public: |
| 181 |
static ExpressionParameter* instance(); |
| 182 |
bool isCaseSensitive() const; |
| 183 |
bool isExactMatch() const; |
| 184 |
}; |
| 185 |
|
| 186 |
} // namespace Gui |
| 187 |
|
| 188 |
#endif // EXPRESSIONCOMPLETER_H |