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 #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   // Do the normal line-edit-with-button thing
00074   LineEditWithButton::resizeEvent( event );
00075 
00076   // Then add text padding on the left to make room for the color swatch
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   // Without this connection the PropertyTreeWidget does not update
00115   // the color info "live" when it changes in the dialog and the 3D
00116   // view.
00117   connect( dialog, SIGNAL( currentColorChanged( const QColor& )),
00118            parentWidget(), SLOT( update() ));
00119 
00120   // On the TWM window manager under linux, and on OSX, this
00121   // ColorEditor object is destroyed when (or soon after) the dialog
00122   // opens.  Therefore, here we delete this ColorEditor immediately to
00123   // force them all to act the same.
00124   deleteLater();
00125 
00126   // dialog->exec() will call an event loop internally, so
00127   // deleteLater() will take effect and "this" will be destroyed.
00128   // Therefore, everything we do in this function after dialog->exec()
00129   // should only use variables on the stack, not member variables.
00130   if( dialog->exec() != QDialog::Accepted )
00131   {
00132     prop->setColor( original_color );
00133   }
00134 }
00135 
00136 } // end namespace rviz


rviz
Author(s): Dave Hershberger, David Gossow, Josh Faust
autogenerated on Thu Aug 27 2015 15:02:27