qwt_column_symbol.cpp 6.69 KB
Newer Older
Bryant's avatar
Bryant committed
1 2 3 4 5 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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 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 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 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 288 289 290 291 292 293
/* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
 * Qwt Widget Library
 * Copyright (C) 1997   Josef Wilgen
 * Copyright (C) 2002   Uwe Rathmann
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the Qwt License, Version 1.0
 *****************************************************************************/

#include "qwt_column_symbol.h"
#include "qwt_math.h"
#include "qwt_painter.h"
#include <qpainter.h>
#include <qpalette.h>

static void qwtDrawBox( QPainter *p, const QRectF &rect,
    const QPalette &pal, double lw )
{
    if ( lw > 0.0 )
    {
        if ( rect.width() == 0.0 )
        {
            p->setPen( pal.dark().color() );
            p->drawLine( rect.topLeft(), rect.bottomLeft() );
            return;
        }

        if ( rect.height() == 0.0 )
        {
            p->setPen( pal.dark().color() );
            p->drawLine( rect.topLeft(), rect.topRight() );
            return;
        }

        lw = qMin( lw, rect.height() / 2.0 - 1.0 );
        lw = qMin( lw, rect.width() / 2.0 - 1.0 );

        const QRectF outerRect = rect.adjusted( 0, 0, 1, 1 );
        QPolygonF polygon( outerRect );

        if ( outerRect.width() > 2 * lw &&
                outerRect.height() > 2 * lw )
        {
            const QRectF innerRect = outerRect.adjusted( lw, lw, -lw, -lw );
            polygon = polygon.subtracted( innerRect );
        }

        p->setPen( Qt::NoPen );

        p->setBrush( pal.dark() );
        p->drawPolygon( polygon );
    }

    const QRectF windowRect = rect.adjusted( lw, lw, -lw + 1, -lw + 1 );
    if ( windowRect.isValid() )
        p->fillRect( windowRect, pal.window() );
}

static void qwtDrawPanel( QPainter *painter, const QRectF &rect,
    const QPalette &pal, double lw )
{
    if ( lw > 0.0 )
    {
        if ( rect.width() == 0.0 )
        {
            painter->setPen( pal.window().color() );
            painter->drawLine( rect.topLeft(), rect.bottomLeft() );
            return;
        }

        if ( rect.height() == 0.0 )
        {
            painter->setPen( pal.window().color() );
            painter->drawLine( rect.topLeft(), rect.topRight() );
            return;
        }

        lw = qMin( lw, rect.height() / 2.0 - 1.0 );
        lw = qMin( lw, rect.width() / 2.0 - 1.0 );

        const QRectF outerRect = rect.adjusted( 0, 0, 1, 1 );
        const QRectF innerRect = outerRect.adjusted( lw, lw, -lw, -lw );

        QPolygonF lines[2];

        lines[0] += outerRect.bottomLeft();
        lines[0] += outerRect.topLeft();
        lines[0] += outerRect.topRight();
        lines[0] += innerRect.topRight();
        lines[0] += innerRect.topLeft();
        lines[0] += innerRect.bottomLeft();

        lines[1] += outerRect.topRight();
        lines[1] += outerRect.bottomRight();
        lines[1] += outerRect.bottomLeft();
        lines[1] += innerRect.bottomLeft();
        lines[1] += innerRect.bottomRight();
        lines[1] += innerRect.topRight();

        painter->setPen( Qt::NoPen );

        painter->setBrush( pal.light() );
        painter->drawPolygon( lines[0] );
        painter->setBrush( pal.dark() );
        painter->drawPolygon( lines[1] );
    }

    painter->fillRect( rect.adjusted( lw, lw, -lw + 1, -lw + 1 ), pal.window() );
}

class QwtColumnSymbol::PrivateData
{
public:
    PrivateData():
        style( QwtColumnSymbol::Box ),
        frameStyle( QwtColumnSymbol::Raised ),
        lineWidth( 2 )
    {
        palette = QPalette( Qt::gray );
    }

    QwtColumnSymbol::Style style;
    QwtColumnSymbol::FrameStyle frameStyle;

    QPalette palette;
    int lineWidth;
};

/*!
  Constructor

  \param style Style of the symbol
  \sa setStyle(), style(), Style
*/
QwtColumnSymbol::QwtColumnSymbol( Style style )
{
    d_data = new PrivateData();
    d_data->style = style;
}

//! Destructor
QwtColumnSymbol::~QwtColumnSymbol()
{
    delete d_data;
}

/*!
  Specify the symbol style

  \param style Style
  \sa style(), setPalette()
*/
void QwtColumnSymbol::setStyle( Style style )
{
    d_data->style = style;
}

/*!
  \return Current symbol style
  \sa setStyle()
*/
QwtColumnSymbol::Style QwtColumnSymbol::style() const
{
    return d_data->style;
}

/*!
  Assign a palette for the symbol

  \param palette Palette
  \sa palette(), setStyle()
*/
void QwtColumnSymbol::setPalette( const QPalette &palette )
{
    d_data->palette = palette;
}

/*!
  \return Current palette
  \sa setPalette()
*/
const QPalette& QwtColumnSymbol::palette() const
{
    return d_data->palette;
}

/*!
   Set the frame, that is used for the Box style.

   \param frameStyle Frame style
   \sa frameStyle(), setLineWidth(), setStyle()
*/
void QwtColumnSymbol::setFrameStyle( FrameStyle frameStyle )
{
    d_data->frameStyle = frameStyle;
}

/*!
  \return Current frame style, that is used for the Box style.
  \sa setFrameStyle(), lineWidth(), setStyle()
*/
QwtColumnSymbol::FrameStyle QwtColumnSymbol::frameStyle() const
{
    return d_data->frameStyle;
}

/*!
   Set the line width of the frame, that is used for the Box style.

   \param width Width
   \sa lineWidth(), setFrameStyle()
*/
void QwtColumnSymbol::setLineWidth( int width )
{
    if ( width < 0 )
        width = 0;

    d_data->lineWidth = width;
}

/*!
  \return Line width of the frame, that is used for the Box style.
  \sa setLineWidth(), frameStyle(), setStyle()
*/
int QwtColumnSymbol::lineWidth() const
{
    return d_data->lineWidth;
}

/*!
  Draw the symbol depending on its style.

  \param painter Painter
  \param rect Directed rectangle

  \sa drawBox()
*/
void QwtColumnSymbol::draw( QPainter *painter,
    const QwtColumnRect &rect ) const
{
    painter->save();

    switch ( d_data->style )
    {
        case QwtColumnSymbol::Box:
        {
            drawBox( painter, rect );
            break;
        }
        default:;
    }

    painter->restore();
}

/*!
  Draw the symbol when it is in Box style.

  \param painter Painter
  \param rect Directed rectangle

  \sa draw()
*/
void QwtColumnSymbol::drawBox( QPainter *painter,
    const QwtColumnRect &rect ) const
{
    QRectF r = rect.toRect();
    if ( QwtPainter::roundingAlignment( painter ) )
    {
        r.setLeft( qRound( r.left() ) );
        r.setRight( qRound( r.right() ) );
        r.setTop( qRound( r.top() ) );
        r.setBottom( qRound( r.bottom() ) );
    }

    switch ( d_data->frameStyle )
    {
        case QwtColumnSymbol::Raised:
        {
            qwtDrawPanel( painter, r, d_data->palette, d_data->lineWidth );
            break;
        }
        case QwtColumnSymbol::Plain:
        {
            qwtDrawBox( painter, r, d_data->palette, d_data->lineWidth );
            break;
        }
        default:
        {
            painter->fillRect( r, d_data->palette.window() );
        }
    }
}