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 
38 
39 namespace rviz
40 {
41 ColorProperty::ColorProperty(const QString& name,
42  const QColor& default_value,
43  const QString& description,
44  Property* parent)
45  : Property(name, QVariant(), description, parent), color_(default_value)
46 {
47  updateString();
48 }
49 
50 bool ColorProperty::setColor(const QColor& new_color)
51 {
52  if (new_color != color_)
53  {
54  Q_EMIT aboutToChange();
55  color_ = new_color;
56  updateString();
57  Q_EMIT changed();
58  if (model_)
59  model_->emitDataChanged(this);
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, const QStyleOptionViewItem& option) const
86 {
87  painter->save();
88  QColor color = color_;
89  if (!(getViewFlags(0) & Qt::ItemIsEnabled))
90  {
91  color = QColor(200, 200, 200);
92  painter->setPen(QColor(Qt::lightGray));
93  }
94  QString text = value_.toString();
95  QRect rect = option.rect;
96  ColorEditor::paintColorBox(painter, rect, color);
97  rect.adjust(rect.height() + 4, 1, 0, 0);
98  painter->drawText(rect, text);
99 
100  painter->restore();
101 
102  return true; // return true, since this function has done the painting.
103 }
104 
105 QWidget* ColorProperty::createEditor(QWidget* parent, const QStyleOptionViewItem& /*option*/)
106 {
107  ColorEditor* editor = new ColorEditor(this, parent);
108  editor->setFrame(false);
109  return editor;
110 }
111 
112 } // end namespace rviz
rviz::ColorEditor
Definition: color_editor.h:38
rviz::ColorProperty::color_
QColor color_
Definition: color_property.h:94
description
description
rviz::ColorProperty::setValue
bool setValue(const QVariant &new_value) override
Set the new value for this property. Returns true if the new value is different from the old value,...
Definition: color_property.cpp:65
rviz::ColorProperty::paint
bool paint(QPainter *painter, const QStyleOptionViewItem &option) const override
Hook to provide custom painting of the value data (right-hand column) in a subclass.
Definition: color_property.cpp:85
rviz::ColorProperty::ColorProperty
ColorProperty(const QString &name=QString(), const QColor &default_value=Qt::black, const QString &description=QString(), Property *parent=nullptr)
Definition: color_property.cpp:41
rviz::Property
A single element of a property tree, with a name, value, description, and possibly children.
Definition: property.h:100
rviz::printColor
QString printColor(const QColor &color)
Definition: parse_color.cpp:73
rviz::PropertyTreeModel::emitDataChanged
void emitDataChanged(Property *property, bool emit_config_changed=true)
Definition: property_tree_model.cpp:294
rviz::ColorEditor::paintColorBox
static void paintColorBox(QPainter *painter, const QRect &rect, const QColor &color)
Definition: color_editor.cpp:58
rviz
Definition: add_display_dialog.cpp:54
color_property.h
rviz::Property::model_
PropertyTreeModel * model_
Pointer to the PropertyTreeModel managing this property tree.
Definition: property.h:560
property_tree_model.h
rviz::ColorProperty::createEditor
QWidget * createEditor(QWidget *parent, const QStyleOptionViewItem &option) override
Create an editor widget to edit the value of this property.
Definition: color_property.cpp:105
color_editor.h
rviz::Property::getViewFlags
virtual Qt::ItemFlags getViewFlags(int column) const
Return item flags appropriate for the given column (0 or 1) for this Property.
Definition: property.cpp:290
default_value
def default_value(type_)
rviz::Property::changed
void changed()
Emitted by setValue() just after the value has changed.
rviz::ColorProperty::updateString
void updateString()
Definition: color_property.cpp:80
rviz::parseColor
QColor parseColor(const QString &color_string)
Definition: parse_color.cpp:43
rviz::Property::value_
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:546
rviz::Property::aboutToChange
void aboutToChange()
Emitted by setValue() just before the value has changed.
rviz::ColorProperty::setColor
virtual bool setColor(const QColor &color)
Definition: color_property.cpp:50
parse_color.h


rviz
Author(s): Dave Hershberger, David Gossow, Josh Faust, William Woodall
autogenerated on Thu May 16 2024 02:30:48