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, &QLineEdit::textChanged, this, &ColorEditor::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, &QColorDialog::currentColorChanged, property_, &ColorProperty::setColor);
115 
116  // Without this connection the PropertyTreeWidget does not update
117  // the color info "live" when it changes in the dialog and the 3D view.
118  connect(&dialog, &QColorDialog::currentColorChanged, [this]() { parentWidget()->update(); });
119 
120  // On the TWM window manager under linux, and on OSX, this ColorEditor object is destroyed
121  // when (or soon after) the dialog opens.
122  // Therefore, here we delete this ColorEditor immediately to force them all to act the same.
123  deleteLater();
124 
125  // dialog->exec() will call an event loop internally, so
126  // deleteLater() will take effect and "this" will be destroyed.
127  // Therefore, everything we do in this function after dialog->exec()
128  // should only use variables on the stack, not member variables.
129  if (dialog.exec() != QDialog::Accepted)
130  {
131  prop->setColor(original_color);
132  }
133 }
134 
135 } // end namespace rviz
rviz::ColorProperty::getColor
virtual QColor getColor() const
Definition: color_property.h:79
rviz::ColorEditor::property_
ColorProperty * property_
Definition: color_editor.h:63
rviz::ColorEditor::resizeEvent
void resizeEvent(QResizeEvent *event) override
Definition: color_editor.cpp:69
rviz::LineEditWithButton::resizeEvent
void resizeEvent(QResizeEvent *event) override
Definition: line_edit_with_button.cpp:52
rviz::ColorEditor::parseText
void parseText()
Definition: color_editor.cpp:79
rviz::LineEditWithButton
Definition: line_edit_with_button.h:41
rviz::ColorProperty
Definition: color_property.h:40
rviz::printColor
QString printColor(const QColor &color)
Definition: parse_color.cpp:73
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::ColorEditor::paintEvent
void paintEvent(QPaintEvent *event) override
Definition: color_editor.cpp:50
rviz::ColorEditor::color_
QColor color_
Definition: color_editor.h:62
color_editor.h
rviz::ColorEditor::setColor
void setColor(const QColor &color)
Definition: color_editor.cpp:97
rviz::ColorEditor::onButtonClick
void onButtonClick() override
Definition: color_editor.cpp:107
rviz::parseColor
QColor parseColor(const QString &color_string)
Definition: parse_color.cpp:43
rviz::ColorProperty::setColor
virtual bool setColor(const QColor &color)
Definition: color_property.cpp:50
t
geometry_msgs::TransformStamped t
rviz::ColorEditor::ColorEditor
ColorEditor(ColorProperty *property=nullptr, QWidget *parent=nullptr)
Definition: color_editor.cpp:44
parse_color.h


rviz
Author(s): Dave Hershberger, David Gossow, Josh Faust, William Woodall
autogenerated on Sat Jun 1 2024 02:31:52