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()
109 if ( inputMaxLength() >= 0 && _qt_textEdit->length() > inputMaxLength() )
114 _qt_textEdit->getCursorPosition( ¶, &index);
116 QString text = _qt_textEdit->document()->toPlainText();
118 int pos = 0; // current positon in text
119 int section =0; // section in text;
120 // iterate over the string
122 while ( pos != (int) text.length()+1 )
124 // we reached the paragraph where the user entered
126 if ( section == para )
128 // remove that character
129 text.remove( pos+index-1, 1 );
133 // new paragraph begins
134 if ( text[pos] == '\n' )
140 _qt_textEdit->setText( text );
142 // user removed a paragraph
146 // the new index is the end of the previous paragraph
147 index = _qt_textEdit->paragraphLength(para) + 1;
150 // adjust to new cursor position before the removed character
151 _qt_textEdit->setCursorPosition( para, index-1 );
157 void YQMultiLineEdit::changed()
159 enforceMaxInputLength();
162 YQUI::ui()->sendEvent( new YWidgetEvent( this, YEvent::ValueChanged ) );
166 void YQMultiLineEdit::setEnabled( bool enabled )
168 _caption->setEnabled( enabled );
169 _qt_textEdit->setEnabled( enabled );
170 YWidget::setEnabled( enabled );
174 int YQMultiLineEdit::preferredWidth()
176 return max( 30, sizeHint().width() );
180 int YQMultiLineEdit::preferredHeight()
182 int hintHeight = defaultVisibleLines() * _qt_textEdit->fontMetrics().lineSpacing();
183 hintHeight += _qt_textEdit->frameWidth() * 2 + YQWidgetMargin * 2;
185 if ( !_caption->isHidden() )
186 hintHeight += _caption->sizeHint().height() + YQWidgetSpacing;
188 return max( 10, hintHeight );
192 void YQMultiLineEdit::setSize( int newWidth, int newHeight )
194 resize( newWidth, newHeight );
198 bool YQMultiLineEdit::setKeyboardFocus()
200 _qt_textEdit->setFocus();
206 #include "YQMultiLineEdit.moc"