WFiberHandler.h 8.38 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
// For more information see http://www.openwalnut.org/copying
//
// This file is part of OpenWalnut.
//
// OpenWalnut is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// OpenWalnut is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------

#ifndef WFIBERHANDLER_H
#define WFIBERHANDLER_H

#include <string>
#include <vector>

#include <boost/shared_ptr.hpp>
#include <boost/lexical_cast.hpp>
#include <osg/Geode>

#include "core/common/WItemSelectionItem.h"
#include "core/common/WItemSelectionItemTyped.h"
#include "core/common/WItemSelector.h"
38
#include "core/common/WPropertyGroup.h"
39 40
#include "core/kernel/WModule.h"

41
#include "action/WActionHandler.h"
42 43
#include "WMPointConnector.h"

44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90

/**
 * Handles the fibers of the WMPointsConnector.
 */
class WFiberHandler
{
public:
    /**
     * A shared_ptr to this class.
     */
    typedef boost::shared_ptr< WFiberHandler > SPtr;

    /**
     * Type of selectable items.
     */
    typedef WItemSelectionItemTyped< std::string > ItemType;

    /**
     * Vector of 3D vectors, representing points
     */
    typedef std::vector< osg::Vec3 > PCFiber;

    /**
     * Vector, that could contain multiple fibers
     */
    typedef std::vector< PCFiber > PCFiberList;

    /**
     * Shared pointer to fibers-vector
     */
    typedef boost::shared_ptr< PCFiberList > PCFiberListSPtr;

    /**
     * Creates the list of fibers.
     * \param pointConnector The WMPointConnector this belongs to.
     */
    explicit WFiberHandler( WMPointConnector* pointConnector );

    /**
     * Creates the properties for this handler.
     * \param properties The PropertyGroup to add the properties to.
     */
    void createProperties( WPropertyGroup::SPtr properties );

    /**
     * Adds a vertex to the currently selected fiber.
     * \param vertex The vertex to add.
Robin Eschbach's avatar
Robin Eschbach committed
91
     * \param fiberIdx The index of the fiber to add the vertex to.
92
     * \param silent Whether or not this should add to the undo stack.
93
     */
Robin Eschbach's avatar
Robin Eschbach committed
94
    void addVertexToFiber( osg::Vec3 vertex, size_t fiberIdx, bool silent = false );
95 96 97 98

    /**
     * Adds a vertex to the currently selected fiber.
     * \param vertex The vertex to add.
Robin Eschbach's avatar
Robin Eschbach committed
99
     * \param fiberIdx The index of the fiber to add the vertex to.
100 101 102
     * \param position The position where to add the vertex.
     * \param silent Whether or not this should add to the undo stack.
     */
Robin Eschbach's avatar
Robin Eschbach committed
103
    void addVertexToFiberAt( osg::Vec3 vertex, size_t fiberIdx, size_t position, bool silent = false );
104 105 106 107

    /**
     * Removes a vertex from the currently selected fiber.
     * \param vertex The vertex to remove.
Robin Eschbach's avatar
Robin Eschbach committed
108
     * \param fiberIdx The index of the fiber to remove the vertex from.
109
     * \param silent Whether or not this should add to the undo stack.
110
     */
Robin Eschbach's avatar
Robin Eschbach committed
111 112
    void removeVertexFromFiber( osg::Vec3 vertex, size_t fiberIdx, bool silent = false );

Robin Eschbach's avatar
Robin Eschbach committed
113 114 115 116
    /**
     * Adds a new fiber.
     * \param name The name of the fiber.
     * \param silent Whether or not this should add to the undo stack.
117
     * \param updateSelector Whether the UI selector should be updated
Robin Eschbach's avatar
Robin Eschbach committed
118
     */
119
    void addFiber( std::string name, bool silent = false, bool updateSelector = true );
Robin Eschbach's avatar
Robin Eschbach committed
120

Robin Eschbach's avatar
Robin Eschbach committed
121 122 123 124
    /**
     * Adds a new or an old fiber at a specific position.
     * \param name The name of the fiber.
     * \param position The position of the fiber.
Robin Eschbach's avatar
Robin Eschbach committed
125
     * \param hidden Whether the fiber is hidden or not.
Robin Eschbach's avatar
Robin Eschbach committed
126 127 128
     * \param silent Whether or not this should add to the undo stack.
     * \param fiber The fiber to add.
     */
Robin Eschbach's avatar
Robin Eschbach committed
129
    void addFiberAt( std::string name, size_t position, bool hidden, bool silent = false, PCFiber fiber = PCFiber() );
Robin Eschbach's avatar
Robin Eschbach committed
130

Robin Eschbach's avatar
Robin Eschbach committed
131 132 133 134
    /**
     * Removes a fiber at an index in the fibers vector.
     * \param idx The index of the fiber.
     * \param silent Whether or not this should add to the undo stack.
135
     * \param updateSelector Whether the UI selector should be updated
Robin Eschbach's avatar
Robin Eschbach committed
136
     */
137
    void removeFiber( size_t idx, bool silent = false, bool updateSelector = true );
Robin Eschbach's avatar
Robin Eschbach committed
138

139 140 141 142 143 144 145
    /**
     * Toggles the visibility of a fiber.
     * \param idx The index of the fiber.
     * \param silent Whether or not this should add to the undo stack.
     */
    void toggleFiber( size_t idx, bool silent = false );

146 147
    /**
     * Updates the UI selector.
148 149
     * 
     * \param idx index of the selected fiber.
150
     */
151
    void selectorUpdate( size_t idx );
152 153 154 155 156 157

    /**
     * Clears the data in this handler.
     */
    void clear();

Robin Eschbach's avatar
Robin Eschbach committed
158 159 160 161
    /**
     * Selects a fiber by its index in the fibers vector.
     * \param idx The index of the fiber.
     */
Robin Eschbach's avatar
Robin Eschbach committed
162
    void selectFiber( size_t idx );
163 164 165 166 167 168

    /**
     * Selects the last point of the currently selected fiber.
     */
    void selectLastPoint();

169 170 171 172 173
    /**
     * Sorts the vertices by their z value.
     */
    void sortVertices();

174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
    /**
     * Checks if a fiber is hidden.
     * \param idx The index of the fiber to check.
     * \return true The fiber is hidden.
     * \return false The fiber is not hidden.
     */
    bool isHidden( size_t idx );

    /**
     * Gets the index of the fiber of a point.
     * \param vertex The point to check.
     * \param idx The index of the fiber of the point. This is a pointer and the secondary output. If this is NULL it is ignored.
     * \return true The point is in a fiber.
     * \return false The point is not in a fiber.
     */
    bool getFiberOfPoint( osg::Vec3 vertex, size_t* idx = NULL );

    /**
     * Checks whether a point is in a hidden fiber.
     * \param vertex The point to check.
     * \return true The point is hidden.
     * \return false The point is not hidden.
     */
    bool isPointHidden( osg::Vec3 vertex );

199 200 201 202 203
    /**
     * \return PCFiberListSPtr All the fibers in this handler.
     */
    PCFiberListSPtr getFibers();

Robin Eschbach's avatar
Robin Eschbach committed
204 205 206
    /**
     * \return WMPointConnector* The WMPointConnector this belongs to
     */
207 208
    WMPointConnector* getPointConnector();

Robin Eschbach's avatar
Robin Eschbach committed
209 210 211
    /**
     * \return size_t The currently selected fiber.
     */
Robin Eschbach's avatar
Robin Eschbach committed
212 213
    size_t getSelectedFiber();

Robin Eschbach's avatar
Robin Eschbach committed
214 215 216 217 218
    /**
     * \return WActionHandler::SPtr The WActionHandler of this class.
     */
    WActionHandler::SPtr getActionHandler();

219 220 221 222 223 224
    /**
     * Sets the fiber count.
     * \param fiberCount The new fiber count.
     */
    void setFiberCount( size_t fiberCount );

225 226 227 228 229 230 231 232 233 234 235 236
private:
    /**
     * Update handler for the properties
     * \param property updated property
     */
    void updateProperty( WPropertyBase::SPtr property );

    /**
     * A reference to the WMPointConnector this belongs to.
     */
    WMPointConnector* m_pointConnector;

237 238 239 240 241
    /**
     * Handles the undo and redo action.
     */
    WActionHandler::SPtr m_actionHandler;

242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
    /**
     * Stores the amount of new created fibers.
     */
    int m_fiberCount = 0;

    /**
     * Represents the index of the current active fiber.
     */
    size_t m_selectedFiber = 0;

    /**
     * The list of possible fibers, which can be selected.
     */
    boost::shared_ptr< WItemSelection > m_possibleFiberSelections;

    /**
     * Represents the current active fiber selection property.
     */
    WPropSelection m_fiberSelection;

    /**
263
     * Property (button) to add a new fiber.
264 265 266
     */
    WPropTrigger m_addFiber;

Robin Eschbach's avatar
Robin Eschbach committed
267
    /**
268
     * Property (button) to remove a fiber.
Robin Eschbach's avatar
Robin Eschbach committed
269
     */
Robin Eschbach's avatar
Robin Eschbach committed
270 271
    WPropTrigger m_removeFiber;

272 273 274 275 276
    /**
     * Property (button) to toggle the visibility of a fiber.
     */
    WPropTrigger m_toggleFiber;

Robin Eschbach's avatar
Robin Eschbach committed
277 278 279
    /**
     * Property (button) to undo the last action.
     */
280
    WPropTrigger m_undoTrigger;
Robin Eschbach's avatar
Robin Eschbach committed
281

Robin Eschbach's avatar
Robin Eschbach committed
282 283 284
    /**
     * Property (button) to redo the last action.
     */
285 286
    WPropTrigger m_redoTrigger;

287 288 289 290
    /**
     * A pointer to the list of fibers.
     */
    PCFiberListSPtr m_fibers;
291 292

    /**
Robin Eschbach's avatar
Robin Eschbach committed
293
     * Vector for the visibility of the fibers.
294
     */
295
    boost::shared_ptr< std::vector< char > > m_hidden;
296 297 298
};

#endif  // WFIBERHANDLER_H