libyui  3.0.10
/usr/src/RPM/BUILD/libyui-3.0.10/src/YTableItem.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:         YTableItem.cc
00020 
00021   Author:       Stefan Hundhammer <sh@suse.de>
00022 
00023 /-*/
00024 
00025 #include "YTableItem.h"
00026 #include "YUIException.h"
00027 
00028 
00029 YTableItem::YTableItem()
00030     : YItem( "" )
00031 {
00032     // NOP
00033 }
00034 
00035 
00036 YTableItem::YTableItem( const std::string & label_0,
00037                         const std::string & label_1,
00038                         const std::string & label_2,
00039                         const std::string & label_3,
00040                         const std::string & label_4,
00041                         const std::string & label_5,
00042                         const std::string & label_6,
00043                         const std::string & label_7,
00044                         const std::string & label_8,
00045                         const std::string & label_9 )
00046     : YItem( "" )
00047 {
00048     std::vector<std::string> labels;
00049     labels.reserve(10); // slight optimization
00050     labels.push_back( label_0 );
00051     labels.push_back( label_1 );
00052     labels.push_back( label_2 );
00053     labels.push_back( label_3 );
00054     labels.push_back( label_4 );
00055     labels.push_back( label_5 );
00056     labels.push_back( label_6 );
00057     labels.push_back( label_7 );
00058     labels.push_back( label_8 );
00059     labels.push_back( label_9 );
00060 
00061     //
00062     // Find the last non-empty label
00063     //
00064 
00065     unsigned lastLabel = labels.size() - 1;
00066 
00067     while ( labels[ lastLabel ].empty() && --lastLabel > 0 )
00068     {}
00069 
00070     //
00071     // Create cells
00072     //
00073 
00074     for ( unsigned i = 0; i <= lastLabel; ++i )
00075     {
00076         addCell( labels[i] );
00077     }
00078 }
00079 
00080 
00081 
00082 YTableItem::~YTableItem()
00083 {
00084     deleteCells();
00085 }
00086 
00087 
00088 void
00089 YTableItem::deleteCells()
00090 {
00091     YTableCellIterator it = cellsBegin();
00092 
00093     while ( it != cellsEnd() )
00094     {
00095         YTableCell * cell = *it;
00096         ++it;
00097         delete cell;
00098     }
00099 
00100     _cells.clear();
00101 }
00102 
00103 
00104 void
00105 YTableItem::addCell( YTableCell * cell )
00106 {
00107     YUI_CHECK_PTR( cell );
00108     _cells.push_back( cell );
00109 
00110     cell->reparent( this, _cells.size() - 1 );
00111 }
00112 
00113 
00114 void
00115 YTableItem::addCell( const std::string & label, const std::string & iconName )
00116 {
00117     YTableCell * cell = new YTableCell( label, iconName );
00118     YUI_CHECK_NEW( cell );
00119 
00120     addCell( cell );
00121 }
00122 
00123 
00124 bool
00125 YTableItem::hasCell( int index ) const
00126 {
00127     return index >= 0 && (unsigned) index < _cells.size();
00128 }
00129 
00130 
00131 const YTableCell *
00132 YTableItem::cell( int index ) const
00133 {
00134     return hasCell( index ) ?
00135         _cells[ index ] : 0;
00136 }
00137 
00138 
00139 YTableCell *
00140 YTableItem::cell( int index )
00141 {
00142     return hasCell( index ) ?
00143         _cells[ index ] : 0;
00144 }
00145 
00146 
00147 std::string
00148 YTableItem::label( int index ) const
00149 {
00150     return hasCell( index ) ? _cells[ index ]->label() : "";
00151 }
00152 
00153 
00154 std::string
00155 YTableItem::iconName( int index ) const
00156 {
00157     return hasCell( index ) ? _cells[ index ]->iconName() : "";
00158 }
00159 
00160 
00161 bool
00162 YTableItem::hasIconName( int index ) const
00163 {
00164     return hasCell( index ) ? _cells[ index ]->hasIconName() : false;
00165 }
00166 
00167 
00168 
00169 
00170 
00171 void YTableCell::reparent( YTableItem * parent, int column )
00172 {
00173     YUI_CHECK_PTR( parent );
00174 
00175     if ( _parent && _parent != parent && column != column )
00176         YUI_THROW( YUIException( std::string( "Cannot reparent YTableCell \"" )
00177                                  + _label
00178                                  + "to different parent." ) );
00179     _parent = parent;
00180     _column = column;
00181 }
 All Classes Functions Variables Enumerations Friends