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