home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 2002 November / SGI Freeware 2002 November - Disc 3.iso / dist / fw_qt3.idb / usr / freeware / Qt / examples / demo / dnd / styledbutton.cpp.z / styledbutton.cpp
Encoding:
C/C++ Source or Header  |  2002-04-08  |  6.3 KB  |  290 lines

  1. /**********************************************************************
  2. ** Copyright (C) 2000 Trolltech AS.  All rights reserved.
  3. **
  4. ** This file is part of Qt Designer.
  5. **
  6. ** This file may be distributed and/or modified under the terms of the
  7. ** GNU General Public License version 2 as published by the Free Software
  8. ** Foundation and appearing in the file LICENSE.GPL included in the
  9. ** packaging of this file.
  10. **
  11. ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
  12. ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  13. **
  14. ** See http://www.trolltech.com/gpl/ for GPL licensing information.
  15. **
  16. ** Contact info@trolltech.com if any conditions of this licensing are
  17. ** not clear to you.
  18. **
  19. **********************************************************************/
  20.  
  21. #include "styledbutton.h"
  22.  
  23. #include <qcolordialog.h>
  24. #include <qpalette.h>
  25. #include <qlabel.h>
  26. #include <qpainter.h>
  27. #include <qimage.h>
  28. #include <qpixmap.h>
  29. #include <qapplication.h>
  30. #include <qdragobject.h>
  31. #include <qstyle.h>
  32.  
  33. StyledButton::StyledButton(QWidget* parent, const char* name)
  34.     : QButton( parent, name ), pix( 0 ), spix( 0 ), s( 0 ), mousePressed( FALSE )
  35. {
  36.     setMinimumSize( minimumSizeHint() );
  37.     setAcceptDrops( TRUE );
  38.  
  39.     connect( this, SIGNAL(clicked()), SLOT(onEditor()));
  40.  
  41.     setEditor( ColorEditor );
  42. }
  43.  
  44. StyledButton::StyledButton( const QBrush& b, QWidget* parent, const char* name, WFlags f )
  45.     : QButton( parent, name, f ), spix( 0 ), s( 0 )
  46. {
  47.     col = b.color();
  48.     pix = b.pixmap();
  49.     setMinimumSize( minimumSizeHint() );
  50. }
  51.  
  52. StyledButton::~StyledButton()
  53. {
  54. }
  55.  
  56. void StyledButton::setEditor( EditorType e )
  57. {
  58.     if ( edit == e )
  59.     return;
  60.  
  61.     edit = e;
  62.     update();
  63. }
  64.  
  65. StyledButton::EditorType StyledButton::editor() const
  66. {
  67.     return edit;
  68. }
  69.  
  70. void StyledButton::setColor( const QColor& c )
  71. {
  72.     col = c;
  73.     update();
  74. }
  75.  
  76. void StyledButton::setPixmap( const QPixmap & pm )
  77. {
  78.     if ( !pm.isNull() ) {
  79.     delete pix;
  80.     pix = new QPixmap( pm );
  81.     } else {
  82.     delete pix;
  83.     pix = 0;
  84.     }
  85.     scalePixmap();
  86. }
  87.  
  88. QColor StyledButton::color() const
  89. {
  90.     return col;
  91. }
  92.  
  93. QPixmap* StyledButton::pixmap() const
  94. {
  95.     return pix;
  96. }
  97.  
  98. bool StyledButton::scale() const
  99. {
  100.     return s;
  101. }
  102.  
  103. void StyledButton::setScale( bool on )
  104. {
  105.     if ( s == on )
  106.     return;
  107.  
  108.     s = on;
  109.     scalePixmap();
  110. }
  111.  
  112. QSize StyledButton::sizeHint() const
  113. {
  114.     return QSize( 50, 25 );
  115. }
  116.  
  117. QSize StyledButton::minimumSizeHint() const
  118. {
  119.     return QSize( 50, 25 );
  120. }
  121.  
  122. void StyledButton::scalePixmap()
  123. {
  124.     delete spix;
  125.  
  126.     if ( pix ) {
  127.     spix = new QPixmap( 6*width()/8, 6*height()/8 );
  128.     QImage img = pix->convertToImage();
  129.  
  130.     spix->convertFromImage( s? img.smoothScale( 6*width()/8, 6*height()/8 ) : img );
  131.     } else {
  132.     spix = 0;
  133.     }
  134.  
  135.     update();
  136. }
  137.  
  138. void StyledButton::resizeEvent( QResizeEvent* e )
  139. {
  140.     scalePixmap();
  141.     QButton::resizeEvent( e );
  142. }
  143.  
  144. void StyledButton::drawButton( QPainter *paint )
  145. {
  146.     style().drawPrimitive(QStyle::PE_ButtonBevel, paint, rect(), colorGroup(),
  147.               isDown() ? QStyle::Style_Sunken : QStyle::Style_Default);
  148.     drawButtonLabel(paint);
  149.  
  150.     if (hasFocus())
  151.     style().drawPrimitive(QStyle::PE_FocusRect, paint,
  152.                   style().subRect(QStyle::SR_PushButtonFocusRect, this),
  153.                   colorGroup(), QStyle::Style_Default);
  154. }
  155.  
  156. void StyledButton::drawButtonLabel( QPainter *paint )
  157. {
  158.     QColor pen = isEnabled() ?
  159.          hasFocus() ? palette().active().buttonText() : palette().inactive().buttonText()
  160.          : palette().disabled().buttonText();
  161.     paint->setPen( pen );
  162.  
  163.     if(!isEnabled()) {
  164.     paint->setBrush( QBrush( colorGroup().button() ) );
  165.     }
  166.     else if ( edit == PixmapEditor && spix ) {
  167.     paint->setBrush( QBrush( col, *spix ) );
  168.     paint->setBrushOrigin( width()/8, height()/8 );
  169.     } else
  170.     paint->setBrush( QBrush( col ) );
  171.  
  172.     paint->drawRect( width()/8, height()/8, 6*width()/8, 6*height()/8 );
  173. }
  174.  
  175. void StyledButton::onEditor()
  176. {
  177.     switch (edit) {
  178.     case ColorEditor: {
  179.     QColor c = QColorDialog::getColor( palette().active().background(), this );
  180.     if ( c.isValid() ) {
  181.         setColor( c );
  182.         emit changed();
  183.     }
  184.     } break;
  185.     case PixmapEditor: {
  186.     QPixmap p;
  187.         /*
  188.         if ( pixmap() )
  189.         p = qChoosePixmap( this,*pixmap() );
  190.         else
  191.         p = qChoosePixmap( this, QPixmap() );
  192.     if ( !p.isNull() ) {
  193.         setPixmap( p );
  194.         emit changed();
  195.     }
  196.         */
  197.     } break;
  198.     default:
  199.     break;
  200.     }
  201. }
  202.  
  203. void StyledButton::mousePressEvent(QMouseEvent* e)
  204. {
  205.     QButton::mousePressEvent(e);
  206.     mousePressed = TRUE;
  207.     pressPos = e->pos();
  208. }
  209.  
  210. void StyledButton::mouseMoveEvent(QMouseEvent* e)
  211. {
  212.     QButton::mouseMoveEvent( e );
  213. #ifndef QT_NO_DRAGANDDROP
  214.     if ( !mousePressed )
  215.     return;
  216.     if ( ( pressPos - e->pos() ).manhattanLength() > QApplication::startDragDistance() ) {
  217.     if ( edit == ColorEditor ) {
  218.         QColorDrag *drg = new QColorDrag( col, this );
  219.         QPixmap pix( 25, 25 );
  220.         pix.fill( col );
  221.         QPainter p( &pix );
  222.         p.drawRect( 0, 0, pix.width(), pix.height() );
  223.         p.end();
  224.         drg->setPixmap( pix );
  225.         mousePressed = FALSE;
  226.         drg->dragCopy();
  227.     }
  228.     else if ( edit == PixmapEditor && pix && !pix->isNull() ) {
  229.         QImage img = pix->convertToImage();
  230.         QImageDrag *drg = new QImageDrag( img, this );
  231.         if(spix)
  232.         drg->setPixmap( *spix );
  233.         mousePressed = FALSE;
  234.         drg->dragCopy();
  235.     }
  236.     }
  237. #endif
  238. }
  239.  
  240. #ifndef QT_NO_DRAGANDDROP
  241. void StyledButton::dragEnterEvent( QDragEnterEvent *e )
  242. {
  243.     setFocus();
  244.     if ( edit == ColorEditor && QColorDrag::canDecode( e ) )
  245.     e->accept();
  246.     else if ( edit == PixmapEditor && QImageDrag::canDecode( e ) )
  247.     e->accept();
  248.     else
  249.     e->ignore();
  250. }
  251.  
  252. void StyledButton::dragLeaveEvent( QDragLeaveEvent * )
  253. {
  254.     if ( hasFocus() )
  255.     parentWidget()->setFocus();
  256. }
  257.  
  258. void StyledButton::dragMoveEvent( QDragMoveEvent *e )
  259. {
  260.     if ( edit == ColorEditor && QColorDrag::canDecode( e ) )
  261.     e->accept();
  262.     else if ( edit == PixmapEditor && QImageDrag::canDecode( e ) )
  263.     e->accept();
  264.     else
  265.     e->ignore();
  266. }
  267.  
  268. void StyledButton::dropEvent( QDropEvent *e )
  269. {
  270.     if ( edit == ColorEditor && QColorDrag::canDecode( e ) ) {
  271.     QColor color;
  272.     QColorDrag::decode( e, color );
  273.     setColor(color);
  274.     emit changed();
  275.     e->accept();
  276.     }
  277.     else if ( edit == PixmapEditor && QImageDrag::canDecode( e ) ) {
  278.     QImage img;
  279.     QImageDrag::decode( e, img );
  280.     QPixmap pm;
  281.     pm.convertFromImage(img);
  282.     setPixmap(pm);
  283.     emit changed();
  284.     e->accept();
  285.     } else {
  286.     e->ignore();
  287.     }
  288. }
  289. #endif // QT_NO_DRAGANDDROP
  290.