OpenWalnut  1.5.0dev
WPropertyColorWidget.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 <cmath>
26 #include <string>
27 
28 
29 #include <QColorDialog>
30 #include <QDropEvent>
31 #include <QToolButton>
32 #include <QMimeData>
33 
34 #include "core/common/WLogger.h"
35 #include "core/common/WPropertyVariable.h"
36 #include "../WGuiConsts.h"
37 #include "../WQtGui.h"
38 #include "../WMainWindow.h"
39 
40 #include "WPropertyColorWidget.h"
41 
42 WPropertyColorWidget::WPropertyColorWidget( WPropColor property, QGridLayout* propertyGrid, QWidget* parent ):
43  WPropertyWidget( property, propertyGrid, parent ),
44  m_colorProperty( property ),
45  m_widget( &m_parameterWidgets ),
46  m_layout(),
47  m_asText( &m_informationWidgets ),
48  m_infoLayout( &m_informationWidgets )
49 {
50  // initialize members
51  m_parameterWidgets.setLayout( &m_layout );
52 
53  // layout both against each other
54  m_layout.addWidget( &m_widget );
55  m_layout.setMargin( WGLOBAL_MARGIN );
56  m_layout.setSpacing( WGLOBAL_SPACING );
57 
58  // Information Output ( Property Purpose = PV_PURPOSE_INFORMATION )
59  m_infoLayout.addWidget( &m_asText );
60  m_infoLayout.setMargin( WGLOBAL_MARGIN );
61  m_infoLayout.setSpacing( WGLOBAL_SPACING );
62  m_informationWidgets.setLayout( &m_infoLayout );
63 
64  QHBoxLayout* wLayout = new QHBoxLayout( &m_widget );
65  wLayout->setContentsMargins( 0, 0, 0, 0 );
66  wLayout->setSpacing( 0 );
67 
68  m_colButton = new QToolButton( this );
69  m_colorPickerAction = new QAction( WQtGui::getMainWindow()->getIconManager()->getIcon( "colorwheel" ), "Select Color", this );
70  connect( m_colorPickerAction, SIGNAL( triggered( bool ) ), this, SLOT( buttonClicked() ) );
71  m_colButton->setDefaultAction( m_colorPickerAction );
72  m_colButton->setToolButtonStyle( Qt::ToolButtonTextBesideIcon );
73  m_colButton->setSizePolicy( QSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::Fixed ) );
74 
75  m_colPanel = new QPushButton( &m_widget );
76  m_colPanel->setSizePolicy( QSizePolicy( QSizePolicy::Preferred, QSizePolicy::Preferred ) );
77  m_colPanel->setMinimumSize( 24, 24 );
78  connect( m_colPanel, SIGNAL( clicked( bool ) ), this, SLOT( buttonClicked() ) );
79 
80  wLayout->addWidget( m_colPanel );
81  wLayout->addWidget( m_colButton );
82 
83  m_colPanel->setMinimumHeight( WMIN_WIDGET_HEIGHT );
84  m_colButton->setMinimumHeight( WMIN_WIDGET_HEIGHT );
85 
86  // accept drag and drop
87  setAcceptDrops( true );
88 
89  // set the initial values
90  update();
91 }
92 
94 {
95  // cleanup
96 }
97 
99 {
100  m_colButton->setVisible( !hide );
101 }
102 
104 {
105  QColor bgColor = toQColor( m_colorProperty->get() );
106 
107  setColor( bgColor );
108 }
109 
110 void WPropertyColorWidget::setColor( const QColor& bgColor )
111 {
112  std::stringstream buttonColorStr;
113  buttonColorStr << "* { background-color: rgb("
114  << bgColor.red() << ","
115  << bgColor.green() << ","
116  << bgColor.blue() << ");"
117  << "border:none;"
118  << "}";
119 
120  std::stringstream buttonColorText;
121  buttonColorText << "RGBA( " << bgColor.red() << ", "
122  << bgColor.green() << ", "
123  << bgColor.blue() << ", "
124  << bgColor.alpha() << " )";
125 
126  m_colorPickerAction->setText( QString().fromStdString( buttonColorText.str() ) );
127 
128  m_colPanel->setStyleSheet( QString().fromStdString( buttonColorStr.str() ) );
129 
130 
131  // if this is a info property -> set background of label and some text
132  m_asText.setText( QString::fromStdString( m_colorProperty->getAsString() ) );
133  QPalette pal = QPalette();
134  pal.setColor( QPalette::Window, toQColor( m_colorProperty->get() ) );
135  m_asText.setAutoFillBackground( true );
136  m_asText.setPalette( pal );
137 }
138 
139 QColor WPropertyColorWidget::toQColor( WColor color )
140 {
141  QColor tmp;
142  tmp.setRgbF( color[0],
143  color[1],
144  color[2],
145  color[3] );
146 
147  return tmp;
148 }
149 
150 WColor WPropertyColorWidget::toWColor( QColor color )
151 {
152  return WColor( color.redF(), color.greenF(), color.blueF(), color.alphaF() );
153 }
154 
156 {
157  QColor current = toQColor( m_colorProperty->get() );
158 
159 #if QT_VERSION >= 0x040500
160  current = QColorDialog::getColor( current, this, QString( "Select Color" ), QColorDialog::ShowAlphaChannel );
161 #else
162  current = QColorDialog::getColor( current, this );
163 #endif
164  if( current.isValid() )
165  {
166  // set the button background to the appropriate color
167  setColor( current );
168 
169  // convert it back to a WColor
170  invalidate( !m_colorProperty->set( toWColor( current ) ) ); // NOTE: set automatically checks the validity of the value
171  }
172 }
173 
174 void WPropertyColorWidget::dragEnterEvent( QDragEnterEvent* event )
175 {
176  if( event->mimeData()->hasColor() )
177  {
178  event->setAccepted( true );
179  }
180 }
181 
182 void WPropertyColorWidget::dropEvent( QDropEvent* event )
183 {
184  if( event->mimeData()->hasColor() )
185  {
186  QColor color = qvariant_cast<QColor>( event->mimeData()->colorData() );
187  if( color.isValid() )
188  {
189  setColor( color );
190 
191  // now we have to trigger an update
192  invalidate( !m_colorProperty->set( toWColor( color ) ) ); // NOTE: set automatically checks the validity of the value
193  }
194  }
195 }
196 
virtual void dropEvent(QDropEvent *event)
Reimplemented to accept color drops.
QWidget * m_colPanel
Color display panel.
QToolButton * m_colButton
Color picker button.
QHBoxLayout m_layout
Layout used to position the label and the checkbox.
QAction * m_colorPickerAction
Color picker.
WPropertyColorWidget(WPropColor property, QGridLayout *propertyGrid, QWidget *parent=0)
Constructor.
WScaleLabel m_asText
Used to show the property as text.
QWidget m_widget
The button field showing the value.
void buttonClicked()
Called when the m_button is clicked.
static WColor toWColor(QColor color)
Helper to convert between QColor and WColor.
WPropColor m_colorProperty
The integer property represented by this widget.
virtual ~WPropertyColorWidget()
Destructor.
virtual void setColor(const QColor &color)
Internal helper, called to set the color.
void setColorPickerButtonHidden(bool hide=true)
Hide the button for a more compact layout.
virtual void dragEnterEvent(QDragEnterEvent *event)
Reimplemented to accept color drops.
virtual void update()
Called whenever the widget should update.
static QColor toQColor(WColor color)
Helper to convert between QColor and WColor.
QHBoxLayout m_infoLayout
The layout used for the pure output (information properties)
Class building the base for all widgets representing properties.
QWidget m_informationWidgets
The widget containing a layout and provides the widgets for showing information properties.
QWidget m_parameterWidgets
The widget containing a layout and provides the edit widgets for the property.
virtual void invalidate(bool invalid=true)
This method marks this widget as invalid.
virtual bool event(QEvent *event)
Custom event dispatcher.
static WMainWindow * getMainWindow()
Returns the current main window instance or NULL if not existent.
Definition: WQtGui.cpp:88
virtual void setText(const QString &text)
reimplemented function to setText
Definition: WScaleLabel.cpp:93