Gui: property view related changes

* Display property from linked object, colored green,

* Change DlgPropertyLink to support external linking and sub-object
  selection

* Improve large selection performance by using a timer

* Improve TAB key behavior in property editor

* Add context menu to show hidden properties, change property status,
  set expression on any and property, and add/remove dynamic properties

* Optimize expression completer model construction, as the original
  implementation gets prohibitively slow for moderate number of objects.
This commit is contained in:
Zheng, Lei
2019-07-11 13:02:45 +08:00
committed by wmayer
parent ebf321fc47
commit 8b3ef8faf5
23 changed files with 2292 additions and 564 deletions

View File

@@ -5,21 +5,292 @@
#include <QStandardItemModel>
#include <QLineEdit>
#include <QAbstractItemView>
#include <QTextBlock>
#endif
#include <boost/algorithm/string/predicate.hpp>
#include <Base/Tools.h>
#include <Base/Console.h>
#include <App/Application.h>
#include <App/Document.h>
#include <App/DocumentObject.h>
#include <App/DocumentObserver.h>
#include <App/ObjectIdentifier.h>
#include "ExpressionCompleter.h"
#include <App/Expression.h>
#include <App/PropertyLinks.h>
FC_LOG_LEVEL_INIT("Completer",true,true);
Q_DECLARE_METATYPE(App::ObjectIdentifier);
using namespace App;
using namespace Gui;
class ExpressionCompleterModel: public QAbstractItemModel {
public:
ExpressionCompleterModel(QObject *parent, const App::DocumentObject *obj)
:QAbstractItemModel(parent)
{
if(obj) {
currentDoc = obj->getDocument()->getName();
currentObj = obj->getNameInDocument();
inList = obj->getInListEx(true);
}
}
// This ExpressionCompleter model works without any pysical items.
// Everything item related is stored inside QModelIndex.InternalPointer/InternalId(),
// using the following Info structure.
//
// The Info contains two indices, one for document and the other for object.
// For 32-bit system, the index is 16bit which limits the size to 64K. For
// 64-bit system, the index is 32bit.
//
// The "virtual" items are organized as a tree. The root items are special,
// which consists of three types in the following order,
//
// * Document, even index contains item using document's name, while
// odd index with quoted document label.
// * Objects of the current document, even index with object's internal
// name, and odd index with quoted object label.
// * Properties of the current object.
//
// Document item contains object item as child, and object item contains
// property item.
//
// The QModelIndex of a root item has both the doc field and obj field set
// to -1, and uses the row as the item index. We can figure out the type of
// the item solely based on this row index.
//
// QModelIndex of a non-root object item has doc field as the document
// index, and obj field set to -1.
//
// QModelIndex of a non-root property item has doc field as the document
// index, and obj field as the object index.
union Info {
struct {
qint32 doc;
qint32 obj;
}d;
struct {
qint16 doc;
qint16 obj;
}d32;
void *ptr;
};
static void *infoId(const Info &info) {
if(sizeof(void*) >= sizeof(info))
return info.ptr;
Info info32;
info32.d32.doc = (qint16)info.d.doc;
info32.d32.obj = (qint16)info.d.obj;
return info32.ptr;
};
static Info getInfo(const QModelIndex &index) {
Info info;
info.ptr = index.internalPointer();
if(sizeof(void*) >= sizeof(Info))
return info;
Info res;
res.d.doc = info.d32.doc;
res.d.obj = info.d32.obj;
return res;
}
QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const {
if(role!=Qt::EditRole && role!=Qt::DisplayRole && role!=Qt::UserRole)
return QVariant();
QVariant v;
Info info = getInfo(index);
_data(info,index.row(),&v,0,role==Qt::UserRole);
FC_TRACE(info.d.doc << "," << info.d.obj << "," << index.row()
<< ": " << v.toString().toUtf8().constData());
return v;
}
void _data(const Info &info, int row, QVariant *v, int *count, bool sep=false) const {
int idx;
idx = info.d.doc<0?row:info.d.doc;
const auto &docs = App::GetApplication().getDocuments();
int docSize = (int)docs.size()*2;
int objSize = 0;
int propSize = 0;
std::vector<App::Property*> props;
App::Document *doc = 0;
App::DocumentObject *obj = 0;
App::Property *prop = 0;
if(idx>=0 && idx<docSize)
doc = docs[idx/2];
else {
doc = App::GetApplication().getDocument(currentDoc.c_str());
if(!doc)
return;
idx -= docSize;
if(info.d.doc<0)
row = idx;
const auto &objs = doc->getObjects();
objSize = (int)objs.size()*2;
if(idx>=0 && idx<objSize) {
obj = objs[idx/2];
if(inList.count(obj))
return;
} else {
auto cobj = doc->getObject(currentObj.c_str());
if(cobj) {
idx -= objSize;
if(info.d.doc<0)
row = idx;
cobj->getPropertyList(props);
propSize = (int)props.size();
if(idx >= propSize)
return;
if(idx>=0) {
obj = cobj;
prop = props[idx];
}
}
}
}
if(info.d.doc<0) {
if(count)
*count = docSize + objSize + propSize;
if(idx>=0 && v) {
QString res;
if(prop)
res = QString::fromLatin1(prop->getName());
else if(obj) {
if(idx & 1)
res = QString::fromUtf8(quote(obj->Label.getStrValue()).c_str());
else
res = QString::fromLatin1(obj->getNameInDocument());
if(sep)
res += QLatin1Char('.');
}else {
if(idx & 1)
res = QString::fromUtf8(quote(doc->Label.getStrValue()).c_str());
else
res = QString::fromLatin1(doc->getName());
if(sep)
res += QLatin1Char('#');
}
v->setValue(res);
}
return;
}
if(!obj) {
idx = info.d.obj<0?row:info.d.obj;
const auto &objs = doc->getObjects();
objSize = (int)objs.size()*2;
if(idx<0 || idx>=objSize || inList.count(obj))
return;
obj = objs[idx/2];
if(info.d.obj<0) {
if(count)
*count = objSize;
if(v) {
QString res;
if(idx&1)
res = QString::fromUtf8(quote(obj->Label.getStrValue()).c_str());
else
res = QString::fromLatin1(obj->getNameInDocument());
if(sep)
res += QLatin1Char('.');
v->setValue(res);
}
return;
}
}
if(!prop) {
idx = row;
obj->getPropertyList(props);
propSize = (int)props.size();
if(idx<0 || idx>=propSize)
return;
prop = props[idx];
if(count)
*count = propSize;
}
if(v)
*v = QString::fromLatin1(prop->getName());
return;
}
QModelIndex parent(const QModelIndex & index) const {
if(!index.isValid())
return QModelIndex();
Info info;
Info parentInfo;
info = parentInfo = getInfo(index);
if(info.d.obj>=0) {
parentInfo.d.obj = -1;
return createIndex(info.d.obj,0,infoId(parentInfo));
}
if(info.d.doc>=0) {
parentInfo.d.doc = -1;
return createIndex(info.d.doc,0,infoId(parentInfo));
}
return QModelIndex();
}
QModelIndex index(int row, int column, const QModelIndex & parent = QModelIndex()) const {
if(row<0)
return QModelIndex();
Info info;
if(!parent.isValid()) {
info.d.doc = -1;
info.d.obj = -1;
}else{
info = getInfo(parent);
if(info.d.doc<=0)
info.d.doc = parent.row();
else if(info.d.obj<=0)
info.d.obj = parent.row();
else
return QModelIndex();
}
return createIndex(row,column,infoId(info));
}
int rowCount(const QModelIndex & parent = QModelIndex()) const {
const auto &docs = App::GetApplication().getDocuments();
Info info;
int row = 0;
if(!parent.isValid()) {
info.d.doc = -1;
info.d.obj = -1;
row = -1;
}else{
info = getInfo(parent);
if(info.d.doc<0)
info.d.doc = parent.row();
else if(info.d.obj<0)
info.d.obj = parent.row();
else
return 0;
}
int count = 0;
_data(info,row,0,&count);
FC_TRACE(info.d.doc << "," << info.d.obj << "," << row << " row count " << count);
return count;
}
int columnCount(const QModelIndex &) const {
return 1;
}
private:
std::set<App::DocumentObject*> inList;
std::string currentDoc;
std::string currentObj;
};
/**
* @brief Construct an ExpressionCompleter object.
* @param currentDoc Current document to generate the model from.
@@ -27,208 +298,88 @@ using namespace Gui;
* @param parent Parent object owning the completer.
*/
ExpressionCompleter::ExpressionCompleter(const App::Document * currentDoc, const App::DocumentObject * currentDocObj, QObject *parent)
: QCompleter(parent), prefixStart(0)
ExpressionCompleter::ExpressionCompleter(const App::DocumentObject * currentDocObj, QObject *parent)
: QCompleter(parent), prefixStart(0), currentObj(currentDocObj)
{
QStandardItemModel* model = new QStandardItemModel(this);
std::vector<App::Document*> docs = App::GetApplication().getDocuments();
std::vector<App::Document*>::const_iterator di = docs.begin();
std::vector<DocumentObject*> deps;
if (currentDocObj)
deps = currentDocObj->getInList();
std::set<const DocumentObject*> forbidden;
for (std::vector<DocumentObject*>::const_iterator it = deps.begin(); it != deps.end(); ++it)
forbidden.insert(*it);
/* Create tree with full path to all objects */
while (di != docs.end()) {
QStandardItem* docItem = new QStandardItem(QString::fromLatin1((*di)->getName()));
docItem->setData(QString::fromLatin1((*di)->getName()) + QString::fromLatin1("#"), Qt::UserRole);
createModelForDocument(*di, docItem, forbidden);
model->appendRow(docItem);
++di;
}
/* Create branch with current document object */
if (currentDocObj) {
createModelForDocument(currentDocObj->getDocument(), model->invisibleRootItem(), forbidden);
createModelForDocumentObject(currentDocObj, model->invisibleRootItem());
}
else {
if (currentDoc)
createModelForDocument(currentDoc, model->invisibleRootItem(), forbidden);
}
setModel(model);
setCaseSensitivity(Qt::CaseInsensitive);
}
/**
* @brief Create model node given document, the parent and forbidden node.
* @param doc Document
* @param parent Parent item
* @param forbidden Forbidden document objects; typically those that will create a loop in the DAG if used.
*/
void ExpressionCompleter::init() {
if(model())
return;
void ExpressionCompleter::createModelForDocument(const App::Document * doc, QStandardItem * parent,
const std::set<const DocumentObject*> & forbidden) {
std::vector<App::DocumentObject*> docObjs = doc->getObjects();
std::vector<App::DocumentObject*>::const_iterator doi = docObjs.begin();
while (doi != docObjs.end()) {
std::set<const DocumentObject*>::const_iterator it = forbidden.find(*doi);
// Skip?
if (it != forbidden.end()) {
++doi;
continue;
}
QStandardItem* docObjItem = new QStandardItem(QString::fromLatin1((*doi)->getNameInDocument()));
docObjItem->setData(QString::fromLatin1((*doi)->getNameInDocument()) + QString::fromLatin1("."), Qt::UserRole);
createModelForDocumentObject(*doi, docObjItem);
parent->appendRow(docObjItem);
if (strcmp((*doi)->getNameInDocument(), (*doi)->Label.getValue()) != 0) {
std::string label = (*doi)->Label.getValue();
if (!ExpressionParser::isTokenAnIndentifier(label))
label = quote(label);
docObjItem = new QStandardItem(QString::fromUtf8(label.c_str()));
docObjItem->setData( QString::fromUtf8(label.c_str()) + QString::fromLatin1("."), Qt::UserRole);
createModelForDocumentObject(*doi, docObjItem);
parent->appendRow(docObjItem);
}
++doi;
}
setModel(new ExpressionCompleterModel(this,currentObj.getObject()));
}
/**
* @brief Create model nodes for document object
* @param docObj Document object
* @param parent Parent item
*/
void ExpressionCompleter::createModelForDocumentObject(const DocumentObject * docObj, QStandardItem * parent)
{
std::vector<App::Property*> props;
docObj->getPropertyList(props);
std::vector<App::Property*>::const_iterator pi = props.begin();
while (pi != props.end()) {
// Skip all types of links
if ((*pi)->isDerivedFrom(App::PropertyLink::getClassTypeId()) ||
(*pi)->isDerivedFrom(App::PropertyLinkSub::getClassTypeId())) {
++pi;
continue;
}
createModelForPaths(*pi, parent);
++pi;
}
}
/**
* @brief Create nodes for a property.
* @param prop
* @param docObjItem
*/
void ExpressionCompleter::createModelForPaths(const App::Property * prop, QStandardItem *docObjItem)
{
std::vector<ObjectIdentifier> paths;
std::vector<ObjectIdentifier>::const_iterator ppi;
prop->getPaths(paths);
for (ppi = paths.begin(); ppi != paths.end(); ++ppi) {
QStandardItem* pathItem = new QStandardItem(Base::Tools::fromStdString(ppi->toString()));
QVariant value;
value.setValue(*ppi);
pathItem->setData(value, Qt::UserRole);
docObjItem->appendRow(pathItem);
}
}
QString ExpressionCompleter::pathFromIndex ( const QModelIndex & index ) const
{
QStandardItemModel * m = static_cast<QStandardItemModel*>(model());
if (m->data(index, Qt::UserRole).canConvert<App::ObjectIdentifier>()) {
App::ObjectIdentifier p = m->data(index, Qt::UserRole).value<App::ObjectIdentifier>();
QString pStr = Base::Tools::fromStdString(p.toString());
QString parentStr;
QModelIndex parent = index.parent();
while (parent.isValid()) {
QString thisParentStr = m->data(parent, Qt::UserRole).toString();
parentStr = thisParentStr + parentStr;
parent = parent.parent();
}
return parentStr + pStr;
}
else if (m->data(index, Qt::UserRole).canConvert<QString>()) {
QModelIndex parent = index;
QString parentStr;
while (parent.isValid()) {
QString thisParentStr = m->data(parent, Qt::UserRole).toString();
parentStr = thisParentStr + parentStr;
parent = parent.parent();
}
return parentStr;
}
else
auto m = model();
if(!m || !index.isValid())
return QString();
QString res;
auto parent = index;
do {
res = m->data(parent, Qt::UserRole).toString() + res;
parent = parent.parent();
}while(parent.isValid());
auto info = ExpressionCompleterModel::getInfo(index);
FC_TRACE("join path " << info.d.doc << "," << info.d.obj << "," << index.row()
<< ": " << res.toUtf8().constData());
return res;
}
QStringList ExpressionCompleter::splitPath ( const QString & path ) const
QStringList ExpressionCompleter::splitPath ( const QString & input ) const
{
try {
App::ObjectIdentifier p = ObjectIdentifier::parse(0, path.toUtf8().constData());
QStringList l;
QStringList l;
std::string path = input.toUtf8().constData();
if(path.empty())
return l;
int retry = 0;
std::string trim;
while(1) {
try {
App::ObjectIdentifier p = ObjectIdentifier::parse(
currentObj.getObject(), path);
if (p.getProperty()) {
for (int i = 0; i < p.numComponents(); ++i)
l << Base::Tools::fromStdString(p.getPropertyComponent(i).toString());
return l;
}
else {
std::vector<std::string> sl = p.getStringList();
std::vector<std::string>::const_iterator sli = sl.begin();
if(retry && sl.size())
sl.pop_back();
if(trim.size() && boost::ends_with(sl.back(),trim))
sl.back().resize(sl.back().size()-trim.size());
while (sli != sl.end()) {
l << Base::Tools::fromStdString(*sli);
++sli;
}
FC_TRACE("split path " << path
<< " -> " << l.join(QLatin1String("/")).toUtf8().constData());
return l;
}
}
catch (const Base::Exception &) {
return QStringList() << path;
catch (const Base::Exception &e) {
FC_TRACE("split path " << path << " error: " << e.what());
if(!retry) {
char last = path[path.size()-1];
if(last!='#' && last!='.' && path.find('#')!=std::string::npos) {
path += "._self";
++retry;
continue;
}
}else if(retry==1) {
path.resize(path.size()-6);
char last = path[path.size()-1];
if(last!='.' && last!='<' && path.find("#<<")!=std::string::npos) {
path += ">>._self";
++retry;
trim = ">>";
continue;
}
}
return QStringList() << input;
}
}
}
@@ -292,7 +443,7 @@ void ExpressionCompleter::slotUpdate(const QString & prefix)
ExpressionLineEdit::ExpressionLineEdit(QWidget *parent)
: QLineEdit(parent)
, completer(0)
, block(false)
, block(true)
{
connect(this, SIGNAL(textChanged(const QString&)), this, SLOT(slotTextChanged(const QString&)));
}
@@ -305,7 +456,7 @@ void ExpressionLineEdit::setDocumentObject(const App::DocumentObject * currentDo
}
if (currentDocObj != 0) {
completer = new ExpressionCompleter(currentDocObj->getDocument(), currentDocObj, this);
completer = new ExpressionCompleter(currentDocObj, this);
completer->setWidget(this);
completer->setCaseSensitivity(Qt::CaseInsensitive);
connect(completer, SIGNAL(activated(QString)), this, SLOT(slotCompleteText(QString)));
@@ -338,10 +489,79 @@ void ExpressionLineEdit::slotCompleteText(const QString & completionPrefix)
QString before(text().left(start));
QString after(text().mid(cursorPosition()));
block = true;
Base::FlagToggler<bool> flag(block,false);
setText(before + completionPrefix + after);
setCursorPosition(QString(before + completionPrefix).length());
block = false;
}
void ExpressionLineEdit::keyPressEvent(QKeyEvent *e) {
Base::FlagToggler<bool> flag(block,true);
QLineEdit::keyPressEvent(e);
}
///////////////////////////////////////////////////////////////////////
ExpressionTextEdit::ExpressionTextEdit(QWidget *parent)
: QPlainTextEdit(parent)
, completer(0)
, block(true)
{
connect(this, SIGNAL(textChanged()), this, SLOT(slotTextChanged()));
}
void ExpressionTextEdit::setDocumentObject(const App::DocumentObject * currentDocObj)
{
if (completer) {
delete completer;
completer = 0;
}
if (currentDocObj != 0) {
completer = new ExpressionCompleter(currentDocObj, this);
completer->setWidget(this);
completer->setCaseSensitivity(Qt::CaseInsensitive);
connect(completer, SIGNAL(activated(QString)), this, SLOT(slotCompleteText(QString)));
connect(completer, SIGNAL(highlighted(QString)), this, SLOT(slotCompleteText(QString)));
connect(this, SIGNAL(textChanged2(QString)), completer, SLOT(slotUpdate(QString)));
}
}
bool ExpressionTextEdit::completerActive() const
{
return completer && completer->popup() && completer->popup()->isVisible();
}
void ExpressionTextEdit::hideCompleter()
{
if (completer && completer->popup())
completer->popup()->setVisible(false);
}
void ExpressionTextEdit::slotTextChanged()
{
if (!block) {
QTextCursor cursor = textCursor();
Q_EMIT textChanged2(cursor.block().text().left(cursor.positionInBlock()));
}
}
void ExpressionTextEdit::slotCompleteText(const QString & completionPrefix)
{
QTextCursor cursor = textCursor();
int start = completer->getPrefixStart();
int pos = cursor.positionInBlock();
if(pos>=start) {
Base::FlagToggler<bool> flag(block,false);
if(pos>start)
cursor.movePosition(QTextCursor::PreviousCharacter,QTextCursor::KeepAnchor,pos-start);
cursor.insertText(completionPrefix);
}
}
void ExpressionTextEdit::keyPressEvent(QKeyEvent *e) {
Base::FlagToggler<bool> flag(block,true);
QPlainTextEdit::keyPressEvent(e);
}
#include "moc_ExpressionCompleter.cpp"