libyui  3.3.1
YLogView.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: YLogView.cc
20 
21  Author: Stefan Hundhammer <sh@suse.de>
22 
23 /-*/
24 
25 #include <deque>
26 
27 #define YUILogComponent "ui"
28 #include "YUILog.h"
29 
30 #include "YUISymbols.h"
31 #include "YLogView.h"
32 
33 
34 typedef std::deque<std::string> StringDeque;
35 typedef std::deque<std::string>::iterator StringDequeIterator;
36 typedef std::deque<std::string>::const_iterator StringDequeConstIterator;
37 
38 
39 
41 {
42  YLogViewPrivate( const std::string & label, int visibleLines, int maxLines )
43  : label( label )
44  , visibleLines( visibleLines )
45  , maxLines( maxLines )
46  {}
47 
48  std::string label;
49  int visibleLines;
50  int maxLines;
51 
52  StringDeque logText;
53 };
54 
55 
56 
57 
58 YLogView::YLogView( YWidget * parent, const std::string & label, int visibleLines, int maxLines )
59  : YWidget( parent )
60  , priv( new YLogViewPrivate( label, visibleLines, maxLines ) )
61 {
62  YUI_CHECK_NEW( priv );
63 
64  setDefaultStretchable( YD_HORIZ, true );
65  setDefaultStretchable( YD_VERT, true );
66 }
67 
68 
70 {
71  // NOP
72 }
73 
74 
75 std::string
77 {
78  return priv->label;
79 }
80 
81 
82 void
83 YLogView::setLabel( const std::string & label )
84 {
85  priv->label = label;
86 }
87 
88 
89 int
91 {
92  return priv->visibleLines;
93 }
94 
95 
96 void
97 YLogView::setVisibleLines( int newVisibleLines )
98 {
99  priv->visibleLines = newVisibleLines;
100 }
101 
102 
103 int
105 {
106  return priv->maxLines;
107 }
108 
109 
110 void
111 YLogView::setMaxLines( int newMaxLines )
112 {
113  int linesToDelete = priv->maxLines - newMaxLines;
114  priv->maxLines = newMaxLines;
115 
116  for ( int i=0; i < linesToDelete; i++ )
117  priv->logText.pop_front();
118 
119  if ( linesToDelete > 0 )
120  updateDisplay();
121 }
122 
123 
124 std::string
126 {
127  std::string text;
128 
129  for ( StringDequeConstIterator it = priv->logText.begin();
130  it != priv->logText.end();
131  ++it )
132  {
133  text += *it;
134  }
135 
136  if ( ! text.empty() )
137  {
138  // Cut off last newline
139 
140  if ( *(text.rbegin()) == '\n' ) // Last char is a newline?
141  {
142  text.resize( text.size() - 1 ); // make one char shorter
143  }
144  }
145 
146  return text;
147 }
148 
149 
150 std::string
152 {
153  if ( priv->logText.empty() )
154  return "";
155  else
156  return priv->logText.back();
157 }
158 
159 
160 void
161 YLogView::appendLines( const std::string & newText )
162 {
163  std::string text = newText;
164  std::string::size_type from = 0;
165  std::string::size_type to = 0;
166 
167 
168  // Split the text into single lines
169 
170  while ( to < text.size() )
171  {
172  from = to;
173  to = text.find( '\n', from );
174  if ( to == std::string::npos ) // no more newline
175  to = text.size();
176  else
177  to++; // include the newline
178 
179  // Output one single line
180  appendLine( text.substr( from, to - from ) );
181  }
182 
183  if ( to < text.size() ) // anything left over?
184  {
185  // Output the rest
186  appendLine( text.substr( to, text.size() - to ) );
187  }
188 
189  updateDisplay();
190 }
191 
192 
193 void
194 YLogView::appendLine( const std::string & line )
195 {
196  priv->logText.push_back( line );
197 
198  if ( maxLines() > 0 && priv->logText.size() > (unsigned) maxLines() )
199  {
200  priv->logText.pop_front();
201  }
202 }
203 
204 void
205 YLogView::setLogText(const std::string & text)
206 {
207  // optimize for regular updating widget when no new content appear
208  if (text == logText())
209  return;
210 
211  // do not use clearText as it do render and cause segfault in qt (bnc#989155)
212  priv->logText.clear();
213  appendLines(text);
214 }
215 
216 
217 void
219 {
220  priv->logText.clear();
221  updateDisplay();
222 }
223 
224 
225 int YLogView::lines() const
226 {
227  return priv->logText.size();
228 }
229 
230 
231 void
232 YLogView::updateDisplay()
233 {
234  displayLogText( logText() );
235 }
236 
237 
238 
239 const YPropertySet &
241 {
242  static YPropertySet propSet;
243 
244  if ( propSet.isEmpty() )
245  {
246  /*
247  * @property std::string Value All log lines.
248  * @property std::string LastLine The last log line(s). Use this to append lines.
249  * @property integer VisibleLines Number of lines to display. Call RecalcLayout() afterwards.
250  * @property integer MaxLines Number of lines to store (0 for all).
251  * @property std::string Label Caption above the log text
252  */
253  propSet.add( YProperty( YUIProperty_Value, YStringProperty ) );
254  propSet.add( YProperty( YUIProperty_LastLine, YStringProperty ) );
255  propSet.add( YProperty( YUIProperty_VisibleLines, YIntegerProperty ) );
256  propSet.add( YProperty( YUIProperty_MaxLines, YIntegerProperty ) );
257  propSet.add( YProperty( YUIProperty_Label, YStringProperty ) );
258  propSet.add( YWidget::propertySet() );
259  }
260 
261  return propSet;
262 }
263 
264 
265 bool
266 YLogView::setProperty( const std::string & propertyName, const YPropertyValue & val )
267 {
268  propertySet().check( propertyName, val.type() ); // throws exceptions if not found or type mismatch
269 
270  if ( propertyName == YUIProperty_Value ) setLogText ( val.stringVal() );
271  else if ( propertyName == YUIProperty_LastLine ) appendLines ( val.stringVal() );
272  else if ( propertyName == YUIProperty_VisibleLines ) setVisibleLines ( val.integerVal() );
273  else if ( propertyName == YUIProperty_MaxLines ) setMaxLines ( val.integerVal() );
274  else if ( propertyName == YUIProperty_Label ) setLabel ( val.stringVal() );
275  else
276  {
277  return YWidget::setProperty( propertyName, val );
278  }
279 
280  return true; // success -- no special processing necessary
281 }
282 
283 
285 YLogView::getProperty( const std::string & propertyName )
286 {
287  propertySet().check( propertyName ); // throws exceptions if not found
288 
289  if ( propertyName == YUIProperty_Value ) return YPropertyValue( logText() );
290  if ( propertyName == YUIProperty_LastLine ) return YPropertyValue( lastLine() );
291  if ( propertyName == YUIProperty_VisibleLines ) return YPropertyValue( visibleLines() );
292  if ( propertyName == YUIProperty_MaxLines ) return YPropertyValue( maxLines() );
293  else if ( propertyName == YUIProperty_Label ) return YPropertyValue( label() );
294  else
295  {
296  return YWidget::getProperty( propertyName );
297  }
298 }
std::string logText() const
Return the entire log text as one large string of concatenated lines delimited with newlines...
Definition: YLogView.cc:125
bool isEmpty() const
Returns &#39;true&#39; if this property set does not contain anything.
Definition: YProperty.h:263
void appendLines(const std::string &text)
Append one or more lines to the log text and trigger a display update.
Definition: YLogView.cc:161
YLogView(YWidget *parent, const std::string &label, int visibleLines, int maxLines)
Constructor.
Definition: YLogView.cc:58
Transport class for the value of simple properties.
Definition: YProperty.h:104
void setVisibleLines(int newVisibleLines)
Set the number of visible lines.
Definition: YLogView.cc:97
void add(const YProperty &prop)
Add a property to this property set.
Definition: YProperty.cc:145
virtual bool setProperty(const std::string &propertyName, const YPropertyValue &val)
Set a property.
Definition: YWidget.cc:428
A set of properties to check names and types against.
Definition: YProperty.h:197
int lines() const
Return the current number of lines.
Definition: YLogView.cc:225
virtual const YPropertySet & propertySet()
Return this class&#39;s property set.
Definition: YLogView.cc:240
virtual void setLabel(const std::string &label)
Set the label (the caption above the log text).
Definition: YLogView.cc:83
void setLogText(const std::string &text)
Set (replace) the entire log text and trigger a display update.
Definition: YLogView.cc:205
virtual void displayLogText(const std::string &text)=0
Display the part of the log text that should be displayed.
virtual const YPropertySet & propertySet()
Return this class&#39;s property set.
Definition: YWidget.cc:393
virtual YPropertyValue getProperty(const std::string &propertyName)
Get a property.
Definition: YWidget.cc:453
std::string stringVal() const
Methods to get the value of this property.
Definition: YProperty.h:180
void setDefaultStretchable(YUIDimension dim, bool newStretch)
Set the stretchable state to "newStretch".
Definition: YWidget.cc:561
virtual ~YLogView()
Destructor.
Definition: YLogView.cc:69
std::string lastLine() const
Return the last log line.
Definition: YLogView.cc:151
Class for widget properties.
Definition: YProperty.h:51
int visibleLines() const
Return the number of visible lines.
Definition: YLogView.cc:90
int maxLines() const
Return the maximum number of lines to store.
Definition: YLogView.cc:104
void clearText()
Clear the log text and trigger a display update.
Definition: YLogView.cc:218
virtual bool setProperty(const std::string &propertyName, const YPropertyValue &val)
Set a property.
Definition: YLogView.cc:266
std::string label() const
Return the label (the caption above the log text).
Definition: YLogView.cc:76
void check(const std::string &propertyName) const
Check if a property &#39;propertyName&#39; exists in this property set.
Definition: YProperty.cc:87
Abstract base class of all UI widgets.
Definition: YWidget.h:54
void setMaxLines(int newMaxLines)
Set the maximum number of lines to store.
Definition: YLogView.cc:111
YPropertyType type() const
Returns the type of this property value.
Definition: YProperty.h:169
virtual YPropertyValue getProperty(const std::string &propertyName)
Get a property.
Definition: YLogView.cc:285