libyui  3.0.10
/usr/src/RPM/BUILD/libyui-3.0.10/src/YLogView.cc
00001 /*
00002   Copyright (C) 2000-2012 Novell, Inc
00003   This library is free software; you can redistribute it and/or modify
00004   it under the terms of the GNU Lesser General Public License as
00005   published by the Free Software Foundation; either version 2.1 of the
00006   License, or (at your option) version 3.0 of the License. This library
00007   is distributed in the hope that it will be useful, but WITHOUT ANY
00008   WARRANTY; without even the implied warranty of MERCHANTABILITY or 
00009   FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
00010   License for more details. You should have received a copy of the GNU
00011   Lesser General Public License along with this library; if not, write
00012   to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
00013   Floor, Boston, MA 02110-1301 USA
00014 */
00015 
00016 
00017 /*-/
00018 
00019   File:         YLogView.cc
00020 
00021   Author:       Stefan Hundhammer <sh@suse.de>
00022 
00023 /-*/
00024 
00025 #include <deque>
00026 
00027 #define YUILogComponent "ui"
00028 #include "YUILog.h"
00029 
00030 #include "YUISymbols.h"
00031 #include "YLogView.h"
00032 
00033 
00034 typedef std::deque<std::string>                      StringDeque;
00035 typedef std::deque<std::string>::iterator            StringDequeIterator;
00036 typedef std::deque<std::string>::const_iterator      StringDequeConstIterator;
00037 
00038 
00039 
00040 struct YLogViewPrivate
00041 {
00042     YLogViewPrivate( const std::string & label, int visibleLines, int maxLines )
00043         : label( label )
00044         , visibleLines( visibleLines )
00045         , maxLines( maxLines )
00046         {}
00047 
00048     std::string label;
00049     int         visibleLines;
00050     int         maxLines;
00051 
00052     StringDeque logText;
00053 };
00054 
00055 
00056 
00057 
00058 YLogView::YLogView( YWidget * parent, const std::string & label, int visibleLines, int maxLines )
00059     : YWidget( parent )
00060     , priv( new YLogViewPrivate( label, visibleLines, maxLines ) )
00061 {
00062     YUI_CHECK_NEW( priv );
00063 
00064     setDefaultStretchable( YD_HORIZ, true );
00065     setDefaultStretchable( YD_VERT,  true );
00066 }
00067 
00068 
00069 YLogView::~YLogView()
00070 {
00071     // NOP
00072 }
00073 
00074 
00075 std::string
00076 YLogView::label() const
00077 {
00078     return priv->label;
00079 }
00080 
00081 
00082 void
00083 YLogView::setLabel( const std::string & label )
00084 {
00085     priv->label = label;
00086 }
00087 
00088 
00089 int
00090 YLogView::visibleLines() const
00091 {
00092     return priv->visibleLines;
00093 }
00094 
00095 
00096 void
00097 YLogView::setVisibleLines( int newVisibleLines )
00098 {
00099     priv->visibleLines = newVisibleLines;
00100 }
00101 
00102 
00103 int
00104 YLogView::maxLines() const
00105 {
00106     return priv->maxLines;
00107 }
00108 
00109 
00110 void
00111 YLogView::setMaxLines( int newMaxLines )
00112 {
00113     int linesToDelete = priv->maxLines - newMaxLines;
00114     priv->maxLines = newMaxLines;
00115 
00116     for ( int i=0; i < linesToDelete; i++ )
00117         priv->logText.pop_front();
00118 
00119     if ( linesToDelete > 0 )
00120         updateDisplay();
00121 }
00122 
00123 
00124 std::string
00125 YLogView::logText() const
00126 {
00127     std::string text;
00128 
00129     for ( StringDequeConstIterator it = priv->logText.begin();
00130           it != priv->logText.end();
00131           ++it )
00132     {
00133         text += *it;
00134     }
00135 
00136     if ( ! text.empty() )
00137     {
00138         // Cut off last newline
00139 
00140         if ( *(text.rbegin()) == '\n' ) // Last char is a newline?
00141         {
00142             text.resize( text.size() - 1 ); // make one char shorter
00143         }
00144     }
00145 
00146     return text;
00147 }
00148 
00149 
00150 std::string
00151 YLogView::lastLine() const
00152 {
00153     if ( priv->logText.empty() )
00154         return "";
00155     else
00156         return priv->logText.back();
00157 }
00158 
00159 
00160 void
00161 YLogView::appendLines( const std::string & newText )
00162 {
00163     std::string                 text    = newText;
00164     std::string::size_type      from    = 0;
00165     std::string::size_type      to      = 0;
00166 
00167 
00168     // Split the text into single lines
00169 
00170     while ( to < text.size() )
00171     {
00172         from = to;
00173         to   = text.find( '\n', from );
00174         if ( to == std::string::npos )               // no more newline
00175             to = text.size();
00176         else
00177             to++;                               // include the newline
00178 
00179         // Output one single line
00180         appendLine( text.substr( from, to - from ) );
00181     }
00182 
00183     if ( to < text.size() )             // anything left over?
00184     {
00185         // Output the rest
00186         appendLine( text.substr( to, text.size() - to ) );
00187     }
00188 
00189     updateDisplay();
00190 }
00191 
00192 
00193 void
00194 YLogView::appendLine( const std::string & line )
00195 {
00196     priv->logText.push_back( line );
00197 
00198     if ( maxLines() > 0 && priv->logText.size() > (unsigned) maxLines() )
00199     {
00200         priv->logText.pop_front();
00201     }
00202 }
00203 
00204 
00205 void
00206 YLogView::clearText()
00207 {
00208     priv->logText.clear();
00209     updateDisplay();
00210 }
00211 
00212 
00213 int YLogView::lines() const
00214 {
00215     return priv->logText.size();
00216 }
00217 
00218 
00219 void
00220 YLogView::updateDisplay()
00221 {
00222     displayLogText( logText() );
00223 }
00224 
00225 
00226 
00227 const YPropertySet &
00228 YLogView::propertySet()
00229 {
00230     static YPropertySet propSet;
00231 
00232     if ( propSet.isEmpty() )
00233     {
00234         /*
00235          * @property std::string        Value           All log lines.
00236          * @property std::string        LastLine        The last log line(s). Use this to append lines.
00237          * @property integer            VisibleLines    Number of lines to display. Call RecalcLayout() afterwards.
00238          * @property integer            MaxLines        Number of lines to store (0 for all).
00239          * @property std::string        Label           Caption above the log text
00240          */
00241         propSet.add( YProperty( YUIProperty_Value,              YStringProperty ) );
00242         propSet.add( YProperty( YUIProperty_LastLine,           YStringProperty ) );
00243         propSet.add( YProperty( YUIProperty_VisibleLines,       YIntegerProperty ) );
00244         propSet.add( YProperty( YUIProperty_MaxLines,           YIntegerProperty ) );
00245         propSet.add( YProperty( YUIProperty_Label,              YStringProperty  ) );
00246         propSet.add( YWidget::propertySet() );
00247     }
00248 
00249     return propSet;
00250 }
00251 
00252 
00253 bool
00254 YLogView::setProperty( const std::string & propertyName, const YPropertyValue & val )
00255 {
00256     propertySet().check( propertyName, val.type() ); // throws exceptions if not found or type mismatch
00257 
00258     if      ( propertyName == YUIProperty_Value         )       setLogText      ( val.stringVal()  );
00259     else if ( propertyName == YUIProperty_LastLine      )       appendLines     ( val.stringVal()  );
00260     else if ( propertyName == YUIProperty_VisibleLines  )       setVisibleLines ( val.integerVal() );
00261     else if ( propertyName == YUIProperty_MaxLines      )       setMaxLines     ( val.integerVal() );
00262     else if ( propertyName == YUIProperty_Label         )       setLabel        ( val.stringVal()  );
00263     else
00264     {
00265         return YWidget::setProperty( propertyName, val );
00266     }
00267 
00268     return true; // success -- no special processing necessary
00269 }
00270 
00271 
00272 YPropertyValue
00273 YLogView::getProperty( const std::string & propertyName )
00274 {
00275     propertySet().check( propertyName ); // throws exceptions if not found
00276 
00277     if      ( propertyName == YUIProperty_Value         )       return YPropertyValue( logText()      );
00278     if      ( propertyName == YUIProperty_LastLine      )       return YPropertyValue( lastLine()     );
00279     if      ( propertyName == YUIProperty_VisibleLines  )       return YPropertyValue( visibleLines() );
00280     if      ( propertyName == YUIProperty_MaxLines      )       return YPropertyValue( maxLines()     );
00281     else if ( propertyName == YUIProperty_Label         )       return YPropertyValue( label()        );
00282     else
00283     {
00284         return YWidget::getProperty( propertyName );
00285     }
00286 }
 All Classes Functions Variables Enumerations Friends