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 paintColorBox( &painter, rect(), color_ );
00058 }
00059
00060 void ColorEditor::paintColorBox( QPainter* painter, const QRect& rect, const QColor& color )
00061 {
00062 int padding = 1;
00063 int size = rect.height() - padding * 2 - 1;
00064 painter->save();
00065 painter->setPen( Qt::black );
00066 painter->setBrush( color );
00067 painter->drawRect( rect.x() + padding, rect.y() + padding, size, size );
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
00107
00108
00109
00110
00111 ColorProperty* prop = property_;
00112 QColor original_color = prop->getColor();
00113
00114 QColorDialog* dialog = new QColorDialog( color_, this );
00115
00116
00117
00118
00119
00120
00121 connect( dialog, SIGNAL( currentColorChanged( const QColor& )),
00122 property_, SLOT( setColor( const QColor& )));
00123 connect( dialog, SIGNAL( currentColorChanged( const QColor& )),
00124 this, SLOT( setColor( const QColor& )));
00125
00126 if( dialog->exec() != QDialog::Accepted )
00127 {
00128 #ifdef Q_OS_MAC
00129 prop->setColor( original_color );
00130 #else
00131 setColor( original_color );
00132 #endif
00133 }
00134 }
00135
00136 }