libyui-qt  2.46.13
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, &pclass(_qt_textEdit)::textChanged,
67  this, &pclass(this)::changed );
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 
Helper class to block Qt signals for QWidgets or QObjects as long as this object exists.
void changed()
Triggered when the text changes.
virtual std::string value()
Get the current value (the text entered by the user or set from the outside) of this MultiLineEdit...
virtual void setEnabled(bool enabled)
Set enabled/disabled state.
virtual void setValue(const std::string &text)
Set the current value (the text entered by the user or set from the outside) of this MultiLineEdit...
virtual void setText(const std::string &newText)
Change the text and handle visibility: If the new text is empty, hide this widget.
void sendEvent(YEvent *event)
Widget event handlers (slots) call this when an event occured that should be the answer to a UserInpu...
Definition: YQUI.cc:486
virtual int preferredWidth()
Preferred width of the widget.
virtual ~YQMultiLineEdit()
Destructor.
Helper class for captions (labels) above a widget: Takes care of hiding itself when its text is empty...
virtual void setInputMaxLength(int numberOfChars)
Set the maximum input length, i.e., the maximum number of characters the user can enter...
virtual bool setKeyboardFocus()
Accept the keyboard focus.
virtual void setSize(int newWidth, int newHeight)
Set the new size of the widget.
void enforceMaxInputLength()
Enforce the maximum input length: If the text becomes too long, remove the just-entered character at ...
virtual void setLabel(const std::string &label)
Set the label (the caption above the MultiLineEdit).
virtual int preferredHeight()
Preferred height of the widget.
static YQUI * ui()
Access the global Qt-UI.
Definition: YQUI.h:81
YQMultiLineEdit(YWidget *parent, const std::string &label)
Constructor.