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