sizewidget.cpp

00001 /*
00002  *  This file is part of the KDE libraries
00003  *  Copyright (c) 2002 Michael Goffioul <kdeprint@swing.be>
00004  *
00005  *  This library is free software; you can redistribute it and/or
00006  *  modify it under the terms of the GNU Library General Public
00007  *  License version 2 as published by the Free Software Foundation.
00008  *
00009  *  This library is distributed in the hope that it will be useful,
00010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  *  Library General Public License for more details.
00013  *
00014  *  You should have received a copy of the GNU Library General Public License
00015  *  along with this library; see the file COPYING.LIB.  If not, write to
00016  *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  *  Boston, MA 02110-1301, USA.
00018  **/
00019 
00020 #include "sizewidget.h"
00021 
00022 #include <qcombobox.h>
00023 #include <qspinbox.h>
00024 #include <qlayout.h>
00025 #include <qregexp.h>
00026 #include <klocale.h>
00027 
00028 SizeWidget::SizeWidget( QWidget *parent, const char *name )
00029     : QWidget( parent, name )
00030 {
00031     m_size = new QSpinBox( 0, 9999, 1, this );
00032     m_unit = new QComboBox( this );
00033 
00034     m_unit->insertItem( i18n( "KB" ) );
00035     m_unit->insertItem( i18n( "MB" ) );
00036     m_unit->insertItem( i18n( "GB" ) );
00037     m_unit->insertItem( i18n( "Tiles" ) );
00038     m_unit->setCurrentItem( 1 );
00039     m_size->setSpecialValueText( i18n( "Unlimited" ) );
00040 
00041     QHBoxLayout *l0 = new QHBoxLayout( this, 0, 5 );
00042     l0->addWidget( m_size, 1 );
00043     l0->addWidget( m_unit, 0 );
00044 }
00045 
00046 void SizeWidget::setSizeString( const QString& sz )
00047 {
00048     int p = sz.find( QRegExp( "\\D" ) );
00049     m_size->setValue( sz.left( p ).toInt() );
00050     switch( sz[ p ].latin1() )
00051     {
00052         case 'k': p = 0; break;
00053         default:
00054         case 'm': p = 1; break;
00055         case 'g': p = 2; break;
00056         case 't': p = 3; break;
00057     }
00058     m_unit->setCurrentItem( p );
00059 }
00060 
00061 QString SizeWidget::sizeString() const
00062 {
00063     QString result = QString::number( m_size->value() );
00064     switch ( m_unit->currentItem() )
00065     {
00066         case 0: result.append( "k" ); break;
00067         case 1: result.append( "m" ); break;
00068         case 2: result.append( "g" ); break;
00069         case 3: result.append( "t" ); break;
00070     }
00071     return result;
00072 }
00073 
00074 void SizeWidget::setValue( int value )
00075 {
00076     m_size->setValue( value );
00077     m_unit->setCurrentItem( 1 );
00078 }
00079 
00080 int SizeWidget::value() const
00081 {
00082     return m_size->value();
00083 }
KDE Home | KDE Accessibility Home | Description of Access Keys