OpenWalnut  1.5.0dev
WQtBranchTreeItem.cpp
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 #include <map>
26 #include <memory>
27 #include <string>
28 
29 #include <QApplication>
30 #include <QListWidgetItem>
31 #include <QScrollArea>
32 #include <QVBoxLayout>
33 #include <QWidgetAction>
34 #include <QtCore/QList>
35 
36 #include "../WMainWindow.h"
37 #include "../WQtGui.h"
38 #include "../guiElements/WScaleLabel.h"
39 #include "WPropertyBoolWidget.h"
40 #include "WPropertyColorWidget.h"
41 #include "WPropertyDoubleWidget.h"
42 #include "WQtBranchTreeItem.h"
43 #include "WQtPropertyGroupWidget.h"
44 #include "WQtRoiTreeItem.h"
45 #include "WTreeItemTypes.h"
46 
47 WQtBranchTreeItem::WQtBranchTreeItem( QTreeWidgetItem* parent, std::shared_ptr< WRMBranch > branch ) :
48  QTreeWidgetItem( parent, ROIBRANCH ),
49  m_branch( branch )
50 {
51  setFlags( Qt::ItemIsEditable | Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsDropEnabled );
52 
53  // create nice widget
54  m_itemWidget = new QWidget( );
55  QHBoxLayout* containerLayout = new QHBoxLayout();
56  m_itemWidget->setLayout( containerLayout );
57 
58  // create a slider for the for the texture
59  QWidget* labelContainer = new QWidget( m_itemWidget );
60  QHBoxLayout* labelContainerLayout = new QHBoxLayout();
61  labelContainer->setLayout( labelContainerLayout );
62 
63  QWidget* propertyContainer = new QWidget( m_itemWidget );
64  QHBoxLayout* propertyContainerLayout = new QHBoxLayout();
65  propertyContainer->setLayout( propertyContainerLayout );
66  propertyContainer->setObjectName( "propertyContainer" );
67 
68  WPropertyStringWidget* name = new WPropertyStringWidget( branch->nameProperty(), NULL, m_itemWidget );
69  name->forceInformationMode();
70  name->disableTextInteraction();
71  name->setToolTip( "The name of this branch." );
72  labelContainerLayout->addWidget( name );
73 
74  // color
75  WPropertyColorWidget* color = new WPropertyColorWidget( branch->colorProperty(), NULL, m_itemWidget );
76  color->setToolTip( QString::fromStdString( branch->colorProperty()->getDescription() ) );
78 
79  // inverse
80  WPropertyBoolWidget* isnot = new WPropertyBoolWidget( branch->invertProperty(), NULL, m_itemWidget );
81  isnot->setToolTip( QString::fromStdString( branch->invertProperty()->getDescription() ) );
82 
83  QLabel* grabWidget = new QLabel( m_itemWidget );
84  grabWidget->setPixmap( WQtGui::getMainWindow()->getIconManager()->getIcon( "touchpoint_small" ).pixmap( 24, 32 ) );
85  grabWidget->setSizePolicy( QSizePolicy( QSizePolicy::Preferred, QSizePolicy::Preferred ) );
86  grabWidget->setFixedWidth( 24 );
87  grabWidget->setToolTip( "Drag and drop these textures to change their composition ordering." );
88 
89  // style
90  QPalette palette;
91  QColor defaultCol = palette.window().color();
92 
93  // label color
94  QColor labelCol = defaultCol.darker( 150 );
95  // property color
96  QColor propertyCol = defaultCol;
97 
98  name->setStyleSheet( "background-color:" + labelCol.name() + ";" );
99  labelContainer->setStyleSheet( "background-color:" + labelCol.name() + ";" );
100  propertyContainer->setStyleSheet( "QWidget#propertyContainer{ background-color:" + propertyCol.name() + ";}" );
101 
102  propertyContainerLayout->addWidget( isnot );
103  propertyContainerLayout->addWidget( color );
104 
105  // fill layout
106  containerLayout->addWidget( grabWidget );
107  containerLayout->addWidget( labelContainer );
108  containerLayout->addWidget( propertyContainer );
109 
110  // compact layout
111  containerLayout->setContentsMargins( 0, 2, 0, 2 );
112  containerLayout->setSpacing( 0 );
113  labelContainerLayout->setContentsMargins( 5, 2, 0, 2 );
114  labelContainerLayout->setSpacing( 0 );
115  propertyContainerLayout->setContentsMargins( 2, 2, 0, 2 );
116  propertyContainerLayout->setSpacing( 0 );
117 
118  // prefer stretching the label
119  containerLayout->setStretchFactor( grabWidget, 0 );
120  containerLayout->setStretchFactor( labelContainer, 100 );
121  containerLayout->setStretchFactor( propertyContainer, 0 );
122 
123  // widget size constraints and policies
124  m_itemWidget->setSizePolicy( QSizePolicy( QSizePolicy::Preferred, QSizePolicy::Preferred ) );
125 }
126 
128 {
129 }
130 
131 WQtRoiTreeItem* WQtBranchTreeItem::addRoiItem( osg::ref_ptr< WROI > roi )
132 {
133  WQtRoiTreeItem* rti = new WQtRoiTreeItem( this, roi, ROI );
134  return rti;
135 }
136 
137 std::shared_ptr< WRMBranch > WQtBranchTreeItem::getBranch()
138 {
139  return m_branch;
140 }
141 
143 {
144  return m_itemWidget;
145 }
146 
147 /**
148  * Simple comparator class for sorting ROIs in the Roi Manager.
149  */
150 class RoiSort
151 {
152 public:
153  /**
154  * COnstructor.
155  *
156  * \param indexMap the map is used to compare each ROI with their Qt index.
157  */
158  explicit RoiSort( std::map< WROI::RefPtr, int > indexMap ):
159  m_indexMap( indexMap )
160  {
161  // nothing else to initialize
162  }
163 
164  /**
165  * Operator to compare the order of two ROIs
166  *
167  * \param a first ROI
168  * \param b second ROI
169  *
170  * \return true if first is in front of second
171  */
173  {
174  return ( m_indexMap[ a ] < m_indexMap[ b ] );
175  }
176 private:
177  /**
178  * Map needed to know the index in the GUI representation
179  */
180  std::map< WROI::RefPtr, int > m_indexMap;
181 };
182 
184 {
185  std::map< WROI::RefPtr, int > indexMap;
186  // build an index map for each roi to its item
187  for( int i = 0; i < childCount(); ++i )
188  {
189  WQtRoiTreeItem* rti = dynamic_cast< WQtRoiTreeItem* >( child( i ) );
190  WAssert( rti, "All children of a branch item need to be ROI items." );
191  indexMap[ rti->getRoi() ] = i;
192  }
193 
194  // this map now allows to start a comparator
195  m_branch->sort( RoiSort( indexMap ) );
196 }
Simple comparator class for sorting ROIs in the Roi Manager.
RoiSort(std::map< WROI::RefPtr, int > indexMap)
COnstructor.
bool operator()(WROI::RefPtr a, WROI::RefPtr b)
Operator to compare the order of two ROIs.
std::map< WROI::RefPtr, int > m_indexMap
Map needed to know the index in the GUI representation.
Implements a property widget for WPropBool.
Implements a property widget for WPropColor.
void setColorPickerButtonHidden(bool hide=true)
Hide the button for a more compact layout.
Implements a property widget for WPropString.
void disableTextInteraction(bool disable=true)
Disable the ability to select text.
void forceInformationMode(bool force=true)
Force the widget to use the information widgets.
WQtBranchTreeItem(QTreeWidgetItem *parent, std::shared_ptr< WRMBranch > branch)
default constructor
WQtRoiTreeItem * addRoiItem(osg::ref_ptr< WROI >)
Add a ROI to the tree view.
std::shared_ptr< WRMBranch > m_branch
ROI.
std::shared_ptr< WRMBranch > getBranch()
getter
QWidget * getWidget() const
Create a representation widget for this item.
virtual ~WQtBranchTreeItem()
destructor
void updateRoiManagerSorting()
Update internal Roi Manager sorting using the sorting of the children of this tree item.
QWidget * m_itemWidget
Widget representing the item.
static WMainWindow * getMainWindow()
Returns the current main window instance or NULL if not existent.
Definition: WQtGui.cpp:88
A tree widget item to represent a ROI in the control panel.
osg::ref_ptr< WROI > getRoi()
Getter.
osg::ref_ptr< WROI > RefPtr
Ref Pointer type.
Definition: WROI.h:50