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