libyui  3.0.10
 All Classes Functions Variables Enumerations Friends
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 
205 void
207 {
208  priv->logText.clear();
209  updateDisplay();
210 }
211 
212 
213 int YLogView::lines() const
214 {
215  return priv->logText.size();
216 }
217 
218 
219 void
220 YLogView::updateDisplay()
221 {
222  displayLogText( logText() );
223 }
224 
225 
226 
227 const YPropertySet &
229 {
230  static YPropertySet propSet;
231 
232  if ( propSet.isEmpty() )
233  {
234  /*
235  * @property std::string Value All log lines.
236  * @property std::string LastLine The last log line(s). Use this to append lines.
237  * @property integer VisibleLines Number of lines to display. Call RecalcLayout() afterwards.
238  * @property integer MaxLines Number of lines to store (0 for all).
239  * @property std::string Label Caption above the log text
240  */
241  propSet.add( YProperty( YUIProperty_Value, YStringProperty ) );
242  propSet.add( YProperty( YUIProperty_LastLine, YStringProperty ) );
243  propSet.add( YProperty( YUIProperty_VisibleLines, YIntegerProperty ) );
244  propSet.add( YProperty( YUIProperty_MaxLines, YIntegerProperty ) );
245  propSet.add( YProperty( YUIProperty_Label, YStringProperty ) );
246  propSet.add( YWidget::propertySet() );
247  }
248 
249  return propSet;
250 }
251 
252 
253 bool
254 YLogView::setProperty( const std::string & propertyName, const YPropertyValue & val )
255 {
256  propertySet().check( propertyName, val.type() ); // throws exceptions if not found or type mismatch
257 
258  if ( propertyName == YUIProperty_Value ) setLogText ( val.stringVal() );
259  else if ( propertyName == YUIProperty_LastLine ) appendLines ( val.stringVal() );
260  else if ( propertyName == YUIProperty_VisibleLines ) setVisibleLines ( val.integerVal() );
261  else if ( propertyName == YUIProperty_MaxLines ) setMaxLines ( val.integerVal() );
262  else if ( propertyName == YUIProperty_Label ) setLabel ( val.stringVal() );
263  else
264  {
265  return YWidget::setProperty( propertyName, val );
266  }
267 
268  return true; // success -- no special processing necessary
269 }
270 
271 
273 YLogView::getProperty( const std::string & propertyName )
274 {
275  propertySet().check( propertyName ); // throws exceptions if not found
276 
277  if ( propertyName == YUIProperty_Value ) return YPropertyValue( logText() );
278  if ( propertyName == YUIProperty_LastLine ) return YPropertyValue( lastLine() );
279  if ( propertyName == YUIProperty_VisibleLines ) return YPropertyValue( visibleLines() );
280  if ( propertyName == YUIProperty_MaxLines ) return YPropertyValue( maxLines() );
281  else if ( propertyName == YUIProperty_Label ) return YPropertyValue( label() );
282  else
283  {
284  return YWidget::getProperty( propertyName );
285  }
286 }
void appendLines(const std::string &text)
Definition: YLogView.cc:161
YLogView(YWidget *parent, const std::string &label, int visibleLines, int maxLines)
Definition: YLogView.cc:58
void check(const std::string &propertyName) const
Definition: YProperty.cc:62
void setVisibleLines(int newVisibleLines)
Definition: YLogView.cc:97
void add(const YProperty &prop)
Definition: YProperty.cc:120
virtual bool setProperty(const std::string &propertyName, const YPropertyValue &val)
Definition: YWidget.cc:428
virtual const YPropertySet & propertySet()
Definition: YLogView.cc:228
std::string lastLine() const
Definition: YLogView.cc:151
virtual void setLabel(const std::string &label)
Definition: YLogView.cc:83
std::string stringVal() const
Definition: YProperty.h:167
std::string label() const
Definition: YLogView.cc:76
void setLogText(const std::string &text)
Definition: YLogView.h:122
virtual void displayLogText(const std::string &text)=0
virtual const YPropertySet & propertySet()
Definition: YWidget.cc:393
virtual YPropertyValue getProperty(const std::string &propertyName)
Definition: YWidget.cc:453
int lines() const
Definition: YLogView.cc:213
int visibleLines() const
Definition: YLogView.cc:90
bool isEmpty() const
Definition: YProperty.h:250
void setDefaultStretchable(YUIDimension dim, bool newStretch)
Definition: YWidget.cc:561
virtual ~YLogView()
Definition: YLogView.cc:69
void clearText()
Definition: YLogView.cc:206
int maxLines() const
Definition: YLogView.cc:104
virtual bool setProperty(const std::string &propertyName, const YPropertyValue &val)
Definition: YLogView.cc:254
std::string logText() const
Definition: YLogView.cc:125
YPropertyType type() const
Definition: YProperty.h:156
void setMaxLines(int newMaxLines)
Definition: YLogView.cc:111
virtual YPropertyValue getProperty(const std::string &propertyName)
Definition: YLogView.cc:273