kivio

stencilbarbutton.cpp

00001 /*
00002  * Kivio - Visual Modelling and Flowcharting
00003  * Copyright (C) 2000-2001 theKompany.com & Dave Marotti
00004  *
00005  * This program is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU General Public License
00007  * as published by the Free Software Foundation; either version 2
00008  * of the License, or (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software
00017  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00018  */
00019 #include "stencilbarbutton.h"
00020 
00021 #include <kiconloader.h>
00022 #include <kstringhandler.h>
00023 #include <qapplication.h>
00024 #include <qstyle.h>
00025 #include <qpainter.h>
00026 #include <qpixmap.h>
00027 #include <qtooltip.h>
00028 #include <kdebug.h>
00029 #include "qdrawutil.h"
00030 
00031 using namespace Kivio;
00032 
00033 DragBarButton::DragBarButton( const QString& text, QWidget* parent, const char* name )
00034 : QPushButton(text,parent,name)
00035 {
00036   m_pIcon = 0L;
00037   m_bPressed = false;
00038   m_bDragged = false;
00039   m_bMouseOn = false;
00040   m_bClose = false;
00041   m_orientation = Qt::Vertical;
00042 
00043   const char* stencil_xpm[] = {
00044   "12 12 17 1",
00045   ".    c #08009A",
00046   "+    c #9A9C10",
00047   "@    c None",
00048   "#    c #0800A1",
00049   "$    c #FAFD00",
00050   "%    c #080092",
00051   "&    c #F9FC07",
00052   "*    c #F9FC00",
00053   "=    c #070094",
00054   "-    c #F4F70C",
00055   ";    c #F6F900",
00056   ">    c #08008D",
00057   ",    c #F7FA00",
00058   "'    c #C00101",
00059   ")    c #C20904",
00060   "!    c #C90000",
00061   "~    c #BC0000",
00062   ".......+++@@",
00063   "..#...$$$$+@",
00064   "..%..&$***$+",
00065   ".=..-***$;*+",
00066   ".>.$**$*$,*+",
00067   "..;**$$$$,*+",
00068   ".$')!!!!!!!~",
00069   "..-!!!!!!!!+",
00070   "...-!!!!!!!+",
00071   "....-!!!!!++",
00072   "....%-!!+++@",
00073   "......++++@@"};
00074 
00075   setPixmap(stencil_xpm);
00076 
00077   const QColorGroup& g = colorGroup();
00078   QColor c = g.buttonText();
00079 
00080   int cr, cg, cb;
00081   QString line;
00082   c.rgb( &cr, &cg, &cb );
00083   line.sprintf(". c #%02X%02X%02X",cr,cg,cb);
00084 
00085   const char* close_xpm[] = {
00086   "8 7 2 1",
00087   "x c None",
00088   (const char*)line.ascii(),
00089   "xxxxxxxx",
00090   "x..xx..x",
00091   "xx....xx",
00092   "xxx..xxx",
00093   "xx....xx",
00094   "x..xx..x",
00095   "xxxxxxxx"
00096   };
00097   m_pClosePix = new QPixmap(close_xpm);
00098 
00099   QToolTip::add(this, text);
00100 }
00101 
00102 DragBarButton::~DragBarButton()
00103 {
00104   delete m_pClosePix;
00105   delete m_pIcon;
00106 
00107   kdDebug(43000) << "DragBarButton - AHHHHHH I'M DYING!" << endl;
00108 }
00109 
00110 void DragBarButton::drawButton( QPainter* paint )
00111 {
00112   QSize pixSize;
00113 
00114   if(m_orientation == Qt::Vertical) {
00115     pixSize = QSize(width(), height());
00116   } else {
00117     pixSize = QSize(height(), width());
00118   }
00119 
00120   QPixmap pix(pixSize.width(), pixSize.height());
00121   pix.fill(eraseColor());
00122   QPainter pixPainter(&pix);
00123 
00124   const QColorGroup& g = colorGroup();
00125   style().drawControl( QStyle::CE_PushButton, &pixPainter, this, QRect(0, 0, pixSize.width(), pixSize.height()), colorGroup() );
00126 
00127   int m = 3;
00128   int tw = 0;
00129   int pw = 0;
00130 
00131   if ( m_pIcon ) {
00132     pw = m_pIcon->width();
00133     style().drawItem( &pixPainter, QRect( m, 0, pw, pixSize.height() ),
00134                       AlignLeft | AlignVCenter,
00135                       colorGroup(), isEnabled(),
00136                       m_pIcon, QString::null, -1,
00137                       &g.buttonText());
00138   }
00139 
00140   if ( !text().isEmpty() ) {
00141     QFontMetrics fm = fontMetrics();
00142     QSize sz = fm.size( ShowPrefix, text() );
00143     tw = sz.width();
00144     int x = m + pw + (tw!=0 && pw!=0 ?m:0);
00145     QString t = KStringHandler::rPixelSqueeze(text(), fm, pixSize.width() - (x + m + 22));
00146 
00147     style().drawItem( &pixPainter, QRect( x, 0, tw, pixSize.height() ),
00148                       AlignLeft | AlignVCenter|ShowPrefix,
00149                       colorGroup(), isEnabled(),
00150                       0L, t, -1,
00151                       &g.buttonText());
00152   }
00153 
00154 
00155   QPoint p1(pixSize.width() - 10, 0);
00156   QPoint p2(pixSize.width() - 10, pixSize.height());
00157   qDrawShadeLine( &pixPainter, p1, p2, g, true, 0, 1 );
00158 
00159   p1 += QPoint(2, 0);
00160   p2 += QPoint(2, 0);
00161   qDrawShadeLine( &pixPainter, p1, p2, g, true, 0, 1 );
00162 
00163   if (m_bMouseOn) {
00164     int z = m_bClose ? 1:0;
00165     pixPainter.drawPixmap(pixSize.width() - 20 + z, (pixSize.height() - m_pClosePix->height()) / 2 + z, *m_pClosePix);
00166   }
00167 
00168   if(m_orientation == Qt::Vertical) {
00169     paint->drawPixmap(0, 0, pix);
00170   } else {
00171     paint->rotate(-90);
00172     paint->drawPixmap(1 - pixSize.width(), 0, pix);
00173   }
00174 }
00175 
00176 void DragBarButton::setIcon( const QString& name )
00177 {
00178   m_pIcon = new QPixmap(BarIcon(name));
00179 }
00180 
00181 void DragBarButton::setPixmap( const QPixmap& pixmap )
00182 {
00183   m_pIcon = new QPixmap(pixmap);
00184 }
00185 
00186 QSize DragBarButton::sizeHint() const
00187 {
00188   constPolish();
00189   int m = 3;
00190   int tw = 0;
00191   int th = 0;
00192   int pw = 0;
00193   int ph = 0;
00194 
00195   if ( !text().isEmpty() ) {
00196     QFontMetrics fm = fontMetrics();
00197     QSize sz = fm.size( ShowPrefix, text() );
00198     tw = 64; // Minimum size
00199     th = sz.height();
00200   }
00201 
00202   if ( m_pIcon ) {
00203     pw = m_pIcon->width();
00204     ph = m_pIcon->height();
00205   }
00206 
00207   int h = QMAX(ph,th)+2*m;
00208   int w = m + pw + (tw!=0 && pw!=0 ?m:0) + tw + m + 22;
00209 
00210   QSize size;
00211   
00212   if(m_orientation == Qt::Vertical) {
00213     size = QSize(w, h).expandedTo(QApplication::globalStrut());
00214   } else {
00215     size = QSize(h, w).expandedTo(QApplication::globalStrut());
00216   }
00217   
00218   return size;
00219 }
00220 
00221 void DragBarButton::mousePressEvent( QMouseEvent* ev )
00222 {
00223   m_bClose = false;
00224   QRect closeRect;
00225 
00226   if(m_orientation == Qt::Vertical) {
00227     closeRect.setRect(width() - 20, 0, m_pClosePix->width(), height());
00228   } else {
00229     closeRect.setRect(0, 20 - m_pClosePix->width(), width(), m_pClosePix->width());
00230   }
00231 
00232   if ( closeRect.contains(ev->pos())) {
00233     m_bClose = true;
00234     repaint();
00235     return;
00236   }
00237 
00238   m_bPressed = true;
00239   m_ppoint = ev->pos();
00240 }
00241 
00242 void DragBarButton::mouseReleaseEvent( QMouseEvent* ev )
00243 {
00244   if ( m_bClose ) {
00245     m_bClose = false;
00246     repaint();
00247     QRect closeRect;
00248 
00249     if(m_orientation == Qt::Vertical) {
00250       closeRect.setRect(width() - 20, 0, m_pClosePix->width(), height());
00251     } else {
00252       closeRect.setRect(0, 20 - m_pClosePix->width(), width(), m_pClosePix->width());
00253     }
00254 
00255     if ( closeRect.contains(ev->pos()))
00256     {
00257        kdDebug(43000) << "DragBarButton::mouseReleaseEvent() - Emitting closeRequest" << endl;
00258         emit closeRequired(this);
00259     }
00260     return;
00261   }
00262 
00263   m_bPressed = false;
00264   if (m_bDragged) {
00265     m_bDragged = false;
00266     emit finishDrag();
00267   } else {
00268     emit clicked();
00269   }
00270 }
00271 
00272 void DragBarButton::mouseMoveEvent( QMouseEvent* ev )
00273 {
00274   if (m_bPressed) {
00275     QPoint p = m_ppoint;
00276     p -= ev->pos();
00277     if (p.manhattanLength() > 2 && !m_bDragged ) {
00278       m_bDragged = true;
00279       emit beginDrag();
00280     }
00281   }
00282 }
00283 
00284 void DragBarButton::enterEvent( QEvent* ev )
00285 {
00286   QPushButton::enterEvent(ev);
00287   m_bMouseOn = true;
00288   repaint(false);
00289 }
00290 
00291 void DragBarButton::leaveEvent( QEvent* ev )
00292 {
00293   QPushButton::leaveEvent(ev);
00294   m_bMouseOn = false;
00295   repaint();
00296 }
00297 
00298 void DragBarButton::setOrientation(Orientation orientation)
00299 {
00300   m_orientation = orientation;
00301   
00302   if(m_orientation == Qt::Vertical) {
00303     setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed);
00304   } else {
00305     setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Minimum);
00306   }
00307 }
00308 
00309 #include "stencilbarbutton.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys