1 /*---------------------------------------------------------------------\
3 | __ __ ____ _____ ____ |
4 | \ \ / /_ _/ ___|_ _|___ \ |
5 | \ V / _` \___ \ | | __) | |
6 | | | (_| |___) || | / __/ |
7 | |_|\__,_|____/ |_| |_____| |
11 \----------------------------------------------------------------------/
13 File: YQMultiLineEdit.cc
15 Author: Stefan Hundhammer <sh@suse.de>
20 #include <q3multilineedit.h>
21 #include <QVBoxLayout>
24 #define y2log_component "qt-ui"
25 #include <ycp/y2log.h>
32 #include "YQMultiLineEdit.h"
33 #include "YQSignalBlocker.h"
34 #include "YQWidgetCaption.h"
37 YQMultiLineEdit::YQMultiLineEdit( YWidget * parent, const string & label )
38 : QFrame( (QWidget *) parent->widgetRep() )
39 , YMultiLineEdit( parent, label )
41 QVBoxLayout* layout = new QVBoxLayout( this );
45 layout->setSpacing( YQWidgetSpacing );
46 layout->setMargin ( YQWidgetMargin );
48 _caption = new YQWidgetCaption( this, label );
49 YUI_CHECK_NEW( _caption );
50 layout->addWidget( _caption );
52 _qt_textEdit = new QTextEdit( this );
53 YUI_CHECK_NEW( _qt_textEdit );
54 layout->addWidget( _qt_textEdit );
56 //_qt_textEdit->setTextFormat( Qt::PlainText );
57 _qt_textEdit->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding ) );
59 _caption->setBuddy( _qt_textEdit );
61 connect( _qt_textEdit, SIGNAL( textChanged( void ) ),
62 this, SLOT ( changed ( void ) ) );
66 YQMultiLineEdit::~YQMultiLineEdit()
72 string YQMultiLineEdit::value()
74 return toUTF8( _qt_textEdit->document()->toPlainText() );
78 void YQMultiLineEdit::setValue( const string & text )
80 YQSignalBlocker sigBlocker( _qt_textEdit );
82 _qt_textEdit->setText( fromUTF8( text ) );
86 void YQMultiLineEdit::setLabel( const string & label )
88 _caption->setText( label );
89 YMultiLineEdit::setLabel( label );
93 void YQMultiLineEdit::setInputMaxLength( int newMaxLength )
95 YMultiLineEdit::setInputMaxLength( newMaxLength );
97 QString text = _qt_textEdit->document()->toPlainText();
99 if ( (int) text.length() > inputMaxLength() )
101 text.truncate( inputMaxLength() );
102 _qt_textEdit->setText(text);
107 void YQMultiLineEdit::enforceMaxInputLength()
110 if ( inputMaxLength() >= 0 && _qt_textEdit->length() > inputMaxLength() )
115 _qt_textEdit->getCursorPosition( ¶, &index);
117 QString text = _qt_textEdit->document()->toPlainText();
119 int pos = 0; // current positon in text
120 int section =0; // section in text;
121 // iterate over the string
123 while ( pos != (int) text.length()+1 )
125 // we reached the paragraph where the user entered
127 if ( section == para )
129 // remove that character
130 text.remove( pos+index-1, 1 );
134 // new paragraph begins
135 if ( text[pos] == '\n' )
141 _qt_textEdit->setText( text );
143 // user removed a paragraph
147 // the new index is the end of the previous paragraph
148 index = _qt_textEdit->paragraphLength(para) + 1;
151 // adjust to new cursor position before the removed character
152 _qt_textEdit->setCursorPosition( para, index-1 );
158 void YQMultiLineEdit::changed()
160 enforceMaxInputLength();
163 YQUI::ui()->sendEvent( new YWidgetEvent( this, YEvent::ValueChanged ) );
167 void YQMultiLineEdit::setEnabled( bool enabled )
169 _caption->setEnabled( enabled );
170 _qt_textEdit->setEnabled( enabled );
171 YWidget::setEnabled( enabled );
175 int YQMultiLineEdit::preferredWidth()
177 return max( 30, sizeHint().width() );
181 int YQMultiLineEdit::preferredHeight()
183 int hintHeight = defaultVisibleLines() * _qt_textEdit->fontMetrics().lineSpacing();
184 hintHeight += _qt_textEdit->frameWidth() * 2 + YQWidgetMargin * 2;
186 if ( !_caption->isHidden() )
187 hintHeight += _caption->sizeHint().height() + YQWidgetSpacing;
189 return max( 10, hintHeight );
193 void YQMultiLineEdit::setSize( int newWidth, int newHeight )
195 resize( newWidth, newHeight );
199 bool YQMultiLineEdit::setKeyboardFocus()
201 _qt_textEdit->setFocus();
207 #include "YQMultiLineEdit.moc"