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  const char* changed_slot,
46  QObject* receiver)
47  : Property(name, QVariant(), description, parent, changed_slot, receiver), color_(default_value)
48 {
49  updateString();
50 }
51 
52 bool ColorProperty::setColor(const QColor& new_color)
53 {
54  if (new_color != color_)
55  {
56  Q_EMIT aboutToChange();
57  color_ = new_color;
58  updateString();
59  Q_EMIT changed();
60  if (model_)
61  model_->emitDataChanged(this);
62  return true;
63  }
64  return false;
65 }
66 
67 bool ColorProperty::setValue(const QVariant& new_value)
68 {
69  if (new_value.type() == QVariant::Color)
70  {
71  return setColor(new_value.value<QColor>());
72  }
73 
74  QColor new_color = parseColor(new_value.toString());
75  if (new_color.isValid())
76  {
77  return setColor(new_color);
78  }
79  return false;
80 }
81 
83 {
85 }
86 
87 bool ColorProperty::paint(QPainter* painter, const QStyleOptionViewItem& option) const
88 {
89  painter->save();
90  QColor color = color_;
91  if (!(getViewFlags(0) & Qt::ItemIsEnabled))
92  {
93  color = QColor(200, 200, 200);
94  painter->setPen(QColor(Qt::lightGray));
95  }
96  QString text = value_.toString();
97  QRect rect = option.rect;
98  ColorEditor::paintColorBox(painter, rect, color);
99  rect.adjust(rect.height() + 4, 1, 0, 0);
100  painter->drawText(rect, text);
101 
102  painter->restore();
103 
104  return true; // return true, since this function has done the painting.
105 }
106 
107 QWidget* ColorProperty::createEditor(QWidget* parent, const QStyleOptionViewItem& /*option*/)
108 {
109  ColorEditor* editor = new ColorEditor(this, parent);
110  editor->setFrame(false);
111  return editor;
112 }
113 
114 } // end namespace rviz
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...
void changed()
Emitted by setValue() just after the value has changed.
PropertyTreeModel * model_
Pointer to the PropertyTreeModel managing this property tree.
Definition: property.h:496
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:482
ColorProperty(const QString &name=QString(), const QColor &default_value=Qt::black, const QString &description=QString(), Property *parent=nullptr, const char *changed_slot=nullptr, QObject *receiver=nullptr)
static void paintColorBox(QPainter *painter, const QRect &rect, const QColor &color)
QWidget * createEditor(QWidget *parent, const QStyleOptionViewItem &option) override
Create an editor widget to edit the value of this property.
virtual Qt::ItemFlags getViewFlags(int column) const
Return item flags appropriate for the given column (0 or 1) for this Property.
Definition: property.cpp:289
virtual bool setColor(const QColor &color)
QColor parseColor(const QString &color_string)
Definition: parse_color.cpp:43
QString printColor(const QColor &color)
Definition: parse_color.cpp:73
void aboutToChange()
Emitted by setValue() just before the value has changed.
void emitDataChanged(Property *property)
bool paint(QPainter *painter, const QStyleOptionViewItem &option) const override
Hook to provide custom painting of the value data (right-hand column) in a subclass.


rviz
Author(s): Dave Hershberger, David Gossow, Josh Faust
autogenerated on Mon Aug 15 2022 02:05:16