color_editor.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2011, Willow Garage, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of the Willow Garage, Inc. nor the names of its
14  * contributors may be used to endorse or promote products derived from
15  * this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #include <stdio.h>
31 #include <QMetaObject>
32 #include <QMetaProperty>
33 
34 #include <QPainter>
35 #include <QColorDialog>
36 
39 
41 
42 namespace rviz
43 {
44 
45 ColorEditor::ColorEditor( ColorProperty* property, QWidget* parent )
46  : LineEditWithButton( parent )
47  , property_( property )
48 {
49  connect( this, SIGNAL( textChanged( const QString& )),
50  this, SLOT( parseText() ));
51 }
52 
53 void ColorEditor::paintEvent( QPaintEvent* event )
54 {
55  LineEditWithButton::paintEvent( event );
56  QPainter painter( this );
57  painter.setPen( Qt::black );
58  paintColorBox( &painter, rect(), color_ );
59 }
60 
61 void ColorEditor::paintColorBox( QPainter* painter, const QRect& rect, const QColor& color )
62 {
63  int padding = 3;
64  int size = rect.height() - padding * 2 - 1;
65  painter->save();
66  painter->setBrush( color );
67  painter->drawRoundedRect( rect.x() + padding + 3, rect.y() + padding, size, size, 0, 0, Qt::AbsoluteSize );
68  painter->restore();
69 }
70 
71 void ColorEditor::resizeEvent( QResizeEvent* event )
72 {
73  // Do the normal line-edit-with-button thing
75 
76  // Then add text padding on the left to make room for the color swatch
77  QMargins marge = textMargins();
78  setTextMargins( height(), marge.top(), marge.right(), marge.bottom() );
79 }
80 
82 {
83  QColor new_color = parseColor( text() );
84  if( new_color.isValid() )
85  {
86  color_ = new_color;
87  if( property_ )
88  {
89  property_->setColor( new_color );
90  }
91  }
92 }
93 
94 void ColorEditor::setColor( const QColor& color )
95 {
96  color_ = color;
97  setText( printColor( color ));
98  if( property_ )
99  {
100  property_->setColor( color );
101  }
102 }
103 
105 {
106  ColorProperty* prop = property_;
107  QColor original_color = prop->getColor();
108 
109  QColorDialog* dialog = new QColorDialog( color_, parentWidget() );
110 
111  connect( dialog, SIGNAL( currentColorChanged( const QColor& )),
112  property_, SLOT( setColor( const QColor& )));
113 
114  // Without this connection the PropertyTreeWidget does not update
115  // the color info "live" when it changes in the dialog and the 3D
116  // view.
117  connect( dialog, SIGNAL( currentColorChanged( const QColor& )),
118  parentWidget(), SLOT( update() ));
119 
120  // On the TWM window manager under linux, and on OSX, this
121  // ColorEditor object is destroyed when (or soon after) the dialog
122  // opens. Therefore, here we delete this ColorEditor immediately to
123  // force them all to act the same.
124  deleteLater();
125 
126  // dialog->exec() will call an event loop internally, so
127  // deleteLater() will take effect and "this" will be destroyed.
128  // Therefore, everything we do in this function after dialog->exec()
129  // should only use variables on the stack, not member variables.
130  if( dialog->exec() != QDialog::Accepted )
131  {
132  prop->setColor( original_color );
133  }
134 }
135 
136 } // end namespace rviz
virtual QColor getColor() const
virtual void paintEvent(QPaintEvent *event)
virtual void onButtonClick()
virtual void resizeEvent(QResizeEvent *event)
virtual void resizeEvent(QResizeEvent *event)
void update(const std::string &key, const XmlRpc::XmlRpcValue &v)
ColorProperty * property_
Definition: color_editor.h:64
static void paintColorBox(QPainter *painter, const QRect &rect, const QColor &color)
virtual bool setColor(const QColor &color)
QColor parseColor(const QString &color_string)
Definition: parse_color.cpp:44
QString printColor(const QColor &color)
Definition: parse_color.cpp:74
void setColor(const QColor &color)
ColorEditor(ColorProperty *property=0, QWidget *parent=0)


rviz
Author(s): Dave Hershberger, David Gossow, Josh Faust
autogenerated on Wed Aug 28 2019 04:01:50