📄 controlwidgetbase.cpp
字号:
/************************************************************************ Copyright (C) 2000-2002 Trolltech AS. All rights reserved.**** This file is part of the Qtopia Environment.**** This file may be distributed and/or modified under the terms of the** GNU General Public License version 2 as published by the Free Software** Foundation and appearing in the file LICENSE.GPL included in the** packaging of this file.**** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.**** See http://www.trolltech.com/gpl/ for GPL licensing information.**** Contact info@trolltech.com if any conditions of this licensing are** not clear to you.************************************************************************/#include "imageutil.h"#include "audiowidget.h"#include "mediaplayerstate.h"#include <qtopia/config.h>#include <qtopia/mediaplayerplugininterface.h>#include <qtopia/resource.h>#include <qapplication.h>#include <qbitmap.h>#include <qbutton.h>#include <qfile.h>#include <qfontmetrics.h>#include <qframe.h>#include <qlineedit.h>#include <qpainter.h>#include <qpixmap.h>#include <qtimer.h>#include <qwidget.h>#include <stdlib.h>#include <stdio.h>ControlWidgetBase::ControlWidgetBase( QWidget* parent, const QString &skinPath, const QString &type, const char* name ) : QWidget( parent, name ), buttonCount( 0 ), slider( Qt::Horizontal, this ), time( this ), sliderBeingMoved( FALSE ){ setCaption( tr("Media Player") ); setMouseTracking( TRUE ); slider.setFixedHeight( 20 ); slider.setMinValue( 0 ); slider.setMaxValue( 1 ); slider.setFocusPolicy( QWidget::NoFocus ); time.setFocusPolicy( QWidget::NoFocus ); time.setAlignment( Qt::AlignCenter ); changeTextColor( &time ); skinW = 0; skinH = 0; skinScaleW = 0; skinScaleH = 0; skin = ""; skinFile = skinPath; skinType = type; imagesLoaded = FALSE; imagesScaled = FALSE; hadFirstResize = FALSE; resized = FALSE; canPaint = FALSE; connect( &slider, SIGNAL( sliderPressed() ), this, SLOT( sliderPressed() ) ); connect( &slider, SIGNAL( sliderReleased() ), this, SLOT( sliderReleased() ) ); connect( mediaPlayerState, SIGNAL( skinChanged(const QString&) ), this, SLOT( setSkin(const QString&) ) ); connect( mediaPlayerState, SIGNAL( lengthChanged(long) ), this, SLOT( setLength(long) ) ); connect( mediaPlayerState, SIGNAL( positionChanged(long) ),this, SLOT( setPosition(long) ) ); connect( mediaPlayerState, SIGNAL( positionUpdated(long) ),this, SLOT( setPosition(long) ) ); connect( mediaPlayerState, SIGNAL( seekableToggled(bool) ),this, SLOT( setSeekable(bool) ) ); //connect( mediaPlayerState, SIGNAL( viewChanged(View) ), this, SLOT( setView(View) ) ); connect( mediaPlayerState, SIGNAL( loopingToggled(bool) ), this, SLOT( setLooping(bool) ) ); connect( mediaPlayerState, SIGNAL( pausedToggled(bool) ), this, SLOT( setPaused(bool) ) ); connect( mediaPlayerState, SIGNAL( playingToggled(bool) ), this, SLOT( setPlaying(bool) ) ); connect( mediaPlayerState, SIGNAL( fullscreenToggled(bool) ), this, SLOT( setFullscreen(bool) ) );}ControlWidgetBase::~ControlWidgetBase(){}void ControlWidgetBase::setSkin( const QString& newskin ){ if ( skin == newskin ) return; qDebug("set %s skin", skinType.latin1() ); // Don't load and scale images yet. // Do this a bit at a time and as needed on demand tmpButtonImgLoaded[0] = FALSE; tmpButtonImgLoaded[1] = FALSE; buttonPixCreated[0] = FALSE; buttonPixCreated[1] = FALSE; for ( int i = 0; i < MaxButtonTypes; i++ ) { tmpButtonMaskLoaded[i] = FALSE; buttonMaskCreated[i] = FALSE; } imagesLoaded = FALSE; bgImgLoaded = FALSE; skin = newskin; // setSkin can be called from selecting a skin in the menus. It looks bad // to load the images etc while the menu is up so we delay the loading in a timer if ( resized || imagesScaled ) // If we have had a resize event we know what size to scale to and can // load and also scale silently in the background until we get a paintEvent QTimer::singleShot( 100, this, SLOT( scaleImages() ) ); else QTimer::singleShot( 0, this, SLOT( loadImages() ) );}void ControlWidgetBase::scaleImages(){ resizeObjects( skinW, skinH, skinScaleW, skinScaleH ); // Also set up the masks in the background if we can for ( int i = 0; i < buttonCount; i++ ) getButtonMask( i );}void ControlWidgetBase::loadImages(){ if ( imagesLoaded ) return; //qDebug("load %s images", skinType.latin1() ); QString skinPath = "mediaplayer/skins/" + skin + "/"; // Using findPixmap is a bit of an abuse Config cfg(Resource::findPixmap(skinPath+"config"), Config::File); cfg.setGroup("Skin"); if ( !bgImgLoaded ) { bgPix = Resource::loadPixmap( skinPath+cfg.readEntry("background", "background") ); bgNeedsScaling = FALSE; if ( bgPix.isNull() ) bgPix = Resource::loadPixmap( skinPath+cfg.readEntry("background_tiled", "background_tiled") ); if ( bgPix.isNull() ) { bgImg = Resource::loadImage( skinPath+cfg.readEntry("background_scaled", "background_scaled") ); bgNeedsScaling = TRUE; } bgImgLoaded = TRUE; } if ( !tmpButtonImgLoaded[0] ) { tmpButtonImg[0] = Resource::loadImage( skinPath+cfg.readEntry(skinType+"_up", skinType+"_up") ); tmpButtonImgLoaded[0] = TRUE; } if ( !tmpButtonImgLoaded[1] ) { tmpButtonImg[1] = Resource::loadImage( skinPath+cfg.readEntry(skinType+"_down", skinType+"_down") ); tmpButtonImgLoaded[1] = TRUE; } for ( int button = 0; button < buttonCount; button++ ) { if ( !tmpButtonMaskLoaded[button] ) { QString filename = skinType + "_mask_" + buttons[button].skinImage; // No tr cfg.readEntry(filename,filename); filename = skinPath + filename; QString file = Resource::findPixmap( filename ); if ( ( buttons[button].hasImage = !file.isNull() ) ) { tmpButtonMask[button] = QImage( file ).convertDepth( 1 ); tmpButtonMaskLoaded[button] = TRUE; } } } imagesLoaded = TRUE; //qDebug("loaded %s images", skinType.latin1() ); }void ControlWidgetBase::resetButtons(){ emit moreReleased(); emit lessReleased(); emit forwardReleased(); emit backwardReleased(); // Reset these buttons to unclicked for ( int i = 0; i < buttonCount; i++ ) { switch ( buttons[i].buttonType ) { case VolumeUpButton: case VolumeDownButton: case ForwardButton: case BackwardButton: buttons[i].isDown = FALSE; buttons[i].isHeld = FALSE; default: break; } }}void ControlWidgetBase::resizeEvent( QResizeEvent * ){ resized = TRUE; virtualResize();}void ControlWidgetBase::paintEvent( QPaintEvent *pe ){ if ( !resized ) { virtualResize(); resized = TRUE; } virtualPaint( pe ); // Set up the masks for ( int i = 0; i < buttonCount; i++ ) getButtonMask( i );}int ControlWidgetBase::resizeObjects( int w, int h, int scaleW, int scaleH, int cornerWidgetWidth ){ //qDebug("scale %s images -> %i %i %i %i", skinType.latin1(), w, h, scaleW, scaleH ); loadImages(); if ( !w || !h || !scaleW || !scaleH || !tmpButtonImg[0].width() || !tmpButtonImg[0].height() ) return 0; if ( bgNeedsScaling ) bgPix = bgImg.smoothScale( w, h ); setBackgroundPixmap( bgPix ); slider.setBackgroundOrigin( QWidget::ParentOrigin ); slider.setBackgroundPixmap( bgPix ); // Save parameters used skinW = w; skinH = h; skinScaleW = scaleW; skinScaleH = scaleH; scaleSize( scaleW, scaleH, tmpButtonImg[0].width(), tmpButtonImg[0].height(), ScaleMin ); actualScaleW = scaleW; actualScaleH = scaleH; int border = w / 40; QFontMetrics fm( qApp->font() ); int timeWidth = fm.width( " 00:00 / 00:00 " ); int timeHeight = 20; // fm.height(); imgButtonMask = QImage( scaleW, scaleH, 8, 255 ); imgButtonMask.fill( 0 ); slider.setFixedWidth( w - timeWidth - 3 * border - cornerWidgetWidth ); slider.setGeometry( border, h - timeHeight - border, w - timeWidth - 2 * border - cornerWidgetWidth, timeHeight ); time.setGeometry( w - timeWidth - border - cornerWidgetWidth, h - timeHeight - border, timeWidth, timeHeight ); xoff = ( w - scaleW ) / 2; // Center buttons horizontally yoff = h - scaleH - 2 * border - timeHeight; // Position buttons above slider and the time lineedit // Force these to update buttonPixCreated[0] = FALSE; buttonPixCreated[1] = FALSE; for ( int i = 0; i < buttonCount; i++ ) buttonMaskCreated[i] = FALSE; getButtonPix( FALSE ); //getButtonPix( TRUE ); // delayed imagesScaled = TRUE; //qDebug("scaled %s images", skinType.latin1() ); return actualScaleH;}void ControlWidgetBase::setButtonData( MediaButton *mediaButtons, int count ){ buttons = mediaButtons; buttonCount = count; setSkin( skinFile ); // Intialise state setLength( mediaPlayerState->length() ); setPosition( mediaPlayerState->position() ); setLooping( mediaPlayerState->looping() ); setPaused( mediaPlayerState->paused() ); setPlaying( mediaPlayerState->playing() ); setSeekable( mediaPlayerState->seekable() ); setFullscreen( mediaPlayerState->fullscreen() );}void ControlWidgetBase::setPaused( bool b ){ // setToggleButton( PauseButton, b ); setToggleButton( PlayButton, !b );}void ControlWidgetBase::setLooping( bool b ){ setToggleButton( LoopButton, b );}void ControlWidgetBase::setPlaying( bool b ){ setToggleButton( PlayButton, b );}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -