00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include <qpainter.h>
00011 #include <qstyle.h>
00012 #include "qwt_math.h"
00013 #include "qwt_arrbtn.h"
00014
00015 #if 1
00016 static const int MaxNum = 3;
00017 static const int Margin = 2;
00018 static const int Spacing = 1;
00019 #endif
00020
00027 QwtArrowButton::QwtArrowButton(int num, Qt::ArrowType arrowType,
00028 QWidget *parent, const char *name):
00029 QPushButton (parent, name),
00030 d_num(qwtLim(num,1,MaxNum)),
00031 d_arrowType(arrowType)
00032 {
00033
00034
00035
00036
00037 setPixmap(QPixmap());
00038 setAutoRepeat(TRUE);
00039 setAutoDefault(FALSE);
00040 }
00041
00045 Qt::ArrowType QwtArrowButton::arrowType() const
00046 {
00047 return d_arrowType;
00048 }
00049
00053 int QwtArrowButton::num() const
00054 {
00055 return d_num;
00056 }
00057
00058 QRect QwtArrowButton::labelRect() const
00059 {
00060 QRect r =
00061 #if QT_VERSION < 300
00062 style().buttonRect(rect().x(), rect().y(),
00063 rect().width(), rect().height());
00064 #else
00065 style().subRect(QStyle::SR_PushButtonContents, this);
00066 #endif
00067
00068 r.setRect(r.x() + Margin, r.y() + Margin,
00069 r.width() - 2 * Margin, r.height() - 2 * Margin);
00070
00071 if ( isDown() )
00072 {
00073 int ph, pv;
00074 #if QT_VERSION < 300
00075 style().getButtonShift(ph, pv);
00076 #else
00077 ph = style().pixelMetric(
00078 QStyle::PM_ButtonShiftHorizontal, this);
00079 pv = style().pixelMetric(
00080 QStyle::PM_ButtonShiftVertical, this);
00081 #endif
00082 r.moveBy(ph, pv);
00083 }
00084
00085 return r;
00086 }
00087
00092 void QwtArrowButton::drawButtonLabel(QPainter *p)
00093 {
00094 const QRect r = labelRect();
00095
00096 QSize boundingSize = labelRect().size();
00097 if ( d_arrowType == Qt::UpArrow || d_arrowType == Qt::DownArrow )
00098 boundingSize.transpose();
00099
00100 const int w = (boundingSize.width() - (MaxNum - 1) * Spacing) / MaxNum;
00101
00102 QSize arrow = arrowSize(Qt::RightArrow,
00103 QSize(w, boundingSize.height()));
00104
00105 if ( d_arrowType == Qt::UpArrow || d_arrowType == Qt::DownArrow )
00106 arrow.transpose();
00107
00108 QRect contentsSize;
00109 if ( d_arrowType == Qt::LeftArrow || d_arrowType == Qt::RightArrow )
00110 {
00111 contentsSize.setWidth(d_num * arrow.width()
00112 + (d_num - 1) * Spacing);
00113 contentsSize.setHeight(arrow.height());
00114 }
00115 else
00116 {
00117 contentsSize.setWidth(arrow.width());
00118 contentsSize.setHeight(d_num * arrow.height()
00119 + (d_num - 1) * Spacing);
00120 }
00121
00122 QRect arrowRect(contentsSize);
00123 arrowRect.moveCenter(r.center());
00124 arrowRect.setSize(arrow);
00125
00126 p->save();
00127 for (int i = 0; i < d_num; i++)
00128 {
00129 drawArrow(p, arrowRect, d_arrowType);
00130
00131 if ( d_arrowType == Qt::LeftArrow || d_arrowType == Qt::RightArrow )
00132 arrowRect.moveBy(arrow.width() + Spacing, 0);
00133 else
00134 arrowRect.moveBy(0, arrow.height() + Spacing);
00135 }
00136 p->restore();
00137
00138 #if QT_VERSION >= 300
00139 if ( hasFocus() )
00140 {
00141 const QRect focusRect =
00142 style().subRect(QStyle::SR_PushButtonFocusRect, this);
00143 style().drawPrimitive(QStyle::PE_FocusRect, p,
00144 focusRect, colorGroup());
00145 }
00146 #endif
00147 }
00148
00149 void QwtArrowButton::drawArrow(QPainter *p,
00150 const QRect &r, Qt::ArrowType arrowType) const
00151 {
00152 QPointArray pa(3);
00153
00154 switch(arrowType)
00155 {
00156 case Qt::UpArrow:
00157 pa.setPoint(0, r.bottomLeft());
00158 pa.setPoint(1, r.bottomRight());
00159 pa.setPoint(2, r.center().x(), r.top());
00160 break;
00161 case Qt::DownArrow:
00162 pa.setPoint(0, r.topLeft());
00163 pa.setPoint(1, r.topRight());
00164 pa.setPoint(2, r.center().x(), r.bottom());
00165 break;
00166 case Qt::RightArrow:
00167 pa.setPoint(0, r.topLeft());
00168 pa.setPoint(1, r.bottomLeft());
00169 pa.setPoint(2, r.right(), r.center().y());
00170 break;
00171 case Qt::LeftArrow:
00172 pa.setPoint(0, r.topRight());
00173 pa.setPoint(1, r.bottomRight());
00174 pa.setPoint(2, r.left(), r.center().y());
00175 break;
00176 }
00177
00178 p->setPen(colorGroup().buttonText());
00179 p->setBrush(colorGroup().buttonText());
00180 p->drawPolygon(pa);
00181 }
00182
00183
00185 QSizePolicy QwtArrowButton::sizePolicy() const
00186 {
00187 QSizePolicy policy;
00188 if ( d_arrowType == Qt::LeftArrow || d_arrowType == Qt::RightArrow )
00189 policy = QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
00190 else
00191 policy = QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding);
00192
00193 return policy;
00194 }
00195
00199 QSize QwtArrowButton::sizeHint() const
00200 {
00201 return minimumSizeHint();
00202 }
00203
00207 QSize QwtArrowButton::minimumSizeHint() const
00208 {
00209 const QSize asz = arrowSize(Qt::RightArrow, QSize());
00210
00211 QSize sz(
00212 2 * Margin + (MaxNum - 1) * Spacing + MaxNum * asz.width(),
00213 2 * Margin + asz.height()
00214 );
00215
00216 if ( d_arrowType == Qt::UpArrow || d_arrowType == Qt::DownArrow )
00217 sz.transpose();
00218
00219 #if QT_VERSION < 300
00220 int bm = style().buttonMargin() - 1;
00221 sz += QSize(2 * bm, 2 * bm);
00222 #else
00223 sz = style().sizeFromContents(QStyle::CT_PushButton, this, sz);
00224 #endif
00225 return sz;
00226 }
00227
00228 QSize QwtArrowButton::arrowSize(Qt::ArrowType arrowType,
00229 const QSize &boundingSize) const
00230 {
00231 QSize bs = boundingSize;
00232 if ( arrowType == Qt::UpArrow || arrowType == Qt::DownArrow )
00233 bs.transpose();
00234
00235 const int MinLen = 2;
00236 const QSize sz = bs.expandedTo(
00237 QSize(MinLen, 2 * MinLen - 1));
00238
00239 int w = sz.width();
00240 int h = 2 * w - 1;
00241
00242 if ( h > sz.height() )
00243 {
00244 h = sz.height();
00245 w = (h + 1) / 2;
00246 }
00247
00248 QSize arrSize(w, h);
00249 if ( arrowType == Qt::UpArrow || arrowType == Qt::DownArrow )
00250 arrSize.transpose();
00251
00252 return arrSize;
00253 }
00254
00258 void QwtArrowButton::keyPressEvent(QKeyEvent *e)
00259 {
00260 if ( e->isAutoRepeat() && e->key() == Key_Space )
00261 emit clicked();
00262
00263 QPushButton::keyPressEvent(e);
00264 }