1 /*---------------------------------------------------------------------\
3 | __ __ ____ _____ ____ |
4 | \ \ / /_ _/ ___|_ _|___ \ |
5 | \ V / _` \___ \ | | __) | |
6 | | | (_| |___) || | / __/ |
7 | |_|\__,_|____/ |_| |_____| |
11 \----------------------------------------------------------------------/
13 File: YQMultiLineEdit.cc
15 Author: Stefan Hundhammer <sh@suse.de>
20 #include <QVBoxLayout>
23 #define y2log_component "qt-ui"
24 #include <ycp/y2log.h>
31 #include "YQMultiLineEdit.h"
32 #include "YQSignalBlocker.h"
33 #include "YQWidgetCaption.h"
36 YQMultiLineEdit::YQMultiLineEdit( YWidget * parent, const string & label )
37 : QFrame( (QWidget *) parent->widgetRep() )
38 , YMultiLineEdit( parent, label )
40 QVBoxLayout* layout = new QVBoxLayout( this );
44 layout->setSpacing( YQWidgetSpacing );
45 layout->setMargin ( YQWidgetMargin );
47 _caption = new YQWidgetCaption( this, label );
48 YUI_CHECK_NEW( _caption );
49 layout->addWidget( _caption );
51 _qt_textEdit = new QTextEdit( this );
52 YUI_CHECK_NEW( _qt_textEdit );
53 layout->addWidget( _qt_textEdit );
55 //_qt_textEdit->setTextFormat( Qt::PlainText );
56 _qt_textEdit->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding ) );
58 _caption->setBuddy( _qt_textEdit );
60 connect( _qt_textEdit, SIGNAL( textChanged( void ) ),
61 this, SLOT ( changed ( void ) ) );
65 YQMultiLineEdit::~YQMultiLineEdit()
71 string YQMultiLineEdit::value()
73 return toUTF8( _qt_textEdit->document()->toPlainText() );
77 void YQMultiLineEdit::setValue( const string & text )
79 YQSignalBlocker sigBlocker( _qt_textEdit );
81 _qt_textEdit->setText( fromUTF8( text ) );
85 void YQMultiLineEdit::setLabel( const string & label )
87 _caption->setText( label );
88 YMultiLineEdit::setLabel( label );
92 void YQMultiLineEdit::setInputMaxLength( int newMaxLength )
94 YMultiLineEdit::setInputMaxLength( newMaxLength );
96 QString text = _qt_textEdit->document()->toPlainText();
98 if ( (int) text.length() > inputMaxLength() )
100 text.truncate( inputMaxLength() );
101 _qt_textEdit->setText(text);
106 void YQMultiLineEdit::enforceMaxInputLength()
108 if ( inputMaxLength() >= 0 && _qt_textEdit->toPlainText().length() > inputMaxLength() )
109 _qt_textEdit->undo();
113 void YQMultiLineEdit::changed()
115 enforceMaxInputLength();
118 YQUI::ui()->sendEvent( new YWidgetEvent( this, YEvent::ValueChanged ) );
122 void YQMultiLineEdit::setEnabled( bool enabled )
124 _caption->setEnabled( enabled );
125 _qt_textEdit->setEnabled( enabled );
126 YWidget::setEnabled( enabled );
130 int YQMultiLineEdit::preferredWidth()
132 return max( 30, sizeHint().width() );
136 int YQMultiLineEdit::preferredHeight()
138 int hintHeight = defaultVisibleLines() * _qt_textEdit->fontMetrics().lineSpacing();
139 hintHeight += _qt_textEdit->frameWidth() * 2 + YQWidgetMargin * 2;
141 if ( !_caption->isHidden() )
142 hintHeight += _caption->sizeHint().height() + YQWidgetSpacing;
144 return max( 10, hintHeight );
148 void YQMultiLineEdit::setSize( int newWidth, int newHeight )
150 resize( newWidth, newHeight );
154 bool YQMultiLineEdit::setKeyboardFocus()
156 _qt_textEdit->setFocus();
162 #include "YQMultiLineEdit.moc"