OpenWalnut  1.5.0dev
WTransferFunctionWidget.h
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #ifndef WTRANSFERFUNCTIONWIDGET_H
26 #define WTRANSFERFUNCTIONWIDGET_H
27 
28 #include <vector>
29 
30 #include "QGraphicsView"
31 
32 
33 class QGraphicsScene;
38 
39 class WTransferFunction; // I want to remove this if possible to keep the code clean from OW datatypes
40 
41 /**
42  * The class managing the widget that wants to receive updates whenever
43  * a change occurrs. This is only here to provide an interface and a better
44  * separation between gui code and the transfer function code
45  */
47 {
48  /**
49  * default destructor
50  */
52  {
53  }
54 
55  /**
56  * update the gui
57  * \param tf the new transfer function
58  */
59  virtual void guiUpdate( const WTransferFunction& tf ) = 0;
60 };
61 
62 /**
63  * A widget that holds a scene to display and modify the transfer function.
64  *
65  * Currently most data storage is done in this class, which is not a great
66  * design. Thus, we should try to split the model and the view a bit more,
67  * even though this is not intended by QGraphicsView.
68  */
70 {
71  Q_OBJECT
72 
73 public:
74  /** type of our base class for easier coding */
76 
77  /**
78  * Constructor. Create a TF widget with a given parent.
79  *
80  * \param qparent parent widgeet
81  * \param parent a class that receives notifications
82  */
83  WTransferFunctionWidget( QWidget* qparent = NULL, WTransferFunctionGuiNotificationClass* parent = NULL );
84 
85  /**
86  * Destructor.
87  */
88  virtual ~WTransferFunctionWidget();
89 
90  /**
91  * Called from external to set histogram.
92  *
93  * \param histogram The histogram data
94  */
95  void setHistogram( const std::vector< double > &histogram );
96 
97  /**
98  * Redraw the widget
99  */
100  void forceRedraw();
101 
102  /**
103  * Set the current active point => this should be changed to QGraphicsScene
104  * object selection.
105  *
106  * \param current the new selection
107  */
109  {
110  this->current = current;
111  this->ccurrent = 0x0;
112  }
113 
114  /**
115  * Similar to setCurrent but for Color control points.
116  *
117  * \param ccurrent the new seelection
118  */
120  {
121  this->ccurrent = ccurrent;
122  this->current = 0x0;
123  }
124 
125  /**
126  * Sample the transfer function into a 1D RGBA texture.
127  *
128  * \param array target byte array of RGBA values
129  * \param width width of array in number of color values
130  */
131  void sample1DTransferFunction( unsigned char*array, int width );
132 
133  /**
134  * Sample the transfer function into a 1D RGBA, ABGR, ARGB, ... whatever...
135  * This function should work on the graphics native system, but I did not find the
136  * function to get information about the alignment, yet.
137  * (X11 Linux, little endian: ABRG. OSX: ARGB, X11 on OSX from Linux host->???)
138  *
139  * \param array target byte array of RGBA values
140  * \param width width of array in number of color values
141  */
142  void sample1DTransferFunctionForDisplay( unsigned char*array, int width );
143 
144  /**
145  * Insert a new alpha control point at scene position position.
146  * \param position the position of the point with position.x = iso and position.y = alpha in window coordinates.
147  */
148  void insertPoint( const QPointF& position );
149 
150  /**
151  * Same as insertPoint but in normalized coordinates and not in screen space.
152  *
153  * \param position: same as in insertPoint, but scaled to [ 0..1 ]x[ 0..1 ]
154  */
155  void insertPointNormalized( const QPointF& position );
156 
157 
158  /**
159  * Insert a new color control point at scene position pos (only the x-value is relevant)
160  * If a color is provided, it is used. Otherwise, a new color is used interpolating the
161  * current color map.
162  *
163  * \param pos: the clicked position where pos.x = iso and pos.y is ignored
164  * \param color: the color of the new point. If color = 0, a new color is created by interpolating the neighbors.
165  */
166  void insertColor( const QPointF& pos, QColor const* const color = 0 );
167 
168  /**
169  * Same as insertColor but in normalized coordinates, i.e., [ 0...1 ] along x.
170  *
171  * \param pos the position
172  * \param color the new color ( my be 0 and will then be interpolated linearly )
173  */
174  void insertColorNormalized( const QPointF& pos, QColor const *const color = 0 );
175 
176  /**
177  * Remove all points from the transfer function widget to be able to insert new points.
178  */
179  void clearTransferFunction();
180 
181 public slots:
182 
183  /**
184  * Notification that the data changed, i.e., a control point has been moved or a color changed.
185  */
186  void dataChanged();
187 
188 protected:
189  /**
190  * Draws the background.
191  *
192  * \param painter the painter to use
193  * \param rect the QRectF to repaint
194  */
195  virtual void drawBackground( QPainter *painter, const QRectF &rect );
196 
197  /**
198  * Interactions implemented so far:
199  * right click: insert new object (Ctrl+left click or two-finger tap on OSX)
200  * left click on objects: (handled by individual objects) move control points and select
201  * object
202  * double click on objects: (handled by individual objects) open parameter dialog, e.g.,
203  * to change the color
204  * "Delete" or "Backspace": delete selected itom
205  * \param event the event to handle
206  */
207  virtual void keyPressEvent( QKeyEvent *event );
208 
209  /**
210  * For a documentation of the implemented actions confer the keyPressEvent documentation.
211  * \param event the event to handle
212  */
213  virtual void mousePressEvent( QMouseEvent *event );
214 
215  /**
216  * Internal helper function: Find the point to the left of the given point.
217  *
218  * \param position in the widget
219  * \returns an alpha point left of the given position, i.e., with return.x < x_position
220  */
221  WTransferFunctionPoint* findPointOnLeft( QPointF position );
222 
223  /**
224  * Internal helper function: Find the point to the left of the given color control point.
225  *
226  * \param position a position
227  * \returns a color point left of the given position, i.e., with return.x < x_position
228  */
229  WTransferFunctionColorPoint* findCPointOnLeft( QPointF position );
230 
231  /**
232  * Updates the transfer function.
233  */
234  void updateTransferFunction();
235 
236  /**
237  * Internal helper function to update the QGraphicsPixmapItem that holds a representation
238  * of the current color map and displays it as a background of the widget.
239  * This function samples the texture into a QPixmap and updates the QGraphicsPixmapItem.
240  */
241  void setMyBackground();
242 
243 private:
244  /** the class that receives our update notifications */
246 
247  /** our scene */
249 
250  /** linked list of alpha items */
251  WTransferFunctionPoint *first; //! first element
252  WTransferFunctionPoint *last; //! last element in list
253  WTransferFunctionPoint *current; //! currently selected/active element
254 
255  /** linked list of color items */
258  WTransferFunctionColorPoint *ccurrent; //! currently selected/active color element
259 
260  WTransferFunctionBackground *background; //! background that displays the color map
261  WTransferFunctionHistogram *histogram; //! item responsible for displaying histogram data
262 
263  bool initialized; //< set to true after initialization
264 };
265 
266 #endif // WTRANSFERFUNCTIONWIDGET_H
A QGraphicsItem that displays a pixmap in the background of the scene.
A control point for the color function.
Display a semi-transparent line graph as the histogram of the current data set.
A control point for the alpha function.
A widget that holds a scene to display and modify the transfer function.
void setCurrent(WTransferFunctionPoint *current)
Set the current active point => this should be changed to QGraphicsScene object selection.
void insertPoint(const QPointF &position)
Insert a new alpha control point at scene position position.
virtual ~WTransferFunctionWidget()
Destructor.
WTransferFunctionGuiNotificationClass * parent
the class that receives our update notifications
WTransferFunctionPoint * findPointOnLeft(QPointF position)
Internal helper function: Find the point to the left of the given point.
void setMyBackground()
Internal helper function to update the QGraphicsPixmapItem that holds a representation of the current...
virtual void keyPressEvent(QKeyEvent *event)
Interactions implemented so far: right click: insert new object (Ctrl+left click or two-finger tap on...
WTransferFunctionColorPoint * findCPointOnLeft(QPointF position)
Internal helper function: Find the point to the left of the given color control point.
void setCurrentColor(WTransferFunctionColorPoint *ccurrent)
Similar to setCurrent but for Color control points.
void insertColorNormalized(const QPointF &pos, QColor const *const color=0)
Same as insertColor but in normalized coordinates, i.e., [ 0...1 ] along x.
WTransferFunctionPoint * current
last element in list
WTransferFunctionHistogram * histogram
background that displays the color map
bool initialized
item responsible for displaying histogram data
void updateTransferFunction()
Updates the transfer function.
WTransferFunctionColorPoint * ccurrent
last element
void sample1DTransferFunctionForDisplay(unsigned char *array, int width)
Sample the transfer function into a 1D RGBA, ABGR, ARGB, ...
QGraphicsView BaseClass
type of our base class for easier coding
WTransferFunctionPoint * last
first element
QGraphicsScene * scene
our scene
virtual void drawBackground(QPainter *painter, const QRectF &rect)
Draws the background.
void insertColor(const QPointF &pos, QColor const *const color=0)
Insert a new color control point at scene position pos (only the x-value is relevant) If a color is p...
void forceRedraw()
Redraw the widget.
void setHistogram(const std::vector< double > &histogram)
Called from external to set histogram.
WTransferFunctionPoint * first
linked list of alpha items
virtual void mousePressEvent(QMouseEvent *event)
For a documentation of the implemented actions confer the keyPressEvent documentation.
WTransferFunctionColorPoint * clast
first element
void insertPointNormalized(const QPointF &position)
Same as insertPoint but in normalized coordinates and not in screen space.
void dataChanged()
Notification that the data changed, i.e., a control point has been moved or a color changed.
WTransferFunctionBackground * background
currently selected/active color element
void clearTransferFunction()
Remove all points from the transfer function widget to be able to insert new points.
WTransferFunctionColorPoint * cfirst
currently selected/active element
WTransferFunctionWidget(QWidget *qparent=NULL, WTransferFunctionGuiNotificationClass *parent=NULL)
Constructor.
void sample1DTransferFunction(unsigned char *array, int width)
Sample the transfer function into a 1D RGBA texture.
A class that stores a 1D transfer function which consists of a linear interpolation of alpha and colo...
The class managing the widget that wants to receive updates whenever a change occurrs.
virtual void guiUpdate(const WTransferFunction &tf)=0
update the gui
virtual ~WTransferFunctionGuiNotificationClass()
default destructor