qwt_arrow_button.cpp 8.95 KB
Newer Older
pixhawk's avatar
pixhawk committed
1 2 3 4
/* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
 * Qwt Widget Library
 * Copyright (C) 1997   Josef Wilgen
 * Copyright (C) 2002   Uwe Rathmann
5
 *
pixhawk's avatar
pixhawk committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the Qwt License, Version 1.0
 *****************************************************************************/

#include <qpainter.h>
#include <qstyle.h>
#include <qevent.h>
#include "qwt_math.h"
#include "qwt_polygon.h"
#include "qwt_arrow_button.h"

static const int MaxNum = 3;
static const int Margin = 2;
static const int Spacing = 1;

class QwtArrowButton::PrivateData
{
public:
    int num;
    Qt::ArrowType arrowType;
};


#if QT_VERSION >= 0x040000
#include <qstyleoption.h>
static QStyleOptionButton styleOpt(const QwtArrowButton* btn)
{
    QStyleOptionButton option;
    option.init(btn);
    option.features = QStyleOptionButton::None;
    if (btn->isFlat())
        option.features |= QStyleOptionButton::Flat;
    if (btn->menu())
        option.features |= QStyleOptionButton::HasMenu;
    if (btn->autoDefault() || btn->isDefault())
        option.features |= QStyleOptionButton::AutoDefaultButton;
    if (btn->isDefault())
        option.features |= QStyleOptionButton::DefaultButton;
    if (btn->isDown())
        option.state |= QStyle::State_Sunken;
    if (!btn->isFlat() && !btn->isDown())
        option.state |= QStyle::State_Raised;

    return option;
}
#endif

/*!
  \param num Number of arrows
  \param arrowType see Qt::ArowType in the Qt docs.
  \param parent Parent widget
*/
58 59
QwtArrowButton::QwtArrowButton(int num,
                               Qt::ArrowType arrowType, QWidget *parent):
pixhawk's avatar
pixhawk committed
60 61 62 63 64 65 66 67 68
    QPushButton(parent)
{
    d_data = new PrivateData;
    d_data->num = qwtLim(num, 1, MaxNum);
    d_data->arrowType = arrowType;

    setAutoRepeat(true);
    setAutoDefault(false);

69 70 71 72 73 74 75 76 77
    switch(d_data->arrowType) {
    case Qt::LeftArrow:
    case Qt::RightArrow:
        setSizePolicy(QSizePolicy::Expanding,
                      QSizePolicy::Fixed);
        break;
    default:
        setSizePolicy(QSizePolicy::Fixed,
                      QSizePolicy::Expanding);
pixhawk's avatar
pixhawk committed
78 79 80 81 82 83 84 85 86 87 88 89 90
    }
}

//! Destructor
QwtArrowButton::~QwtArrowButton()
{
    delete d_data;
    d_data = NULL;
}

/*!
  \brief The direction of the arrows
*/
91 92 93
Qt::ArrowType QwtArrowButton::arrowType() const
{
    return d_data->arrowType;
pixhawk's avatar
pixhawk committed
94 95 96 97 98
}

/*!
  \brief The number of arrows
*/
99 100 101
int QwtArrowButton::num() const
{
    return d_data->num;
pixhawk's avatar
pixhawk committed
102 103 104 105 106 107 108 109 110 111
}

/*!
  \return the bounding rect for the label
*/
QRect QwtArrowButton::labelRect() const
{
    const int m = Margin;

    QRect r = rect();
112 113
    r.setRect(r.x() + m, r.y() + m,
              r.width() - 2 * m, r.height() - 2 * m);
pixhawk's avatar
pixhawk committed
114

115
    if ( isDown() ) {
pixhawk's avatar
pixhawk committed
116 117 118
        int ph, pv;
#if QT_VERSION < 0x040000
        ph = style().pixelMetric(
119
                 QStyle::PM_ButtonShiftHorizontal, this);
pixhawk's avatar
pixhawk committed
120
        pv = style().pixelMetric(
121
                 QStyle::PM_ButtonShiftVertical, this);
pixhawk's avatar
pixhawk committed
122 123 124 125
        r.moveBy(ph, pv);
#else
        QStyleOptionButton option = styleOpt(this);
        ph = style()->pixelMetric(
126
                 QStyle::PM_ButtonShiftHorizontal, &option, this);
pixhawk's avatar
pixhawk committed
127
        pv = style()->pixelMetric(
128
                 QStyle::PM_ButtonShiftVertical, &option, this);
pixhawk's avatar
pixhawk committed
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
        r.translate(ph, pv);
#endif
    }

    return r;
}

#if QT_VERSION >= 0x040000
/*!
   Paint event handler
   \param event Paint event
*/
void QwtArrowButton::paintEvent(QPaintEvent *event)
{
    QPushButton::paintEvent(event);
    QPainter painter(this);
    drawButtonLabel(&painter);
}
#endif

/*!
  \brief Draw the button label

  \param painter Painter
  \sa The Qt Manual on QPushButton
*/
void QwtArrowButton::drawButtonLabel(QPainter *painter)
{
    const bool isVertical = d_data->arrowType == Qt::UpArrow ||
158
                            d_data->arrowType == Qt::DownArrow;
pixhawk's avatar
pixhawk committed
159 160 161 162 163

    const QRect r = labelRect();
    QSize boundingSize = labelRect().size();
    if ( isVertical )
        boundingSize.transpose();
164 165

    const int w =
pixhawk's avatar
pixhawk committed
166 167
        (boundingSize.width() - (MaxNum - 1) * Spacing) / MaxNum;

168 169
    QSize arrow = arrowSize(Qt::RightArrow,
                            QSize(w, boundingSize.height()));
pixhawk's avatar
pixhawk committed
170 171 172 173 174

    if ( isVertical )
        arrow.transpose();

    QRect contentsSize; // aligned rect where to paint all arrows
175 176 177
    if ( d_data->arrowType == Qt::LeftArrow || d_data->arrowType == Qt::RightArrow ) {
        contentsSize.setWidth(d_data->num * arrow.width()
                              + (d_data->num - 1) * Spacing);
pixhawk's avatar
pixhawk committed
178
        contentsSize.setHeight(arrow.height());
179
    } else {
pixhawk's avatar
pixhawk committed
180
        contentsSize.setWidth(arrow.width());
181 182
        contentsSize.setHeight(d_data->num * arrow.height()
                               + (d_data->num - 1) * Spacing);
pixhawk's avatar
pixhawk committed
183 184 185 186 187 188 189
    }

    QRect arrowRect(contentsSize);
    arrowRect.moveCenter(r.center());
    arrowRect.setSize(arrow);

    painter->save();
190
    for (int i = 0; i < d_data->num; i++) {
pixhawk's avatar
pixhawk committed
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
        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();

209
    if ( hasFocus() ) {
pixhawk's avatar
pixhawk committed
210 211 212 213 214
#if QT_VERSION >= 0x040000
        QStyleOptionFocusRect option;
        option.init(this);
        option.backgroundColor = palette().color(QPalette::Background);

215 216
        style()->drawPrimitive(QStyle::PE_FrameFocusRect,
                               &option, painter, this);
pixhawk's avatar
pixhawk committed
217
#else
218
        const QRect focusRect =
pixhawk's avatar
pixhawk committed
219 220
            style().subRect(QStyle::SR_PushButtonFocusRect, this);
        style().drawPrimitive(QStyle::PE_FocusRect, painter,
221
                              focusRect, colorGroup());
pixhawk's avatar
pixhawk committed
222 223 224 225 226 227 228 229 230 231 232
#endif
    }
}

/*!
    Draw an arrow int a bounding rect

    \param painter Painter
    \param r Rectangle where to paint the arrow
    \param arrowType Arrow type
*/
233 234
void QwtArrowButton::drawArrow(QPainter *painter,
                               const QRect &r, Qt::ArrowType arrowType) const
pixhawk's avatar
pixhawk committed
235 236 237
{
    QwtPolygon pa(3);

238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
    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;
pixhawk's avatar
pixhawk committed
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
    }

    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();
}

/*!
  \return a size hint
*/
QSize QwtArrowButton::sizeHint() const
{
    return minimumSizeHint();
}

/*!
  \brief Return a minimum size hint
*/
QSize QwtArrowButton::minimumSizeHint() const
{
288
    const QSize asz = arrowSize(Qt::RightArrow, QSize());
pixhawk's avatar
pixhawk committed
289 290 291 292 293 294 295 296 297 298 299 300 301

    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);

302 303
    const QSize hsz = style()->sizeFromContents(QStyle::CT_PushButton,
                      &styleOption, sz, this);
pixhawk's avatar
pixhawk committed
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
#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;
}

/*!
   Calculate the size for a arrow that fits into a rect of a given size

   \param arrowType Arrow type
   \param boundingSize Bounding size
   \return Size of the arrow
*/
QSize QwtArrowButton::arrowSize(Qt::ArrowType arrowType,
324
                                const QSize &boundingSize) const
pixhawk's avatar
pixhawk committed
325 326 327 328
{
    QSize bs = boundingSize;
    if ( arrowType == Qt::UpArrow || arrowType == Qt::DownArrow )
        bs.transpose();
329

pixhawk's avatar
pixhawk committed
330 331
    const int MinLen = 2;
    const QSize sz = bs.expandedTo(
332
                         QSize(MinLen, 2 * MinLen - 1)); // minimum
pixhawk's avatar
pixhawk committed
333 334 335 336

    int w = sz.width();
    int h = 2 * w - 1;

337
    if ( h > sz.height() ) {
pixhawk's avatar
pixhawk committed
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
        h = sz.height();
        w = (h + 1) / 2;
    }

    QSize arrSize(w, h);
    if ( arrowType == Qt::UpArrow || arrowType == Qt::DownArrow )
        arrSize.transpose();

    return arrSize;
}

/*!
  \brief autoRepeat for the space keys
*/
void QwtArrowButton::keyPressEvent(QKeyEvent *e)
{
    if ( e->isAutoRepeat() && e->key() == Qt::Key_Space )
        emit clicked();

    QPushButton::keyPressEvent(e);
}