| 1 |
/*************************************************************************** |
| 2 |
* Copyright (c) 2004 Jürgen Riegel <[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 |
|
| 24 |
#include <QAction> |
| 25 |
#include <QActionGroup> |
| 26 |
#include <QApplication> |
| 27 |
#include <QContextMenuEvent> |
| 28 |
#include <QCursor> |
| 29 |
#include <QDir> |
| 30 |
#include <QFileInfo> |
| 31 |
#include <QHeaderView> |
| 32 |
#include <QMenu> |
| 33 |
#include <QMessageBox> |
| 34 |
#include <QPainter> |
| 35 |
#include <QPixmap> |
| 36 |
#include <QProcess> |
| 37 |
#include <QThread> |
| 38 |
#include <QTimer> |
| 39 |
#include <QToolTip> |
| 40 |
#include <QVBoxLayout> |
| 41 |
|
| 42 |
|
| 43 |
#include <Base/Console.h> |
| 44 |
#include <Base/Reader.h> |
| 45 |
#include <Base/Sequencer.h> |
| 46 |
#include <Base/Tools.h> |
| 47 |
#include <Base/Writer.h> |
| 48 |
|
| 49 |
#include <Base/Color.h> |
| 50 |
#include <App/Document.h> |
| 51 |
#include <App/DocumentObjectGroup.h> |
| 52 |
#include <App/AutoTransaction.h> |
| 53 |
#include <App/GeoFeatureGroupExtension.h> |
| 54 |
#include <App/Link.h> |
| 55 |
|
| 56 |
#include "Tree.h" |
| 57 |
#include "BitmapFactory.h" |
| 58 |
#include "Command.h" |
| 59 |
#include "Document.h" |
| 60 |
#include "ExpressionCompleter.h" |
| 61 |
#include "Macro.h" |
| 62 |
#include "MainWindow.h" |
| 63 |
#include "MenuManager.h" |
| 64 |
#include "TreeParams.h" |
| 65 |
#include "View3DInventor.h" |
| 66 |
#include "ViewProviderDocumentObject.h" |
| 67 |
#include "Widgets.h" |
| 68 |
#include "Workbench.h" |
| 69 |
|
| 70 |
|
| 71 |
FC_LOG_LEVEL_INIT("Tree", false, true, true) |
| 72 |
|
| 73 |
#define _TREE_PRINT(_level, _func, _msg) \ |
| 74 |
_FC_PRINT(FC_LOG_INSTANCE, _level, _func, '[' << getTreeName() << "] " << _msg) |
| 75 |
#define TREE_MSG(_msg) _TREE_PRINT(FC_LOGLEVEL_MSG, notify<Base::LogStyle::Message>, _msg) |
| 76 |
#define TREE_WARN(_msg) _TREE_PRINT(FC_LOGLEVEL_WARN, notify<Base::LogStyle::Warning>, _msg) |
| 77 |
#define TREE_ERR(_msg) _TREE_PRINT(FC_LOGLEVEL_ERR, notify<Base::LogStyle::Error>, _msg) |
| 78 |
#define TREE_LOG(_msg) _TREE_PRINT(FC_LOGLEVEL_LOG, notify<Base::LogStyle::Log>, _msg) |
| 79 |
#define TREE_TRACE(_msg) _TREE_PRINT(FC_LOGLEVEL_TRACE, notify<Base::LogStyle::Log>, _msg) |
| 80 |
|
| 81 |
using namespace Gui; |
| 82 |
namespace sp = std::placeholders; |
| 83 |
|
| 84 |
///////////////////////////////////////////////////////////////////////////////// |
| 85 |
|
| 86 |
std::unique_ptr<QPixmap> TreeWidget::documentPixmap; |
| 87 |
std::unique_ptr<QPixmap> TreeWidget::documentPartialPixmap; |
| 88 |
static QBrush _TreeItemBackground; |
| 89 |
std::set<TreeWidget*> TreeWidget::Instances; |
| 90 |
static TreeWidget* _LastSelectedTreeWidget; |
| 91 |
const int TreeWidget::DocumentType = 1000; |
| 92 |
const int TreeWidget::ObjectType = 1001; |
| 93 |
static bool _DraggingActive; |
| 94 |
static bool _DragEventFilter; |
| 95 |
|
| 96 |
static bool isVisibilityIconEnabled() |
| 97 |
{ |
| 98 |
return TreeParams::getVisibilityIcon(); |
| 99 |
} |
| 100 |
|
| 101 |
static bool isOnlyNameColumnDisplayed() |
| 102 |
{ |
| 103 |
return TreeParams::getHideInternalNames() && TreeParams::getHideColumn(); |
| 104 |
} |
| 105 |
|
| 106 |
static bool isSelectionCheckBoxesEnabled() |
| 107 |
{ |
| 108 |
return TreeParams::getCheckBoxesSelection(); |
| 109 |
} |
| 110 |
|
| 111 |
void TreeParams::onItemBackgroundChanged() |
| 112 |
{ |
| 113 |
if (getItemBackground()) { |
| 114 |
Base::Color color; |
| 115 |
color.setPackedValue(getItemBackground()); |
| 116 |
QColor col; |
| 117 |
col.setRedF(color.r); |
| 118 |
col.setGreenF(color.g); |
| 119 |
col.setBlueF(color.b); |
| 120 |
col.setAlphaF(color.a); |
| 121 |
_TreeItemBackground = QBrush(col); |
| 122 |
} |
| 123 |
else { |
| 124 |
_TreeItemBackground = QBrush(); |
| 125 |
} |
| 126 |
refreshTreeViews(); |
| 127 |
} |
| 128 |
|
| 129 |
////////////////////////////////////////////////////////////////////////////////////// |
| 130 |
struct Stats |
| 131 |
{ |
| 132 |
#define DEFINE_STATS \ |
| 133 |
DEFINE_STAT(testStatus1) \ |
| 134 |
DEFINE_STAT(testStatus2) \ |
| 135 |
DEFINE_STAT(testStatus3) \ |
| 136 |
DEFINE_STAT(getIcon) \ |
| 137 |
DEFINE_STAT(setIcon) |
| 138 |
|
| 139 |
#define DEFINE_STAT(_name) \ |
| 140 |
FC_DURATION_DECLARE(_name); \ |
| 141 |
int _name##_count; |
| 142 |
|
| 143 |
DEFINE_STATS |
| 144 |
|
| 145 |
void init() |
| 146 |
{ |
| 147 |
#undef DEFINE_STAT |
| 148 |
#define DEFINE_STAT(_name) \ |
| 149 |
FC_DURATION_INIT(_name); \ |
| 150 |
_name##_count = 0; |
| 151 |
|
| 152 |
DEFINE_STATS |
| 153 |
} |
| 154 |
|
| 155 |
void print() |
| 156 |
{ |
| 157 |
#undef DEFINE_STAT |
| 158 |
#define DEFINE_STAT(_name) FC_DURATION_MSG(_name, #_name " count: " << _name##_count); |
| 159 |
DEFINE_STATS |
| 160 |
} |
| 161 |
|
| 162 |
#undef DEFINE_STAT |
| 163 |
#define DEFINE_STAT(_name) \ |
| 164 |
void time_##_name(FC_TIME_POINT& t) \ |
| 165 |
{ \ |
| 166 |
++_name##_count; \ |
| 167 |
FC_DURATION_PLUS(_name, t); \ |
| 168 |
} |
| 169 |
|
| 170 |
DEFINE_STATS |
| 171 |
}; |
| 172 |
|
| 173 |
// static Stats _Stats; |
| 174 |
|
| 175 |
struct TimingInfo |
| 176 |
{ |
| 177 |
bool timed = false; |
| 178 |
FC_TIME_POINT t; |
| 179 |
FC_DURATION& d; |
| 180 |
explicit TimingInfo(FC_DURATION& d) |
| 181 |
: d(d) |
| 182 |
{ |
| 183 |
_FC_TIME_INIT(t); |
| 184 |
} |
| 185 |
~TimingInfo() |
| 186 |
{ |
| 187 |
stop(); |
| 188 |
} |
| 189 |
void stop() |
| 190 |
{ |
| 191 |
if (!timed) { |
| 192 |
timed = true; |
| 193 |
FC_DURATION_PLUS(d, t); |
| 194 |
} |
| 195 |
} |
| 196 |
void reset() |
| 197 |
{ |
| 198 |
stop(); |
| 199 |
_FC_TIME_INIT(t); |
| 200 |
} |
| 201 |
}; |
| 202 |
|
| 203 |
// #define DO_TIMING |
| 204 |
#ifdef DO_TIMING |
| 205 |
# define _Timing(_idx, _name) \ |
| 206 |
++_Stats._name##_count; \ |
| 207 |
TimingInfo _tt##_idx(_Stats._name) |
| 208 |
# define Timing(_name) _Timing(0, _name) |
| 209 |
# define _TimingStop(_idx, _name) _tt##_idx.stop(); |
| 210 |
# define TimingStop(_name) _TimingStop(0, _name); |
| 211 |
# define TimingInit() _Stats.init(); |
| 212 |
# define TimingPrint() _Stats.print(); |
| 213 |
#else |
| 214 |
# define _Timing(...) \ |
| 215 |
do { \ |
| 216 |
} while (0) |
| 217 |
# define Timing(...) \ |
| 218 |
do { \ |
| 219 |
} while (0) |
| 220 |
# define TimingInit() \ |
| 221 |
do { \ |
| 222 |
} while (0) |
| 223 |
# define TimingPrint() \ |
| 224 |
do { \ |
| 225 |
} while (0) |
| 226 |
# define _TimingStop(...) \ |
| 227 |
do { \ |
| 228 |
} while (0); |
| 229 |
# define TimingStop(...) \ |
| 230 |
do { \ |
| 231 |
} while (0); |
| 232 |
#endif |
| 233 |
|
| 234 |
// --------------------------------------------------------------------------- |
| 235 |
|
| 236 |
using DocumentObjectItems = std::set<DocumentObjectItem*>; |
| 237 |
|
| 238 |
class Gui::DocumentObjectData |
| 239 |
{ |
| 240 |
public: |
| 241 |
bool dirtyFlag {}; |
| 242 |
DocumentItem* docItem; |
| 243 |
DocumentObjectItems items; |
| 244 |
ViewProviderDocumentObject* viewObject; |
| 245 |
DocumentObjectItem* rootItem {nullptr}; |
| 246 |
std::vector<App::DocumentObject*> children; |
| 247 |
std::set<App::DocumentObject*> childSet; |
| 248 |
bool removeChildrenFromRoot; |
| 249 |
bool itemHidden; |
| 250 |
std::string label; |
| 251 |
std::string label2; |
| 252 |
std::string internalName; |
| 253 |
|
| 254 |
using Connection = boost::signals2::scoped_connection; |
| 255 |
|
| 256 |
Connection connectIcon; |
| 257 |
Connection connectTool; |
| 258 |
Connection connectStat; |
| 259 |
Connection connectHl; |
| 260 |
|
| 261 |
DocumentObjectData(DocumentItem* docItem, ViewProviderDocumentObject* vpd) |
| 262 |
: docItem(docItem) |
| 263 |
, viewObject(vpd) |
| 264 |
{ |
| 265 |
// NOLINTBEGIN |
| 266 |
// Setup connections |
| 267 |
connectIcon = viewObject->signalChangeIcon.connect( |
| 268 |
std::bind(&DocumentObjectData::slotChangeIcon, this) |
| 269 |
); |
| 270 |
connectTool = viewObject->signalChangeToolTip.connect( |
| 271 |
std::bind(&DocumentObjectData::slotChangeToolTip, this, sp::_1) |
| 272 |
); |
| 273 |
connectStat = viewObject->signalChangeStatusTip.connect( |
| 274 |
std::bind(&DocumentObjectData::slotChangeStatusTip, this, sp::_1) |
| 275 |
); |
| 276 |
connectHl = viewObject->signalChangeHighlight.connect( |
| 277 |
std::bind(&DocumentObjectData::slotChangeHighlight, this, sp::_1, sp::_2) |
| 278 |
); |
| 279 |
// NOLINTEND |
| 280 |
|
| 281 |
removeChildrenFromRoot = viewObject->canRemoveChildrenFromRoot(); |
| 282 |
itemHidden = !viewObject->showInTree(); |
| 283 |
label = viewObject->getObject()->Label.getValue(); |
| 284 |
label2 = viewObject->getObject()->Label2.getValue(); |
| 285 |
internalName = viewObject->getObject()->getNameInDocument(); |
| 286 |
} |
| 287 |
|
| 288 |
void insertItem(DocumentObjectItem* item) |
| 289 |
{ |
| 290 |
items.insert(item); |
| 291 |
dirtyFlag = true; |
| 292 |
} |
| 293 |
|
| 294 |
void removeItem(DocumentObjectItem* item) |
| 295 |
{ |
| 296 |
auto it = items.find(item); |
| 297 |
if (it == items.end()) { |
| 298 |
assert(0); |
| 299 |
} |
| 300 |
else { |
| 301 |
items.erase(it); |
| 302 |
dirtyFlag = true; |
| 303 |
} |
| 304 |
} |
| 305 |
|
| 306 |
const char* getTreeName() const |
| 307 |
{ |
| 308 |
return docItem->getTreeName(); |
| 309 |
} |
| 310 |
|
| 311 |
void updateChildren(DocumentObjectDataPtr other) |
| 312 |
{ |
| 313 |
children = other->children; |
| 314 |
childSet = other->childSet; |
| 315 |
} |
| 316 |
|
| 317 |
bool updateChildren(bool checkVisibility) |
| 318 |
{ |
| 319 |
auto newChildren = viewObject->claimChildren(); |
| 320 |
auto obj = viewObject->getObject(); |
| 321 |
std::set<App::DocumentObject*> newSet; |
| 322 |
bool updated = false; |
| 323 |
for (auto child : newChildren) { |
| 324 |
auto childVp = docItem->getViewProvider(child); |
| 325 |
if (!childVp) { |
| 326 |
continue; |
| 327 |
} |
| 328 |
if (child && child->isAttachedToDocument()) { |
| 329 |
if (!newSet.insert(child).second) { |
| 330 |
TREE_WARN( |
| 331 |
"duplicate child item " << obj->getFullName() << '.' |
| 332 |
<< child->getNameInDocument() |
| 333 |
); |
| 334 |
} |
| 335 |
else if (!childSet.erase(child)) { |
| 336 |
// this means new child detected |
| 337 |
updated = true; |
| 338 |
if (child->getDocument() == obj->getDocument() |
| 339 |
&& child->getDocument() == docItem->document()->getDocument()) { |
| 340 |
auto& parents = docItem->_ParentMap[child]; |
| 341 |
if (parents.insert(obj).second && child->Visibility.getValue()) { |
| 342 |
bool showable = false; |
| 343 |
for (auto parent : parents) { |
| 344 |
if (!parent->hasChildElement() |
| 345 |
&& parent->getLinkedObject(false) == parent) { |
| 346 |
showable = true; |
| 347 |
break; |
| 348 |
} |
| 349 |
} |
| 350 |
childVp->setShowable(showable); |
| 351 |
} |
| 352 |
} |
| 353 |
} |
| 354 |
} |
| 355 |
} |
| 356 |
for (auto child : childSet) { |
| 357 |
if (newSet.find(child) == newSet.end()) { |
| 358 |
// this means old child removed |
| 359 |
updated = true; |
| 360 |
auto mapIt = docItem->_ParentMap.find(child); |
| 361 |
|
| 362 |
// If 'child' is not part of the map then it has already been deleted |
| 363 |
// in _slotDeleteObject. |
| 364 |
if (mapIt != docItem->_ParentMap.end()) { |
| 365 |
docItem->_ParentMap[child].erase(obj); |
| 366 |
|
| 367 |
auto childVp = docItem->getViewProvider(child); |
| 368 |
if (childVp && child->getDocument() == obj->getDocument()) { |
| 369 |
childVp->setShowable(docItem->isObjectShowable(child)); |
| 370 |
} |
| 371 |
} |
| 372 |
} |
| 373 |
} |
| 374 |
// We still need to check the order of the children |
| 375 |
updated = updated || children != newChildren; |
| 376 |
children.swap(newChildren); |
| 377 |
childSet.swap(newSet); |
| 378 |
|
| 379 |
if (updated && checkVisibility) { |
| 380 |
for (auto child : children) { |
| 381 |
auto childVp = docItem->getViewProvider(child); |
| 382 |
if (childVp && child->getDocument() == obj->getDocument()) { |
| 383 |
childVp->setShowable(docItem->isObjectShowable(child)); |
| 384 |
} |
| 385 |
} |
| 386 |
} |
| 387 |
return updated; |
| 388 |
} |
| 389 |
|
| 390 |
void testStatus(bool resetStatus = false) |
| 391 |
{ |
| 392 |
QIcon icon, icon2; |
| 393 |
for (auto item : items) { |
| 394 |
item->testStatus(resetStatus, icon, icon2); |
| 395 |
} |
| 396 |
} |
| 397 |
|
| 398 |
void slotChangeIcon() |
| 399 |
{ |
| 400 |
testStatus(true); |
| 401 |
} |
| 402 |
|
| 403 |
void slotChangeToolTip(const QString& tip) |
| 404 |
{ |
| 405 |
for (auto item : items) { |
| 406 |
item->setToolTip(0, tip); |
| 407 |
} |
| 408 |
} |
| 409 |
|
| 410 |
void slotChangeStatusTip(const QString& tip) |
| 411 |
{ |
| 412 |
for (auto item : items) { |
| 413 |
item->setStatusTip(0, tip); |
| 414 |
} |
| 415 |
} |
| 416 |
|
| 417 |
void slotChangeHighlight(bool set, Gui::HighlightMode mode) |
| 418 |
{ |
| 419 |
for (auto item : items) { |
| 420 |
item->setHighlight(set, mode); |
| 421 |
} |
| 422 |
} |
| 423 |
}; |
| 424 |
|
| 425 |
// --------------------------------------------------------------------------- |
| 426 |
|
| 427 |
class DocumentItem::ExpandInfo: public std::unordered_map<std::string, DocumentItem::ExpandInfoPtr> |
| 428 |
{ |
| 429 |
public: |
| 430 |
void restore(Base::XMLReader& reader) |
| 431 |
{ |
| 432 |
int level = reader.level(); |
| 433 |
int count = reader.getAttribute<long>("count"); |
| 434 |
for (int i = 0; i < count; ++i) { |
| 435 |
reader.readElement("Expand"); |
| 436 |
auto& entry = (*this)[reader.getAttribute<const char*>("name")]; |
| 437 |
if (!reader.hasAttribute("count")) { |
| 438 |
continue; |
| 439 |
} |
| 440 |
entry.reset(new ExpandInfo); |
| 441 |
entry->restore(reader); |
| 442 |
} |
| 443 |
reader.readEndElement("Expand", level - 1); |
| 444 |
} |
| 445 |
}; |
| 446 |
|
| 447 |
// --------------------------------------------------------------------------- |
| 448 |
|
| 449 |
namespace Gui |
| 450 |
{ |
| 451 |
/** |
| 452 |
* TreeWidget item delegate for editing |
| 453 |
*/ |
| 454 |
class TreeWidgetItemDelegate: public QStyledItemDelegate |
| 455 |
{ |
| 456 |
typedef QStyledItemDelegate inherited; |
| 457 |
|
| 458 |
// Beware, big scary hack incoming! |
| 459 |
// |
| 460 |
// This is artificial QTreeWidget that is not rendered and its sole goal is to be the source |
| 461 |
// of style information that can be manipulated using QSS. From Qt6.5 tree branches also |
| 462 |
// have rendered background using ::item sub-control. Whole row also gets background from |
| 463 |
// the same sub-control. Only way to prevent this is to disable background of ::item, |
| 464 |
// this however limits our ability to style tree items. As solution we create this widget |
| 465 |
// that will be for painter to read information and draw proper backgrounds only when asked. |
| 466 |
// |
| 467 |
// More information: https://github.com/FreeCAD/FreeCAD/pull/13807 |
| 468 |
QTreeView* artificial; |
| 469 |
|
| 470 |
QRect calculateItemRect(const QStyleOptionViewItem& option) const; |
| 471 |
|
| 472 |
public: |
| 473 |
explicit TreeWidgetItemDelegate(QObject* parent = nullptr); |
| 474 |
|
| 475 |
virtual QWidget* createEditor( |
| 476 |
QWidget* parent, |
| 477 |
const QStyleOptionViewItem&, |
| 478 |
const QModelIndex& index |
| 479 |
) const; |
| 480 |
|
| 481 |
virtual QSize sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const; |
| 482 |
|
| 483 |
virtual void initStyleOption(QStyleOptionViewItem* option, const QModelIndex& index) const; |
| 484 |
|
| 485 |
virtual void paint( |
| 486 |
QPainter* painter, |
| 487 |
const QStyleOptionViewItem& option, |
| 488 |
const QModelIndex& index |
| 489 |
) const; |
| 490 |
}; |
| 491 |
|
| 492 |
} // namespace Gui |
| 493 |
|
| 494 |
TreeWidgetItemDelegate::TreeWidgetItemDelegate(QObject* parent) |
| 495 |
: QStyledItemDelegate(parent) |
| 496 |
{ |
| 497 |
artificial = new QTreeView(qobject_cast<QWidget*>(parent)); |
| 498 |
artificial->setObjectName(QStringLiteral("DocumentTreeItems")); |
| 499 |
artificial->setFixedSize(0, 0); // ensure that it does not render |
| 500 |
} |
| 501 |
|
| 502 |
|
| 503 |
QRect TreeWidgetItemDelegate::calculateItemRect(const QStyleOptionViewItem& option) const |
| 504 |
{ |
| 505 |
auto tree = static_cast<TreeWidget*>(parent()); |
| 506 |
auto style = tree->style(); |
| 507 |
|
| 508 |
QRect rect = option.rect; |
| 509 |
|
| 510 |
const int margin = style->pixelMetric(QStyle::PM_FocusFrameHMargin, &option, artificial) + 1; |
| 511 |
|
| 512 |
// 2 margin for text, 2 margin for decoration (icon) = 4 times margin |
| 513 |
int width = 4 * margin + option.fontMetrics.boundingRect(option.text).width() |
| 514 |
+ option.decorationSize.width() + TreeParams::getItemBackgroundPadding(); |
| 515 |
|
| 516 |
if (TreeParams::getCheckBoxesSelection()) { |
| 517 |
// another 2 margin for checkbox |
| 518 |
width += 2 * margin + style->pixelMetric(QStyle::PM_IndicatorWidth) |
| 519 |
+ style->pixelMetric(QStyle::PM_LayoutHorizontalSpacing); |
| 520 |
} |
| 521 |
|
| 522 |
if (width < rect.width()) { |
| 523 |
rect.setWidth(width); |
| 524 |
} |
| 525 |
|
| 526 |
return rect; |
| 527 |
} |
| 528 |
|
| 529 |
void TreeWidgetItemDelegate::paint( |
| 530 |
QPainter* painter, |
| 531 |
const QStyleOptionViewItem& option, |
| 532 |
const QModelIndex& index |
| 533 |
) const |
| 534 |
{ |
| 535 |
QStyleOptionViewItem opt = option; |
| 536 |
initStyleOption(&opt, index); |
| 537 |
|
| 538 |
auto tree = static_cast<TreeWidget*>(parent()); |
| 539 |
auto style = tree->style(); |
| 540 |
|
| 541 |
// If only the first column is shown, we'll trim the color background when |
| 542 |
// rendering as transparent overlay. |
| 543 |
bool trimColumnSize = isOnlyNameColumnDisplayed(); |
| 544 |
|
| 545 |
if (index.column() == 0) { |
| 546 |
if (tree->testAttribute(Qt::WA_NoSystemBackground) |
| 547 |
&& (trimColumnSize |
| 548 |
|| (opt.backgroundBrush.style() == Qt::NoBrush |
| 549 |
&& _TreeItemBackground.style() != Qt::NoBrush))) { |
| 550 |
QRect rect = calculateItemRect(option); |
| 551 |
|
| 552 |
if (trimColumnSize && opt.backgroundBrush.style() == Qt::NoBrush) { |
| 553 |
painter->fillRect(rect, _TreeItemBackground); |
| 554 |
} |
| 555 |
else if (!opt.state.testFlag(QStyle::State_Selected)) { |
| 556 |
painter->fillRect(rect, _TreeItemBackground); |
| 557 |
} |
| 558 |
} |
| 559 |
} |
| 560 |
style->drawControl(QStyle::CE_ItemViewItem, &opt, painter, artificial); |
| 561 |
} |
| 562 |
|
| 563 |
void TreeWidgetItemDelegate::initStyleOption(QStyleOptionViewItem* option, const QModelIndex& index) const |
| 564 |
{ |
| 565 |
inherited::initStyleOption(option, index); |
| 566 |
|
| 567 |
auto tree = static_cast<TreeWidget*>(parent()); |
| 568 |
auto item = tree->itemFromIndex(index); |
| 569 |
|
| 570 |
if (!item) { |
| 571 |
return; |
| 572 |
} |
| 573 |
|
| 574 |
// Clear State_Enabled for hidden objects so QSS ::item:disabled rules can |
| 575 |
// override the overlay stylesheet's blanket ::item { color } for text fading. |
| 576 |
if (item->type() == TreeWidget::ObjectType) { |
| 577 |
if (auto* docItem = static_cast<DocumentObjectItem*>(item); |
| 578 |
docItem->object() && !docItem->object()->isShow()) { |
| 579 |
option->state &= ~QStyle::State_Enabled; |
| 580 |
} |
| 581 |
} |
| 582 |
|
| 583 |
option->textElideMode = Qt::ElideMiddle; |
| 584 |
auto mousePos = option->widget->mapFromGlobal(QCursor::pos()); |
| 585 |
auto isHovered = option->rect.contains(mousePos); |
| 586 |
if (!isHovered) { |
| 587 |
option->state &= ~QStyle::State_MouseOver; |
| 588 |
} |
| 589 |
|
| 590 |
QSize size = option->icon.actualSize(QSize(0xffff, 0xffff)); |
| 591 |
|
| 592 |
if (size.height() > 0) { |
| 593 |
option->decorationSize |
| 594 |
= QSize(size.width() * TreeWidget::iconSize() / size.height(), TreeWidget::iconSize()); |
| 595 |
} |
| 596 |
|
| 597 |
if (isOnlyNameColumnDisplayed()) { |
| 598 |
option->rect = calculateItemRect(*option); |
| 599 |
|
| 600 |
// we need to extend this shape a bit, 3px on each side |
| 601 |
// this value was obtained experimentally |
| 602 |
option->rect.setWidth(option->rect.width() + 3 * 2); |
| 603 |
} |
| 604 |
} |
| 605 |
|
| 606 |
class DynamicQLineEdit: public ExpLineEdit |
| 607 |
{ |
| 608 |
public: |
| 609 |
DynamicQLineEdit(QWidget* parent = nullptr) |
| 610 |
: ExpLineEdit(parent) |
| 611 |
{} |
| 612 |
|
| 613 |
QSize sizeHint() const override |
| 614 |
{ |
| 615 |
QSize size = QLineEdit::sizeHint(); |
| 616 |
QFontMetrics fm(font()); |
| 617 |
int availableWidth = parentWidget()->width() - geometry().x(); // Calculate available width |
| 618 |
int margin = 2 * (style()->pixelMetric(QStyle::PM_FocusFrameHMargin) + 1) |
| 619 |
+ 2 * style()->pixelMetric(QStyle::PM_LayoutHorizontalSpacing) |
| 620 |
+ TreeParams::getItemBackgroundPadding(); |
| 621 |
size.setWidth(std::min(fm.horizontalAdvance(text()) + margin, availableWidth)); |
| 622 |
return size; |
| 623 |
} |
| 624 |
|
| 625 |
// resize on key presses |
| 626 |
void keyPressEvent(QKeyEvent* event) override |
| 627 |
{ |
| 628 |
ExpLineEdit::keyPressEvent(event); |
| 629 |
setMinimumWidth(sizeHint().width()); |
| 630 |
} |
| 631 |
}; |
| 632 |
|
| 633 |
QWidget* TreeWidgetItemDelegate::createEditor( |
| 634 |
QWidget* parent, |
| 635 |
const QStyleOptionViewItem&, |
| 636 |
const QModelIndex& index |
| 637 |
) const |
| 638 |
{ |
| 639 |
auto ti = static_cast<QTreeWidgetItem*>(index.internalPointer()); |
| 640 |
if (ti->type() != TreeWidget::ObjectType || index.column() > 1) { |
| 641 |
return nullptr; |
| 642 |
} |
| 643 |
auto item = static_cast<DocumentObjectItem*>(ti); |
| 644 |
App::DocumentObject* obj = item->object()->getObject(); |
| 645 |
auto& prop = index.column() ? obj->Label2 : obj->Label; |
| 646 |
|
| 647 |
DynamicQLineEdit* editor; |
| 648 |
if (TreeParams::getLabelExpression()) { |
| 649 |
DynamicQLineEdit* le = new DynamicQLineEdit(parent); |
| 650 |
le->setAutoApply(true); |
| 651 |
le->setFrame(false); |
| 652 |
le->bind(App::ObjectIdentifier(prop)); |
| 653 |
editor = le; |
| 654 |
} |
| 655 |
else { |
| 656 |
editor = new DynamicQLineEdit(parent); |
| 657 |
} |
| 658 |
editor->setReadOnly(prop.isReadOnly()); |
| 659 |
return editor; |
| 660 |
} |
| 661 |
|
| 662 |
QSize TreeWidgetItemDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const |
| 663 |
{ |
| 664 |
QSize size = QStyledItemDelegate::sizeHint(option, index); |
| 665 |
int spacing = std::max(0, static_cast<int>(TreeParams::getItemSpacing())); |
| 666 |
size.setHeight(size.height() + spacing); |
| 667 |
return size; |
| 668 |
} |
| 669 |
// --------------------------------------------------------------------------- |
| 670 |
|
| 671 |
TreeWidget::TreeWidget(const char* name, QWidget* parent) |
| 672 |
: QTreeWidget(parent) |
| 673 |
, SelectionObserver(true, ResolveMode::NoResolve) |
| 674 |
, contextItem(nullptr) |
| 675 |
, searchObject(nullptr) |
| 676 |
, searchDoc(nullptr) |
| 677 |
, searchContextDoc(nullptr) |
| 678 |
, editingItem(nullptr) |
| 679 |
, currentDocItem(nullptr) |
| 680 |
, myName(name) |
| 681 |
{ |
| 682 |
Instances.insert(this); |
| 683 |
if (!_LastSelectedTreeWidget) { |
| 684 |
_LastSelectedTreeWidget = this; |
| 685 |
} |
| 686 |
|
| 687 |
this->setDragEnabled(true); |
| 688 |
this->setAcceptDrops(true); |
| 689 |
this->setColumnCount(3); |
| 690 |
this->setItemDelegate(new TreeWidgetItemDelegate(this)); |
| 691 |
this->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); |
| 692 |
|
| 693 |
this->showHiddenAction = new QAction(this); |
| 694 |
this->showHiddenAction->setCheckable(true); |
| 695 |
connect(this->showHiddenAction, &QAction::triggered, this, &TreeWidget::onShowHidden); |
| 696 |
|
| 697 |
this->toggleVisibilityInTreeAction = new QAction(this); |
| 698 |
connect( |
| 699 |
this->toggleVisibilityInTreeAction, |
| 700 |
&QAction::triggered, |
| 701 |
this, |
| 702 |
&TreeWidget::onToggleVisibilityInTree |
| 703 |
); |
| 704 |
|
| 705 |
this->createGroupAction = new QAction(this); |
| 706 |
connect(this->createGroupAction, &QAction::triggered, this, &TreeWidget::onCreateGroup); |
| 707 |
|
| 708 |
this->relabelObjectAction = new QAction(this); |
| 709 |
#ifndef Q_OS_MAC |
| 710 |
this->relabelObjectAction->setShortcut(Qt::Key_F2); |
| 711 |
#endif |
| 712 |
connect(this->relabelObjectAction, &QAction::triggered, this, &TreeWidget::onRelabelObject); |
| 713 |
|
| 714 |
this->finishEditingAction = new QAction(this); |
| 715 |
connect(this->finishEditingAction, &QAction::triggered, this, &TreeWidget::onFinishEditing); |
| 716 |
|
| 717 |
this->selectDependentsAction = new QAction(this); |
| 718 |
connect(this->selectDependentsAction, &QAction::triggered, this, &TreeWidget::onSelectDependents); |
| 719 |
|
| 720 |
this->closeDocAction = new QAction(this); |
| 721 |
connect(this->closeDocAction, &QAction::triggered, this, &TreeWidget::onCloseDoc); |
| 722 |
|
| 723 |
this->reloadDocAction = new QAction(this); |
| 724 |
connect(this->reloadDocAction, &QAction::triggered, this, &TreeWidget::onReloadDoc); |
| 725 |
|
| 726 |
this->skipRecomputeAction = new QAction(this); |
| 727 |
this->skipRecomputeAction->setCheckable(true); |
| 728 |
connect(this->skipRecomputeAction, &QAction::toggled, this, &TreeWidget::onSkipRecompute); |
| 729 |
this->skipRecomputeCommand = Gui::Application::Instance->commandManager().getCommandByName( |
| 730 |
"Std_ToggleSkipRecompute" |
| 731 |
); |
| 732 |
|
| 733 |
this->allowPartialRecomputeAction = new QAction(this); |
| 734 |
this->allowPartialRecomputeAction->setCheckable(true); |
| 735 |
connect( |
| 736 |
this->allowPartialRecomputeAction, |
| 737 |
&QAction::toggled, |
| 738 |
this, |
| 739 |
&TreeWidget::onAllowPartialRecompute |
| 740 |
); |
| 741 |
|
| 742 |
this->markRecomputeAction = new QAction(this); |
| 743 |
connect(this->markRecomputeAction, &QAction::triggered, this, &TreeWidget::onMarkRecompute); |
| 744 |
|
| 745 |
this->recomputeObjectAction = new QAction(this); |
| 746 |
connect(this->recomputeObjectAction, &QAction::triggered, this, &TreeWidget::onRecomputeObject); |
| 747 |
this->searchObjectsAction = new QAction(this); |
| 748 |
this->searchObjectsAction->setText(tr("Search Objects")); |
| 749 |
this->searchObjectsAction->setStatusTip(tr("Searches for objects in the tree")); |
| 750 |
connect(this->searchObjectsAction, &QAction::triggered, this, &TreeWidget::onSearchObjects); |
| 751 |
|
| 752 |
this->openFileLocationAction = new QAction(this); |
| 753 |
connect(this->openFileLocationAction, &QAction::triggered, this, &TreeWidget::onOpenFileLocation); |
| 754 |
|
| 755 |
// NOLINTBEGIN |
| 756 |
// Setup connections |
| 757 |
connectNewDocument = Application::Instance->signalNewDocument.connect( |
| 758 |
std::bind(&TreeWidget::slotNewDocument, this, sp::_1, sp::_2) |
| 759 |
); |
| 760 |
connectDelDocument = Application::Instance->signalDeleteDocument.connect( |
| 761 |
std::bind(&TreeWidget::slotDeleteDocument, this, sp::_1) |
| 762 |
); |
| 763 |
connectRenDocument = Application::Instance->signalRenameDocument.connect( |
| 764 |
std::bind(&TreeWidget::slotRenameDocument, this, sp::_1) |
| 765 |
); |
| 766 |
connectActDocument = Application::Instance->signalActiveDocument.connect( |
| 767 |
std::bind(&TreeWidget::slotActiveDocument, this, sp::_1) |
| 768 |
); |
| 769 |
connectRelDocument = Application::Instance->signalRelabelDocument.connect( |
| 770 |
std::bind(&TreeWidget::slotRelabelDocument, this, sp::_1) |
| 771 |
); |
| 772 |
connectShowHidden = Application::Instance->signalShowHidden.connect( |
| 773 |
std::bind(&TreeWidget::slotShowHidden, this, sp::_1) |
| 774 |
); |
| 775 |
|
| 776 |
// Gui::Document::signalChangedObject informs the App::Document property |
| 777 |
// change, not view provider's own property, which is what the signal below |
| 778 |
// for |
| 779 |
connectChangedViewObj = Application::Instance->signalChangedObject.connect( |
| 780 |
std::bind(&TreeWidget::slotChangedViewObject, this, sp::_1, sp::_2) |
| 781 |
); |
| 782 |
// NOLINTEND |
| 783 |
|
| 784 |
setupResizableColumn(this); |
| 785 |
this->header()->setStretchLastSection(true); |
| 786 |
QObject::connect(this->header(), &QHeaderView::sectionResized, [](int idx, int, int newSize) { |
| 787 |
if (idx == 1) { |
| 788 |
TreeParams::setColumnSize2(newSize); |
| 789 |
} |
| 790 |
else if (idx == 2) { |
| 791 |
TreeParams::setColumnSize3(newSize); |
| 792 |
} |
| 793 |
else { |
| 794 |
TreeParams::setColumnSize1(newSize); |
| 795 |
} |
| 796 |
}); |
| 797 |
|
| 798 |
// Add the first main label |
| 799 |
this->rootItem = invisibleRootItem(); |
| 800 |
this->expandItem(this->rootItem); |
| 801 |
this->setSelectionMode(QAbstractItemView::ExtendedSelection); |
| 802 |
|
| 803 |
this->setMouseTracking(true); // needed for itemEntered() to work |
| 804 |
|
| 805 |
|
| 806 |
this->preselectTimer = new QTimer(this); |
| 807 |
this->preselectTimer->setSingleShot(true); |
| 808 |
|
| 809 |
this->statusTimer = new QTimer(this); |
| 810 |
this->statusTimer->setSingleShot(false); |
| 811 |
|
| 812 |
this->selectTimer = new QTimer(this); |
| 813 |
this->selectTimer->setSingleShot(true); |
| 814 |
|
| 815 |
connect(this->statusTimer, &QTimer::timeout, this, &TreeWidget::onUpdateStatus); |
| 816 |
connect(this, &QTreeWidget::itemEntered, this, &TreeWidget::onItemEntered); |
| 817 |
connect(this, &QTreeWidget::itemCollapsed, this, &TreeWidget::onItemCollapsed); |
| 818 |
connect(this, &QTreeWidget::itemExpanded, this, &TreeWidget::onItemExpanded); |
| 819 |
connect(this, &QTreeWidget::itemSelectionChanged, this, &TreeWidget::onItemSelectionChanged); |
| 820 |
connect(this, &QTreeWidget::itemChanged, this, &TreeWidget::onItemChanged); |
| 821 |
connect(this->preselectTimer, &QTimer::timeout, this, &TreeWidget::onPreSelectTimer); |
| 822 |
connect(this->selectTimer, &QTimer::timeout, this, &TreeWidget::onSelectTimer); |
| 823 |
preselectTime.start(); |
| 824 |
|
| 825 |
setupText(); |
| 826 |
if (!documentPixmap) { |
| 827 |
documentPixmap = std::make_unique<QPixmap>(Gui::BitmapFactory().pixmap("Document")); |
| 828 |
QIcon icon(*documentPixmap); |
| 829 |
documentPartialPixmap = std::make_unique<QPixmap>( |
| 830 |
icon.pixmap(documentPixmap->size(), QIcon::Disabled) |
| 831 |
); |
| 832 |
} |
| 833 |
setColumnHidden(1, TreeParams::getHideColumn()); |
| 834 |
setColumnHidden(2, TreeParams::getHideInternalNames()); |
| 835 |
header()->setVisible(!TreeParams::getHideColumn() || !TreeParams::getHideInternalNames()); |
| 836 |
TreeParams::onFontSizeChanged(); |
| 837 |
} |
| 838 |
|
| 839 |
TreeWidget::~TreeWidget() |
| 840 |
{ |
| 841 |
connectNewDocument.disconnect(); |
| 842 |
connectDelDocument.disconnect(); |
| 843 |
connectRenDocument.disconnect(); |
| 844 |
connectActDocument.disconnect(); |
| 845 |
connectRelDocument.disconnect(); |
| 846 |
connectShowHidden.disconnect(); |
| 847 |
connectChangedViewObj.disconnect(); |
| 848 |
Instances.erase(this); |
| 849 |
if (_LastSelectedTreeWidget == this) { |
| 850 |
_LastSelectedTreeWidget = nullptr; |
| 851 |
} |
| 852 |
} |
| 853 |
|
| 854 |
const char* TreeWidget::getTreeName() const |
| 855 |
{ |
| 856 |
return myName.c_str(); |
| 857 |
} |
| 858 |
|
| 859 |
// reimpelement to select only objects in the active document |
| 860 |
void TreeWidget::selectAll() |
| 861 |
{ |
| 862 |
auto gdoc = Application::Instance->getDocument(App::GetApplication().getActiveDocument()); |
| 863 |
if (!gdoc) { |
| 864 |
return; |
| 865 |
} |
| 866 |
auto itDoc = DocumentMap.find(gdoc); |
| 867 |
if (itDoc == DocumentMap.end()) { |
| 868 |
return; |
| 869 |
} |
| 870 |
if (TreeParams::getRecordSelection()) { |
| 871 |
Gui::Selection().selStackPush(); |
| 872 |
} |
| 873 |
Gui::Selection().clearSelection(); |
| 874 |
Gui::Selection().setSelection(gdoc->getDocument()->getName(), gdoc->getDocument()->getObjects()); |
| 875 |
} |
| 876 |
|
| 877 |
bool TreeWidget::isObjectShowable(App::DocumentObject* obj) |
| 878 |
{ |
| 879 |
if (!obj || !obj->isAttachedToDocument()) { |
| 880 |
return true; |
| 881 |
} |
| 882 |
Gui::Document* doc = Application::Instance->getDocument(obj->getDocument()); |
| 883 |
if (!doc) { |
| 884 |
return true; |
| 885 |
} |
| 886 |
if (Instances.empty()) { |
| 887 |
return true; |
| 888 |
} |
| 889 |
auto tree = *Instances.begin(); |
| 890 |
auto it = tree->DocumentMap.find(doc); |
| 891 |
if (it != tree->DocumentMap.end()) { |
| 892 |
return it->second->isObjectShowable(obj); |
| 893 |
} |
| 894 |
return true; |
| 895 |
} |
| 896 |
|
| 897 |
static bool _DisableCheckTopParent; |
| 898 |
|
| 899 |
void TreeWidget::checkTopParent(App::DocumentObject*& obj, std::string& subname) |
| 900 |
{ |
| 901 |
if (_DisableCheckTopParent) { |
| 902 |
return; |
| 903 |
} |
| 904 |
if (!Instances.empty() && obj && obj->isAttachedToDocument()) { |
| 905 |
auto tree = *Instances.begin(); |
| 906 |
auto it = tree->DocumentMap.find(Application::Instance->getDocument(obj->getDocument())); |
| 907 |
if (it != tree->DocumentMap.end()) { |
| 908 |
if (tree->statusTimer->isActive()) { |
| 909 |
bool locked = tree->blockSelection(true); |
| 910 |
tree->_updateStatus(false); |
| 911 |
tree->blockSelection(locked); |
| 912 |
} |
| 913 |
auto parent = it->second->getTopParent(obj, subname); |
| 914 |
if (parent) { |
| 915 |
obj = parent; |
| 916 |
} |
| 917 |
} |
| 918 |
} |
| 919 |
} |
| 920 |
|
| 921 |
void TreeWidget::resetItemSearch() |
| 922 |
{ |
| 923 |
if (!searchObject) { |
| 924 |
return; |
| 925 |
} |
| 926 |
auto it = ObjectTable.find(searchObject); |
| 927 |
if (it != ObjectTable.end()) { |
| 928 |
for (auto& data : it->second) { |
| 929 |
if (!data) { |
| 930 |
continue; |
| 931 |
} |
| 932 |
for (auto item : data->items) { |
| 933 |
static_cast<DocumentObjectItem*>(item)->restoreBackground(); |
| 934 |
} |
| 935 |
} |
| 936 |
} |
| 937 |
searchObject = nullptr; |
| 938 |
} |
| 939 |
|
| 940 |
void TreeWidget::startItemSearch(QLineEdit* edit) |
| 941 |
{ |
| 942 |
resetItemSearch(); |
| 943 |
searchDoc = nullptr; |
| 944 |
searchContextDoc = nullptr; |
| 945 |
auto sels = selectedItems(); |
| 946 |
if (sels.size() == 1) { |
| 947 |
if (sels.front()->type() == DocumentType) { |
| 948 |
searchDoc = static_cast<DocumentItem*>(sels.front())->document(); |
| 949 |
} |
| 950 |
else if (sels.front()->type() == ObjectType) { |
| 951 |
auto item = static_cast<DocumentObjectItem*>(sels.front()); |
| 952 |
searchDoc = item->object()->getDocument(); |
| 953 |
searchContextDoc = item->getOwnerDocument()->document(); |
| 954 |
} |
| 955 |
} |
| 956 |
else { |
| 957 |
searchDoc = Application::Instance->activeDocument(); |
| 958 |
} |
| 959 |
|
| 960 |
App::DocumentObject* obj = nullptr; |
| 961 |
if (searchContextDoc && !searchContextDoc->getDocument()->getObjects().empty()) { |
| 962 |
obj = searchContextDoc->getDocument()->getObjects().front(); |
| 963 |
} |
| 964 |
else if (searchDoc && !searchDoc->getDocument()->getObjects().empty()) { |
| 965 |
obj = searchDoc->getDocument()->getObjects().front(); |
| 966 |
} |
| 967 |
|
| 968 |
if (obj) { |
| 969 |
static_cast<ExpressionLineEdit*>(edit)->setDocumentObject(obj); |
| 970 |
} |
| 971 |
} |
| 972 |
|
| 973 |
void TreeWidget::itemSearch(const QString& text, bool select) |
| 974 |
{ |
| 975 |
resetItemSearch(); |
| 976 |
|
| 977 |
auto docItem = getDocumentItem(searchDoc); |
| 978 |
if (!docItem) { |
| 979 |
docItem = getDocumentItem(Application::Instance->activeDocument()); |
| 980 |
if (!docItem) { |
| 981 |
FC_TRACE("item search no document"); |
| 982 |
resetItemSearch(); |
| 983 |
return; |
| 984 |
} |
| 985 |
} |
| 986 |
|
| 987 |
auto doc = docItem->document()->getDocument(); |
| 988 |
const auto& objs = doc->getObjects(); |
| 989 |
if (objs.empty()) { |
| 990 |
FC_TRACE("item search no objects"); |
| 991 |
return; |
| 992 |
} |
| 993 |
std::string txt(text.toUtf8().constData()); |
| 994 |
try { |
| 995 |
if (txt.empty()) { |
| 996 |
return; |
| 997 |
} |
| 998 |
if (txt.find("<<") == std::string::npos) { |
| 999 |
auto pos = txt.find('.'); |
| 1000 |
if (pos == std::string::npos) { |
| 1001 |
txt += '.'; |
| 1002 |
} |
| 1003 |
else if (pos != txt.size() - 1) { |
| 1004 |
txt.insert(pos + 1, "<<"); |
| 1005 |
if (txt.back() != '.') { |
| 1006 |
txt += '.'; |
| 1007 |
} |
| 1008 |
txt += ">>."; |
| 1009 |
} |
| 1010 |
} |
| 1011 |
else if (txt.back() != '.') { |
| 1012 |
txt += '.'; |
| 1013 |
} |
| 1014 |
txt += "_self"; |
| 1015 |
auto path = App::ObjectIdentifier::parse(objs.front(), txt); |
| 1016 |
if (path.getPropertyName() != "_self") { |
| 1017 |
FC_TRACE("Object " << txt << " not found in " << doc->getName()); |
| 1018 |
return; |
| 1019 |
} |
| 1020 |
auto obj = path.getDocumentObject(); |
| 1021 |
if (!obj) { |
| 1022 |
FC_TRACE("Object " << txt << " not found in " << doc->getName()); |
| 1023 |
return; |
| 1024 |
} |
| 1025 |
std::string subname = path.getSubObjectName(); |
| 1026 |
App::DocumentObject* parent = nullptr; |
| 1027 |
if (searchContextDoc) { |
| 1028 |
auto it = DocumentMap.find(searchContextDoc); |
| 1029 |
if (it != DocumentMap.end()) { |
| 1030 |
parent = it->second->getTopParent(obj, subname); |
| 1031 |
if (parent) { |
| 1032 |
obj = parent; |
| 1033 |
docItem = it->second; |
| 1034 |
doc = docItem->document()->getDocument(); |
| 1035 |
} |
| 1036 |
} |
| 1037 |
} |
| 1038 |
if (!parent) { |
| 1039 |
parent = docItem->getTopParent(obj, subname); |
| 1040 |
while (!parent) { |
| 1041 |
if (docItem->document()->getDocument() == obj->getDocument()) { |
| 1042 |
// this shouldn't happen |
| 1043 |
FC_LOG("Object " << txt << " not found in " << doc->getName()); |
| 1044 |
return; |
| 1045 |
} |
| 1046 |
auto it = DocumentMap.find(Application::Instance->getDocument(obj->getDocument())); |
| 1047 |
if (it == DocumentMap.end()) { |
| 1048 |
return; |
| 1049 |
} |
| 1050 |
docItem = it->second; |
| 1051 |
parent = docItem->getTopParent(obj, subname); |
| 1052 |
} |
| 1053 |
obj = parent; |
| 1054 |
} |
| 1055 |
auto item = docItem->findItemByObject(true, obj, subname.c_str()); |
| 1056 |
if (!item) { |
| 1057 |
FC_TRACE("item " << txt << " not found in " << doc->getName()); |
| 1058 |
return; |
| 1059 |
} |
| 1060 |
scrollToItem(item); |
| 1061 |
Selection().setPreselect( |
| 1062 |
obj->getDocument()->getName(), |
| 1063 |
obj->getNameInDocument(), |
| 1064 |
subname.c_str(), |
| 1065 |
0, |
| 1066 |
0, |
| 1067 |
0, |
| 1068 |
SelectionChanges::MsgSource::TreeView |
| 1069 |
); |
| 1070 |
if (select) { |
| 1071 |
Gui::Selection().selStackPush(); |
| 1072 |
Gui::Selection().clearSelection(); |
| 1073 |
Gui::Selection().addSelection( |
| 1074 |
obj->getDocument()->getName(), |
| 1075 |
obj->getNameInDocument(), |
| 1076 |
subname.c_str() |
| 1077 |
); |
| 1078 |
Gui::Selection().selStackPush(); |
| 1079 |
} |
| 1080 |
else { |
| 1081 |
searchObject = item->object()->getObject(); |
| 1082 |
item->setBackground(0, QColor(255, 255, 0, 100)); |
| 1083 |
} |
| 1084 |
FC_TRACE("found item " << txt); |
| 1085 |
} |
| 1086 |
catch (...) { |
| 1087 |
FC_TRACE("item " << txt << " search exception in " << doc->getName()); |
| 1088 |
} |
| 1089 |
} |
| 1090 |
|
| 1091 |
Gui::Document* TreeWidget::selectedDocument() |
| 1092 |
{ |
| 1093 |
for (auto tree : Instances) { |
| 1094 |
if (!tree->isVisible()) { |
| 1095 |
continue; |
| 1096 |
} |
| 1097 |
auto sels = tree->selectedItems(); |
| 1098 |
if (sels.size() == 1 && sels[0]->type() == DocumentType) { |
| 1099 |
return static_cast<DocumentItem*>(sels[0])->document(); |
| 1100 |
} |
| 1101 |
} |
| 1102 |
return nullptr; |
| 1103 |
} |
| 1104 |
|
| 1105 |
void TreeWidget::updateStatus(bool delay) |
| 1106 |
{ |
| 1107 |
for (auto tree : Instances) { |
| 1108 |
tree->_updateStatus(delay); |
| 1109 |
} |
| 1110 |
} |
| 1111 |
|
| 1112 |
void TreeWidget::_updateStatus(bool delay) |
| 1113 |
{ |
| 1114 |
// When running from a different thread Qt will raise a warning |
| 1115 |
// when trying to start the QTimer |
| 1116 |
if (Q_UNLIKELY(thread() != QThread::currentThread())) { |
| 1117 |
return; |
| 1118 |
} |
| 1119 |
|
| 1120 |
if (!delay) { |
| 1121 |
if (!ChangedObjects.empty() || !NewObjects.empty()) { |
| 1122 |
onUpdateStatus(); |
| 1123 |
} |
| 1124 |
return; |
| 1125 |
} |
| 1126 |
int timeout = TreeParams::getStatusTimeout(); |
| 1127 |
if (timeout < 0) { |
| 1128 |
timeout = 1; |
| 1129 |
} |
| 1130 |
statusTimer->start(timeout); |
| 1131 |
} |
| 1132 |
|
| 1133 |
void TreeWidget::contextMenuEvent(QContextMenuEvent* e) |
| 1134 |
{ |
| 1135 |
// ask workbenches and view provider, ... |
| 1136 |
MenuItem view; |
| 1137 |
Gui::Application::Instance->setupContextMenu("Tree", &view); |
| 1138 |
|
| 1139 |
view << "Std_Properties" << "Separator" << "Std_Expressions"; |
| 1140 |
Workbench::createLinkMenu(&view); |
| 1141 |
|
| 1142 |
QMenu contextMenu; |
| 1143 |
|
| 1144 |
QMenu subMenu; |
| 1145 |
QMenu editMenu; |
| 1146 |
QActionGroup subMenuGroup(&subMenu); |
| 1147 |
subMenuGroup.setExclusive(true); |
| 1148 |
connect(&subMenuGroup, &QActionGroup::triggered, this, &TreeWidget::onActivateDocument); |
| 1149 |
MenuManager::getInstance()->setupContextMenu(&view, contextMenu); |
| 1150 |
|
| 1151 |
// get the current item |
| 1152 |
this->contextItem = itemAt(e->pos()); |
| 1153 |
|
| 1154 |
if (this->contextItem && this->contextItem->type() == DocumentType) { |
| 1155 |
auto docitem = static_cast<DocumentItem*>(this->contextItem); |
| 1156 |
App::Document* doc = docitem->document()->getDocument(); |
| 1157 |
|
| 1158 |
// It's better to let user decide whether and how to activate |
| 1159 |
// the current document, such as by double-clicking. |
| 1160 |
// App::GetApplication().setActiveDocument(doc); |
| 1161 |
|
| 1162 |
showHiddenAction->setChecked(docitem->showHidden()); |
| 1163 |
contextMenu.addAction(this->showHiddenAction); |
| 1164 |
contextMenu.addAction(this->openFileLocationAction); |
| 1165 |
contextMenu.addAction(this->searchObjectsAction); |
| 1166 |
contextMenu.addAction(this->closeDocAction); |
| 1167 |
if (doc->testStatus(App::Document::PartialDoc)) { |
| 1168 |
contextMenu.addAction(this->reloadDocAction); |
| 1169 |
} |
| 1170 |
else { |
| 1171 |
for (auto d : doc->getDependentDocuments()) { |
| 1172 |
if (d->testStatus(App::Document::PartialDoc)) { |
| 1173 |
contextMenu.addAction(this->reloadDocAction); |
| 1174 |
break; |
| 1175 |
} |
| 1176 |
} |
| 1177 |
contextMenu.addAction(this->selectDependentsAction); |
| 1178 |
if (doc == App::GetApplication().getActiveDocument() |
| 1179 |
&& this->skipRecomputeCommand != nullptr) { |
| 1180 |
// if active document is selected, use Command |
| 1181 |
this->skipRecomputeCommand->addTo(&contextMenu); |
| 1182 |
} |
| 1183 |
else { |
| 1184 |
// if other document is selected or Command load fails, edit selected Document directly |
| 1185 |
this->skipRecomputeAction->setChecked(doc->testStatus(App::Document::SkipRecompute)); |
| 1186 |
contextMenu.addAction(this->skipRecomputeAction); |
| 1187 |
} |
| 1188 |
this->allowPartialRecomputeAction->setChecked( |
| 1189 |
doc->testStatus(App::Document::AllowPartialRecompute) |
| 1190 |
); |
| 1191 |
if (doc->testStatus(App::Document::SkipRecompute)) { |
| 1192 |
contextMenu.addAction(this->allowPartialRecomputeAction); |
| 1193 |
} |
| 1194 |
contextMenu.addAction(this->markRecomputeAction); |
| 1195 |
contextMenu.addAction(this->createGroupAction); |
| 1196 |
} |
| 1197 |
contextMenu.addSeparator(); |
| 1198 |
} |
| 1199 |
else if (this->contextItem && this->contextItem->type() == ObjectType) { |
| 1200 |
auto objitem = static_cast<DocumentObjectItem*>(this->contextItem); |
| 1201 |
|
| 1202 |
// check that the selection is not across several documents |
| 1203 |
bool acrossDocuments = false; |
| 1204 |
auto SelectedObjectsList = Selection().getCompleteSelection(); |
| 1205 |
// get the object's document as reference |
| 1206 |
App::Document* doc = objitem->object()->getObject()->getDocument(); |
| 1207 |
for (auto it = SelectedObjectsList.begin(); it != SelectedObjectsList.end(); ++it) { |
| 1208 |
if ((*it).pDoc != doc) { |
| 1209 |
acrossDocuments = true; |
| 1210 |
break; |
| 1211 |
} |
| 1212 |
} |
| 1213 |
|
| 1214 |
showHiddenAction->setChecked(doc->ShowHidden.getValue()); |
| 1215 |
contextMenu.addAction(this->showHiddenAction); |
| 1216 |
contextMenu.addAction(this->toggleVisibilityInTreeAction); |
| 1217 |
|
| 1218 |
if (!acrossDocuments) { // is only sensible for selections within one document |
| 1219 |
if (objitem->object()->getObject()->isDerivedFrom<App::DocumentObjectGroup>()) { |
| 1220 |
contextMenu.addAction(this->createGroupAction); |
| 1221 |
} |
| 1222 |
// if there are dependent objects in the selection, add context menu to add them to selection |
| 1223 |
if (CheckForDependents()) { |
| 1224 |
contextMenu.addAction(this->selectDependentsAction); |
| 1225 |
} |
| 1226 |
} |
| 1227 |
|
| 1228 |
contextMenu.addSeparator(); |
| 1229 |
contextMenu.addAction(this->markRecomputeAction); |
| 1230 |
contextMenu.addAction(this->recomputeObjectAction); |
| 1231 |
contextMenu.addSeparator(); |
| 1232 |
|
| 1233 |
// relabeling is only possible for a single selected document |
| 1234 |
if (SelectedObjectsList.size() == 1) { |
| 1235 |
contextMenu.addAction(this->relabelObjectAction); |
| 1236 |
} |
| 1237 |
|
| 1238 |
auto selItems = this->selectedItems(); |
| 1239 |
// if only one item is selected, setup the edit menu |
| 1240 |
if (selItems.size() == 1) { |
| 1241 |
objitem->object()->setupContextMenu(&editMenu, this, SLOT(onStartEditing())); |
| 1242 |
QList<QAction*> editAct = editMenu.actions(); |
| 1243 |
if (!editAct.isEmpty()) { |
| 1244 |
QAction* topact = contextMenu.actions().constFirst(); |
| 1245 |
for (QList<QAction*>::iterator it = editAct.begin(); it != editAct.end(); ++it) { |
| 1246 |
contextMenu.insertAction(topact, *it); |
| 1247 |
} |
| 1248 |
QAction* first = editAct.front(); |
| 1249 |
contextMenu.setDefaultAction(first); |
| 1250 |
if (objitem->object()->isEditing()) { |
| 1251 |
contextMenu.insertAction(topact, this->finishEditingAction); |
| 1252 |
} |
| 1253 |
contextMenu.insertSeparator(topact); |
| 1254 |
} |
| 1255 |
} |
| 1256 |
} |
| 1257 |
|
| 1258 |
|
| 1259 |
// add a submenu to active a document if two or more exist |
| 1260 |
std::vector<App::Document*> docs = App::GetApplication().getDocuments(); |
| 1261 |
if (docs.size() >= 2) { |
| 1262 |
contextMenu.addSeparator(); |
| 1263 |
App::Document* activeDoc = App::GetApplication().getActiveDocument(); |
| 1264 |
subMenu.setTitle(tr("Activate Document")); |
| 1265 |
contextMenu.addMenu(&subMenu); |
| 1266 |
QAction* active = nullptr; |
| 1267 |
for (auto it = docs.begin(); it != docs.end(); ++it) { |
| 1268 |
QString label = QString::fromUtf8((*it)->Label.getValue()); |
| 1269 |
QAction* action = subMenuGroup.addAction(label); |
| 1270 |
action->setCheckable(true); |
| 1271 |
action->setStatusTip(tr("Activates document %1").arg(label)); |
| 1272 |
action->setData(QByteArray((*it)->getName())); |
| 1273 |
if (*it == activeDoc) { |
| 1274 |
active = action; |
| 1275 |
} |
| 1276 |
} |
| 1277 |
|
| 1278 |
if (active) { |
| 1279 |
active->setChecked(true); |
| 1280 |
} |
| 1281 |
subMenu.addActions(subMenuGroup.actions()); |
| 1282 |
} |
| 1283 |
|
| 1284 |
// add a submenu to present the settings of the tree. |
| 1285 |
QMenu settingsMenu; |
| 1286 |
settingsMenu.setTitle(tr("Tree Settings")); |
| 1287 |
contextMenu.addSeparator(); |
| 1288 |
contextMenu.addMenu(&settingsMenu); |
| 1289 |
|
| 1290 |
QAction* action = new QAction(tr("Show Description"), this); |
| 1291 |
QAction* internalNameAction = new QAction(tr("Show Internal Name"), this); |
| 1292 |
action->setStatusTip( |
| 1293 |
tr("Shows a description column for items. An item's description can be set by editing the " |
| 1294 |
"'label2' property.") |
| 1295 |
); |
| 1296 |
action->setCheckable(true); |
| 1297 |
|
| 1298 |
ParameterGrp::handle hGrp = App::GetApplication().GetParameterGroupByPath( |
| 1299 |
"User parameter:BaseApp/Preferences/TreeView" |
| 1300 |
); |
| 1301 |
action->setChecked(!hGrp->GetBool("HideColumn", true)); |
| 1302 |
|
| 1303 |
settingsMenu.addAction(action); |
| 1304 |
QObject::connect(action, &QAction::triggered, this, [this, action, internalNameAction, hGrp]() { |
| 1305 |
bool show = action->isChecked(); |
| 1306 |
hGrp->SetBool("HideColumn", !show); |
| 1307 |
setColumnHidden(1, !show); |
| 1308 |
header()->setVisible(action->isChecked() || internalNameAction->isChecked()); |
| 1309 |
}); |
| 1310 |
|
| 1311 |
|
| 1312 |
internalNameAction->setStatusTip(tr("Shows an internal name column for items.")); |
| 1313 |
internalNameAction->setCheckable(true); |
| 1314 |
|
| 1315 |
internalNameAction->setChecked(!hGrp->GetBool("HideInternalNames", true)); |
| 1316 |
|
| 1317 |
settingsMenu.addAction(internalNameAction); |
| 1318 |
|
| 1319 |
QObject::connect( |
| 1320 |
internalNameAction, |
| 1321 |
&QAction::triggered, |
| 1322 |
this, |
| 1323 |
[this, action, internalNameAction, hGrp]() { |
| 1324 |
bool show = internalNameAction->isChecked(); |
| 1325 |
hGrp->SetBool("HideInternalNames", !show); |
| 1326 |
setColumnHidden(2, !show); |
| 1327 |
header()->setVisible(action->isChecked() || internalNameAction->isChecked()); |
| 1328 |
} |
| 1329 |
); |
| 1330 |
|
| 1331 |
if (!contextMenu.actions().isEmpty()) { |
| 1332 |
try { |
| 1333 |
contextMenu.exec(QCursor::pos()); |
| 1334 |
} |
| 1335 |
catch (Base::Exception& e) { |
| 1336 |
e.reportException(); |
| 1337 |
} |
| 1338 |
catch (std::exception& e) { |
| 1339 |
FC_ERR("C++ exception: " << e.what()); |
| 1340 |
} |
| 1341 |
catch (...) { |
| 1342 |
FC_ERR("Unknown exception"); |
| 1343 |
} |
| 1344 |
contextItem = nullptr; |
| 1345 |
} |
| 1346 |
} |
| 1347 |
|
| 1348 |
void TreeWidget::hideEvent(QHideEvent* ev) |
| 1349 |
{ |
| 1350 |
QTreeWidget::hideEvent(ev); |
| 1351 |
} |
| 1352 |
|
| 1353 |
void TreeWidget::showEvent(QShowEvent* ev) |
| 1354 |
{ |
| 1355 |
QTreeWidget::showEvent(ev); |
| 1356 |
} |
| 1357 |
|
| 1358 |
void TreeWidget::onCreateGroup() |
| 1359 |
{ |
| 1360 |
QString name = tr("Group"); |
| 1361 |
App::AutoTransaction trans("Create group"); |
| 1362 |
if (this->contextItem->type() == DocumentType) { |
| 1363 |
auto docitem = static_cast<DocumentItem*>(this->contextItem); |
| 1364 |
App::Document* doc = docitem->document()->getDocument(); |
| 1365 |
QString cmd = QStringLiteral( |
| 1366 |
"App.getDocument(\"%1\").addObject" |
| 1367 |
"(\"App::DocumentObjectGroup\",\"Group\").Label=\"%2\"" |
| 1368 |
) |
| 1369 |
.arg(QString::fromUtf8(doc->getName()), name); |
| 1370 |
Gui::Command::runCommand(Gui::Command::App, cmd.toUtf8()); |
| 1371 |
} |
| 1372 |
else if (this->contextItem->type() == ObjectType) { |
| 1373 |
auto objitem = static_cast<DocumentObjectItem*>(this->contextItem); |
| 1374 |
App::DocumentObject* obj = objitem->object()->getObject(); |
| 1375 |
App::Document* doc = obj->getDocument(); |
| 1376 |
QString cmd = QStringLiteral( |
| 1377 |
"App.getDocument(\"%1\").getObject(\"%2\")" |
| 1378 |
".newObject(\"App::DocumentObjectGroup\",\"Group\").Label=\"%3\"" |
| 1379 |
) |
| 1380 |
.arg( |
| 1381 |
QString::fromUtf8(doc->getName()), |
| 1382 |
QString::fromUtf8(obj->getNameInDocument()), |
| 1383 |
name |
| 1384 |
); |
| 1385 |
Gui::Command::runCommand(Gui::Command::App, cmd.toUtf8()); |
| 1386 |
} |
| 1387 |
} |
| 1388 |
|
| 1389 |
void TreeWidget::onRelabelObject() |
| 1390 |
{ |
| 1391 |
QTreeWidgetItem* item = currentItem(); |
| 1392 |
if (item) { |
| 1393 |
editItem(item); |
| 1394 |
} |
| 1395 |
} |
| 1396 |
|
| 1397 |
void TreeWidget::onStartEditing() |
| 1398 |
{ |
| 1399 |
auto action = qobject_cast<QAction*>(sender()); |
| 1400 |
if (action) { |
| 1401 |
if (this->contextItem && this->contextItem->type() == ObjectType) { |
| 1402 |
auto objitem = static_cast<DocumentObjectItem*>(this->contextItem); |
| 1403 |
int edit = action->data().toInt(); |
| 1404 |
|
| 1405 |
App::DocumentObject* obj = objitem->object()->getObject(); |
| 1406 |
if (!obj || !obj->isAttachedToDocument()) { |
| 1407 |
return; |
| 1408 |
} |
| 1409 |
auto doc = const_cast<Document*>(objitem->getOwnerDocument()->document()); |
| 1410 |
MDIView* view = doc->getActiveView(); |
| 1411 |
if (view) { |
| 1412 |
getMainWindow()->setActiveWindow(view); |
| 1413 |
} |
| 1414 |
|
| 1415 |
editingItem = objitem; |
| 1416 |
if (!doc->setEdit(objitem->object(), edit)) { |
| 1417 |
editingItem = nullptr; |
| 1418 |
} |
| 1419 |
} |
| 1420 |
} |
| 1421 |
} |
| 1422 |
|
| 1423 |
void TreeWidget::onFinishEditing() |
| 1424 |
{ |
| 1425 |
if (this->contextItem && this->contextItem->type() == ObjectType) { |
| 1426 |
auto objitem = static_cast<DocumentObjectItem*>(this->contextItem); |
| 1427 |
App::DocumentObject* obj = objitem->object()->getObject(); |
| 1428 |
if (!obj) { |
| 1429 |
return; |
| 1430 |
} |
| 1431 |
Gui::Document* doc = Gui::Application::Instance->getDocument(obj->getDocument()); |
| 1432 |
doc->commitCommand(); |
| 1433 |
doc->resetEdit(); |
| 1434 |
doc->getDocument()->recompute(); |
| 1435 |
} |
| 1436 |
} |
| 1437 |
|
| 1438 |
// check if selection has dependent objects |
| 1439 |
bool TreeWidget::CheckForDependents() |
| 1440 |
{ |
| 1441 |
// if the selected object is a document |
| 1442 |
if (this->contextItem && this->contextItem->type() == DocumentType) { |
| 1443 |
return true; |
| 1444 |
} |
| 1445 |
// it can be an object |
| 1446 |
else { |
| 1447 |
QList<QTreeWidgetItem*> items = this->selectedItems(); |
| 1448 |
for (QList<QTreeWidgetItem*>::iterator it = items.begin(); it != items.end(); ++it) { |
| 1449 |
if ((*it)->type() == ObjectType) { |
| 1450 |
auto objitem = static_cast<DocumentObjectItem*>(*it); |
| 1451 |
App::DocumentObject* obj = objitem->object()->getObject(); |
| 1452 |
// get dependents |
| 1453 |
auto subObjectList = obj->getOutList(); |
| 1454 |
if (!subObjectList.empty()) { |
| 1455 |
return true; |
| 1456 |
} |
| 1457 |
} |
| 1458 |
} |
| 1459 |
} |
| 1460 |
|
| 1461 |
return false; |
| 1462 |
} |
| 1463 |
|
| 1464 |
// adds an App::DocumentObject* and its dependent objects to the selection |
| 1465 |
void TreeWidget::addDependentToSelection(App::Document* doc, App::DocumentObject* docObject) |
| 1466 |
{ |
| 1467 |
// add the docObject to the selection |
| 1468 |
Selection().addSelection(doc->getName(), docObject->getNameInDocument()); |
| 1469 |
// get the dependent |
| 1470 |
auto subObjectList = docObject->getOutList(); |
| 1471 |
for (auto itDepend : subObjectList) { |
| 1472 |
if (!Selection().isSelected(itDepend)) { |
| 1473 |
addDependentToSelection(doc, itDepend); |
| 1474 |
} |
| 1475 |
} |
| 1476 |
} |
| 1477 |
|
| 1478 |
// add dependents of the selected tree object to selection |
| 1479 |
void TreeWidget::onSelectDependents() |
| 1480 |
{ |
| 1481 |
// We only have this context menu entry if the selection is within one document but it |
| 1482 |
// might be not the active document. Therefore get the document not here but later by casting. |
| 1483 |
App::Document* doc; |
| 1484 |
|
| 1485 |
// if the selected object is a document |
| 1486 |
if (this->contextItem && this->contextItem->type() == DocumentType) { |
| 1487 |
auto docitem = static_cast<DocumentItem*>(this->contextItem); |
| 1488 |
doc = docitem->document()->getDocument(); |
| 1489 |
std::vector<App::DocumentObject*> obj = doc->getObjects(); |
| 1490 |
for (auto it = obj.begin(); it != obj.end(); ++it) { |
| 1491 |
Selection().addSelection(doc->getName(), (*it)->getNameInDocument()); |
| 1492 |
} |
| 1493 |
} |
| 1494 |
// it can be an object |
| 1495 |
else { |
| 1496 |
QList<QTreeWidgetItem*> items = this->selectedItems(); |
| 1497 |
for (QList<QTreeWidgetItem*>::iterator it = items.begin(); it != items.end(); ++it) { |
| 1498 |
if ((*it)->type() == ObjectType) { |
| 1499 |
auto objitem = static_cast<DocumentObjectItem*>(*it); |
| 1500 |
doc = objitem->object()->getObject()->getDocument(); |
| 1501 |
App::DocumentObject* obj = objitem->object()->getObject(); |
| 1502 |
// the dependents can also have dependents, thus add them recursively via a separate void |
| 1503 |
addDependentToSelection(doc, obj); |
| 1504 |
} |
| 1505 |
} |
| 1506 |
} |
| 1507 |
} |
| 1508 |
|
| 1509 |
void TreeWidget::onSkipRecompute(bool on) |
| 1510 |
{ |
| 1511 |
// if a document item is selected then touch all objects |
| 1512 |
if (this->contextItem && this->contextItem->type() == DocumentType) { |
| 1513 |
auto docitem = static_cast<DocumentItem*>(this->contextItem); |
| 1514 |
App::Document* doc = docitem->document()->getDocument(); |
| 1515 |
doc->setStatus(App::Document::SkipRecompute, on); |
| 1516 |
} |
| 1517 |
} |
| 1518 |
|
| 1519 |
void TreeWidget::onAllowPartialRecompute(bool on) |
| 1520 |
{ |
| 1521 |
// if a document item is selected then touch all objects |
| 1522 |
if (this->contextItem && this->contextItem->type() == DocumentType) { |
| 1523 |
auto docitem = static_cast<DocumentItem*>(this->contextItem); |
| 1524 |
App::Document* doc = docitem->document()->getDocument(); |
| 1525 |
doc->setStatus(App::Document::AllowPartialRecompute, on); |
| 1526 |
} |
| 1527 |
} |
| 1528 |
|
| 1529 |
void TreeWidget::onMarkRecompute() |
| 1530 |
{ |
| 1531 |
// if a document item is selected then touch all objects |
| 1532 |
if (this->contextItem && this->contextItem->type() == DocumentType) { |
| 1533 |
auto docitem = static_cast<DocumentItem*>(this->contextItem); |
| 1534 |
App::Document* doc = docitem->document()->getDocument(); |
| 1535 |
std::vector<App::DocumentObject*> obj = doc->getObjects(); |
| 1536 |
for (auto it = obj.begin(); it != obj.end(); ++it) { |
| 1537 |
(*it)->enforceRecompute(); |
| 1538 |
} |
| 1539 |
} |
| 1540 |
// mark all selected objects |
| 1541 |
else { |
| 1542 |
QList<QTreeWidgetItem*> items = this->selectedItems(); |
| 1543 |
for (QList<QTreeWidgetItem*>::iterator it = items.begin(); it != items.end(); ++it) { |
| 1544 |
if ((*it)->type() == ObjectType) { |
| 1545 |
auto objitem = static_cast<DocumentObjectItem*>(*it); |
| 1546 |
App::DocumentObject* obj = objitem->object()->getObject(); |
| 1547 |
obj->enforceRecompute(); |
| 1548 |
} |
| 1549 |
} |
| 1550 |
} |
| 1551 |
} |
| 1552 |
|
| 1553 |
void TreeWidget::onRecomputeObject() |
| 1554 |
{ |
| 1555 |
std::vector<App::DocumentObject*> objs; |
| 1556 |
const auto items = selectedItems(); |
| 1557 |
for (auto ti : items) { |
| 1558 |
if (ti->type() == ObjectType) { |
| 1559 |
auto objitem = static_cast<DocumentObjectItem*>(ti); |
| 1560 |
objs.push_back(objitem->object()->getObject()); |
| 1561 |
objs.back()->enforceRecompute(); |
| 1562 |
} |
| 1563 |
} |
| 1564 |
if (objs.empty()) { |
| 1565 |
return; |
| 1566 |
} |
| 1567 |
App::AutoTransaction committer("Recompute object"); |
| 1568 |
objs.front()->getDocument()->recompute(objs, true); |
| 1569 |
} |
| 1570 |
|
| 1571 |
|
| 1572 |
DocumentItem* TreeWidget::getDocumentItem(const Gui::Document* doc) const |
| 1573 |
{ |
| 1574 |
auto it = DocumentMap.find(doc); |
| 1575 |
if (it != DocumentMap.end()) { |
| 1576 |
return it->second; |
| 1577 |
} |
| 1578 |
return nullptr; |
| 1579 |
} |
| 1580 |
|
| 1581 |
void TreeWidget::selectAllInstances(const ViewProviderDocumentObject& vpd) |
| 1582 |
{ |
| 1583 |
if (!isSelectionAttached()) { |
| 1584 |
return; |
| 1585 |
} |
| 1586 |
|
| 1587 |
if (selectTimer->isActive()) { |
| 1588 |
onSelectTimer(); |
| 1589 |
} |
| 1590 |
else { |
| 1591 |
_updateStatus(false); |
| 1592 |
} |
| 1593 |
|
| 1594 |
for (const auto& v : DocumentMap) { |
| 1595 |
v.second->selectAllInstances(vpd); |
| 1596 |
} |
| 1597 |
} |
| 1598 |
|
| 1599 |
static int& treeIconSize() |
| 1600 |
{ |
| 1601 |
static int _treeIconSize = -1; |
| 1602 |
|
| 1603 |
if (_treeIconSize < 0) { |
| 1604 |
_treeIconSize = TreeParams::getIconSize(); |
| 1605 |
} |
| 1606 |
return _treeIconSize; |
| 1607 |
} |
| 1608 |
|
| 1609 |
int TreeWidget::iconHeight() const |
| 1610 |
{ |
| 1611 |
return treeIconSize(); |
| 1612 |
} |
| 1613 |
|
| 1614 |
void TreeWidget::setIconHeight(int height) |
| 1615 |
{ |
| 1616 |
if (treeIconSize() == height) { |
| 1617 |
return; |
| 1618 |
} |
| 1619 |
|
| 1620 |
treeIconSize() = height; |
| 1621 |
if (treeIconSize() <= 0) { |
| 1622 |
treeIconSize() = std::max(10, iconSize()); |
| 1623 |
} |
| 1624 |
|
| 1625 |
for (auto tree : Instances) { |
| 1626 |
tree->setIconSize(QSize(treeIconSize(), treeIconSize())); |
| 1627 |
} |
| 1628 |
} |
| 1629 |
|
| 1630 |
int TreeWidget::iconSize() |
| 1631 |
{ |
| 1632 |
static int defaultSize; |
| 1633 |
if (defaultSize == 0) { |
| 1634 |
auto tree = instance(); |
| 1635 |
if (tree) { |
| 1636 |
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) |
| 1637 |
defaultSize = tree->viewOptions().decorationSize.width(); |
| 1638 |
#else |
| 1639 |
QStyleOptionViewItem opt; |
| 1640 |
tree->initViewItemOption(&opt); |
| 1641 |
defaultSize = opt.decorationSize.width(); |
| 1642 |
#endif |
| 1643 |
} |
| 1644 |
else { |
| 1645 |
defaultSize = QApplication::style()->pixelMetric(QStyle::PM_SmallIconSize); |
| 1646 |
} |
| 1647 |
} |
| 1648 |
if (treeIconSize() > 0) { |
| 1649 |
return std::max(10, treeIconSize()); |
| 1650 |
} |
| 1651 |
return defaultSize; |
| 1652 |
} |
| 1653 |
|
| 1654 |
TreeWidget* TreeWidget::instance() |
| 1655 |
{ |
| 1656 |
auto res = _LastSelectedTreeWidget; |
| 1657 |
if (res && res->isVisible()) { |
| 1658 |
return res; |
| 1659 |
} |
| 1660 |
for (auto inst : Instances) { |
| 1661 |
if (!res) { |
| 1662 |
res = inst; |
| 1663 |
} |
| 1664 |
if (inst->isVisible()) { |
| 1665 |
return inst; |
| 1666 |
} |
| 1667 |
} |
| 1668 |
return res; |
| 1669 |
} |
| 1670 |
|
| 1671 |
void TreeWidget::setupResizableColumn(TreeWidget* tree) |
| 1672 |
{ |
| 1673 |
auto mode = TreeParams::getResizableColumn() ? QHeaderView::Interactive |
| 1674 |
: QHeaderView::ResizeToContents; |
| 1675 |
for (auto inst : Instances) { |
| 1676 |
if (!tree || tree == inst) { |
| 1677 |
inst->header()->setSectionResizeMode(0, mode); |
| 1678 |
inst->header()->setSectionResizeMode(1, mode); |
| 1679 |
inst->header()->setSectionResizeMode(2, mode); |
| 1680 |
if (TreeParams::getResizableColumn()) { |
| 1681 |
QSignalBlocker blocker(inst); |
| 1682 |
if (TreeParams::getColumnSize1() > 0) { |
| 1683 |
inst->header()->resizeSection(0, TreeParams::getColumnSize1()); |
| 1684 |
} |
| 1685 |
if (TreeParams::getColumnSize2() > 0) { |
| 1686 |
inst->header()->resizeSection(1, TreeParams::getColumnSize2()); |
| 1687 |
} |
| 1688 |
if (TreeParams::getColumnSize3() > 0) { |
| 1689 |
inst->header()->resizeSection(2, TreeParams::getColumnSize3()); |
| 1690 |
} |
| 1691 |
} |
| 1692 |
} |
| 1693 |
} |
| 1694 |
} |
| 1695 |
|
| 1696 |
TreeWidget* TreeWidget::getTreeForSelection() |
| 1697 |
{ |
| 1698 |
TreeWidget* tree = instance(); |
| 1699 |
if (!tree || !tree->isSelectionAttached()) { |
| 1700 |
for (auto pTree : Instances) { |
| 1701 |
if (pTree->isSelectionAttached()) { |
| 1702 |
tree = pTree; |
| 1703 |
break; |
| 1704 |
} |
| 1705 |
} |
| 1706 |
} |
| 1707 |
if (!tree) { |
| 1708 |
return nullptr; |
| 1709 |
} |
| 1710 |
|
| 1711 |
if (tree->selectTimer->isActive()) { |
| 1712 |
tree->onSelectTimer(); |
| 1713 |
} |
| 1714 |
else { |
| 1715 |
tree->_updateStatus(false); |
| 1716 |
} |
| 1717 |
|
| 1718 |
return tree; |
| 1719 |
} |
| 1720 |
|
| 1721 |
std::vector<Document*> TreeWidget::getSelectedDocuments() |
| 1722 |
{ |
| 1723 |
std::vector<Document*> ret; |
| 1724 |
TreeWidget* tree = getTreeForSelection(); |
| 1725 |
|
| 1726 |
if (!tree) { |
| 1727 |
return ret; |
| 1728 |
} |
| 1729 |
|
| 1730 |
const auto items = tree->selectedItems(); |
| 1731 |
for (auto ti : items) { |
| 1732 |
if (ti->type() != DocumentType) { |
| 1733 |
continue; |
| 1734 |
} |
| 1735 |
auto item = static_cast<DocumentItem*>(ti); |
| 1736 |
auto doc = item->document(); |
| 1737 |
if (!doc || !doc->getDocument()) { |
| 1738 |
FC_WARN("skip invalid document"); |
| 1739 |
continue; |
| 1740 |
} |
| 1741 |
ret.push_back(doc); |
| 1742 |
} |
| 1743 |
return ret; |
| 1744 |
} |
| 1745 |
|
| 1746 |
std::vector<TreeWidget::SelInfo> TreeWidget::getSelection(App::Document* doc) |
| 1747 |
{ |
| 1748 |
std::vector<SelInfo> ret; |
| 1749 |
TreeWidget* tree = getTreeForSelection(); |
| 1750 |
|
| 1751 |
if (!tree) { |
| 1752 |
return ret; |
| 1753 |
} |
| 1754 |
|
| 1755 |
const auto items = tree->selectedItems(); |
| 1756 |
for (auto ti : items) { |
| 1757 |
if (ti->type() != ObjectType) { |
| 1758 |
continue; |
| 1759 |
} |
| 1760 |
auto item = static_cast<DocumentObjectItem*>(ti); |
| 1761 |
auto vp = item->object(); |
| 1762 |
auto obj = vp->getObject(); |
| 1763 |
if (!obj || !obj->isAttachedToDocument()) { |
| 1764 |
FC_WARN("skip invalid object"); |
| 1765 |
continue; |
| 1766 |
} |
| 1767 |
if (doc && obj->getDocument() != doc) { |
| 1768 |
FC_LOG("skip objects not from current document"); |
| 1769 |
continue; |
| 1770 |
} |
| 1771 |
ViewProviderDocumentObject* parentVp = nullptr; |
| 1772 |
auto parent = item->getParentItem(); |
| 1773 |
if (parent) { |
| 1774 |
parentVp = parent->object(); |
| 1775 |
if (!parentVp->getObject()->isAttachedToDocument()) { |
| 1776 |
FC_WARN("skip '" << obj->getFullName() << "' with invalid parent"); |
| 1777 |
continue; |
| 1778 |
} |
| 1779 |
} |
| 1780 |
ret.emplace_back(); |
| 1781 |
auto& sel = ret.back(); |
| 1782 |
sel.topParent = nullptr; |
| 1783 |
std::ostringstream ss; |
| 1784 |
item->getSubName(ss, sel.topParent); |
| 1785 |
if (!sel.topParent) { |
| 1786 |
sel.topParent = obj; |
| 1787 |
} |
| 1788 |
else { |
| 1789 |
ss << obj->getNameInDocument() << '.'; |
| 1790 |
} |
| 1791 |
sel.subname = ss.str(); |
| 1792 |
sel.parentVp = parentVp; |
| 1793 |
sel.vp = vp; |
| 1794 |
} |
| 1795 |
return ret; |
| 1796 |
} |
| 1797 |
|
| 1798 |
void TreeWidget::selectAllLinks(App::DocumentObject* obj) |
| 1799 |
{ |
| 1800 |
if (!isSelectionAttached()) { |
| 1801 |
return; |
| 1802 |
} |
| 1803 |
|
| 1804 |
if (!obj || !obj->isAttachedToDocument()) { |
| 1805 |
TREE_ERR("invalid object"); |
| 1806 |
return; |
| 1807 |
} |
| 1808 |
|
| 1809 |
if (selectTimer->isActive()) { |
| 1810 |
onSelectTimer(); |
| 1811 |
} |
| 1812 |
else { |
| 1813 |
_updateStatus(false); |
| 1814 |
} |
| 1815 |
|
| 1816 |
for (auto link : App::GetApplication().getLinksTo(obj, App::GetLinkRecursive)) { |
| 1817 |
if (!link || !link->isAttachedToDocument()) { |
| 1818 |
TREE_ERR("invalid linked object"); |
| 1819 |
continue; |
| 1820 |
} |
| 1821 |
auto vp = freecad_cast<ViewProviderDocumentObject*>( |
| 1822 |
Application::Instance->getViewProvider(link) |
| 1823 |
); |
| 1824 |
if (!vp) { |
| 1825 |
TREE_ERR("invalid view provider of the linked object"); |
| 1826 |
continue; |
| 1827 |
} |
| 1828 |
for (auto& v : DocumentMap) { |
| 1829 |
v.second->selectAllInstances(*vp); |
| 1830 |
} |
| 1831 |
} |
| 1832 |
} |
| 1833 |
|
| 1834 |
void TreeWidget::onSearchObjects() |
| 1835 |
{ |
| 1836 |
Q_EMIT emitSearchObjects(); |
| 1837 |
} |
| 1838 |
|
| 1839 |
void TreeWidget::onActivateDocument(QAction* active) |
| 1840 |
{ |
| 1841 |
// activate the specified document |
| 1842 |
QByteArray docname = active->data().toByteArray(); |
| 1843 |
Gui::Document* doc = Application::Instance->getDocument((const char*)docname); |
| 1844 |
if (doc && !doc->setActiveView()) { |
| 1845 |
doc->setActiveView(nullptr, View3DInventor::getClassTypeId()); |
| 1846 |
} |
| 1847 |
} |
| 1848 |
|
| 1849 |
Qt::DropActions TreeWidget::supportedDropActions() const |
| 1850 |
{ |
| 1851 |
return Qt::LinkAction | Qt::CopyAction | Qt::MoveAction; |
| 1852 |
} |
| 1853 |
|
| 1854 |
bool TreeWidget::event(QEvent* e) |
| 1855 |
{ |
| 1856 |
return QTreeWidget::event(e); |
| 1857 |
} |
| 1858 |
|
| 1859 |
bool TreeWidget::eventFilter(QObject*, QEvent* ev) |
| 1860 |
{ |
| 1861 |
switch (ev->type()) { |
| 1862 |
case QEvent::KeyPress: |
| 1863 |
case QEvent::KeyRelease: { |
| 1864 |
auto ke = static_cast<QKeyEvent*>(ev); |
| 1865 |
if (ke->key() != Qt::Key_Escape) { |
| 1866 |
// Qt 5 only recheck key modifier on mouse move, so generate a fake |
| 1867 |
// event to trigger drag cursor change |
| 1868 |
auto mouseEvent = new QMouseEvent( |
| 1869 |
QEvent::MouseMove, |
| 1870 |
mapFromGlobal(QCursor::pos()), |
| 1871 |
QCursor::pos(), |
| 1872 |
Qt::NoButton, |
| 1873 |
QApplication::mouseButtons(), |
| 1874 |
QApplication::queryKeyboardModifiers() |
| 1875 |
); |
| 1876 |
QApplication::postEvent(this, mouseEvent); |
| 1877 |
} |
| 1878 |
break; |
| 1879 |
} |
| 1880 |
default: |
| 1881 |
break; |
| 1882 |
} |
| 1883 |
return false; |
| 1884 |
} |
| 1885 |
|
| 1886 |
namespace Gui |
| 1887 |
{ |
| 1888 |
|
| 1889 |
bool isTreeViewDragging() |
| 1890 |
{ |
| 1891 |
return _DraggingActive; |
| 1892 |
} |
| 1893 |
|
| 1894 |
} // namespace Gui |
| 1895 |
|
| 1896 |
void TreeWidget::keyPressEvent(QKeyEvent* event) |
| 1897 |
{ |
| 1898 |
if (event->matches(QKeySequence::Find)) { |
| 1899 |
event->accept(); |
| 1900 |
onSearchObjects(); |
| 1901 |
return; |
| 1902 |
} |
| 1903 |
else if (event->modifiers() == Qt::AltModifier) { |
| 1904 |
if (event->key() == Qt::Key_Left) { |
| 1905 |
for (auto& item : selectedItems()) { |
| 1906 |
item->setExpanded(false); |
| 1907 |
} |
| 1908 |
event->accept(); |
| 1909 |
return; |
| 1910 |
} |
| 1911 |
else if (event->key() == Qt::Key_Right) { |
| 1912 |
for (auto& item : selectedItems()) { |
| 1913 |
item->setExpanded(true); |
| 1914 |
} |
| 1915 |
event->accept(); |
| 1916 |
return; |
| 1917 |
} |
| 1918 |
else if (event->key() == Qt::Key_Up) { |
| 1919 |
for (auto& item : selectedItems()) { |
| 1920 |
item->setExpanded(true); |
| 1921 |
for (auto& child : childrenOfItem(*item)) { |
| 1922 |
child->setExpanded(false); |
| 1923 |
} |
| 1924 |
} |
| 1925 |
event->accept(); |
| 1926 |
return; |
| 1927 |
} |
| 1928 |
else if (event->key() == Qt::Key_Down) { |
| 1929 |
for (auto& item : selectedItems()) { |
| 1930 |
item->setExpanded(true); |
| 1931 |
for (auto& child : childrenOfItem(*item)) { |
| 1932 |
child->setExpanded(true); |
| 1933 |
} |
| 1934 |
} |
| 1935 |
event->accept(); |
| 1936 |
return; |
| 1937 |
} |
| 1938 |
} |
| 1939 |
|
| 1940 |
QTreeWidget::keyPressEvent(event); |
| 1941 |
} |
| 1942 |
|
| 1943 |
void TreeWidget::mousePressEvent(QMouseEvent* event) |
| 1944 |
{ |
| 1945 |
if (isVisibilityIconEnabled()) { |
| 1946 |
QTreeWidgetItem* item = itemAt(event->pos()); |
| 1947 |
if (item && item->type() == TreeWidget::ObjectType && event->button() == Qt::LeftButton) { |
| 1948 |
auto objitem = static_cast<DocumentObjectItem*>(item); |
| 1949 |
|
| 1950 |
// Mouse position relative to viewport |
| 1951 |
auto mousePos = event->pos(); |
| 1952 |
|
| 1953 |
// Rect occupied by the item relative to viewport |
| 1954 |
auto iconRect = visualItemRect(objitem); |
| 1955 |
|
| 1956 |
auto style = this->style(); |
| 1957 |
|
| 1958 |
// If the checkboxes are visible, these are displayed before the icon |
| 1959 |
// and we have to compensate for its width. |
| 1960 |
if (isSelectionCheckBoxesEnabled()) { |
| 1961 |
int checkboxWidth = style->pixelMetric(QStyle::PM_IndicatorWidth) |
| 1962 |
+ style->pixelMetric(QStyle::PM_LayoutHorizontalSpacing); |
| 1963 |
iconRect.adjust(checkboxWidth, 0, 0, 0); |
| 1964 |
} |
| 1965 |
|
| 1966 |
int const margin = style->pixelMetric(QStyle::PM_FocusFrameHMargin) + 1; |
| 1967 |
iconRect.adjust(margin, 0, 0, 0); |
| 1968 |
|
| 1969 |
// We are interested in the first icon (visibility icon) |
| 1970 |
iconRect.setWidth(iconSize()); |
| 1971 |
|
| 1972 |
// If the visibility icon was clicked, toggle the DocumentObject visibility |
| 1973 |
if (iconRect.contains(mousePos)) { |
| 1974 |
auto obj = objitem->object()->getObject(); |
| 1975 |
char const* objname = obj->getNameInDocument(); |
| 1976 |
|
| 1977 |
App::DocumentObject* parent = nullptr; |
| 1978 |
std::ostringstream subName; |
| 1979 |
objitem->getSubName(subName, parent); |
| 1980 |
|
| 1981 |
// Try the ElementVisible API, if that is not supported toggle the Visibility property |
| 1982 |
int visible = -1; |
| 1983 |
if (parent) { |
| 1984 |
visible = parent->isElementVisible(objname); |
| 1985 |
} |
| 1986 |
if (parent && visible >= 0) { |
| 1987 |
parent->setElementVisible(objname, !visible); |
| 1988 |
} |
| 1989 |
else { |
| 1990 |
visible = obj->Visibility.getValue(); |
| 1991 |
obj->Visibility.setValue(!visible); |
| 1992 |
} |
| 1993 |
|
| 1994 |
// to prevent selection of the item via QTreeWidget::mousePressEvent |
| 1995 |
event->accept(); |
| 1996 |
return; |
| 1997 |
} |
| 1998 |
} |
| 1999 |
} |
| 2000 |
|
| 2001 |
QTreeWidget::mousePressEvent(event); |
| 2002 |
} |
| 2003 |
|
| 2004 |
void TreeWidget::mouseDoubleClickEvent(QMouseEvent* event) |
| 2005 |
{ |
| 2006 |
QTreeWidgetItem* item = itemAt(event->pos()); |
| 2007 |
if (!item) { |
| 2008 |
return; |
| 2009 |
} |
| 2010 |
|
| 2011 |
try { |
| 2012 |
if (item->type() == TreeWidget::DocumentType) { |
| 2013 |
Gui::Document* doc = static_cast<DocumentItem*>(item)->document(); |
| 2014 |
if (!doc) { |
| 2015 |
return; |
| 2016 |
} |
| 2017 |
if (doc->getDocument()->testStatus(App::Document::PartialDoc)) { |
| 2018 |
contextItem = item; |
| 2019 |
onReloadDoc(); |
| 2020 |
return; |
| 2021 |
} |
| 2022 |
if (!doc->setActiveView()) { |
| 2023 |
doc->setActiveView(nullptr, View3DInventor::getClassTypeId()); |
| 2024 |
} |
| 2025 |
} |
| 2026 |
else if (item->type() == TreeWidget::ObjectType) { |
| 2027 |
auto objitem = static_cast<DocumentObjectItem*>(item); |
| 2028 |
ViewProviderDocumentObject* vp = objitem->object(); |
| 2029 |
|
| 2030 |
objitem->getOwnerDocument()->document()->setActiveView(vp); |
| 2031 |
auto manager = Application::Instance->macroManager(); |
| 2032 |
auto lines = manager->getLines(); |
| 2033 |
|
| 2034 |
std::ostringstream ss; |
| 2035 |
ss << Command::getObjectCmd(vp->getObject()) << ".ViewObject.doubleClicked()"; |
| 2036 |
|
| 2037 |
const char* commandText = vp->getTransactionText(); |
| 2038 |
if (commandText) { |
| 2039 |
auto editDoc = Application::Instance->editDocument(); |
| 2040 |
App::AutoTransaction committer(commandText, true); |
| 2041 |
|
| 2042 |
if (!vp->doubleClicked()) { |
| 2043 |
QTreeWidget::mouseDoubleClickEvent(event); |
| 2044 |
} |
| 2045 |
else if (lines == manager->getLines()) { |
| 2046 |
manager->addLine(MacroManager::Gui, ss.str().c_str()); |
| 2047 |
} |
| 2048 |
|
| 2049 |
// If the double click starts an editing, let the transaction persist |
| 2050 |
if (!editDoc && Application::Instance->editDocument()) { |
| 2051 |
committer.setEnable(false); |
| 2052 |
} |
| 2053 |
} |
| 2054 |
else { |
| 2055 |
if (!vp->doubleClicked()) { |
| 2056 |
QTreeWidget::mouseDoubleClickEvent(event); |
| 2057 |
} |
| 2058 |
else if (lines == manager->getLines()) { |
| 2059 |
manager->addLine(MacroManager::Gui, ss.str().c_str()); |
| 2060 |
} |
| 2061 |
} |
| 2062 |
} |
| 2063 |
} |
| 2064 |
catch (Base::Exception& e) { |
| 2065 |
e.reportException(); |
| 2066 |
} |
| 2067 |
catch (std::exception& e) { |
| 2068 |
FC_ERR("C++ exception: " << e.what()); |
| 2069 |
} |
| 2070 |
catch (...) { |
| 2071 |
FC_ERR("Unknown exception"); |
| 2072 |
} |
| 2073 |
} |
| 2074 |
|
| 2075 |
void TreeWidget::startDragging() |
| 2076 |
{ |
| 2077 |
if (state() != NoState) { |
| 2078 |
return; |
| 2079 |
} |
| 2080 |
if (selectedItems().empty()) { |
| 2081 |
return; |
| 2082 |
} |
| 2083 |
|
| 2084 |
setState(DraggingState); |
| 2085 |
startDrag(model()->supportedDragActions()); |
| 2086 |
setState(NoState); |
| 2087 |
stopAutoScroll(); |
| 2088 |
} |
| 2089 |
|
| 2090 |
void TreeWidget::startDrag(Qt::DropActions supportedActions) |
| 2091 |
{ |
| 2092 |
Base::StateLocker guard(_DraggingActive); |
| 2093 |
QTreeWidget::startDrag(supportedActions); |
| 2094 |
if (_DragEventFilter) { |
| 2095 |
_DragEventFilter = false; |
| 2096 |
qApp->removeEventFilter(this); |
| 2097 |
} |
| 2098 |
} |
| 2099 |
|
| 2100 |
bool TreeWidget::dropMimeData(QTreeWidgetItem* parent, int index, const QMimeData* data, Qt::DropAction action) |
| 2101 |
{ |
| 2102 |
return QTreeWidget::dropMimeData(parent, index, data, action); |
| 2103 |
} |
| 2104 |
|
| 2105 |
void TreeWidget::dragEnterEvent(QDragEnterEvent* event) |
| 2106 |
{ |
| 2107 |
QTreeWidget::dragEnterEvent(event); |
| 2108 |
} |
| 2109 |
|
| 2110 |
void TreeWidget::dragLeaveEvent(QDragLeaveEvent* event) |
| 2111 |
{ |
| 2112 |
QTreeWidget::dragLeaveEvent(event); |
| 2113 |
} |
| 2114 |
|
| 2115 |
|
| 2116 |
struct ItemInfo |
| 2117 |
{ |
| 2118 |
std::string doc; |
| 2119 |
std::string obj; |
| 2120 |
std::string parentDoc; |
| 2121 |
std::string parent; |
| 2122 |
std::string ownerDoc; |
| 2123 |
std::string owner; |
| 2124 |
std::string subname; |
| 2125 |
std::string topDoc; |
| 2126 |
std::string topObj; |
| 2127 |
std::string topSubname; |
| 2128 |
std::vector<std::string> subs; |
| 2129 |
bool dragging = false; |
| 2130 |
}; |
| 2131 |
|
| 2132 |
struct ItemInfo2 |
| 2133 |
{ |
| 2134 |
std::string doc; |
| 2135 |
std::string obj; |
| 2136 |
std::string parentDoc; |
| 2137 |
std::string parent; |
| 2138 |
std::string topDoc; |
| 2139 |
std::string topObj; |
| 2140 |
std::string topSubname; |
| 2141 |
}; |
| 2142 |
|
| 2143 |
namespace |
| 2144 |
{ |
| 2145 |
class DropHandler |
| 2146 |
{ |
| 2147 |
public: |
| 2148 |
static std::vector<std::pair<DocumentObjectItem*, std::vector<std::string>>> filterItems( |
| 2149 |
const QList<QTreeWidgetItem*>& sels, |
| 2150 |
QTreeWidgetItem* targetItem |
| 2151 |
) |
| 2152 |
{ |
| 2153 |
std::vector<std::pair<DocumentObjectItem*, std::vector<std::string>>> items; |
| 2154 |
items.reserve(sels.size()); |
| 2155 |
for (auto ti : sels) { |
| 2156 |
if (ti->type() != TreeWidget::ObjectType) { |
| 2157 |
continue; |
| 2158 |
} |
| 2159 |
// ignore child elements if the parent is selected |
| 2160 |
if (sels.contains(ti->parent())) { |
| 2161 |
continue; |
| 2162 |
} |
| 2163 |
if (ti == targetItem) { |
| 2164 |
continue; |
| 2165 |
} |
| 2166 |
auto item = static_cast<DocumentObjectItem*>(ti); |
| 2167 |
items.emplace_back(); |
| 2168 |
auto& info = items.back(); |
| 2169 |
info.first = item; |
| 2170 |
const auto& subnames = item->getSubNames(); |
| 2171 |
info.second.insert(info.second.end(), subnames.begin(), subnames.end()); |
| 2172 |
} |
| 2173 |
|
| 2174 |
return items; |
| 2175 |
} |
| 2176 |
static App::PropertyPlacement* getPlacement( |
| 2177 |
const ItemInfo& info, |
| 2178 |
const App::DocumentObject* obj, |
| 2179 |
Base::Matrix4D& mat |
| 2180 |
) |
| 2181 |
{ |
| 2182 |
App::PropertyPlacement* propPlacement = nullptr; |
| 2183 |
if (!info.topObj.empty()) { |
| 2184 |
auto doc = App::GetApplication().getDocument(info.topDoc.c_str()); |
| 2185 |
if (doc) { |
| 2186 |
auto topObj = doc->getObject(info.topObj.c_str()); |
| 2187 |
if (topObj) { |
| 2188 |
auto sobj = topObj->getSubObject(info.topSubname.c_str(), nullptr, &mat); |
| 2189 |
if (sobj == obj) { |
| 2190 |
propPlacement = freecad_cast<App::PropertyPlacement*>( |
| 2191 |
obj->getPropertyByName("Placement") |
| 2192 |
); |
| 2193 |
} |
| 2194 |
} |
| 2195 |
} |
| 2196 |
} |
| 2197 |
else { |
| 2198 |
propPlacement = freecad_cast<App::PropertyPlacement*>(obj->getPropertyByName("Placement")); |
| 2199 |
if (propPlacement) { |
| 2200 |
mat = propPlacement->getValue().toMatrix(); |
| 2201 |
} |
| 2202 |
} |
| 2203 |
|
| 2204 |
return propPlacement; |
| 2205 |
} |
| 2206 |
}; |
| 2207 |
|
| 2208 |
QPoint getPos(QEvent* event) |
| 2209 |
{ |
| 2210 |
if (auto* dragMoveEvent = dynamic_cast<QDragMoveEvent*>(event)) { |
| 2211 |
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) |
| 2212 |
return dragMoveEvent->pos(); |
| 2213 |
#else |
| 2214 |
return dragMoveEvent->position().toPoint(); |
| 2215 |
#endif |
| 2216 |
} |
| 2217 |
|
| 2218 |
else if (auto* dropEvent = dynamic_cast<QDropEvent*>(event)) { |
| 2219 |
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) |
| 2220 |
return dropEvent->pos(); |
| 2221 |
#else |
| 2222 |
return dropEvent->position().toPoint(); |
| 2223 |
#endif |
| 2224 |
} |
| 2225 |
|
| 2226 |
// For unsupported event types or if casting fails |
| 2227 |
return QPoint(-1, -1); |
| 2228 |
} |
| 2229 |
|
| 2230 |
Qt::DropAction getDropAction(int size, const int type) |
| 2231 |
{ |
| 2232 |
if (QApplication::keyboardModifiers() == Qt::ControlModifier) { |
| 2233 |
return Qt::CopyAction; |
| 2234 |
} |
| 2235 |
else if (QApplication::keyboardModifiers() == Qt::AltModifier |
| 2236 |
&& (size == 1 || type == TreeWidget::DocumentType)) { |
| 2237 |
return Qt::LinkAction; |
| 2238 |
} |
| 2239 |
else { |
| 2240 |
return Qt::MoveAction; |
| 2241 |
} |
| 2242 |
} |
| 2243 |
} |
| 2244 |
|
| 2245 |
void TreeWidget::dragMoveEvent(QDragMoveEvent* event) |
| 2246 |
{ |
| 2247 |
// Qt5 does not change drag cursor in response to modifier key press, |
| 2248 |
// because QDrag installs a event filter that eats up key event. We install |
| 2249 |
// a filter after Qt and generate fake mouse move event in response to key |
| 2250 |
// press event, which triggers QDrag to update its cursor |
| 2251 |
if (!_DragEventFilter) { |
| 2252 |
_DragEventFilter = true; |
| 2253 |
qApp->installEventFilter(this); |
| 2254 |
} |
| 2255 |
|
| 2256 |
QTreeWidget::dragMoveEvent(event); |
| 2257 |
if (!event->isAccepted()) { |
| 2258 |
// return; |
| 2259 |
// QTreeWidget::dragMoveEvent is rejecting the event when in between items |
| 2260 |
// at DocumentItem root level. Which is preventing reordering. To work around |
| 2261 |
// we accept for now, then reject below if targetItem not found. |
| 2262 |
event->accept(); |
| 2263 |
} |
| 2264 |
|
| 2265 |
|
| 2266 |
TargetItemInfo targetInfo = getTargetInfo(event); |
| 2267 |
QTreeWidgetItem* targetItem = targetInfo.targetItem; |
| 2268 |
if (!targetItem) { |
| 2269 |
event->ignore(); |
| 2270 |
return; |
| 2271 |
} |
| 2272 |
|
| 2273 |
auto items = selectedItems(); |
| 2274 |
|
| 2275 |
auto da = getDropAction(items.size(), targetItem->type()); |
| 2276 |
event->setDropAction(da); |
| 2277 |
|
| 2278 |
if (targetItem->type() == TreeWidget::DocumentType) { |
| 2279 |
leaveEvent(nullptr); |
| 2280 |
} |
| 2281 |
else if (targetItem->type() == TreeWidget::ObjectType) { |
| 2282 |
onItemEntered(targetItem); |
| 2283 |
|
| 2284 |
auto targetItemObj = static_cast<DocumentObjectItem*>(targetItem); |
| 2285 |
Gui::ViewProviderDocumentObject* vp = targetItemObj->object(); |
| 2286 |
|
| 2287 |
// if we are in between or if target doesn't accept drops then the target is the parent |
| 2288 |
if (da == Qt::MoveAction && (targetInfo.inThresholdZone || !vp->canDropObjects())) { |
| 2289 |
targetInfo.targetItem = targetInfo.targetItem->parent(); |
| 2290 |
if (targetInfo.targetItem->type() == TreeWidget::DocumentType) { |
| 2291 |
leaveEvent(nullptr); |
| 2292 |
return; |
| 2293 |
} |
| 2294 |
|
| 2295 |
targetItemObj = static_cast<DocumentObjectItem*>(targetInfo.targetItem); |
| 2296 |
vp = targetItemObj->object(); |
| 2297 |
|
| 2298 |
if (!vp) { |
| 2299 |
TREE_TRACE("cannot drop"); |
| 2300 |
return; |
| 2301 |
} |
| 2302 |
} |
| 2303 |
try { |
| 2304 |
if (da != Qt::LinkAction && !vp->canDropObjects()) { |
| 2305 |
if (!(event->possibleActions() & Qt::LinkAction) || items.size() != 1) { |
| 2306 |
TREE_TRACE("Cannot drop here"); |
| 2307 |
event->ignore(); |
| 2308 |
return; |
| 2309 |
} |
| 2310 |
} |
| 2311 |
for (auto ti : items) { |
| 2312 |
if (ti->type() != TreeWidget::ObjectType) { |
| 2313 |
TREE_TRACE("cannot drop"); |
| 2314 |
event->ignore(); |
| 2315 |
return; |
| 2316 |
} |
| 2317 |
auto item = static_cast<DocumentObjectItem*>(ti); |
| 2318 |
|
| 2319 |
auto obj = item->object()->getObject(); |
| 2320 |
|
| 2321 |
if (da == Qt::MoveAction) { |
| 2322 |
// Check if item can be dragged from his parent |
| 2323 |
auto parentItem = item->getParentItem(); |
| 2324 |
if (parentItem |
| 2325 |
&& !( |
| 2326 |
parentItem->object()->canDragObjects() |
| 2327 |
&& parentItem->object()->canDragObject(obj) |
| 2328 |
)) { |
| 2329 |
TREE_TRACE("Cannot drag object"); |
| 2330 |
event->ignore(); |
| 2331 |
return; |
| 2332 |
} |
| 2333 |
} |
| 2334 |
|
| 2335 |
std::ostringstream str; |
| 2336 |
auto owner = item->getRelativeParent(str, targetItemObj); |
| 2337 |
auto subname = str.str(); |
| 2338 |
|
| 2339 |
// let the view provider decide to accept the object or ignore it |
| 2340 |
if (da != Qt::LinkAction |
| 2341 |
&& !vp->canDropObjectEx(obj, owner, subname.c_str(), item->mySubs)) { |
| 2342 |
TREE_TRACE( |
| 2343 |
"cannot drop " << obj->getFullName() << ' ' |
| 2344 |
<< (owner ? owner->getFullName() : "<No Owner>") << '.' |
| 2345 |
<< subname |
| 2346 |
); |
| 2347 |
event->ignore(); |
| 2348 |
return; |
| 2349 |
} |
| 2350 |
} |
| 2351 |
} |
| 2352 |
catch (Base::Exception& e) { |
| 2353 |
e.reportException(); |
| 2354 |
event->ignore(); |
| 2355 |
} |
| 2356 |
catch (std::exception& e) { |
| 2357 |
FC_ERR("C++ exception: " << e.what()); |
| 2358 |
event->ignore(); |
| 2359 |
} |
| 2360 |
catch (...) { |
| 2361 |
FC_ERR("Unknown exception"); |
| 2362 |
event->ignore(); |
| 2363 |
} |
| 2364 |
} |
| 2365 |
else { |
| 2366 |
leaveEvent(nullptr); |
| 2367 |
event->ignore(); |
| 2368 |
} |
| 2369 |
} |
| 2370 |
|
| 2371 |
TreeWidget::TargetItemInfo TreeWidget::getTargetInfo(QEvent* ev) |
| 2372 |
{ |
| 2373 |
TargetItemInfo targetInfo; |
| 2374 |
|
| 2375 |
QPoint pos = getPos(ev); |
| 2376 |
if (pos == QPoint(-1, -1)) { |
| 2377 |
return {}; // Return an empty struct |
| 2378 |
} |
| 2379 |
|
| 2380 |
targetInfo.targetItem = itemAt(pos); |
| 2381 |
// not dropped onto an item or one of the source items is also the destination item |
| 2382 |
if (!targetInfo.targetItem || targetInfo.targetItem->isSelected()) { |
| 2383 |
return {}; |
| 2384 |
} |
| 2385 |
targetInfo.underMouseItem = targetInfo.targetItem; |
| 2386 |
|
| 2387 |
if (targetInfo.targetItem->type() == TreeWidget::ObjectType) { |
| 2388 |
auto targetItemObj = static_cast<DocumentObjectItem*>(targetInfo.targetItem); |
| 2389 |
targetInfo.targetDoc = targetItemObj->getOwnerDocument()->document()->getDocument(); |
| 2390 |
} |
| 2391 |
else if (targetInfo.targetItem->type() == TreeWidget::DocumentType) { |
| 2392 |
auto targetDocItem = static_cast<DocumentItem*>(targetInfo.targetItem); |
| 2393 |
targetInfo.targetDoc = targetDocItem->document()->getDocument(); |
| 2394 |
} |
| 2395 |
else { |
| 2396 |
return {}; |
| 2397 |
} |
| 2398 |
|
| 2399 |
// Calculate the position of the mouse relative to the item's rectangle |
| 2400 |
QRect itemRect = visualItemRect(targetInfo.targetItem); |
| 2401 |
int mouseY = pos.y(); |
| 2402 |
int itemMidPoint = itemRect.top() + itemRect.height() / 2; |
| 2403 |
int threshold = itemRect.height() * 0.20; // 20% of the item's height as threshold |
| 2404 |
|
| 2405 |
targetInfo.inBottomHalf = mouseY > itemMidPoint; |
| 2406 |
targetInfo.inThresholdZone = ((mouseY < itemRect.top() + threshold) |
| 2407 |
&& !targetInfo.targetItem->isExpanded()) |
| 2408 |
|| (mouseY > itemRect.top() + itemRect.height() - threshold); |
| 2409 |
return targetInfo; |
| 2410 |
} |
| 2411 |
|
| 2412 |
bool TreeWidget::dropInDocument( |
| 2413 |
QDropEvent* event, |
| 2414 |
TargetItemInfo& targetInfo, |
| 2415 |
std::vector<TreeWidget::ObjectItemSubname> items |
| 2416 |
) |
| 2417 |
{ |
| 2418 |
std::string errMsg; |
| 2419 |
auto da = event->dropAction(); |
| 2420 |
bool touched = false; |
| 2421 |
|
| 2422 |
std::vector<ItemInfo2> infos; |
| 2423 |
infos.reserve(items.size()); |
| 2424 |
bool syncPlacement = TreeParams::getSyncPlacement(); |
| 2425 |
|
| 2426 |
App::AutoTransaction committer( |
| 2427 |
da == Qt::LinkAction ? "Link object" |
| 2428 |
: da == Qt::CopyAction ? "Copy object" |
| 2429 |
: "Move object" |
| 2430 |
); |
| 2431 |
|
| 2432 |
// check if items can be dragged |
| 2433 |
for (auto& v : items) { |
| 2434 |
auto item = v.first; |
| 2435 |
auto obj = item->object()->getObject(); |
| 2436 |
auto parentItem = item->getParentItem(); |
| 2437 |
if (parentItem) { |
| 2438 |
bool allParentsOK = canDragFromParents(parentItem, obj, nullptr); |
| 2439 |
|
| 2440 |
if (!allParentsOK || !parentItem->object()->canDragObjects() |
| 2441 |
|| !parentItem->object()->canDragObject(obj)) { |
| 2442 |
committer.close(true); |
| 2443 |
TREE_ERR( |
| 2444 |
"'" << obj->getFullName() << "' cannot be dragged out of '" |
| 2445 |
<< parentItem->object()->getObject()->getFullName() << "'" |
| 2446 |
); |
| 2447 |
return false; |
| 2448 |
} |
| 2449 |
} |
| 2450 |
else if (da != Qt::MoveAction || item->myOwner != targetInfo.targetItem) { |
| 2451 |
// We will not drag item out of parent if either, 1) modifier |
| 2452 |
// key is held, or 2) the dragging item is not inside the |
| 2453 |
// dropping document tree. |
| 2454 |
parentItem = nullptr; |
| 2455 |
} |
| 2456 |
infos.emplace_back(); |
| 2457 |
auto& info = infos.back(); |
| 2458 |
info.doc = obj->getDocument()->getName(); |
| 2459 |
info.obj = obj->getNameInDocument(); |
| 2460 |
if (parentItem) { |
| 2461 |
auto parent = parentItem->object()->getObject(); |
| 2462 |
info.parentDoc = parent->getDocument()->getName(); |
| 2463 |
info.parent = parent->getNameInDocument(); |
| 2464 |
} |
| 2465 |
if (syncPlacement) { |
| 2466 |
std::ostringstream ss; |
| 2467 |
App::DocumentObject* topParent = nullptr; |
| 2468 |
item->getSubName(ss, topParent); |
| 2469 |
if (topParent) { |
| 2470 |
info.topDoc = topParent->getDocument()->getName(); |
| 2471 |
info.topObj = topParent->getNameInDocument(); |
| 2472 |
ss << obj->getNameInDocument() << '.'; |
| 2473 |
info.topSubname = ss.str(); |
| 2474 |
} |
| 2475 |
} |
| 2476 |
} |
| 2477 |
// Because the existence of subname, we must de-select the drag the |
| 2478 |
// object manually. Just do a complete clear here for simplicity |
| 2479 |
Selection().selStackPush(); |
| 2480 |
Selection().clearCompleteSelection(); |
| 2481 |
|
| 2482 |
// Open command |
| 2483 |
auto manager = Application::Instance->macroManager(); |
| 2484 |
try { |
| 2485 |
std::vector<App::DocumentObject*> droppedObjs; |
| 2486 |
for (auto& info : infos) { |
| 2487 |
auto doc = App::GetApplication().getDocument(info.doc.c_str()); |
| 2488 |
if (!doc) { |
| 2489 |
continue; |
| 2490 |
} |
| 2491 |
auto obj = doc->getObject(info.obj.c_str()); |
| 2492 |
auto vpc = freecad_cast<ViewProviderDocumentObject*>( |
| 2493 |
Application::Instance->getViewProvider(obj) |
| 2494 |
); |
| 2495 |
if (!vpc) { |
| 2496 |
FC_WARN("Cannot find dragging object " << info.obj); |
| 2497 |
continue; |
| 2498 |
} |
| 2499 |
|
| 2500 |
Base::Matrix4D mat; |
| 2501 |
App::PropertyPlacement* propPlacement = nullptr; |
| 2502 |
if (syncPlacement) { |
| 2503 |
if (!info.topObj.empty()) { |
| 2504 |
auto doc = App::GetApplication().getDocument(info.topDoc.c_str()); |
| 2505 |
if (doc) { |
| 2506 |
auto topObj = doc->getObject(info.topObj.c_str()); |
| 2507 |
if (topObj) { |
| 2508 |
auto sobj = topObj->getSubObject(info.topSubname.c_str(), nullptr, &mat); |
| 2509 |
if (sobj == obj) { |
| 2510 |
propPlacement = dynamic_cast<App::PropertyPlacement*>( |
| 2511 |
obj->getPropertyByName("Placement") |
| 2512 |
); |
| 2513 |
} |
| 2514 |
} |
| 2515 |
} |
| 2516 |
} |
| 2517 |
else { |
| 2518 |
propPlacement = dynamic_cast<App::PropertyPlacement*>( |
| 2519 |
obj->getPropertyByName("Placement") |
| 2520 |
); |
| 2521 |
if (propPlacement) { |
| 2522 |
mat = propPlacement->getValue().toMatrix(); |
| 2523 |
} |
| 2524 |
} |
| 2525 |
} |
| 2526 |
|
| 2527 |
if (da == Qt::LinkAction) { |
| 2528 |
std::string name = targetInfo.targetDoc->getUniqueObjectName("Link"); |
| 2529 |
FCMD_DOC_CMD( |
| 2530 |
targetInfo.targetDoc, |
| 2531 |
"addObject('App::Link','" << name << "').setLink(" << Command::getObjectCmd(obj) |
| 2532 |
<< ")" |
| 2533 |
); |
| 2534 |
auto link = targetInfo.targetDoc->getObject(name.c_str()); |
| 2535 |
if (!link) { |
| 2536 |
continue; |
| 2537 |
} |
| 2538 |
std::string linkedLabel = Base::Tools::escapeEncodeString( |
| 2539 |
std::string(obj->getLinkedObject(true)->Label.getValue()) |
| 2540 |
); |
| 2541 |
FCMD_OBJ_CMD(link, "Label='" << linkedLabel << "'"); |
| 2542 |
propPlacement = dynamic_cast<App::PropertyPlacement*>( |
| 2543 |
link->getPropertyByName("Placement") |
| 2544 |
); |
| 2545 |
if (propPlacement) { |
| 2546 |
propPlacement->setValueIfChanged(Base::Placement(mat)); |
| 2547 |
} |
| 2548 |
droppedObjs.push_back(link); |
| 2549 |
} |
| 2550 |
else if (!info.parent.empty()) { |
| 2551 |
auto parentDoc = App::GetApplication().getDocument(info.parentDoc.c_str()); |
| 2552 |
if (!parentDoc) { |
| 2553 |
FC_WARN("Canont find document " << info.parentDoc); |
| 2554 |
continue; |
| 2555 |
} |
| 2556 |
auto parent = parentDoc->getObject(info.parent.c_str()); |
| 2557 |
auto vpp = freecad_cast<ViewProviderDocumentObject*>( |
| 2558 |
Application::Instance->getViewProvider(parent) |
| 2559 |
); |
| 2560 |
if (!vpp) { |
| 2561 |
FC_WARN("Cannot find dragging object's parent " << info.parent); |
| 2562 |
continue; |
| 2563 |
} |
| 2564 |
|
| 2565 |
std::ostringstream ss; |
| 2566 |
ss << Command::getObjectCmd(vpp->getObject()) << ".ViewObject.dragObject(" |
| 2567 |
<< Command::getObjectCmd(obj) << ')'; |
| 2568 |
auto lines = manager->getLines(); |
| 2569 |
vpp->dragObject(obj); |
| 2570 |
if (manager->getLines() == lines) { |
| 2571 |
manager->addLine(MacroManager::Gui, ss.str().c_str()); |
| 2572 |
} |
| 2573 |
|
| 2574 |
// make sure it is not part of a geofeaturegroup anymore. |
| 2575 |
// When this has happen we need to handle all removed |
| 2576 |
// objects |
| 2577 |
auto grp = App::GeoFeatureGroupExtension::getGroupOfObject(obj); |
| 2578 |
if (grp) { |
| 2579 |
FCMD_OBJ_CMD(grp, "removeObject(" << Command::getObjectCmd(obj) << ")"); |
| 2580 |
} |
| 2581 |
|
| 2582 |
// check if the object has been deleted |
| 2583 |
obj = doc->getObject(info.obj.c_str()); |
| 2584 |
if (!obj || !obj->isAttachedToDocument()) { |
| 2585 |
continue; |
| 2586 |
} |
| 2587 |
droppedObjs.push_back(obj); |
| 2588 |
if (propPlacement) { |
| 2589 |
propPlacement->setValueIfChanged(Base::Placement(mat)); |
| 2590 |
} |
| 2591 |
} |
| 2592 |
else { |
| 2593 |
std::ostringstream ss; |
| 2594 |
ss << "App.getDocument('" << targetInfo.targetDoc->getName() << "')." |
| 2595 |
<< (da == Qt::CopyAction ? "copyObject(" : "moveObject(") |
| 2596 |
<< Command::getObjectCmd(obj) << ", True)"; |
| 2597 |
App::DocumentObject* res = nullptr; |
| 2598 |
if (da == Qt::CopyAction) { |
| 2599 |
auto copied = targetInfo.targetDoc->copyObject({obj}, true); |
| 2600 |
if (!copied.empty()) { |
| 2601 |
res = copied.back(); |
| 2602 |
} |
| 2603 |
} |
| 2604 |
else if (da == Qt::MoveAction && obj->getDocument() == targetInfo.targetDoc) { |
| 2605 |
// Moving a object within the document root. |
| 2606 |
// Do not set 'res' as changing the placement is not desired: #13690 |
| 2607 |
droppedObjs.push_back(obj); |
| 2608 |
} |
| 2609 |
else { |
| 2610 |
// Moving a object from another document. |
| 2611 |
res = targetInfo.targetDoc->moveObject(obj, true); |
| 2612 |
} |
| 2613 |
if (res) { |
| 2614 |
propPlacement = dynamic_cast<App::PropertyPlacement*>( |
| 2615 |
res->getPropertyByName("Placement") |
| 2616 |
); |
| 2617 |
if (propPlacement) { |
| 2618 |
propPlacement->setValueIfChanged(Base::Placement(mat)); |
| 2619 |
} |
| 2620 |
droppedObjs.push_back(res); |
| 2621 |
} |
| 2622 |
manager->addLine(MacroManager::App, ss.str().c_str()); |
| 2623 |
} |
| 2624 |
} |
| 2625 |
touched = true; |
| 2626 |
Base::FlagToggler<> guard(_DisableCheckTopParent); |
| 2627 |
Selection().setSelection(targetInfo.targetDoc->getName(), droppedObjs); |
| 2628 |
|
| 2629 |
// If moved, then we sort objects properly. |
| 2630 |
if (da == Qt::MoveAction) { |
| 2631 |
sortDroppedObjects(targetInfo, droppedObjs); |
| 2632 |
} |
| 2633 |
} |
| 2634 |
catch (const Base::Exception& e) { |
| 2635 |
e.reportException(); |
| 2636 |
errMsg = e.what(); |
| 2637 |
} |
| 2638 |
catch (std::exception& e) { |
| 2639 |
FC_ERR("C++ exception: " << e.what()); |
| 2640 |
errMsg = e.what(); |
| 2641 |
} |
| 2642 |
catch (...) { |
| 2643 |
FC_ERR("Unknown exception"); |
| 2644 |
errMsg = "Unknown exception"; |
| 2645 |
} |
| 2646 |
if (!errMsg.empty()) { |
| 2647 |
committer.close(true); |
| 2648 |
QMessageBox::critical( |
| 2649 |
getMainWindow(), |
| 2650 |
QObject::tr("Drag & drop failed"), |
| 2651 |
QString::fromUtf8(errMsg.c_str()) |
| 2652 |
); |
| 2653 |
return false; |
| 2654 |
} |
| 2655 |
return touched; |
| 2656 |
} |
| 2657 |
|
| 2658 |
bool TreeWidget::dropInObject( |
| 2659 |
QDropEvent* event, |
| 2660 |
TargetItemInfo& targetInfo, |
| 2661 |
std::vector<TreeWidget::ObjectItemSubname> items |
| 2662 |
) |
| 2663 |
{ |
| 2664 |
std::string errMsg; |
| 2665 |
auto da = event->dropAction(); |
| 2666 |
bool touched = false; |
| 2667 |
|
| 2668 |
// add object to group |
| 2669 |
auto targetItemObj = static_cast<DocumentObjectItem*>(targetInfo.targetItem); |
| 2670 |
Gui::ViewProviderDocumentObject* vp = targetItemObj->object(); |
| 2671 |
|
| 2672 |
if (!vp || !vp->getObject() || !vp->getObject()->isAttachedToDocument()) { |
| 2673 |
TREE_TRACE("invalid object"); |
| 2674 |
return false; |
| 2675 |
} |
| 2676 |
|
| 2677 |
// if we are in between or if target doesn't accept drops then the target is the parent |
| 2678 |
if (da == Qt::MoveAction && (targetInfo.inThresholdZone || !vp->canDropObjects())) { |
| 2679 |
targetInfo.targetItem = targetInfo.targetItem->parent(); |
| 2680 |
if (targetInfo.targetItem->type() == TreeWidget::DocumentType) { |
| 2681 |
return dropInDocument(event, targetInfo, items); |
| 2682 |
} |
| 2683 |
|
| 2684 |
targetItemObj = static_cast<DocumentObjectItem*>(targetInfo.targetItem); |
| 2685 |
vp = targetItemObj->object(); |
| 2686 |
|
| 2687 |
if (!vp || !vp->getObject() || !vp->getObject()->isAttachedToDocument()) { |
| 2688 |
TREE_TRACE("invalid object"); |
| 2689 |
return false; |
| 2690 |
} |
| 2691 |
} |
| 2692 |
|
| 2693 |
if (da != Qt::LinkAction && !vp->canDropObjects()) { |
| 2694 |
if (!(event->possibleActions() & Qt::LinkAction) || items.size() != 1) { |
| 2695 |
TREE_TRACE("Cannot drop objects"); |
| 2696 |
return false; // no group like object |
| 2697 |
} |
| 2698 |
} |
| 2699 |
|
| 2700 |
App::DocumentObject* targetObj = targetItemObj->object()->getObject(); |
| 2701 |
std::ostringstream targetSubname; |
| 2702 |
App::DocumentObject* targetParent = nullptr; |
| 2703 |
targetItemObj->getSubName(targetSubname, targetParent); |
| 2704 |
Selection().selStackPush(); |
| 2705 |
Selection().clearCompleteSelection(); |
| 2706 |
if (targetParent) { |
| 2707 |
targetSubname << vp->getObject()->getNameInDocument() << '.'; |
| 2708 |
Selection().addSelection( |
| 2709 |
targetParent->getDocument()->getName(), |
| 2710 |
targetParent->getNameInDocument(), |
| 2711 |
targetSubname.str().c_str() |
| 2712 |
); |
| 2713 |
} |
| 2714 |
else { |
| 2715 |
targetParent = targetObj; |
| 2716 |
Selection().addSelection( |
| 2717 |
targetParent->getDocument()->getName(), |
| 2718 |
targetParent->getNameInDocument() |
| 2719 |
); |
| 2720 |
} |
| 2721 |
|
| 2722 |
// Open command |
| 2723 |
App::AutoTransaction committer("Drop object"); |
| 2724 |
|
| 2725 |
bool syncPlacement = TreeParams::getSyncPlacement() && targetItemObj->isGroup(); |
| 2726 |
bool setSelection = true; |
| 2727 |
std::vector<App::DocumentObject*> draggedObjects; |
| 2728 |
std::vector<std::pair<App::DocumentObject*, std::string>> droppedObjects; |
| 2729 |
std::vector<ItemInfo> infos; |
| 2730 |
// Only keep text names here, because you never know when doing drag |
| 2731 |
// and drop some object may delete other objects. |
| 2732 |
infos.reserve(items.size()); |
| 2733 |
for (auto& v : items) { |
| 2734 |
infos.emplace_back(); |
| 2735 |
auto& info = infos.back(); |
| 2736 |
auto item = v.first; |
| 2737 |
App::DocumentObject* obj = item->object()->getObject(); |
| 2738 |
|
| 2739 |
std::ostringstream str; |
| 2740 |
App::DocumentObject* topParent = nullptr; |
| 2741 |
auto owner = item->getRelativeParent(str, targetItemObj, &topParent, &info.topSubname); |
| 2742 |
if (syncPlacement && topParent) { |
| 2743 |
info.topDoc = topParent->getDocument()->getName(); |
| 2744 |
info.topObj = topParent->getNameInDocument(); |
| 2745 |
} |
| 2746 |
info.subname = str.str(); |
| 2747 |
info.doc = obj->getDocument()->getName(); |
| 2748 |
info.obj = obj->getNameInDocument(); |
| 2749 |
if (owner) { |
| 2750 |
info.ownerDoc = owner->getDocument()->getName(); |
| 2751 |
info.owner = owner->getNameInDocument(); |
| 2752 |
} |
| 2753 |
|
| 2754 |
info.subs.swap(v.second); |
| 2755 |
|
| 2756 |
// check if items can be dragged |
| 2757 |
if (da == Qt::MoveAction && item->myOwner == targetItemObj->myOwner |
| 2758 |
&& vp->canDragAndDropObject(obj)) { |
| 2759 |
auto parentItem = item->getParentItem(); |
| 2760 |
if (!parentItem) { |
| 2761 |
info.dragging = true; |
| 2762 |
} |
| 2763 |
else { |
| 2764 |
|
| 2765 |
bool allParentsOK = canDragFromParents(parentItem, obj, targetObj); |
| 2766 |
|
| 2767 |
if (allParentsOK) { |
| 2768 |
auto vpp = parentItem->object(); |
| 2769 |
info.dragging = true; |
| 2770 |
info.parent = vpp->getObject()->getNameInDocument(); |
| 2771 |
info.parentDoc = vpp->getObject()->getDocument()->getName(); |
| 2772 |
} |
| 2773 |
else { |
| 2774 |
committer.close(true); |
| 2775 |
return false; |
| 2776 |
} |
| 2777 |
} |
| 2778 |
} |
| 2779 |
|
| 2780 |
if (da != Qt::LinkAction |
| 2781 |
&& !vp->canDropObjectEx(obj, owner, info.subname.c_str(), item->mySubs)) { |
| 2782 |
if (event->possibleActions() & Qt::LinkAction) { |
| 2783 |
if (items.size() > 1) { |
| 2784 |
committer.close(true); |
| 2785 |
TREE_TRACE("Cannot replace with more than one object"); |
| 2786 |
return false; |
| 2787 |
} |
| 2788 |
auto ext = vp->getObject()->getExtensionByType<App::LinkBaseExtension>(true); |
| 2789 |
if ((!ext || !ext->getLinkedObjectProperty()) && !targetItemObj->getParentItem()) { |
| 2790 |
committer.close(true); |
| 2791 |
TREE_TRACE("Cannot replace without parent"); |
| 2792 |
return false; |
| 2793 |
} |
| 2794 |
da = Qt::LinkAction; |
| 2795 |
} |
| 2796 |
} |
| 2797 |
} |
| 2798 |
|
| 2799 |
try { |
| 2800 |
std::set<App::DocumentObject*> inList; |
| 2801 |
auto parentObj = targetObj; |
| 2802 |
if (da == Qt::LinkAction && targetItemObj->getParentItem()) { |
| 2803 |
parentObj = targetItemObj->getParentItem()->object()->getObject(); |
| 2804 |
} |
| 2805 |
inList = parentObj->getInListEx(true); |
| 2806 |
inList.insert(parentObj); |
| 2807 |
|
| 2808 |
std::string target = targetObj->getNameInDocument(); |
| 2809 |
auto targetDoc = targetObj->getDocument(); |
| 2810 |
for (auto& info : infos) { |
| 2811 |
auto& subname = info.subname; |
| 2812 |
targetObj = targetDoc->getObject(target.c_str()); |
| 2813 |
vp = freecad_cast<ViewProviderDocumentObject*>( |
| 2814 |
Application::Instance->getViewProvider(targetObj) |
| 2815 |
); |
| 2816 |
if (!vp) { |
| 2817 |
FC_ERR("Cannot find drop target object " << target); |
| 2818 |
break; |
| 2819 |
} |
| 2820 |
|
| 2821 |
auto doc = App::GetApplication().getDocument(info.doc.c_str()); |
| 2822 |
if (!doc) { |
| 2823 |
FC_WARN("Cannot find document " << info.doc); |
| 2824 |
continue; |
| 2825 |
} |
| 2826 |
auto obj = doc->getObject(info.obj.c_str()); |
| 2827 |
auto vpc = freecad_cast<ViewProviderDocumentObject*>( |
| 2828 |
Application::Instance->getViewProvider(obj) |
| 2829 |
); |
| 2830 |
if (!vpc) { |
| 2831 |
FC_WARN("Cannot find dragging object " << info.obj); |
| 2832 |
continue; |
| 2833 |
} |
| 2834 |
|
| 2835 |
ViewProviderDocumentObject* vpp = nullptr; |
| 2836 |
if (da != Qt::LinkAction && !info.parentDoc.empty()) { |
| 2837 |
auto parentDoc = App::GetApplication().getDocument(info.parentDoc.c_str()); |
| 2838 |
if (parentDoc) { |
| 2839 |
auto parent = parentDoc->getObject(info.parent.c_str()); |
| 2840 |
vpp = freecad_cast<ViewProviderDocumentObject*>( |
| 2841 |
Application::Instance->getViewProvider(parent) |
| 2842 |
); |
| 2843 |
} |
| 2844 |
if (!vpp) { |
| 2845 |
FC_WARN("Cannot find dragging object's parent " << info.parent); |
| 2846 |
continue; |
| 2847 |
} |
| 2848 |
} |
| 2849 |
|
| 2850 |
App::DocumentObject* owner = nullptr; |
| 2851 |
if (!info.ownerDoc.empty()) { |
| 2852 |
auto ownerDoc = App::GetApplication().getDocument(info.ownerDoc.c_str()); |
| 2853 |
if (ownerDoc) { |
| 2854 |
owner = ownerDoc->getObject(info.owner.c_str()); |
| 2855 |
} |
| 2856 |
if (!owner) { |
| 2857 |
FC_WARN("Cannot find dragging object's top parent " << info.owner); |
| 2858 |
continue; |
| 2859 |
} |
| 2860 |
} |
| 2861 |
|
| 2862 |
Base::Matrix4D mat; |
| 2863 |
App::PropertyPlacement* propPlacement = nullptr; |
| 2864 |
if (syncPlacement) { |
| 2865 |
propPlacement = DropHandler::getPlacement(info, obj, mat); |
| 2866 |
} |
| 2867 |
|
| 2868 |
auto dropParent = targetParent; |
| 2869 |
|
| 2870 |
auto manager = Application::Instance->macroManager(); |
| 2871 |
std::ostringstream ss; |
| 2872 |
if (vpp) { |
| 2873 |
auto lines = manager->getLines(); |
| 2874 |
ss << Command::getObjectCmd(vpp->getObject()) << ".ViewObject.dragObject(" |
| 2875 |
<< Command::getObjectCmd(obj) << ')'; |
| 2876 |
vpp->dragObject(obj); |
| 2877 |
if (manager->getLines() == lines) { |
| 2878 |
manager->addLine(MacroManager::Gui, ss.str().c_str()); |
| 2879 |
} |
| 2880 |
owner = nullptr; |
| 2881 |
subname.clear(); |
| 2882 |
ss.str(""); |
| 2883 |
|
| 2884 |
obj = doc->getObject(info.obj.c_str()); |
| 2885 |
if (!obj || !obj->isAttachedToDocument()) { |
| 2886 |
FC_WARN("Dropping object deleted: " << info.doc << '#' << info.obj); |
| 2887 |
continue; |
| 2888 |
} |
| 2889 |
} |
| 2890 |
|
| 2891 |
if (da == Qt::MoveAction) { |
| 2892 |
// Try to adjust relative links to avoid cyclic dependency, may |
| 2893 |
// throw exception if failed |
| 2894 |
ss.str(""); |
| 2895 |
ss << Command::getObjectCmd(obj) << ".adjustRelativeLinks(" |
| 2896 |
<< Command::getObjectCmd(targetObj) << ")"; |
| 2897 |
manager->addLine(MacroManager::Gui, ss.str().c_str()); |
| 2898 |
|
| 2899 |
std::set<App::DocumentObject*> visited; |
| 2900 |
if (obj->adjustRelativeLinks(inList, &visited)) { |
| 2901 |
inList = parentObj->getInListEx(true); |
| 2902 |
inList.insert(parentObj); |
| 2903 |
|
| 2904 |
// TODO: link adjustment and placement adjustment does |
| 2905 |
// not work together at the moment. |
| 2906 |
propPlacement = nullptr; |
| 2907 |
} |
| 2908 |
} |
| 2909 |
|
| 2910 |
if (inList.contains(obj)) { |
| 2911 |
FC_THROWM(Base::RuntimeError, "Dependency loop detected for " << obj->getFullName()); |
| 2912 |
} |
| 2913 |
|
| 2914 |
|
| 2915 |
std::string dropName; |
| 2916 |
ss.str(""); |
| 2917 |
if (da == Qt::LinkAction) { |
| 2918 |
auto parentItem = targetItemObj->getParentItem(); |
| 2919 |
if (parentItem) { |
| 2920 |
ss << Command::getObjectCmd( |
| 2921 |
parentItem->object()->getObject(), |
| 2922 |
nullptr, |
| 2923 |
".replaceObject(", |
| 2924 |
true |
| 2925 |
) << Command::getObjectCmd(targetObj) |
| 2926 |
<< "," << Command::getObjectCmd(obj) << ")"; |
| 2927 |
|
| 2928 |
std::ostringstream ss; |
| 2929 |
|
| 2930 |
dropParent = nullptr; |
| 2931 |
parentItem->getSubName(ss, dropParent); |
| 2932 |
if (dropParent) { |
| 2933 |
ss << parentItem->object()->getObject()->getNameInDocument() << '.'; |
| 2934 |
} |
| 2935 |
else { |
| 2936 |
dropParent = parentItem->object()->getObject(); |
| 2937 |
} |
| 2938 |
ss << obj->getNameInDocument() << '.'; |
| 2939 |
dropName = ss.str(); |
| 2940 |
} |
| 2941 |
else { |
| 2942 |
TREE_WARN("ignore replace operation without parent"); |
| 2943 |
continue; |
| 2944 |
} |
| 2945 |
|
| 2946 |
Gui::Command::runCommand(Gui::Command::App, ss.str().c_str()); |
| 2947 |
} |
| 2948 |
else { |
| 2949 |
ss << Command::getObjectCmd(vp->getObject()) << ".ViewObject.dropObject(" |
| 2950 |
<< Command::getObjectCmd(obj); |
| 2951 |
if (owner) { |
| 2952 |
ss << "," << Command::getObjectCmd(owner) << ",'" << subname << "',["; |
| 2953 |
} |
| 2954 |
else { |
| 2955 |
ss << ",None,'',["; |
| 2956 |
} |
| 2957 |
for (auto& sub : info.subs) { |
| 2958 |
ss << "'" << sub << "',"; |
| 2959 |
} |
| 2960 |
ss << "])"; |
| 2961 |
auto lines = manager->getLines(); |
| 2962 |
dropName = vp->dropObjectEx(obj, owner, subname.c_str(), info.subs); |
| 2963 |
if (manager->getLines() == lines) { |
| 2964 |
manager->addLine(MacroManager::Gui, ss.str().c_str()); |
| 2965 |
} |
| 2966 |
if (!dropName.empty()) { |
| 2967 |
dropName = targetSubname.str() + dropName; |
| 2968 |
} |
| 2969 |
} |
| 2970 |
|
| 2971 |
touched = true; |
| 2972 |
|
| 2973 |
// Construct the subname pointing to the dropped object |
| 2974 |
if (dropName.empty()) { |
| 2975 |
auto pos = targetSubname.tellp(); |
| 2976 |
targetSubname << obj->getNameInDocument() << '.' << std::ends; |
| 2977 |
dropName = targetSubname.str(); |
| 2978 |
targetSubname.seekp(pos); |
| 2979 |
} |
| 2980 |
|
| 2981 |
Base::Matrix4D newMat; |
| 2982 |
auto sobj = dropParent->getSubObject(dropName.c_str(), nullptr, &newMat); |
| 2983 |
if (!sobj) { |
| 2984 |
FC_LOG( |
| 2985 |
"failed to find dropped object " << dropParent->getFullName() << '.' << dropName |
| 2986 |
); |
| 2987 |
setSelection = false; |
| 2988 |
continue; |
| 2989 |
} |
| 2990 |
|
| 2991 |
if (da != Qt::CopyAction && propPlacement) { |
| 2992 |
// try to adjust placement |
| 2993 |
if ((info.dragging && sobj == obj) |
| 2994 |
|| (!info.dragging && sobj->getLinkedObject(false) == obj)) { |
| 2995 |
if (!info.dragging) { |
| 2996 |
propPlacement = freecad_cast<App::PropertyPlacement*>( |
| 2997 |
sobj->getPropertyByName("Placement") |
| 2998 |
); |
| 2999 |
} |
| 3000 |
if (propPlacement) { |
| 3001 |
newMat *= propPlacement->getValue().inverse().toMatrix(); |
| 3002 |
newMat.inverseGauss(); |
| 3003 |
Base::Placement pla(newMat * mat); |
| 3004 |
propPlacement->setValueIfChanged(pla); |
| 3005 |
} |
| 3006 |
} |
| 3007 |
} |
| 3008 |
droppedObjects.emplace_back(dropParent, dropName); |
| 3009 |
draggedObjects.push_back(obj); |
| 3010 |
} |
| 3011 |
Base::FlagToggler<> guard(_DisableCheckTopParent); |
| 3012 |
if (setSelection && !droppedObjects.empty()) { |
| 3013 |
Selection().selStackPush(); |
| 3014 |
Selection().clearCompleteSelection(); |
| 3015 |
for (auto& v : droppedObjects) { |
| 3016 |
Selection().addSelection( |
| 3017 |
v.first->getDocument()->getName(), |
| 3018 |
v.first->getNameInDocument(), |
| 3019 |
v.second.c_str() |
| 3020 |
); |
| 3021 |
} |
| 3022 |
Selection().selStackPush(); |
| 3023 |
} |
| 3024 |
|
| 3025 |
// If moved, then we sort objects properly. |
| 3026 |
if (da == Qt::MoveAction && vp->acceptReorderingObjects()) { |
| 3027 |
sortDroppedObjects(targetInfo, draggedObjects); |
| 3028 |
} |
| 3029 |
} |
| 3030 |
catch (const Base::Exception& e) { |
| 3031 |
e.reportException(); |
| 3032 |
errMsg = e.what(); |
| 3033 |
} |
| 3034 |
catch (std::exception& e) { |
| 3035 |
FC_ERR("C++ exception: " << e.what()); |
| 3036 |
errMsg = e.what(); |
| 3037 |
} |
| 3038 |
catch (...) { |
| 3039 |
FC_ERR("Unknown exception"); |
| 3040 |
errMsg = "Unknown exception"; |
| 3041 |
} |
| 3042 |
if (!errMsg.empty()) { |
| 3043 |
committer.close(true); |
| 3044 |
QMessageBox::critical( |
| 3045 |
getMainWindow(), |
| 3046 |
QObject::tr("Drag & drop failed"), |
| 3047 |
QString::fromUtf8(errMsg.c_str()) |
| 3048 |
); |
| 3049 |
return false; |
| 3050 |
} |
| 3051 |
return touched; |
| 3052 |
} |
| 3053 |
|
| 3054 |
bool TreeWidget::canDragFromParents( |
| 3055 |
DocumentObjectItem* parentItem, |
| 3056 |
App::DocumentObject* obj, |
| 3057 |
App::DocumentObject* target |
| 3058 |
) |
| 3059 |
{ |
| 3060 |
// We query all the parents recursively. (for cases like assembly/group/part) |
| 3061 |
bool allParentsOK = true; |
| 3062 |
while (parentItem) { |
| 3063 |
if (!parentItem->object()->canDragObjectToTarget(obj, target)) { |
| 3064 |
allParentsOK = false; |
| 3065 |
break; |
| 3066 |
} |
| 3067 |
parentItem = parentItem->getParentItem(); |
| 3068 |
} |
| 3069 |
|
| 3070 |
return allParentsOK; |
| 3071 |
} |
| 3072 |
|
| 3073 |
void TreeWidget::dropEvent(QDropEvent* event) |
| 3074 |
{ |
| 3075 |
// FIXME: This should actually be done inside dropMimeData |
| 3076 |
|
| 3077 |
TargetItemInfo targetInfo = getTargetInfo(event); |
| 3078 |
if (!targetInfo.targetItem) { |
| 3079 |
return; |
| 3080 |
} |
| 3081 |
|
| 3082 |
// filter out the selected items we cannot handle |
| 3083 |
std::vector<ObjectItemSubname> items; |
| 3084 |
items = DropHandler::filterItems(selectedItems(), targetInfo.targetItem); |
| 3085 |
if (items.empty()) { |
| 3086 |
return; // nothing needs to be done |
| 3087 |
} |
| 3088 |
|
| 3089 |
event->setDropAction(getDropAction(items.size(), targetInfo.targetItem->type())); |
| 3090 |
|
| 3091 |
bool touched = false; |
| 3092 |
if (targetInfo.targetItem->type() == TreeWidget::ObjectType) { |
| 3093 |
touched = dropInObject(event, targetInfo, items); |
| 3094 |
} |
| 3095 |
else if (targetInfo.targetItem->type() == TreeWidget::DocumentType) { |
| 3096 |
touched = dropInDocument(event, targetInfo, items); |
| 3097 |
} |
| 3098 |
|
| 3099 |
if (touched && TreeParams::getRecomputeOnDrop()) { |
| 3100 |
targetInfo.targetDoc->recompute(); |
| 3101 |
} |
| 3102 |
if (touched && TreeParams::getSyncView()) { |
| 3103 |
auto gdoc = Application::Instance->getDocument(targetInfo.targetDoc); |
| 3104 |
if (gdoc) { |
| 3105 |
gdoc->setActiveView(); |
| 3106 |
} |
| 3107 |
} |
| 3108 |
} |
| 3109 |
|
| 3110 |
void TreeWidget::sortDroppedObjects( |
| 3111 |
TargetItemInfo& targetInfo, |
| 3112 |
std::vector<App::DocumentObject*> draggedObjects |
| 3113 |
) |
| 3114 |
{ |
| 3115 |
if (targetInfo.targetItem == targetInfo.underMouseItem) { |
| 3116 |
return; |
| 3117 |
} |
| 3118 |
auto underMouseItemObj = static_cast<DocumentObjectItem*>(targetInfo.underMouseItem); |
| 3119 |
auto underMouseObj = underMouseItemObj->object()->getObject(); |
| 3120 |
std::vector<App::DocumentObject*> sortedObjList; |
| 3121 |
std::vector<App::DocumentObject*> objList; |
| 3122 |
|
| 3123 |
auto sortIntoList = [&sortedObjList, &draggedObjects, underMouseObj, &targetInfo]( |
| 3124 |
const std::vector<App::DocumentObject*>& objects |
| 3125 |
) { |
| 3126 |
for (auto* obj : objects) { |
| 3127 |
if (obj == underMouseObj) { |
| 3128 |
if (targetInfo.inBottomHalf) { |
| 3129 |
sortedObjList.push_back(obj); |
| 3130 |
} |
| 3131 |
|
| 3132 |
for (auto* draggedObj : draggedObjects) { |
| 3133 |
sortedObjList.push_back(draggedObj); |
| 3134 |
} |
| 3135 |
|
| 3136 |
if (!targetInfo.inBottomHalf) { |
| 3137 |
sortedObjList.push_back(obj); |
| 3138 |
} |
| 3139 |
} |
| 3140 |
else { |
| 3141 |
if (std::ranges::find(draggedObjects, obj) == draggedObjects.end()) { |
| 3142 |
sortedObjList.push_back(obj); |
| 3143 |
} |
| 3144 |
} |
| 3145 |
} |
| 3146 |
}; |
| 3147 |
|
| 3148 |
if (targetInfo.targetItem->type() == TreeWidget::ObjectType) { |
| 3149 |
// To update the order of items of groups such as App::Part, we just need to change the |
| 3150 |
// order in the Group property |
| 3151 |
auto targetItemObj = static_cast<DocumentObjectItem*>(targetInfo.targetItem); |
| 3152 |
App::DocumentObject* targetObj = targetItemObj->object()->getObject(); |
| 3153 |
|
| 3154 |
auto propGroup = freecad_cast<App::PropertyLinkList*>(targetObj->getPropertyByName("Group")); |
| 3155 |
if (!propGroup) { |
| 3156 |
return; |
| 3157 |
} |
| 3158 |
|
| 3159 |
objList = propGroup->getValue(); |
| 3160 |
sortIntoList(objList); // Move dropped objects to correct position |
| 3161 |
propGroup->setValue(sortedObjList); |
| 3162 |
} |
| 3163 |
else if (targetInfo.targetItem->type() == TreeWidget::DocumentType) { |
| 3164 |
Gui::Document* guiDoc = Gui::Application::Instance->getDocument( |
| 3165 |
targetInfo.targetDoc->getName() |
| 3166 |
); |
| 3167 |
objList = guiDoc->getTreeRootObjects(); |
| 3168 |
// First we need to sort objList by treeRank. |
| 3169 |
std::sort(objList.begin(), objList.end(), [](App::DocumentObject* a, App::DocumentObject* b) { |
| 3170 |
auto vpA = dynamic_cast<Gui::ViewProviderDocumentObject*>( |
| 3171 |
Gui::Application::Instance->getViewProvider(a) |
| 3172 |
); |
| 3173 |
auto vpB = dynamic_cast<Gui::ViewProviderDocumentObject*>( |
| 3174 |
Gui::Application::Instance->getViewProvider(b) |
| 3175 |
); |
| 3176 |
if (vpA && vpB) { |
| 3177 |
return vpA->getTreeRank() < vpB->getTreeRank(); |
| 3178 |
} |
| 3179 |
return false; // Keep the original order if either vpA or vpB is nullptr |
| 3180 |
}); |
| 3181 |
|
| 3182 |
// Then we move dropped objects to their correct position |
| 3183 |
sortIntoList(objList); |
| 3184 |
|
| 3185 |
// Then we set the tree rank |
| 3186 |
for (size_t i = 0; i < sortedObjList.size(); ++i) { |
| 3187 |
auto vp = freecad_cast<ViewProviderDocumentObject*>( |
| 3188 |
Application::Instance->getViewProvider(sortedObjList[i]) |
| 3189 |
); |
| 3190 |
vp->setTreeRank(i); |
| 3191 |
} |
| 3192 |
|
| 3193 |
// Lastly we refresh the tree |
| 3194 |
static_cast<DocumentItem*>(targetInfo.targetItem)->sortObjectItems(); |
| 3195 |
} |
| 3196 |
} |
| 3197 |
|
| 3198 |
void TreeWidget::drawRow( |
| 3199 |
QPainter* painter, |
| 3200 |
const QStyleOptionViewItem& options, |
| 3201 |
const QModelIndex& index |
| 3202 |
) const |
| 3203 |
{ |
| 3204 |
QTreeWidget::drawRow(painter, options, index); |
| 3205 |
} |
| 3206 |
|
| 3207 |
void TreeWidget::slotNewDocument(const Gui::Document& Doc, bool isMainDoc) |
| 3208 |
{ |
| 3209 |
if (Doc.getDocument()->testStatus(App::Document::TempDoc)) { |
| 3210 |
return; |
| 3211 |
} |
| 3212 |
auto item = new DocumentItem(&Doc, this->rootItem); |
| 3213 |
if (isMainDoc) { |
| 3214 |
this->expandItem(item); |
| 3215 |
} |
| 3216 |
item->setIcon(0, *documentPixmap); |
| 3217 |
item->setText(0, QString::fromUtf8(Doc.getDocument()->Label.getValue())); |
| 3218 |
DocumentMap[&Doc] = item; |
| 3219 |
} |
| 3220 |
|
| 3221 |
|
| 3222 |
void TreeWidget::onReloadDoc() |
| 3223 |
{ |
| 3224 |
if (!this->contextItem || this->contextItem->type() != DocumentType) { |
| 3225 |
return; |
| 3226 |
} |
| 3227 |
auto docitem = static_cast<DocumentItem*>(this->contextItem); |
| 3228 |
App::Document* doc = docitem->document()->getDocument(); |
| 3229 |
std::string name = doc->FileName.getValue(); |
| 3230 |
Application::Instance->reopen(doc); |
| 3231 |
for (auto& v : DocumentMap) { |
| 3232 |
if (name == v.first->getDocument()->FileName.getValue()) { |
| 3233 |
scrollToItem(v.second); |
| 3234 |
App::GetApplication().setActiveDocument(v.first->getDocument()); |
| 3235 |
break; |
| 3236 |
} |
| 3237 |
} |
| 3238 |
} |
| 3239 |
|
| 3240 |
void TreeWidget::onCloseDoc() |
| 3241 |
{ |
| 3242 |
if (!this->contextItem || this->contextItem->type() != DocumentType) { |
| 3243 |
return; |
| 3244 |
} |
| 3245 |
try { |
| 3246 |
auto docitem = static_cast<DocumentItem*>(this->contextItem); |
| 3247 |
Gui::Document* gui = docitem->document(); |
| 3248 |
App::Document* doc = gui->getDocument(); |
| 3249 |
if (gui->canClose(true, true)) { |
| 3250 |
Command::doCommand(Command::Doc, "App.closeDocument(\"%s\")", doc->getName()); |
| 3251 |
} |
| 3252 |
} |
| 3253 |
catch (const Base::Exception& e) { |
| 3254 |
e.reportException(); |
| 3255 |
} |
| 3256 |
catch (std::exception& e) { |
| 3257 |
FC_ERR("C++ exception: " << e.what()); |
| 3258 |
} |
| 3259 |
catch (...) { |
| 3260 |
FC_ERR("Unknown exception"); |
| 3261 |
} |
| 3262 |
} |
| 3263 |
|
| 3264 |
void TreeWidget::onOpenFileLocation() |
| 3265 |
{ |
| 3266 |
auto docitem = static_cast<DocumentItem*>(this->contextItem); |
| 3267 |
App::Document* doc = docitem->document()->getDocument(); |
| 3268 |
std::string name = doc->FileName.getValue(); |
| 3269 |
|
| 3270 |
const QFileInfo fileInfo(QString::fromStdString(name)); |
| 3271 |
if (!fileInfo.exists()) { |
| 3272 |
QMessageBox::warning(this, tr("Error"), tr("File does not exist.")); |
| 3273 |
return; |
| 3274 |
} |
| 3275 |
|
| 3276 |
const QString filePath = fileInfo.canonicalPath(); |
| 3277 |
bool success = false; |
| 3278 |
|
| 3279 |
#if defined(Q_OS_MAC) |
| 3280 |
success = QProcess::startDetached(QStringLiteral("open"), {filePath}); |
| 3281 |
#elif defined(Q_OS_WIN) |
| 3282 |
QStringList param; |
| 3283 |
if (!fileInfo.isDir()) { |
| 3284 |
param += QStringLiteral("/select,"); |
| 3285 |
} |
| 3286 |
param += QDir::toNativeSeparators(filePath); |
| 3287 |
success = QProcess::startDetached(QStringLiteral("explorer.exe"), param); |
| 3288 |
#else |
| 3289 |
success = QProcess::startDetached(QStringLiteral("xdg-open"), {filePath}); |
| 3290 |
#endif |
| 3291 |
|
| 3292 |
if (!success) { |
| 3293 |
QMessageBox::warning(this, tr("Error"), tr("Failed to open directory.")); |
| 3294 |
} |
| 3295 |
} |
| 3296 |
|
| 3297 |
void TreeWidget::slotRenameDocument(const Gui::Document& Doc) |
| 3298 |
{ |
| 3299 |
// do nothing here |
| 3300 |
Q_UNUSED(Doc); |
| 3301 |
} |
| 3302 |
|
| 3303 |
void TreeWidget::slotChangedViewObject(const Gui::ViewProvider& vp, const App::Property& prop) |
| 3304 |
{ |
| 3305 |
if (!App::GetApplication().isRestoring() && vp.isDerivedFrom<ViewProviderDocumentObject>()) { |
| 3306 |
const auto& vpd = static_cast<const ViewProviderDocumentObject&>(vp); |
| 3307 |
if (&prop == &vpd.ShowInTree) { |
| 3308 |
ChangedObjects.emplace(vpd.getObject(), 0); |
| 3309 |
_updateStatus(); |
| 3310 |
} |
| 3311 |
} |
| 3312 |
} |
| 3313 |
|
| 3314 |
void TreeWidget::slotTouchedObject(const App::DocumentObject& obj) |
| 3315 |
{ |
| 3316 |
ChangedObjects.emplace(const_cast<App::DocumentObject*>(&obj), 0); |
| 3317 |
_updateStatus(); |
| 3318 |
} |
| 3319 |
|
| 3320 |
void TreeWidget::slotShowHidden(const Gui::Document& Doc) |
| 3321 |
{ |
| 3322 |
auto it = DocumentMap.find(&Doc); |
| 3323 |
if (it != DocumentMap.end()) { |
| 3324 |
it->second->updateItemsVisibility(it->second, it->second->showHidden()); |
| 3325 |
} |
| 3326 |
} |
| 3327 |
|
| 3328 |
void TreeWidget::slotRelabelDocument(const Gui::Document& Doc) |
| 3329 |
{ |
| 3330 |
auto it = DocumentMap.find(&Doc); |
| 3331 |
if (it != DocumentMap.end()) { |
| 3332 |
it->second->setText(0, QString::fromUtf8(Doc.getDocument()->Label.getValue())); |
| 3333 |
} |
| 3334 |
} |
| 3335 |
|
| 3336 |
void TreeWidget::slotActiveDocument(const Gui::Document& Doc) |
| 3337 |
{ |
| 3338 |
auto jt = DocumentMap.find(&Doc); |
| 3339 |
if (jt == DocumentMap.end()) { |
| 3340 |
return; // signal is emitted before the item gets created |
| 3341 |
} |
| 3342 |
int displayMode = TreeParams::getDocumentMode(); |
| 3343 |
for (auto it = DocumentMap.begin(); it != DocumentMap.end(); ++it) { |
| 3344 |
QFont f = it->second->font(0); |
| 3345 |
f.setBold(it == jt); |
| 3346 |
it->second->setHidden(0 == displayMode && it != jt); |
| 3347 |
if (2 == displayMode) { |
| 3348 |
it->second->setExpanded(it == jt); |
| 3349 |
} |
| 3350 |
// this must be done as last step |
| 3351 |
it->second->setFont(0, f); |
| 3352 |
} |
| 3353 |
} |
| 3354 |
|
| 3355 |
struct UpdateDisabler |
| 3356 |
{ |
| 3357 |
QWidget& widget; |
| 3358 |
int& blocked; |
| 3359 |
bool visible {false}; |
| 3360 |
bool focus {false}; |
| 3361 |
|
| 3362 |
// Note! DO NOT block signal here, or else |
| 3363 |
// QTreeWidgetItem::setChildIndicatorPolicy() does not work |
| 3364 |
UpdateDisabler(QWidget& w, int& blocked) |
| 3365 |
: widget(w) |
| 3366 |
, blocked(blocked) |
| 3367 |
{ |
| 3368 |
if (++blocked > 1) { |
| 3369 |
return; |
| 3370 |
} |
| 3371 |
focus = widget.hasFocus(); |
| 3372 |
visible = widget.isVisible(); |
| 3373 |
if (visible) { |
| 3374 |
// setUpdatesEnabled(false) does not seem to speed up anything. |
| 3375 |
// setVisible(false) on the other hand makes QTreeWidget::setData |
| 3376 |
// (i.e. any change to QTreeWidgetItem) faster by 10+ times. |
| 3377 |
// |
| 3378 |
// widget.setUpdatesEnabled(false); |
| 3379 |
|
| 3380 |
widget.setVisible(false); |
| 3381 |
} |
| 3382 |
} |
| 3383 |
~UpdateDisabler() |
| 3384 |
{ |
| 3385 |
if (blocked <= 0 || --blocked != 0) { |
| 3386 |
return; |
| 3387 |
} |
| 3388 |
|
| 3389 |
if (visible) { |
| 3390 |
widget.setVisible(true); |
| 3391 |
if (focus) { |
| 3392 |
widget.setFocus(); |
| 3393 |
} |
| 3394 |
} |
| 3395 |
} |
| 3396 |
}; |
| 3397 |
|
| 3398 |
void TreeWidget::onUpdateStatus() |
| 3399 |
{ |
| 3400 |
if (this->state() == DraggingState || App::GetApplication().isRestoring()) { |
| 3401 |
_updateStatus(); |
| 3402 |
return; |
| 3403 |
} |
| 3404 |
|
| 3405 |
for (auto& v : DocumentMap) { |
| 3406 |
if (v.first->isPerformingTransaction()) { |
| 3407 |
// We have to delay item creation until undo/redo is done, because the |
| 3408 |
// object re-creation while in transaction may break tree view item |
| 3409 |
// update logic. For example, a parent object re-created before its |
| 3410 |
// children, but the parent's link property already contains all the |
| 3411 |
// (detached) children. |
| 3412 |
_updateStatus(); |
| 3413 |
return; |
| 3414 |
} |
| 3415 |
} |
| 3416 |
|
| 3417 |
FC_LOG("begin update status"); |
| 3418 |
|
| 3419 |
UpdateDisabler disabler(*this, updateBlocked); |
| 3420 |
|
| 3421 |
std::vector<App::DocumentObject*> errors; |
| 3422 |
|
| 3423 |
// Use a local copy in case of nested calls |
| 3424 |
auto localNewObjects = NewObjects; |
| 3425 |
NewObjects.clear(); |
| 3426 |
|
| 3427 |
// Checking for new objects |
| 3428 |
for (auto& v : localNewObjects) { |
| 3429 |
auto doc = App::GetApplication().getDocument(v.first.c_str()); |
| 3430 |
if (!doc) { |
| 3431 |
continue; |
| 3432 |
} |
| 3433 |
auto gdoc = Application::Instance->getDocument(doc); |
| 3434 |
if (!gdoc) { |
| 3435 |
continue; |
| 3436 |
} |
| 3437 |
auto docItem = getDocumentItem(gdoc); |
| 3438 |
if (!docItem) { |
| 3439 |
continue; |
| 3440 |
} |
| 3441 |
for (auto id : v.second) { |
| 3442 |
auto obj = doc->getObjectByID(id); |
| 3443 |
if (!obj) { |
| 3444 |
continue; |
| 3445 |
} |
| 3446 |
if (obj->isError()) { |
| 3447 |
errors.push_back(obj); |
| 3448 |
} |
| 3449 |
if (docItem->ObjectMap.contains(obj)) { |
| 3450 |
continue; |
| 3451 |
} |
| 3452 |
auto vpd = freecad_cast<ViewProviderDocumentObject*>(gdoc->getViewProvider(obj)); |
| 3453 |
if (vpd) { |
| 3454 |
docItem->createNewItem(*vpd); |
| 3455 |
} |
| 3456 |
} |
| 3457 |
} |
| 3458 |
|
| 3459 |
// Use a local copy in case of nested calls |
| 3460 |
auto localChangedObjects = ChangedObjects; |
| 3461 |
ChangedObjects.clear(); |
| 3462 |
|
| 3463 |
// Update children of changed objects |
| 3464 |
for (auto& v : localChangedObjects) { |
| 3465 |
auto obj = v.first; |
| 3466 |
|
| 3467 |
auto iter = ObjectTable.find(obj); |
| 3468 |
if (iter == ObjectTable.end()) { |
| 3469 |
continue; |
| 3470 |
} |
| 3471 |
|
| 3472 |
if (v.second.test(CS_Error) && obj->isError()) { |
| 3473 |
errors.push_back(obj); |
| 3474 |
} |
| 3475 |
|
| 3476 |
if (!iter->second.empty()) { |
| 3477 |
auto data = *iter->second.begin(); |
| 3478 |
bool itemHidden = !data->viewObject->showInTree(); |
| 3479 |
if (data->itemHidden != itemHidden) { |
| 3480 |
for (auto& data : iter->second) { |
| 3481 |
data->itemHidden = itemHidden; |
| 3482 |
if (data->docItem->showHidden()) { |
| 3483 |
continue; |
| 3484 |
} |
| 3485 |
for (auto item : data->items) { |
| 3486 |
item->setHidden(itemHidden); |
| 3487 |
} |
| 3488 |
} |
| 3489 |
} |
| 3490 |
} |
| 3491 |
|
| 3492 |
updateChildren(iter->first, iter->second, v.second.test(CS_Output), false); |
| 3493 |
} |
| 3494 |
|
| 3495 |
FC_LOG("update item status"); |
| 3496 |
TimingInit(); |
| 3497 |
for (auto pos = DocumentMap.begin(); pos != DocumentMap.end(); ++pos) { |
| 3498 |
pos->second->testStatus(); |
| 3499 |
} |
| 3500 |
TimingPrint(); |
| 3501 |
|
| 3502 |
// Checking for just restored documents |
| 3503 |
for (auto& v : DocumentMap) { |
| 3504 |
auto docItem = v.second; |
| 3505 |
|
| 3506 |
for (auto obj : docItem->PopulateObjects) { |
| 3507 |
docItem->populateObject(obj); |
| 3508 |
} |
| 3509 |
docItem->PopulateObjects.clear(); |
| 3510 |
|
| 3511 |
auto doc = v.first->getDocument(); |
| 3512 |
|
| 3513 |
if (!docItem->connectChgObject.connected()) { |
| 3514 |
// NOLINTBEGIN |
| 3515 |
docItem->connectChgObject = docItem->document()->signalChangedObject.connect( |
| 3516 |
std::bind(&TreeWidget::slotChangeObject, this, sp::_1, sp::_2) |
| 3517 |
); |
| 3518 |
docItem->connectTouchedObject = doc->signalTouchedObject.connect( |
| 3519 |
std::bind(&TreeWidget::slotTouchedObject, this, sp::_1) |
| 3520 |
); |
| 3521 |
// NOLINTEND |
| 3522 |
} |
| 3523 |
|
| 3524 |
if (doc->testStatus(App::Document::PartialDoc)) { |
| 3525 |
docItem->setIcon(0, *documentPartialPixmap); |
| 3526 |
} |
| 3527 |
else if (docItem->_ExpandInfo) { |
| 3528 |
for (auto& entry : *docItem->_ExpandInfo) { |
| 3529 |
const char* name = entry.first.c_str(); |
| 3530 |
bool legacy = name[0] == '*'; |
| 3531 |
if (legacy) { |
| 3532 |
++name; |
| 3533 |
} |
| 3534 |
auto obj = doc->getObject(name); |
| 3535 |
if (!obj) { |
| 3536 |
continue; |
| 3537 |
} |
| 3538 |
auto iter = docItem->ObjectMap.find(obj); |
| 3539 |
if (iter == docItem->ObjectMap.end()) { |
| 3540 |
continue; |
| 3541 |
} |
| 3542 |
if (iter->second->rootItem) { |
| 3543 |
docItem->restoreItemExpansion(entry.second, iter->second->rootItem); |
| 3544 |
} |
| 3545 |
else if (legacy && !iter->second->items.empty()) { |
| 3546 |
auto item = *iter->second->items.begin(); |
| 3547 |
item->setExpanded(true); |
| 3548 |
} |
| 3549 |
} |
| 3550 |
} |
| 3551 |
docItem->_ExpandInfo.reset(); |
| 3552 |
} |
| 3553 |
|
| 3554 |
if (Selection().hasSelection() && !selectTimer->isActive() && !this->isSelectionBlocked()) { |
| 3555 |
this->blockSelection(true); |
| 3556 |
currentDocItem = nullptr; |
| 3557 |
for (auto& v : DocumentMap) { |
| 3558 |
v.second->setSelected(false); |
| 3559 |
v.second->selectItems(); |
| 3560 |
} |
| 3561 |
this->blockSelection(false); |
| 3562 |
} |
| 3563 |
|
| 3564 |
auto activeDocItem = getDocumentItem(Application::Instance->activeDocument()); |
| 3565 |
|
| 3566 |
QTreeWidgetItem* errItem = nullptr; |
| 3567 |
for (auto obj : errors) { |
| 3568 |
DocumentObjectDataPtr data; |
| 3569 |
if (activeDocItem) { |
| 3570 |
auto it = activeDocItem->ObjectMap.find(obj); |
| 3571 |
if (it != activeDocItem->ObjectMap.end()) { |
| 3572 |
data = it->second; |
| 3573 |
} |
| 3574 |
} |
| 3575 |
if (!data) { |
| 3576 |
auto docItem = getDocumentItem(Application::Instance->getDocument(obj->getDocument())); |
| 3577 |
if (docItem) { |
| 3578 |
auto it = docItem->ObjectMap.find(obj); |
| 3579 |
if (it != docItem->ObjectMap.end()) { |
| 3580 |
data = it->second; |
| 3581 |
} |
| 3582 |
} |
| 3583 |
} |
| 3584 |
if (data) { |
| 3585 |
auto item = data->rootItem; |
| 3586 |
if (!item && !data->items.empty()) { |
| 3587 |
item = *data->items.begin(); |
| 3588 |
data->docItem->showItem(item, false, true); |
| 3589 |
} |
| 3590 |
if (!errItem) { |
| 3591 |
errItem = item; |
| 3592 |
} |
| 3593 |
} |
| 3594 |
} |
| 3595 |
if (errItem) { |
| 3596 |
scrollToItem(errItem); |
| 3597 |
} |
| 3598 |
|
| 3599 |
updateGeometries(); |
| 3600 |
statusTimer->stop(); |
| 3601 |
|
| 3602 |
FC_LOG("done update status"); |
| 3603 |
} |
| 3604 |
|
| 3605 |
void TreeWidget::onItemEntered(QTreeWidgetItem* item) |
| 3606 |
{ |
| 3607 |
if (item && item->type() == TreeWidget::ObjectType) { |
| 3608 |
auto objItem = static_cast<DocumentObjectItem*>(item); |
| 3609 |
objItem->displayStatusInfo(); |
| 3610 |
|
| 3611 |
if (TreeParams::getPreSelection()) { |
| 3612 |
int timeout = TreeParams::getPreSelectionDelay(); |
| 3613 |
if (timeout < 0) { |
| 3614 |
timeout = 1; |
| 3615 |
} |
| 3616 |
if (preselectTime.elapsed() < timeout) { |
| 3617 |
onPreSelectTimer(); |
| 3618 |
} |
| 3619 |
else { |
| 3620 |
timeout = TreeParams::getPreSelectionTimeout(); |
| 3621 |
if (timeout < 0) { |
| 3622 |
timeout = 1; |
| 3623 |
} |
| 3624 |
preselectTimer->start(timeout); |
| 3625 |
Selection().rmvPreselect(); |
| 3626 |
} |
| 3627 |
} |
| 3628 |
} |
| 3629 |
else if (TreeParams::getPreSelection()) { |
| 3630 |
Selection().rmvPreselect(); |
| 3631 |
} |
| 3632 |
} |
| 3633 |
|
| 3634 |
void TreeWidget::leaveEvent(QEvent* event) |
| 3635 |
{ |
| 3636 |
Q_UNUSED(event) |
| 3637 |
if (!updateBlocked && TreeParams::getPreSelection()) { |
| 3638 |
preselectTimer->stop(); |
| 3639 |
Selection().rmvPreselect(); |
| 3640 |
} |
| 3641 |
} |
| 3642 |
|
| 3643 |
void TreeWidget::onPreSelectTimer() |
| 3644 |
{ |
| 3645 |
if (!TreeParams::getPreSelection()) { |
| 3646 |
return; |
| 3647 |
} |
| 3648 |
auto item = itemAt(viewport()->mapFromGlobal(QCursor::pos())); |
| 3649 |
if (!item || item->type() != TreeWidget::ObjectType) { |
| 3650 |
return; |
| 3651 |
} |
| 3652 |
|
| 3653 |
preselectTime.restart(); |
| 3654 |
auto objItem = static_cast<DocumentObjectItem*>(item); |
| 3655 |
auto vp = objItem->object(); |
| 3656 |
auto obj = vp->getObject(); |
| 3657 |
std::ostringstream ss; |
| 3658 |
App::DocumentObject* parent = nullptr; |
| 3659 |
objItem->getSubName(ss, parent); |
| 3660 |
if (!parent) { |
| 3661 |
parent = obj; |
| 3662 |
} |
| 3663 |
else if (!obj->redirectSubName(ss, parent, nullptr)) { |
| 3664 |
ss << obj->getNameInDocument() << '.'; |
| 3665 |
} |
| 3666 |
Selection().setPreselect( |
| 3667 |
parent->getDocument()->getName(), |
| 3668 |
parent->getNameInDocument(), |
| 3669 |
ss.str().c_str(), |
| 3670 |
0, |
| 3671 |
0, |
| 3672 |
0, |
| 3673 |
SelectionChanges::MsgSource::TreeView |
| 3674 |
); |
| 3675 |
} |
| 3676 |
|
| 3677 |
void TreeWidget::onItemCollapsed(QTreeWidgetItem* item) |
| 3678 |
{ |
| 3679 |
// object item collapsed |
| 3680 |
if (item && item->type() == TreeWidget::ObjectType) { |
| 3681 |
static_cast<DocumentObjectItem*>(item)->setExpandedStatus(false); |
| 3682 |
} |
| 3683 |
} |
| 3684 |
|
| 3685 |
void TreeWidget::onItemExpanded(QTreeWidgetItem* item) |
| 3686 |
{ |
| 3687 |
// object item expanded |
| 3688 |
if (item && item->type() == TreeWidget::ObjectType) { |
| 3689 |
auto objItem = static_cast<DocumentObjectItem*>(item); |
| 3690 |
objItem->setExpandedStatus(true); |
| 3691 |
objItem->getOwnerDocument()->populateItem(objItem, false, false); |
| 3692 |
} |
| 3693 |
} |
| 3694 |
|
| 3695 |
void TreeWidget::scrollItemToTop() |
| 3696 |
{ |
| 3697 |
auto doc = Application::Instance->activeDocument(); |
| 3698 |
for (auto tree : Instances) { |
| 3699 |
if (!tree->isSelectionAttached() || tree->isSelectionBlocked()) { |
| 3700 |
continue; |
| 3701 |
} |
| 3702 |
|
| 3703 |
tree->_updateStatus(false); |
| 3704 |
|
| 3705 |
if (doc |
| 3706 |
&& Gui::Selection().hasSelection(doc->getDocument()->getName(), ResolveMode::NoResolve)) { |
| 3707 |
auto it = tree->DocumentMap.find(doc); |
| 3708 |
if (it != tree->DocumentMap.end()) { |
| 3709 |
bool lock = tree->blockSelection(true); |
| 3710 |
it->second->selectItems(DocumentItem::SR_FORCE_EXPAND); |
| 3711 |
tree->blockSelection(lock); |
| 3712 |
} |
| 3713 |
} |
| 3714 |
else { |
| 3715 |
tree->blockSelection(true); |
| 3716 |
for (int i = 0; i < tree->rootItem->childCount(); i++) { |
| 3717 |
auto docItem = dynamic_cast<DocumentItem*>(tree->rootItem->child(i)); |
| 3718 |
if (!docItem) { |
| 3719 |
continue; |
| 3720 |
} |
| 3721 |
auto doc = docItem->document()->getDocument(); |
| 3722 |
if (Gui::Selection().hasSelection(doc->getName())) { |
| 3723 |
tree->currentDocItem = docItem; |
| 3724 |
docItem->selectItems(DocumentItem::SR_FORCE_EXPAND); |
| 3725 |
tree->currentDocItem = nullptr; |
| 3726 |
break; |
| 3727 |
} |
| 3728 |
} |
| 3729 |
tree->blockSelection(false); |
| 3730 |
} |
| 3731 |
tree->selectTimer->stop(); |
| 3732 |
tree->_updateStatus(false); |
| 3733 |
} |
| 3734 |
} |
| 3735 |
|
| 3736 |
void TreeWidget::expandSelectedItems(TreeItemMode mode) |
| 3737 |
{ |
| 3738 |
if (!isSelectionAttached()) { |
| 3739 |
return; |
| 3740 |
} |
| 3741 |
|
| 3742 |
const auto items = selectedItems(); |
| 3743 |
for (auto item : items) { |
| 3744 |
switch (mode) { |
| 3745 |
case TreeItemMode::ExpandPath: { |
| 3746 |
QTreeWidgetItem* parentItem = item->parent(); |
| 3747 |
while (parentItem) { |
| 3748 |
parentItem->setExpanded(true); |
| 3749 |
parentItem = parentItem->parent(); |
| 3750 |
} |
| 3751 |
item->setExpanded(true); |
| 3752 |
break; |
| 3753 |
} |
| 3754 |
case TreeItemMode::ExpandItem: |
| 3755 |
item->setExpanded(true); |
| 3756 |
break; |
| 3757 |
case TreeItemMode::CollapseItem: |
| 3758 |
item->setExpanded(false); |
| 3759 |
break; |
| 3760 |
case TreeItemMode::ToggleItem: |
| 3761 |
if (item->isExpanded()) { |
| 3762 |
item->setExpanded(false); |
| 3763 |
} |
| 3764 |
else { |
| 3765 |
item->setExpanded(true); |
| 3766 |
} |
| 3767 |
break; |
| 3768 |
} |
| 3769 |
} |
| 3770 |
} |
| 3771 |
|
| 3772 |
void TreeWidget::setupText() |
| 3773 |
{ |
| 3774 |
this->headerItem()->setText(0, tr("Labels & Attributes")); |
| 3775 |
this->headerItem()->setText(1, tr("Description")); |
| 3776 |
this->headerItem()->setText(2, tr("Internal name")); |
| 3777 |
|
| 3778 |
this->showHiddenAction->setText(tr("Show Items Hidden in Tree View")); |
| 3779 |
this->showHiddenAction->setStatusTip( |
| 3780 |
tr("Shows items that are marked as 'hidden' in the tree view") |
| 3781 |
); |
| 3782 |
|
| 3783 |
this->toggleVisibilityInTreeAction->setText(tr("Toggle Visibility in Tree View")); |
| 3784 |
this->toggleVisibilityInTreeAction->setStatusTip( |
| 3785 |
tr("Toggles the visibility of selected items in the tree view") |
| 3786 |
); |
| 3787 |
|
| 3788 |
this->createGroupAction->setText(tr("Create Group")); |
| 3789 |
this->createGroupAction->setStatusTip(tr("Creates a group")); |
| 3790 |
|
| 3791 |
this->relabelObjectAction->setText(tr("Rename")); |
| 3792 |
this->relabelObjectAction->setStatusTip(tr("Renames object")); |
| 3793 |
|
| 3794 |
this->finishEditingAction->setText(tr("Finish Editing")); |
| 3795 |
this->finishEditingAction->setStatusTip(tr("Finishes editing object")); |
| 3796 |
|
| 3797 |
this->selectDependentsAction->setText(tr("Add Dependent Objects to Selection")); |
| 3798 |
this->selectDependentsAction->setStatusTip(tr("Adds all dependent objects to the selection")); |
| 3799 |
|
| 3800 |
this->closeDocAction->setText(tr("Close Document")); |
| 3801 |
this->closeDocAction->setStatusTip(tr("Closes the document")); |
| 3802 |
|
| 3803 |
#ifdef Q_OS_MAC |
| 3804 |
this->openFileLocationAction->setText(tr("Reveal in Finder")); |
| 3805 |
this->openFileLocationAction->setStatusTip(tr("Reveals the current file location in Finder")); |
| 3806 |
#else |
| 3807 |
this->openFileLocationAction->setText(tr("Open File Location")); |
| 3808 |
this->openFileLocationAction->setStatusTip(tr("Opens the current file location")); |
| 3809 |
#endif |
| 3810 |
|
| 3811 |
this->reloadDocAction->setText(tr("Reload Document")); |
| 3812 |
this->reloadDocAction->setStatusTip(tr("Reloads a partially loaded document")); |
| 3813 |
|
| 3814 |
this->skipRecomputeAction->setText(tr("Skip Recomputes")); |
| 3815 |
this->skipRecomputeAction->setStatusTip(tr("Enables or disables the recomputations of document")); |
| 3816 |
|
| 3817 |
this->allowPartialRecomputeAction->setText(tr("Allow Partial Recomputes")); |
| 3818 |
this->allowPartialRecomputeAction->setStatusTip(tr( |
| 3819 |
"Enables or disables the recomputating editing object when 'skip recomputation' is enabled" |
| 3820 |
)); |
| 3821 |
|
| 3822 |
this->markRecomputeAction->setText(tr("Mark to Recompute")); |
| 3823 |
this->markRecomputeAction->setStatusTip(tr("Marks this object to be recomputed")); |
| 3824 |
this->markRecomputeAction->setIcon(BitmapFactory().iconFromTheme("Std_MarkToRecompute")); |
| 3825 |
|
| 3826 |
this->recomputeObjectAction->setText(tr("Recompute Object")); |
| 3827 |
this->recomputeObjectAction->setStatusTip(tr("Recomputes the selected object")); |
| 3828 |
this->recomputeObjectAction->setIcon(BitmapFactory().iconFromTheme("view-refresh")); |
| 3829 |
} |
| 3830 |
|
| 3831 |
void TreeWidget::syncView(ViewProviderDocumentObject* vp) |
| 3832 |
{ |
| 3833 |
if (currentDocItem && TreeParams::getSyncView()) { |
| 3834 |
bool focus = hasFocus(); |
| 3835 |
currentDocItem->document()->setActiveView(vp); |
| 3836 |
if (focus) { |
| 3837 |
setFocus(); |
| 3838 |
} |
| 3839 |
} |
| 3840 |
} |
| 3841 |
|
| 3842 |
void TreeWidget::onShowHidden() |
| 3843 |
{ |
| 3844 |
if (!this->contextItem) { |
| 3845 |
return; |
| 3846 |
} |
| 3847 |
DocumentItem* docItem = nullptr; |
| 3848 |
if (this->contextItem->type() == DocumentType) { |
| 3849 |
docItem = static_cast<DocumentItem*>(contextItem); |
| 3850 |
} |
| 3851 |
else if (this->contextItem->type() == ObjectType) { |
| 3852 |
docItem = static_cast<DocumentObjectItem*>(contextItem)->getOwnerDocument(); |
| 3853 |
} |
| 3854 |
if (docItem) { |
| 3855 |
docItem->setShowHidden(showHiddenAction->isChecked()); |
| 3856 |
} |
| 3857 |
} |
| 3858 |
|
| 3859 |
void TreeWidget::onToggleVisibilityInTree() |
| 3860 |
{ |
| 3861 |
const auto items = selectedItems(); |
| 3862 |
for (auto item : items) { |
| 3863 |
if (item->type() == ObjectType) { |
| 3864 |
auto objectItem = static_cast<DocumentObjectItem*>(item); |
| 3865 |
auto object = objectItem->object(); |
| 3866 |
|
| 3867 |
// toggle value |
| 3868 |
bool showInTree = !object->showInTree(); |
| 3869 |
|
| 3870 |
// update object |
| 3871 |
object->ShowInTree.setValue(showInTree); |
| 3872 |
|
| 3873 |
// update GUI |
| 3874 |
auto ownerDocument = objectItem->getOwnerDocument(); |
| 3875 |
bool hidden = !ownerDocument->showHidden() && !showInTree; |
| 3876 |
objectItem->setHidden(hidden); |
| 3877 |
if (hidden) { |
| 3878 |
objectItem->setSelected(false); |
| 3879 |
} |
| 3880 |
} |
| 3881 |
} |
| 3882 |
} |
| 3883 |
|
| 3884 |
void TreeWidget::changeEvent(QEvent* e) |
| 3885 |
{ |
| 3886 |
if (e->type() == QEvent::LanguageChange) { |
| 3887 |
setupText(); |
| 3888 |
} |
| 3889 |
|
| 3890 |
QTreeWidget::changeEvent(e); |
| 3891 |
} |
| 3892 |
|
| 3893 |
void TreeWidget::onItemSelectionChanged() |
| 3894 |
{ |
| 3895 |
if (!this->isSelectionAttached() || this->isSelectionBlocked() || updateBlocked) { |
| 3896 |
return; |
| 3897 |
} |
| 3898 |
|
| 3899 |
_LastSelectedTreeWidget = this; |
| 3900 |
|
| 3901 |
// block tmp. the connection to avoid to notify us ourself |
| 3902 |
bool lock = this->blockSelection(true); |
| 3903 |
|
| 3904 |
if (preselectTimer->isActive()) { |
| 3905 |
// block preselect after selecting |
| 3906 |
preselectTimer->stop(); |
| 3907 |
} |
| 3908 |
|
| 3909 |
if (selectTimer->isActive()) { |
| 3910 |
onSelectTimer(); |
| 3911 |
} |
| 3912 |
else { |
| 3913 |
_updateStatus(false); |
| 3914 |
} |
| 3915 |
|
| 3916 |
auto selItems = selectedItems(); |
| 3917 |
|
| 3918 |
// do not allow document item multi-selection |
| 3919 |
if (!selItems.empty()) { |
| 3920 |
auto firstType = selItems.back()->type(); |
| 3921 |
for (auto it = selItems.begin(); it != selItems.end();) { |
| 3922 |
auto item = *it; |
| 3923 |
if ((firstType == ObjectType && item->type() != ObjectType) |
| 3924 |
|| (firstType == DocumentType && item != selItems.back())) { |
| 3925 |
item->setSelected(false); |
| 3926 |
it = selItems.erase(it); |
| 3927 |
} |
| 3928 |
else { |
| 3929 |
++it; |
| 3930 |
} |
| 3931 |
} |
| 3932 |
} |
| 3933 |
|
| 3934 |
if (selItems.size() <= 1) { |
| 3935 |
if (TreeParams::getRecordSelection()) { |
| 3936 |
Gui::Selection().selStackPush(); |
| 3937 |
} |
| 3938 |
|
| 3939 |
// This special handling to deal with possible discrepancy of |
| 3940 |
// Gui.Selection and Tree view selection because of newly added |
| 3941 |
// DocumentObject::redirectSubName() |
| 3942 |
Selection().clearCompleteSelection(); |
| 3943 |
DocumentObjectItem* item = nullptr; |
| 3944 |
if (!selItems.empty()) { |
| 3945 |
if (selItems.front()->type() == ObjectType) { |
| 3946 |
item = static_cast<DocumentObjectItem*>(selItems.front()); |
| 3947 |
} |
| 3948 |
else if (selItems.front()->type() == DocumentType) { |
| 3949 |
auto ditem = static_cast<DocumentItem*>(selItems.front()); |
| 3950 |
if (TreeParams::getSyncView()) { |
| 3951 |
bool focus = hasFocus(); |
| 3952 |
ditem->document()->setActiveView(); |
| 3953 |
if (focus) { |
| 3954 |
setFocus(); |
| 3955 |
} |
| 3956 |
} |
| 3957 |
// For triggering property editor refresh |
| 3958 |
Gui::Selection().signalSelectionChanged(SelectionChanges()); |
| 3959 |
} |
| 3960 |
} |
| 3961 |
for (auto& v : DocumentMap) { |
| 3962 |
currentDocItem = v.second; |
| 3963 |
v.second->clearSelection(item); |
| 3964 |
currentDocItem = nullptr; |
| 3965 |
} |
| 3966 |
if (TreeParams::getRecordSelection()) { |
| 3967 |
Gui::Selection().selStackPush(); |
| 3968 |
} |
| 3969 |
} |
| 3970 |
else { |
| 3971 |
for (auto pos = DocumentMap.begin(); pos != DocumentMap.end(); ++pos) { |
| 3972 |
currentDocItem = pos->second; |
| 3973 |
pos->second->updateSelection(pos->second); |
| 3974 |
currentDocItem = nullptr; |
| 3975 |
} |
| 3976 |
if (TreeParams::getRecordSelection()) { |
| 3977 |
Gui::Selection().selStackPush(true, true); |
| 3978 |
} |
| 3979 |
} |
| 3980 |
|
| 3981 |
this->blockSelection(lock); |
| 3982 |
} |
| 3983 |
|
| 3984 |
void TreeWidget::synchronizeSelectionCheckBoxes() |
| 3985 |
{ |
| 3986 |
const bool useCheckBoxes = isSelectionCheckBoxesEnabled(); |
| 3987 |
for (auto tree : TreeWidget::Instances) { |
| 3988 |
QSignalBlocker blocker(tree); |
| 3989 |
for (QTreeWidgetItemIterator it(tree); *it; ++it) { |
| 3990 |
auto item = *it; |
| 3991 |
if (item->type() == ObjectType) { |
| 3992 |
if (useCheckBoxes) { |
| 3993 |
item->setCheckState(0, item->isSelected() ? Qt::Checked : Qt::Unchecked); |
| 3994 |
} |
| 3995 |
else { |
| 3996 |
item->setData(0, Qt::CheckStateRole, QVariant()); |
| 3997 |
} |
| 3998 |
} |
| 3999 |
} |
| 4000 |
tree->resizeColumnToContents(0); |
| 4001 |
} |
| 4002 |
} |
| 4003 |
|
| 4004 |
void TreeWidget::updateVisibilityIcons() |
| 4005 |
{ |
| 4006 |
for (auto tree : TreeWidget::Instances) { |
| 4007 |
QSignalBlocker blocker(tree); |
| 4008 |
for (QTreeWidgetItemIterator it(tree); *it; ++it) { |
| 4009 |
auto item = *it; |
| 4010 |
if (item->type() == ObjectType) { |
| 4011 |
auto objitem = static_cast<DocumentObjectItem*>(item); |
| 4012 |
objitem->testStatus(true); |
| 4013 |
} |
| 4014 |
} |
| 4015 |
tree->resizeColumnToContents(0); |
| 4016 |
} |
| 4017 |
} |
| 4018 |
|
| 4019 |
QList<QTreeWidgetItem*> TreeWidget::childrenOfItem(const QTreeWidgetItem& item) const |
| 4020 |
{ |
| 4021 |
QList children = QList<QTreeWidgetItem*>(); |
| 4022 |
|
| 4023 |
// check item is in this tree |
| 4024 |
if (!this->indexFromItem(&item).isValid()) { |
| 4025 |
return children; |
| 4026 |
} |
| 4027 |
|
| 4028 |
for (int i = 0; i < item.childCount(); i++) { |
| 4029 |
children.append(item.child(i)); |
| 4030 |
} |
| 4031 |
return children; |
| 4032 |
} |
| 4033 |
|
| 4034 |
void TreeWidget::onItemChanged(QTreeWidgetItem* item, int column) |
| 4035 |
{ |
| 4036 |
if (column == 0 && isSelectionCheckBoxesEnabled()) { |
| 4037 |
bool selected = item->isSelected(); |
| 4038 |
bool checked = item->checkState(0) == Qt::Checked; |
| 4039 |
if (checked != selected) { |
| 4040 |
item->setSelected(checked); |
| 4041 |
} |
| 4042 |
} |
| 4043 |
} |
| 4044 |
|
| 4045 |
void TreeWidget::onSelectTimer() |
| 4046 |
{ |
| 4047 |
|
| 4048 |
_updateStatus(false); |
| 4049 |
|
| 4050 |
bool syncSelect = TreeParams::getSyncSelection(); |
| 4051 |
bool locked = this->blockSelection(true); |
| 4052 |
if (Selection().hasSelection()) { |
| 4053 |
for (auto& v : DocumentMap) { |
| 4054 |
v.second->setSelected(false); |
| 4055 |
currentDocItem = v.second; |
| 4056 |
v.second->selectItems(syncSelect ? DocumentItem::SR_EXPAND : DocumentItem::SR_SELECT); |
| 4057 |
currentDocItem = nullptr; |
| 4058 |
} |
| 4059 |
} |
| 4060 |
else { |
| 4061 |
for (auto& v : DocumentMap) { |
| 4062 |
v.second->clearSelection(); |
| 4063 |
} |
| 4064 |
} |
| 4065 |
this->blockSelection(locked); |
| 4066 |
selectTimer->stop(); |
| 4067 |
return; |
| 4068 |
} |
| 4069 |
|
| 4070 |
void TreeWidget::onSelectionChanged(const SelectionChanges& msg) |
| 4071 |
{ |
| 4072 |
// When running from a different thread Qt will raise a warning |
| 4073 |
// when trying to start the QTimer |
| 4074 |
if (Q_UNLIKELY(thread() != QThread::currentThread())) { |
| 4075 |
return; |
| 4076 |
} |
| 4077 |
|
| 4078 |
switch (msg.Type) { |
| 4079 |
case SelectionChanges::AddSelection: |
| 4080 |
case SelectionChanges::RmvSelection: |
| 4081 |
case SelectionChanges::SetSelection: |
| 4082 |
case SelectionChanges::ClrSelection: { |
| 4083 |
int timeout = TreeParams::getSelectionTimeout(); |
| 4084 |
if (timeout <= 0) { |
| 4085 |
timeout = 1; |
| 4086 |
} |
| 4087 |
selectTimer->start(timeout); |
| 4088 |
break; |
| 4089 |
} |
| 4090 |
default: |
| 4091 |
break; |
| 4092 |
} |
| 4093 |
} |
| 4094 |
|
| 4095 |
// ---------------------------------------------------------------------------- |
| 4096 |
|
| 4097 |
/* TRANSLATOR Gui::TreePanel */ |
| 4098 |
|
| 4099 |
TreePanel::TreePanel(const char* name, QWidget* parent) |
| 4100 |
: QWidget(parent) |
| 4101 |
{ |
| 4102 |
this->treeWidget = new TreeWidget(name, this); |
| 4103 |
int indent = TreeParams::getIndentation(); |
| 4104 |
if (indent) { |
| 4105 |
this->treeWidget->setIndentation(indent); |
| 4106 |
} |
| 4107 |
|
| 4108 |
auto pLayout = new QVBoxLayout(this); |
| 4109 |
pLayout->setSpacing(0); |
| 4110 |
pLayout->setContentsMargins(0, 0, 0, 0); |
| 4111 |
pLayout->addWidget(this->treeWidget); |
| 4112 |
connect(this->treeWidget, &TreeWidget::emitSearchObjects, this, &TreePanel::showEditor); |
| 4113 |
|
| 4114 |
this->searchBox = new Gui::ExpressionLineEdit(this, true); |
| 4115 |
static_cast<ExpressionLineEdit*>(this->searchBox) |
| 4116 |
->setExactMatch(Gui::ExpressionParameter::instance()->isExactMatch()); |
| 4117 |
pLayout->addWidget(this->searchBox); |
| 4118 |
this->searchBox->hide(); |
| 4119 |
this->searchBox->installEventFilter(this); |
| 4120 |
this->searchBox->setPlaceholderText(tr("Search")); |
| 4121 |
connect(this->searchBox, &QLineEdit::returnPressed, this, &TreePanel::accept); |
| 4122 |
connect(this->searchBox, &QLineEdit::textChanged, this, &TreePanel::itemSearch); |
| 4123 |
} |
| 4124 |
|
| 4125 |
TreePanel::~TreePanel() = default; |
| 4126 |
|
| 4127 |
void TreePanel::accept() |
| 4128 |
{ |
| 4129 |
QString text = this->searchBox->text(); |
| 4130 |
hideEditor(); |
| 4131 |
this->treeWidget->setFocus(); |
| 4132 |
this->treeWidget->itemSearch(text, true); |
| 4133 |
} |
| 4134 |
|
| 4135 |
bool TreePanel::eventFilter(QObject* obj, QEvent* ev) |
| 4136 |
{ |
| 4137 |
if (obj != this->searchBox) { |
| 4138 |
return false; |
| 4139 |
} |
| 4140 |
|
| 4141 |
if (ev->type() == QEvent::KeyPress) { |
| 4142 |
bool consumed = false; |
| 4143 |
int key = static_cast<QKeyEvent*>(ev)->key(); |
| 4144 |
switch (key) { |
| 4145 |
case Qt::Key_Escape: |
| 4146 |
hideEditor(); |
| 4147 |
consumed = true; |
| 4148 |
treeWidget->setFocus(); |
| 4149 |
break; |
| 4150 |
|
| 4151 |
default: |
| 4152 |
break; |
| 4153 |
} |
| 4154 |
|
| 4155 |
return consumed; |
| 4156 |
} |
| 4157 |
|
| 4158 |
return false; |
| 4159 |
} |
| 4160 |
|
| 4161 |
void TreePanel::showEditor() |
| 4162 |
{ |
| 4163 |
this->searchBox->show(); |
| 4164 |
this->searchBox->setFocus(); |
| 4165 |
this->treeWidget->startItemSearch(searchBox); |
| 4166 |
} |
| 4167 |
|
| 4168 |
void TreePanel::hideEditor() |
| 4169 |
{ |
| 4170 |
static_cast<ExpressionLineEdit*>(this->searchBox)->setDocumentObject(nullptr); |
| 4171 |
this->searchBox->clear(); |
| 4172 |
this->searchBox->hide(); |
| 4173 |
this->treeWidget->resetItemSearch(); |
| 4174 |
auto sels = this->treeWidget->selectedItems(); |
| 4175 |
if (!sels.empty()) { |
| 4176 |
this->treeWidget->scrollToItem(sels.front()); |
| 4177 |
} |
| 4178 |
} |
| 4179 |
|
| 4180 |
void TreePanel::itemSearch(const QString& text) |
| 4181 |
{ |
| 4182 |
this->treeWidget->itemSearch(text, false); |
| 4183 |
} |
| 4184 |
|
| 4185 |
// ---------------------------------------------------------------------------- |
| 4186 |
|
| 4187 |
/* TRANSLATOR Gui::TreeDockWidget */ |
| 4188 |
|
| 4189 |
TreeDockWidget::TreeDockWidget(Gui::Document* pcDocument, QWidget* parent) |
| 4190 |
: DockWindow(pcDocument, parent) |
| 4191 |
{ |
| 4192 |
setWindowTitle(tr("Tree View")); |
| 4193 |
auto panel = new TreePanel("TreeView", this); |
| 4194 |
auto pLayout = new QGridLayout(this); |
| 4195 |
pLayout->setSpacing(0); |
| 4196 |
pLayout->setContentsMargins(0, 0, 0, 0); |
| 4197 |
pLayout->addWidget(panel, 0, 0); |
| 4198 |
} |
| 4199 |
|
| 4200 |
TreeDockWidget::~TreeDockWidget() = default; |
| 4201 |
|
| 4202 |
void TreeWidget::selectLinkedObject(App::DocumentObject* linked) |
| 4203 |
{ |
| 4204 |
if (!isSelectionAttached() || isSelectionBlocked()) { |
| 4205 |
return; |
| 4206 |
} |
| 4207 |
|
| 4208 |
auto linkedVp = freecad_cast<ViewProviderDocumentObject*>( |
| 4209 |
Application::Instance->getViewProvider(linked) |
| 4210 |
); |
| 4211 |
if (!linkedVp) { |
| 4212 |
TREE_ERR("invalid linked view provider"); |
| 4213 |
return; |
| 4214 |
} |
| 4215 |
auto linkedDoc = getDocumentItem(linkedVp->getDocument()); |
| 4216 |
if (!linkedDoc) { |
| 4217 |
TREE_ERR("cannot find document of linked object"); |
| 4218 |
return; |
| 4219 |
} |
| 4220 |
|
| 4221 |
if (selectTimer->isActive()) { |
| 4222 |
onSelectTimer(); |
| 4223 |
} |
| 4224 |
else { |
| 4225 |
_updateStatus(false); |
| 4226 |
} |
| 4227 |
|
| 4228 |
auto it = linkedDoc->ObjectMap.find(linked); |
| 4229 |
if (it == linkedDoc->ObjectMap.end()) { |
| 4230 |
TREE_ERR("cannot find tree item of linked object"); |
| 4231 |
return; |
| 4232 |
} |
| 4233 |
auto linkedItem = it->second->rootItem; |
| 4234 |
if (!linkedItem) { |
| 4235 |
linkedItem = *it->second->items.begin(); |
| 4236 |
} |
| 4237 |
|
| 4238 |
if (linkedDoc->showItem(linkedItem, true)) { |
| 4239 |
scrollToItem(linkedItem); |
| 4240 |
} |
| 4241 |
|
| 4242 |
if (linkedDoc->document()->getDocument() != App::GetApplication().getActiveDocument()) { |
| 4243 |
bool focus = hasFocus(); |
| 4244 |
linkedDoc->document()->setActiveView(linkedItem->object()); |
| 4245 |
if (focus) { |
| 4246 |
setFocus(); |
| 4247 |
} |
| 4248 |
} |
| 4249 |
} |
| 4250 |
|
| 4251 |
// ---------------------------------------------------------------------------- |
| 4252 |
|
| 4253 |
DocumentItem::DocumentItem(const Gui::Document* doc, QTreeWidgetItem* parent) |
| 4254 |
: QTreeWidgetItem(parent, TreeWidget::DocumentType) |
| 4255 |
, pDocument(const_cast<Gui::Document*>(doc)) |
| 4256 |
{ |
| 4257 |
// NOLINTBEGIN |
| 4258 |
// Setup connections |
| 4259 |
connectNewObject = doc->signalNewObject.connect( |
| 4260 |
std::bind(&DocumentItem::slotNewObject, this, sp::_1) |
| 4261 |
); |
| 4262 |
connectDelObject = doc->signalDeletedObject.connect( |
| 4263 |
std::bind(&TreeWidget::slotDeleteObject, getTree(), sp::_1) |
| 4264 |
); |
| 4265 |
if (!App::GetApplication().isRestoring()) { |
| 4266 |
connectChgObject = doc->signalChangedObject.connect( |
| 4267 |
std::bind(&TreeWidget::slotChangeObject, getTree(), sp::_1, sp::_2) |
| 4268 |
); |
| 4269 |
connectTouchedObject = doc->getDocument()->signalTouchedObject.connect( |
| 4270 |
std::bind(&TreeWidget::slotTouchedObject, getTree(), sp::_1) |
| 4271 |
); |
| 4272 |
} |
| 4273 |
connectEdtObject = doc->signalInEdit.connect(std::bind(&DocumentItem::slotInEdit, this, sp::_1)); |
| 4274 |
connectResObject = doc->signalResetEdit.connect( |
| 4275 |
std::bind(&DocumentItem::slotResetEdit, this, sp::_1) |
| 4276 |
); |
| 4277 |
connectHltObject = doc->signalHighlightObject.connect( |
| 4278 |
std::bind(&DocumentItem::slotHighlightObject, this, sp::_1, sp::_2, sp::_3, sp::_4, sp::_5) |
| 4279 |
); |
| 4280 |
connectExpObject = doc->signalExpandObject.connect( |
| 4281 |
std::bind(&DocumentItem::slotExpandObject, this, sp::_1, sp::_2, sp::_3, sp::_4) |
| 4282 |
); |
| 4283 |
connectScrObject = doc->signalScrollToObject.connect( |
| 4284 |
std::bind(&DocumentItem::slotScrollToObject, this, sp::_1) |
| 4285 |
); |
| 4286 |
auto adoc = doc->getDocument(); |
| 4287 |
connectRecomputed = adoc->signalRecomputed.connect( |
| 4288 |
std::bind(&DocumentItem::slotRecomputed, this, sp::_1, sp::_2) |
| 4289 |
); |
| 4290 |
connectRecomputedObj = adoc->signalRecomputedObject.connect( |
| 4291 |
std::bind(&DocumentItem::slotRecomputedObject, this, sp::_1) |
| 4292 |
); |
| 4293 |
// NOLINTEND |
| 4294 |
|
| 4295 |
setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable /*|Qt::ItemIsEditable*/); |
| 4296 |
|
| 4297 |
treeName = getTree()->getTreeName(); |
| 4298 |
} |
| 4299 |
|
| 4300 |
DocumentItem::~DocumentItem() |
| 4301 |
{ |
| 4302 |
connectNewObject.disconnect(); |
| 4303 |
connectDelObject.disconnect(); |
| 4304 |
connectChgObject.disconnect(); |
| 4305 |
connectTouchedObject.disconnect(); |
| 4306 |
connectEdtObject.disconnect(); |
| 4307 |
connectResObject.disconnect(); |
| 4308 |
connectHltObject.disconnect(); |
| 4309 |
connectExpObject.disconnect(); |
| 4310 |
connectScrObject.disconnect(); |
| 4311 |
connectRecomputed.disconnect(); |
| 4312 |
connectRecomputedObj.disconnect(); |
| 4313 |
} |
| 4314 |
|
| 4315 |
TreeWidget* DocumentItem::getTree() const |
| 4316 |
{ |
| 4317 |
return static_cast<TreeWidget*>(treeWidget()); |
| 4318 |
} |
| 4319 |
|
| 4320 |
const char* DocumentItem::getTreeName() const |
| 4321 |
{ |
| 4322 |
return treeName; |
| 4323 |
} |
| 4324 |
|
| 4325 |
#define FOREACH_ITEM(_item, _obj) \ |
| 4326 |
auto _it = ObjectMap.end(); \ |
| 4327 |
if (_obj.getObject() && _obj.getObject()->isAttachedToDocument()) \ |
| 4328 |
_it = ObjectMap.find(_obj.getObject()); \ |
| 4329 |
if (_it != ObjectMap.end()) { \ |
| 4330 |
for (auto _item : _it->second->items) { |
| 4331 |
|
| 4332 |
#define FOREACH_ITEM_ALL(_item) \ |
| 4333 |
for (const auto& _v : ObjectMap) { \ |
| 4334 |
for (auto _item : _v.second->items) { |
| 4335 |
|
| 4336 |
#define END_FOREACH_ITEM \ |
| 4337 |
} \ |
| 4338 |
} |
| 4339 |
|
| 4340 |
|
| 4341 |
void DocumentItem::slotInEdit(const Gui::ViewProviderDocumentObject& v) |
| 4342 |
{ |
| 4343 |
(void)v; |
| 4344 |
|
| 4345 |
ParameterGrp::handle hGrp = App::GetApplication().GetParameterGroupByPath( |
| 4346 |
"User parameter:BaseApp/Preferences/TreeView" |
| 4347 |
); |
| 4348 |
unsigned long col = hGrp->GetUnsigned("TreeEditColor", 563609599); |
| 4349 |
QColor color(Base::Color::fromPackedRGB<QColor>(col)); |
| 4350 |
|
| 4351 |
if (!getTree()->editingItem) { |
| 4352 |
auto doc = Application::Instance->editDocument(); |
| 4353 |
if (!doc) { |
| 4354 |
return; |
| 4355 |
} |
| 4356 |
ViewProviderDocumentObject* parentVp = nullptr; |
| 4357 |
std::string subname; |
| 4358 |
auto vp = doc->getInEdit(&parentVp, &subname); |
| 4359 |
if (!parentVp) { |
| 4360 |
parentVp = freecad_cast<ViewProviderDocumentObject*>(vp); |
| 4361 |
} |
| 4362 |
if (parentVp) { |
| 4363 |
getTree()->editingItem = findItemByObject(true, parentVp->getObject(), subname.c_str()); |
| 4364 |
} |
| 4365 |
} |
| 4366 |
|
| 4367 |
if (getTree()->editingItem) { |
| 4368 |
getTree()->editingItem->setBackground(0, color); |
| 4369 |
} |
| 4370 |
else { |
| 4371 |
FOREACH_ITEM(item, v) |
| 4372 |
item->setBackground(0, color); |
| 4373 |
END_FOREACH_ITEM |
| 4374 |
} |
| 4375 |
} |
| 4376 |
|
| 4377 |
void DocumentItem::slotResetEdit(const Gui::ViewProviderDocumentObject& v) |
| 4378 |
{ |
| 4379 |
auto tree = getTree(); |
| 4380 |
FOREACH_ITEM_ALL(item) |
| 4381 |
if (tree->editingItem) { |
| 4382 |
if (item == tree->editingItem) { |
| 4383 |
item->setData(0, Qt::BackgroundRole, QVariant()); |
| 4384 |
break; |
| 4385 |
} |
| 4386 |
} |
| 4387 |
else if (item->object() == &v) { |
| 4388 |
item->setData(0, Qt::BackgroundRole, QVariant()); |
| 4389 |
} |
| 4390 |
END_FOREACH_ITEM |
| 4391 |
tree->editingItem = nullptr; |
| 4392 |
} |
| 4393 |
|
| 4394 |
void DocumentItem::slotNewObject(const Gui::ViewProviderDocumentObject& obj) |
| 4395 |
{ |
| 4396 |
if (!obj.getObject() || !obj.getObject()->isAttachedToDocument()) { |
| 4397 |
FC_ERR("view provider not attached"); |
| 4398 |
return; |
| 4399 |
} |
| 4400 |
getTree()->NewObjects[pDocument->getDocument()->getName()].push_back(obj.getObject()->getID()); |
| 4401 |
getTree()->_updateStatus(); |
| 4402 |
} |
| 4403 |
|
| 4404 |
bool DocumentItem::createNewItem( |
| 4405 |
const Gui::ViewProviderDocumentObject& obj, |
| 4406 |
QTreeWidgetItem* parent, |
| 4407 |
int index, |
| 4408 |
DocumentObjectDataPtr data |
| 4409 |
) |
| 4410 |
{ |
| 4411 |
if (!obj.getObject() || !obj.getObject()->isAttachedToDocument() |
| 4412 |
|| obj.getObject()->testStatus(App::PartialObject)) { |
| 4413 |
return false; |
| 4414 |
} |
| 4415 |
|
| 4416 |
if (!data) { |
| 4417 |
auto& pdata = ObjectMap[obj.getObject()]; |
| 4418 |
if (!pdata) { |
| 4419 |
pdata = std::make_shared<DocumentObjectData>( |
| 4420 |
this, |
| 4421 |
const_cast<ViewProviderDocumentObject*>(&obj) |
| 4422 |
); |
| 4423 |
auto& entry = getTree()->ObjectTable[obj.getObject()]; |
| 4424 |
if (!entry.empty()) { |
| 4425 |
pdata->updateChildren(*entry.begin()); |
| 4426 |
} |
| 4427 |
else { |
| 4428 |
pdata->updateChildren(true); |
| 4429 |
} |
| 4430 |
entry.insert(pdata); |
| 4431 |
} |
| 4432 |
else if (pdata->rootItem && !parent) { |
| 4433 |
Base::Console().warning("DocumentItem::slotNewObject: Cannot add view provider twice.\n"); |
| 4434 |
return false; |
| 4435 |
} |
| 4436 |
data = pdata; |
| 4437 |
} |
| 4438 |
|
| 4439 |
auto item = new DocumentObjectItem(this, data); |
| 4440 |
if (!parent || parent == this) { |
| 4441 |
parent = this; |
| 4442 |
data->rootItem = item; |
| 4443 |
if (index < 0) { |
| 4444 |
index = findRootIndex(obj.getObject()); |
| 4445 |
} |
| 4446 |
} |
| 4447 |
if (index < 0) { |
| 4448 |
parent->addChild(item); |
| 4449 |
} |
| 4450 |
else { |
| 4451 |
parent->insertChild(index, item); |
| 4452 |
} |
| 4453 |
assert(item->parent() == parent); |
| 4454 |
item->setText(0, QString::fromUtf8(data->label.c_str())); |
| 4455 |
if (!data->label2.empty()) { |
| 4456 |
item->setText(1, QString::fromUtf8(data->label2.c_str())); |
| 4457 |
} |
| 4458 |
item->setText(2, QString::fromUtf8(data->internalName.c_str())); |
| 4459 |
if (!obj.showInTree() && !showHidden()) { |
| 4460 |
item->setHidden(true); |
| 4461 |
} |
| 4462 |
item->testStatus(true); |
| 4463 |
|
| 4464 |
populateItem(item); |
| 4465 |
return true; |
| 4466 |
} |
| 4467 |
|
| 4468 |
ViewProviderDocumentObject* DocumentItem::getViewProvider(App::DocumentObject* obj) |
| 4469 |
{ |
| 4470 |
return freecad_cast<ViewProviderDocumentObject*>(Application::Instance->getViewProvider(obj)); |
| 4471 |
} |
| 4472 |
|
| 4473 |
void TreeWidget::slotDeleteDocument(const Gui::Document& Doc) |
| 4474 |
{ |
| 4475 |
NewObjects.erase(Doc.getDocument()->getName()); |
| 4476 |
auto it = DocumentMap.find(&Doc); |
| 4477 |
if (it != DocumentMap.end()) { |
| 4478 |
UpdateDisabler disabler(*this, updateBlocked); |
| 4479 |
auto docItem = it->second; |
| 4480 |
for (auto& v : docItem->ObjectMap) { |
| 4481 |
for (auto item : v.second->items) { |
| 4482 |
item->myOwner = nullptr; |
| 4483 |
} |
| 4484 |
auto obj = v.second->viewObject->getObject(); |
| 4485 |
if (obj->getDocument() == Doc.getDocument()) { |
| 4486 |
_slotDeleteObject(*v.second->viewObject, docItem); |
| 4487 |
continue; |
| 4488 |
} |
| 4489 |
auto it = ObjectTable.find(obj); |
| 4490 |
assert(it != ObjectTable.end()); |
| 4491 |
assert(it->second.size() > 1); |
| 4492 |
it->second.erase(v.second); |
| 4493 |
} |
| 4494 |
this->rootItem->takeChild(this->rootItem->indexOfChild(docItem)); |
| 4495 |
delete docItem; |
| 4496 |
DocumentMap.erase(it); |
| 4497 |
} |
| 4498 |
} |
| 4499 |
|
| 4500 |
void TreeWidget::slotDeleteObject(const Gui::ViewProviderDocumentObject& view) |
| 4501 |
{ |
| 4502 |
_slotDeleteObject(view, nullptr); |
| 4503 |
} |
| 4504 |
|
| 4505 |
void TreeWidget::_slotDeleteObject(const Gui::ViewProviderDocumentObject& view, DocumentItem* deletingDoc) |
| 4506 |
{ |
| 4507 |
auto obj = view.getObject(); |
| 4508 |
auto itEntry = ObjectTable.find(obj); |
| 4509 |
if (itEntry == ObjectTable.end()) { |
| 4510 |
return; |
| 4511 |
} |
| 4512 |
|
| 4513 |
if (itEntry->second.empty()) { |
| 4514 |
ObjectTable.erase(itEntry); |
| 4515 |
return; |
| 4516 |
} |
| 4517 |
|
| 4518 |
TREE_LOG("delete object " << obj->getFullName()); |
| 4519 |
|
| 4520 |
// Block all selection signals during deletion to prevent cascading selection change events |
| 4521 |
// during item creation or deletion |
| 4522 |
bool lock = blockSelection(true); |
| 4523 |
bool needUpdate = false; |
| 4524 |
bool hadFocus = (QApplication::focusWidget() == this); |
| 4525 |
|
| 4526 |
for (const auto& data : itEntry->second) { |
| 4527 |
DocumentItem* docItem = data->docItem; |
| 4528 |
if (docItem == deletingDoc) { |
| 4529 |
continue; |
| 4530 |
} |
| 4531 |
|
| 4532 |
auto doc = docItem->document()->getDocument(); |
| 4533 |
auto& items = data->items; |
| 4534 |
|
| 4535 |
if (obj->getDocument() == doc) { |
| 4536 |
docItem->_ParentMap.erase(obj); |
| 4537 |
} |
| 4538 |
|
| 4539 |
for (auto cit = items.begin(), citNext = cit; cit != items.end(); cit = citNext) { |
| 4540 |
++citNext; |
| 4541 |
DocumentObjectItem* itemToDelete = *cit; |
| 4542 |
itemToDelete->myOwner = nullptr; |
| 4543 |
delete itemToDelete; |
| 4544 |
} |
| 4545 |
|
| 4546 |
// Check for any child of the deleted object that is not in the tree, and put it |
| 4547 |
// under document item. |
| 4548 |
for (auto child : data->children) { |
| 4549 |
auto childVp = docItem->getViewProvider(child); |
| 4550 |
if (!childVp || child->getDocument() != doc) { |
| 4551 |
continue; |
| 4552 |
} |
| 4553 |
docItem->_ParentMap[child].erase(obj); |
| 4554 |
auto cit = docItem->ObjectMap.find(child); |
| 4555 |
if (cit == docItem->ObjectMap.end() || cit->second->items.empty()) { |
| 4556 |
if (docItem->createNewItem(*childVp)) { |
| 4557 |
needUpdate = true; |
| 4558 |
} |
| 4559 |
} |
| 4560 |
else { |
| 4561 |
auto childItem = *cit->second->items.begin(); |
| 4562 |
if (childItem->requiredAtRoot(false)) { |
| 4563 |
if (docItem->createNewItem(*childItem->object(), docItem, -1, childItem->myData)) { |
| 4564 |
needUpdate = true; |
| 4565 |
} |
| 4566 |
} |
| 4567 |
} |
| 4568 |
childVp->setShowable(docItem->isObjectShowable(child)); |
| 4569 |
} |
| 4570 |
docItem->ObjectMap.erase(obj); |
| 4571 |
} |
| 4572 |
ObjectTable.erase(itEntry); |
| 4573 |
|
| 4574 |
// Restore signal state |
| 4575 |
blockSelection(lock); |
| 4576 |
|
| 4577 |
// restore focus to the tree widget if it had focus before deletion |
| 4578 |
if (hadFocus) { |
| 4579 |
QTimer::singleShot(0, this, [this]() { |
| 4580 |
setFocus(Qt::OtherFocusReason); |
| 4581 |
activateWindow(); |
| 4582 |
}); |
| 4583 |
} |
| 4584 |
|
| 4585 |
if (needUpdate) { |
| 4586 |
_updateStatus(); |
| 4587 |
} |
| 4588 |
} |
| 4589 |
|
| 4590 |
bool DocumentItem::populateObject(App::DocumentObject* obj) |
| 4591 |
{ |
| 4592 |
// make sure at least one of the item corresponding to obj is populated |
| 4593 |
auto it = ObjectMap.find(obj); |
| 4594 |
if (it == ObjectMap.end()) { |
| 4595 |
return false; |
| 4596 |
} |
| 4597 |
auto& items = it->second->items; |
| 4598 |
if (items.empty()) { |
| 4599 |
return false; |
| 4600 |
} |
| 4601 |
for (auto item : items) { |
| 4602 |
if (item->populated) { |
| 4603 |
return true; |
| 4604 |
} |
| 4605 |
} |
| 4606 |
TREE_LOG("force populate object " << obj->getFullName()); |
| 4607 |
auto item = *items.begin(); |
| 4608 |
item->populated = true; |
| 4609 |
populateItem(item, true); |
| 4610 |
return true; |
| 4611 |
} |
| 4612 |
|
| 4613 |
void DocumentItem::populateItem(DocumentObjectItem* item, bool refresh, bool delay) |
| 4614 |
{ |
| 4615 |
(void)delay; |
| 4616 |
|
| 4617 |
if (item->populated && !refresh) { |
| 4618 |
return; |
| 4619 |
} |
| 4620 |
|
| 4621 |
// Lazy loading policy: We will create an item for each children object if |
| 4622 |
// a) the item is expanded, or b) there is at least one free child, i.e. |
| 4623 |
// child originally located at root. |
| 4624 |
|
| 4625 |
item->setChildIndicatorPolicy( |
| 4626 |
item->myData->children.empty() ? QTreeWidgetItem::DontShowIndicator |
| 4627 |
: QTreeWidgetItem::ShowIndicator |
| 4628 |
); |
| 4629 |
|
| 4630 |
if (!item->populated && !item->isExpanded()) { |
| 4631 |
bool doPopulate = false; |
| 4632 |
|
| 4633 |
bool external = item->object()->getDocument() != item->getOwnerDocument()->document(); |
| 4634 |
if (external) { |
| 4635 |
return; |
| 4636 |
} |
| 4637 |
auto obj = item->object()->getObject(); |
| 4638 |
auto linked = obj->getLinkedObject(true); |
| 4639 |
if (linked && linked->getDocument() != obj->getDocument()) { |
| 4640 |
return; |
| 4641 |
} |
| 4642 |
for (auto child : item->myData->children) { |
| 4643 |
auto it = ObjectMap.find(child); |
| 4644 |
if (it == ObjectMap.end() || it->second->items.empty()) { |
| 4645 |
auto vp = getViewProvider(child); |
| 4646 |
if (!vp) { |
| 4647 |
continue; |
| 4648 |
} |
| 4649 |
doPopulate = true; |
| 4650 |
break; |
| 4651 |
} |
| 4652 |
if (item->myData->removeChildrenFromRoot) { |
| 4653 |
if (it->second->rootItem) { |
| 4654 |
doPopulate = true; |
| 4655 |
break; |
| 4656 |
} |
| 4657 |
} |
| 4658 |
} |
| 4659 |
|
| 4660 |
if (!doPopulate) { |
| 4661 |
return; |
| 4662 |
} |
| 4663 |
} |
| 4664 |
|
| 4665 |
item->populated = true; |
| 4666 |
bool checkHidden = !showHidden(); |
| 4667 |
bool updated = false; |
| 4668 |
|
| 4669 |
int i = -1; |
| 4670 |
// iterate through the claimed children, and try to synchronize them with the |
| 4671 |
// children tree item with the same order of appearance. |
| 4672 |
int childCount = item->childCount(); |
| 4673 |
for (auto child : item->myData->children) { |
| 4674 |
|
| 4675 |
++i; // the current index of the claimed child |
| 4676 |
|
| 4677 |
bool found = false; |
| 4678 |
for (int j = i; j < childCount; ++j) { |
| 4679 |
QTreeWidgetItem* ci = item->child(j); |
| 4680 |
if (ci->type() != TreeWidget::ObjectType) { |
| 4681 |
continue; |
| 4682 |
} |
| 4683 |
|
| 4684 |
auto childItem = static_cast<DocumentObjectItem*>(ci); |
| 4685 |
if (childItem->object()->getObject() != child) { |
| 4686 |
continue; |
| 4687 |
} |
| 4688 |
|
| 4689 |
found = true; |
| 4690 |
if (j != i) { // fix index if it is changed |
| 4691 |
childItem->setHighlight(false); |
| 4692 |
item->removeChild(ci); |
| 4693 |
item->insertChild(i, ci); |
| 4694 |
assert(ci->parent() == item); |
| 4695 |
if (checkHidden) { |
| 4696 |
updateItemsVisibility(ci, false); |
| 4697 |
} |
| 4698 |
} |
| 4699 |
|
| 4700 |
// Check if the item just changed its policy of whether to remove |
| 4701 |
// children item from the root. |
| 4702 |
if (item->myData->removeChildrenFromRoot) { |
| 4703 |
if (childItem->myData->rootItem) { |
| 4704 |
assert(childItem != childItem->myData->rootItem); |
| 4705 |
bool lock = getTree()->blockSelection(true); |
| 4706 |
delete childItem->myData->rootItem; |
| 4707 |
getTree()->blockSelection(lock); |
| 4708 |
} |
| 4709 |
} |
| 4710 |
else if (childItem->requiredAtRoot()) { |
| 4711 |
createNewItem(*childItem->object(), this, -1, childItem->myData); |
| 4712 |
updated = true; |
| 4713 |
} |
| 4714 |
break; |
| 4715 |
} |
| 4716 |
|
| 4717 |
if (found) { |
| 4718 |
continue; |
| 4719 |
} |
| 4720 |
|
| 4721 |
// This algo will be recursively applied to newly created child items |
| 4722 |
// through slotNewObject -> populateItem |
| 4723 |
|
| 4724 |
auto it = ObjectMap.find(child); |
| 4725 |
if (it == ObjectMap.end() || it->second->items.empty()) { |
| 4726 |
auto vp = getViewProvider(child); |
| 4727 |
if (!vp |
| 4728 |
|| !createNewItem( |
| 4729 |
*vp, |
| 4730 |
item, |
| 4731 |
i, |
| 4732 |
it == ObjectMap.end() ? DocumentObjectDataPtr() : it->second |
| 4733 |
)) { |
| 4734 |
--i; |
| 4735 |
} |
| 4736 |
else { |
| 4737 |
updated = true; |
| 4738 |
} |
| 4739 |
continue; |
| 4740 |
} |
| 4741 |
|
| 4742 |
if (!item->myData->removeChildrenFromRoot || !it->second->rootItem) { |
| 4743 |
DocumentObjectItem* childItem = *it->second->items.begin(); |
| 4744 |
if (!createNewItem(*childItem->object(), item, i, it->second)) { |
| 4745 |
--i; |
| 4746 |
} |
| 4747 |
else { |
| 4748 |
updated = true; |
| 4749 |
} |
| 4750 |
} |
| 4751 |
else { |
| 4752 |
DocumentObjectItem* childItem = it->second->rootItem; |
| 4753 |
if (item == childItem || item->isChildOfItem(childItem)) { |
| 4754 |
TREE_ERR( |
| 4755 |
"Cyclic dependency in " << item->object()->getObject()->getFullName() << '.' |
| 4756 |
<< childItem->object()->getObject()->getFullName() |
| 4757 |
); |
| 4758 |
--i; |
| 4759 |
continue; |
| 4760 |
} |
| 4761 |
it->second->rootItem = nullptr; |
| 4762 |
childItem->setHighlight(false); |
| 4763 |
this->removeChild(childItem); |
| 4764 |
item->insertChild(i, childItem); |
| 4765 |
assert(childItem->parent() == item); |
| 4766 |
if (checkHidden) { |
| 4767 |
updateItemsVisibility(childItem, false); |
| 4768 |
} |
| 4769 |
} |
| 4770 |
} |
| 4771 |
|
| 4772 |
for (++i; item->childCount() > i;) { |
| 4773 |
QTreeWidgetItem* ci = item->child(i); |
| 4774 |
if (ci->type() == TreeWidget::ObjectType) { |
| 4775 |
auto childItem = static_cast<DocumentObjectItem*>(ci); |
| 4776 |
if (childItem->requiredAtRoot()) { |
| 4777 |
item->removeChild(childItem); |
| 4778 |
auto index = findRootIndex(childItem->object()->getObject()); |
| 4779 |
if (index >= 0) { |
| 4780 |
this->insertChild(index, childItem); |
| 4781 |
} |
| 4782 |
else { |
| 4783 |
this->addChild(childItem); |
| 4784 |
} |
| 4785 |
assert(childItem->parent() == this); |
| 4786 |
if (checkHidden) { |
| 4787 |
updateItemsVisibility(childItem, false); |
| 4788 |
} |
| 4789 |
childItem->myData->rootItem = childItem; |
| 4790 |
continue; |
| 4791 |
} |
| 4792 |
} |
| 4793 |
|
| 4794 |
bool lock = getTree()->blockSelection(true); |
| 4795 |
delete ci; |
| 4796 |
getTree()->blockSelection(lock); |
| 4797 |
} |
| 4798 |
if (updated) { |
| 4799 |
getTree()->_updateStatus(); |
| 4800 |
} |
| 4801 |
} |
| 4802 |
|
| 4803 |
int DocumentItem::findRootIndex(App::DocumentObject* childObj) |
| 4804 |
{ |
| 4805 |
if (!TreeParams::getKeepRootOrder() || !childObj || !childObj->isAttachedToDocument()) { |
| 4806 |
return -1; |
| 4807 |
} |
| 4808 |
|
| 4809 |
// Use view provider's tree rank to find correct place at the root level. |
| 4810 |
|
| 4811 |
int count = this->childCount(); |
| 4812 |
if (!count) { |
| 4813 |
return -1; |
| 4814 |
} |
| 4815 |
|
| 4816 |
int first, last; |
| 4817 |
|
| 4818 |
auto getTreeRank = [](Gui::ViewProviderDocumentObject* vp) -> int { |
| 4819 |
if (vp->getTreeRank() == -1) { |
| 4820 |
vp->setTreeRank(vp->getObject()->getID()); |
| 4821 |
} |
| 4822 |
return vp->getTreeRank(); |
| 4823 |
}; |
| 4824 |
|
| 4825 |
auto vpc = freecad_cast<ViewProviderDocumentObject*>( |
| 4826 |
Application::Instance->getViewProvider(childObj) |
| 4827 |
); |
| 4828 |
int childTreeRank = getTreeRank(vpc); |
| 4829 |
|
| 4830 |
// find the last item |
| 4831 |
for (last = count - 1; last >= 0; --last) { |
| 4832 |
auto citem = this->child(last); |
| 4833 |
if (citem->type() == TreeWidget::ObjectType) { |
| 4834 |
auto vp = static_cast<DocumentObjectItem*>(citem)->object(); |
| 4835 |
if (getTreeRank(vp) <= childTreeRank) { |
| 4836 |
return last + 1; |
| 4837 |
} |
| 4838 |
break; |
| 4839 |
} |
| 4840 |
} |
| 4841 |
|
| 4842 |
// find the first item |
| 4843 |
for (first = 0; first < count; ++first) { |
| 4844 |
auto citem = this->child(first); |
| 4845 |
if (citem->type() == TreeWidget::ObjectType) { |
| 4846 |
auto vp = static_cast<DocumentObjectItem*>(citem)->object(); |
| 4847 |
if (getTreeRank(vp) > childTreeRank) { |
| 4848 |
return first; |
| 4849 |
} |
| 4850 |
break; |
| 4851 |
} |
| 4852 |
} |
| 4853 |
|
| 4854 |
// now do a binary search to find the lower bound, assuming the root level |
| 4855 |
// object is already in order |
| 4856 |
count = last - first; |
| 4857 |
int pos; |
| 4858 |
while (count > 0) { |
| 4859 |
int step = count / 2; |
| 4860 |
pos = first + step; |
| 4861 |
for (; pos <= last; ++pos) { |
| 4862 |
auto citem = this->child(pos); |
| 4863 |
if (citem->type() != TreeWidget::ObjectType) { |
| 4864 |
continue; |
| 4865 |
} |
| 4866 |
auto vp = static_cast<DocumentObjectItem*>(citem)->object(); |
| 4867 |
if (vp->getTreeRank() < childTreeRank) { |
| 4868 |
first = ++pos; |
| 4869 |
count -= step + 1; |
| 4870 |
} |
| 4871 |
else { |
| 4872 |
count = step; |
| 4873 |
} |
| 4874 |
break; |
| 4875 |
} |
| 4876 |
if (pos > last) { |
| 4877 |
return -1; |
| 4878 |
} |
| 4879 |
} |
| 4880 |
if (first > last) { |
| 4881 |
return -1; |
| 4882 |
} |
| 4883 |
return first; |
| 4884 |
} |
| 4885 |
|
| 4886 |
void DocumentItem::sortObjectItems() |
| 4887 |
{ |
| 4888 |
QSignalBlocker guard(getTree()); |
| 4889 |
|
| 4890 |
std::vector<DocumentObjectItem*> sortedItems; |
| 4891 |
sortedItems.reserve(this->childCount()); |
| 4892 |
|
| 4893 |
for (int i = 0; i < this->childCount(); ++i) { |
| 4894 |
QTreeWidgetItem* treeItem = this->child(i); |
| 4895 |
if (treeItem->type() == TreeWidget::ObjectType) { |
| 4896 |
sortedItems.push_back(static_cast<DocumentObjectItem*>(treeItem)); |
| 4897 |
} |
| 4898 |
} |
| 4899 |
|
| 4900 |
std::stable_sort( |
| 4901 |
sortedItems.begin(), |
| 4902 |
sortedItems.end(), |
| 4903 |
[](DocumentObjectItem* a, DocumentObjectItem* b) { |
| 4904 |
return a->object()->getTreeRank() < b->object()->getTreeRank(); |
| 4905 |
} |
| 4906 |
); |
| 4907 |
|
| 4908 |
int sortedIndex = 0; |
| 4909 |
std::vector<bool> expansion; |
| 4910 |
for (int i = 0; i < this->childCount(); ++i) { |
| 4911 |
QTreeWidgetItem* treeItem = this->child(i); |
| 4912 |
if (treeItem->type() != TreeWidget::ObjectType) { |
| 4913 |
continue; |
| 4914 |
} |
| 4915 |
|
| 4916 |
DocumentObjectItem* sortedItem = sortedItems[sortedIndex++]; |
| 4917 |
if (sortedItem == treeItem) { |
| 4918 |
continue; |
| 4919 |
} |
| 4920 |
|
| 4921 |
expansion.clear(); |
| 4922 |
sortedItem->getExpandedSnapshot(expansion); |
| 4923 |
|
| 4924 |
this->removeChild(sortedItem); |
| 4925 |
this->insertChild(i, sortedItem); |
| 4926 |
if (!showHidden()) { |
| 4927 |
updateItemsVisibility(sortedItem, false); |
| 4928 |
} |
| 4929 |
|
| 4930 |
std::vector<bool>::const_iterator expFrom = expansion.cbegin(); |
| 4931 |
sortedItem->applyExpandedSnapshot(expansion, expFrom); |
| 4932 |
} |
| 4933 |
} |
| 4934 |
|
| 4935 |
void TreeWidget::slotChangeObject(const Gui::ViewProviderDocumentObject& view, const App::Property& prop) |
| 4936 |
{ |
| 4937 |
|
| 4938 |
auto obj = view.getObject(); |
| 4939 |
if (!obj || !obj->isAttachedToDocument()) { |
| 4940 |
return; |
| 4941 |
} |
| 4942 |
|
| 4943 |
auto itEntry = ObjectTable.find(obj); |
| 4944 |
if (itEntry == ObjectTable.end() || itEntry->second.empty()) { |
| 4945 |
return; |
| 4946 |
} |
| 4947 |
|
| 4948 |
_updateStatus(); |
| 4949 |
|
| 4950 |
// Let's not waste time on the newly added Visibility property in |
| 4951 |
// DocumentObject. |
| 4952 |
if (&prop == &obj->Visibility) { |
| 4953 |
return; |
| 4954 |
} |
| 4955 |
|
| 4956 |
if (&prop == &obj->Label) { |
| 4957 |
const char* label = obj->Label.getValue(); |
| 4958 |
auto firstData = *itEntry->second.begin(); |
| 4959 |
if (firstData->label != label) { |
| 4960 |
for (const auto& data : itEntry->second) { |
| 4961 |
data->label = label; |
| 4962 |
auto displayName = QString::fromUtf8(label); |
| 4963 |
for (auto item : data->items) { |
| 4964 |
item->setText(0, displayName); |
| 4965 |
} |
| 4966 |
} |
| 4967 |
} |
| 4968 |
return; |
| 4969 |
} |
| 4970 |
|
| 4971 |
if (&prop == &obj->Label2) { |
| 4972 |
const char* label = obj->Label2.getValue(); |
| 4973 |
auto firstData = *itEntry->second.begin(); |
| 4974 |
if (firstData->label2 != label) { |
| 4975 |
for (const auto& data : itEntry->second) { |
| 4976 |
data->label2 = label; |
| 4977 |
auto displayName = QString::fromUtf8(label); |
| 4978 |
for (auto item : data->items) { |
| 4979 |
item->setText(1, displayName); |
| 4980 |
} |
| 4981 |
} |
| 4982 |
} |
| 4983 |
return; |
| 4984 |
} |
| 4985 |
|
| 4986 |
auto& s = ChangedObjects[obj]; |
| 4987 |
if (prop.testStatus(App::Property::Output) || prop.testStatus(App::Property::NoRecompute)) { |
| 4988 |
s.set(CS_Output); |
| 4989 |
} |
| 4990 |
} |
| 4991 |
|
| 4992 |
void TreeWidget::updateChildren( |
| 4993 |
App::DocumentObject* obj, |
| 4994 |
const std::set<DocumentObjectDataPtr>& dataSet, |
| 4995 |
bool propOutput, |
| 4996 |
bool force |
| 4997 |
) |
| 4998 |
{ |
| 4999 |
bool childrenChanged = false; |
| 5000 |
std::vector<App::DocumentObject*> children; |
| 5001 |
bool removeChildrenFromRoot = true; |
| 5002 |
|
| 5003 |
DocumentObjectDataPtr found; |
| 5004 |
for (auto data : dataSet) { |
| 5005 |
if (!found) { |
| 5006 |
found = data; |
| 5007 |
childrenChanged = found->updateChildren(force); |
| 5008 |
removeChildrenFromRoot = found->viewObject->canRemoveChildrenFromRoot(); |
| 5009 |
if (!childrenChanged && found->removeChildrenFromRoot == removeChildrenFromRoot) { |
| 5010 |
return; |
| 5011 |
} |
| 5012 |
} |
| 5013 |
else if (childrenChanged) { |
| 5014 |
data->updateChildren(found); |
| 5015 |
} |
| 5016 |
data->removeChildrenFromRoot = removeChildrenFromRoot; |
| 5017 |
DocumentItem* docItem = data->docItem; |
| 5018 |
for (auto item : data->items) { |
| 5019 |
docItem->populateItem(item, true); |
| 5020 |
} |
| 5021 |
} |
| 5022 |
|
| 5023 |
if (force) { |
| 5024 |
return; |
| 5025 |
} |
| 5026 |
|
| 5027 |
if (childrenChanged && propOutput) { |
| 5028 |
// When a property is marked as output, it will not touch its object, |
| 5029 |
// and thus, its property change will not be propagated through |
| 5030 |
// recomputation. So we have to manually check for each links here. |
| 5031 |
for (auto link : App::GetApplication().getLinksTo(obj, App::GetLinkRecursive)) { |
| 5032 |
if (ChangedObjects.contains(link)) { |
| 5033 |
continue; |
| 5034 |
} |
| 5035 |
std::vector<App::DocumentObject*> linkedChildren; |
| 5036 |
DocumentObjectDataPtr found; |
| 5037 |
auto it = ObjectTable.find(link); |
| 5038 |
if (it == ObjectTable.end()) { |
| 5039 |
continue; |
| 5040 |
} |
| 5041 |
for (auto data : it->second) { |
| 5042 |
if (!found) { |
| 5043 |
found = data; |
| 5044 |
if (!found->updateChildren(false)) { |
| 5045 |
break; |
| 5046 |
} |
| 5047 |
} |
| 5048 |
data->updateChildren(found); |
| 5049 |
DocumentItem* docItem = data->docItem; |
| 5050 |
for (auto item : data->items) { |
| 5051 |
docItem->populateItem(item, true); |
| 5052 |
} |
| 5053 |
} |
| 5054 |
} |
| 5055 |
} |
| 5056 |
|
| 5057 |
if (childrenChanged) { |
| 5058 |
if (!selectTimer->isActive()) { |
| 5059 |
onSelectionChanged(SelectionChanges()); |
| 5060 |
} |
| 5061 |
|
| 5062 |
// if the item is in a GeoFeatureGroup we may need to update that too, as the claim children |
| 5063 |
// of the geofeaturegroup depends on what the childs claim |
| 5064 |
auto grp = App::GeoFeatureGroupExtension::getGroupOfObject(obj); |
| 5065 |
if (grp && !ChangedObjects.contains(grp)) { |
| 5066 |
auto iter = ObjectTable.find(grp); |
| 5067 |
if (iter != ObjectTable.end()) { |
| 5068 |
updateChildren(grp, iter->second, true, false); |
| 5069 |
} |
| 5070 |
} |
| 5071 |
} |
| 5072 |
} |
| 5073 |
|
| 5074 |
void DocumentItem::slotHighlightObject( |
| 5075 |
const Gui::ViewProviderDocumentObject& obj, |
| 5076 |
const Gui::HighlightMode& high, |
| 5077 |
bool set, |
| 5078 |
const App::DocumentObject* parent, |
| 5079 |
const char* subname |
| 5080 |
) |
| 5081 |
{ |
| 5082 |
getTree()->_updateStatus(false); |
| 5083 |
if (parent && parent->getDocument() != document()->getDocument()) { |
| 5084 |
auto it = getTree()->DocumentMap.find( |
| 5085 |
Application::Instance->getDocument(parent->getDocument()) |
| 5086 |
); |
| 5087 |
if (it != getTree()->DocumentMap.end()) { |
| 5088 |
it->second->slotHighlightObject(obj, high, set, parent, subname); |
| 5089 |
} |
| 5090 |
return; |
| 5091 |
} |
| 5092 |
FOREACH_ITEM(item, obj) |
| 5093 |
if (parent) { |
| 5094 |
App::DocumentObject* topParent = nullptr; |
| 5095 |
std::ostringstream ss; |
| 5096 |
item->getSubName(ss, topParent); |
| 5097 |
if (!topParent) { |
| 5098 |
if (parent != obj.getObject()) { |
| 5099 |
continue; |
| 5100 |
} |
| 5101 |
} |
| 5102 |
} |
| 5103 |
item->setHighlight(set, high); |
| 5104 |
if (parent) { |
| 5105 |
return; |
| 5106 |
} |
| 5107 |
END_FOREACH_ITEM |
| 5108 |
} |
| 5109 |
|
| 5110 |
static unsigned int countExpandedItem(const QTreeWidgetItem* item) |
| 5111 |
{ |
| 5112 |
unsigned int size = 0; |
| 5113 |
for (int i = 0, count = item->childCount(); i < count; ++i) { |
| 5114 |
auto citem = item->child(i); |
| 5115 |
if (citem->type() != TreeWidget::ObjectType || !citem->isExpanded()) { |
| 5116 |
continue; |
| 5117 |
} |
| 5118 |
auto obj = static_cast<const DocumentObjectItem*>(citem)->object()->getObject(); |
| 5119 |
if (obj->isAttachedToDocument()) { |
| 5120 |
size += strlen(obj->getNameInDocument()) + countExpandedItem(citem); |
| 5121 |
} |
| 5122 |
} |
| 5123 |
return size; |
| 5124 |
} |
| 5125 |
|
| 5126 |
unsigned int DocumentItem::getMemSize() const |
| 5127 |
{ |
| 5128 |
return countExpandedItem(this); |
| 5129 |
} |
| 5130 |
|
| 5131 |
static void saveExpandedItem(Base::Writer& writer, const QTreeWidgetItem* item) |
| 5132 |
{ |
| 5133 |
int itemCount = 0; |
| 5134 |
for (int i = 0, count = item->childCount(); i < count; ++i) { |
| 5135 |
auto citem = item->child(i); |
| 5136 |
if (citem->type() != TreeWidget::ObjectType || !citem->isExpanded()) { |
| 5137 |
continue; |
| 5138 |
} |
| 5139 |
auto obj = static_cast<const DocumentObjectItem*>(citem)->object()->getObject(); |
| 5140 |
if (obj->isAttachedToDocument()) { |
| 5141 |
++itemCount; |
| 5142 |
} |
| 5143 |
} |
| 5144 |
|
| 5145 |
if (!itemCount) { |
| 5146 |
writer.Stream() << "/>" << std::endl; |
| 5147 |
return; |
| 5148 |
} |
| 5149 |
|
| 5150 |
writer.Stream() << " count=\"" << itemCount << "\">" << std::endl; |
| 5151 |
writer.incInd(); |
| 5152 |
for (int i = 0, count = item->childCount(); i < count; ++i) { |
| 5153 |
auto citem = item->child(i); |
| 5154 |
if (citem->type() != TreeWidget::ObjectType || !citem->isExpanded()) { |
| 5155 |
continue; |
| 5156 |
} |
| 5157 |
auto obj = static_cast<const DocumentObjectItem*>(citem)->object()->getObject(); |
| 5158 |
if (obj->isAttachedToDocument()) { |
| 5159 |
writer.Stream() << writer.ind() << "<Expand name=\"" << obj->getNameInDocument() << "\""; |
| 5160 |
saveExpandedItem(writer, static_cast<const DocumentObjectItem*>(citem)); |
| 5161 |
} |
| 5162 |
} |
| 5163 |
writer.decInd(); |
| 5164 |
writer.Stream() << writer.ind() << "</Expand>" << std::endl; |
| 5165 |
} |
| 5166 |
|
| 5167 |
void DocumentItem::Save(Base::Writer& writer) const |
| 5168 |
{ |
| 5169 |
writer.Stream() << writer.ind() << "<Expand "; |
| 5170 |
saveExpandedItem(writer, this); |
| 5171 |
} |
| 5172 |
|
| 5173 |
void DocumentItem::Restore(Base::XMLReader& reader) |
| 5174 |
{ |
| 5175 |
reader.readElement("Expand"); |
| 5176 |
if (!reader.hasAttribute("count")) { |
| 5177 |
return; |
| 5178 |
} |
| 5179 |
_ExpandInfo.reset(new ExpandInfo); |
| 5180 |
_ExpandInfo->restore(reader); |
| 5181 |
for (auto inst : TreeWidget::Instances) { |
| 5182 |
if (inst != getTree()) { |
| 5183 |
auto docItem = inst->getDocumentItem(document()); |
| 5184 |
if (docItem) { |
| 5185 |
docItem->_ExpandInfo = _ExpandInfo; |
| 5186 |
} |
| 5187 |
} |
| 5188 |
} |
| 5189 |
} |
| 5190 |
|
| 5191 |
void DocumentItem::restoreItemExpansion(const ExpandInfoPtr& info, DocumentObjectItem* item) |
| 5192 |
{ |
| 5193 |
item->setExpanded(true); |
| 5194 |
if (!info) { |
| 5195 |
return; |
| 5196 |
} |
| 5197 |
for (int i = 0, count = item->childCount(); i < count; ++i) { |
| 5198 |
auto citem = item->child(i); |
| 5199 |
if (citem->type() != TreeWidget::ObjectType) { |
| 5200 |
continue; |
| 5201 |
} |
| 5202 |
auto obj = static_cast<DocumentObjectItem*>(citem)->object()->getObject(); |
| 5203 |
if (!obj->isAttachedToDocument()) { |
| 5204 |
continue; |
| 5205 |
} |
| 5206 |
auto it = info->find(obj->getNameInDocument()); |
| 5207 |
if (it != info->end()) { |
| 5208 |
restoreItemExpansion(it->second, static_cast<DocumentObjectItem*>(citem)); |
| 5209 |
} |
| 5210 |
} |
| 5211 |
} |
| 5212 |
|
| 5213 |
void DocumentItem::slotExpandObject( |
| 5214 |
const Gui::ViewProviderDocumentObject& obj, |
| 5215 |
const Gui::TreeItemMode& mode, |
| 5216 |
const App::DocumentObject* parent, |
| 5217 |
const char* subname |
| 5218 |
) |
| 5219 |
{ |
| 5220 |
getTree()->_updateStatus(false); |
| 5221 |
|
| 5222 |
if ((mode == TreeItemMode::ExpandItem || mode == TreeItemMode::ExpandPath) |
| 5223 |
&& obj.getDocument()->getDocument()->testStatus(App::Document::Restoring)) { |
| 5224 |
if (!_ExpandInfo) { |
| 5225 |
_ExpandInfo.reset(new ExpandInfo); |
| 5226 |
} |
| 5227 |
_ExpandInfo->emplace(std::string("*") + obj.getObject()->getNameInDocument(), ExpandInfoPtr()); |
| 5228 |
return; |
| 5229 |
} |
| 5230 |
|
| 5231 |
if (parent && parent->getDocument() != document()->getDocument()) { |
| 5232 |
auto it = getTree()->DocumentMap.find( |
| 5233 |
Application::Instance->getDocument(parent->getDocument()) |
| 5234 |
); |
| 5235 |
if (it != getTree()->DocumentMap.end()) { |
| 5236 |
it->second->slotExpandObject(obj, mode, parent, subname); |
| 5237 |
} |
| 5238 |
return; |
| 5239 |
} |
| 5240 |
|
| 5241 |
FOREACH_ITEM(item, obj) |
| 5242 |
// All document object items must always have a parent, either another |
| 5243 |
// object item or document item. If not, then there is a bug somewhere |
| 5244 |
// else. |
| 5245 |
assert(item->parent()); |
| 5246 |
|
| 5247 |
switch (mode) { |
| 5248 |
case TreeItemMode::ExpandPath: |
| 5249 |
if (!parent) { |
| 5250 |
QTreeWidgetItem* parentItem = item->parent(); |
| 5251 |
while (parentItem) { |
| 5252 |
parentItem->setExpanded(true); |
| 5253 |
parentItem = parentItem->parent(); |
| 5254 |
} |
| 5255 |
item->setExpanded(true); |
| 5256 |
break; |
| 5257 |
} |
| 5258 |
// fall through |
| 5259 |
case TreeItemMode::ExpandItem: |
| 5260 |
if (!parent) { |
| 5261 |
if (item->parent()->isExpanded()) { |
| 5262 |
item->setExpanded(true); |
| 5263 |
} |
| 5264 |
} |
| 5265 |
else { |
| 5266 |
App::DocumentObject* topParent = nullptr; |
| 5267 |
std::ostringstream ss; |
| 5268 |
item->getSubName(ss, topParent); |
| 5269 |
if (!topParent) { |
| 5270 |
if (parent != obj.getObject()) { |
| 5271 |
continue; |
| 5272 |
} |
| 5273 |
} |
| 5274 |
else if (topParent != parent) { |
| 5275 |
continue; |
| 5276 |
} |
| 5277 |
showItem(item, false, true); |
| 5278 |
item->setExpanded(true); |
| 5279 |
} |
| 5280 |
break; |
| 5281 |
case TreeItemMode::CollapseItem: |
| 5282 |
item->setExpanded(false); |
| 5283 |
break; |
| 5284 |
case TreeItemMode::ToggleItem: |
| 5285 |
if (item->isExpanded()) { |
| 5286 |
item->setExpanded(false); |
| 5287 |
} |
| 5288 |
else { |
| 5289 |
item->setExpanded(true); |
| 5290 |
} |
| 5291 |
break; |
| 5292 |
|
| 5293 |
default: |
| 5294 |
break; |
| 5295 |
} |
| 5296 |
if (item->isExpanded()) { |
| 5297 |
populateItem(item); |
| 5298 |
} |
| 5299 |
if (parent) { |
| 5300 |
return; |
| 5301 |
} |
| 5302 |
END_FOREACH_ITEM |
| 5303 |
} |
| 5304 |
|
| 5305 |
void DocumentItem::slotScrollToObject(const Gui::ViewProviderDocumentObject& obj) |
| 5306 |
{ |
| 5307 |
if (!obj.getObject() || !obj.getObject()->isAttachedToDocument()) { |
| 5308 |
return; |
| 5309 |
} |
| 5310 |
auto it = ObjectMap.find(obj.getObject()); |
| 5311 |
if (it == ObjectMap.end() || it->second->items.empty()) { |
| 5312 |
return; |
| 5313 |
} |
| 5314 |
auto item = it->second->rootItem; |
| 5315 |
if (!item) { |
| 5316 |
item = *it->second->items.begin(); |
| 5317 |
} |
| 5318 |
getTree()->_updateStatus(false); |
| 5319 |
getTree()->scrollToItem(item); |
| 5320 |
} |
| 5321 |
|
| 5322 |
void DocumentItem::slotRecomputedObject(const App::DocumentObject& obj) |
| 5323 |
{ |
| 5324 |
if (obj.isValid()) { |
| 5325 |
return; |
| 5326 |
} |
| 5327 |
slotRecomputed(*obj.getDocument(), {const_cast<App::DocumentObject*>(&obj)}); |
| 5328 |
} |
| 5329 |
|
| 5330 |
void DocumentItem::slotRecomputed(const App::Document&, const std::vector<App::DocumentObject*>& objs) |
| 5331 |
{ |
| 5332 |
auto tree = getTree(); |
| 5333 |
for (auto obj : objs) { |
| 5334 |
if (!obj->isValid()) { |
| 5335 |
tree->ChangedObjects[obj].set(TreeWidget::CS_Error); |
| 5336 |
} |
| 5337 |
} |
| 5338 |
if (!tree->ChangedObjects.empty()) { |
| 5339 |
tree->_updateStatus(); |
| 5340 |
} |
| 5341 |
} |
| 5342 |
|
| 5343 |
Gui::Document* DocumentItem::document() const |
| 5344 |
{ |
| 5345 |
return this->pDocument; |
| 5346 |
} |
| 5347 |
|
| 5348 |
void DocumentItem::testStatus() |
| 5349 |
{ |
| 5350 |
for (const auto& v : ObjectMap) { |
| 5351 |
v.second->testStatus(); |
| 5352 |
} |
| 5353 |
} |
| 5354 |
|
| 5355 |
void DocumentItem::setData(int column, int role, const QVariant& value) |
| 5356 |
{ |
| 5357 |
if (role == Qt::EditRole) { |
| 5358 |
QString label = value.toString(); |
| 5359 |
pDocument->getDocument()->Label.setValue((const char*)label.toUtf8()); |
| 5360 |
} |
| 5361 |
|
| 5362 |
QTreeWidgetItem::setData(column, role, value); |
| 5363 |
} |
| 5364 |
|
| 5365 |
void DocumentItem::clearSelection(DocumentObjectItem* exclude) |
| 5366 |
{ |
| 5367 |
// Block signals here otherwise we get a recursion and quadratic runtime |
| 5368 |
bool ok = treeWidget()->blockSignals(true); |
| 5369 |
FOREACH_ITEM_ALL(item); |
| 5370 |
_v.second->dirtyFlag = false; |
| 5371 |
if (item == exclude) { |
| 5372 |
if (item->selected > 0) { |
| 5373 |
item->selected = -1; |
| 5374 |
} |
| 5375 |
else { |
| 5376 |
item->selected = 0; |
| 5377 |
} |
| 5378 |
updateItemSelection(item); |
| 5379 |
// The set has been changed while calling updateItemSelection |
| 5380 |
// so that the iterator has become invalid -> Abort |
| 5381 |
if (_v.second->dirtyFlag) { |
| 5382 |
break; |
| 5383 |
} |
| 5384 |
} |
| 5385 |
else { |
| 5386 |
item->selected = 0; |
| 5387 |
item->mySubs.clear(); |
| 5388 |
item->setSelected(false); |
| 5389 |
item->setCheckState(false); |
| 5390 |
} |
| 5391 |
END_FOREACH_ITEM; |
| 5392 |
treeWidget()->blockSignals(ok); |
| 5393 |
} |
| 5394 |
|
| 5395 |
void DocumentItem::updateSelection(QTreeWidgetItem* ti, bool unselect) |
| 5396 |
{ |
| 5397 |
for (int i = 0, count = ti->childCount(); i < count; ++i) { |
| 5398 |
auto child = ti->child(i); |
| 5399 |
if (child && child->type() == TreeWidget::ObjectType) { |
| 5400 |
auto childItem = static_cast<DocumentObjectItem*>(child); |
| 5401 |
if (unselect) { |
| 5402 |
childItem->setSelected(false); |
| 5403 |
childItem->setCheckState(false); |
| 5404 |
} |
| 5405 |
updateItemSelection(childItem); |
| 5406 |
if (unselect && childItem->isGroup()) { |
| 5407 |
// If the child item being force unselected by its group parent |
| 5408 |
// is itself a group, propagate the unselection to its own |
| 5409 |
// children |
| 5410 |
updateSelection(childItem, true); |
| 5411 |
} |
| 5412 |
} |
| 5413 |
} |
| 5414 |
|
| 5415 |
if (unselect) { |
| 5416 |
return; |
| 5417 |
} |
| 5418 |
for (int i = 0, count = ti->childCount(); i < count; ++i) { |
| 5419 |
updateSelection(ti->child(i)); |
| 5420 |
} |
| 5421 |
} |
| 5422 |
|
| 5423 |
void DocumentItem::updateItemSelection(DocumentObjectItem* item) |
| 5424 |
{ |
| 5425 |
// Note: In several places of this function the selection is altered and the notification of |
| 5426 |
// the selection observers can trigger a recreation of all DocumentObjectItem so that the |
| 5427 |
// passed 'item' can become a dangling pointer. |
| 5428 |
// Thus,'item' mustn't be accessed any more after altering the selection. |
| 5429 |
// For further details see the bug analysis of #13107 |
| 5430 |
bool selected = item->isSelected(); |
| 5431 |
bool checked = item->checkState(0) == Qt::Checked; |
| 5432 |
|
| 5433 |
if (selected && !checked) { |
| 5434 |
item->setCheckState(true); |
| 5435 |
} |
| 5436 |
|
| 5437 |
if (!selected && checked) { |
| 5438 |
item->setCheckState(false); |
| 5439 |
} |
| 5440 |
|
| 5441 |
if (!selected && !item->selected) { |
| 5442 |
return; |
| 5443 |
} |
| 5444 |
if (item->selected != -1) { |
| 5445 |
item->mySubs.clear(); |
| 5446 |
} |
| 5447 |
item->selected = selected; |
| 5448 |
|
| 5449 |
auto obj = item->object()->getObject(); |
| 5450 |
if (!obj || !obj->isAttachedToDocument()) { |
| 5451 |
return; |
| 5452 |
} |
| 5453 |
|
| 5454 |
std::ostringstream str; |
| 5455 |
App::DocumentObject* topParent = nullptr; |
| 5456 |
item->getSubName(str, topParent); |
| 5457 |
if (topParent) { |
| 5458 |
if (!obj->redirectSubName(str, topParent, nullptr)) { |
| 5459 |
str << obj->getNameInDocument() << '.'; |
| 5460 |
} |
| 5461 |
obj = topParent; |
| 5462 |
} |
| 5463 |
const char* objname = obj->getNameInDocument(); |
| 5464 |
const char* docname = obj->getDocument()->getName(); |
| 5465 |
const auto& subname = str.str(); |
| 5466 |
|
| 5467 |
#ifdef FC_DEBUG |
| 5468 |
if (!subname.empty()) { |
| 5469 |
assert(item->getParentItem()); |
| 5470 |
} |
| 5471 |
#endif |
| 5472 |
|
| 5473 |
if (!selected) { |
| 5474 |
// Handles deselection for same name object and subname |
| 5475 |
bool keep = false; |
| 5476 |
auto items = getTree()->selectedItems(); |
| 5477 |
for (auto it : items) { |
| 5478 |
if (it->type() == TreeWidget::ObjectType) { |
| 5479 |
auto docitem = static_cast<DocumentObjectItem*>(it); |
| 5480 |
auto obj2 = docitem->object()->getObject(); |
| 5481 |
if (!obj2 || !obj2->isAttachedToDocument()) { |
| 5482 |
continue; |
| 5483 |
} |
| 5484 |
|
| 5485 |
std::ostringstream str2; |
| 5486 |
App::DocumentObject* topParent2 = nullptr; |
| 5487 |
docitem->getSubName(str2, topParent2); |
| 5488 |
|
| 5489 |
if (topParent2) { |
| 5490 |
if (!obj2->redirectSubName(str2, topParent2, nullptr)) { |
| 5491 |
str2 << obj2->getNameInDocument() << '.'; |
| 5492 |
} |
| 5493 |
obj2 = topParent2; |
| 5494 |
} |
| 5495 |
|
| 5496 |
if (obj2 == obj && str2.str() == subname) { |
| 5497 |
keep = true; |
| 5498 |
break; |
| 5499 |
} |
| 5500 |
} |
| 5501 |
} |
| 5502 |
|
| 5503 |
if (!keep) { |
| 5504 |
Gui::Selection().rmvSelection(docname, objname, subname.c_str()); |
| 5505 |
} |
| 5506 |
return; |
| 5507 |
} |
| 5508 |
|
| 5509 |
auto vobj = item->object(); |
| 5510 |
selected = false; |
| 5511 |
if (!item->mySubs.empty()) { |
| 5512 |
for (auto& sub : item->mySubs) { |
| 5513 |
if (Gui::Selection().isSelected(docname, objname, (subname + sub).c_str()) |
| 5514 |
|| Gui::Selection().addSelection(docname, objname, (subname + sub).c_str())) { |
| 5515 |
selected = true; |
| 5516 |
} |
| 5517 |
} |
| 5518 |
} |
| 5519 |
if (!selected) { |
| 5520 |
item->mySubs.clear(); |
| 5521 |
if (!Gui::Selection().isSelected(docname, objname, subname.c_str()) |
| 5522 |
&& !Gui::Selection().addSelection(docname, objname, subname.c_str())) { |
| 5523 |
// Safely re-access the item |
| 5524 |
DocumentObjectItem* item2 = findItem(vobj->getObject(), subname); |
| 5525 |
if (item2) { |
| 5526 |
item2->selected = 0; |
| 5527 |
item2->setSelected(false); |
| 5528 |
item2->setCheckState(false); |
| 5529 |
} |
| 5530 |
return; |
| 5531 |
} |
| 5532 |
} |
| 5533 |
getTree()->syncView(vobj); |
| 5534 |
} |
| 5535 |
|
| 5536 |
App::DocumentObject* DocumentItem::getTopParent(App::DocumentObject* obj, std::string& subname) |
| 5537 |
{ |
| 5538 |
auto it = ObjectMap.find(obj); |
| 5539 |
if (it == ObjectMap.end() || it->second->items.empty()) { |
| 5540 |
return nullptr; |
| 5541 |
} |
| 5542 |
|
| 5543 |
// already a top parent |
| 5544 |
if (it->second->rootItem) { |
| 5545 |
return obj; |
| 5546 |
} |
| 5547 |
|
| 5548 |
for (auto item : it->second->items) { |
| 5549 |
// non group object do not provide a coordinate system, hence its |
| 5550 |
// claimed child is still in the global coordinate space, so the |
| 5551 |
// child can still be considered a top level object |
| 5552 |
if (!item->isParentGroup()) { |
| 5553 |
return obj; |
| 5554 |
} |
| 5555 |
} |
| 5556 |
|
| 5557 |
// If no top level item, find an item that is closest to the top level |
| 5558 |
std::multimap<int, DocumentObjectItem*> items; |
| 5559 |
for (auto item : it->second->items) { |
| 5560 |
int i = 0; |
| 5561 |
for (auto parent = item->parent(); parent; ++i, parent = parent->parent()) { |
| 5562 |
if (parent->isHidden()) { |
| 5563 |
i += 1000; |
| 5564 |
} |
| 5565 |
++i; |
| 5566 |
} |
| 5567 |
items.emplace(i, item); |
| 5568 |
} |
| 5569 |
|
| 5570 |
App::DocumentObject* topParent = nullptr; |
| 5571 |
std::ostringstream ss; |
| 5572 |
items.begin()->second->getSubName(ss, topParent); |
| 5573 |
if (!topParent) { |
| 5574 |
// this shouldn't happen |
| 5575 |
FC_WARN("No top parent for " << obj->getFullName() << '.' << subname); |
| 5576 |
return obj; |
| 5577 |
} |
| 5578 |
ss << obj->getNameInDocument() << '.' << subname; |
| 5579 |
FC_LOG( |
| 5580 |
"Subname correction " << obj->getFullName() << '.' << subname << " -> " |
| 5581 |
<< topParent->getFullName() << '.' << ss.str() |
| 5582 |
); |
| 5583 |
subname = ss.str(); |
| 5584 |
return topParent; |
| 5585 |
} |
| 5586 |
|
| 5587 |
DocumentObjectItem* DocumentItem::findItem(App::DocumentObject* obj, const std::string& subname) const |
| 5588 |
{ |
| 5589 |
auto it = ObjectMap.find(obj); |
| 5590 |
if (it == ObjectMap.end()) { |
| 5591 |
return nullptr; |
| 5592 |
} |
| 5593 |
|
| 5594 |
// There is only one instance in the tree view |
| 5595 |
if (it->second->items.size() == 1) { |
| 5596 |
return *(it->second->items.begin()); |
| 5597 |
} |
| 5598 |
|
| 5599 |
// If there are multiple instances use the one with the same subname |
| 5600 |
DocumentObjectItem* item {}; |
| 5601 |
for (auto jt : it->second->items) { |
| 5602 |
std::ostringstream str; |
| 5603 |
App::DocumentObject* topParent = nullptr; |
| 5604 |
jt->getSubName(str, topParent); |
| 5605 |
if (topParent) { |
| 5606 |
if (!obj->redirectSubName(str, topParent, nullptr)) { |
| 5607 |
str << obj->getNameInDocument() << '.'; |
| 5608 |
} |
| 5609 |
} |
| 5610 |
|
| 5611 |
if (subname == str.str()) { |
| 5612 |
item = jt; |
| 5613 |
break; |
| 5614 |
} |
| 5615 |
} |
| 5616 |
|
| 5617 |
return item; |
| 5618 |
} |
| 5619 |
|
| 5620 |
DocumentObjectItem* DocumentItem::findItemByObject( |
| 5621 |
bool sync, |
| 5622 |
App::DocumentObject* obj, |
| 5623 |
const char* subname, |
| 5624 |
bool select |
| 5625 |
) |
| 5626 |
{ |
| 5627 |
if (!subname) { |
| 5628 |
subname = ""; |
| 5629 |
} |
| 5630 |
|
| 5631 |
auto it = ObjectMap.find(obj); |
| 5632 |
if (it == ObjectMap.end() || it->second->items.empty()) { |
| 5633 |
return nullptr; |
| 5634 |
} |
| 5635 |
|
| 5636 |
// prefer top level item of this object |
| 5637 |
if (it->second->rootItem) { |
| 5638 |
return findItem(sync, it->second->rootItem, subname, select); |
| 5639 |
} |
| 5640 |
|
| 5641 |
for (auto item : it->second->items) { |
| 5642 |
// non group object do not provide a coordinate system, hence its |
| 5643 |
// claimed child is still in the global coordinate space, so the |
| 5644 |
// child can still be considered a top level object |
| 5645 |
if (!item->isParentGroup()) { |
| 5646 |
return findItem(sync, item, subname, select); |
| 5647 |
} |
| 5648 |
} |
| 5649 |
|
| 5650 |
// If no top level item, find an item that is closest to the top level |
| 5651 |
std::multimap<int, DocumentObjectItem*> items; |
| 5652 |
for (auto item : it->second->items) { |
| 5653 |
int i = 0; |
| 5654 |
for (auto parent = item->parent(); parent; ++i, parent = parent->parent()) { |
| 5655 |
++i; |
| 5656 |
} |
| 5657 |
items.emplace(i, item); |
| 5658 |
} |
| 5659 |
for (auto& v : items) { |
| 5660 |
auto item = findItem(sync, v.second, subname, select); |
| 5661 |
if (item) { |
| 5662 |
return item; |
| 5663 |
} |
| 5664 |
} |
| 5665 |
return nullptr; |
| 5666 |
} |
| 5667 |
|
| 5668 |
DocumentObjectItem* DocumentItem::findItem( |
| 5669 |
bool sync, |
| 5670 |
DocumentObjectItem* item, |
| 5671 |
const char* subname, |
| 5672 |
bool select |
| 5673 |
) |
| 5674 |
{ |
| 5675 |
if (item->isHidden()) { |
| 5676 |
item->setHidden(false); |
| 5677 |
} |
| 5678 |
|
| 5679 |
if (!subname || *subname == 0) { |
| 5680 |
if (select) { |
| 5681 |
item->selected += 2; |
| 5682 |
item->mySubs.clear(); |
| 5683 |
} |
| 5684 |
return item; |
| 5685 |
} |
| 5686 |
|
| 5687 |
TREE_TRACE("find next " << subname); |
| 5688 |
|
| 5689 |
// try to find the next level object name |
| 5690 |
const char* nextsub = nullptr; |
| 5691 |
const char* dot = nullptr; |
| 5692 |
if ((dot = strchr(subname, '.'))) { |
| 5693 |
nextsub = dot + 1; |
| 5694 |
} |
| 5695 |
else { |
| 5696 |
if (select) { |
| 5697 |
item->selected += 2; |
| 5698 |
if (std::ranges::find(item->mySubs, subname) == item->mySubs.end()) { |
| 5699 |
item->mySubs.emplace_back(subname); |
| 5700 |
} |
| 5701 |
} |
| 5702 |
return item; |
| 5703 |
} |
| 5704 |
|
| 5705 |
std::string name(subname, nextsub - subname); |
| 5706 |
auto obj = item->object()->getObject(); |
| 5707 |
auto subObj = obj->getSubObject(name.c_str()); |
| 5708 |
if (!subObj || subObj == obj) { |
| 5709 |
if (!subObj && !getTree()->searchDoc) { |
| 5710 |
TREE_LOG("sub object not found " << item->getName() << '.' << name.c_str()); |
| 5711 |
} |
| 5712 |
if (select) { |
| 5713 |
item->selected += 2; |
| 5714 |
if (std::ranges::find(item->mySubs, subname) == item->mySubs.end()) { |
| 5715 |
item->mySubs.emplace_back(subname); |
| 5716 |
} |
| 5717 |
} |
| 5718 |
return item; |
| 5719 |
} |
| 5720 |
|
| 5721 |
if (select) { |
| 5722 |
item->mySubs.clear(); |
| 5723 |
} |
| 5724 |
|
| 5725 |
if (!item->populated && sync) { |
| 5726 |
// force populate the item |
| 5727 |
item->populated = true; |
| 5728 |
populateItem(item, true); |
| 5729 |
} |
| 5730 |
|
| 5731 |
for (int i = 0, count = item->childCount(); i < count; ++i) { |
| 5732 |
auto ti = item->child(i); |
| 5733 |
if (!ti || ti->type() != TreeWidget::ObjectType) { |
| 5734 |
continue; |
| 5735 |
} |
| 5736 |
auto child = static_cast<DocumentObjectItem*>(ti); |
| 5737 |
|
| 5738 |
if (child->object()->getObject() == subObj) { |
| 5739 |
return findItem(sync, child, nextsub, select); |
| 5740 |
} |
| 5741 |
} |
| 5742 |
|
| 5743 |
// The sub object is not found. This could happen for geo group, since its |
| 5744 |
// children may be in more than one hierarchy down. |
| 5745 |
bool found = false; |
| 5746 |
DocumentObjectItem* res = nullptr; |
| 5747 |
auto it = ObjectMap.find(subObj); |
| 5748 |
if (it != ObjectMap.end()) { |
| 5749 |
for (auto child : it->second->items) { |
| 5750 |
if (child->isChildOfItem(item)) { |
| 5751 |
found = true; |
| 5752 |
res = findItem(sync, child, nextsub, select); |
| 5753 |
if (!select) { |
| 5754 |
return res; |
| 5755 |
} |
| 5756 |
} |
| 5757 |
} |
| 5758 |
} |
| 5759 |
|
| 5760 |
if (select && !found) { |
| 5761 |
// The sub object is still not found. Maybe it is a non-object sub-element. |
| 5762 |
// Select the current object instead. |
| 5763 |
TREE_TRACE("element " << subname << " not found"); |
| 5764 |
item->selected += 2; |
| 5765 |
if (std::ranges::find(item->mySubs, subname) == item->mySubs.end()) { |
| 5766 |
item->mySubs.emplace_back(subname); |
| 5767 |
} |
| 5768 |
} |
| 5769 |
return res; |
| 5770 |
} |
| 5771 |
|
| 5772 |
void DocumentItem::selectItems(SelectionReason reason) |
| 5773 |
{ |
| 5774 |
const auto& sels |
| 5775 |
= Selection().getSelection(pDocument->getDocument()->getName(), ResolveMode::NoResolve); |
| 5776 |
|
| 5777 |
bool sync = (sels.size() > 50 || reason == SR_SELECT) ? false : true; |
| 5778 |
|
| 5779 |
for (const auto& sel : sels) { |
| 5780 |
findItemByObject(sync, sel.pObject, sel.SubName, true); |
| 5781 |
} |
| 5782 |
|
| 5783 |
DocumentObjectItem* newSelect = nullptr; |
| 5784 |
DocumentObjectItem* oldSelect = nullptr; |
| 5785 |
|
| 5786 |
FOREACH_ITEM_ALL(item) |
| 5787 |
if (item->selected == 1) { |
| 5788 |
// this means it is the old selection and is not in the current |
| 5789 |
// selection |
| 5790 |
item->selected = 0; |
| 5791 |
item->mySubs.clear(); |
| 5792 |
item->setSelected(false); |
| 5793 |
item->setCheckState(false); |
| 5794 |
} |
| 5795 |
else if (item->selected) { |
| 5796 |
if (sync) { |
| 5797 |
if ((item->selected == 2 || reason == SR_FORCE_EXPAND) |
| 5798 |
&& showItem(item, false, reason == SR_FORCE_EXPAND)) { |
| 5799 |
// This means newly selected and can auto expand |
| 5800 |
if (!newSelect) { |
| 5801 |
newSelect = item; |
| 5802 |
} |
| 5803 |
} |
| 5804 |
if (!newSelect && !oldSelect && !item->isHidden()) { |
| 5805 |
bool visible = true; |
| 5806 |
for (auto parent = item->parent(); parent; parent = parent->parent()) { |
| 5807 |
if (!parent->isExpanded() || parent->isHidden()) { |
| 5808 |
visible = false; |
| 5809 |
break; |
| 5810 |
} |
| 5811 |
} |
| 5812 |
if (visible) { |
| 5813 |
oldSelect = item; |
| 5814 |
} |
| 5815 |
} |
| 5816 |
} |
| 5817 |
item->selected = 1; |
| 5818 |
item->setSelected(true); |
| 5819 |
item->setCheckState(true); |
| 5820 |
} |
| 5821 |
END_FOREACH_ITEM; |
| 5822 |
|
| 5823 |
if (sync) { |
| 5824 |
if (!newSelect) { |
| 5825 |
newSelect = oldSelect; |
| 5826 |
} |
| 5827 |
else { |
| 5828 |
getTree()->syncView(newSelect->object()); |
| 5829 |
} |
| 5830 |
if (newSelect) { |
| 5831 |
getTree()->scrollToItem(newSelect); |
| 5832 |
} |
| 5833 |
} |
| 5834 |
} |
| 5835 |
|
| 5836 |
void DocumentItem::populateParents(const ViewProvider* vp, ViewParentMap& parentMap) |
| 5837 |
{ |
| 5838 |
auto it = parentMap.find(vp); |
| 5839 |
if (it == parentMap.end()) { |
| 5840 |
return; |
| 5841 |
} |
| 5842 |
for (auto parent : it->second) { |
| 5843 |
auto it = ObjectMap.find(parent->getObject()); |
| 5844 |
if (it == ObjectMap.end()) { |
| 5845 |
continue; |
| 5846 |
} |
| 5847 |
|
| 5848 |
populateParents(parent, parentMap); |
| 5849 |
for (auto item : it->second->items) { |
| 5850 |
if (!item->isHidden() && !item->populated) { |
| 5851 |
item->populated = true; |
| 5852 |
populateItem(item, true); |
| 5853 |
} |
| 5854 |
} |
| 5855 |
} |
| 5856 |
} |
| 5857 |
|
| 5858 |
void DocumentItem::selectAllInstances(const ViewProviderDocumentObject& vpd) |
| 5859 |
{ |
| 5860 |
ViewParentMap parentMap; |
| 5861 |
auto pObject = vpd.getObject(); |
| 5862 |
if (!ObjectMap.contains(pObject)) { |
| 5863 |
return; |
| 5864 |
} |
| 5865 |
|
| 5866 |
bool lock = getTree()->blockSelection(true); |
| 5867 |
|
| 5868 |
// We are trying to select all items corresponding to a given view |
| 5869 |
// provider, i.e. all appearance of the object inside all its parent items |
| 5870 |
// |
| 5871 |
// Build a map of object to all its parent |
| 5872 |
for (auto& v : ObjectMap) { |
| 5873 |
if (v.second->viewObject == &vpd) { |
| 5874 |
continue; |
| 5875 |
} |
| 5876 |
for (auto child : v.second->viewObject->claimChildren()) { |
| 5877 |
auto vp = getViewProvider(child); |
| 5878 |
if (!vp) { |
| 5879 |
continue; |
| 5880 |
} |
| 5881 |
parentMap[vp].push_back(v.second->viewObject); |
| 5882 |
} |
| 5883 |
} |
| 5884 |
|
| 5885 |
// now make sure all parent items are populated. In order to do that, we |
| 5886 |
// need to populate the oldest parent first |
| 5887 |
populateParents(&vpd, parentMap); |
| 5888 |
|
| 5889 |
DocumentObjectItem* first = nullptr; |
| 5890 |
FOREACH_ITEM(item, vpd); |
| 5891 |
if (showItem(item, true) && !first) { |
| 5892 |
first = item; |
| 5893 |
} |
| 5894 |
END_FOREACH_ITEM; |
| 5895 |
|
| 5896 |
getTree()->blockSelection(lock); |
| 5897 |
if (first) { |
| 5898 |
treeWidget()->scrollToItem(first); |
| 5899 |
// updateSelection(); // commented out - it was incorrectly deselecting |
| 5900 |
// ...newly selected items because their qt selection state wasn't |
| 5901 |
// ...yet synchronized when updateItemSelection() checked them |
| 5902 |
} |
| 5903 |
} |
| 5904 |
|
| 5905 |
bool DocumentItem::showHidden() const |
| 5906 |
{ |
| 5907 |
return pDocument->getDocument()->ShowHidden.getValue(); |
| 5908 |
} |
| 5909 |
|
| 5910 |
void DocumentItem::setShowHidden(bool show) |
| 5911 |
{ |
| 5912 |
pDocument->getDocument()->ShowHidden.setValue(show); |
| 5913 |
} |
| 5914 |
|
| 5915 |
bool DocumentItem::showItem(DocumentObjectItem* item, bool select, bool force) |
| 5916 |
{ |
| 5917 |
auto parent = item->parent(); |
| 5918 |
if (item->isHidden()) { |
| 5919 |
if (!force) { |
| 5920 |
return false; |
| 5921 |
} |
| 5922 |
item->setHidden(false); |
| 5923 |
} |
| 5924 |
|
| 5925 |
if (parent->type() == TreeWidget::ObjectType) { |
| 5926 |
if (!showItem(static_cast<DocumentObjectItem*>(parent), false, force)) { |
| 5927 |
return false; |
| 5928 |
} |
| 5929 |
auto pitem = static_cast<DocumentObjectItem*>(parent); |
| 5930 |
if (force || !pitem->object()->getObject()->testStatus(App::NoAutoExpand)) { |
| 5931 |
parent->setExpanded(true); |
| 5932 |
} |
| 5933 |
else if (!select) { |
| 5934 |
return false; |
| 5935 |
} |
| 5936 |
} |
| 5937 |
else { |
| 5938 |
parent->setExpanded(true); |
| 5939 |
} |
| 5940 |
|
| 5941 |
if (select) { |
| 5942 |
item->setSelected(true); |
| 5943 |
item->setCheckState(true); |
| 5944 |
} |
| 5945 |
return true; |
| 5946 |
} |
| 5947 |
|
| 5948 |
void DocumentItem::updateItemsVisibility(QTreeWidgetItem* item, bool show) |
| 5949 |
{ |
| 5950 |
if (item->type() == TreeWidget::ObjectType) { |
| 5951 |
auto objitem = static_cast<DocumentObjectItem*>(item); |
| 5952 |
objitem->setHidden(!show && !objitem->object()->showInTree()); |
| 5953 |
} |
| 5954 |
for (int i = 0; i < item->childCount(); ++i) { |
| 5955 |
updateItemsVisibility(item->child(i), show); |
| 5956 |
} |
| 5957 |
} |
| 5958 |
|
| 5959 |
void DocumentItem::updateSelection() |
| 5960 |
{ |
| 5961 |
bool lock = getTree()->blockSelection(true); |
| 5962 |
updateSelection(this, false); |
| 5963 |
getTree()->blockSelection(lock); |
| 5964 |
} |
| 5965 |
|
| 5966 |
// ---------------------------------------------------------------------------- |
| 5967 |
|
| 5968 |
static int countItems; |
| 5969 |
|
| 5970 |
DocumentObjectItem::DocumentObjectItem(DocumentItem* ownerDocItem, DocumentObjectDataPtr data) |
| 5971 |
: QTreeWidgetItem(TreeWidget::ObjectType) |
| 5972 |
, myOwner(ownerDocItem) |
| 5973 |
, myData(data) |
| 5974 |
, previousStatus(-1) |
| 5975 |
, selected(0) |
| 5976 |
, populated(false) |
| 5977 |
{ |
| 5978 |
setFlags(flags() | Qt::ItemIsEditable | Qt::ItemIsUserCheckable); |
| 5979 |
setCheckState(false); |
| 5980 |
|
| 5981 |
myData->insertItem(this); |
| 5982 |
++countItems; |
| 5983 |
TREE_LOG("Create item: " << countItems << ", " << object()->getObject()->getFullName()); |
| 5984 |
} |
| 5985 |
|
| 5986 |
DocumentObjectItem::~DocumentObjectItem() |
| 5987 |
{ |
| 5988 |
--countItems; |
| 5989 |
TREE_LOG("Delete item: " << countItems << ", " << object()->getObject()->getFullName()); |
| 5990 |
myData->removeItem(this); |
| 5991 |
|
| 5992 |
if (myData->rootItem == this) { |
| 5993 |
myData->rootItem = nullptr; |
| 5994 |
} |
| 5995 |
|
| 5996 |
if (myOwner && myData->items.empty()) { |
| 5997 |
auto it = myOwner->_ParentMap.find(object()->getObject()); |
| 5998 |
if (it != myOwner->_ParentMap.end() && !it->second.empty()) { |
| 5999 |
myOwner->PopulateObjects.push_back(*it->second.begin()); |
| 6000 |
myOwner->getTree()->_updateStatus(); |
| 6001 |
} |
| 6002 |
} |
| 6003 |
} |
| 6004 |
|
| 6005 |
void DocumentObjectItem::restoreBackground() |
| 6006 |
{ |
| 6007 |
this->setBackground(0, this->bgBrush); |
| 6008 |
} |
| 6009 |
|
| 6010 |
void DocumentObjectItem::setHighlight(bool set, Gui::HighlightMode high) |
| 6011 |
{ |
| 6012 |
QFont f = this->font(0); |
| 6013 |
auto highlight = [this, set](const QColor& col) { |
| 6014 |
if (set) { |
| 6015 |
this->setBackground(0, col); |
| 6016 |
} |
| 6017 |
else { |
| 6018 |
this->setBackground(0, QBrush()); |
| 6019 |
} |
| 6020 |
this->bgBrush = this->background(0); |
| 6021 |
}; |
| 6022 |
|
| 6023 |
switch (high) { |
| 6024 |
case HighlightMode::Bold: |
| 6025 |
f.setBold(set); |
| 6026 |
break; |
| 6027 |
case HighlightMode::Italic: |
| 6028 |
f.setItalic(set); |
| 6029 |
break; |
| 6030 |
case HighlightMode::Underlined: |
| 6031 |
f.setUnderline(set); |
| 6032 |
break; |
| 6033 |
case HighlightMode::Overlined: |
| 6034 |
f.setOverline(set); |
| 6035 |
break; |
| 6036 |
case HighlightMode::StrikeOut: |
| 6037 |
f.setStrikeOut(set); |
| 6038 |
break; |
| 6039 |
case HighlightMode::Blue: |
| 6040 |
highlight(QColor(200, 200, 255)); |
| 6041 |
break; |
| 6042 |
case HighlightMode::LightBlue: |
| 6043 |
highlight(QColor(230, 230, 255)); |
| 6044 |
break; |
| 6045 |
case HighlightMode::UserDefined: { |
| 6046 |
QColor color(230, 230, 255); |
| 6047 |
if (set) { |
| 6048 |
ParameterGrp::handle hGrp = App::GetApplication().GetParameterGroupByPath( |
| 6049 |
"User parameter:BaseApp/Preferences/TreeView" |
| 6050 |
); |
| 6051 |
bool bold = hGrp->GetBool("TreeActiveBold", true); |
| 6052 |
bool italic = hGrp->GetBool("TreeActiveItalic", false); |
| 6053 |
bool underlined = hGrp->GetBool("TreeActiveUnderlined", false); |
| 6054 |
bool overlined = hGrp->GetBool("TreeActiveOverlined", false); |
| 6055 |
f.setBold(bold); |
| 6056 |
f.setItalic(italic); |
| 6057 |
f.setUnderline(underlined); |
| 6058 |
f.setOverline(overlined); |
| 6059 |
|
| 6060 |
unsigned long col = hGrp->GetUnsigned("TreeActiveColor", 1538528255); |
| 6061 |
color = Base::Color::fromPackedRGB<QColor>(col); |
| 6062 |
} |
| 6063 |
else { |
| 6064 |
f.setBold(false); |
| 6065 |
f.setItalic(false); |
| 6066 |
f.setUnderline(false); |
| 6067 |
f.setOverline(false); |
| 6068 |
} |
| 6069 |
highlight(color); |
| 6070 |
} break; |
| 6071 |
default: |
| 6072 |
break; |
| 6073 |
} |
| 6074 |
this->setFont(0, f); |
| 6075 |
} |
| 6076 |
|
| 6077 |
const char* DocumentObjectItem::getTreeName() const |
| 6078 |
{ |
| 6079 |
return myData->getTreeName(); |
| 6080 |
} |
| 6081 |
|
| 6082 |
Gui::ViewProviderDocumentObject* DocumentObjectItem::object() const |
| 6083 |
{ |
| 6084 |
return myData->viewObject; |
| 6085 |
} |
| 6086 |
|
| 6087 |
void DocumentObjectItem::testStatus(bool resetStatus) |
| 6088 |
{ |
| 6089 |
QIcon icon, icon2; |
| 6090 |
testStatus(resetStatus, icon, icon2); |
| 6091 |
} |
| 6092 |
|
| 6093 |
namespace |
| 6094 |
{ |
| 6095 |
enum Status |
| 6096 |
{ |
| 6097 |
Visible = 1 << 0, |
| 6098 |
Recompute = 1 << 1, |
| 6099 |
Error = 1 << 2, |
| 6100 |
Hidden = 1 << 3, |
| 6101 |
External = 1 << 4, |
| 6102 |
Freezed = 1 << 5 |
| 6103 |
}; |
| 6104 |
} |
| 6105 |
|
| 6106 |
void DocumentObjectItem::testStatus(bool resetStatus, QIcon& icon1, QIcon& icon2) |
| 6107 |
{ |
| 6108 |
// guard against calling this during destruction when tree widget may be nullptr |
| 6109 |
if (!treeWidget()) { |
| 6110 |
return; |
| 6111 |
} |
| 6112 |
|
| 6113 |
App::DocumentObject* pObject = object()->getObject(); |
| 6114 |
|
| 6115 |
int visible = -1; |
| 6116 |
auto parentItem = getParentItem(); |
| 6117 |
if (parentItem) { |
| 6118 |
Timing(testStatus1); |
| 6119 |
auto parent = parentItem->object()->getObject(); |
| 6120 |
auto ext = parent->getExtensionByType<App::GroupExtension>(true, false); |
| 6121 |
if (!ext) { |
| 6122 |
visible = parent->isElementVisible(pObject->getNameInDocument()); |
| 6123 |
} |
| 6124 |
else { |
| 6125 |
// We are dealing with a plain group. It has special handling when |
| 6126 |
// linked, which allows it to have indpenedent visibility control. |
| 6127 |
// We need to go up the hierarchy and see if there is any link to |
| 6128 |
// it. |
| 6129 |
for (auto pp = parentItem->getParentItem(); pp; pp = pp->getParentItem()) { |
| 6130 |
auto obj = pp->object()->getObject(); |
| 6131 |
if (!obj->hasExtension(App::GroupExtension::getExtensionClassTypeId(), false)) { |
| 6132 |
visible = pp->object()->getObject()->isElementVisible(pObject->getNameInDocument()); |
| 6133 |
break; |
| 6134 |
} |
| 6135 |
} |
| 6136 |
} |
| 6137 |
} |
| 6138 |
|
| 6139 |
Timing(testStatus2); |
| 6140 |
|
| 6141 |
if (visible < 0) { |
| 6142 |
visible = object()->isShow() ? 1 : 0; |
| 6143 |
} |
| 6144 |
|
| 6145 |
auto obj = object()->getObject(); |
| 6146 |
auto linked = obj->getLinkedObject(false); |
| 6147 |
bool external = object()->getDocument() != getOwnerDocument()->document() |
| 6148 |
|| (linked && linked->getDocument() != obj->getDocument()); |
| 6149 |
bool freezed = pObject->isFreezed(); |
| 6150 |
|
| 6151 |
int currentStatus = ((freezed ? 1 : 0) << 5) | ((external ? 1 : 0) << 4) |
| 6152 |
| ((object()->showInTree() ? 0 : 1) << 3) | ((pObject->isError() ? 1 : 0) << 2) |
| 6153 |
| ((pObject->isTouched() || pObject->mustExecute() == 1 ? 1 : 0) << 1) | (visible ? 1 : 0); |
| 6154 |
|
| 6155 |
TimingStop(testStatus2); |
| 6156 |
|
| 6157 |
if (!resetStatus && previousStatus == currentStatus) { |
| 6158 |
return; |
| 6159 |
} |
| 6160 |
|
| 6161 |
_Timing(1, testStatus3); |
| 6162 |
|
| 6163 |
previousStatus = currentStatus; |
| 6164 |
|
| 6165 |
QIcon::Mode mode = QIcon::Normal; |
| 6166 |
if (currentStatus & Status::Visible) { |
| 6167 |
// Note: By default the foreground, i.e. text color is invalid |
| 6168 |
// to make use of the default color of the tree widget's palette. |
| 6169 |
// If we temporarily set this color to dark and reset to an invalid |
| 6170 |
// color again we cannot do it with setTextColor() or setForeground(), |
| 6171 |
// respectively, because for any reason the color would always switch |
| 6172 |
// to black which will lead to unreadable text if the system background |
| 6173 |
// hss already a dark color. |
| 6174 |
// However, it works if we set the appropriate role to an empty QVariant(). |
| 6175 |
for (int column = 0; column < this->columnCount(); ++column) { |
| 6176 |
this->setData(column, Qt::ForegroundRole, QVariant()); |
| 6177 |
} |
| 6178 |
} |
| 6179 |
else { // invisible |
| 6180 |
QStyleOptionViewItem opt; |
| 6181 |
// it can happen that a tree item is not attached to the tree widget (#0003025) |
| 6182 |
if (this->treeWidget()) { |
| 6183 |
opt.initFrom(this->treeWidget()); |
| 6184 |
} |
| 6185 |
for (int column = 0; column < this->columnCount(); ++column) { |
| 6186 |
this->setForeground(column, opt.palette.color(QPalette::Disabled, QPalette::Text)); |
| 6187 |
} |
| 6188 |
mode = QIcon::Disabled; |
| 6189 |
} |
| 6190 |
|
| 6191 |
_TimingStop(1, testStatus3); |
| 6192 |
|
| 6193 |
QIcon& icon = mode == QIcon::Normal ? icon1 : icon2; |
| 6194 |
|
| 6195 |
if (icon.isNull()) { |
| 6196 |
Timing(getIcon); |
| 6197 |
QPixmap px; |
| 6198 |
if (currentStatus & Status::Error) { |
| 6199 |
static QPixmap pxError; |
| 6200 |
if (pxError.isNull()) { |
| 6201 |
// object is in error state |
| 6202 |
pxError = Gui::BitmapFactory().pixmapFromSvg("overlay_error", QSize(10, 10)); |
| 6203 |
} |
| 6204 |
px = pxError; |
| 6205 |
} |
| 6206 |
else if (currentStatus & Status::Recompute) { |
| 6207 |
static QPixmap pxRecompute; |
| 6208 |
if (pxRecompute.isNull()) { |
| 6209 |
// object must be recomputed |
| 6210 |
pxRecompute = Gui::BitmapFactory().pixmapFromSvg("overlay_recompute", QSize(10, 10)); |
| 6211 |
} |
| 6212 |
px = pxRecompute; |
| 6213 |
} |
| 6214 |
|
| 6215 |
// get the original icon set |
| 6216 |
QIcon icon_org = object()->getIcon(); |
| 6217 |
|
| 6218 |
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) |
| 6219 |
int w = getTree()->viewOptions().decorationSize.width(); |
| 6220 |
#else |
| 6221 |
QStyleOptionViewItem opt; |
| 6222 |
getTree()->initViewItemOption(&opt); |
| 6223 |
int w = opt.decorationSize.width(); |
| 6224 |
#endif |
| 6225 |
|
| 6226 |
QPixmap pxOn, pxOff; |
| 6227 |
|
| 6228 |
// if needed show small pixmap inside |
| 6229 |
if (!px.isNull()) { |
| 6230 |
pxOff = BitmapFactory().merge( |
| 6231 |
icon_org.pixmap(w, w, mode, QIcon::Off), |
| 6232 |
px, |
| 6233 |
BitmapFactoryInst::TopRight |
| 6234 |
); |
| 6235 |
pxOn = BitmapFactory().merge( |
| 6236 |
icon_org.pixmap(w, w, mode, QIcon::On), |
| 6237 |
px, |
| 6238 |
BitmapFactoryInst::TopRight |
| 6239 |
); |
| 6240 |
} |
| 6241 |
else { |
| 6242 |
pxOff = icon_org.pixmap(w, w, mode, QIcon::Off); |
| 6243 |
pxOn = icon_org.pixmap(w, w, mode, QIcon::On); |
| 6244 |
} |
| 6245 |
|
| 6246 |
if (currentStatus & Status::Hidden) { |
| 6247 |
static QPixmap pxHidden; |
| 6248 |
if (pxHidden.isNull()) { |
| 6249 |
pxHidden = Gui::BitmapFactory().pixmapFromSvg("TreeItemVisible", QSize(10, 10)); |
| 6250 |
} |
| 6251 |
pxOff = BitmapFactory().merge(pxOff, pxHidden, BitmapFactoryInst::TopLeft); |
| 6252 |
pxOn = BitmapFactory().merge(pxOn, pxHidden, BitmapFactoryInst::TopLeft); |
| 6253 |
} |
| 6254 |
|
| 6255 |
if (currentStatus & Status::External) { |
| 6256 |
static QPixmap pxExternal; |
| 6257 |
constexpr int px = 12; |
| 6258 |
if (pxExternal.isNull()) { |
| 6259 |
pxExternal = Gui::BitmapFactory().pixmapFromSvg("LinkOverlay", QSize(px, px)); |
| 6260 |
} |
| 6261 |
pxOff = BitmapFactory().merge(pxOff, pxExternal, BitmapFactoryInst::BottomRight); |
| 6262 |
pxOn = BitmapFactory().merge(pxOn, pxExternal, BitmapFactoryInst::BottomRight); |
| 6263 |
} |
| 6264 |
|
| 6265 |
if (currentStatus & Status::Freezed) { |
| 6266 |
static QPixmap pxFreeze; |
| 6267 |
if (pxFreeze.isNull()) { |
| 6268 |
// object is in freezed state |
| 6269 |
pxFreeze = Gui::BitmapFactory().pixmapFromSvg("Std_ToggleFreeze", QSize(16, 16)); |
| 6270 |
} |
| 6271 |
pxOff = BitmapFactory().merge(pxOff, pxFreeze, BitmapFactoryInst::TopLeft); |
| 6272 |
pxOn = BitmapFactory().merge(pxOn, pxFreeze, BitmapFactoryInst::TopLeft); |
| 6273 |
} |
| 6274 |
|
| 6275 |
icon.addPixmap(pxOn, QIcon::Normal, QIcon::On); |
| 6276 |
icon.addPixmap(pxOff, QIcon::Normal, QIcon::Off); |
| 6277 |
|
| 6278 |
icon = object()->mergeColorfulOverlayIcons(icon); |
| 6279 |
|
| 6280 |
if (isVisibilityIconEnabled()) { |
| 6281 |
static QPixmap pxVisible, pxInvisible; |
| 6282 |
if (pxVisible.isNull()) { |
| 6283 |
pxVisible = BitmapFactory().pixmap("TreeItemVisible"); |
| 6284 |
} |
| 6285 |
if (pxInvisible.isNull()) { |
| 6286 |
pxInvisible = BitmapFactory().pixmap("TreeItemInvisible"); |
| 6287 |
} |
| 6288 |
|
| 6289 |
// Prepend the visibility pixmap to the final icon pixmaps and use these as the icon. |
| 6290 |
QIcon new_icon; |
| 6291 |
auto style = this->getTree()->style(); |
| 6292 |
int const spacing = style->pixelMetric(QStyle::PM_LayoutHorizontalSpacing); |
| 6293 |
for (auto state : {QIcon::On, QIcon::Off}) { |
| 6294 |
QPixmap px_org = icon.pixmap(0xFFFF, 0xFFFF, QIcon::Normal, state); |
| 6295 |
|
| 6296 |
QPixmap px(2 * px_org.width() + spacing, px_org.height()); |
| 6297 |
px.fill(Qt::transparent); |
| 6298 |
|
| 6299 |
QPainter pt; |
| 6300 |
pt.begin(&px); |
| 6301 |
pt.setPen(Qt::NoPen); |
| 6302 |
if (object()->canToggleVisibility()) { |
| 6303 |
pt.drawPixmap( |
| 6304 |
0, |
| 6305 |
0, |
| 6306 |
px_org.width(), |
| 6307 |
px_org.height(), |
| 6308 |
(currentStatus & Status::Visible) ? pxVisible : pxInvisible |
| 6309 |
); |
| 6310 |
} |
| 6311 |
pt.drawPixmap(px_org.width() + spacing, 0, px_org.width(), px_org.height(), px_org); |
| 6312 |
pt.end(); |
| 6313 |
|
| 6314 |
new_icon.addPixmap(px, QIcon::Normal, state); |
| 6315 |
} |
| 6316 |
icon = new_icon; |
| 6317 |
} |
| 6318 |
} |
| 6319 |
|
| 6320 |
_Timing(2, setIcon); |
| 6321 |
this->setIcon(0, icon); |
| 6322 |
} |
| 6323 |
|
| 6324 |
void DocumentObjectItem::displayStatusInfo() |
| 6325 |
{ |
| 6326 |
App::DocumentObject* Obj = object()->getObject(); |
| 6327 |
|
| 6328 |
QString info = QApplication::translate(Obj->getTypeId().getName(), Obj->getStatusString()); |
| 6329 |
|
| 6330 |
if (Obj->mustExecute() == 1 && !Obj->isError()) { |
| 6331 |
info += TreeWidget::tr(" (but must be executed)"); |
| 6332 |
} |
| 6333 |
|
| 6334 |
QString status = TreeWidget::tr("%1, Internal name: %2") |
| 6335 |
.arg(info, QString::fromLatin1(Obj->getNameInDocument())); |
| 6336 |
|
| 6337 |
if (!Obj->isError()) { |
| 6338 |
getMainWindow()->showMessage(status); |
| 6339 |
} |
| 6340 |
else { |
| 6341 |
getMainWindow()->showStatus(MainWindow::Err, status); |
| 6342 |
QTreeWidget* tree = this->treeWidget(); |
| 6343 |
QPoint pos = tree->visualItemRect(this).topRight(); |
| 6344 |
QToolTip::showText(tree->mapToGlobal(pos), info); |
| 6345 |
} |
| 6346 |
} |
| 6347 |
|
| 6348 |
void DocumentObjectItem::setExpandedStatus(bool on) |
| 6349 |
{ |
| 6350 |
if (getOwnerDocument()->document() == object()->getDocument()) { |
| 6351 |
object()->getObject()->setStatus(App::Expand, on); |
| 6352 |
} |
| 6353 |
} |
| 6354 |
|
| 6355 |
void DocumentObjectItem::setData(int column, int role, const QVariant& value) |
| 6356 |
{ |
| 6357 |
QVariant myValue(value); |
| 6358 |
if (role == Qt::EditRole && column <= 1) { |
| 6359 |
auto obj = object()->getObject(); |
| 6360 |
auto& label = column ? obj->Label2 : obj->Label; |
| 6361 |
|
| 6362 |
std::ostringstream str; |
| 6363 |
str << TreeWidget::tr("Rename").toStdString() << ' ' << getName() << '.' << label.getName(); |
| 6364 |
|
| 6365 |
// Explicitly open and commit a transaction since this is a single change here |
| 6366 |
// For more details: https://forum.freecad.org/viewtopic.php?f=3&t=72351 |
| 6367 |
App::Document* doc = obj->getDocument(); |
| 6368 |
doc->openTransaction(str.str().c_str()); |
| 6369 |
label.setValue(value.toString().toUtf8().constData()); |
| 6370 |
doc->commitTransaction(); |
| 6371 |
|
| 6372 |
myValue = QString::fromUtf8(label.getValue()); |
| 6373 |
} |
| 6374 |
QTreeWidgetItem::setData(column, role, myValue); |
| 6375 |
} |
| 6376 |
|
| 6377 |
bool DocumentObjectItem::isChildOfItem(DocumentObjectItem* item) |
| 6378 |
{ |
| 6379 |
for (auto pitem = parent(); pitem; pitem = pitem->parent()) { |
| 6380 |
if (pitem == item) { |
| 6381 |
return true; |
| 6382 |
} |
| 6383 |
} |
| 6384 |
return false; |
| 6385 |
} |
| 6386 |
|
| 6387 |
bool DocumentObjectItem::requiredAtRoot(bool excludeSelf) const |
| 6388 |
{ |
| 6389 |
if (myData->rootItem || object()->getDocument() != getOwnerDocument()->document()) { |
| 6390 |
return false; |
| 6391 |
} |
| 6392 |
bool checkMap = true; |
| 6393 |
for (auto item : myData->items) { |
| 6394 |
if (excludeSelf && item == this) { |
| 6395 |
continue; |
| 6396 |
} |
| 6397 |
auto pi = item->getParentItem(); |
| 6398 |
if (!pi || pi->myData->removeChildrenFromRoot) { |
| 6399 |
return false; |
| 6400 |
} |
| 6401 |
checkMap = false; |
| 6402 |
} |
| 6403 |
if (checkMap && myOwner) { |
| 6404 |
auto it = myOwner->_ParentMap.find(object()->getObject()); |
| 6405 |
if (it != myOwner->_ParentMap.end()) { |
| 6406 |
// Reaching here means all items of this corresponding object is |
| 6407 |
// going to be deleted, but the object itself is not deleted and |
| 6408 |
// still being referred to by some parent item that is not expanded |
| 6409 |
// yet. So, we force populate at least one item of the parent |
| 6410 |
// object to make sure that there is at least one corresponding |
| 6411 |
// item for each object. |
| 6412 |
// |
| 6413 |
// PS: practically speaking, it won't hurt much to delete all the |
| 6414 |
// items, because the item will be auto created once the user |
| 6415 |
// expand its parent item. It only causes minor problems, such as, |
| 6416 |
// tree scroll to object command won't work properly. |
| 6417 |
|
| 6418 |
for (auto parent : it->second) { |
| 6419 |
if (getOwnerDocument()->populateObject(parent)) { |
| 6420 |
return false; |
| 6421 |
} |
| 6422 |
} |
| 6423 |
} |
| 6424 |
} |
| 6425 |
return true; |
| 6426 |
} |
| 6427 |
|
| 6428 |
bool DocumentObjectItem::isLink() const |
| 6429 |
{ |
| 6430 |
auto obj = object()->getObject(); |
| 6431 |
auto linked = obj->getLinkedObject(false); |
| 6432 |
return linked && obj != linked; |
| 6433 |
} |
| 6434 |
|
| 6435 |
bool DocumentObjectItem::isLinkFinal() const |
| 6436 |
{ |
| 6437 |
auto obj = object()->getObject(); |
| 6438 |
auto linked = obj->getLinkedObject(false); |
| 6439 |
return linked && linked == linked->getLinkedObject(true); |
| 6440 |
} |
| 6441 |
|
| 6442 |
|
| 6443 |
bool DocumentObjectItem::isParentLink() const |
| 6444 |
{ |
| 6445 |
auto pi = getParentItem(); |
| 6446 |
return pi && pi->isLink(); |
| 6447 |
} |
| 6448 |
|
| 6449 |
enum GroupType |
| 6450 |
{ |
| 6451 |
NotGroup = 0, |
| 6452 |
LinkGroup = 1, |
| 6453 |
PartGroup = 2, |
| 6454 |
SuperGroup = 3, // reversed for future |
| 6455 |
}; |
| 6456 |
|
| 6457 |
int DocumentObjectItem::isGroup() const |
| 6458 |
{ |
| 6459 |
auto obj = object()->getObject(); |
| 6460 |
auto linked = obj->getLinkedObject(true); |
| 6461 |
if (linked && linked->hasExtension(App::GeoFeatureGroupExtension::getExtensionClassTypeId())) { |
| 6462 |
return PartGroup; |
| 6463 |
} |
| 6464 |
if (obj->hasChildElement()) { |
| 6465 |
return LinkGroup; |
| 6466 |
} |
| 6467 |
if (obj->hasExtension(App::GroupExtension::getExtensionClassTypeId(), false)) { |
| 6468 |
for (auto parent = getParentItem(); parent; parent = parent->getParentItem()) { |
| 6469 |
auto pobj = parent->object()->getObject(); |
| 6470 |
if (pobj->hasExtension(App::GroupExtension::getExtensionClassTypeId(), false)) { |
| 6471 |
continue; |
| 6472 |
} |
| 6473 |
if (pobj->isElementVisible(obj->getNameInDocument()) >= 0) { |
| 6474 |
return LinkGroup; |
| 6475 |
} |
| 6476 |
} |
| 6477 |
} |
| 6478 |
return NotGroup; |
| 6479 |
} |
| 6480 |
|
| 6481 |
bool DocumentItem::isObjectShowable(App::DocumentObject* obj) |
| 6482 |
{ |
| 6483 |
auto itParents = _ParentMap.find(obj); |
| 6484 |
if (itParents == _ParentMap.end() || itParents->second.empty()) { |
| 6485 |
return true; |
| 6486 |
} |
| 6487 |
bool showable = true; |
| 6488 |
for (auto parent : itParents->second) { |
| 6489 |
if (parent->getDocument() != obj->getDocument()) { |
| 6490 |
continue; |
| 6491 |
} |
| 6492 |
if (!parent->hasChildElement() && parent->getLinkedObject(false) == parent) { |
| 6493 |
return true; |
| 6494 |
} |
| 6495 |
showable = false; |
| 6496 |
} |
| 6497 |
return showable; |
| 6498 |
} |
| 6499 |
|
| 6500 |
int DocumentObjectItem::isParentGroup() const |
| 6501 |
{ |
| 6502 |
auto pi = getParentItem(); |
| 6503 |
return pi ? pi->isGroup() : 0; |
| 6504 |
} |
| 6505 |
|
| 6506 |
DocumentObjectItem* DocumentObjectItem::getParentItem() const |
| 6507 |
{ |
| 6508 |
if (parent()->type() != TreeWidget::ObjectType) { |
| 6509 |
return nullptr; |
| 6510 |
} |
| 6511 |
return static_cast<DocumentObjectItem*>(parent()); |
| 6512 |
} |
| 6513 |
|
| 6514 |
DocumentObjectItem* DocumentObjectItem::getNextSibling() const |
| 6515 |
{ |
| 6516 |
QTreeWidgetItem* parent = this->parent(); |
| 6517 |
if (parent) { |
| 6518 |
int index = parent->indexOfChild(const_cast<DocumentObjectItem*>(this)); |
| 6519 |
if (index >= 0) { |
| 6520 |
while (++index < parent->childCount()) { |
| 6521 |
QTreeWidgetItem* sibling = parent->child(index); |
| 6522 |
if (sibling->type() == TreeWidget::ObjectType) { |
| 6523 |
return static_cast<DocumentObjectItem*>(sibling); |
| 6524 |
} |
| 6525 |
} |
| 6526 |
} |
| 6527 |
} |
| 6528 |
|
| 6529 |
return nullptr; |
| 6530 |
} |
| 6531 |
|
| 6532 |
DocumentObjectItem* DocumentObjectItem::getPreviousSibling() const |
| 6533 |
{ |
| 6534 |
QTreeWidgetItem* parent = this->parent(); |
| 6535 |
if (parent) { |
| 6536 |
int index = parent->indexOfChild(const_cast<DocumentObjectItem*>(this)); |
| 6537 |
while (index > 0) { |
| 6538 |
QTreeWidgetItem* sibling = parent->child(--index); |
| 6539 |
if (sibling->type() == TreeWidget::ObjectType) { |
| 6540 |
return static_cast<DocumentObjectItem*>(sibling); |
| 6541 |
} |
| 6542 |
} |
| 6543 |
} |
| 6544 |
|
| 6545 |
return nullptr; |
| 6546 |
} |
| 6547 |
|
| 6548 |
const char* DocumentObjectItem::getName() const |
| 6549 |
{ |
| 6550 |
const char* name = object()->getObject()->getNameInDocument(); |
| 6551 |
return name ? name : ""; |
| 6552 |
} |
| 6553 |
|
| 6554 |
int DocumentObjectItem::getSubName(std::ostringstream& str, App::DocumentObject*& topParent) const |
| 6555 |
{ |
| 6556 |
auto parent = getParentItem(); |
| 6557 |
if (!parent) { |
| 6558 |
return NotGroup; |
| 6559 |
} |
| 6560 |
int ret = parent->getSubName(str, topParent); |
| 6561 |
if (ret != SuperGroup) { |
| 6562 |
int group = parent->isGroup(); |
| 6563 |
if (group == NotGroup) { |
| 6564 |
if (ret != PartGroup) { |
| 6565 |
// Handle this situation, |
| 6566 |
// |
| 6567 |
// LinkGroup |
| 6568 |
// |--PartExtrude |
| 6569 |
// |--Sketch |
| 6570 |
// |
| 6571 |
// This function traverse from top down, so, when seeing a |
| 6572 |
// non-group object 'PartExtrude', its following children should |
| 6573 |
// not be grouped, so must reset any previous parents here. |
| 6574 |
topParent = nullptr; |
| 6575 |
str.str(""); // reset the current subname |
| 6576 |
return NotGroup; |
| 6577 |
} |
| 6578 |
group = PartGroup; |
| 6579 |
} |
| 6580 |
ret = group; |
| 6581 |
} |
| 6582 |
|
| 6583 |
auto obj = parent->object()->getObject(); |
| 6584 |
if (!obj || !obj->isAttachedToDocument()) { |
| 6585 |
topParent = nullptr; |
| 6586 |
str.str(""); |
| 6587 |
return NotGroup; |
| 6588 |
} |
| 6589 |
if (!topParent) { |
| 6590 |
topParent = obj; |
| 6591 |
} |
| 6592 |
else if (!obj->redirectSubName(str, topParent, object()->getObject())) { |
| 6593 |
str << obj->getNameInDocument() << '.'; |
| 6594 |
} |
| 6595 |
return ret; |
| 6596 |
} |
| 6597 |
|
| 6598 |
App::DocumentObject* DocumentObjectItem::getFullSubName( |
| 6599 |
std::ostringstream& str, |
| 6600 |
DocumentObjectItem* parent |
| 6601 |
) const |
| 6602 |
{ |
| 6603 |
auto pi = getParentItem(); |
| 6604 |
if (this == parent || !pi || (!parent && !pi->isGroup())) { |
| 6605 |
return object()->getObject(); |
| 6606 |
} |
| 6607 |
|
| 6608 |
auto ret = pi->getFullSubName(str, parent); |
| 6609 |
str << getName() << '.'; |
| 6610 |
return ret; |
| 6611 |
} |
| 6612 |
|
| 6613 |
App::DocumentObject* DocumentObjectItem::getRelativeParent( |
| 6614 |
std::ostringstream& str, |
| 6615 |
DocumentObjectItem* cousin, |
| 6616 |
App::DocumentObject** topParent, |
| 6617 |
std::string* topSubname |
| 6618 |
) const |
| 6619 |
{ |
| 6620 |
std::ostringstream str2; |
| 6621 |
App::DocumentObject *top = nullptr, *top2 = nullptr; |
| 6622 |
getSubName(str, top); |
| 6623 |
if (topParent) { |
| 6624 |
*topParent = top; |
| 6625 |
} |
| 6626 |
if (!top) { |
| 6627 |
return nullptr; |
| 6628 |
} |
| 6629 |
if (topSubname) { |
| 6630 |
*topSubname = str.str() + getName() + '.'; |
| 6631 |
} |
| 6632 |
cousin->getSubName(str2, top2); |
| 6633 |
if (top != top2) { |
| 6634 |
str << getName() << '.'; |
| 6635 |
return top; |
| 6636 |
} |
| 6637 |
|
| 6638 |
auto subname = str.str(); |
| 6639 |
auto subname2 = str2.str(); |
| 6640 |
const char* sub = subname.c_str(); |
| 6641 |
const char* sub2 = subname2.c_str(); |
| 6642 |
while (true) { |
| 6643 |
const char* dot = strchr(sub, '.'); |
| 6644 |
if (!dot) { |
| 6645 |
str.str(""); |
| 6646 |
return nullptr; |
| 6647 |
} |
| 6648 |
const char* dot2 = strchr(sub2, '.'); |
| 6649 |
if (!dot2 || dot - sub != dot2 - sub2 || strncmp(sub, sub2, dot - sub) != 0) { |
| 6650 |
auto substr = subname.substr(0, dot - subname.c_str() + 1); |
| 6651 |
auto ret = top->getSubObject(substr.c_str()); |
| 6652 |
if (!top) { |
| 6653 |
FC_ERR("invalid subname " << top->getFullName() << '.' << substr); |
| 6654 |
str.str(""); |
| 6655 |
return nullptr; |
| 6656 |
} |
| 6657 |
str.str(""); |
| 6658 |
str << dot + 1 << getName() << '.'; |
| 6659 |
return ret; |
| 6660 |
} |
| 6661 |
sub = dot + 1; |
| 6662 |
sub2 = dot2 + 1; |
| 6663 |
} |
| 6664 |
str.str(""); |
| 6665 |
return nullptr; |
| 6666 |
} |
| 6667 |
|
| 6668 |
void DocumentObjectItem::setCheckState(bool checked) |
| 6669 |
{ |
| 6670 |
if (isSelectionCheckBoxesEnabled()) { |
| 6671 |
QTreeWidgetItem::setCheckState(0, checked ? Qt::Checked : Qt::Unchecked); |
| 6672 |
} |
| 6673 |
else { |
| 6674 |
setData(0, Qt::CheckStateRole, QVariant()); |
| 6675 |
} |
| 6676 |
} |
| 6677 |
|
| 6678 |
DocumentItem* DocumentObjectItem::getParentDocument() const |
| 6679 |
{ |
| 6680 |
return getTree()->getDocumentItem(object()->getDocument()); |
| 6681 |
} |
| 6682 |
|
| 6683 |
DocumentItem* DocumentObjectItem::getOwnerDocument() const |
| 6684 |
{ |
| 6685 |
return myOwner; |
| 6686 |
} |
| 6687 |
|
| 6688 |
TreeWidget* DocumentObjectItem::getTree() const |
| 6689 |
{ |
| 6690 |
return static_cast<TreeWidget*>(treeWidget()); |
| 6691 |
} |
| 6692 |
|
| 6693 |
void DocumentObjectItem::getExpandedSnapshot(std::vector<bool>& snapshot) const |
| 6694 |
{ |
| 6695 |
snapshot.push_back(isExpanded()); |
| 6696 |
|
| 6697 |
for (int i = 0; i < childCount(); ++i) { |
| 6698 |
static_cast<const DocumentObjectItem*>(child(i))->getExpandedSnapshot(snapshot); |
| 6699 |
} |
| 6700 |
} |
| 6701 |
|
| 6702 |
void DocumentObjectItem::applyExpandedSnapshot( |
| 6703 |
const std::vector<bool>& snapshot, |
| 6704 |
std::vector<bool>::const_iterator& from |
| 6705 |
) |
| 6706 |
{ |
| 6707 |
setExpanded(*from++); |
| 6708 |
|
| 6709 |
for (int i = 0; i < childCount(); ++i) { |
| 6710 |
static_cast<DocumentObjectItem*>(child(i))->applyExpandedSnapshot(snapshot, from); |
| 6711 |
} |
| 6712 |
} |
| 6713 |
|
| 6714 |
#include "moc_Tree.cpp" |