| 1 |
/**************************************************************************** |
| 2 |
* Copyright (c) 2017 Zheng Lei (realthunder) <[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 |
#include <sstream> |
| 24 |
#include <QMessageBox> |
| 25 |
|
| 26 |
#include <App/Application.h> |
| 27 |
#include <App/ElementNamingUtils.h> |
| 28 |
#include <App/Document.h> |
| 29 |
#include <App/DocumentObject.h> |
| 30 |
#include <App/Link.h> |
| 31 |
#include <Base/Exception.h> |
| 32 |
#include <Base/Tools.h> |
| 33 |
|
| 34 |
#include "Action.h" |
| 35 |
#include "Application.h" |
| 36 |
#include "Command.h" |
| 37 |
#include "Document.h" |
| 38 |
#include "MainWindow.h" |
| 39 |
#include "Selection.h" |
| 40 |
#include "Tree.h" |
| 41 |
#include "ViewProviderDocumentObject.h" |
| 42 |
#include "WaitCursor.h" |
| 43 |
|
| 44 |
|
| 45 |
FC_LOG_LEVEL_INIT("CommandLink", true, true) |
| 46 |
|
| 47 |
using namespace Gui; |
| 48 |
|
| 49 |
static void setLinkLabel(App::DocumentObject* obj, const char* doc, const char* name) |
| 50 |
{ |
| 51 |
std::string label = obj->Label.getValue(); |
| 52 |
label = Base::Tools::escapeEncodeString(label); |
| 53 |
Command::doCommand( |
| 54 |
Command::Doc, |
| 55 |
"App.getDocument('%s').getObject('%s').Label='%s'", |
| 56 |
doc, |
| 57 |
name, |
| 58 |
label.c_str() |
| 59 |
); |
| 60 |
} |
| 61 |
|
| 62 |
//////////////////////////////////////////////////////////////////////////////////////////// |
| 63 |
|
| 64 |
class StdCmdLinkMakeGroup: public Gui::Command |
| 65 |
{ |
| 66 |
public: |
| 67 |
StdCmdLinkMakeGroup(); |
| 68 |
const char* className() const override |
| 69 |
{ |
| 70 |
return "StdCmdLinkMakeGroup"; |
| 71 |
} |
| 72 |
|
| 73 |
protected: |
| 74 |
void activated(int iMsg) override; |
| 75 |
bool isActive() override; |
| 76 |
Action* createAction() override; |
| 77 |
void languageChange() override; |
| 78 |
}; |
| 79 |
|
| 80 |
StdCmdLinkMakeGroup::StdCmdLinkMakeGroup() |
| 81 |
: Command("Std_LinkMakeGroup") |
| 82 |
{ |
| 83 |
sGroup = "Link"; |
| 84 |
sMenuText = QT_TR_NOOP("Link Group"); |
| 85 |
sToolTipText = QT_TR_NOOP("Creates a group of links"); |
| 86 |
sWhatsThis = "Std_LinkMakeGroup"; |
| 87 |
sStatusTip = sToolTipText; |
| 88 |
eType = AlterDoc; |
| 89 |
sPixmap = "LinkGroup"; |
| 90 |
} |
| 91 |
|
| 92 |
bool StdCmdLinkMakeGroup::isActive() |
| 93 |
{ |
| 94 |
return !!App::GetApplication().getActiveDocument(); |
| 95 |
} |
| 96 |
|
| 97 |
Action* StdCmdLinkMakeGroup::createAction() |
| 98 |
{ |
| 99 |
auto pcAction = new ActionGroup(this, getMainWindow()); |
| 100 |
pcAction->setDropDownMenu(true); |
| 101 |
applyCommandData(this->className(), pcAction); |
| 102 |
|
| 103 |
// add the action items |
| 104 |
QAction* action = nullptr; |
| 105 |
action = pcAction->addAction(QObject::tr("Simple Group")); |
| 106 |
action->setWhatsThis(QString::fromLatin1(getWhatsThis())); |
| 107 |
action = pcAction->addAction(QObject::tr("Group With Links")); |
| 108 |
action->setWhatsThis(QString::fromLatin1(getWhatsThis())); |
| 109 |
action = pcAction->addAction(QObject::tr("Group With Transform Links")); |
| 110 |
action->setWhatsThis(QString::fromLatin1(getWhatsThis())); |
| 111 |
return pcAction; |
| 112 |
} |
| 113 |
|
| 114 |
void StdCmdLinkMakeGroup::languageChange() |
| 115 |
{ |
| 116 |
Command::languageChange(); |
| 117 |
|
| 118 |
if (!_pcAction) { |
| 119 |
return; |
| 120 |
} |
| 121 |
auto pcAction = qobject_cast<ActionGroup*>(_pcAction); |
| 122 |
QList<QAction*> acts = pcAction->actions(); |
| 123 |
acts[0]->setText(QObject::tr("Simple Group")); |
| 124 |
acts[1]->setText(QObject::tr("Group With Links")); |
| 125 |
acts[2]->setText(QObject::tr("Group With Transform Links")); |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
void StdCmdLinkMakeGroup::activated(int option) |
| 130 |
{ |
| 131 |
|
| 132 |
std::vector<App::DocumentObject*> objs; |
| 133 |
std::set<App::DocumentObject*> objset; |
| 134 |
|
| 135 |
auto doc = App::GetApplication().getActiveDocument(); |
| 136 |
if (!doc) { |
| 137 |
FC_ERR("no active document"); |
| 138 |
return; |
| 139 |
} |
| 140 |
|
| 141 |
for (auto& sel : Selection().getCompleteSelection()) { |
| 142 |
if (sel.pObject && sel.pObject->isAttachedToDocument() && objset.insert(sel.pObject).second) { |
| 143 |
objs.push_back(sel.pObject); |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
Selection().selStackPush(); |
| 148 |
Selection().clearCompleteSelection(); |
| 149 |
|
| 150 |
Command::openCommand(QT_TRANSLATE_NOOP("Command", "Make link group")); |
| 151 |
try { |
| 152 |
std::string groupName = doc->getUniqueObjectName("LinkGroup"); |
| 153 |
Command::doCommand( |
| 154 |
Command::Doc, |
| 155 |
"App.getDocument('%s').addObject('App::LinkGroup','%s')", |
| 156 |
doc->getName(), |
| 157 |
groupName.c_str() |
| 158 |
); |
| 159 |
if (objs.empty()) { |
| 160 |
Selection().addSelection(doc->getName(), groupName.c_str()); |
| 161 |
Selection().selStackPush(); |
| 162 |
} |
| 163 |
else { |
| 164 |
Command::doCommand(Command::Doc, "__objs__ = []"); |
| 165 |
for (auto obj : objs) { |
| 166 |
std::string name; |
| 167 |
if (option != 0 || doc != obj->getDocument()) { |
| 168 |
name = doc->getUniqueObjectName("Link"); |
| 169 |
Command::doCommand( |
| 170 |
Command::Doc, |
| 171 |
"App.getDocument('%s').addObject('App::Link','%s').setLink(" |
| 172 |
"App.getDocument('%s').getObject('%s'))", |
| 173 |
doc->getName(), |
| 174 |
name.c_str(), |
| 175 |
obj->getDocument()->getName(), |
| 176 |
obj->getNameInDocument() |
| 177 |
); |
| 178 |
setLinkLabel(obj, doc->getName(), name.c_str()); |
| 179 |
if (option == 2) { |
| 180 |
Command::doCommand( |
| 181 |
Command::Doc, |
| 182 |
"App.getDocument('%s').getObject('%s').LinkTransform = True", |
| 183 |
doc->getName(), |
| 184 |
name.c_str() |
| 185 |
); |
| 186 |
} |
| 187 |
else if (obj->getPropertyByName("Placement")) { |
| 188 |
Command::doCommand( |
| 189 |
Command::Doc, |
| 190 |
"App.getDocument('%s').getObject('%s').Placement = " |
| 191 |
"App.getDocument('%s').getObject('%s').Placement", |
| 192 |
doc->getName(), |
| 193 |
name.c_str(), |
| 194 |
obj->getDocument()->getName(), |
| 195 |
obj->getNameInDocument() |
| 196 |
); |
| 197 |
} |
| 198 |
} |
| 199 |
else { |
| 200 |
name = obj->getNameInDocument(); |
| 201 |
} |
| 202 |
Command::doCommand( |
| 203 |
Command::Doc, |
| 204 |
"__objs__.append(App.getDocument('%s').getObject('%s'))", |
| 205 |
doc->getName(), |
| 206 |
name.c_str() |
| 207 |
); |
| 208 |
Command::doCommand( |
| 209 |
Command::Doc, |
| 210 |
"App.getDocument('%s').getObject('%s').ViewObject.Visibility=False", |
| 211 |
doc->getName(), |
| 212 |
name.c_str() |
| 213 |
); |
| 214 |
} |
| 215 |
Command::doCommand( |
| 216 |
Command::Doc, |
| 217 |
"App.getDocument('%s').getObject('%s').setLink(__objs__)", |
| 218 |
doc->getName(), |
| 219 |
groupName.c_str() |
| 220 |
); |
| 221 |
Command::doCommand(Command::Doc, "del __objs__"); |
| 222 |
|
| 223 |
for (size_t i = 0; i < objs.size(); ++i) { |
| 224 |
auto name = std::to_string(i) + "."; |
| 225 |
Selection().addSelection(doc->getName(), groupName.c_str(), name.c_str()); |
| 226 |
} |
| 227 |
Selection().selStackPush(); |
| 228 |
} |
| 229 |
if (option != 0) { |
| 230 |
Command::doCommand( |
| 231 |
Command::Doc, |
| 232 |
"App.getDocument('%s').getObject('%s').LinkMode = 'Auto Delete'", |
| 233 |
doc->getName(), |
| 234 |
groupName.c_str() |
| 235 |
); |
| 236 |
} |
| 237 |
Command::commitCommand(); |
| 238 |
} |
| 239 |
catch (const Base::Exception& e) { |
| 240 |
QMessageBox::critical( |
| 241 |
getMainWindow(), |
| 242 |
QObject::tr("Create link group failed"), |
| 243 |
QString::fromLatin1(e.what()) |
| 244 |
); |
| 245 |
Command::abortCommand(); |
| 246 |
e.reportException(); |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
//////////////////////////////////////////////////////////////////////////////////////////// |
| 251 |
|
| 252 |
DEF_STD_CMD_A(StdCmdLinkMake) |
| 253 |
|
| 254 |
StdCmdLinkMake::StdCmdLinkMake() |
| 255 |
: Command("Std_LinkMake") |
| 256 |
{ |
| 257 |
sGroup = "Link"; |
| 258 |
sMenuText = QT_TR_NOOP("Make Link"); |
| 259 |
sToolTipText = QT_TR_NOOP( |
| 260 |
"A link is an object that references another object, either within the same " |
| 261 |
"or in another document. Unlike clones, links reference the original shape directly, " |
| 262 |
"making them more memory-efficient, which helps with the creation of complex assemblies." |
| 263 |
); |
| 264 |
sWhatsThis = "Std_LinkMake"; |
| 265 |
sStatusTip = sToolTipText; |
| 266 |
eType = AlterDoc; |
| 267 |
sPixmap = "Link"; |
| 268 |
} |
| 269 |
|
| 270 |
bool StdCmdLinkMake::isActive() |
| 271 |
{ |
| 272 |
return App::GetApplication().getActiveDocument(); |
| 273 |
} |
| 274 |
|
| 275 |
void StdCmdLinkMake::activated(int) |
| 276 |
{ |
| 277 |
auto doc = App::GetApplication().getActiveDocument(); |
| 278 |
if (!doc) { |
| 279 |
FC_ERR("no active document"); |
| 280 |
return; |
| 281 |
} |
| 282 |
|
| 283 |
std::set<App::DocumentObject*> objs; |
| 284 |
for (auto& sel : Selection().getCompleteSelection()) { |
| 285 |
if (sel.pObject && sel.pObject->isAttachedToDocument()) { |
| 286 |
objs.insert(sel.pObject); |
| 287 |
} |
| 288 |
} |
| 289 |
|
| 290 |
Selection().selStackPush(); |
| 291 |
Selection().clearCompleteSelection(); |
| 292 |
|
| 293 |
Command::openCommand(QT_TRANSLATE_NOOP("Command", "Make link")); |
| 294 |
try { |
| 295 |
if (objs.empty()) { |
| 296 |
std::string name = doc->getUniqueObjectName("Link"); |
| 297 |
Command::doCommand( |
| 298 |
Command::Doc, |
| 299 |
"App.getDocument('%s').addObject('App::Link','%s')", |
| 300 |
doc->getName(), |
| 301 |
name.c_str() |
| 302 |
); |
| 303 |
Selection().addSelection(doc->getName(), name.c_str()); |
| 304 |
} |
| 305 |
else { |
| 306 |
for (auto obj : objs) { |
| 307 |
std::string name = doc->getUniqueObjectName("Link"); |
| 308 |
Command::doCommand( |
| 309 |
Command::Doc, |
| 310 |
"App.getDocument('%s').addObject('App::Link','%s').setLink(App.getDocument('%s'" |
| 311 |
").%s)", |
| 312 |
doc->getName(), |
| 313 |
name.c_str(), |
| 314 |
obj->getDocument()->getName(), |
| 315 |
obj->getNameInDocument() |
| 316 |
); |
| 317 |
setLinkLabel(obj, doc->getName(), name.c_str()); |
| 318 |
Selection().addSelection(doc->getName(), name.c_str()); |
| 319 |
} |
| 320 |
} |
| 321 |
Selection().selStackPush(); |
| 322 |
Command::commitCommand(); |
| 323 |
} |
| 324 |
catch (const Base::Exception& e) { |
| 325 |
Command::abortCommand(); |
| 326 |
QMessageBox::critical( |
| 327 |
getMainWindow(), |
| 328 |
QObject::tr("Create link failed"), |
| 329 |
QString::fromLatin1(e.what()) |
| 330 |
); |
| 331 |
e.reportException(); |
| 332 |
} |
| 333 |
} |
| 334 |
|
| 335 |
//////////////////////////////////////////////////////////////////////////////////////////// |
| 336 |
|
| 337 |
DEF_STD_CMD_A(StdCmdLinkMakeRelative) |
| 338 |
|
| 339 |
StdCmdLinkMakeRelative::StdCmdLinkMakeRelative() |
| 340 |
: Command("Std_LinkMakeRelative") |
| 341 |
{ |
| 342 |
sGroup = "Link"; |
| 343 |
sMenuText = QT_TR_NOOP("Make Sub-Link"); |
| 344 |
sToolTipText = QT_TR_NOOP("Creates a sub-object or sub-element link"); |
| 345 |
sWhatsThis = "Std_LinkMakeRelative"; |
| 346 |
sStatusTip = sToolTipText; |
| 347 |
eType = AlterDoc; |
| 348 |
sPixmap = "LinkSub"; |
| 349 |
} |
| 350 |
|
| 351 |
bool StdCmdLinkMakeRelative::isActive() |
| 352 |
{ |
| 353 |
return Selection().hasSubSelection(nullptr, true); |
| 354 |
} |
| 355 |
|
| 356 |
void StdCmdLinkMakeRelative::activated(int) |
| 357 |
{ |
| 358 |
auto doc = App::GetApplication().getActiveDocument(); |
| 359 |
if (!doc) { |
| 360 |
FC_ERR("no active document"); |
| 361 |
return; |
| 362 |
} |
| 363 |
Command::openCommand(QT_TRANSLATE_NOOP("Command", "Make sub-link")); |
| 364 |
try { |
| 365 |
std::map< |
| 366 |
std::pair<App::DocumentObject*, std::string>, |
| 367 |
std::pair<App::DocumentObject*, std::vector<std::string>>> |
| 368 |
linkInfo; |
| 369 |
for (auto& sel : Selection().getCompleteSelection(ResolveMode::NoResolve)) { |
| 370 |
if (!sel.pObject || !sel.pObject->isAttachedToDocument()) { |
| 371 |
continue; |
| 372 |
} |
| 373 |
auto key = std::make_pair(sel.pObject, Data::noElementName(sel.SubName)); |
| 374 |
auto element = Data::findElementName(sel.SubName); |
| 375 |
auto& info = linkInfo[key]; |
| 376 |
info.first = sel.pResolvedObject; |
| 377 |
if (!Base::Tools::isNullOrEmpty(element)) { |
| 378 |
info.second.emplace_back(element); |
| 379 |
} |
| 380 |
} |
| 381 |
|
| 382 |
Selection().selStackPush(); |
| 383 |
Selection().clearCompleteSelection(); |
| 384 |
|
| 385 |
for (auto& v : linkInfo) { |
| 386 |
auto& key = v.first; |
| 387 |
auto& info = v.second; |
| 388 |
|
| 389 |
std::string name = doc->getUniqueObjectName("Link"); |
| 390 |
|
| 391 |
std::ostringstream ss; |
| 392 |
ss << '['; |
| 393 |
for (auto& s : info.second) { |
| 394 |
ss << "'" << s << "',"; |
| 395 |
} |
| 396 |
ss << ']'; |
| 397 |
FCMD_DOC_CMD( |
| 398 |
doc, |
| 399 |
"addObject('App::Link','" << name << "').setLink(" << getObjectCmd(key.first) |
| 400 |
<< ",'" << key.second << "'," << ss.str() << ")" |
| 401 |
); |
| 402 |
auto link = doc->getObject(name.c_str()); |
| 403 |
FCMD_OBJ_CMD(link, "LinkTransform = True"); |
| 404 |
setLinkLabel(info.first, doc->getName(), name.c_str()); |
| 405 |
|
| 406 |
Selection().addSelection(doc->getName(), name.c_str()); |
| 407 |
} |
| 408 |
Selection().selStackPush(); |
| 409 |
Command::commitCommand(); |
| 410 |
} |
| 411 |
catch (const Base::Exception& e) { |
| 412 |
Command::abortCommand(); |
| 413 |
QMessageBox::critical( |
| 414 |
getMainWindow(), |
| 415 |
QObject::tr("Failed to create relative link"), |
| 416 |
QString::fromLatin1(e.what()) |
| 417 |
); |
| 418 |
e.reportException(); |
| 419 |
} |
| 420 |
return; |
| 421 |
} |
| 422 |
|
| 423 |
///////////////////////////////////////////////////////////////////////////////////// |
| 424 |
|
| 425 |
struct Info |
| 426 |
{ |
| 427 |
bool inited = false; |
| 428 |
App::DocumentObjectT topParent; |
| 429 |
std::string subname; |
| 430 |
App::DocumentObjectT parent; |
| 431 |
App::DocumentObjectT obj; |
| 432 |
}; |
| 433 |
|
| 434 |
static void linkConvert(bool unlink) |
| 435 |
{ |
| 436 |
// We are trying to replace an object with a link (App::Link), or replace a |
| 437 |
// link back to its linked object (i.e. unlink). This is a very complex |
| 438 |
// operation. It works by reassign the link property of the parent of the |
| 439 |
// selected object(s) to a newly created link to the original object. |
| 440 |
// Everything should remain the same. This complexity is now largely handled |
| 441 |
// by ViewProviderDocumentObject::replaceObject(), which in turn relies on |
| 442 |
// PropertyLinkBase::CopyOnLinkReplace(). |
| 443 |
|
| 444 |
std::map<std::pair<App::DocumentObject*, App::DocumentObject*>, Info> infos; |
| 445 |
for (const auto& sel : TreeWidget::getSelection()) { |
| 446 |
auto obj = sel.vp->getObject(); |
| 447 |
auto parent = sel.parentVp; |
| 448 |
if (!parent) { |
| 449 |
FC_WARN("skip '" << obj->getFullName() << "' with no parent"); |
| 450 |
continue; |
| 451 |
} |
| 452 |
auto parentObj = parent->getObject(); |
| 453 |
auto& info = infos[std::make_pair(parentObj, obj)]; |
| 454 |
if (info.inited) { |
| 455 |
continue; |
| 456 |
} |
| 457 |
info.inited = true; |
| 458 |
if (unlink) { |
| 459 |
auto linked = obj->getLinkedObject(false); |
| 460 |
if (!linked || !linked->isAttachedToDocument() || linked == obj) { |
| 461 |
FC_WARN("skip non link"); |
| 462 |
continue; |
| 463 |
} |
| 464 |
} |
| 465 |
info.topParent = sel.topParent; |
| 466 |
info.parent = parentObj; |
| 467 |
info.obj = obj; |
| 468 |
} |
| 469 |
|
| 470 |
if (infos.empty()) { |
| 471 |
return; |
| 472 |
} |
| 473 |
|
| 474 |
Selection().selStackPush(); |
| 475 |
Selection().clearCompleteSelection(); |
| 476 |
|
| 477 |
// now, do actual operation |
| 478 |
const char* transactionName = unlink ? "Unlink" : "Replace with link"; |
| 479 |
Command::openCommand(transactionName); |
| 480 |
try { |
| 481 |
std::unordered_map<App::DocumentObject*, App::DocumentObjectT> recomputeSet; |
| 482 |
for (auto& v : infos) { |
| 483 |
auto& info = v.second; |
| 484 |
auto parent = info.parent.getObject(); |
| 485 |
auto parentVp = freecad_cast<ViewProviderDocumentObject*>( |
| 486 |
Application::Instance->getViewProvider(parent) |
| 487 |
); |
| 488 |
auto obj = info.obj.getObject(); |
| 489 |
if (!parent || !obj || !parentVp) { |
| 490 |
continue; |
| 491 |
} |
| 492 |
if (!recomputeSet.contains(parent)) { |
| 493 |
recomputeSet.emplace(parent, parent); |
| 494 |
} |
| 495 |
auto doc = parent->getDocument(); |
| 496 |
App::DocumentObject* replaceObj; |
| 497 |
if (unlink) { |
| 498 |
replaceObj = obj->getLinkedObject(false); |
| 499 |
if (!replaceObj || !replaceObj->isAttachedToDocument() || replaceObj == obj) { |
| 500 |
continue; |
| 501 |
} |
| 502 |
} |
| 503 |
else { |
| 504 |
auto name = doc->getUniqueObjectName("Link"); |
| 505 |
auto link = doc->addObject<App::Link>(name.c_str()); |
| 506 |
if (!link) { |
| 507 |
FC_THROWM(Base::RuntimeError, "Failed to create link"); |
| 508 |
} |
| 509 |
link->setLink(-1, obj); |
| 510 |
link->Label.setValue(obj->Label.getValue()); |
| 511 |
auto pla = freecad_cast<App::PropertyPlacement*>(obj->getPropertyByName("Placement")); |
| 512 |
if (pla) { |
| 513 |
link->Placement.setValue(pla->getValue()); |
| 514 |
} |
| 515 |
else { |
| 516 |
link->LinkTransform.setValue(true); |
| 517 |
} |
| 518 |
replaceObj = link; |
| 519 |
} |
| 520 |
|
| 521 |
// adjust subname for the new object |
| 522 |
auto pos = info.subname.rfind('.'); |
| 523 |
if (pos == std::string::npos && pos) { |
| 524 |
info.subname.clear(); |
| 525 |
} |
| 526 |
else { |
| 527 |
pos = info.subname.rfind('.', pos - 1); |
| 528 |
if (pos == std::string::npos) { |
| 529 |
info.subname.clear(); |
| 530 |
} |
| 531 |
else { |
| 532 |
info.subname.resize(pos + 1); |
| 533 |
info.subname += replaceObj->getNameInDocument(); |
| 534 |
info.subname += "."; |
| 535 |
} |
| 536 |
} |
| 537 |
|
| 538 |
// do the replacement operation |
| 539 |
if (parentVp->replaceObject(obj, replaceObj) <= 0) { |
| 540 |
FC_THROWM(Base::RuntimeError, "Failed to change link for " << parent->getFullName()); |
| 541 |
} |
| 542 |
} |
| 543 |
|
| 544 |
std::vector<App::DocumentObject*> recomputes; |
| 545 |
for (auto& v : recomputeSet) { |
| 546 |
auto obj = v.second.getObject(); |
| 547 |
if (obj) { |
| 548 |
recomputes.push_back(obj); |
| 549 |
} |
| 550 |
} |
| 551 |
if (!recomputes.empty()) { |
| 552 |
recomputes.front()->getDocument()->recompute(recomputes); |
| 553 |
} |
| 554 |
|
| 555 |
Command::commitCommand(); |
| 556 |
} |
| 557 |
catch (const Base::Exception& e) { |
| 558 |
Command::abortCommand(); |
| 559 |
auto title = unlink ? QObject::tr("Unlink failed") : QObject::tr("Replace link failed"); |
| 560 |
QMessageBox::critical(getMainWindow(), title, QString::fromLatin1(e.what())); |
| 561 |
e.reportException(); |
| 562 |
return; |
| 563 |
} |
| 564 |
} |
| 565 |
|
| 566 |
static bool linkConvertible(bool unlink) |
| 567 |
{ |
| 568 |
int count = 0; |
| 569 |
for (auto& sel : TreeWidget::getSelection()) { |
| 570 |
auto parent = sel.parentVp; |
| 571 |
if (!parent) { |
| 572 |
return false; |
| 573 |
} |
| 574 |
auto obj = sel.vp->getObject(); |
| 575 |
if (unlink) { |
| 576 |
auto linked = obj->getLinkedObject(false); |
| 577 |
if (!linked || linked == obj) { |
| 578 |
return false; |
| 579 |
} |
| 580 |
} |
| 581 |
++count; |
| 582 |
} |
| 583 |
return count != 0; |
| 584 |
} |
| 585 |
|
| 586 |
//////////////////////////////////////////////////////////////////////////////////////////// |
| 587 |
|
| 588 |
DEF_STD_CMD_A(StdCmdLinkReplace) |
| 589 |
|
| 590 |
StdCmdLinkReplace::StdCmdLinkReplace() |
| 591 |
: Command("Std_LinkReplace") |
| 592 |
{ |
| 593 |
sGroup = "Link"; |
| 594 |
sMenuText = QT_TR_NOOP("Replace With Link"); |
| 595 |
sToolTipText = QT_TR_NOOP("Replaces the selected objects with links"); |
| 596 |
sWhatsThis = "Std_LinkReplace"; |
| 597 |
sStatusTip = sToolTipText; |
| 598 |
eType = AlterDoc; |
| 599 |
sPixmap = "LinkReplace"; |
| 600 |
} |
| 601 |
|
| 602 |
bool StdCmdLinkReplace::isActive() |
| 603 |
{ |
| 604 |
return linkConvertible(false); |
| 605 |
} |
| 606 |
|
| 607 |
void StdCmdLinkReplace::activated(int) |
| 608 |
{ |
| 609 |
linkConvert(false); |
| 610 |
} |
| 611 |
|
| 612 |
//////////////////////////////////////////////////////////////////////////////////////////// |
| 613 |
|
| 614 |
DEF_STD_CMD_A(StdCmdLinkUnlink) |
| 615 |
|
| 616 |
StdCmdLinkUnlink::StdCmdLinkUnlink() |
| 617 |
: Command("Std_LinkUnlink") |
| 618 |
{ |
| 619 |
sGroup = "Link"; |
| 620 |
sMenuText = QT_TR_NOOP("Unlink"); |
| 621 |
sToolTipText = QT_TR_NOOP("Unlinks the object by placing it directly in the container"); |
| 622 |
sWhatsThis = "Std_LinkUnlink"; |
| 623 |
sStatusTip = sToolTipText; |
| 624 |
eType = AlterDoc; |
| 625 |
sPixmap = "Unlink"; |
| 626 |
} |
| 627 |
|
| 628 |
bool StdCmdLinkUnlink::isActive() |
| 629 |
{ |
| 630 |
return linkConvertible(true); |
| 631 |
} |
| 632 |
|
| 633 |
void StdCmdLinkUnlink::activated(int) |
| 634 |
{ |
| 635 |
linkConvert(true); |
| 636 |
} |
| 637 |
|
| 638 |
//////////////////////////////////////////////////////////////////////////////////////////// |
| 639 |
|
| 640 |
DEF_STD_CMD_A(StdCmdLinkImport) |
| 641 |
|
| 642 |
StdCmdLinkImport::StdCmdLinkImport() |
| 643 |
: Command("Std_LinkImport") |
| 644 |
{ |
| 645 |
sGroup = "Link"; |
| 646 |
sMenuText = QT_TR_NOOP("Import Links"); |
| 647 |
sToolTipText = QT_TR_NOOP("Imports selected external links"); |
| 648 |
sWhatsThis = "Std_LinkImport"; |
| 649 |
sStatusTip = sToolTipText; |
| 650 |
eType = AlterDoc; |
| 651 |
sPixmap = "LinkImport"; |
| 652 |
} |
| 653 |
|
| 654 |
static std::map<App::Document*, std::vector<App::DocumentObject*>> getLinkImportSelections() |
| 655 |
{ |
| 656 |
std::map<App::Document*, std::vector<App::DocumentObject*>> objMap; |
| 657 |
for (auto& sel : Selection().getCompleteSelection(ResolveMode::NoResolve)) { |
| 658 |
auto obj = sel.pObject->resolve(sel.SubName); |
| 659 |
if (!obj || !obj->isAttachedToDocument()) { |
| 660 |
continue; |
| 661 |
} |
| 662 |
for (auto o : obj->getOutList()) { |
| 663 |
if (o && o->isAttachedToDocument() && o->getDocument() != obj->getDocument()) { |
| 664 |
objMap[obj->getDocument()].push_back(obj); |
| 665 |
break; |
| 666 |
} |
| 667 |
} |
| 668 |
} |
| 669 |
return objMap; |
| 670 |
} |
| 671 |
|
| 672 |
bool StdCmdLinkImport::isActive() |
| 673 |
{ |
| 674 |
auto links = getLinkImportSelections(); |
| 675 |
if (links.empty()) { |
| 676 |
return false; |
| 677 |
} |
| 678 |
for (auto& v : links) { |
| 679 |
if (v.first->testStatus(App::Document::PartialDoc)) { |
| 680 |
return false; |
| 681 |
} |
| 682 |
} |
| 683 |
return true; |
| 684 |
} |
| 685 |
|
| 686 |
void StdCmdLinkImport::activated(int) |
| 687 |
{ |
| 688 |
Command::openCommand(QT_TRANSLATE_NOOP("Command", "Import links")); |
| 689 |
try { |
| 690 |
WaitCursor wc; |
| 691 |
wc.setIgnoreEvents(WaitCursor::NoEvents); |
| 692 |
for (auto& v : getLinkImportSelections()) { |
| 693 |
auto doc = v.first; |
| 694 |
// TODO: Is it possible to do this using interpreter? |
| 695 |
for (auto obj : doc->importLinks(v.second)) { |
| 696 |
obj->Visibility.setValue(false); |
| 697 |
} |
| 698 |
} |
| 699 |
Command::commitCommand(); |
| 700 |
} |
| 701 |
catch (const Base::Exception& e) { |
| 702 |
Command::abortCommand(); |
| 703 |
QMessageBox::critical( |
| 704 |
getMainWindow(), |
| 705 |
QObject::tr("Failed to import links"), |
| 706 |
QString::fromLatin1(e.what()) |
| 707 |
); |
| 708 |
e.reportException(); |
| 709 |
} |
| 710 |
} |
| 711 |
|
| 712 |
//////////////////////////////////////////////////////////////////////////////////////////// |
| 713 |
|
| 714 |
DEF_STD_CMD_A(StdCmdLinkImportAll) |
| 715 |
|
| 716 |
StdCmdLinkImportAll::StdCmdLinkImportAll() |
| 717 |
: Command("Std_LinkImportAll") |
| 718 |
{ |
| 719 |
sGroup = "Link"; |
| 720 |
sMenuText = QT_TR_NOOP("Import All Links"); |
| 721 |
sToolTipText = QT_TR_NOOP("Imports all links of the active document"); |
| 722 |
sWhatsThis = "Std_LinkImportAll"; |
| 723 |
sStatusTip = sToolTipText; |
| 724 |
eType = AlterDoc; |
| 725 |
sPixmap = "LinkImportAll"; |
| 726 |
} |
| 727 |
|
| 728 |
bool StdCmdLinkImportAll::isActive() |
| 729 |
{ |
| 730 |
auto doc = App::GetApplication().getActiveDocument(); |
| 731 |
return doc && !doc->testStatus(App::Document::PartialDoc) && App::PropertyXLink::hasXLink(doc); |
| 732 |
} |
| 733 |
|
| 734 |
void StdCmdLinkImportAll::activated(int) |
| 735 |
{ |
| 736 |
Command::openCommand(QT_TRANSLATE_NOOP("Command", "Import all links")); |
| 737 |
try { |
| 738 |
WaitCursor wc; |
| 739 |
wc.setIgnoreEvents(WaitCursor::NoEvents); |
| 740 |
auto doc = App::GetApplication().getActiveDocument(); |
| 741 |
if (doc) { |
| 742 |
for (auto obj : doc->importLinks()) { |
| 743 |
obj->Visibility.setValue(false); |
| 744 |
} |
| 745 |
} |
| 746 |
Command::commitCommand(); |
| 747 |
} |
| 748 |
catch (const Base::Exception& e) { |
| 749 |
QMessageBox::critical( |
| 750 |
getMainWindow(), |
| 751 |
QObject::tr("Failed to import all links"), |
| 752 |
QString::fromLatin1(e.what()) |
| 753 |
); |
| 754 |
Command::abortCommand(); |
| 755 |
e.reportException(); |
| 756 |
} |
| 757 |
} |
| 758 |
|
| 759 |
|
| 760 |
//////////////////////////////////////////////////////////////////////////////////////////// |
| 761 |
|
| 762 |
DEF_STD_CMD_A(StdCmdLinkSelectLinked) |
| 763 |
|
| 764 |
StdCmdLinkSelectLinked::StdCmdLinkSelectLinked() |
| 765 |
: Command("Std_LinkSelectLinked") |
| 766 |
{ |
| 767 |
sGroup = "Link"; |
| 768 |
sMenuText = QT_TR_NOOP("&Go to Linked Object"); |
| 769 |
sToolTipText = QT_TR_NOOP("Selects the linked object and switches to its original document"); |
| 770 |
sWhatsThis = "Std_LinkSelectLinked"; |
| 771 |
sStatusTip = sToolTipText; |
| 772 |
eType = AlterSelection; |
| 773 |
sPixmap = "LinkSelect"; |
| 774 |
sAccel = "S, G"; |
| 775 |
} |
| 776 |
|
| 777 |
static App::DocumentObject* getSelectedLink(bool finalLink, std::string* subname = nullptr) |
| 778 |
{ |
| 779 |
const auto& sels = Selection().getSelection("*", ResolveMode::NoResolve, true); |
| 780 |
if (sels.empty()) { |
| 781 |
return nullptr; |
| 782 |
} |
| 783 |
auto sobj = sels[0].pObject->getSubObject(sels[0].SubName); |
| 784 |
if (!sobj) { |
| 785 |
return nullptr; |
| 786 |
} |
| 787 |
auto vp = freecad_cast<ViewProviderDocumentObject*>(Application::Instance->getViewProvider(sobj)); |
| 788 |
if (!vp) { |
| 789 |
return nullptr; |
| 790 |
} |
| 791 |
|
| 792 |
auto linkedVp = vp->getLinkedViewProvider(subname, finalLink); |
| 793 |
if (!linkedVp || linkedVp == vp) { |
| 794 |
if (sobj->getDocument() == sels[0].pObject->getDocument()) { |
| 795 |
return nullptr; |
| 796 |
} |
| 797 |
for (const char* dot = strchr(sels[0].SubName, '.'); dot; dot = strchr(dot + 1, '.')) { |
| 798 |
std::string sub(sels[0].SubName, dot + 1 - sels[0].SubName); |
| 799 |
auto obj = sels[0].pObject->getSubObject(sub.c_str()); |
| 800 |
if (!obj) { |
| 801 |
break; |
| 802 |
} |
| 803 |
obj = obj->getLinkedObject(true); |
| 804 |
if (obj->getDocument() != sels[0].pObject->getDocument()) { |
| 805 |
if (finalLink) { |
| 806 |
return sobj == obj ? nullptr : sobj; |
| 807 |
} |
| 808 |
if (subname) { |
| 809 |
*subname = std::string(dot + 1); |
| 810 |
} |
| 811 |
return obj; |
| 812 |
} |
| 813 |
} |
| 814 |
return finalLink ? nullptr : sobj; |
| 815 |
} |
| 816 |
|
| 817 |
if (finalLink && linkedVp == vp->getLinkedViewProvider()) { |
| 818 |
return nullptr; |
| 819 |
} |
| 820 |
|
| 821 |
auto linked = linkedVp->getObject(); |
| 822 |
if (!linked || !linked->isAttachedToDocument()) { |
| 823 |
return nullptr; |
| 824 |
} |
| 825 |
|
| 826 |
if (subname && sels[0].pObject != sobj && sels[0].SubName) { |
| 827 |
bool found = false; |
| 828 |
int pre_len = 0; |
| 829 |
std::size_t post_len = 0; |
| 830 |
std::string prefix; |
| 831 |
std::string prefix2; |
| 832 |
// An object can be claimed by multiple objects. Let's try select one |
| 833 |
// that causes minimum jump in tree view, and prefer upper over lower |
| 834 |
// hierarchy (because of less depth/complexity of tree expansion) |
| 835 |
for (auto& v : linked->getParents()) { |
| 836 |
if (v.first != sels[0].pObject) { |
| 837 |
continue; |
| 838 |
} |
| 839 |
|
| 840 |
const char* sub = v.second.c_str(); |
| 841 |
const char* dot = sub; |
| 842 |
for (const char* s = sels[0].SubName; *s && *sub == *s; ++s, ++sub) { |
| 843 |
if (*sub == '.') { |
| 844 |
dot = sub; |
| 845 |
} |
| 846 |
} |
| 847 |
found = true; |
| 848 |
if (dot - v.second.c_str() > pre_len |
| 849 |
|| (dot - v.second.c_str() == pre_len && v.second.size() < post_len)) { |
| 850 |
pre_len = dot - v.second.c_str(); |
| 851 |
prefix = std::string(sels[0].SubName, pre_len) + (v.second.c_str() + pre_len); |
| 852 |
post_len = v.second.size(); |
| 853 |
} |
| 854 |
else if (!pre_len) { |
| 855 |
if (prefix2.empty() || prefix2.size() > v.second.size()) { |
| 856 |
prefix2 = v.second; |
| 857 |
} |
| 858 |
} |
| 859 |
} |
| 860 |
|
| 861 |
if (found) { |
| 862 |
linked = sels[0].pObject; |
| 863 |
*subname = !prefix.empty() ? prefix : prefix2 + *subname; |
| 864 |
} |
| 865 |
} |
| 866 |
|
| 867 |
return linked; |
| 868 |
} |
| 869 |
|
| 870 |
bool StdCmdLinkSelectLinked::isActive() |
| 871 |
{ |
| 872 |
return getSelectedLink(false) != nullptr; |
| 873 |
} |
| 874 |
|
| 875 |
void StdCmdLinkSelectLinked::activated(int) |
| 876 |
{ |
| 877 |
std::string subname; |
| 878 |
auto linked = getSelectedLink(false, &subname); |
| 879 |
if (!linked) { |
| 880 |
FC_WARN("invalid selection"); |
| 881 |
return; |
| 882 |
} |
| 883 |
Selection().selStackPush(); |
| 884 |
Selection().clearCompleteSelection(); |
| 885 |
if (!subname.empty()) { |
| 886 |
Selection().addSelection( |
| 887 |
linked->getDocument()->getName(), |
| 888 |
linked->getNameInDocument(), |
| 889 |
subname.c_str() |
| 890 |
); |
| 891 |
auto doc = Application::Instance->getDocument(linked->getDocument()); |
| 892 |
if (doc) { |
| 893 |
auto vp = freecad_cast<ViewProviderDocumentObject*>( |
| 894 |
Application::Instance->getViewProvider(linked) |
| 895 |
); |
| 896 |
doc->setActiveView(vp); |
| 897 |
} |
| 898 |
} |
| 899 |
else { |
| 900 |
const auto trees = getMainWindow()->findChildren<TreeWidget*>(); |
| 901 |
for (auto tree : trees) { |
| 902 |
tree->selectLinkedObject(linked); |
| 903 |
} |
| 904 |
} |
| 905 |
Selection().selStackPush(); |
| 906 |
} |
| 907 |
|
| 908 |
//////////////////////////////////////////////////////////////////////////////////////////// |
| 909 |
|
| 910 |
DEF_STD_CMD_A(StdCmdLinkSelectLinkedFinal) |
| 911 |
|
| 912 |
StdCmdLinkSelectLinkedFinal::StdCmdLinkSelectLinkedFinal() |
| 913 |
: Command("Std_LinkSelectLinkedFinal") |
| 914 |
{ |
| 915 |
sGroup = "Link"; |
| 916 |
sMenuText = QT_TR_NOOP("Go to &Deepest Linked Object"); |
| 917 |
sToolTipText = QT_TR_NOOP( |
| 918 |
"Selects the deepest linked object and switches to its original document" |
| 919 |
); |
| 920 |
sWhatsThis = "Std_LinkSelectLinkedFinal"; |
| 921 |
sStatusTip = sToolTipText; |
| 922 |
eType = AlterSelection; |
| 923 |
sPixmap = "LinkSelectFinal"; |
| 924 |
sAccel = "S, D"; |
| 925 |
} |
| 926 |
|
| 927 |
bool StdCmdLinkSelectLinkedFinal::isActive() |
| 928 |
{ |
| 929 |
return getSelectedLink(true) != nullptr; |
| 930 |
} |
| 931 |
|
| 932 |
void StdCmdLinkSelectLinkedFinal::activated(int) |
| 933 |
{ |
| 934 |
auto linked = getSelectedLink(true); |
| 935 |
if (!linked) { |
| 936 |
FC_WARN("invalid selection"); |
| 937 |
return; |
| 938 |
} |
| 939 |
Selection().selStackPush(); |
| 940 |
Selection().clearCompleteSelection(); |
| 941 |
const auto trees = getMainWindow()->findChildren<TreeWidget*>(); |
| 942 |
for (auto tree : trees) { |
| 943 |
tree->selectLinkedObject(linked); |
| 944 |
} |
| 945 |
Selection().selStackPush(); |
| 946 |
} |
| 947 |
|
| 948 |
//////////////////////////////////////////////////////////////////////////////////////////// |
| 949 |
|
| 950 |
DEF_STD_CMD_A(StdCmdLinkSelectAllLinks) |
| 951 |
|
| 952 |
StdCmdLinkSelectAllLinks::StdCmdLinkSelectAllLinks() |
| 953 |
: Command("Std_LinkSelectAllLinks") |
| 954 |
{ |
| 955 |
sGroup = "Link"; |
| 956 |
sMenuText = QT_TR_NOOP("Select &All Links"); |
| 957 |
sToolTipText = QT_TR_NOOP("Selects all links to the current selected object"); |
| 958 |
sWhatsThis = "Std_LinkSelectAllLinks"; |
| 959 |
sStatusTip = sToolTipText; |
| 960 |
eType = AlterSelection; |
| 961 |
sPixmap = "LinkSelectAll"; |
| 962 |
} |
| 963 |
|
| 964 |
bool StdCmdLinkSelectAllLinks::isActive() |
| 965 |
{ |
| 966 |
const auto& sels = Selection().getSelection("*", ResolveMode::OldStyleElement, true); |
| 967 |
if (sels.empty()) { |
| 968 |
return false; |
| 969 |
} |
| 970 |
return App::GetApplication().hasLinksTo(sels[0].pObject); |
| 971 |
} |
| 972 |
|
| 973 |
void StdCmdLinkSelectAllLinks::activated(int) |
| 974 |
{ |
| 975 |
auto sels = Selection().getSelection("*", ResolveMode::OldStyleElement, true); |
| 976 |
if (sels.empty()) { |
| 977 |
return; |
| 978 |
} |
| 979 |
Selection().selStackPush(); |
| 980 |
Selection().clearCompleteSelection(); |
| 981 |
const auto trees = getMainWindow()->findChildren<TreeWidget*>(); |
| 982 |
for (auto tree : trees) { |
| 983 |
tree->selectAllLinks(sels[0].pObject); |
| 984 |
} |
| 985 |
Selection().selStackPush(); |
| 986 |
} |
| 987 |
|
| 988 |
|
| 989 |
//====================================================================== |
| 990 |
// Std_LinkSelectActions |
| 991 |
//=========================================================================== |
| 992 |
|
| 993 |
class StdCmdLinkSelectActions: public GroupCommand |
| 994 |
{ |
| 995 |
public: |
| 996 |
StdCmdLinkSelectActions() |
| 997 |
: GroupCommand("Std_LinkSelectActions") |
| 998 |
{ |
| 999 |
sGroup = "View"; |
| 1000 |
sMenuText = QT_TR_NOOP("&Link Navigation"); |
| 1001 |
sToolTipText = QT_TR_NOOP("Link navigation actions"); |
| 1002 |
sWhatsThis = "Std_LinkSelectActions"; |
| 1003 |
sStatusTip = sToolTipText; |
| 1004 |
eType = AlterSelection; |
| 1005 |
bCanLog = false; |
| 1006 |
|
| 1007 |
addCommand(new StdCmdLinkSelectLinked()); |
| 1008 |
addCommand(new StdCmdLinkSelectLinkedFinal()); |
| 1009 |
addCommand(new StdCmdLinkSelectAllLinks()); |
| 1010 |
} |
| 1011 |
|
| 1012 |
const char* className() const override |
| 1013 |
{ |
| 1014 |
return "StdCmdLinkSelectActions"; |
| 1015 |
} |
| 1016 |
}; |
| 1017 |
|
| 1018 |
//====================================================================== |
| 1019 |
// Std_LinkActions |
| 1020 |
//=========================================================================== |
| 1021 |
|
| 1022 |
class StdCmdLinkActions: public GroupCommand |
| 1023 |
{ |
| 1024 |
public: |
| 1025 |
StdCmdLinkActions() |
| 1026 |
: GroupCommand("Std_LinkActions") |
| 1027 |
{ |
| 1028 |
sGroup = "View"; |
| 1029 |
sMenuText = QT_TR_NOOP("Link Actions"); |
| 1030 |
sToolTipText = QT_TR_NOOP("Commands that operate on link objects"); |
| 1031 |
sWhatsThis = "Std_LinkActions"; |
| 1032 |
sStatusTip = sToolTipText; |
| 1033 |
eType = AlterDoc; |
| 1034 |
bCanLog = false; |
| 1035 |
|
| 1036 |
setCheckable(false); |
| 1037 |
|
| 1038 |
addCommand(new StdCmdLinkMake()); |
| 1039 |
addCommand(new StdCmdLinkMakeRelative()); |
| 1040 |
addCommand(new StdCmdLinkReplace()); |
| 1041 |
addCommand(new StdCmdLinkUnlink()); |
| 1042 |
addCommand(new StdCmdLinkImport()); |
| 1043 |
addCommand(new StdCmdLinkImportAll()); |
| 1044 |
} |
| 1045 |
|
| 1046 |
const char* className() const override |
| 1047 |
{ |
| 1048 |
return "StdCmdLinkActions"; |
| 1049 |
} |
| 1050 |
|
| 1051 |
bool isActive() override |
| 1052 |
{ |
| 1053 |
return hasActiveDocument(); |
| 1054 |
} |
| 1055 |
}; |
| 1056 |
|
| 1057 |
//=========================================================================== |
| 1058 |
// Instantiation |
| 1059 |
//=========================================================================== |
| 1060 |
|
| 1061 |
|
| 1062 |
namespace Gui |
| 1063 |
{ |
| 1064 |
|
| 1065 |
void CreateLinkCommands() |
| 1066 |
{ |
| 1067 |
CommandManager& rcCmdMgr = Application::Instance->commandManager(); |
| 1068 |
rcCmdMgr.addCommand(new StdCmdLinkActions()); |
| 1069 |
rcCmdMgr.addCommand(new StdCmdLinkMakeGroup()); |
| 1070 |
rcCmdMgr.addCommand(new StdCmdLinkSelectActions()); |
| 1071 |
} |
| 1072 |
|
| 1073 |
} // namespace Gui |