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