color_property.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012, 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 <QPainter>
31 #include <QStringList>
32 #include <QStyleOptionViewItem>
33 
37 
38 namespace rviz
39 {
40 
41 ColorProperty::ColorProperty( const QString& name,
42  const QColor& default_value,
43  const QString& description,
44  Property* parent,
45  const char *changed_slot,
46  QObject* receiver )
47  : Property( name, QVariant(), description, parent, changed_slot, receiver )
48  , color_( default_value )
49 {
50  updateString();
51 }
52 
53 bool ColorProperty::setColor( const QColor& new_color )
54 {
55  if( new_color != color_ ) {
56  Q_EMIT aboutToChange();
57  color_ = new_color;
58  updateString();
59  Q_EMIT changed();
60  return true;
61  }
62  return false;
63 }
64 
65 bool ColorProperty::setValue( const QVariant& new_value )
66 {
67  if( new_value.type() == QVariant::Color )
68  {
69  return setColor( new_value.value<QColor>() );
70  }
71 
72  QColor new_color = parseColor( new_value.toString() );
73  if( new_color.isValid() )
74  {
75  return setColor( new_color );
76  }
77  return false;
78 }
79 
81 {
83 }
84 
85 bool ColorProperty::paint( QPainter * painter,
86  const QStyleOptionViewItem & option ) const
87 {
88  painter->save();
89  QColor color = color_;
90  if ( !(getViewFlags( 0 ) & Qt::ItemIsEnabled) )
91  {
92  color = QColor( 200, 200, 200 );
93  painter->setPen( QColor( Qt::lightGray ) );
94  }
95  QString text = value_.toString();
96  QRect rect = option.rect;
97  ColorEditor::paintColorBox( painter, rect, color );
98  rect.adjust( rect.height() + 4, 1, 0, 0 );
99  painter->drawText( rect, text );
100 
101  painter->restore();
102 
103  return true; // return true, since this function has done the painting.
104 }
105 
106 QWidget *ColorProperty::createEditor( QWidget* parent,
107  const QStyleOptionViewItem& option )
108 {
109  ColorEditor* editor = new ColorEditor( this, parent );
110  editor->setFrame( false );
111  return editor;
112 }
113 
114 } // end namespace rviz
virtual bool paint(QPainter *painter, const QStyleOptionViewItem &option) const
Hook to provide custom painting of the value data (right-hand column) in a subclass.
void changed()
Emitted by setValue() just after the value has changed.
A single element of a property tree, with a name, value, description, and possibly children...
Definition: property.h:100
QVariant value_
This is the central property value. If you set it directly in a subclass, do so with care because man...
Definition: property.h:440
static void paintColorBox(QPainter *painter, const QRect &rect, const QColor &color)
ColorProperty(const QString &name=QString(), const QColor &default_value=Qt::black, const QString &description=QString(), Property *parent=0, const char *changed_slot=0, QObject *receiver=0)
virtual QWidget * createEditor(QWidget *parent, const QStyleOptionViewItem &option)
Create an editor widget to edit the value of this property.
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 aboutToChange()
Emitted by setValue() just before the value has changed.
virtual bool setValue(const QVariant &new_value)
Set the new value for this property. Returns true if the new value is different from the old value...
virtual Qt::ItemFlags getViewFlags(int column) const
Return item flags appropriate for the given column (0 or 1) for this Property.
Definition: property.cpp:285


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