Commit 7d6514bd authored by Frédéric Larue's avatar Frédéric Larue
Browse files

ByteSizeInputWidget added.

parent b80c08b2
/*
* (c) LSIIT, UMR CNRS/UdS
* Authors: F. Larue.
*
* See licence.txt for additional information.
*/
#include "ByteSizeInputWidget.h"
ByteSizeInputWidget::ByteSizeInputWidget( QWidget *parent ) : QLineEdit( parent )
{
setByteSize( 0 );
connect( this, SIGNAL(textChanged(QString)), this, SLOT(convertTextToByteSize(QString)) );
}
ByteSizeInputWidget::ByteSizeInputWidget( uint64_t byteSize, QWidget *parent ) : QLineEdit( parent )
{
setByteSize( byteSize );
connect( this, SIGNAL(textChanged(QString)), this, SLOT(convertTextToByteSize(QString)) );
}
bool ByteSizeInputWidget::isAValidByteSize( const QString& text )
{
if( text.isEmpty() )
return true;
int i;
for( i=0; i<text.size()-1; ++i )
if( !text[i].isDigit() )
return false;
QChar last = text[i];
return last.isDigit() || last == 'K' || last == 'M' || last == 'G' || last == 'T';
}
void ByteSizeInputWidget::setByteSize( uint64_t byteSize )
{
if( byteSize == m_ByteSize )
return;
m_ByteSize = byteSize;
if( !(byteSize & (uint64_t(1024)*1024*1024*1024-1)) )
setText( QString::number( byteSize>>40 ) + 'T' );
if( !(byteSize & (1024*1024*1024-1)) )
setText( QString::number( byteSize>>30 ) + 'G' );
if( !(byteSize & (1024*1024-1)) )
setText( QString::number( byteSize>>20 ) + 'M' );
if( !(byteSize & (1024-1)) )
setText( QString::number( byteSize>>10 ) + 'K' );
setText( QString::number( byteSize ) );
emit byteSizeChanged( byteSize );
}
void ByteSizeInputWidget::convertTextToByteSize( const QString &text )
{
uint64_t byteSize;
QString txt = text;
if( !txt.isEmpty() )
{
QChar suffix = txt.at( txt.length()-1 );
if( !suffix.isDigit() )
{
byteSize = txt.left( txt.length()-1 ).toULongLong();
if( suffix == "K" )
byteSize <<= 10;
else if( suffix == "M" )
byteSize <<= 20;
else if( suffix == "G" )
byteSize <<= 30;
else if( suffix == "T" )
byteSize <<= 40;
}
else
byteSize = txt.toULongLong();
}
else
byteSize = 0;
if( byteSize != m_ByteSize )
{
m_ByteSize = byteSize;
emit byteSizeChanged( byteSize );
}
}
void ByteSizeInputWidget::keyPressEvent( QKeyEvent *evt )
{
switch( evt->key() )
{
case Qt::Key_K:
case Qt::Key_M:
case Qt::Key_G:
case Qt::Key_T:
{
if( evt->text()[0].isLower() )
{
QKeyEvent e( QEvent::Type::KeyPress, evt->key(), evt->modifiers(), evt->text().toUpper() );
keyPressEvent( &e );
return;
}
}
}
switch( evt->key() )
{
case Qt::Key_V: // CTRL-V for pasting.
{
if( !(evt->modifiers() & Qt::ControlModifier) )
break;
}
case Qt::Key_0:
case Qt::Key_1:
case Qt::Key_2:
case Qt::Key_3:
case Qt::Key_4:
case Qt::Key_5:
case Qt::Key_6:
case Qt::Key_7:
case Qt::Key_8:
case Qt::Key_9:
case Qt::Key_K:
case Qt::Key_M:
case Qt::Key_G:
case Qt::Key_T:
case Qt::Key_Backspace:
case Qt::Key_Delete:
{
QString t = text(), left, right;
if( selectionStart() >= 0 )
{
left = t.left( selectionStart() );
right = t.mid( selectionStart() + selectedText().length() );
}
else
{
left = t.left( cursorPosition() - (evt->key()==Qt::Key_Backspace? 1 : 0) );
right = t.mid ( cursorPosition() + (evt->key()==Qt::Key_Delete ? 1 : 0) );
}
if( evt->key() == Qt::Key_Backspace || evt->key() == Qt::Key_Delete )
t = left + right;
else if( evt->key() == Qt::Key_V )
t = left + QApplication::clipboard()->text() + right;
else
t = left + evt->text() + right;
int i;
for( i=0; i<t.length(); ++i )
if( t[i] != '0' )
break;
t = t.mid(i);
if( t.isEmpty() )
setText( "0" );
else if( isAValidByteSize(t) )
{
if( t[0].isDigit() )
{
if( !m_ByteSize )
setText( "" );
QLineEdit::keyPressEvent( evt );
}
else
setText( "0" );
}
break;
}
case Qt::Key_C:
case Qt::Key_X:
{
if( !(evt->modifiers() & Qt::ControlModifier) )
break;
}
case Qt::Key_Right:
case Qt::Key_Left:
{
QLineEdit::keyPressEvent( evt );
break;
}
}
}
/*
* (c) LSIIT, UMR CNRS/UdS
* Authors: F. Larue.
*
* See licence.txt for additional information.
*/
#ifndef BYTESIZEINPUTWIDGET_H
#define BYTESIZEINPUTWIDGET_H
#include <QtWidgets>
class ByteSizeInputWidget : public QLineEdit
{
Q_OBJECT
uint64_t m_ByteSize;
bool isAValidByteSize( const QString& text );
public:
ByteSizeInputWidget( QWidget *parent = NULL );
ByteSizeInputWidget( uint64_t byteSize, QWidget *parent = NULL );
inline uint64_t byteSize() const { return m_ByteSize; }
virtual void keyPressEvent( QKeyEvent *evt );
signals:
void byteSizeChanged( uint64_t byteSize );
private slots:
void convertTextToByteSize( const QString &text );
public slots:
void setByteSize( uint64_t byteSize );
};
#endif // BYTESIZEINPUTWIDGET_H
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment