line_edit_with_button.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 "rviz/properties/line_edit_with_button.h"
00031 
00032 #include <QPushButton>
00033 #include <QLineEdit>
00034 #include <QHBoxLayout>
00035 #include <QKeyEvent>
00036 #include <QApplication>
00037 
00038 namespace rviz
00039 {
00040 
00041 LineEditWithButton::LineEditWithButton( QWidget* parent )
00042   : QLineEdit( parent )
00043 {
00044   button_ = new QPushButton( this );
00045   button_->setText( "..." );
00046   button_->setCursor( Qt::ArrowCursor );
00047   button_->setDefault( false );
00048   button_->setAutoDefault( false );
00049   button_->setFocusPolicy( Qt::NoFocus );
00050 
00051   connect( button_, SIGNAL( clicked() ), this, SLOT( onButtonClick() ));
00052 }
00053 
00054 void LineEditWithButton::resizeEvent( QResizeEvent* event )
00055 {
00056   // This widget does not use a QLayout object, because I want the
00057   // child button to be sized and positioned a specific way.  This
00058   // function lays out the child button based on the new size of the
00059   // LineEdit.
00060 
00061   int padding = 1;
00062 
00063   // Make sure the text area doesn't go under the button.
00064   setTextMargins( padding, padding, height(), padding );
00065 
00066   // Call the original resize handler.
00067   QLineEdit::resizeEvent( event );
00068 
00069   // Make the button square, matching the height of this widget minus
00070   // padding, and located all the way to the right.
00071   int button_width = height() - 2 * padding;
00072   int button_height = button_width;
00073   button_->setGeometry( width() - button_width - padding, padding,
00074                         button_width, button_height );
00075 }
00076 
00077 void LineEditWithButton::simulateReturnPressed()
00078 {
00079   // I couldn't find a way to directly tell the editor that I was
00080   // done with it here.  "Q_EMIT returnPressed()", "Q_EMIT
00081   // editingFinished()" etc did nothing.  So instead, here I
00082   // simulate the user pressing and releasing the "Return" key,
00083   // which does indeed make it act like I want: when you select a
00084   // topic from the dialog and the dialog closes, the property's
00085   // Setter is called and this editor closes.
00086   QKeyEvent* event = new QKeyEvent( QEvent::KeyPress, Qt::Key_Return, Qt::NoModifier );
00087   QApplication::postEvent( this, event );
00088   event = new QKeyEvent( QEvent::KeyRelease, Qt::Key_Return, Qt::NoModifier );
00089   QApplication::postEvent( this, event );
00090 }
00091 
00092 } // end namespace rviz


rviz
Author(s): Dave Hershberger, David Gossow, Josh Faust
autogenerated on Mon Oct 6 2014 07:26:35