]> icculus.org git repositories - duncan/yast2-qt4.git/blob - src/YQMultiLineEdit.cc
a6dfdc35e617d62049e83d2fad2a3882a0b01df9
[duncan/yast2-qt4.git] / src / YQMultiLineEdit.cc
1 /*---------------------------------------------------------------------\
2 |                                                                      |
3 |                      __   __    ____ _____ ____                      |
4 |                      \ \ / /_ _/ ___|_   _|___ \                     |
5 |                       \ V / _` \___ \ | |   __) |                    |
6 |                        | | (_| |___) || |  / __/                     |
7 |                        |_|\__,_|____/ |_| |_____|                    |
8 |                                                                      |
9 |                               core system                            |
10 |                                                        (C) SuSE GmbH |
11 \----------------------------------------------------------------------/
12
13   File:       YQMultiLineEdit.cc
14
15   Author:     Stefan Hundhammer <sh@suse.de>
16
17 /-*/
18
19
20 #include <qmultilineedit.h>
21 #include <qlabel.h>
22 #define y2log_component "qt-ui"
23 #include <ycp/y2log.h>
24
25 using std::max;
26
27 #include "utf8.h"
28 #include "YQUI.h"
29 #include "YEvent.h"
30 #include "YQMultiLineEdit.h"
31 #include "YQSignalBlocker.h"
32 #include "YQWidgetCaption.h"
33
34
35 YQMultiLineEdit::YQMultiLineEdit( YWidget * parent, const string & label )
36     : QVBox( (QWidget *) parent->widgetRep() )
37     , YMultiLineEdit( parent, label )
38 {
39     setWidgetRep( this );
40     setSpacing( YQWidgetSpacing );
41     setMargin ( YQWidgetMargin  );
42
43     _caption = new YQWidgetCaption( this, label );
44     YUI_CHECK_NEW( _caption );
45     
46     _qt_textEdit = new QTextEdit( this );
47     YUI_CHECK_NEW( _qt_textEdit );
48     
49     _qt_textEdit->setTextFormat( Qt::PlainText );
50     _qt_textEdit->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding ) );
51     
52     _caption->setBuddy( _qt_textEdit );
53
54     connect( _qt_textEdit,      SIGNAL( textChanged( void ) ),
55              this,              SLOT  ( changed    ( void ) ) );
56 }
57
58
59 YQMultiLineEdit::~YQMultiLineEdit()
60 {
61     // NOP
62 }
63
64
65 string YQMultiLineEdit::value()
66 {
67     return toUTF8( _qt_textEdit->text() );
68 }
69
70
71 void YQMultiLineEdit::setValue( const string & text )
72 {
73     YQSignalBlocker sigBlocker( _qt_textEdit );
74     
75     _qt_textEdit->setText( fromUTF8( text ) );
76 }
77
78
79 void YQMultiLineEdit::setLabel( const string & label )
80 {
81     _caption->setText( label );
82     YMultiLineEdit::setLabel( label );
83 }
84
85
86 void YQMultiLineEdit::setInputMaxLength( int newMaxLength )
87 {
88     YMultiLineEdit::setInputMaxLength( newMaxLength );
89
90     QString text = _qt_textEdit->text();
91
92     if ( (int) text.length() > inputMaxLength() )
93     {
94         text.truncate( inputMaxLength() );
95         _qt_textEdit->setText(text);
96     }
97 }
98
99
100 void YQMultiLineEdit::enforceMaxInputLength()
101 {
102     if ( inputMaxLength() >= 0 && _qt_textEdit->length() > inputMaxLength() )
103     {
104         int index;
105         int para;
106
107         _qt_textEdit->getCursorPosition( &para, &index);
108
109         QString text = _qt_textEdit->text();
110
111         int pos = 0; // current positon in text
112         int section =0; // section in text;
113         // iterate over the string
114         
115         while ( pos != (int) text.length()+1 )
116         {
117             // we reached the paragraph where the user entered
118             // a character
119             if ( section == para )
120             {
121                 // remove that character
122                 text.remove( pos+index-1, 1 );
123                 break;
124             }
125
126             // new paragraph begins
127             if ( text[pos] == '\n' )
128                 section++;
129
130             pos++;
131         }
132
133         _qt_textEdit->setText( text );
134
135         // user removed a paragraph
136         if ( index == 0 )
137         {
138             --para;
139             // the new index is the end of the previous paragraph
140             index = _qt_textEdit->paragraphLength(para) + 1;
141         }
142
143         // adjust to new cursor position before the removed character
144         _qt_textEdit->setCursorPosition( para, index-1 );
145     }
146 }
147
148
149 void YQMultiLineEdit::changed()
150 {
151     enforceMaxInputLength();
152
153     if ( notify() )
154         YQUI::ui()->sendEvent( new YWidgetEvent( this, YEvent::ValueChanged ) );
155 }
156
157
158 void YQMultiLineEdit::setEnabled( bool enabled )
159 {
160     _caption->setEnabled( enabled );
161     _qt_textEdit->setEnabled( enabled );
162     YWidget::setEnabled( enabled );
163 }
164
165
166 int YQMultiLineEdit::preferredWidth()
167 {
168     return max( 30, sizeHint().width() );
169 }
170
171
172 int YQMultiLineEdit::preferredHeight()
173 {
174     int hintHeight       = defaultVisibleLines() * _qt_textEdit->fontMetrics().lineSpacing();
175     hintHeight          += _qt_textEdit->frameWidth() * 2 + YQWidgetMargin * 2;
176
177     if ( _caption->isShown() )
178         hintHeight += _caption->sizeHint().height() + YQWidgetSpacing;
179
180     return max( 10, hintHeight );
181 }
182
183
184 void YQMultiLineEdit::setSize( int newWidth, int newHeight )
185 {
186     resize( newWidth, newHeight );
187 }
188
189
190 bool YQMultiLineEdit::setKeyboardFocus()
191 {
192     _qt_textEdit->setFocus();
193
194     return true;
195 }
196
197
198 #include "YQMultiLineEdit.moc"
199