color_editor.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2011, Willow Garage, Inc.
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions are met:
00007  *
00008  *     * Redistributions of source code must retain the above copyright
00009  *       notice, this list of conditions and the following disclaimer.
00010  *     * Redistributions in binary form must reproduce the above copyright
00011  *       notice, this list of conditions and the following disclaimer in the
00012  *       documentation and/or other materials provided with the distribution.
00013  *     * Neither the name of the Willow Garage, Inc. nor the names of its
00014  *       contributors may be used to endorse or promote products derived from
00015  *       this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00018  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00021  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00022  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00023  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00024  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00025  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00026  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00027  * POSSIBILITY OF SUCH DAMAGE.
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   // Do the normal line-edit-with-button thing
00068   LineEditWithButton::resizeEvent( event );
00069 
00070   // Then add text padding on the left to make room for the color swatch
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   // Check for a color name like "black", "red", etc.
00083   if( QColor::colorNames().contains( text(), Qt::CaseInsensitive ))
00084   {
00085     color_.setNamedColor( text().toLower() );
00086     parse_valid_ = true;
00087     return;
00088   }
00089   // Next look for comma-separated list of ints.
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   // Finally, if we can't figure out the string, leave the color
00106   // unchanged and reset the text.
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 } // end namespace rviz


rviz
Author(s): Dave Hershberger, Josh Faust
autogenerated on Mon Jan 6 2014 11:54:32