libyui-qt  2.43.5
/usr/src/RPM/BUILD/libyui-qt-2.43.5/src/YQMultiLineEdit.cc
00001 /*
00002   Copyright (C) 2000-2012 Novell, Inc
00003   This library is free software; you can redistribute it and/or modify
00004   it under the terms of the GNU Lesser General Public License as
00005   published by the Free Software Foundation; either version 2.1 of the
00006   License, or (at your option) version 3.0 of the License. This library
00007   is distributed in the hope that it will be useful, but WITHOUT ANY
00008   WARRANTY; without even the implied warranty of MERCHANTABILITY or
00009   FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
00010   License for more details. You should have received a copy of the GNU
00011   Lesser General Public License along with this library; if not, write
00012   to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
00013   Floor, Boston, MA 02110-1301 USA
00014 */
00015 
00016 
00017 /*-/
00018 
00019   File:       YQMultiLineEdit.cc
00020 
00021   Author:     Stefan Hundhammer <sh@suse.de>
00022 
00023 /-*/
00024 
00025 
00026 #include <QVBoxLayout>
00027 #include <QTextEdit>
00028 #include <qlabel.h>
00029 #define YUILogComponent "qt-ui"
00030 #include <yui/YUILog.h>
00031 
00032 using std::max;
00033 
00034 #include "utf8.h"
00035 #include "YQUI.h"
00036 #include <yui/YEvent.h>
00037 #include "YQMultiLineEdit.h"
00038 #include "YQSignalBlocker.h"
00039 #include "YQWidgetCaption.h"
00040 
00041 
00042 YQMultiLineEdit::YQMultiLineEdit( YWidget * parent, const std::string & label )
00043     : QFrame( (QWidget *) parent->widgetRep() )
00044     , YMultiLineEdit( parent, label )
00045 {
00046     QVBoxLayout* layout = new QVBoxLayout( this );
00047     setLayout( layout );
00048 
00049     setWidgetRep( this );
00050     layout->setSpacing( YQWidgetSpacing );
00051     layout->setMargin ( YQWidgetMargin  );
00052 
00053     _caption = new YQWidgetCaption( this, label );
00054     YUI_CHECK_NEW( _caption );
00055     layout->addWidget( _caption );
00056 
00057     _qt_textEdit = new QTextEdit( this );
00058     YUI_CHECK_NEW( _qt_textEdit );
00059     layout->addWidget( _qt_textEdit );
00060 
00061     _qt_textEdit->setAcceptRichText( false );
00062     _qt_textEdit->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding ) );
00063 
00064     _caption->setBuddy( _qt_textEdit );
00065 
00066     connect( _qt_textEdit,      SIGNAL( textChanged( void ) ),
00067              this,              SLOT  ( changed    ( void ) ) );
00068 }
00069 
00070 
00071 YQMultiLineEdit::~YQMultiLineEdit()
00072 {
00073     // NOP
00074 }
00075 
00076 
00077 string YQMultiLineEdit::value()
00078 {
00079     return toUTF8( _qt_textEdit->document()->toPlainText() );
00080 }
00081 
00082 
00083 void YQMultiLineEdit::setValue( const std::string & text )
00084 {
00085     YQSignalBlocker sigBlocker( _qt_textEdit );
00086 
00087     _qt_textEdit->setText( fromUTF8( text ) );
00088 }
00089 
00090 
00091 void YQMultiLineEdit::setLabel( const std::string & label )
00092 {
00093     _caption->setText( label );
00094     YMultiLineEdit::setLabel( label );
00095 }
00096 
00097 
00098 void YQMultiLineEdit::setInputMaxLength( int newMaxLength )
00099 {
00100     YMultiLineEdit::setInputMaxLength( newMaxLength );
00101 
00102     QString text = _qt_textEdit->document()->toPlainText();
00103 
00104     if ( (int) text.length() > inputMaxLength() )
00105     {
00106         text.truncate( inputMaxLength() );
00107         _qt_textEdit->setText(text);
00108     }
00109 }
00110 
00111 
00112 void YQMultiLineEdit::enforceMaxInputLength()
00113 {
00114     if ( inputMaxLength() >= 0 && _qt_textEdit->toPlainText().length() > inputMaxLength() )
00115         _qt_textEdit->undo();
00116 }
00117 
00118 
00119 void YQMultiLineEdit::changed()
00120 {
00121     enforceMaxInputLength();
00122 
00123     if ( notify() )
00124         YQUI::ui()->sendEvent( new YWidgetEvent( this, YEvent::ValueChanged ) );
00125 }
00126 
00127 
00128 void YQMultiLineEdit::setEnabled( bool enabled )
00129 {
00130     _caption->setEnabled( enabled );
00131     _qt_textEdit->setEnabled( enabled );
00132     YWidget::setEnabled( enabled );
00133 }
00134 
00135 
00136 int YQMultiLineEdit::preferredWidth()
00137 {
00138     return max( 30, sizeHint().width() );
00139 }
00140 
00141 
00142 int YQMultiLineEdit::preferredHeight()
00143 {
00144     int hintHeight       = defaultVisibleLines() * _qt_textEdit->fontMetrics().lineSpacing();
00145     hintHeight          += _qt_textEdit->frameWidth() * 2 + YQWidgetMargin * 2;
00146 
00147     if ( !_caption->isHidden() )
00148         hintHeight += _caption->sizeHint().height() + YQWidgetSpacing;
00149 
00150     return max( 10, hintHeight );
00151 }
00152 
00153 
00154 void YQMultiLineEdit::setSize( int newWidth, int newHeight )
00155 {
00156     resize( newWidth, newHeight );
00157 }
00158 
00159 
00160 bool YQMultiLineEdit::setKeyboardFocus()
00161 {
00162     _qt_textEdit->setFocus();
00163 
00164     return true;
00165 }
00166 
00167 
00168 #include "YQMultiLineEdit.moc"
00169 
 All Classes Functions Variables