Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #include <stdio.h>
00031 #include <QMetaObject>
00032 #include <QMetaProperty>
00033
00034 #include <QPainter>
00035 #include <QColorDialog>
00036
00037 #include "rviz/properties/color_property.h"
00038 #include "rviz/properties/parse_color.h"
00039
00040 #include "rviz/properties/color_editor.h"
00041
00042 namespace rviz
00043 {
00044
00045 ColorEditor::ColorEditor( ColorProperty* property, QWidget* parent )
00046 : LineEditWithButton( parent )
00047 , property_( property )
00048 {
00049 connect( this, SIGNAL( textChanged( const QString& )),
00050 this, SLOT( parseText() ));
00051 }
00052
00053 void ColorEditor::paintEvent( QPaintEvent* event )
00054 {
00055 LineEditWithButton::paintEvent( event );
00056 QPainter painter( this );
00057 painter.setPen( Qt::black );
00058 paintColorBox( &painter, rect(), color_ );
00059 }
00060
00061 void ColorEditor::paintColorBox( QPainter* painter, const QRect& rect, const QColor& color )
00062 {
00063 int padding = 3;
00064 int size = rect.height() - padding * 2 - 1;
00065 painter->save();
00066 painter->setBrush( color );
00067 painter->drawRoundedRect( rect.x() + padding + 3, rect.y() + padding, size, size, 0, 0, Qt::AbsoluteSize );
00068 painter->restore();
00069 }
00070
00071 void ColorEditor::resizeEvent( QResizeEvent* event )
00072 {
00073
00074 LineEditWithButton::resizeEvent( event );
00075
00076
00077 QMargins marge = textMargins();
00078 setTextMargins( height(), marge.top(), marge.right(), marge.bottom() );
00079 }
00080
00081 void ColorEditor::parseText()
00082 {
00083 QColor new_color = parseColor( text() );
00084 if( new_color.isValid() )
00085 {
00086 color_ = new_color;
00087 if( property_ )
00088 {
00089 property_->setColor( new_color );
00090 }
00091 }
00092 }
00093
00094 void ColorEditor::setColor( const QColor& color )
00095 {
00096 color_ = color;
00097 setText( printColor( color ));
00098 if( property_ )
00099 {
00100 property_->setColor( color );
00101 }
00102 }
00103
00104 void ColorEditor::onButtonClick()
00105 {
00106 ColorProperty* prop = property_;
00107 QColor original_color = prop->getColor();
00108
00109 QColorDialog* dialog = new QColorDialog( color_, parentWidget() );
00110
00111 connect( dialog, SIGNAL( currentColorChanged( const QColor& )),
00112 property_, SLOT( setColor( const QColor& )));
00113
00114
00115
00116
00117 connect( dialog, SIGNAL( currentColorChanged( const QColor& )),
00118 parentWidget(), SLOT( update() ));
00119
00120
00121
00122
00123
00124 deleteLater();
00125
00126
00127
00128
00129
00130 if( dialog->exec() != QDialog::Accepted )
00131 {
00132 prop->setColor( original_color );
00133 }
00134 }
00135
00136 }