Arrow Button. More...
#include <qwt_arrow_button.h>
Classes | |
class | PrivateData |
Public Member Functions | |
QwtArrowButton (int num, Qt::ArrowType, QWidget *parent=NULL) | |
virtual | ~QwtArrowButton () |
Destructor. | |
Qt::ArrowType | arrowType () const |
The direction of the arrows. | |
int | num () const |
The number of arrows. | |
virtual QSize | sizeHint () const |
virtual QSize | minimumSizeHint () const |
Return a minimum size hint. | |
Protected Member Functions | |
virtual void | drawButtonLabel (QPainter *p) |
Draw the button label. | |
virtual void | drawArrow (QPainter *, const QRect &, Qt::ArrowType) const |
virtual QRect | labelRect () const |
virtual QSize | arrowSize (Qt::ArrowType, const QSize &boundingSize) const |
virtual void | keyPressEvent (QKeyEvent *) |
autoRepeat for the space keys |
Arrow Button.
A push button with one or more filled triangles on its front. An Arrow button can have 1 to 3 arrows in a row, pointing up, down, left or right.
Definition at line 23 of file qwt_arrow_button.h.
QwtArrowButton::QwtArrowButton | ( | int | num, |
Qt::ArrowType | arrowType, | ||
QWidget * | parent = NULL |
||
) | [explicit] |
num | Number of arrows |
arrowType | see Qt::ArowType in the Qt docs. |
parent | Parent widget |
Definition at line 58 of file qwt_arrow_button.cpp.
References arrowType(), QwtArrowButton::PrivateData::arrowType, MaxNum, QwtArrowButton::PrivateData::num, and qwtLim().
: QPushButton(parent) { d_data = new PrivateData; d_data->num = qwtLim(num, 1, MaxNum); d_data->arrowType = arrowType; setAutoRepeat(true); setAutoDefault(false); switch(d_data->arrowType) { case Qt::LeftArrow: case Qt::RightArrow: setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed); break; default: setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding); } }
QwtArrowButton::~QwtArrowButton | ( | ) | [virtual] |
QSize QwtArrowButton::arrowSize | ( | Qt::ArrowType | arrowType, |
const QSize & | boundingSize | ||
) | const [protected, virtual] |
Calculate the size for a arrow that fits into a rect of a given size
arrowType | Arrow type |
boundingSize | Bounding size |
Definition at line 331 of file qwt_arrow_button.cpp.
Referenced by drawButtonLabel(), and minimumSizeHint().
{ QSize bs = boundingSize; if ( arrowType == Qt::UpArrow || arrowType == Qt::DownArrow ) bs.transpose(); const int MinLen = 2; const QSize sz = bs.expandedTo( QSize(MinLen, 2 * MinLen - 1)); // minimum int w = sz.width(); int h = 2 * w - 1; if ( h > sz.height() ) { h = sz.height(); w = (h + 1) / 2; } QSize arrSize(w, h); if ( arrowType == Qt::UpArrow || arrowType == Qt::DownArrow ) arrSize.transpose(); return arrSize; }
Qt::ArrowType QwtArrowButton::arrowType | ( | ) | const |
The direction of the arrows.
Definition at line 92 of file qwt_arrow_button.cpp.
References QwtArrowButton::PrivateData::arrowType.
Referenced by QwtArrowButton().
{ return d_data->arrowType; }
void QwtArrowButton::drawArrow | ( | QPainter * | painter, |
const QRect & | r, | ||
Qt::ArrowType | arrowType | ||
) | const [protected, virtual] |
Draw an arrow int a bounding rect
painter | Painter |
r | Rectangle where to paint the arrow |
arrowType | Arrow type |
Definition at line 240 of file qwt_arrow_button.cpp.
Referenced by drawButtonLabel().
{ QwtPolygon pa(3); switch(arrowType) { case Qt::UpArrow: pa.setPoint(0, r.bottomLeft()); pa.setPoint(1, r.bottomRight()); pa.setPoint(2, r.center().x(), r.top()); break; case Qt::DownArrow: pa.setPoint(0, r.topLeft()); pa.setPoint(1, r.topRight()); pa.setPoint(2, r.center().x(), r.bottom()); break; case Qt::RightArrow: pa.setPoint(0, r.topLeft()); pa.setPoint(1, r.bottomLeft()); pa.setPoint(2, r.right(), r.center().y()); break; case Qt::LeftArrow: pa.setPoint(0, r.topRight()); pa.setPoint(1, r.bottomRight()); pa.setPoint(2, r.left(), r.center().y()); break; default: break; } painter->save(); #if QT_VERSION < 0x040000 painter->setPen(colorGroup().buttonText()); painter->setBrush(colorGroup().brush(QColorGroup::ButtonText)); #else painter->setPen(palette().color(QPalette::ButtonText)); painter->setBrush(palette().brush(QPalette::ButtonText)); #endif painter->drawPolygon(pa); painter->restore(); }
void QwtArrowButton::drawButtonLabel | ( | QPainter * | painter ) | [protected, virtual] |
Draw the button label.
painter | Painter |
Definition at line 157 of file qwt_arrow_button.cpp.
References arrowSize(), QwtArrowButton::PrivateData::arrowType, drawArrow(), labelRect(), MaxNum, QwtArrowButton::PrivateData::num, and Spacing.
{ const bool isVertical = d_data->arrowType == Qt::UpArrow || d_data->arrowType == Qt::DownArrow; const QRect r = labelRect(); QSize boundingSize = labelRect().size(); if ( isVertical ) boundingSize.transpose(); const int w = (boundingSize.width() - (MaxNum - 1) * Spacing) / MaxNum; QSize arrow = arrowSize(Qt::RightArrow, QSize(w, boundingSize.height())); if ( isVertical ) arrow.transpose(); QRect contentsSize; // aligned rect where to paint all arrows if ( d_data->arrowType == Qt::LeftArrow || d_data->arrowType == Qt::RightArrow ) { contentsSize.setWidth(d_data->num * arrow.width() + (d_data->num - 1) * Spacing); contentsSize.setHeight(arrow.height()); } else { contentsSize.setWidth(arrow.width()); contentsSize.setHeight(d_data->num * arrow.height() + (d_data->num - 1) * Spacing); } QRect arrowRect(contentsSize); arrowRect.moveCenter(r.center()); arrowRect.setSize(arrow); painter->save(); for (int i = 0; i < d_data->num; i++) { drawArrow(painter, arrowRect, d_data->arrowType); int dx = 0; int dy = 0; if ( isVertical ) dy = arrow.height() + Spacing; else dx = arrow.width() + Spacing; #if QT_VERSION >= 0x040000 arrowRect.translate(dx, dy); #else arrowRect.moveBy(dx, dy); #endif } painter->restore(); if ( hasFocus() ) { #if QT_VERSION >= 0x040000 QStyleOptionFocusRect option; option.init(this); option.backgroundColor = palette().color(QPalette::Background); style()->drawPrimitive(QStyle::PE_FrameFocusRect, &option, painter, this); #else const QRect focusRect = style().subRect(QStyle::SR_PushButtonFocusRect, this); style().drawPrimitive(QStyle::PE_FocusRect, painter, focusRect, colorGroup()); #endif } }
void QwtArrowButton::keyPressEvent | ( | QKeyEvent * | e ) | [protected, virtual] |
autoRepeat for the space keys
Definition at line 361 of file qwt_arrow_button.cpp.
References mitk::Key_Space.
{ if ( e->isAutoRepeat() && e->key() == Qt::Key_Space ) emit clicked(); QPushButton::keyPressEvent(e); }
QRect QwtArrowButton::labelRect | ( | ) | const [protected, virtual] |
Definition at line 108 of file qwt_arrow_button.cpp.
References Margin.
Referenced by drawButtonLabel().
{ const int m = Margin; QRect r = rect(); r.setRect(r.x() + m, r.y() + m, r.width() - 2 * m, r.height() - 2 * m); if ( isDown() ) { int ph, pv; #if QT_VERSION < 0x040000 ph = style().pixelMetric( QStyle::PM_ButtonShiftHorizontal, this); pv = style().pixelMetric( QStyle::PM_ButtonShiftVertical, this); r.moveBy(ph, pv); #else QStyleOptionButton option = styleOpt(this); ph = style()->pixelMetric( QStyle::PM_ButtonShiftHorizontal, &option, this); pv = style()->pixelMetric( QStyle::PM_ButtonShiftVertical, &option, this); r.translate(ph, pv); #endif } return r; }
QSize QwtArrowButton::minimumSizeHint | ( | ) | const [virtual] |
Return a minimum size hint.
Definition at line 294 of file qwt_arrow_button.cpp.
References arrowSize(), QwtArrowButton::PrivateData::arrowType, Margin, MaxNum, and Spacing.
Referenced by sizeHint().
{ const QSize asz = arrowSize(Qt::RightArrow, QSize()); QSize sz( 2 * Margin + (MaxNum - 1) * Spacing + MaxNum * asz.width(), 2 * Margin + asz.height() ); if ( d_data->arrowType == Qt::UpArrow || d_data->arrowType == Qt::DownArrow ) sz.transpose(); #if QT_VERSION >= 0x040000 QStyleOption styleOption; styleOption.init(this); const QSize hsz = style()->sizeFromContents(QStyle::CT_PushButton, &styleOption, sz, this); #if QT_VERSION < 0x040300 if ( hsz.width() != 80 ) // avoid a bug in the Cleanlooks style #endif sz = hsz; #else sz = style().sizeFromContents(QStyle::CT_PushButton, this, sz); #endif return sz; }
int QwtArrowButton::num | ( | ) | const |
The number of arrows.
Definition at line 100 of file qwt_arrow_button.cpp.
References QwtArrowButton::PrivateData::num.
{ return d_data->num; }
QSize QwtArrowButton::sizeHint | ( | ) | const [virtual] |
Definition at line 286 of file qwt_arrow_button.cpp.
References minimumSizeHint().
{ return minimumSizeHint(); }