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