1 // Copyright (C) 2005 OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
8 // This library is distributed in the hope that it will be useful
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/
19 // File: QtxSplash.cxx
20 // Author: Vadim SANDLER
22 #include "QtxSplash.h"
24 #include <qapplication.h>
27 #include <qmessagebox.h>
29 const int _PROGRESS_EVENT = QEvent::User + 10;
30 const int _PROGRESS_WIDTH = 10;
33 Class ProgressEvent [ internal ].
35 class ProgressEvent: public QCustomEvent
38 ProgressEvent( const QString& msg, const int progress = 0 )
39 : QCustomEvent( id() ),
41 myProgress( progress )
43 QString message() const { return myMessage; }
44 int progress() const { return myProgress; }
45 static int id() { return _PROGRESS_EVENT; }
52 // Only one instance of splash screen is allowed
53 QtxSplash* QtxSplash::mySplash = 0;
56 Construct a splash screen that will display the \a pixmap.
58 QtxSplash::QtxSplash( const QPixmap& pixmap )
59 : QWidget( 0, 0, WStyle_Customize | WStyle_StaysOnTop | WX11BypassWM | WStyle_NoBorder )
61 myAlignment = AlignBottom | AlignRight;
63 myHideOnClick = false;
66 myGradientType = Vertical;
76 QtxSplash::~QtxSplash()
82 Returns an only instance of splash screen.
83 If \a px is valid sets this pixmap to the splash screen.
85 QtxSplash* QtxSplash::splash( const QPixmap& px )
88 mySplash = new QtxSplash( px );
89 else if ( !px.isNull() )
90 mySplash->setPixmap( px );
95 Sends the status message and (optionally) progress to the splash screen.
96 Can be used, for example, from the progress thread.
98 void QtxSplash::setStatus( const QString& msg,
102 QApplication::postEvent( mySplash, new ProgressEvent( msg, progress ) );
103 qApp->processEvents();
108 Sets error status and shows error message box to the user.
110 void QtxSplash::error( const QString& error, const QString& title, const int code )
112 printf("QtxSplash::error: %s\n",error.latin1());
114 mySplash->setError( code );
115 QMessageBox::critical( mySplash,
116 title.isEmpty() ? tr( "Error" ) : title,
123 Sets the pixmap that will be used as the splash screen's image to
126 void QtxSplash::setPixmap( const QPixmap& pixmap )
129 QRect r( 0, 0, myPixmap.size().width(), myPixmap.size().height() );
130 resize( myPixmap.size() );
131 move( QApplication::desktop()->screenGeometry().center() - r.center() );
136 Returns the pixmap that is used in the splash screen.
138 QPixmap QtxSplash::pixmap() const
144 Sets/clear the 'hide on mouse click' flag.
146 When this flag is set, user can hide the splash screen window
147 by clicking on it with mouse.
148 But for this to work it is necessary to call periodically
149 QApplication::processEvents() in order to allow event loop to process
150 events because usually main application loop is not yet started
153 void QtxSplash::setHideOnClick( const bool on )
159 Returns the 'hide on mouse click' flag.
161 bool QtxSplash::hideOnClick() const
163 return myHideOnClick;
167 Sets total progress steps to \a total.
169 void QtxSplash::setTotalSteps( const int total )
176 Return total progress steps number.
177 \sa setTotalSteps(), setProgress()
179 int QtxSplash::totalSteps() const
185 Sets progress to \a progress.
187 void QtxSplash::setProgress( const int progress )
189 myProgress = progress;
194 Return current progress.
195 \sa setProgress(), setTotalSteps()
197 int QtxSplash::progress() const
203 Sets progress to \a progress and total progress steps to \a total.
205 void QtxSplash::setProgress( const int progress, const int total )
208 myProgress = progress;
213 Sets progress bar colors to \a startColor and \a endColor.
214 If the colors differ the gradient color bar is drawed.
215 If the \a endColor is not valid, \a startColor is used instead.
216 \a gradientType sets the type of gradient to be used for progress
217 bar - horizontal or vertical. Default is vertical.
219 void QtxSplash::setProgressColors( const QColor& startColor,
220 const QColor& endColor,
221 const int gradientType )
223 myStartColor = startColor;
224 myEndColor = endColor;
225 myGradientType = gradientType;
230 Return progress colors and gradient type (horizontal or vertical).
231 \sa setProgressColors()
233 int QtxSplash::progressColors( QColor& startColor, QColor& endColor )
235 startColor = myStartColor;
236 endColor = myEndColor;
237 return myGradientType;
241 Sets message text alignment flags to \a alignment.
242 Default is AlignBottom | AlignRight.
244 void QtxSplash::setTextAlignment( const int alignment )
246 myAlignment = alignment;
251 Return message text alignment flags.
252 \sa setTextAlignment()
254 int QtxSplash::textAlignment() const
260 Sets message text color to \a color.
264 void QtxSplash::setTextColor( const QColor& color )
267 myShadowColor = QColor();
272 Return message text color.
275 QColor QtxSplash::textColor() const
281 Sets message text color to \a color and text shadow color to \a shadow.
284 void QtxSplash::setTextColors( const QColor& color, const QColor& shadow )
287 myShadowColor = shadow;
292 Return message text color and text shadow color.
295 void QtxSplash::textColors( QColor& color, QColor& shadow ) const
298 shadow = myShadowColor;
302 Returns current status message.
304 QString QtxSplash::message() const
310 Return error code. If no errors were occured returns 0.
311 Error code can be set by error( QString&, QString, int ).
313 int QtxSplash::error() const
319 Makes the splash screen wait until the widget \a mainWin is displayed
320 before calling close() on itself.
322 void QtxSplash::finish( QWidget* mainWin )
325 #if defined(Q_WS_X11)
326 extern void qt_wait_for_window_manager( QWidget* w );
327 qt_wait_for_window_manager( mainWin );
334 Repaint the splash screen.
336 void QtxSplash::repaint()
340 QApplication::flush();
344 Draws the \a message text onto the splash screen with color \a
345 color and aligns the text according to the flags in \a alignment.
347 void QtxSplash::message( const QString& msg,
349 const QColor& color )
352 myAlignment = alignment;
358 This is an overloaded member function, provided for convenience.
359 It behaves essentially like the above function.
360 Draws the \a message text onto the splash screen with default color
361 and aligns the text according to the default alignment flags.
363 void QtxSplash::message( const QString& msg )
370 Removes the message being displayed on the splash screen.
373 void QtxSplash::clear()
375 myMessage = QString::null;
380 Draw the contents of the splash screen using painter \a painter.
382 void QtxSplash::drawContents( QPainter* painter )
386 // draw progress bar outline rectangle
387 painter->setPen( palette().active().dark() );
388 painter->drawLine( r.x()+5,
389 r.height()-5-_PROGRESS_WIDTH,
391 r.height()-5-_PROGRESS_WIDTH );
392 painter->drawLine( r.x()+5,
393 r.height()-5-_PROGRESS_WIDTH,
396 painter->setPen( palette().active().light() );
397 painter->drawLine( r.x()+5,
401 painter->drawLine( r.width()-5,
402 r.height()-5-_PROGRESS_WIDTH,
406 if ( myGradientType == Horizontal ) {
407 int tng = r.width() - r.x() - 11;
408 int ng = (int) ( 1.0 * tng * ( myProgress > 0 ? myProgress : 0 ) / myTotal );
409 int h1, h2, s1, s2, v1, v2;
410 myStartColor.hsv( &h1, &s1, &v1 );
411 myEndColor.isValid() ? myEndColor.hsv( &h2, &s2, &v2 ) :
412 myStartColor.hsv( &h2, &s2, &v2 );
413 for ( int i = 0; i < ng; i++ ) {
414 painter->setPen( QColor( h1 + ((h2-h1)*i)/(tng-1),
415 s1 + ((s2-s1)*i)/(tng-1),
416 v1 + ((v2-v1)*i)/(tng-1),
418 painter->drawLine( r.x()+6+i,
419 r.height()-5-_PROGRESS_WIDTH+1,
425 int ng = (int) ( 1.0 * (r.width() - r.x() - 11) * ( myProgress > 0 ? myProgress : 0 ) / myTotal );
426 int h1, h2, s1, s2, v1, v2;
427 myStartColor.hsv( &h1, &s1, &v1 );
428 myEndColor.isValid() ? myEndColor.hsv( &h2, &s2, &v2 ) :
429 myStartColor.hsv( &h2, &s2, &v2 );
430 for ( int i = 0; i < _PROGRESS_WIDTH-1; i++ ) {
431 painter->setPen( QColor( h1 + ((h2-h1)*i)/(_PROGRESS_WIDTH-2),
432 s1 + ((s2-s1)*i)/(_PROGRESS_WIDTH-2),
433 v1 + ((v2-v1)*i)/(_PROGRESS_WIDTH-2),
435 painter->drawLine( r.x()+6,
436 r.height()-5-_PROGRESS_WIDTH+1+i,
438 r.height()-5-_PROGRESS_WIDTH+1+i );
443 if ( !myMessage.isEmpty() ) {
444 QFontMetrics f( font() );
445 int spacing = f.lineSpacing();
446 int shift = myTotal > 0 ? _PROGRESS_WIDTH : _PROGRESS_WIDTH; // : 0
447 int i = myMessage.length() - 1;
448 while( i >= 0 && myMessage[ i-- ] == '\n' )
450 QRect r1( r.x() + 5, r.y() + 5, r.width() - 10, r.height() - 10 - shift );
452 if ( myAlignment & Qt::AlignLeft ) r2.setLeft ( r2.left() + 1 );
453 if ( myAlignment & Qt::AlignTop ) r2.setTop ( r2.top() + 1 );
454 if ( myAlignment & Qt::AlignRight ) r2.setRight ( r2.right() + 1 );
455 if ( myAlignment & Qt::AlignBottom ) r2.setBottom( r2.bottom() + 1 );
456 if ( myShadowColor.isValid() ) {
457 painter->setPen( myShadowColor );
458 painter->drawText( r2, myAlignment, myMessage );
460 painter->setPen( myColor );
461 painter->drawText( r1, myAlignment, myMessage );
467 Hides splash screen if the 'hide on mouse click' flag is set.
470 void QtxSplash::mousePressEvent( QMouseEvent* )
477 Processes custom event sent by setStatus() method.
480 void QtxSplash::customEvent( QCustomEvent* ce )
482 if ( ce->type() == ProgressEvent::id() ) {
483 ProgressEvent* pe = (ProgressEvent*)ce;
484 pe->message().isEmpty() ? clear() : message( pe->message() );
485 setProgress( pe->progress() );
486 qApp->processEvents();
491 Draws the splash screen window [ internal ].
493 void QtxSplash::drawContents()
495 QPixmap textPix = myPixmap;
496 QPainter painter( &textPix, this );
497 drawContents( &painter );
498 setErasePixmap( textPix );
502 Sets error code [ internal ].
504 void QtxSplash::setError( const int code )