/* * text_float.cpp - class textFloat, a floating text-label * * Copyright (c) 2005-2008 Tobias Doerffel * * This file is part of Linux MultiMedia Studio - http://lmms.sourceforge.net * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public * License along with this program (see COPYING); if not, write to the * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301 USA. * */ #include #include #include "text_float.h" #include "gui_templates.h" #include "main_window.h" #include "engine.h" textFloat::textFloat( void ) : QWidget( engine::getMainWindow(), Qt::ToolTip ), m_title( "" ), m_text( "" ), m_pixmap() { resize( 20, 20 ); hide(); setFont( pointSize<8>( font() ) ); } void textFloat::setTitle( const QString & _title ) { m_title = _title; updateSize(); } void textFloat::setText( const QString & _text ) { m_text = _text; updateSize(); } void textFloat::setPixmap( const QPixmap & _pixmap ) { m_pixmap = _pixmap; updateSize(); } void textFloat::setVisibilityTimeOut( int _msecs ) { QTimer::singleShot( _msecs, this, SLOT( hide() ) ); show(); } textFloat * textFloat::displayMessage( const QString & _msg, int _timeout, QWidget * _parent, int _add_y_margin ) { QWidget * mw = engine::getMainWindow(); textFloat * tf = new textFloat; if( _parent != NULL ) { tf->moveGlobal( _parent, QPoint( _parent->width() + 2, 0 ) ); } else { tf->moveGlobal( mw, QPoint( 32, mw->height() - tf->height() - 8 - _add_y_margin ) ); } tf->setText( _msg ); tf->show(); if( _timeout > 0 ) { tf->setAttribute( Qt::WA_DeleteOnClose, TRUE ); QTimer::singleShot( _timeout, tf, SLOT( close() ) ); } return( tf ); } textFloat * textFloat::displayMessage( const QString & _title, const QString & _msg, const QPixmap & _pixmap, int _timeout, QWidget * _parent ) { textFloat * tf = displayMessage( _msg, _timeout, _parent, 16 ); tf->setTitle( _title ); tf->setPixmap( _pixmap ); return( tf ); } void textFloat::paintEvent( QPaintEvent * _pe ) { QPainter p( this ); p.setPen( QColor( 0, 0, 0 ) ); p.setBrush( QColor( 224, 224, 224 ) ); p.setFont( pointSize<8>( p.font() ) ); p.drawRect( 0, 0, rect().right(), rect().bottom() ); // p.setPen( Qt::black ); // small message? if( m_title == "" ) { p.drawText( 2, 10, m_text ); } else { int text_x = 2; if( m_pixmap.isNull() == FALSE ) { p.drawPixmap( 5, 5, m_pixmap ); text_x += m_pixmap.width() + 8; } p.drawText( text_x, 28, m_text ); QFont f = p.font(); f.setBold( TRUE ); p.setFont( f ); p.drawText( text_x, 12, m_title ); } } void textFloat::mousePressEvent( QMouseEvent * ) { close(); } void textFloat::updateSize( void ) { QFontMetrics metrics( pointSize<8>( font() ) ); QRect textBound = metrics.boundingRect( m_text ); if( m_title != "" ) { QFont f = pointSize<8>( font() ); f.setBold( TRUE ); int title_w = QFontMetrics( f ).boundingRect( m_title ).width(); if( title_w > textBound.width() ) { textBound.setWidth( title_w ); } textBound.setHeight( textBound.height() * 2 + 14 ); } if( m_pixmap.isNull() == FALSE ) { textBound.setWidth( textBound.width() + m_pixmap.width() + 10 ); } resize( textBound.width() + 5, textBound.height()*3/4+2 ); //move( QPoint( parentWidget()->width() + 5, 5 ) ); update(); }