| 1 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 |
/**************************************************************************** |
| 3 |
* Copyright (c) 2018 Zheng Lei (realthunder) <[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_SOFCSELECTIONCONTEXT_H |
| 25 |
#define GUI_SOFCSELECTIONCONTEXT_H |
| 26 |
|
| 27 |
#include <limits> |
| 28 |
#include <map> |
| 29 |
#include <memory> |
| 30 |
#include <set> |
| 31 |
#include <vector> |
| 32 |
#include <Inventor/SbColor.h> |
| 33 |
|
| 34 |
#include <App/Material.h> |
| 35 |
|
| 36 |
class SoState; |
| 37 |
|
| 38 |
namespace Gui |
| 39 |
{ |
| 40 |
|
| 41 |
class SoFCSelectionRoot; |
| 42 |
struct SoFCSelectionContextBase; |
| 43 |
using SoFCSelectionContextBasePtr = std::shared_ptr<SoFCSelectionContextBase>; |
| 44 |
|
| 45 |
struct GuiExport SoFCSelectionContextBase |
| 46 |
{ |
| 47 |
virtual ~SoFCSelectionContextBase() = default; |
| 48 |
using MergeFunc = int( |
| 49 |
int status, |
| 50 |
SoFCSelectionContextBasePtr& output, |
| 51 |
SoFCSelectionContextBasePtr input, |
| 52 |
SoNode* node |
| 53 |
); |
| 54 |
}; |
| 55 |
|
| 56 |
struct SoFCSelectionContext; |
| 57 |
using SoFCSelectionContextPtr = std::shared_ptr<SoFCSelectionContext>; |
| 58 |
|
| 59 |
struct GuiExport SoFCSelectionContext: SoFCSelectionContextBase |
| 60 |
{ |
| 61 |
int highlightIndex = -1; |
| 62 |
std::set<int> selectionIndex; |
| 63 |
SbColor selectionColor; |
| 64 |
SbColor highlightColor; |
| 65 |
std::shared_ptr<int> counter; |
| 66 |
|
| 67 |
~SoFCSelectionContext() override; |
| 68 |
|
| 69 |
bool isSelected() const |
| 70 |
{ |
| 71 |
return !selectionIndex.empty(); |
| 72 |
} |
| 73 |
|
| 74 |
void selectAll() |
| 75 |
{ |
| 76 |
selectionIndex.clear(); |
| 77 |
selectionIndex.insert(-1); |
| 78 |
} |
| 79 |
|
| 80 |
bool isSelectAll() const |
| 81 |
{ |
| 82 |
return !selectionIndex.empty() && *selectionIndex.begin() < 0; |
| 83 |
} |
| 84 |
|
| 85 |
bool isHighlighted() const |
| 86 |
{ |
| 87 |
return highlightIndex >= 0; |
| 88 |
} |
| 89 |
|
| 90 |
bool isHighlightAll() const |
| 91 |
{ |
| 92 |
return highlightIndex == std::numeric_limits<int>::max() |
| 93 |
&& (selectionIndex.empty() || isSelectAll()); |
| 94 |
} |
| 95 |
|
| 96 |
void highlightAll() |
| 97 |
{ |
| 98 |
highlightIndex = std::numeric_limits<int>::max(); |
| 99 |
} |
| 100 |
|
| 101 |
void removeHighlight() |
| 102 |
{ |
| 103 |
highlightIndex = -1; |
| 104 |
} |
| 105 |
|
| 106 |
bool removeIndex(int index); |
| 107 |
bool checkGlobal(SoFCSelectionContextPtr ctx); |
| 108 |
|
| 109 |
virtual SoFCSelectionContextBasePtr copy() |
| 110 |
{ |
| 111 |
return std::make_shared<SoFCSelectionContext>(*this); |
| 112 |
} |
| 113 |
|
| 114 |
static MergeFunc merge; |
| 115 |
}; |
| 116 |
|
| 117 |
struct SoFCSelectionContextEx; |
| 118 |
using SoFCSelectionContextExPtr = std::shared_ptr<SoFCSelectionContextEx>; |
| 119 |
|
| 120 |
struct GuiExport SoFCSelectionContextEx: SoFCSelectionContext |
| 121 |
{ |
| 122 |
std::map<int, Base::Color> colors; |
| 123 |
float trans0 = 0.0; |
| 124 |
|
| 125 |
bool setColors(const std::map<std::string, Base::Color>& colors, const std::string& element); |
| 126 |
uint32_t packColor(const Base::Color& c, bool& hasTransparency); |
| 127 |
bool applyColor(int idx, std::vector<uint32_t>& packedColors, bool& hasTransparency); |
| 128 |
bool isSingleColor(uint32_t& color, bool& hasTransparency); |
| 129 |
|
| 130 |
SoFCSelectionContextBasePtr copy() override |
| 131 |
{ |
| 132 |
return std::make_shared<SoFCSelectionContextEx>(*this); |
| 133 |
} |
| 134 |
|
| 135 |
static MergeFunc merge; |
| 136 |
}; |
| 137 |
|
| 138 |
class SoHighlightElementAction; |
| 139 |
class SoSelectionElementAction; |
| 140 |
|
| 141 |
class GuiExport SoFCSelectionCounter |
| 142 |
{ |
| 143 |
public: |
| 144 |
SoFCSelectionCounter(); |
| 145 |
virtual ~SoFCSelectionCounter(); |
| 146 |
bool checkRenderCache(SoState* state); |
| 147 |
void checkAction(SoHighlightElementAction* hlaction); |
| 148 |
void checkAction(SoSelectionElementAction* selaction, SoFCSelectionContextPtr ctx); |
| 149 |
|
| 150 |
protected: |
| 151 |
std::shared_ptr<int> counter; |
| 152 |
bool hasSelection {false}; |
| 153 |
bool hasPreselection {false}; |
| 154 |
static int cachingMode; |
| 155 |
}; |
| 156 |
|
| 157 |
} // namespace Gui |
| 158 |
#endif // GUI_SOFCSELECTIONCONTEXT_H |