libyui-qt  2.53.0
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 #include "utf8.h"
33 #include "YQUI.h"
34 #include <yui/YEvent.h>
35 #include "YQMultiLineEdit.h"
36 #include "YQSignalBlocker.h"
37 #include "YQWidgetCaption.h"
38 
39 using std::string;
40 
41 
42 
43 YQMultiLineEdit::YQMultiLineEdit( YWidget * parent, const string & label )
44  : QFrame( (QWidget *) parent->widgetRep() )
45  , YMultiLineEdit( parent, label )
46 {
47  QVBoxLayout* layout = new QVBoxLayout( this );
48  setLayout( layout );
49 
50  setWidgetRep( this );
51  layout->setSpacing( YQWidgetSpacing );
52  layout->setMargin ( YQWidgetMargin );
53 
54  _caption = new YQWidgetCaption( this, label );
55  YUI_CHECK_NEW( _caption );
56  layout->addWidget( _caption );
57 
58  _qt_textEdit = new QTextEdit( this );
59  YUI_CHECK_NEW( _qt_textEdit );
60  layout->addWidget( _qt_textEdit );
61 
62  _qt_textEdit->setAcceptRichText( false );
63  _qt_textEdit->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding ) );
64 
65  _caption->setBuddy( _qt_textEdit );
66 
67  connect( _qt_textEdit, &pclass(_qt_textEdit)::textChanged,
68  this, &pclass(this)::changed );
69 }
70 
71 
73 {
74  // NOP
75 }
76 
77 
79 {
80  return toUTF8( _qt_textEdit->document()->toPlainText() );
81 }
82 
83 
84 void YQMultiLineEdit::setValue( const string & text )
85 {
86  YQSignalBlocker sigBlocker( _qt_textEdit );
87 
88  _qt_textEdit->setText( fromUTF8( text ) );
89 }
90 
91 
92 void YQMultiLineEdit::setLabel( const string & label )
93 {
94  _caption->setText( label );
95  YMultiLineEdit::setLabel( label );
96 }
97 
98 
99 void YQMultiLineEdit::setInputMaxLength( int newMaxLength )
100 {
101  YMultiLineEdit::setInputMaxLength( newMaxLength );
102 
103  QString text = _qt_textEdit->document()->toPlainText();
104 
105  if ( (int) text.length() > inputMaxLength() )
106  {
107  text.truncate( inputMaxLength() );
108  _qt_textEdit->setText(text);
109  }
110 }
111 
112 
114 {
115  if ( inputMaxLength() >= 0 && _qt_textEdit->toPlainText().length() > inputMaxLength() )
116  _qt_textEdit->undo();
117 }
118 
119 
121 {
123 
124  if ( notify() )
125  YQUI::ui()->sendEvent( new YWidgetEvent( this, YEvent::ValueChanged ) );
126 }
127 
128 
129 void YQMultiLineEdit::setEnabled( bool enabled )
130 {
131  _caption->setEnabled( enabled );
132  _qt_textEdit->setEnabled( enabled );
133  YWidget::setEnabled( enabled );
134 }
135 
136 
138 {
139  return std::max( 30, sizeHint().width() );
140 }
141 
142 
144 {
145  int hintHeight = defaultVisibleLines() * _qt_textEdit->fontMetrics().lineSpacing();
146  hintHeight += _qt_textEdit->frameWidth() * 2 + YQWidgetMargin * 2;
147 
148  if ( !_caption->isHidden() )
149  hintHeight += _caption->sizeHint().height() + YQWidgetSpacing;
150 
151  return std::max( 10, hintHeight );
152 }
153 
154 
155 void YQMultiLineEdit::setSize( int newWidth, int newHeight )
156 {
157  resize( newWidth, newHeight );
158 }
159 
160 
162 {
163  _qt_textEdit->setFocus();
164 
165  return true;
166 }
167 
168 
169 
170 
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 int preferredWidth()
Preferred width of the widget.
void changed()
Triggered when the text changes.
virtual void setLabel(const std::string &label)
Set the label (the caption above the MultiLineEdit).
virtual int preferredHeight()
Preferred height of the widget.
virtual bool setKeyboardFocus()
Accept the keyboard focus.
virtual ~YQMultiLineEdit()
Destructor.
virtual void setSize(int newWidth, int newHeight)
Set the new size of the widget.
virtual std::string value()
Get the current value (the text entered by the user or set from the outside) of this MultiLineEdit.
virtual void setInputMaxLength(int numberOfChars)
Set the maximum input length, i.e., the maximum number of characters the user can enter.
YQMultiLineEdit(YWidget *parent, const std::string &label)
Constructor.
void enforceMaxInputLength()
Enforce the maximum input length: If the text becomes too long, remove the just-entered character at ...
virtual void setEnabled(bool enabled)
Set enabled/disabled state.
Helper class to block Qt signals for QWidgets or QObjects as long as this object exists.
static YQUI * ui()
Access the global Qt-UI.
Definition: YQUI.h:83
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:480
Helper class for captions (labels) above a widget: Takes care of hiding itself when its text is empty...
virtual void setText(const std::string &newText)
Change the text and handle visibility: If the new text is empty, hide this widget.