libyui-qt  2.42.4
 All Classes Functions Variables
YQMultiLineEdit.cc
1 /*
2  Copyright (C) 2000-2012 Novell, Inc
3  This library is free software; you can redistribute it and/or modify
4  it under the terms of the GNU Lesser General Public License as
5  published by the Free Software Foundation; either version 2.1 of the
6  License, or (at your option) version 3.0 of the License. This library
7  is distributed in the hope that it will be useful, but WITHOUT ANY
8  WARRANTY; without even the implied warranty of MERCHANTABILITY or
9  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
10  License for more details. You should have received a copy of the GNU
11  Lesser General Public License along with this library; if not, write
12  to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
13  Floor, Boston, MA 02110-1301 USA
14 */
15 
16 
17 /*-/
18 
19  File: YQMultiLineEdit.cc
20 
21  Author: Stefan Hundhammer <sh@suse.de>
22 
23 /-*/
24 
25 
26 #include <QVBoxLayout>
27 #include <QTextEdit>
28 #include <qlabel.h>
29 #define YUILogComponent "qt-ui"
30 #include <yui/YUILog.h>
31 
32 using std::max;
33 
34 #include "utf8.h"
35 #include "YQUI.h"
36 #include <yui/YEvent.h>
37 #include "YQMultiLineEdit.h"
38 #include "YQSignalBlocker.h"
39 #include "YQWidgetCaption.h"
40 
41 
42 YQMultiLineEdit::YQMultiLineEdit( YWidget * parent, const std::string & label )
43  : QFrame( (QWidget *) parent->widgetRep() )
44  , YMultiLineEdit( parent, label )
45 {
46  QVBoxLayout* layout = new QVBoxLayout( this );
47  setLayout( layout );
48 
49  setWidgetRep( this );
50  layout->setSpacing( YQWidgetSpacing );
51  layout->setMargin ( YQWidgetMargin );
52 
53  _caption = new YQWidgetCaption( this, label );
54  YUI_CHECK_NEW( _caption );
55  layout->addWidget( _caption );
56 
57  _qt_textEdit = new QTextEdit( this );
58  YUI_CHECK_NEW( _qt_textEdit );
59  layout->addWidget( _qt_textEdit );
60 
61  _qt_textEdit->setAcceptRichText( false );
62  _qt_textEdit->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding ) );
63 
64  _caption->setBuddy( _qt_textEdit );
65 
66  connect( _qt_textEdit, SIGNAL( textChanged( void ) ),
67  this, SLOT ( changed ( void ) ) );
68 }
69 
70 
72 {
73  // NOP
74 }
75 
76 
78 {
79  return toUTF8( _qt_textEdit->document()->toPlainText() );
80 }
81 
82 
83 void YQMultiLineEdit::setValue( const std::string & text )
84 {
85  YQSignalBlocker sigBlocker( _qt_textEdit );
86 
87  _qt_textEdit->setText( fromUTF8( text ) );
88 }
89 
90 
91 void YQMultiLineEdit::setLabel( const std::string & label )
92 {
93  _caption->setText( label );
94  YMultiLineEdit::setLabel( label );
95 }
96 
97 
98 void YQMultiLineEdit::setInputMaxLength( int newMaxLength )
99 {
100  YMultiLineEdit::setInputMaxLength( newMaxLength );
101 
102  QString text = _qt_textEdit->document()->toPlainText();
103 
104  if ( (int) text.length() > inputMaxLength() )
105  {
106  text.truncate( inputMaxLength() );
107  _qt_textEdit->setText(text);
108  }
109 }
110 
111 
113 {
114  if ( inputMaxLength() >= 0 && _qt_textEdit->toPlainText().length() > inputMaxLength() )
115  _qt_textEdit->undo();
116 }
117 
118 
120 {
122 
123  if ( notify() )
124  YQUI::ui()->sendEvent( new YWidgetEvent( this, YEvent::ValueChanged ) );
125 }
126 
127 
128 void YQMultiLineEdit::setEnabled( bool enabled )
129 {
130  _caption->setEnabled( enabled );
131  _qt_textEdit->setEnabled( enabled );
132  YWidget::setEnabled( enabled );
133 }
134 
135 
137 {
138  return max( 30, sizeHint().width() );
139 }
140 
141 
143 {
144  int hintHeight = defaultVisibleLines() * _qt_textEdit->fontMetrics().lineSpacing();
145  hintHeight += _qt_textEdit->frameWidth() * 2 + YQWidgetMargin * 2;
146 
147  if ( !_caption->isHidden() )
148  hintHeight += _caption->sizeHint().height() + YQWidgetSpacing;
149 
150  return max( 10, hintHeight );
151 }
152 
153 
154 void YQMultiLineEdit::setSize( int newWidth, int newHeight )
155 {
156  resize( newWidth, newHeight );
157 }
158 
159 
161 {
162  _qt_textEdit->setFocus();
163 
164  return true;
165 }
166 
167 
168 #include "YQMultiLineEdit.moc"
169