iisiconlabel.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *                                                                         *
00003  *   Copyright: http://www.ii-system.com                                   *
00004  *   License:   LGPL                                                       *
00005  *                                                                         *
00006  ***************************************************************************/
00007 
00008 #include "iisiconlabel.h"
00009 #include "iistaskpanelscheme.h"
00010 
00011 iisIconLabel::iisIconLabel(const QIcon &icon, const QString &title, QWidget *parent)
00012         : QWidget(parent),
00013         myPixmap(icon),
00014         myText(title),
00015         mySchemePointer(0),
00016         m_over(false),
00017         m_pressed(false),
00018         m_changeCursorOver(true),
00019         m_underlineOver(true)
00020 {
00021         setFocusPolicy(Qt::StrongFocus);
00022 
00023         myFont.setWeight(0);
00024         myPen.setStyle(Qt::NoPen);
00025 
00026         myColor = myColorOver = myColorDisabled = QColor();
00027 }
00028 
00029 iisIconLabel::~iisIconLabel()
00030 {
00031         if (m_changeCursorOver)
00032                 QApplication::restoreOverrideCursor();
00033 }
00034 
00035 void iisIconLabel::setSchemePointer(iisIconLabelScheme **pointer)
00036 {
00037         mySchemePointer = pointer;
00038         update();
00039 }
00040 
00041 void iisIconLabel::setColors(const QColor &color, const QColor &colorOver, const QColor &colorOff)
00042 {
00043         myColor = color;
00044         myColorOver = colorOver;
00045         myColorDisabled = colorOff;
00046         update();
00047 }
00048 
00049 void iisIconLabel::setFont(const QFont &font)
00050 {
00051         myFont = font;
00052         update();
00053 }
00054 
00055 void iisIconLabel::setFocusPen(const QPen &pen)
00056 {
00057         myPen = pen;
00058         update();
00059 }
00060 
00061 QSize iisIconLabel::sizeHint() const
00062 {
00063         return minimumSize();
00064 }
00065 
00066 QSize iisIconLabel::minimumSizeHint() const
00067 {
00068         int s = (mySchemePointer && *mySchemePointer) ? (*mySchemePointer)->iconSize : 16;
00069         QPixmap px = myPixmap.pixmap(s,s, 
00070                 isEnabled() ? QIcon::Normal     : QIcon::Disabled);
00071 
00072         int h = 4+px.height();
00073         int w = 8 + px.width();
00074         if (!myText.isEmpty()) {
00075                 QFontMetrics fm(myFont);
00076                 w += fm.width(myText);
00077                 h = qMax(h, 4+fm.height());
00078         }
00079 
00080         return QSize(w+2,h+2);
00081 }
00082 
00083 void iisIconLabel::paintEvent ( QPaintEvent * event ) 
00084 {
00085         QPainter p(this);
00086 
00087         QRect textRect(rect().adjusted(0,0,-1,0));
00088 
00089         int x = 2;
00090 
00091         if (!myPixmap.isNull()) {
00092                 int s = (mySchemePointer && *mySchemePointer) ? (*mySchemePointer)->iconSize : 16;
00093                 QPixmap px = myPixmap.pixmap(s,s,
00094                         isEnabled() ? QIcon::Normal     : QIcon::Disabled);
00095                 p.drawPixmap(x,0,px);
00096                 x += px.width() + 4;
00097         }
00098 
00099         if (!myText.isEmpty()) {
00100                 QColor text = myColor, textOver = myColorOver, textOff = myColorDisabled;
00101                 QFont fnt = myFont;
00102                 QPen focusPen = myPen;
00103                 bool underline = m_underlineOver/*, cursover = m_changeCursorOver*/;
00104                 if (mySchemePointer && *mySchemePointer) {
00105                         if (!text.isValid()) text = (*mySchemePointer)->text;
00106                         if (!textOver.isValid()) textOver = (*mySchemePointer)->textOver;
00107                         if (!textOff.isValid()) textOff = (*mySchemePointer)->textOff;
00108                         if (!fnt.weight()) fnt = (*mySchemePointer)->font;
00109                         if (focusPen.style() == Qt::NoPen) focusPen = (*mySchemePointer)->focusPen;
00110                         underline = (*mySchemePointer)->underlineOver;
00111                         //cursover = (*mySchemePointer)->cursorOver;
00112                 }
00113 
00114                 p.setPen(isEnabled() ? m_over ? textOver : text : textOff);
00115 
00116                 if (isEnabled() && underline && m_over)
00117                         fnt.setUnderline(true);
00118                 p.setFont(fnt);
00119 
00120                 textRect.setLeft(x);
00121                 QRect boundingRect;
00122 
00123                 QFontMetrics fm(fnt);
00124 #if QT_VERSION >= 0x040203
00125                 QString txt(fm.elidedText(myText, Qt::ElideRight, textRect.width()));
00126 #else
00127                 QString txt = myText;
00128 #endif
00129 
00130                 p.drawText(textRect, Qt::AlignLeft | Qt::AlignVCenter, txt, &boundingRect);
00131 
00132                 if (hasFocus()) {
00133                         p.setPen(focusPen);
00134                         p.drawRect(boundingRect.adjusted(-2,-1,0,0));
00135                 }
00136         }
00137 }
00138 
00139 
00140 void iisIconLabel::enterEvent ( QEvent * /*event*/ )
00141 {
00142         m_over = true;
00143 
00144         if (m_changeCursorOver)
00145                 QApplication::setOverrideCursor(Qt::PointingHandCursor);
00146 
00147         update();
00148 }
00149 
00150 void iisIconLabel::leaveEvent ( QEvent * /*event*/ )
00151 {
00152         m_over = false;
00153         update();
00154 
00155         if (m_changeCursorOver)
00156                 QApplication::restoreOverrideCursor();
00157 }
00158 
00159 void iisIconLabel::mousePressEvent ( QMouseEvent * event )
00160 {
00161         if (event->button() == Qt::LeftButton) {
00162                 m_pressed = true;
00163                 emit pressed();
00164         } else
00165                 if (event->button() == Qt::RightButton)
00166                         emit contextMenu();
00167 
00168         update();
00169 }
00170 
00171 void iisIconLabel::mouseReleaseEvent ( QMouseEvent * event )
00172 {
00173         if (event->button() == Qt::LeftButton) {
00174                 m_pressed = false;
00175                 emit released();
00176 
00177                 if (rect().contains( event->pos() )) {
00178                         emit clicked();
00179                         emit activated();
00180                 }
00181         }
00182 
00183         update();
00184 }
00185 
00186 void iisIconLabel::keyPressEvent ( QKeyEvent * event )
00187 {
00188         switch (event->key()) {
00189                 case Qt::Key_Space:
00190                 case Qt::Key_Return:
00191                         emit activated();
00192                         break;
00193 
00194                 default:;
00195         }
00196 
00197         QWidget::keyPressEvent(event);
00198 }
00199 

Generated on Wed Nov 23 19:00:18 2011 for FreeCAD by  doxygen 1.6.1