LcdSpinBox: use LcdWidget as base class

We can now use the new LcdWidget class as base class for actually
displaying our model's values.
This commit is contained in:
Tobias Doerffel
2014-01-06 20:40:28 +01:00
parent ec6c30549a
commit f568a81c7a
2 changed files with 27 additions and 246 deletions

View File

@@ -1,8 +1,8 @@
/*
* lcd_spinbox.h - class lcdSpinBox, an improved QLCDNumber
*
* Copyright (c) 2005-2008 Tobias Doerffel <tobydox/at/users.sourceforge.net>
*
* Copyright (c) 2005-2014 Tobias Doerffel <tobydox/at/users.sourceforge.net>
*
* This file is part of Linux MultiMedia Studio - http://lmms.sourceforge.net
*
* This program is free software; you can redistribute it and/or
@@ -26,32 +26,20 @@
#ifndef _LCD_SPINBOX_H
#define _LCD_SPINBOX_H
#include <QtCore/QMap>
#include <QtGui/QWidget>
#include "LcdWidget.h"
#include "AutomatableModelView.h"
class EXPORT lcdSpinBox : public QWidget, public IntModelView
class EXPORT lcdSpinBox : public LcdWidget, public IntModelView
{
Q_OBJECT
public:
lcdSpinBox( int _num_digits, QWidget * _parent, const QString & _name =
QString::null );
lcdSpinBox( int numDigits, QWidget* parent, const QString& name = QString::null );
lcdSpinBox( int _num_digits, const QString & _lcd_style,
QWidget * _parent, const QString & _name = QString::null );
lcdSpinBox( int numDigits, const QString& style, QWidget* parent, const QString& name = QString::null );
virtual ~lcdSpinBox();
void setLabel( const QString & _txt );
inline void addTextForValue( int _val, const QString & _text )
{
m_textForValue[_val] = _text;
update();
}
virtual void modelChanged()
{
ModelView::modelChanged();
@@ -60,8 +48,6 @@ public:
public slots:
virtual void setEnabled( bool _on );
virtual void setMarginWidth( int _width );
virtual void update();
@@ -71,26 +57,8 @@ protected:
virtual void mouseMoveEvent( QMouseEvent * _me );
virtual void mouseReleaseEvent( QMouseEvent * _me );
virtual void wheelEvent( QWheelEvent * _we );
virtual void paintEvent( QPaintEvent * _me );
virtual void updateSize();
private:
static const int charsPerPixmap = 12;
QMap<int, QString> m_textForValue;
QString m_display;
QString m_label;
QPixmap * m_lcdPixmap;
int m_cellWidth;
int m_cellHeight;
int m_numDigits;
int m_marginWidth;
QPoint m_origMousePos;

View File

@@ -1,9 +1,9 @@
/*
* lcd_spinbox.cpp - class lcdSpinBox, an improved QLCDNumber
*
* Copyright (c) 2005-2011 Tobias Doerffel <tobydox/at/users.sourceforge.net>
* Copyright (c) 2005-2014 Tobias Doerffel <tobydox/at/users.sourceforge.net>
* Copyright (c) 2008 Paul Giblock <pgllama/at/gmail.com>
*
*
* This file is part of Linux MultiMedia Studio - http://lmms.sourceforge.net
*
* This program is free software; you can redistribute it and/or
@@ -23,8 +23,6 @@
*
*/
#include <QtGui/QApplication>
#include <QtGui/QLabel>
#include <QtGui/QMouseEvent>
@@ -42,228 +40,43 @@
lcdSpinBox::lcdSpinBox( int _num_digits, QWidget * _parent,
const QString & _name ) :
QWidget( _parent ),
IntModelView( new IntModel( 0, 0, 0, NULL, _name, true ), this ),
m_label(),
m_numDigits( _num_digits ),
lcdSpinBox::lcdSpinBox( int numDigits, QWidget* parent, const QString& name ) :
LcdWidget( numDigits, parent, name ),
IntModelView( new IntModel( 0, 0, 0, NULL, name, true ), this ),
m_origMousePos()
{
setEnabled( true );
setWindowTitle( _name );
m_lcdPixmap = new QPixmap( embed::getIconPixmap( "lcd_19green" ) );
m_cellWidth = m_lcdPixmap->size().width() / lcdSpinBox::charsPerPixmap;
m_cellHeight = m_lcdPixmap->size().height() / 2;
m_marginWidth = m_cellWidth / 2;
updateSize();
}
lcdSpinBox::lcdSpinBox( int _num_digits, const QString & _lcd_style,
QWidget * _parent, const QString & _name ) :
QWidget( _parent ),
IntModelView( new IntModel( 0, 0, 0, NULL, _name, true ), this ),
m_label(),
m_numDigits( _num_digits ),
lcdSpinBox::lcdSpinBox( int numDigits, const QString& style, QWidget* parent, const QString& name ) :
LcdWidget( numDigits, parent, name ),
IntModelView( new IntModel( 0, 0, 0, NULL, name, true ), this ),
m_origMousePos()
{
setEnabled( true );
setWindowTitle( _name );
// We should make a factory for these or something.
m_lcdPixmap = new QPixmap( embed::getIconPixmap( QString( "lcd_" +
_lcd_style ).toUtf8().constData() ) );
m_cellWidth = m_lcdPixmap->size().width() / lcdSpinBox::charsPerPixmap;
m_cellHeight = m_lcdPixmap->size().height() / 2;
m_marginWidth = m_cellWidth / 2;
updateSize();
}
lcdSpinBox::~lcdSpinBox()
{
delete m_lcdPixmap;
}
void lcdSpinBox::paintEvent( QPaintEvent * _me )
{
QRect ur = _me->rect();
QPainter p( this );
QSize cellSize( m_cellWidth, m_cellHeight );
QRect cellRect( 0, 0, m_cellWidth, m_cellHeight );
int margin = 1; // QStyle::PM_DefaultFrameWidth;
//int lcdWidth = m_cellWidth * m_numDigits + (margin*m_marginWidth)*2;
// p.translate( width() / 2 - lcdWidth / 2, 0 );
p.save();
p.translate( margin, margin );
// Left Margin
p.drawPixmap( cellRect, *m_lcdPixmap,
QRect( QPoint( charsPerPixmap*m_cellWidth,
isEnabled()?0:m_cellHeight ),
cellSize ) );
p.translate( m_marginWidth, 0 );
// Padding
for( int i=0; i < m_numDigits - m_display.length(); i++ )
{
p.drawPixmap( cellRect, *m_lcdPixmap,
QRect( QPoint( 10 * m_cellWidth, isEnabled()?0:m_cellHeight) , cellSize ) );
p.translate( m_cellWidth, 0 );
}
// Digits
for( int i=0; i < m_display.length(); i++ )
{
int val = m_display[i].digitValue();
if( val < 0 )
{
if( m_display[i] == '-' )
val = 11;
else
val = 10;
}
p.drawPixmap( cellRect, *m_lcdPixmap,
QRect( QPoint( val*m_cellWidth,
isEnabled()?0:m_cellHeight ),
cellSize ) );
p.translate( m_cellWidth, 0 );
}
// Right Margin
p.drawPixmap( QRect( 0, 0, m_marginWidth-1, m_cellHeight ), *m_lcdPixmap,
QRect( charsPerPixmap*m_cellWidth, isEnabled()?0:m_cellHeight,
m_cellWidth / 2, m_cellHeight ) );
p.restore();
// Border
QStyleOptionFrame opt;
opt.initFrom( this );
opt.state = QStyle::State_Sunken;
opt.rect = QRect( 0, 0, m_cellWidth * m_numDigits + (margin+m_marginWidth)*2 - 1,
m_cellHeight + (margin*2) );
style()->drawPrimitive( QStyle::PE_Frame, &opt, &p, this );
p.resetTransform();
// Label
if( !m_label.isEmpty() )
{
p.setFont( pointSize<6>( p.font() ) );
p.setPen( QColor( 64, 64, 64 ) );
p.drawText( width() / 2 -
p.fontMetrics().width( m_label ) / 2 + 1,
height(), m_label );
p.setPen( QColor( 255, 255, 255 ) );
p.drawText( width() / 2 -
p.fontMetrics().width( m_label ) / 2,
height() - 1, m_label );
}
}
void lcdSpinBox::update()
{
QString s = m_textForValue[model()->value()];
if( s == "" )
{
s = QString::number( model()->value() );
// TODO: if pad == true
/*
while( (int) s.length() < m_numDigits )
{
s = "0" + s;
}
*/
}
m_display = s;
setValue( model()->value() );
QWidget::update();
}
void lcdSpinBox::setLabel( const QString & _txt )
void lcdSpinBox::contextMenuEvent( QContextMenuEvent* event )
{
m_label = _txt;
updateSize();
}
void lcdSpinBox::setEnabled( bool _on )
{
QWidget::setEnabled( _on );
}
void lcdSpinBox::setMarginWidth( int _width )
{
m_marginWidth = _width;
updateSize();
}
void lcdSpinBox::updateSize()
{
int margin = 1;
if (m_label.isEmpty()) {
setFixedSize( m_cellWidth * m_numDigits + 2*(margin+m_marginWidth),
m_cellHeight + (2*margin) );
}
else {
setFixedSize( qMax<int>(
m_cellWidth * m_numDigits + 2*(margin+m_marginWidth),
QFontMetrics( pointSize<6>( font() ) ).width( m_label ) ),
m_cellHeight + (2*margin) + 10 );
}
update();
}
void lcdSpinBox::contextMenuEvent( QContextMenuEvent * _me )
{
m_origMousePos = _me->globalPos();
m_origMousePos = event->globalPos();
// for the case, the user clicked right while pressing left mouse-
// button, the context-menu appears while mouse-cursor is still hidden
@@ -279,30 +92,30 @@ void lcdSpinBox::contextMenuEvent( QContextMenuEvent * _me )
void lcdSpinBox::mousePressEvent( QMouseEvent * _me )
void lcdSpinBox::mousePressEvent( QMouseEvent* event )
{
if( _me->button() == Qt::LeftButton &&
! ( _me->modifiers() & Qt::ControlModifier ) &&
_me->y() < m_cellHeight + 2 )
if( event->button() == Qt::LeftButton &&
! ( event->modifiers() & Qt::ControlModifier ) &&
event->y() < cellHeight() + 2 )
{
m_origMousePos = _me->globalPos();
m_origMousePos = event->globalPos();
QApplication::setOverrideCursor( Qt::BlankCursor );
model()->prepareJournalEntryFromOldVal();
}
else
{
IntModelView::mousePressEvent( _me );
IntModelView::mousePressEvent( event );
}
}
void lcdSpinBox::mouseMoveEvent( QMouseEvent * _me )
void lcdSpinBox::mouseMoveEvent( QMouseEvent* event )
{
if( _me->buttons() & Qt::LeftButton )
if( event->buttons() & Qt::LeftButton )
{
int dy = _me->globalY() - m_origMousePos.y();
int dy = event->globalY() - m_origMousePos.y();
if( dy > 1 || dy < -1 )
{
model()->setInitValue( model()->value() -
@@ -316,7 +129,7 @@ void lcdSpinBox::mouseMoveEvent( QMouseEvent * _me )
void lcdSpinBox::mouseReleaseEvent( QMouseEvent * _me )
void lcdSpinBox::mouseReleaseEvent( QMouseEvent* event )
{
model()->addJournalEntryFromOldToCurVal();