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
00032 #include <QPainter>
00033 #include <QColorDialog>
00034
00035 #include "rviz/properties/color_editor.h"
00036
00037 namespace rviz
00038 {
00039
00040 ColorEditor::ColorEditor( QWidget* parent )
00041 : LineEditWithButton( parent )
00042 {
00043 connect( this, SIGNAL( textEdited( const QString& )),
00044 this, SLOT( invalidateParse() ));
00045 }
00046
00047 void ColorEditor::paintEvent( QPaintEvent* event )
00048 {
00049 LineEditWithButton::paintEvent( event );
00050 QPainter painter( this );
00051 paintColorBox( &painter, rect(), color_ );
00052 }
00053
00054 void ColorEditor::paintColorBox( QPainter* painter, const QRect& rect, const QColor& color )
00055 {
00056 int padding = 1;
00057 int size = rect.height() - padding * 2 - 1;
00058 painter->save();
00059 painter->setPen( Qt::black );
00060 painter->setBrush( color );
00061 painter->drawRect( rect.x() + padding, rect.y() + padding, size, size );
00062 painter->restore();
00063 }
00064
00065 void ColorEditor::resizeEvent( QResizeEvent* event )
00066 {
00067
00068 LineEditWithButton::resizeEvent( event );
00069
00070
00071 QMargins marge = textMargins();
00072 setTextMargins( height(), marge.top(), marge.right(), marge.bottom() );
00073 }
00074
00075 void ColorEditor::invalidateParse()
00076 {
00077 parse_valid_ = false;
00078 }
00079
00080 void ColorEditor::parseText()
00081 {
00082
00083 if( QColor::colorNames().contains( text(), Qt::CaseInsensitive ))
00084 {
00085 color_.setNamedColor( text().toLower() );
00086 parse_valid_ = true;
00087 return;
00088 }
00089
00090 else
00091 {
00092 QStringList list = text().split(QRegExp("[,;]\\s*"));
00093 if( list.size() >= 3 )
00094 {
00095 bool red_ok, green_ok, blue_ok;
00096 QColor new_color( list.at( 0 ).toInt( &red_ok ), list.at( 1 ).toInt( &green_ok ), list.at( 2 ).toInt( &blue_ok ));
00097 if( red_ok && green_ok && blue_ok )
00098 {
00099 color_ = new_color;
00100 parse_valid_ = true;
00101 return;
00102 }
00103 }
00104 }
00105
00106
00107 setColor( color_ );
00108 }
00109
00110 void ColorEditor::setColor( const QColor& color )
00111 {
00112 color_ = color;
00113 setText( QString("%1, %2, %3").arg( color.red() ).arg( color.green() ).arg( color.blue() ) );
00114 parse_valid_ = true;
00115 }
00116
00117 QColor ColorEditor::getColor()
00118 {
00119 if( !parse_valid_ )
00120 {
00121 parseText();
00122 }
00123 return color_;
00124 }
00125
00126 void ColorEditor::onButtonClick()
00127 {
00128 Q_EMIT startPersistence();
00129 QColorDialog* dialog = new QColorDialog( color_, this );
00130 connect( dialog, SIGNAL( colorSelected( const QColor& )),
00131 this, SLOT( setColor( const QColor& )));
00132 if( dialog->exec() == QDialog::Accepted )
00133 {
00134 update();
00135 setModified( true );
00136 }
00137 Q_EMIT endPersistence();
00138 }
00139
00140 }