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