| 1 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 |
/*************************************************************************** |
| 3 |
* Copyright (c) 2002 Jürgen Riegel <[email protected]> * |
| 4 |
* * |
| 5 |
* This file is part of the FreeCAD CAx development system. * |
| 6 |
* * |
| 7 |
* This library is free software; you can redistribute it and/or * |
| 8 |
* modify it under the terms of the GNU Library General Public * |
| 9 |
* License as published by the Free Software Foundation; either * |
| 10 |
* version 2 of the License, or (at your option) any later version. * |
| 11 |
* * |
| 12 |
* This library is distributed in the hope that it will be useful, * |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 15 |
* GNU Library General Public License for more details. * |
| 16 |
* * |
| 17 |
* You should have received a copy of the GNU Library General Public * |
| 18 |
* License along with this library; see the file COPYING.LIB. If not, * |
| 19 |
* write to the Free Software Foundation, Inc., 59 Temple Place, * |
| 20 |
* Suite 330, Boston, MA 02111-1307, USA * |
| 21 |
* * |
| 22 |
***************************************************************************/ |
| 23 |
|
| 24 |
#ifndef GUI_DOCKWND_SELECTIONVIEW_H |
| 25 |
#define GUI_DOCKWND_SELECTIONVIEW_H |
| 26 |
|
| 27 |
#include "DockWindow.h" |
| 28 |
#include "Selection.h" |
| 29 |
#include <QMenu> |
| 30 |
#include <QPointer> |
| 31 |
#include <map> |
| 32 |
#include <set> |
| 33 |
#include <string> |
| 34 |
#include <vector> |
| 35 |
|
| 36 |
|
| 37 |
class QListWidget; |
| 38 |
class QListWidgetItem; |
| 39 |
class QCheckBox; |
| 40 |
class QLabel; |
| 41 |
|
| 42 |
namespace App |
| 43 |
{ |
| 44 |
class DocumentObject; |
| 45 |
} |
| 46 |
|
| 47 |
struct ElementInfo; |
| 48 |
struct SubMenuInfo; |
| 49 |
|
| 50 |
namespace Gui |
| 51 |
{ |
| 52 |
namespace DockWnd |
| 53 |
{ |
| 54 |
|
| 55 |
/** A test class. A more elaborate class description. |
| 56 |
*/ |
| 57 |
class SelectionView: public Gui::DockWindow, public Gui::SelectionObserver |
| 58 |
{ |
| 59 |
Q_OBJECT |
| 60 |
|
| 61 |
public: |
| 62 |
/** |
| 63 |
* A constructor. |
| 64 |
* A more elaborate description of the constructor. |
| 65 |
*/ |
| 66 |
explicit SelectionView(Gui::Document* pcDocument, QWidget* parent = nullptr); |
| 67 |
|
| 68 |
/** |
| 69 |
* A destructor. |
| 70 |
* A more elaborate description of the destructor. |
| 71 |
*/ |
| 72 |
~SelectionView() override; |
| 73 |
|
| 74 |
/// Observer message from the Selection |
| 75 |
void onSelectionChanged(const SelectionChanges& msg) override; |
| 76 |
|
| 77 |
void leaveEvent(QEvent*) override; |
| 78 |
|
| 79 |
bool onMsg(const char* pMsg, const char** ppReturn) override; |
| 80 |
|
| 81 |
const char* getName() const override |
| 82 |
{ |
| 83 |
return "SelectionView"; |
| 84 |
} |
| 85 |
|
| 86 |
/// get called when the document is changed or updated |
| 87 |
void onUpdate() override; |
| 88 |
|
| 89 |
QListWidget* selectionView; |
| 90 |
QLabel* countLabel; |
| 91 |
|
| 92 |
QCheckBox* enablePickList; |
| 93 |
QListWidget* pickList; |
| 94 |
|
| 95 |
public Q_SLOTS: |
| 96 |
/// get called when text is entered in the search box |
| 97 |
void search(const QString& text); |
| 98 |
/// get called when enter is pressed in the search box |
| 99 |
void validateSearch(); |
| 100 |
/// get called when the list is right-clicked |
| 101 |
void onItemContextMenu(const QPoint& point); |
| 102 |
/// different actions |
| 103 |
void select(QListWidgetItem* item = nullptr); |
| 104 |
void deselect(); |
| 105 |
void zoom(); |
| 106 |
void treeSelect(); |
| 107 |
void toPython(); |
| 108 |
void touch(); |
| 109 |
void showPart(); |
| 110 |
void onEnablePickList(); |
| 111 |
void toggleSelect(QListWidgetItem* item = nullptr); |
| 112 |
void preselect(QListWidgetItem* item = nullptr); |
| 113 |
|
| 114 |
protected: |
| 115 |
void showEvent(QShowEvent*) override; |
| 116 |
void hideEvent(QHideEvent*) override; |
| 117 |
|
| 118 |
private: |
| 119 |
QString getModule(const char* type) const; |
| 120 |
QString getProperty(App::DocumentObject* obj) const; |
| 121 |
bool supportPart(App::DocumentObject* obj, const QString& part) const; |
| 122 |
|
| 123 |
private: |
| 124 |
float x, y, z; |
| 125 |
std::vector<App::DocumentObject*> searchList; |
| 126 |
bool openedAutomatically; |
| 127 |
}; |
| 128 |
|
| 129 |
} // namespace DockWnd |
| 130 |
|
| 131 |
// Simple selection data structure |
| 132 |
struct PickData |
| 133 |
{ |
| 134 |
App::DocumentObject* obj; |
| 135 |
std::string element; |
| 136 |
std::string docName; |
| 137 |
std::string objName; |
| 138 |
std::string subName; |
| 139 |
}; |
| 140 |
|
| 141 |
// Add SelectionMenu class outside the DockWnd namespace |
| 142 |
class GuiExport SelectionMenu: public QMenu |
| 143 |
{ |
| 144 |
Q_OBJECT |
| 145 |
public: |
| 146 |
SelectionMenu(QWidget* parent = nullptr); |
| 147 |
|
| 148 |
/** Populate and show the menu for picking geometry elements. |
| 149 |
* |
| 150 |
* @param sels: a list of geometry element references |
| 151 |
* @param pos: optional position to show the menu (defaults to current cursor position) |
| 152 |
* @return Return the picked geometry reference |
| 153 |
* |
| 154 |
* The menu will be divided into submenus that are grouped by element type. |
| 155 |
*/ |
| 156 |
PickData doPick(const std::vector<PickData>& sels, const QPoint& pos = QCursor::pos()); |
| 157 |
|
| 158 |
public Q_SLOTS: |
| 159 |
void onHover(QAction*); |
| 160 |
|
| 161 |
protected: |
| 162 |
bool eventFilter(QObject*, QEvent*) override; |
| 163 |
void leaveEvent(QEvent* e) override; |
| 164 |
PickData onPicked(QAction*, const std::vector<PickData>& sels); |
| 165 |
|
| 166 |
private: |
| 167 |
void processSelections(std::vector<PickData>& selections, std::map<std::string, SubMenuInfo>& menus); |
| 168 |
void buildMenuStructure( |
| 169 |
std::map<std::string, SubMenuInfo>& menus, |
| 170 |
const std::vector<PickData>& selections |
| 171 |
); |
| 172 |
|
| 173 |
App::DocumentObject* getSubObject(const PickData& sel); |
| 174 |
std::string extractElementType(const PickData& sel); |
| 175 |
std::string createObjectKey(const PickData& sel); |
| 176 |
QIcon getOrCreateIcon(App::DocumentObject* sobj, std::map<App::DocumentObject*, QIcon>& icons); |
| 177 |
void addGeoFeatureTypes( |
| 178 |
App::DocumentObject* sobj, |
| 179 |
std::map<std::string, SubMenuInfo>& menus, |
| 180 |
std::set<std::string>& createdTypes |
| 181 |
); |
| 182 |
void addWholeObjectSelection( |
| 183 |
const PickData& sel, |
| 184 |
App::DocumentObject* sobj, |
| 185 |
std::vector<PickData>& selections, |
| 186 |
std::map<std::string, SubMenuInfo>& menus, |
| 187 |
const QIcon& icon |
| 188 |
); |
| 189 |
bool shouldGroupMenu(const SubMenuInfo& info); |
| 190 |
void createFlatMenu( |
| 191 |
ElementInfo& elementInfo, |
| 192 |
QMenu* parentMenu, |
| 193 |
const std::string& label, |
| 194 |
const std::string& elementType, |
| 195 |
const std::vector<PickData>& selections |
| 196 |
); |
| 197 |
void createGroupedMenu( |
| 198 |
ElementInfo& elementInfo, |
| 199 |
QMenu* parentMenu, |
| 200 |
const std::string& label, |
| 201 |
const std::string& elementType, |
| 202 |
const std::vector<PickData>& selections |
| 203 |
); |
| 204 |
|
| 205 |
QPointer<QMenu> activeMenu; |
| 206 |
QPointer<QAction> activeAction; |
| 207 |
std::vector<PickData> currentSelections; |
| 208 |
}; |
| 209 |
|
| 210 |
} // namespace Gui |
| 211 |
|
| 212 |
#endif // GUI_DOCKWND_SELECTIONVIEW_H |