| 1 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 |
/*************************************************************************** |
| 3 |
* Copyright (c) 2011 Jürgen Riegel <[email protected]> * |
| 4 |
* Copyright (c) 2011 Werner Mayer <wmayer[at]users.sourceforge.net> * |
| 5 |
* * |
| 6 |
* This file is part of the FreeCAD CAx development system. * |
| 7 |
* * |
| 8 |
* This library is free software; you can redistribute it and/or * |
| 9 |
* modify it under the terms of the GNU Library General Public * |
| 10 |
* License as published by the Free Software Foundation; either * |
| 11 |
* version 2 of the License, or (at your option) any later version. * |
| 12 |
* * |
| 13 |
* This library is distributed in the hope that it will be useful, * |
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 16 |
* GNU Library General Public License for more details. * |
| 17 |
* * |
| 18 |
* You should have received a copy of the GNU Library General Public * |
| 19 |
* License along with this library; see the file COPYING.LIB. If not, * |
| 20 |
* write to the Free Software Foundation, Inc., 59 Temple Place, * |
| 21 |
* Suite 330, Boston, MA 02111-1307, USA * |
| 22 |
* * |
| 23 |
***************************************************************************/ |
| 24 |
|
| 25 |
#ifndef GUI_SELECTION_H |
| 26 |
#define GUI_SELECTION_H |
| 27 |
|
| 28 |
#include <deque> |
| 29 |
#include <list> |
| 30 |
#include <string> |
| 31 |
#include <vector> |
| 32 |
|
| 33 |
#include <App/DocumentObject.h> |
| 34 |
#include <App/DocumentObserver.h> |
| 35 |
#include <Base/Observer.h> |
| 36 |
|
| 37 |
#include "SelectionObject.h" |
| 38 |
|
| 39 |
|
| 40 |
using PyObject = struct _object; |
| 41 |
|
| 42 |
namespace App |
| 43 |
{ |
| 44 |
class DocumentObject; |
| 45 |
class Document; |
| 46 |
class PropertyLinkSubList; |
| 47 |
} // namespace App |
| 48 |
|
| 49 |
namespace Gui |
| 50 |
{ |
| 51 |
|
| 52 |
enum class ResolveMode |
| 53 |
{ |
| 54 |
/// No resolve |
| 55 |
NoResolve, |
| 56 |
/// Sub-object with old style element name |
| 57 |
OldStyleElement, |
| 58 |
/// Sub-object with new style element name |
| 59 |
NewStyleElement, |
| 60 |
/// Resolve sub-object and follow link |
| 61 |
FollowLink |
| 62 |
}; |
| 63 |
|
| 64 |
class SelectionObject; |
| 65 |
class SelectionFilter; |
| 66 |
|
| 67 |
/** Transport the changes of the Selection |
| 68 |
* This class transports closer information what was changed in the |
| 69 |
* selection. It's an optional information and not all commands set this |
| 70 |
* information. If not set all observer of the selection assume a full change |
| 71 |
* and update everything (e.g 3D view). This is not a very good idea if, e.g. only |
| 72 |
* a small parameter has changed. Therefore one can use this class and make the |
| 73 |
* update of the document much faster! |
| 74 |
* @see Base::Observer |
| 75 |
*/ |
| 76 |
class GuiExport SelectionChanges |
| 77 |
{ |
| 78 |
public: |
| 79 |
enum MsgType |
| 80 |
{ |
| 81 |
AddSelection, |
| 82 |
RmvSelection, |
| 83 |
SetSelection, |
| 84 |
ClrSelection, |
| 85 |
SetPreselect, // to signal observer the preselect has changed |
| 86 |
RmvPreselect, |
| 87 |
SetPreselectSignal, // to request 3D view to change preselect |
| 88 |
PickedListChanged, |
| 89 |
ShowSelection, // to show a selection |
| 90 |
HideSelection, // to hide a selection |
| 91 |
RmvPreselectSignal, // to request 3D view to remove preselect |
| 92 |
MovePreselect, // to signal observer the mouse movement when preselect |
| 93 |
}; |
| 94 |
enum class MsgSource |
| 95 |
{ |
| 96 |
Any = 0, |
| 97 |
Internal = 1, |
| 98 |
TreeView = 2 |
| 99 |
}; |
| 100 |
|
| 101 |
SelectionChanges( |
| 102 |
MsgType type = ClrSelection, |
| 103 |
const char* docName = nullptr, |
| 104 |
const char* objName = nullptr, |
| 105 |
const char* subName = nullptr, |
| 106 |
const char* typeName = nullptr, |
| 107 |
float x = 0, |
| 108 |
float y = 0, |
| 109 |
float z = 0, |
| 110 |
MsgSource subtype = MsgSource::Any |
| 111 |
) |
| 112 |
: Type(type) |
| 113 |
, SubType(subtype) |
| 114 |
, x(x) |
| 115 |
, y(y) |
| 116 |
, z(z) |
| 117 |
, Object(docName, objName, subName) |
| 118 |
{ |
| 119 |
pDocName = Object.getDocumentName().c_str(); |
| 120 |
pObjectName = Object.getObjectName().c_str(); |
| 121 |
pSubName = Object.getSubName().c_str(); |
| 122 |
if (typeName) { |
| 123 |
TypeName = typeName; |
| 124 |
} |
| 125 |
pTypeName = TypeName.c_str(); |
| 126 |
} // explicit bombs |
| 127 |
|
| 128 |
SelectionChanges( |
| 129 |
MsgType type, |
| 130 |
const std::string& docName, |
| 131 |
const std::string& objName, |
| 132 |
const std::string& subName, |
| 133 |
const std::string& typeName = std::string(), |
| 134 |
float x = 0, |
| 135 |
float y = 0, |
| 136 |
float z = 0, |
| 137 |
MsgSource subtype = MsgSource::Any |
| 138 |
) |
| 139 |
: Type(type) |
| 140 |
, SubType(subtype) |
| 141 |
, x(x) |
| 142 |
, y(y) |
| 143 |
, z(z) |
| 144 |
, Object(docName.c_str(), objName.c_str(), subName.c_str()) |
| 145 |
, TypeName(typeName) |
| 146 |
{ |
| 147 |
pDocName = Object.getDocumentName().c_str(); |
| 148 |
pObjectName = Object.getObjectName().c_str(); |
| 149 |
pSubName = Object.getSubName().c_str(); |
| 150 |
pTypeName = TypeName.c_str(); |
| 151 |
} |
| 152 |
|
| 153 |
SelectionChanges(const SelectionChanges& other) |
| 154 |
{ |
| 155 |
*this = other; |
| 156 |
} |
| 157 |
|
| 158 |
SelectionChanges& operator=(const SelectionChanges& other) |
| 159 |
{ |
| 160 |
Type = other.Type; |
| 161 |
SubType = other.SubType; |
| 162 |
x = other.x; |
| 163 |
y = other.y; |
| 164 |
z = other.z; |
| 165 |
Object = other.Object; |
| 166 |
TypeName = other.TypeName; |
| 167 |
pDocName = Object.getDocumentName().c_str(); |
| 168 |
pObjectName = Object.getObjectName().c_str(); |
| 169 |
pSubName = Object.getSubName().c_str(); |
| 170 |
pTypeName = TypeName.c_str(); |
| 171 |
pOriginalMsg = other.pOriginalMsg; |
| 172 |
return *this; |
| 173 |
} |
| 174 |
|
| 175 |
SelectionChanges(SelectionChanges&& other) |
| 176 |
{ |
| 177 |
*this = std::move(other); |
| 178 |
} |
| 179 |
|
| 180 |
SelectionChanges& operator=(SelectionChanges&& other) |
| 181 |
{ |
| 182 |
Type = other.Type; |
| 183 |
SubType = other.SubType; |
| 184 |
x = other.x; |
| 185 |
y = other.y; |
| 186 |
z = other.z; |
| 187 |
Object = std::move(other.Object); |
| 188 |
TypeName = std::move(other.TypeName); |
| 189 |
pDocName = Object.getDocumentName().c_str(); |
| 190 |
pObjectName = Object.getObjectName().c_str(); |
| 191 |
pSubName = Object.getSubName().c_str(); |
| 192 |
pTypeName = TypeName.c_str(); |
| 193 |
pOriginalMsg = other.pOriginalMsg; |
| 194 |
return *this; |
| 195 |
} |
| 196 |
|
| 197 |
MsgType Type; |
| 198 |
MsgSource SubType; |
| 199 |
|
| 200 |
const char* pDocName; |
| 201 |
const char* pObjectName; |
| 202 |
const char* pSubName; |
| 203 |
const char* pTypeName; |
| 204 |
float x; |
| 205 |
float y; |
| 206 |
float z; |
| 207 |
|
| 208 |
App::SubObjectT Object; |
| 209 |
std::string TypeName; |
| 210 |
|
| 211 |
// Original selection message in case resolve!=0 |
| 212 |
const SelectionChanges* pOriginalMsg = nullptr; |
| 213 |
}; |
| 214 |
|
| 215 |
class ViewProviderDocumentObject; |
| 216 |
|
| 217 |
/** |
| 218 |
* The SelectionObserver class simplifies the step to write classes that listen |
| 219 |
* to what happens to the selection. |
| 220 |
* |
| 221 |
* @author Werner Mayer |
| 222 |
*/ |
| 223 |
class GuiExport SelectionObserver |
| 224 |
{ |
| 225 |
|
| 226 |
public: |
| 227 |
/** Constructor |
| 228 |
* |
| 229 |
* @param attach: whether to attach this observer on construction |
| 230 |
* @param resolve: sub-object resolving mode. |
| 231 |
*/ |
| 232 |
explicit SelectionObserver(bool attach = true, ResolveMode resolve = ResolveMode::OldStyleElement); |
| 233 |
/** Constructor |
| 234 |
* |
| 235 |
* @param vp: filtering view object. |
| 236 |
* @param attach: whether to attach this observer on construction |
| 237 |
* @param resolve: sub-object resolving mode. |
| 238 |
* |
| 239 |
* Constructs an selection observer that receives only selection event of |
| 240 |
* objects within the same document as the input view object. |
| 241 |
*/ |
| 242 |
explicit SelectionObserver( |
| 243 |
const Gui::ViewProviderDocumentObject* vp, |
| 244 |
bool attach = true, |
| 245 |
ResolveMode resolve = ResolveMode::OldStyleElement |
| 246 |
); |
| 247 |
|
| 248 |
virtual ~SelectionObserver(); |
| 249 |
bool blockSelection(bool block); |
| 250 |
bool isSelectionBlocked() const; |
| 251 |
bool isSelectionAttached() const; |
| 252 |
|
| 253 |
/** Attaches to the selection. */ |
| 254 |
void attachSelection(); |
| 255 |
/** Detaches from the selection. */ |
| 256 |
void detachSelection(); |
| 257 |
|
| 258 |
private: |
| 259 |
virtual void onSelectionChanged(const SelectionChanges& msg) = 0; |
| 260 |
void _onSelectionChanged(const SelectionChanges& msg); |
| 261 |
|
| 262 |
private: |
| 263 |
using Connection = boost::signals2::connection; |
| 264 |
Connection connectSelection; |
| 265 |
std::string filterDocName; |
| 266 |
std::string filterObjName; |
| 267 |
ResolveMode resolve; |
| 268 |
bool blockedSelection; |
| 269 |
}; |
| 270 |
|
| 271 |
/** SelectionGate |
| 272 |
* The selection gate allows or disallows selection of certain types. |
| 273 |
* It has to be registered to the selection. |
| 274 |
*/ |
| 275 |
class GuiExport SelectionGate |
| 276 |
{ |
| 277 |
public: |
| 278 |
virtual ~SelectionGate() = default; |
| 279 |
virtual bool allow(App::Document*, App::DocumentObject*, const char*) = 0; |
| 280 |
|
| 281 |
/** |
| 282 |
* @brief notAllowedReason is a string that sets the message to be |
| 283 |
* displayed in statusbar for cluing the user on why is the selection not |
| 284 |
* allowed. Set this variable in allow() implementation. Enclose the |
| 285 |
* literal into QT_TR_NOOP() for translatability. |
| 286 |
*/ |
| 287 |
std::string notAllowedReason; |
| 288 |
}; |
| 289 |
|
| 290 |
/** SelectionGateFilterExternal |
| 291 |
* The selection gate disallows any external object |
| 292 |
*/ |
| 293 |
class GuiExport SelectionGateFilterExternal: public SelectionGate |
| 294 |
{ |
| 295 |
public: |
| 296 |
explicit SelectionGateFilterExternal(const char* docName, const char* objName = nullptr); |
| 297 |
bool allow(App::Document*, App::DocumentObject*, const char*) override; |
| 298 |
|
| 299 |
private: |
| 300 |
std::string DocName; |
| 301 |
std::string ObjName; |
| 302 |
}; |
| 303 |
|
| 304 |
/** The Selection class |
| 305 |
* The selection singleton keeps track of the selection state of |
| 306 |
* the whole application. It gets messages from all entities which can |
| 307 |
* alter the selection (e.g. tree view and 3D-view) and sends messages |
| 308 |
* to entities which need to keep track on the selection state. |
| 309 |
* |
| 310 |
* The selection consists mainly out of following information per selected object: |
| 311 |
* - document (pointer) |
| 312 |
* - Object (pointer) |
| 313 |
* - list of subelements (list of strings) |
| 314 |
* - 3D coordinates where the user clicks to select (Vector3d) |
| 315 |
* |
| 316 |
* Also the preselection is managed. That means you can add a filter to prevent selection |
| 317 |
* of unwanted objects or subelements. |
| 318 |
*/ |
| 319 |
class GuiExport SelectionSingleton: public Base::Subject<const SelectionChanges&> |
| 320 |
{ |
| 321 |
public: |
| 322 |
struct SelObj |
| 323 |
{ |
| 324 |
const char* DocName; |
| 325 |
const char* FeatName; |
| 326 |
const char* SubName; |
| 327 |
const char* TypeName; |
| 328 |
App::Document* pDoc; |
| 329 |
App::DocumentObject* pObject; |
| 330 |
App::DocumentObject* pResolvedObject; |
| 331 |
float x, y, z; |
| 332 |
}; |
| 333 |
|
| 334 |
/// Add to selection |
| 335 |
bool addSelection( |
| 336 |
const char* pDocName, |
| 337 |
const char* pObjectName = nullptr, |
| 338 |
const char* pSubName = nullptr, |
| 339 |
float x = 0, |
| 340 |
float y = 0, |
| 341 |
float z = 0, |
| 342 |
const std::vector<SelObj>* pickedList = nullptr, |
| 343 |
bool clearPreSelect = true |
| 344 |
); |
| 345 |
bool addSelection2( |
| 346 |
const char* pDocName, |
| 347 |
const char* pObjectName = nullptr, |
| 348 |
const char* pSubName = nullptr, |
| 349 |
float x = 0, |
| 350 |
float y = 0, |
| 351 |
float z = 0, |
| 352 |
const std::vector<SelObj>* pickedList = nullptr |
| 353 |
) |
| 354 |
{ |
| 355 |
return addSelection(pDocName, pObjectName, pSubName, x, y, z, pickedList, false); |
| 356 |
} |
| 357 |
|
| 358 |
/// Add to selection |
| 359 |
bool addSelection(const SelectionObject&, bool clearPreSelect = true); |
| 360 |
/// Add to selection with several sub-elements |
| 361 |
bool addSelections( |
| 362 |
const char* pDocName, |
| 363 |
const char* pObjectName, |
| 364 |
const std::vector<std::string>& pSubNames |
| 365 |
); |
| 366 |
/// Update a selection |
| 367 |
bool updateSelection( |
| 368 |
bool show, |
| 369 |
const char* pDocName, |
| 370 |
const char* pObjectName = nullptr, |
| 371 |
const char* pSubName = nullptr |
| 372 |
); |
| 373 |
/// Remove from selection (for internal use) |
| 374 |
void rmvSelection( |
| 375 |
const char* pDocName, |
| 376 |
const char* pObjectName = nullptr, |
| 377 |
const char* pSubName = nullptr, |
| 378 |
const std::vector<SelObj>* pickedList = nullptr |
| 379 |
); |
| 380 |
/// Set the selection for a document |
| 381 |
void setSelection(const char* pDocName, const std::vector<App::DocumentObject*>&); |
| 382 |
/// Clear the selection of document \a pDocName. If the document name is not given the selection |
| 383 |
/// of the active document is cleared. |
| 384 |
void clearSelection(const char* pDocName = nullptr, bool clearPreSelect = true); |
| 385 |
/// Clear the selection of all documents |
| 386 |
void clearCompleteSelection(bool clearPreSelect = true); |
| 387 |
/// Check if selected |
| 388 |
bool isSelected( |
| 389 |
const char* pDocName, |
| 390 |
const char* pObjectName = nullptr, |
| 391 |
const char* pSubName = nullptr, |
| 392 |
ResolveMode resolve = ResolveMode::OldStyleElement |
| 393 |
) const; |
| 394 |
/// Check if selected |
| 395 |
bool isSelected( |
| 396 |
App::DocumentObject*, |
| 397 |
const char* pSubName = nullptr, |
| 398 |
ResolveMode resolve = ResolveMode::OldStyleElement |
| 399 |
) const; |
| 400 |
|
| 401 |
const char* getSelectedElement(App::DocumentObject*, const char* pSubName) const; |
| 402 |
|
| 403 |
/// set the preselected object (mostly by the 3D view) |
| 404 |
int setPreselect( |
| 405 |
const char* pDocName, |
| 406 |
const char* pObjectName, |
| 407 |
const char* pSubName, |
| 408 |
float x = 0, |
| 409 |
float y = 0, |
| 410 |
float z = 0, |
| 411 |
SelectionChanges::MsgSource signal = SelectionChanges::MsgSource::Any |
| 412 |
); |
| 413 |
/// remove the present preselection |
| 414 |
void rmvPreselect(bool signal = false); |
| 415 |
/// sets different coords for the preselection |
| 416 |
void setPreselectCoord(float x, float y, float z); |
| 417 |
/// returns the present preselection |
| 418 |
const SelectionChanges& getPreselection() const; |
| 419 |
/// add a SelectionGate to control what is selectable |
| 420 |
void addSelectionGate(Gui::SelectionGate* gate, ResolveMode resolve = ResolveMode::OldStyleElement); |
| 421 |
/// remove the active SelectionGate |
| 422 |
void rmvSelectionGate(); |
| 423 |
|
| 424 |
int disableCommandLog(); |
| 425 |
int enableCommandLog(bool silent = false); |
| 426 |
|
| 427 |
/** Returns the number of selected objects with a special object type |
| 428 |
* It's the convenient way to check if the right objects are selected to |
| 429 |
* perform an operation (GuiCommand). The check also detects base types. |
| 430 |
* E.g. "Part" also fits on "PartImport" or "PartTransform types. |
| 431 |
* If no document name is given the active document is assumed. |
| 432 |
* |
| 433 |
* Set 'resolve' to true to resolve any sub object inside selection SubName |
| 434 |
* field. |
| 435 |
* |
| 436 |
* The typename T must be based on App::DocumentObject. |
| 437 |
*/ |
| 438 |
template<typename T> |
| 439 |
inline unsigned int countObjectsOfType( |
| 440 |
const char* pDocName = nullptr, |
| 441 |
ResolveMode resolve = ResolveMode::OldStyleElement |
| 442 |
) const; |
| 443 |
|
| 444 |
/** |
| 445 |
* Does basically the same as the method above unless that it accepts a string literal as first |
| 446 |
* argument. |
| 447 |
* \a typeName must be a registered type, otherwise 0 is returned. |
| 448 |
*/ |
| 449 |
unsigned int countObjectsOfType( |
| 450 |
const char* typeName, |
| 451 |
const char* pDocName = nullptr, |
| 452 |
ResolveMode resolve = ResolveMode::OldStyleElement |
| 453 |
) const; |
| 454 |
|
| 455 |
/** Returns a vector of objects of type \a TypeName selected for the given document name \a |
| 456 |
* pDocName. If no document name is specified the objects from the active document are regarded. |
| 457 |
* If no objects of this document are selected an empty vector is returned. |
| 458 |
* @note The vector reflects the sequence of selection. |
| 459 |
*/ |
| 460 |
std::vector<App::DocumentObject*> getObjectsOfType( |
| 461 |
const Base::Type& typeId, |
| 462 |
const char* pDocName = nullptr, |
| 463 |
ResolveMode resolve = ResolveMode::OldStyleElement |
| 464 |
) const; |
| 465 |
|
| 466 |
/** |
| 467 |
* Does basically the same as the method above unless that it accepts a string literal as first |
| 468 |
* argument. |
| 469 |
* \a typeName must be a registered type otherwise an empty array is returned. |
| 470 |
*/ |
| 471 |
std::vector<App::DocumentObject*> getObjectsOfType( |
| 472 |
const char* typeName, |
| 473 |
const char* pDocName = nullptr, |
| 474 |
ResolveMode resolve = ResolveMode::OldStyleElement |
| 475 |
) const; |
| 476 |
/** |
| 477 |
* A convenience template-based method that returns an array with the correct types already. |
| 478 |
*/ |
| 479 |
template<typename T> |
| 480 |
inline std::vector<T*> getObjectsOfType( |
| 481 |
const char* pDocName = nullptr, |
| 482 |
ResolveMode resolve = ResolveMode::OldStyleElement |
| 483 |
) const; |
| 484 |
|
| 485 |
/// Visible state used by setVisible() |
| 486 |
enum VisibleState |
| 487 |
{ |
| 488 |
/// Hide the selection |
| 489 |
VisHide = 0, |
| 490 |
/// Show the selection |
| 491 |
VisShow = 1, |
| 492 |
/// Toggle visibility of the selection |
| 493 |
VisToggle = -1, |
| 494 |
}; |
| 495 |
|
| 496 |
/** Set selection object visibility |
| 497 |
* |
| 498 |
* @param visible: see VisibleState |
| 499 |
*/ |
| 500 |
void setVisible(VisibleState visible); |
| 501 |
|
| 502 |
bool isClarifySelectionActive(); |
| 503 |
void setClarifySelectionActive(bool active); |
| 504 |
|
| 505 |
/// signal on new object |
| 506 |
boost::signals2::signal<void(const SelectionChanges& msg)> signalSelectionChanged; |
| 507 |
|
| 508 |
/// signal on selection change with resolved object |
| 509 |
boost::signals2::signal<void(const SelectionChanges& msg)> signalSelectionChanged2; |
| 510 |
/// signal on selection change with resolved object and sub element map |
| 511 |
boost::signals2::signal<void(const SelectionChanges& msg)> signalSelectionChanged3; |
| 512 |
|
| 513 |
/** Returns a vector of selection objects |
| 514 |
* |
| 515 |
* @param pDocName: document name. If no document name is given the objects |
| 516 |
* of the active are returned. If nothing for this Document is selected an |
| 517 |
* empty vector is returned. If document name is "*", then all document is |
| 518 |
* considered. |
| 519 |
* @param resolve: sub-object resolving mode |
| 520 |
* @param single: if set to true, then it will return an empty vector if |
| 521 |
* there is more than one selections. |
| 522 |
* |
| 523 |
* @return The returned vector reflects the sequence of selection. |
| 524 |
*/ |
| 525 |
std::vector<SelObj> getSelection( |
| 526 |
const char* pDocName = nullptr, |
| 527 |
ResolveMode resolve = ResolveMode::OldStyleElement, |
| 528 |
bool single = false |
| 529 |
) const; |
| 530 |
/** Returns a vector of selection objects |
| 531 |
* |
| 532 |
* @param pDocName: document name. If no document name is given the objects |
| 533 |
* of the active are returned. If nothing for this Document is selected an |
| 534 |
* empty vector is returned. If document name is "*", then all documents are |
| 535 |
* considered. |
| 536 |
* @param typeId: specify the type of object to be returned. |
| 537 |
* @param resolve: sub-object resolving mode. |
| 538 |
* @param single: if set to true, then it will return an empty vector if |
| 539 |
* there is more than one selection. |
| 540 |
* |
| 541 |
* @return The returned vector reflects the sequence of selection. |
| 542 |
*/ |
| 543 |
std::vector<Gui::SelectionObject> getSelectionEx( |
| 544 |
const char* pDocName = nullptr, |
| 545 |
Base::Type typeId = App::DocumentObject::getClassTypeId(), |
| 546 |
ResolveMode resolve = ResolveMode::OldStyleElement, |
| 547 |
bool single = false |
| 548 |
) const; |
| 549 |
/** Returns a vector of selection objects children of an object |
| 550 |
* |
| 551 |
* @param obj: Object within which you want to find a selection. |
| 552 |
* The selection objects returned will be direct children of Obj. The rest of the subname will |
| 553 |
* be unresolved. So this is equivalent to ResolveMode::NoResolve, but starting from obj. For |
| 554 |
* instance if you have : Assembly.Part.Body.LCS.PlaneXY |
| 555 |
* - If obj = Assembly : SelectionObject = Part, subname = "Body.LCS.PlaneXY." |
| 556 |
* - If obj = Part : SelectionObject = Body, subname = "LCS.PlaneXY." |
| 557 |
* - If obj = Body : SelectionObject = LCS, subname = "PlaneXY." |
| 558 |
* The docname used is the document of obj. |
| 559 |
* if obj is nullptr, this acts as getSelectionEx with empty docName |
| 560 |
* |
| 561 |
* @param typeId: specify the type of object to be returned. |
| 562 |
* @param single: if set to true, then it will return an empty vector if |
| 563 |
* there is more than one selection. |
| 564 |
* |
| 565 |
* @return The returned vector reflects the sequence of selection. |
| 566 |
*/ |
| 567 |
std::vector<Gui::SelectionObject> getSelectionIn( |
| 568 |
App::DocumentObject* obj = nullptr, |
| 569 |
Base::Type typeId = App::DocumentObject::getClassTypeId(), |
| 570 |
bool single = false |
| 571 |
) const; |
| 572 |
|
| 573 |
/** |
| 574 |
* @brief getAsPropertyLinkSubList fills PropertyLinkSubList with current selection. |
| 575 |
* @param prop (output). The property object to receive links |
| 576 |
* @return the number of items written to the link |
| 577 |
*/ |
| 578 |
int getAsPropertyLinkSubList(App::PropertyLinkSubList& prop) const; |
| 579 |
|
| 580 |
/** Returns a vector of all selection objects of all documents. */ |
| 581 |
std::vector<SelObj> getCompleteSelection(ResolveMode resolve = ResolveMode::OldStyleElement) const; |
| 582 |
|
| 583 |
/// Check if there is any selection |
| 584 |
bool hasSelection() const; |
| 585 |
|
| 586 |
/** Check if there is any selection within a given document |
| 587 |
* |
| 588 |
* @param doc: specify the document to check for selection. If NULL, then |
| 589 |
* check the current active document. |
| 590 |
* @param resolve: whether to resolve the selected sub-object |
| 591 |
* |
| 592 |
* If \c resolve is true, then the selection is first resolved before |
| 593 |
* matching its owner document. So in case the selected sub-object is |
| 594 |
* linked from an external document, it may not match the input \c doc. |
| 595 |
* If \c resolve is false, then the match is only done with the top |
| 596 |
* level parent object. |
| 597 |
*/ |
| 598 |
bool hasSelection(const char* doc, ResolveMode resolve = ResolveMode::OldStyleElement) const; |
| 599 |
|
| 600 |
/** Check if there is any sub-element selection |
| 601 |
* |
| 602 |
* @param doc: optional document to check for selection |
| 603 |
* @param subElement: whether to count sub-element only selection |
| 604 |
* |
| 605 |
* Example sub selections are face, edge or vertex. If \c subElement is false, |
| 606 |
* then sub-object (i.e. a group child object) selection is also counted |
| 607 |
* even if it selects the whole sub-object. |
| 608 |
*/ |
| 609 |
bool hasSubSelection(const char* doc = nullptr, bool subElement = false) const; |
| 610 |
|
| 611 |
/// Check if there is any preselection |
| 612 |
bool hasPreselection() const; |
| 613 |
|
| 614 |
/// Size of selected entities for all documents |
| 615 |
unsigned int size() const |
| 616 |
{ |
| 617 |
return static_cast<unsigned int>(_SelList.size()); |
| 618 |
} |
| 619 |
|
| 620 |
/** @name Selection stack functions |
| 621 |
* |
| 622 |
* Selection stack is for storing selection history so that the user can go |
| 623 |
* back and forward to previous selections. |
| 624 |
*/ |
| 625 |
//@{ |
| 626 |
/// Return the current selection stack size |
| 627 |
std::size_t selStackBackSize() const |
| 628 |
{ |
| 629 |
return _SelStackBack.size(); |
| 630 |
} |
| 631 |
|
| 632 |
/// Return the current forward selection stack size |
| 633 |
std::size_t selStackForwardSize() const |
| 634 |
{ |
| 635 |
return _SelStackForward.size(); |
| 636 |
} |
| 637 |
|
| 638 |
/** Obtain selected objects from stack |
| 639 |
* |
| 640 |
* @param pDocName: optional filtering document, NULL for current active |
| 641 |
* document |
| 642 |
* @param resolve: sub-object resolving mode. |
| 643 |
* @param index: optional position in the stack |
| 644 |
*/ |
| 645 |
std::vector<Gui::SelectionObject> selStackGet( |
| 646 |
const char* pDocName = nullptr, |
| 647 |
ResolveMode resolve = ResolveMode::OldStyleElement, |
| 648 |
int index = 0 |
| 649 |
) const; |
| 650 |
|
| 651 |
/** Go back selection history |
| 652 |
* |
| 653 |
* @param count: optional number of steps to go back |
| 654 |
* |
| 655 |
* This function pops the selection stack, and populate the current |
| 656 |
* selection with the content of the last pop'd entry |
| 657 |
*/ |
| 658 |
void selStackGoBack(int count = 1); |
| 659 |
|
| 660 |
/** Go forward selection history |
| 661 |
* |
| 662 |
* @param count: optional number of steps to go back |
| 663 |
* |
| 664 |
* This function pops the selection stack, and populate the current |
| 665 |
* selection with the content of the last pop'd entry |
| 666 |
*/ |
| 667 |
void selStackGoForward(int count = 1); |
| 668 |
|
| 669 |
/** Save the current selection on to the stack |
| 670 |
* |
| 671 |
* @param clearForward: whether to clear forward selection stack |
| 672 |
* @param overwrite: whether to overwrite the current top entry of the |
| 673 |
* stack instead of pushing a new entry. |
| 674 |
*/ |
| 675 |
void selStackPush(bool clearForward = true, bool overwrite = false); |
| 676 |
//@} |
| 677 |
|
| 678 |
/** @name Picked list functions |
| 679 |
* |
| 680 |
* Picked list stores all selected geometry elements that intersects the |
| 681 |
* 3D pick point. The list population is done by SoFCUnifiedSelection through |
| 682 |
* addSelection() with the pickedList argument. |
| 683 |
*/ |
| 684 |
//@{ |
| 685 |
/// Check whether picked list is enabled |
| 686 |
bool needPickedList() const; |
| 687 |
/// Turn on or off picked list |
| 688 |
void enablePickedList(bool); |
| 689 |
/// Check if there is any selection inside picked list |
| 690 |
bool hasPickedList() const; |
| 691 |
/// Return select objects inside picked list |
| 692 |
std::vector<SelectionSingleton::SelObj> getPickedList(const char* pDocName) const; |
| 693 |
/// Return selected object inside picked list grouped by top level parents |
| 694 |
std::vector<Gui::SelectionObject> getPickedListEx( |
| 695 |
const char* pDocName = nullptr, |
| 696 |
Base::Type typeId = App::DocumentObject::getClassTypeId() |
| 697 |
) const; |
| 698 |
//@} |
| 699 |
|
| 700 |
/** @name Selection style functions |
| 701 |
* |
| 702 |
* The selection style changes the way selection works. In Greedy selection |
| 703 |
* it is as if you were pressing Ctrl. |
| 704 |
*/ |
| 705 |
//@{ |
| 706 |
enum class SelectionStyle |
| 707 |
{ |
| 708 |
NormalSelection, |
| 709 |
GreedySelection |
| 710 |
}; |
| 711 |
/// Changes the style of selection between greedy and normal. |
| 712 |
void setSelectionStyle(SelectionStyle selStyle); |
| 713 |
/// Get the style of selection. |
| 714 |
SelectionStyle getSelectionStyle(); |
| 715 |
//@} |
| 716 |
|
| 717 |
static SelectionSingleton& instance(); |
| 718 |
static void destruct(); |
| 719 |
friend class SelectionFilter; |
| 720 |
|
| 721 |
// Python interface |
| 722 |
static PyMethodDef Methods[]; |
| 723 |
|
| 724 |
protected: |
| 725 |
static PyObject* sAddSelection(PyObject* self, PyObject* args); |
| 726 |
static PyObject* sUpdateSelection(PyObject* self, PyObject* args); |
| 727 |
static PyObject* sRemoveSelection(PyObject* self, PyObject* args); |
| 728 |
static PyObject* sClearSelection(PyObject* self, PyObject* args); |
| 729 |
static PyObject* sIsSelected(PyObject* self, PyObject* args); |
| 730 |
static PyObject* sCountObjectsOfType(PyObject* self, PyObject* args); |
| 731 |
static PyObject* sGetSelection(PyObject* self, PyObject* args); |
| 732 |
static PyObject* sSetPreselection(PyObject* self, PyObject* args, PyObject* kwd); |
| 733 |
static PyObject* sGetPreselection(PyObject* self, PyObject* args); |
| 734 |
static PyObject* sRemPreselection(PyObject* self, PyObject* args); |
| 735 |
static PyObject* sGetCompleteSelection(PyObject* self, PyObject* args); |
| 736 |
static PyObject* sGetSelectionEx(PyObject* self, PyObject* args); |
| 737 |
static PyObject* sGetSelectionObject(PyObject* self, PyObject* args); |
| 738 |
static PyObject* sSetSelectionStyle(PyObject* self, PyObject* args); |
| 739 |
static PyObject* sAddSelObserver(PyObject* self, PyObject* args); |
| 740 |
static PyObject* sRemSelObserver(PyObject* self, PyObject* args); |
| 741 |
static PyObject* sAddSelectionGate(PyObject* self, PyObject* args); |
| 742 |
static PyObject* sRemoveSelectionGate(PyObject* self, PyObject* args); |
| 743 |
static PyObject* sGetPickedList(PyObject* self, PyObject* args); |
| 744 |
static PyObject* sEnablePickedList(PyObject* self, PyObject* args); |
| 745 |
static PyObject* sPreselect(PyObject* self, PyObject* args); |
| 746 |
static PyObject* sSetVisible(PyObject* self, PyObject* args); |
| 747 |
static PyObject* sPushSelStack(PyObject* self, PyObject* args); |
| 748 |
static PyObject* sHasSelection(PyObject* self, PyObject* args); |
| 749 |
static PyObject* sHasSubSelection(PyObject* self, PyObject* args); |
| 750 |
static PyObject* sGetSelectionFromStack(PyObject* self, PyObject* args); |
| 751 |
|
| 752 |
protected: |
| 753 |
/// Construction |
| 754 |
SelectionSingleton(); |
| 755 |
/// Destruction |
| 756 |
~SelectionSingleton() override; |
| 757 |
|
| 758 |
/// Observer message from the App doc |
| 759 |
void slotDeletedObject(const App::DocumentObject&); |
| 760 |
|
| 761 |
/// helper to retrieve document by name |
| 762 |
App::Document* getDocument(const char* pDocName = nullptr) const; |
| 763 |
|
| 764 |
void slotSelectionChanged(const SelectionChanges& msg); |
| 765 |
|
| 766 |
SelectionChanges CurrentPreselection; |
| 767 |
|
| 768 |
std::deque<SelectionChanges> NotificationQueue; |
| 769 |
bool Notifying = false; |
| 770 |
|
| 771 |
void notify(SelectionChanges&& Chng); |
| 772 |
void notify(const SelectionChanges& Chng) |
| 773 |
{ |
| 774 |
notify(SelectionChanges(Chng)); |
| 775 |
} |
| 776 |
|
| 777 |
struct _SelObj |
| 778 |
{ |
| 779 |
std::string DocName; |
| 780 |
std::string FeatName; |
| 781 |
std::string SubName; |
| 782 |
std::string TypeName; |
| 783 |
App::Document* pDoc = nullptr; |
| 784 |
App::DocumentObject* pObject = nullptr; |
| 785 |
float x = 0.0f; |
| 786 |
float y = 0.0f; |
| 787 |
float z = 0.0f; |
| 788 |
bool logged = false; |
| 789 |
|
| 790 |
App::ElementNamePair elementName; |
| 791 |
App::DocumentObject* pResolvedObject = nullptr; |
| 792 |
|
| 793 |
void log(bool remove = false, bool clearPreselect = true); |
| 794 |
std::string getSubString() const; |
| 795 |
}; |
| 796 |
mutable std::list<_SelObj> _SelList; |
| 797 |
|
| 798 |
mutable std::list<_SelObj> _PickedList; |
| 799 |
bool _needPickedList {false}; |
| 800 |
|
| 801 |
using SelStackItem = std::set<App::SubObjectT>; |
| 802 |
std::deque<SelStackItem> _SelStackBack; |
| 803 |
std::deque<SelStackItem> _SelStackForward; |
| 804 |
|
| 805 |
int checkSelection( |
| 806 |
const char* pDocName, |
| 807 |
const char* pObjectName, |
| 808 |
const char* pSubName, |
| 809 |
ResolveMode resolve, |
| 810 |
_SelObj& sel, |
| 811 |
const std::list<_SelObj>* selList = nullptr |
| 812 |
) const; |
| 813 |
|
| 814 |
std::vector<Gui::SelectionObject> getObjectList( |
| 815 |
const char* pDocName, |
| 816 |
Base::Type typeId, |
| 817 |
std::list<_SelObj>& objs, |
| 818 |
ResolveMode resolve, |
| 819 |
bool single = false |
| 820 |
) const; |
| 821 |
|
| 822 |
static App::DocumentObject* getObjectOfType( |
| 823 |
_SelObj& sel, |
| 824 |
Base::Type type, |
| 825 |
ResolveMode resolve, |
| 826 |
const char** subelement = nullptr |
| 827 |
); |
| 828 |
|
| 829 |
unsigned int countObjectsOfType( |
| 830 |
const Base::Type& typeId = App::DocumentObject::getClassTypeId(), |
| 831 |
const char* pDocName = nullptr, |
| 832 |
ResolveMode resolve = ResolveMode::OldStyleElement |
| 833 |
) const; |
| 834 |
|
| 835 |
static SelectionSingleton* _pcSingleton; |
| 836 |
|
| 837 |
std::string DocName; |
| 838 |
std::string FeatName; |
| 839 |
std::string SubName; |
| 840 |
float hx, hy, hz; |
| 841 |
|
| 842 |
Gui::SelectionGate* ActiveGate; |
| 843 |
ResolveMode gateResolve; |
| 844 |
|
| 845 |
int logDisabled = 0; |
| 846 |
bool logHasSelection = false; |
| 847 |
bool clarifySelectionActive = false; |
| 848 |
|
| 849 |
SelectionStyle selectionStyle; |
| 850 |
}; |
| 851 |
|
| 852 |
/** |
| 853 |
* A convenience template-based method that returns the number of objects of the given type. |
| 854 |
*/ |
| 855 |
template<typename T> |
| 856 |
inline unsigned int SelectionSingleton::countObjectsOfType(const char* pDocName, ResolveMode resolve) const |
| 857 |
{ |
| 858 |
static_assert( |
| 859 |
std::is_base_of<App::DocumentObject, T>::value, |
| 860 |
"Template parameter T must be derived from App::DocumentObject" |
| 861 |
); |
| 862 |
return this->countObjectsOfType(T::getClassTypeId(), pDocName, resolve); |
| 863 |
} |
| 864 |
|
| 865 |
/** |
| 866 |
* A convenience template-based method that returns an array with the correct types already. |
| 867 |
*/ |
| 868 |
template<typename T> |
| 869 |
inline std::vector<T*> SelectionSingleton::getObjectsOfType(const char* pDocName, ResolveMode resolve) const |
| 870 |
{ |
| 871 |
std::vector<T*> type; |
| 872 |
std::vector<App::DocumentObject*> obj |
| 873 |
= this->getObjectsOfType(T::getClassTypeId(), pDocName, resolve); |
| 874 |
type.reserve(obj.size()); |
| 875 |
for (auto it = obj.begin(); it != obj.end(); ++it) { |
| 876 |
type.push_back(static_cast<T*>(*it)); |
| 877 |
} |
| 878 |
return type; |
| 879 |
} |
| 880 |
|
| 881 |
/// Get the global instance |
| 882 |
inline SelectionSingleton& Selection() |
| 883 |
{ |
| 884 |
return SelectionSingleton::instance(); |
| 885 |
} |
| 886 |
|
| 887 |
/** Helper class to disable logging selection action to MacroManager |
| 888 |
*/ |
| 889 |
class GuiExport SelectionLogDisabler |
| 890 |
{ |
| 891 |
public: |
| 892 |
explicit SelectionLogDisabler(bool silent = false) |
| 893 |
: silent(silent) |
| 894 |
{ |
| 895 |
Selection().disableCommandLog(); |
| 896 |
} |
| 897 |
~SelectionLogDisabler() |
| 898 |
{ |
| 899 |
Selection().enableCommandLog(silent); |
| 900 |
} |
| 901 |
|
| 902 |
private: |
| 903 |
bool silent; |
| 904 |
}; |
| 905 |
|
| 906 |
} // namespace Gui |
| 907 |
|
| 908 |
#endif // GUI_SELECTION_H |