1 /*---------------------------------------------------------------------\
3 | __ __ ____ _____ ____ |
4 | \ \ / /_ _/ ___|_ _|___ \ |
5 | \ V / _` \___ \ | | __) | |
6 | | | (_| |___) || | / __/ |
7 | |_|\__,_|____/ |_| |_____| |
11 \----------------------------------------------------------------------/
13 File: YQMultiLineEdit.cc
15 Author: Stefan Hundhammer <sh@suse.de>
20 #include <qmultilineedit.h>
22 #define y2log_component "qt-ui"
23 #include <ycp/y2log.h>
30 #include "YQMultiLineEdit.h"
31 #include "YQSignalBlocker.h"
32 #include "YQWidgetCaption.h"
35 YQMultiLineEdit::YQMultiLineEdit( YWidget * parent, const string & label )
36 : QVBox( (QWidget *) parent->widgetRep() )
37 , YMultiLineEdit( parent, label )
40 setSpacing( YQWidgetSpacing );
41 setMargin ( YQWidgetMargin );
43 _caption = new YQWidgetCaption( this, label );
44 YUI_CHECK_NEW( _caption );
46 _qt_textEdit = new QTextEdit( this );
47 YUI_CHECK_NEW( _qt_textEdit );
49 _qt_textEdit->setTextFormat( Qt::PlainText );
50 _qt_textEdit->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding ) );
52 _caption->setBuddy( _qt_textEdit );
54 connect( _qt_textEdit, SIGNAL( textChanged( void ) ),
55 this, SLOT ( changed ( void ) ) );
59 YQMultiLineEdit::~YQMultiLineEdit()
65 string YQMultiLineEdit::value()
67 return toUTF8( _qt_textEdit->text() );
71 void YQMultiLineEdit::setValue( const string & text )
73 YQSignalBlocker sigBlocker( _qt_textEdit );
75 _qt_textEdit->setText( fromUTF8( text ) );
79 void YQMultiLineEdit::setLabel( const string & label )
81 _caption->setText( label );
82 YMultiLineEdit::setLabel( label );
86 void YQMultiLineEdit::setInputMaxLength( int newMaxLength )
88 YMultiLineEdit::setInputMaxLength( newMaxLength );
90 QString text = _qt_textEdit->text();
92 if ( (int) text.length() > inputMaxLength() )
94 text.truncate( inputMaxLength() );
95 _qt_textEdit->setText(text);
100 void YQMultiLineEdit::enforceMaxInputLength()
102 if ( inputMaxLength() >= 0 && _qt_textEdit->length() > inputMaxLength() )
107 _qt_textEdit->getCursorPosition( ¶, &index);
109 QString text = _qt_textEdit->text();
111 int pos = 0; // current positon in text
112 int section =0; // section in text;
113 // iterate over the string
115 while ( pos != (int) text.length()+1 )
117 // we reached the paragraph where the user entered
119 if ( section == para )
121 // remove that character
122 text.remove( pos+index-1, 1 );
126 // new paragraph begins
127 if ( text[pos] == '\n' )
133 _qt_textEdit->setText( text );
135 // user removed a paragraph
139 // the new index is the end of the previous paragraph
140 index = _qt_textEdit->paragraphLength(para) + 1;
143 // adjust to new cursor position before the removed character
144 _qt_textEdit->setCursorPosition( para, index-1 );
149 void YQMultiLineEdit::changed()
151 enforceMaxInputLength();
154 YQUI::ui()->sendEvent( new YWidgetEvent( this, YEvent::ValueChanged ) );
158 void YQMultiLineEdit::setEnabled( bool enabled )
160 _caption->setEnabled( enabled );
161 _qt_textEdit->setEnabled( enabled );
162 YWidget::setEnabled( enabled );
166 int YQMultiLineEdit::preferredWidth()
168 return max( 30, sizeHint().width() );
172 int YQMultiLineEdit::preferredHeight()
174 int hintHeight = defaultVisibleLines() * _qt_textEdit->fontMetrics().lineSpacing();
175 hintHeight += _qt_textEdit->frameWidth() * 2 + YQWidgetMargin * 2;
177 if ( _caption->isShown() )
178 hintHeight += _caption->sizeHint().height() + YQWidgetSpacing;
180 return max( 10, hintHeight );
184 void YQMultiLineEdit::setSize( int newWidth, int newHeight )
186 resize( newWidth, newHeight );
190 bool YQMultiLineEdit::setKeyboardFocus()
192 _qt_textEdit->setFocus();
198 #include "YQMultiLineEdit.moc"