| 1 |
/*************************************************************************** |
| 2 |
* Copyright (c) 2007 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 |
#include <limits> |
| 24 |
|
| 25 |
#include <QApplication> |
| 26 |
#include <QKeyEvent> |
| 27 |
#include <QLabel> |
| 28 |
#include <QPlainTextEdit> |
| 29 |
#include <QRegularExpression> |
| 30 |
#include <QRegularExpressionMatch> |
| 31 |
#include <QTextCursor> |
| 32 |
#include <QToolTip> |
| 33 |
|
| 34 |
#include <App/Property.h> |
| 35 |
#include <App/PropertyContainer.h> |
| 36 |
#include <App/PropertyContainerPy.h> |
| 37 |
#include <App/Document.h> |
| 38 |
#include <App/DocumentObject.h> |
| 39 |
#include <App/DocumentPy.h> |
| 40 |
#include <App/DocumentObjectPy.h> |
| 41 |
#include <Base/Console.h> |
| 42 |
#include <Base/Interpreter.h> |
| 43 |
#include <Base/PyObjectBase.h> |
| 44 |
#include <Gui/BitmapFactory.h> |
| 45 |
#include <Gui/DocumentPy.h> |
| 46 |
|
| 47 |
#include "CallTips.h" |
| 48 |
|
| 49 |
|
| 50 |
Q_DECLARE_METATYPE(Gui::CallTip) //< allows use of QVariant |
| 51 |
|
| 52 |
namespace Gui |
| 53 |
{ |
| 54 |
|
| 55 |
/** |
| 56 |
* template class Temporary. |
| 57 |
* Allows variable changes limited to a scope. |
| 58 |
*/ |
| 59 |
template<typename TYPE> |
| 60 |
class Temporary |
| 61 |
{ |
| 62 |
public: |
| 63 |
Temporary(TYPE& var, const TYPE tmpVal) |
| 64 |
: _var(var) |
| 65 |
, _saveVal(var) |
| 66 |
{ |
| 67 |
var = tmpVal; |
| 68 |
} |
| 69 |
|
| 70 |
~Temporary() |
| 71 |
{ |
| 72 |
_var = _saveVal; |
| 73 |
} |
| 74 |
|
| 75 |
private: |
| 76 |
TYPE& _var; |
| 77 |
TYPE _saveVal; |
| 78 |
}; |
| 79 |
|
| 80 |
} /* namespace Gui */ |
| 81 |
|
| 82 |
using namespace Gui; |
| 83 |
|
| 84 |
CallTipsList::CallTipsList(QPlainTextEdit* parent) |
| 85 |
: QListWidget(parent) |
| 86 |
, textEdit(parent) |
| 87 |
, cursorPos(0) |
| 88 |
, validObject(true) |
| 89 |
, doCallCompletion(false) |
| 90 |
{ |
| 91 |
// make the user assume that the widget is active |
| 92 |
QPalette pal = parent->palette(); |
| 93 |
pal.setColor( |
| 94 |
QPalette::Inactive, |
| 95 |
QPalette::Highlight, |
| 96 |
pal.color(QPalette::Active, QPalette::Highlight) |
| 97 |
); |
| 98 |
pal.setColor( |
| 99 |
QPalette::Inactive, |
| 100 |
QPalette::HighlightedText, |
| 101 |
pal.color(QPalette::Active, QPalette::HighlightedText) |
| 102 |
); |
| 103 |
parent->setPalette(pal); |
| 104 |
|
| 105 |
connect(this, &QListWidget::itemActivated, this, &CallTipsList::callTipItemActivated); |
| 106 |
|
| 107 |
hideKeys.append(Qt::Key_Space); |
| 108 |
hideKeys.append(Qt::Key_Exclam); |
| 109 |
hideKeys.append(Qt::Key_QuoteDbl); |
| 110 |
hideKeys.append(Qt::Key_NumberSign); |
| 111 |
hideKeys.append(Qt::Key_Dollar); |
| 112 |
hideKeys.append(Qt::Key_Percent); |
| 113 |
hideKeys.append(Qt::Key_Ampersand); |
| 114 |
hideKeys.append(Qt::Key_Apostrophe); |
| 115 |
hideKeys.append(Qt::Key_Asterisk); |
| 116 |
hideKeys.append(Qt::Key_Plus); |
| 117 |
hideKeys.append(Qt::Key_Comma); |
| 118 |
hideKeys.append(Qt::Key_Minus); |
| 119 |
hideKeys.append(Qt::Key_Period); |
| 120 |
hideKeys.append(Qt::Key_Slash); |
| 121 |
hideKeys.append(Qt::Key_Colon); |
| 122 |
hideKeys.append(Qt::Key_Semicolon); |
| 123 |
hideKeys.append(Qt::Key_Less); |
| 124 |
hideKeys.append(Qt::Key_Equal); |
| 125 |
hideKeys.append(Qt::Key_Greater); |
| 126 |
hideKeys.append(Qt::Key_Question); |
| 127 |
hideKeys.append(Qt::Key_At); |
| 128 |
hideKeys.append(Qt::Key_Backslash); |
| 129 |
|
| 130 |
compKeys.append(Qt::Key_ParenLeft); |
| 131 |
compKeys.append(Qt::Key_ParenRight); |
| 132 |
compKeys.append(Qt::Key_BracketLeft); |
| 133 |
compKeys.append(Qt::Key_BracketRight); |
| 134 |
compKeys.append(Qt::Key_BraceLeft); |
| 135 |
compKeys.append(Qt::Key_BraceRight); |
| 136 |
} |
| 137 |
|
| 138 |
CallTipsList::~CallTipsList() = default; |
| 139 |
|
| 140 |
void CallTipsList::keyboardSearch(const QString& wordPrefix) |
| 141 |
{ |
| 142 |
// first search for the item that matches perfectly |
| 143 |
for (int i = 0; i < count(); ++i) { |
| 144 |
QString text = item(i)->text(); |
| 145 |
if (text.startsWith(wordPrefix)) { |
| 146 |
setCurrentRow(i); |
| 147 |
return; |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
// now do a case insensitive comparison |
| 152 |
for (int i = 0; i < count(); ++i) { |
| 153 |
QString text = item(i)->text(); |
| 154 |
if (text.startsWith(wordPrefix, Qt::CaseInsensitive)) { |
| 155 |
setCurrentRow(i); |
| 156 |
return; |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
if (currentItem()) { |
| 161 |
currentItem()->setSelected(false); |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
void CallTipsList::validateCursor() |
| 166 |
{ |
| 167 |
QTextCursor cursor = textEdit->textCursor(); |
| 168 |
int currentPos = cursor.position(); |
| 169 |
if (currentPos < this->cursorPos) { |
| 170 |
hide(); |
| 171 |
} |
| 172 |
else { |
| 173 |
cursor.setPosition(this->cursorPos); |
| 174 |
cursor.movePosition(QTextCursor::EndOfWord, QTextCursor::KeepAnchor); |
| 175 |
QString word = cursor.selectedText(); |
| 176 |
if (!word.isEmpty()) { |
| 177 |
// the following text might be an operator, brackets, ... |
| 178 |
const QChar underscore = QLatin1Char('_'); |
| 179 |
const QChar ch = word.at(0); |
| 180 |
if (!ch.isLetterOrNumber() && ch != underscore) { |
| 181 |
word.clear(); |
| 182 |
} |
| 183 |
} |
| 184 |
if (currentPos > this->cursorPos + word.length()) { |
| 185 |
hide(); |
| 186 |
} |
| 187 |
else if (!word.isEmpty()) { |
| 188 |
// If the word is empty we should not allow one to do a search, |
| 189 |
// otherwise we may select the next item which is not okay in this |
| 190 |
// context. This might happen if e.g. Shift is pressed. |
| 191 |
keyboardSearch(word); |
| 192 |
} |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
QString CallTipsList::extractContext(const QString& line) const |
| 197 |
{ |
| 198 |
int len = line.size(); |
| 199 |
int index = len - 1; |
| 200 |
for (int i = 0; i < len; i++) { |
| 201 |
int pos = len - 1 - i; |
| 202 |
const char ch = line.at(pos).toLatin1(); |
| 203 |
if ((ch >= 48 && ch <= 57) || // Numbers |
| 204 |
(ch >= 65 && ch <= 90) || // Uppercase letters |
| 205 |
(ch >= 97 && ch <= 122) || // Lowercase letters |
| 206 |
(ch == '.') || (ch == '_') || // dot or underscore |
| 207 |
(ch == ' ') || (ch == '\t')) { // whitespace (between dot and text) |
| 208 |
index = pos; |
| 209 |
} |
| 210 |
else { |
| 211 |
break; |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
return line.mid(index); |
| 216 |
} |
| 217 |
|
| 218 |
QMap<QString, CallTip> CallTipsList::extractTips(const QString& context) const |
| 219 |
{ |
| 220 |
Base::PyGILStateLocker lock; |
| 221 |
QMap<QString, CallTip> tips; |
| 222 |
if (context.isEmpty()) { |
| 223 |
return tips; |
| 224 |
} |
| 225 |
|
| 226 |
try { |
| 227 |
PyErr_Clear(); |
| 228 |
Py::Module module("__main__"); |
| 229 |
if (module.ptr() == nullptr) { |
| 230 |
return tips; |
| 231 |
} |
| 232 |
Py::Dict dict = module.getDict(); |
| 233 |
|
| 234 |
// this is used to filter out input of the form "1." |
| 235 |
QStringList items = context.split(QLatin1Char('.')); |
| 236 |
QString modname = items.front(); |
| 237 |
items.pop_front(); |
| 238 |
if (!dict.hasKey(std::string(modname.toLatin1()))) { |
| 239 |
return tips; // unknown object |
| 240 |
} |
| 241 |
// Don't use hasattr & getattr because if a property is bound to a method this will be |
| 242 |
// executed twice. |
| 243 |
PyObject* code = Py_CompileString( |
| 244 |
static_cast<const char*>(context.toLatin1()), |
| 245 |
"<CallTipsList>", |
| 246 |
Py_eval_input |
| 247 |
); |
| 248 |
if (!code) { |
| 249 |
PyErr_Clear(); |
| 250 |
return tips; |
| 251 |
} |
| 252 |
|
| 253 |
PyObject* eval = nullptr; |
| 254 |
if (PyCode_Check(code)) { |
| 255 |
eval = PyEval_EvalCode(code, dict.ptr(), dict.ptr()); |
| 256 |
} |
| 257 |
Py_DECREF(code); |
| 258 |
if (!eval) { |
| 259 |
PyErr_Clear(); |
| 260 |
return tips; |
| 261 |
} |
| 262 |
Py::Object obj(eval, true); |
| 263 |
|
| 264 |
// Checks whether the type is a subclass of PyObjectBase because to get the doc string |
| 265 |
// of a member we must get it by its type instead of its instance otherwise we get the |
| 266 |
// wrong string, namely that of the type of the member. |
| 267 |
// Note: 3rd party libraries may use their own type object classes so that we cannot |
| 268 |
// reliably use Py::Type. To be on the safe side we should use Py::Object to assign |
| 269 |
// the used type object to. |
| 270 |
// Py::Object type = obj.type(); |
| 271 |
Py::Object type(PyObject_Type(obj.ptr()), true); |
| 272 |
Py::Object inst = obj; // the object instance |
| 273 |
PyObject* typeobj = Base::getTypeAsObject(&Base::PyObjectBase::Type); |
| 274 |
PyObject* typedoc = Base::getTypeAsObject(&App::DocumentObjectPy::Type); |
| 275 |
PyObject* basetype = Base::getTypeAsObject(&PyBaseObject_Type); |
| 276 |
|
| 277 |
if (PyObject_IsSubclass(type.ptr(), typedoc) == 1) { |
| 278 |
// From the template Python object we don't query its type object because there we keep |
| 279 |
// a list of additional methods that we won't see otherwise. But to get the correct doc |
| 280 |
// strings we query the type's dict in the class itself. |
| 281 |
// To see if we have a template Python object we check for the existence of |
| 282 |
// '__fc_template__' See also: FeaturePythonPyT |
| 283 |
if (!obj.hasAttr("__fc_template__")) { |
| 284 |
obj = type; |
| 285 |
} |
| 286 |
} |
| 287 |
else if (PyObject_IsSubclass(type.ptr(), typeobj) == 1) { |
| 288 |
obj = type; |
| 289 |
} |
| 290 |
else if (PyObject_IsInstance(obj.ptr(), basetype) == 1) { |
| 291 |
// New style class which can be a module, type, list, tuple, int, float, ... |
| 292 |
// Make sure it's not a type object |
| 293 |
PyObject* typetype = Base::getTypeAsObject(&PyType_Type); |
| 294 |
if (PyObject_IsInstance(obj.ptr(), typetype) != 1) { |
| 295 |
// For wrapped objects with PySide2 use the object, not its type |
| 296 |
// as otherwise attributes added at runtime won't be listed (e.g. MainWindowPy) |
| 297 |
QString typestr(QLatin1String(Py_TYPE(obj.ptr())->tp_name)); |
| 298 |
|
| 299 |
// this should be now a user-defined Python class |
| 300 |
// http://stackoverflow.com/questions/12233103/in-python-at-runtime-determine-if-an-object-is-a-class-old-and-new-type-instan |
| 301 |
if (!typestr.startsWith(QLatin1String("PySide")) |
| 302 |
&& Py_TYPE(obj.ptr())->tp_flags & Py_TPFLAGS_HEAPTYPE) { |
| 303 |
obj = type; |
| 304 |
} |
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
// If we have an instance of PyObjectBase then determine whether it's valid or not |
| 309 |
if (PyObject_IsInstance(inst.ptr(), typeobj) == 1) { |
| 310 |
auto baseobj = static_cast<Base::PyObjectBase*>(inst.ptr()); |
| 311 |
validObject = baseobj->isValid(); |
| 312 |
} |
| 313 |
else { |
| 314 |
// PyObject_IsInstance might set an exception |
| 315 |
PyErr_Clear(); |
| 316 |
} |
| 317 |
|
| 318 |
Py::List list(obj.dir()); |
| 319 |
|
| 320 |
// If we derive from PropertyContainerPy we can search for the properties in the |
| 321 |
// C++ twin class. |
| 322 |
PyObject* proptypeobj = Base::getTypeAsObject(&App::PropertyContainerPy::Type); |
| 323 |
if (PyObject_IsSubclass(type.ptr(), proptypeobj) == 1) { |
| 324 |
// These are the attributes of the instance itself which are NOT accessible by |
| 325 |
// its type object |
| 326 |
extractTipsFromProperties(inst, tips); |
| 327 |
} |
| 328 |
|
| 329 |
// If we derive from App::DocumentPy we have direct access to the objects by their internal |
| 330 |
// names. So, we add these names to the list, too. |
| 331 |
PyObject* appdoctypeobj = Base::getTypeAsObject(&App::DocumentPy::Type); |
| 332 |
if (PyObject_IsSubclass(type.ptr(), appdoctypeobj) == 1) { |
| 333 |
auto docpy = static_cast<App::DocumentPy*>(inst.ptr()); |
| 334 |
auto document = docpy->getDocumentPtr(); |
| 335 |
// Make sure that the C++ object is alive |
| 336 |
if (document) { |
| 337 |
std::vector<App::DocumentObject*> objects = document->getObjects(); |
| 338 |
Py::List list; |
| 339 |
for (const auto& object : objects) { |
| 340 |
list.append(Py::String(object->getNameInDocument())); |
| 341 |
} |
| 342 |
extractTipsFromObject(inst, list, tips); |
| 343 |
} |
| 344 |
} |
| 345 |
|
| 346 |
// If we derive from Gui::DocumentPy we have direct access to the objects by their internal |
| 347 |
// names. So, we add these names to the list, too. |
| 348 |
PyObject* guidoctypeobj = Base::getTypeAsObject(&Gui::DocumentPy::Type); |
| 349 |
if (PyObject_IsSubclass(type.ptr(), guidoctypeobj) == 1) { |
| 350 |
auto docpy = static_cast<Gui::DocumentPy*>(inst.ptr()); |
| 351 |
if (docpy->getDocumentPtr()) { |
| 352 |
App::Document* document = docpy->getDocumentPtr()->getDocument(); |
| 353 |
// Make sure that the C++ object is alive |
| 354 |
if (document) { |
| 355 |
std::vector<App::DocumentObject*> objects = document->getObjects(); |
| 356 |
Py::List list; |
| 357 |
for (const auto& object : objects) { |
| 358 |
list.append(Py::String(object->getNameInDocument())); |
| 359 |
} |
| 360 |
extractTipsFromObject(inst, list, tips); |
| 361 |
} |
| 362 |
} |
| 363 |
} |
| 364 |
|
| 365 |
// These are the attributes from the type object |
| 366 |
extractTipsFromObject(obj, list, tips); |
| 367 |
} |
| 368 |
catch (Py::Exception& e) { |
| 369 |
// Just clear the Python exception |
| 370 |
e.clear(); |
| 371 |
} |
| 372 |
|
| 373 |
return tips; |
| 374 |
} |
| 375 |
|
| 376 |
bool shibokenMayCrash(void) |
| 377 |
{ |
| 378 |
// Shiboken 6.4.8 to 6.7.3 crash if we try to read their object |
| 379 |
// attributes without a current stack frame. |
| 380 |
// FreeCAD issue: https://github.com/FreeCAD/FreeCAD/issues/14101 |
| 381 |
// Qt issue: https://bugreports.qt.io/browse/PYSIDE-2796 |
| 382 |
|
| 383 |
Py::Module shiboken("shiboken6"); |
| 384 |
Py::Tuple version(shiboken.getAttr("__version_info__")); |
| 385 |
int major = Py::Long(version.getItem(0)); |
| 386 |
int minor = Py::Long(version.getItem(1)); |
| 387 |
int patch = Py::Long(version.getItem(2)); |
| 388 |
bool brokenVersion = (major == 6 && minor >= 4 && minor < 8); |
| 389 |
bool fixedVersion = (major == 6 && minor == 7 && patch > 2); |
| 390 |
return brokenVersion && !fixedVersion; |
| 391 |
} |
| 392 |
|
| 393 |
Py::Object CallTipsList::getAttrWorkaround(Py::Object& obj, Py::String& name) const |
| 394 |
{ |
| 395 |
QString typestr(QLatin1String(Py_TYPE(obj.ptr())->tp_name)); |
| 396 |
bool hasWorkingGetAttr = !(typestr == QLatin1String("Shiboken.ObjectType") && shibokenMayCrash()); |
| 397 |
|
| 398 |
if (hasWorkingGetAttr) { |
| 399 |
return obj.getAttr(name.as_string()); |
| 400 |
} |
| 401 |
|
| 402 |
Py::Dict globals; |
| 403 |
Py::Dict locals; |
| 404 |
locals.setItem("obj", obj); |
| 405 |
locals.setItem("attr", name); |
| 406 |
|
| 407 |
Py::Object bouncer(Py_CompileString("getattr(obj, attr)", "<CallTipsList>", Py_eval_input)); |
| 408 |
Py::Object attr(PyEval_EvalCode(bouncer.ptr(), globals.ptr(), locals.ptr())); |
| 409 |
|
| 410 |
return attr; |
| 411 |
} |
| 412 |
|
| 413 |
void CallTipsList::extractTipsFromObject(Py::Object& obj, Py::List& list, QMap<QString, CallTip>& tips) const |
| 414 |
{ |
| 415 |
for (Py::List::iterator it = list.begin(); it != list.end(); ++it) { |
| 416 |
try { |
| 417 |
Py::String attrname(*it); |
| 418 |
std::string name(attrname.as_string()); |
| 419 |
|
| 420 |
Py::Object attr = getAttrWorkaround(obj, attrname); |
| 421 |
if (!attr.ptr()) { |
| 422 |
Base::Console().log("Python attribute '%s' returns null!\n", name.c_str()); |
| 423 |
continue; |
| 424 |
} |
| 425 |
|
| 426 |
CallTip tip; |
| 427 |
QString str = QString::fromLatin1(name.c_str()); |
| 428 |
tip.name = str; |
| 429 |
|
| 430 |
if (attr.isCallable()) { |
| 431 |
PyObject* basetype = Base::getTypeAsObject(&PyBaseObject_Type); |
| 432 |
if (PyObject_IsSubclass(attr.ptr(), basetype) == 1) { |
| 433 |
tip.type = CallTip::Class; |
| 434 |
} |
| 435 |
else { |
| 436 |
PyErr_Clear(); // PyObject_IsSubclass might set an exception |
| 437 |
tip.type = CallTip::Method; |
| 438 |
} |
| 439 |
} |
| 440 |
else if (PyModule_Check(attr.ptr())) { |
| 441 |
tip.type = CallTip::Module; |
| 442 |
} |
| 443 |
else { |
| 444 |
tip.type = CallTip::Member; |
| 445 |
} |
| 446 |
|
| 447 |
if (str == QLatin1String("__doc__") && attr.isString()) { |
| 448 |
Py::Object help = attr; |
| 449 |
if (help.isString()) { |
| 450 |
Py::String doc(help); |
| 451 |
QString longdoc = QString::fromUtf8(doc.as_string().c_str()); |
| 452 |
int pos = longdoc.indexOf(QLatin1Char('\n')); |
| 453 |
pos = qMin(pos, 70); |
| 454 |
if (pos < 0) { |
| 455 |
pos = qMin(longdoc.length(), 70); |
| 456 |
} |
| 457 |
tip.description = stripWhiteSpace(longdoc); |
| 458 |
tip.parameter = longdoc.left(pos); |
| 459 |
} |
| 460 |
} |
| 461 |
else if (attr.hasAttr("__doc__")) { |
| 462 |
Py::Object help = attr.getAttr("__doc__"); |
| 463 |
if (help.isString()) { |
| 464 |
Py::String doc(help); |
| 465 |
QString longdoc = QString::fromUtf8(doc.as_string().c_str()); |
| 466 |
int pos = longdoc.indexOf(QLatin1Char('\n')); |
| 467 |
pos = qMin(pos, 70); |
| 468 |
if (pos < 0) { |
| 469 |
pos = qMin(longdoc.length(), 70); |
| 470 |
} |
| 471 |
tip.description = stripWhiteSpace(longdoc); |
| 472 |
tip.parameter = longdoc.left(pos); |
| 473 |
} |
| 474 |
} |
| 475 |
|
| 476 |
// Do not override existing items |
| 477 |
QMap<QString, CallTip>::iterator pos = tips.find(str); |
| 478 |
if (pos == tips.end()) { |
| 479 |
tips[str] = tip; |
| 480 |
} |
| 481 |
} |
| 482 |
catch (Py::Exception& e) { |
| 483 |
// Just clear the Python exception |
| 484 |
e.clear(); |
| 485 |
} |
| 486 |
} |
| 487 |
} |
| 488 |
|
| 489 |
void CallTipsList::extractTipsFromProperties(Py::Object& obj, QMap<QString, CallTip>& tips) const |
| 490 |
{ |
| 491 |
auto cont = static_cast<App::PropertyContainerPy*>(obj.ptr()); |
| 492 |
App::PropertyContainer* container = cont->getPropertyContainerPtr(); |
| 493 |
// Make sure that the C++ object is alive |
| 494 |
if (!container) { |
| 495 |
return; |
| 496 |
} |
| 497 |
std::map<std::string, App::Property*> Map; |
| 498 |
container->getPropertyMap(Map); |
| 499 |
|
| 500 |
for (const auto& It : Map) { |
| 501 |
CallTip tip; |
| 502 |
QString str = QString::fromLatin1(It.first.c_str()); |
| 503 |
tip.name = str; |
| 504 |
tip.type = CallTip::Property; |
| 505 |
QString longdoc = QString::fromUtf8(container->getPropertyDocumentation(It.second)); |
| 506 |
// a point, mesh or shape property |
| 507 |
if (It.second->isDerivedFrom(Base::Type::fromName("App::PropertyComplexGeoData"))) { |
| 508 |
Py::Object data(It.second->getPyObject(), true); |
| 509 |
if (data.hasAttr("__doc__")) { |
| 510 |
Py::Object help = data.getAttr("__doc__"); |
| 511 |
if (help.isString()) { |
| 512 |
Py::String doc(help); |
| 513 |
longdoc = QString::fromUtf8(doc.as_string().c_str()); |
| 514 |
} |
| 515 |
} |
| 516 |
} |
| 517 |
if (!longdoc.isEmpty()) { |
| 518 |
int pos = longdoc.indexOf(QLatin1Char('\n')); |
| 519 |
pos = qMin(pos, 70); |
| 520 |
if (pos < 0) { |
| 521 |
pos = qMin(longdoc.length(), 70); |
| 522 |
} |
| 523 |
tip.description = stripWhiteSpace(longdoc); |
| 524 |
tip.parameter = longdoc.left(pos); |
| 525 |
} |
| 526 |
tips[str] = tip; |
| 527 |
} |
| 528 |
} |
| 529 |
|
| 530 |
void CallTipsList::showTips(const QString& line) |
| 531 |
{ |
| 532 |
// search only once |
| 533 |
static QPixmap type_module_icon = BitmapFactory().pixmap("ClassBrowser/type_module.svg"); |
| 534 |
static QPixmap type_class_icon = BitmapFactory().pixmap("ClassBrowser/type_class.svg"); |
| 535 |
static QPixmap method_icon = BitmapFactory().pixmap("ClassBrowser/method.svg"); |
| 536 |
static QPixmap member_icon = BitmapFactory().pixmap("ClassBrowser/member.svg"); |
| 537 |
static QPixmap property_icon = BitmapFactory().pixmap("ClassBrowser/property.svg"); |
| 538 |
|
| 539 |
// object is in error state |
| 540 |
static QPixmap forbidden_icon( |
| 541 |
Gui::BitmapFactory().pixmapFromSvg("forbidden", property_icon.size() / 4) |
| 542 |
); |
| 543 |
static QPixmap forbidden_type_module_icon |
| 544 |
= BitmapFactory().merge(type_module_icon, forbidden_icon, BitmapFactoryInst::BottomLeft); |
| 545 |
static QPixmap forbidden_type_class_icon |
| 546 |
= BitmapFactory().merge(type_class_icon, forbidden_icon, BitmapFactoryInst::BottomLeft); |
| 547 |
static QPixmap forbidden_method_icon |
| 548 |
= BitmapFactory().merge(method_icon, forbidden_icon, BitmapFactoryInst::BottomLeft); |
| 549 |
static QPixmap forbidden_member_icon |
| 550 |
= BitmapFactory().merge(member_icon, forbidden_icon, BitmapFactoryInst::BottomLeft); |
| 551 |
static QPixmap forbidden_property_icon |
| 552 |
= BitmapFactory().merge(property_icon, forbidden_icon, BitmapFactoryInst::BottomLeft); |
| 553 |
|
| 554 |
this->validObject = true; |
| 555 |
QString context = extractContext(line); |
| 556 |
context = context.simplified(); |
| 557 |
QMap<QString, CallTip> tips = extractTips(context); |
| 558 |
clear(); |
| 559 |
for (QMap<QString, CallTip>::Iterator it = tips.begin(); it != tips.end(); ++it) { |
| 560 |
addItem(it.key()); |
| 561 |
QListWidgetItem* item = this->item(this->count() - 1); |
| 562 |
item->setData(Qt::ToolTipRole, QVariant(it.value().description)); |
| 563 |
item->setData(Qt::UserRole, QVariant::fromValue(it.value())); //< store full CallTip data |
| 564 |
switch (it.value().type) { |
| 565 |
case CallTip::Module: { |
| 566 |
item->setIcon((this->validObject ? type_module_icon : forbidden_type_module_icon)); |
| 567 |
} break; |
| 568 |
case CallTip::Class: { |
| 569 |
item->setIcon((this->validObject ? type_class_icon : forbidden_type_class_icon)); |
| 570 |
} break; |
| 571 |
case CallTip::Method: { |
| 572 |
item->setIcon((this->validObject ? method_icon : forbidden_method_icon)); |
| 573 |
} break; |
| 574 |
case CallTip::Member: { |
| 575 |
item->setIcon((this->validObject ? member_icon : forbidden_member_icon)); |
| 576 |
} break; |
| 577 |
case CallTip::Property: { |
| 578 |
item->setIcon((this->validObject ? property_icon : forbidden_property_icon)); |
| 579 |
} break; |
| 580 |
default: |
| 581 |
break; |
| 582 |
} |
| 583 |
} |
| 584 |
|
| 585 |
if (count() == 0) { |
| 586 |
return; // nothing found |
| 587 |
} |
| 588 |
|
| 589 |
// get the minimum width and height of the box |
| 590 |
int h = 0; |
| 591 |
int w = 0; |
| 592 |
for (int i = 0; i < count(); ++i) { |
| 593 |
QRect r = visualItemRect(item(i)); |
| 594 |
w = qMax(w, r.width()); |
| 595 |
h += r.height(); |
| 596 |
} |
| 597 |
|
| 598 |
// Add an offset |
| 599 |
w += 2 * frameWidth(); |
| 600 |
h += 2 * frameWidth(); |
| 601 |
|
| 602 |
// get the start position of the word prefix |
| 603 |
QTextCursor cursor = textEdit->textCursor(); |
| 604 |
this->cursorPos = cursor.position(); |
| 605 |
QRect rect = textEdit->cursorRect(cursor); |
| 606 |
int posX = rect.x(); |
| 607 |
int posY = rect.y(); |
| 608 |
int boxH = h; |
| 609 |
|
| 610 |
// Decide whether to show downstairs or upstairs |
| 611 |
if (posY > textEdit->viewport()->height() / 2) { |
| 612 |
h = qMin(qMin(h, posY), 250); |
| 613 |
if (h < boxH) { |
| 614 |
w += textEdit->style()->pixelMetric(QStyle::PM_ScrollBarExtent); |
| 615 |
} |
| 616 |
setGeometry(posX, posY - h, w, h); |
| 617 |
} |
| 618 |
else { |
| 619 |
h = qMin(qMin(h, textEdit->viewport()->height() - fontMetrics().height() - posY), 250); |
| 620 |
if (h < boxH) { |
| 621 |
w += textEdit->style()->pixelMetric(QStyle::PM_ScrollBarExtent); |
| 622 |
} |
| 623 |
setGeometry(posX, posY + fontMetrics().height(), w, h); |
| 624 |
} |
| 625 |
|
| 626 |
setCurrentRow(0); |
| 627 |
show(); |
| 628 |
} |
| 629 |
|
| 630 |
void CallTipsList::showEvent(QShowEvent* e) |
| 631 |
{ |
| 632 |
QListWidget::showEvent(e); |
| 633 |
// install this object to filter timer events for the tooltip label |
| 634 |
qApp->installEventFilter(this); |
| 635 |
} |
| 636 |
|
| 637 |
void CallTipsList::hideEvent(QHideEvent* e) |
| 638 |
{ |
| 639 |
QListWidget::hideEvent(e); |
| 640 |
qApp->removeEventFilter(this); |
| 641 |
} |
| 642 |
|
| 643 |
/** |
| 644 |
* Get all incoming events of the text edit and redirect some of them, like key up and |
| 645 |
* down, mouse press events, ... to the widget itself. |
| 646 |
*/ |
| 647 |
bool CallTipsList::eventFilter(QObject* watched, QEvent* event) |
| 648 |
{ |
| 649 |
// This is a trick to avoid to hide the tooltip window after the defined time span |
| 650 |
// of 10 seconds. We just filter out all timer events to keep the label visible. |
| 651 |
if (watched->inherits("QLabel")) { |
| 652 |
auto label = qobject_cast<QLabel*>(watched); |
| 653 |
// Ignore the timer events to prevent from being closed |
| 654 |
if (label->windowFlags() & Qt::ToolTip && event->type() == QEvent::Timer) { |
| 655 |
return true; |
| 656 |
} |
| 657 |
} |
| 658 |
if (isVisible() && watched == textEdit->viewport()) { |
| 659 |
if (event->type() == QEvent::MouseButtonPress) { |
| 660 |
hide(); |
| 661 |
} |
| 662 |
} |
| 663 |
else if (isVisible() && watched == textEdit) { |
| 664 |
if (event->type() == QEvent::KeyPress) { |
| 665 |
auto ke = static_cast<QKeyEvent*>(event); |
| 666 |
if (ke->key() == Qt::Key_Up || ke->key() == Qt::Key_Down) { |
| 667 |
keyPressEvent(ke); |
| 668 |
return true; |
| 669 |
} |
| 670 |
else if (ke->key() == Qt::Key_PageUp || ke->key() == Qt::Key_PageDown) { |
| 671 |
keyPressEvent(ke); |
| 672 |
return true; |
| 673 |
} |
| 674 |
else if (ke->key() == Qt::Key_Escape) { |
| 675 |
hide(); |
| 676 |
return true; |
| 677 |
} |
| 678 |
else if ((ke->key() == Qt::Key_Minus) && (ke->modifiers() & Qt::ShiftModifier)) { |
| 679 |
// do nothing here, but this check is needed to ignore underscore |
| 680 |
// which in Qt 4.8 gives Key_Minus instead of Key_Underscore |
| 681 |
} |
| 682 |
else if (this->hideKeys.indexOf(ke->key()) > -1) { |
| 683 |
Q_EMIT itemActivated(currentItem()); |
| 684 |
return false; |
| 685 |
} |
| 686 |
else if (ke->key() == Qt::Key_Return || ke->key() == Qt::Key_Enter) { |
| 687 |
Q_EMIT itemActivated(currentItem()); |
| 688 |
return true; |
| 689 |
} |
| 690 |
else if (ke->key() == Qt::Key_Tab) { |
| 691 |
// enable call completion for activating items |
| 692 |
Temporary<bool> tmp(this->doCallCompletion, true); //< previous state restored on |
| 693 |
// scope exit |
| 694 |
Q_EMIT itemActivated(currentItem()); |
| 695 |
return true; |
| 696 |
} |
| 697 |
else if (this->compKeys.indexOf(ke->key()) > -1) { |
| 698 |
Q_EMIT itemActivated(currentItem()); |
| 699 |
return false; |
| 700 |
} |
| 701 |
else if (ke->key() == Qt::Key_Shift || ke->key() == Qt::Key_Control |
| 702 |
|| ke->key() == Qt::Key_Meta || ke->key() == Qt::Key_Alt |
| 703 |
|| ke->key() == Qt::Key_AltGr) { |
| 704 |
// filter these meta keys to avoid to call keyboardSearch() |
| 705 |
return true; |
| 706 |
} |
| 707 |
} |
| 708 |
else if (event->type() == QEvent::KeyRelease) { |
| 709 |
auto ke = static_cast<QKeyEvent*>(event); |
| 710 |
if (ke->key() == Qt::Key_Up || ke->key() == Qt::Key_Down || ke->key() == Qt::Key_PageUp |
| 711 |
|| ke->key() == Qt::Key_PageDown) { |
| 712 |
QList<QListWidgetItem*> items = selectedItems(); |
| 713 |
if (!items.isEmpty()) { |
| 714 |
QPoint p(width(), 0); |
| 715 |
QString text = items.front()->toolTip(); |
| 716 |
if (!text.isEmpty()) { |
| 717 |
QToolTip::showText(mapToGlobal(p), text); |
| 718 |
} |
| 719 |
else { |
| 720 |
QToolTip::showText(p, QString()); |
| 721 |
} |
| 722 |
} |
| 723 |
return true; |
| 724 |
} |
| 725 |
} |
| 726 |
else if (event->type() == QEvent::FocusOut) { |
| 727 |
if (!hasFocus()) { |
| 728 |
hide(); |
| 729 |
} |
| 730 |
} |
| 731 |
} |
| 732 |
|
| 733 |
return QListWidget::eventFilter(watched, event); |
| 734 |
} |
| 735 |
|
| 736 |
void CallTipsList::callTipItemActivated(QListWidgetItem* item) |
| 737 |
{ |
| 738 |
hide(); |
| 739 |
if (!item->isSelected()) { |
| 740 |
return; |
| 741 |
} |
| 742 |
|
| 743 |
QString text = item->text(); |
| 744 |
QTextCursor cursor = textEdit->textCursor(); |
| 745 |
cursor.setPosition(this->cursorPos); |
| 746 |
cursor.movePosition(QTextCursor::EndOfWord, QTextCursor::KeepAnchor); |
| 747 |
QString sel = cursor.selectedText(); |
| 748 |
if (!sel.isEmpty()) { |
| 749 |
// in case the cursor moved too far on the right side |
| 750 |
const QChar underscore = QLatin1Char('_'); |
| 751 |
const QChar ch = sel.at(sel.size() - 1); |
| 752 |
if (!ch.isLetterOrNumber() && ch != underscore) { |
| 753 |
cursor.movePosition(QTextCursor::Left, QTextCursor::KeepAnchor); |
| 754 |
} |
| 755 |
} |
| 756 |
cursor.insertText(text); |
| 757 |
|
| 758 |
// get CallTip from item's UserRole-data |
| 759 |
auto callTip = item->data(Qt::UserRole).value<CallTip>(); |
| 760 |
|
| 761 |
// if call completion enabled and we've something callable (method or class constructor) ... |
| 762 |
if (this->doCallCompletion |
| 763 |
&& (callTip.type == CallTip::Method || callTip.type == CallTip::Class)) { |
| 764 |
cursor.insertText(QLatin1String("()")); //< just append parenthesis to identifier even inserted. |
| 765 |
|
| 766 |
/** |
| 767 |
* Try to find out if call needs arguments. |
| 768 |
* For this we search the description for appropriate hints ... |
| 769 |
*/ |
| 770 |
QRegularExpression argumentMatcher( |
| 771 |
QRegularExpression::escape(callTip.name) + QLatin1String(R"(\s*\(\s*\w+.*\))") |
| 772 |
); |
| 773 |
argumentMatcher.setPatternOptions( |
| 774 |
QRegularExpression::InvertedGreedinessOption |
| 775 |
); //< set regex non-greedy! |
| 776 |
if (argumentMatcher.match(callTip.description).hasMatch()) { |
| 777 |
// if arguments are needed, we just move the cursor one left, to between the parentheses. |
| 778 |
cursor.movePosition(QTextCursor::Left, QTextCursor::MoveAnchor, 1); |
| 779 |
textEdit->setTextCursor(cursor); |
| 780 |
} |
| 781 |
} |
| 782 |
textEdit->ensureCursorVisible(); |
| 783 |
|
| 784 |
QRect rect = textEdit->cursorRect(cursor); |
| 785 |
int posX = rect.x(); |
| 786 |
int posY = rect.y(); |
| 787 |
|
| 788 |
QPoint p(posX, posY); |
| 789 |
p = textEdit->mapToGlobal(p); |
| 790 |
QToolTip::showText(p, callTip.parameter); |
| 791 |
} |
| 792 |
|
| 793 |
QString CallTipsList::stripWhiteSpace(const QString& str) const |
| 794 |
{ |
| 795 |
QString stripped = str; |
| 796 |
QStringList lines = str.split(QLatin1String("\n")); |
| 797 |
int minspace = std::numeric_limits<int>::max(); |
| 798 |
int line = 0; |
| 799 |
for (QStringList::iterator it = lines.begin(); it != lines.end(); ++it, ++line) { |
| 800 |
if (it->size() > 0 && line > 0) { |
| 801 |
int space = 0; |
| 802 |
for (int i = 0; i < it->size(); i++) { |
| 803 |
if ((*it)[i] == QLatin1Char('\t')) { |
| 804 |
space++; |
| 805 |
} |
| 806 |
else { |
| 807 |
break; |
| 808 |
} |
| 809 |
} |
| 810 |
|
| 811 |
if (it->size() > space) { |
| 812 |
minspace = std::min<int>(minspace, space); |
| 813 |
} |
| 814 |
} |
| 815 |
} |
| 816 |
|
| 817 |
// remove all leading tabs from each line |
| 818 |
if (minspace > 0 && minspace < std::numeric_limits<int>::max()) { |
| 819 |
int line = 0; |
| 820 |
QStringList strippedlines; |
| 821 |
for (QStringList::iterator it = lines.begin(); it != lines.end(); ++it, ++line) { |
| 822 |
if (line == 0 && !it->isEmpty()) { |
| 823 |
strippedlines << *it; |
| 824 |
} |
| 825 |
else if (it->size() > 0 && line > 0) { |
| 826 |
strippedlines << it->mid(minspace); |
| 827 |
} |
| 828 |
} |
| 829 |
|
| 830 |
stripped = strippedlines.join(QLatin1String("\n")); |
| 831 |
} |
| 832 |
|
| 833 |
return stripped; |
| 834 |
} |
| 835 |
|
| 836 |
#include "moc_CallTips.cpp" |