- Don't create layouts with parent. Qt 4.x automatically reparents
[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 ( inputMaxLength() >= 0 && _qt_textEdit->toPlainText().length() > inputMaxLength() )
109         _qt_textEdit->undo();
110 }
111
112
113 void YQMultiLineEdit::changed()
114 {
115     enforceMaxInputLength();
116
117     if ( notify() )
118         YQUI::ui()->sendEvent( new YWidgetEvent( this, YEvent::ValueChanged ) );
119 }
120
121
122 void YQMultiLineEdit::setEnabled( bool enabled )
123 {
124     _caption->setEnabled( enabled );
125     _qt_textEdit->setEnabled( enabled );
126     YWidget::setEnabled( enabled );
127 }
128
129
130 int YQMultiLineEdit::preferredWidth()
131 {
132     return max( 30, sizeHint().width() );
133 }
134
135
136 int YQMultiLineEdit::preferredHeight()
137 {
138     int hintHeight       = defaultVisibleLines() * _qt_textEdit->fontMetrics().lineSpacing();
139     hintHeight          += _qt_textEdit->frameWidth() * 2 + YQWidgetMargin * 2;
140
141     if ( !_caption->isHidden() )
142         hintHeight += _caption->sizeHint().height() + YQWidgetSpacing;
143
144     return max( 10, hintHeight );
145 }
146
147
148 void YQMultiLineEdit::setSize( int newWidth, int newHeight )
149 {
150     resize( newWidth, newHeight );
151 }
152
153
154 bool YQMultiLineEdit::setKeyboardFocus()
155 {
156     _qt_textEdit->setFocus();
157
158     return true;
159 }
160
161
162 #include "YQMultiLineEdit.moc"
163