libyui-qt  2.52.2
YQLogView.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: YQLogView.cc
20 
21  Author: Stefan Hundhammer <sh@suse.de>
22 
23 /-*/
24 
25 
26 #include <qlabel.h>
27 #include <qstyle.h>
28 #include <QVBoxLayout>
29 #include <QScrollBar>
30 #define YUILogComponent "qt-ui"
31 #include <yui/YUILog.h>
32 
33 #include "utf8.h"
34 #include "YQUI.h"
35 #include "YQLogView.h"
36 #include "YQWidgetCaption.h"
37 
38 using std::string;
39 
40 
41 
42 YQLogView::YQLogView( YWidget * parent,
43  const string & label,
44  int visibleLines,
45  int maxLines )
46  : QFrame( (QWidget *) parent->widgetRep() )
47  , YLogView( parent, label, visibleLines, maxLines )
48 {
49  setWidgetRep( this );
50  QVBoxLayout* layout = new QVBoxLayout( this );
51  setLayout( layout );
52 
53  layout->setSpacing( YQWidgetSpacing );
54  layout->setMargin( YQWidgetMargin );
55 
56  _caption = new YQWidgetCaption( this, label );
57  YUI_CHECK_NEW( _caption );
58  layout->addWidget( _caption );
59 
60  _qt_text = new MyTextEdit( this );
61  YUI_CHECK_NEW( _qt_text );
62  layout->addWidget( _qt_text );
63 
64  _qt_text->setReadOnly( true );
65  _qt_text->setAcceptRichText( false );
66  _qt_text->setSizePolicy( QSizePolicy( QSizePolicy::Expanding, QSizePolicy::Expanding ) );
67 
68  _caption->setBuddy( _qt_text );
69 
70  connect (_qt_text, &pclass(_qt_text)::resized, this, &pclass(this)::slotResize);
71 
72 }
73 
74 
76 {
77  // NOP
78 }
79 
80 
81 void
82 YQLogView::displayLogText( const string & text )
83 {
84  QScrollBar *sb = _qt_text->verticalScrollBar();
85  QString newString = fromUTF8( text );
86  // no change -> no need to update
87  if (newString == _lastText)
88  return;
89 
90  bool atEnd = sb->value() == sb->maximum();
91 
92  if (newString.startsWith(_lastText) && !_lastText.isEmpty() )
93  {
94  int position = _lastText.length();
95 
96  // prevent double line break caused by QTextEdit::append()
97  if ( newString.mid( _lastText.length(), 1 ) == QString('\n') )
98  position++;
99 
100  _qt_text->append( newString.mid( position) );
101  }
102  else
103  {
104  _qt_text->setPlainText( newString );
105  }
106 
107 
108  if (atEnd)
109  {
110  _qt_text->moveCursor( QTextCursor::End );
111  _qt_text->ensureCursorVisible();
112  sb->setValue( sb->maximum() );
113  }
114 
115  _lastText = newString;
116 }
117 
118 
119 void
120 YQLogView::setLabel( const string & label )
121 {
122  _caption->setText( label );
123  YLogView::setLabel( label );
124 }
125 
126 
127 void
128 YQLogView::setEnabled( bool enabled )
129 {
130  _caption->setEnabled( enabled );
131  _qt_text->setEnabled( enabled );
132  YWidget::setEnabled( enabled );
133 }
134 
135 
136 int
138 {
139  return std::max( 50, sizeHint().width() );
140 }
141 
142 
143 int
145 {
146  int hintHeight = visibleLines() * _qt_text->fontMetrics().lineSpacing();
147  hintHeight += _qt_text->style()->pixelMetric( QStyle::PM_ScrollBarExtent );
148  hintHeight += _qt_text->frameWidth() * 2;
149 
150  if ( !_caption->isHidden() )
151  hintHeight += _caption->sizeHint().height();
152 
153  return std::max( 80, hintHeight );
154 }
155 
156 
157 void
158 YQLogView::slotResize()
159 {
160  QScrollBar *sb = _qt_text->verticalScrollBar();
161 
162  bool atEnd = sb->value() == sb->maximum();
163 
164  if (atEnd)
165  {
166  _qt_text->moveCursor( QTextCursor::End );
167  _qt_text->ensureCursorVisible();
168  sb->setValue( sb->maximum() );
169  }
170 }
171 
172 
173 void
174 YQLogView::setSize( int newWidth, int newHeight )
175 {
176  resize( newWidth, newHeight );
177 }
178 
179 
180 bool
182 {
183  _qt_text->setFocus();
184 
185  return true;
186 }
virtual void displayLogText(const std::string &text)
Display the part of the log text that should be displayed.
Definition: YQLogView.cc:82
YQLogView(YWidget *parent, const std::string &label, int visibleLines, int maxLines)
Constructor.
Definition: YQLogView.cc:42
virtual void setText(const std::string &newText)
Change the text and handle visibility: If the new text is empty, hide this widget.
virtual int preferredWidth()
Preferred width of the widget.
Definition: YQLogView.cc:137
virtual bool setKeyboardFocus()
Accept the keyboard focus.
Definition: YQLogView.cc:181
virtual void setLabel(const std::string &label)
Set the label (the caption above the log text).
Definition: YQLogView.cc:120
virtual void setEnabled(bool enabled)
Set enabled/disabled state.
Definition: YQLogView.cc:128
virtual int preferredHeight()
Preferred height of the widget.
Definition: YQLogView.cc:144
virtual ~YQLogView()
Destructor.
Definition: YQLogView.cc:75
virtual void setSize(int newWidth, int newHeight)
Set the new size of the widget.
Definition: YQLogView.cc:174
Helper class for captions (labels) above a widget: Takes care of hiding itself when its text is empty...