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 ColorEditor::ColorEditor(ColorProperty* property, QWidget* parent)
45  : LineEditWithButton(parent), property_(property)
46 {
47  connect(this, SIGNAL(textChanged(const QString&)), this, SLOT(parseText()));
48 }
49 
50 void ColorEditor::paintEvent(QPaintEvent* event)
51 {
52  LineEditWithButton::paintEvent(event);
53  QPainter painter(this);
54  painter.setPen(Qt::black);
55  paintColorBox(&painter, rect(), color_);
56 }
57 
58 void ColorEditor::paintColorBox(QPainter* painter, const QRect& rect, const QColor& color)
59 {
60  int padding = 3;
61  int size = rect.height() - padding * 2 - 1;
62  painter->save();
63  painter->setBrush(color);
64  painter->drawRoundedRect(rect.x() + padding + 3, rect.y() + padding, size, size, 0, 0,
65  Qt::AbsoluteSize);
66  painter->restore();
67 }
68 
69 void ColorEditor::resizeEvent(QResizeEvent* event)
70 {
71  // Do the normal line-edit-with-button thing
73 
74  // Then add text padding on the left to make room for the color swatch
75  QMargins marge = textMargins();
76  setTextMargins(height(), marge.top(), marge.right(), marge.bottom());
77 }
78 
80 {
81  const QString t = text();
82  QColor new_color = parseColor(t);
83  if (new_color.isValid())
84  {
85  color_ = new_color;
86  if (property_)
87  {
88  auto pos = cursorPosition();
89  property_->setColor(new_color);
90  // setColor() normalizes the text display and thus looses cursor pos
91  setText(t); // thus: restore original, unnormalized text
92  setCursorPosition(pos); // as well as cursor position
93  }
94  }
95 }
96 
97 void ColorEditor::setColor(const QColor& color)
98 {
99  color_ = color;
100  setText(printColor(color));
101  if (property_)
102  {
103  property_->setColor(color);
104  }
105 }
106 
108 {
109  ColorProperty* prop = property_;
110  QColor original_color = prop->getColor();
111 
112  QColorDialog dialog(color_, window());
113 
114  connect(&dialog, SIGNAL(currentColorChanged(const QColor&)), property_, SLOT(setColor(const QColor&)));
115 
116  // Without this connection the PropertyTreeWidget does not update
117  // the color info "live" when it changes in the dialog and the 3D
118  // view.
119  connect(&dialog, SIGNAL(currentColorChanged(const QColor&)), parentWidget(), SLOT(update()));
120 
121  // On the TWM window manager under linux, and on OSX, this
122  // ColorEditor object is destroyed when (or soon after) the dialog
123  // opens. Therefore, here we delete this ColorEditor immediately to
124  // force them all to act the same.
125  deleteLater();
126 
127  // dialog->exec() will call an event loop internally, so
128  // deleteLater() will take effect and "this" will be destroyed.
129  // Therefore, everything we do in this function after dialog->exec()
130  // should only use variables on the stack, not member variables.
131  if (dialog.exec() != QDialog::Accepted)
132  {
133  prop->setColor(original_color);
134  }
135 }
136 
137 } // end namespace rviz
void resizeEvent(QResizeEvent *event) override
virtual QColor getColor() const
ColorEditor(ColorProperty *property=nullptr, QWidget *parent=nullptr)
geometry_msgs::TransformStamped t
void update(const std::string &key, const XmlRpc::XmlRpcValue &v)
ColorProperty * property_
Definition: color_editor.h:63
static void paintColorBox(QPainter *painter, const QRect &rect, const QColor &color)
void onButtonClick() override
virtual bool setColor(const QColor &color)
QColor parseColor(const QString &color_string)
Definition: parse_color.cpp:43
void resizeEvent(QResizeEvent *event) override
QString printColor(const QColor &color)
Definition: parse_color.cpp:73
void setColor(const QColor &color)
void paintEvent(QPaintEvent *event) override


rviz
Author(s): Dave Hershberger, David Gossow, Josh Faust
autogenerated on Sat May 27 2023 02:06:24