Main Page | Class Hierarchy | Alphabetical List | Compound List | File List | Compound Members | File Members | Related Pages

qwt_scale.cpp

Go to the documentation of this file.
00001 /* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
00002  * Qwt Widget Library
00003  * Copyright (C) 1997   Josef Wilgen
00004  * Copyright (C) 2002   Uwe Rathmann
00005  *
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the Qwt License, Version 1.0
00008  *****************************************************************************/
00009 
00010 #include <qpainter.h>
00011 #include <qsimplerichtext.h>
00012 #include <qpixmap.h>
00013 #include "qwt_painter.h"
00014 #include "qwt_scale.h"
00015 #include "qwt_math.h"
00016 #include "qwt_paint_buffer.h"
00017 
00026 QwtScale::QwtScale(QwtScale::Position pos, QWidget *parent, const char *name):
00027     QWidget(parent,name, WRepaintNoErase|WResizeNoErase)
00028 {
00029     QwtScaleDraw::Orientation sdo;
00030     d_borderDist[0] = 0;
00031     d_borderDist[1] = 0;
00032     d_baseDist = 4;
00033     d_titleOffset = 0;
00034     d_titleDist = 2;
00035     d_titleAlign = AlignHCenter | WordBreak | ExpandTabs;
00036 
00037     switch(pos)
00038     {
00039         case QwtScale::Left:
00040             sdo = QwtScaleDraw::Left;
00041             break;
00042         case QwtScale::Right:
00043             sdo = QwtScaleDraw::Right;
00044             break;
00045         case QwtScale::Bottom:
00046             sdo = QwtScaleDraw::Bottom;
00047             break;
00048         case QwtScale::Top:
00049             sdo = QwtScaleDraw::Top;
00050             break;
00051         default:
00052             sdo = QwtScaleDraw::Left;
00053     }
00054 
00055     d_scaleDraw = new QwtScaleDraw;
00056     d_scaleDraw->setGeometry(0,0,10,sdo);
00057 }
00058 
00060 QwtScale::~QwtScale()
00061 {
00062     delete d_scaleDraw;
00063 }
00064 
00070 void QwtScale::setTitle(const QString &title)
00071 {
00072     if ( title != d_title )
00073     {
00074         d_title = title;
00075         layoutScale();
00076     }
00077 }
00078 
00084 void QwtScale::setPosition(Position pos)
00085 {
00086     QwtScaleDraw::Orientation sdo;
00087     switch(pos)
00088     {
00089         case QwtScale::Left:
00090             sdo = QwtScaleDraw::Left;
00091             break;
00092         case QwtScale::Right:
00093             sdo = QwtScaleDraw::Right;
00094             break;
00095         case QwtScale::Bottom:
00096             sdo = QwtScaleDraw::Bottom;
00097             break;
00098         case QwtScale::Top:
00099             sdo = QwtScaleDraw::Top;
00100             break;
00101         default:
00102             sdo = QwtScaleDraw::Left;
00103     }
00104     
00105     if (d_scaleDraw)
00106       d_scaleDraw->setGeometry(0,0,10,sdo);
00107     layoutScale();
00108 }
00109 
00110     
00115 QwtScale::Position QwtScale::position() const 
00116 {
00117   if (!scaleDraw())
00118     return Left;
00119   
00120   QwtScaleDraw::Orientation ori = scaleDraw()->orientation();
00121   switch(ori)
00122   {
00123     case QwtScaleDraw::Left:
00124         return QwtScale::Left;
00125     case QwtScaleDraw::Right:
00126         return QwtScale::Right;
00127     case QwtScaleDraw::Bottom:
00128         return QwtScale::Bottom;
00129     case QwtScaleDraw::Top:
00130         return QwtScale::Top;
00131     default:
00132         return QwtScale::Left;
00133   }    
00134 }
00135 
00144 void QwtScale::setBorderDist(int dist1, int dist2)
00145 {
00146     if ( dist1 != d_borderDist[0] || dist2 != d_borderDist[1] )
00147     {
00148         d_borderDist[0] = dist1;
00149         d_borderDist[1] = dist2;
00150         layoutScale();
00151     }
00152 }
00153 
00159 void QwtScale::setBaselineDist(int bd)
00160 {
00161     bd = qwtMax( 0, bd );
00162     if ( bd != d_baseDist )
00163     {
00164         d_baseDist = bd;
00165         layoutScale();
00166     }
00167 }
00168 
00174 void QwtScale::setTitleFont(const QFont &f)
00175 {
00176     if ( f != d_titleFont )
00177     {
00178         d_titleFont = f;
00179         layoutScale();
00180     }
00181 }
00182 
00188 void QwtScale::setTitleColor(const QColor &c)
00189 {
00190     d_titleColor = c;
00191     update();
00192 }
00193 
00202 void QwtScale::setTitleAlignment(int flags)
00203 {
00204     flags &= ~(AlignTop | AlignBottom);
00205     d_titleAlign = flags;
00206     layoutScale(); // SingleLine, ExpandTabs might affect layout
00207 }
00208 
00220 void QwtScale::setLabelFormat(char f, int prec, int fieldwidth)
00221 {
00222     d_scaleDraw->setLabelFormat(f, prec, fieldwidth);
00223     layoutScale();
00224 }
00225 
00233 void QwtScale::setLabelAlignment(int align)
00234 {
00235     d_scaleDraw->setLabelAlignment(align);
00236     layoutScale();
00237 }
00238 
00244 void QwtScale::setLabelRotation(double rotation)
00245 {
00246     d_scaleDraw->setLabelRotation(rotation);
00247     layoutScale();
00248 }
00249 
00255 void QwtScale::setScaleDraw(QwtScaleDraw *sd)
00256 {
00257     if ( sd == NULL || sd == d_scaleDraw )
00258         return;
00259 
00260   if ( d_scaleDraw )
00261     *sd = *d_scaleDraw;
00262 
00263     delete d_scaleDraw;
00264     d_scaleDraw = sd;
00265 
00266     layoutScale();
00267 }
00268 
00279 void QwtScale::labelFormat(char &f, int &prec, int &fieldwidth) const
00280 {
00281     d_scaleDraw->labelFormat(f, prec, fieldwidth);
00282 }
00283 
00288 const QwtScaleDraw *QwtScale::scaleDraw() const 
00289 { 
00290     return d_scaleDraw; 
00291 }
00292 
00297 int QwtScale::titleAlignment() const 
00298 { 
00299     return d_titleAlign; 
00300 }
00301 
00306 const QFont &QwtScale::titleFont() const 
00307 { 
00308     return d_titleFont; 
00309 }
00310 
00315 const QColor &QwtScale::titleColor() const 
00316 { 
00317     return d_titleColor; 
00318 }
00319 
00324 const QString& QwtScale::title() const 
00325 { 
00326     return d_title; 
00327 }
00328 
00333 int QwtScale::startBorderDist() const 
00334 { 
00335     return d_borderDist[0]; 
00336 }  
00337 
00342 int QwtScale::endBorderDist() const 
00343 { 
00344     return d_borderDist[1]; 
00345 }
00346 
00351 int QwtScale::baseLineDist() const 
00352 { 
00353     return d_baseDist; 
00354 }
00355 
00359 void QwtScale::paintEvent(QPaintEvent *e)
00360 {
00361     const QRect &ur = e->rect();
00362     if ( ur.isValid() )
00363     {
00364         QwtPaintBuffer paintBuffer(this, ur);
00365         draw(paintBuffer.painter());
00366     }
00367 }
00368 
00372 void QwtScale::draw(QPainter *p) const
00373 {
00374     d_scaleDraw->draw(p);
00375 
00376     p->setFont(d_titleFont);
00377     p->setPen(d_titleColor);
00378 
00379     QRect r = rect();
00380 
00381     switch(d_scaleDraw->orientation())
00382     {
00383         case QwtScaleDraw::Bottom:
00384             r.setTop( r.top() + d_titleOffset );
00385             break;
00386 
00387         case QwtScaleDraw::Left:
00388             r.setRight( r.right() - d_titleOffset );
00389             break;
00390 
00391         case QwtScaleDraw::Right:
00392             r.setLeft( r.left() + d_titleOffset );
00393             break;
00394 
00395         case QwtScaleDraw::Top:
00396         default:
00397             r.setBottom( r.bottom() - d_titleOffset );
00398             break;
00399     }
00400     drawTitle(p, d_scaleDraw->orientation(), r,
00401         d_titleAlign, d_title);
00402 }
00403 
00407 void QwtScale::resizeEvent(QResizeEvent *)
00408 {
00409     layoutScale(FALSE);
00410 }
00411 
00413 //  the current rect and fonts.
00414 //  \param update_geometry   notify the layout system and call update
00415 //         to redraw the scale
00416 
00417 void QwtScale::layoutScale( bool update_geometry )
00418 {
00419     QRect r = this->rect();
00420 
00421     // Borders cannot be less than the minBorderDist
00422 
00423     int bd0, bd1;
00424     minBorderDist(bd0, bd1);
00425     if ( d_borderDist[0] > bd0 )
00426         bd0 = d_borderDist[0];
00427     if ( d_borderDist[1] > bd1 )
00428         bd1 = d_borderDist[1];
00429 
00430   const QFontMetrics fm(font());
00431     switch(d_scaleDraw->orientation())
00432     {
00433     case QwtScaleDraw::Bottom:
00434         d_scaleDraw->setGeometry(r.x() + bd0, r.y() + d_baseDist,
00435             r.width() - bd0 - bd1, QwtScaleDraw::Bottom);
00436 
00437         d_titleOffset = d_titleDist + d_baseDist +
00438             d_scaleDraw->maxHeight(QPen(), fm);
00439         break;
00440 
00441     case QwtScaleDraw::Top:
00442             d_scaleDraw->setGeometry(r.x() + bd0, r.bottom() - d_baseDist,
00443                r.width() - bd0 - bd1, QwtScaleDraw::Top);
00444         d_titleOffset =  d_titleDist + d_baseDist +
00445                            d_scaleDraw->maxHeight(QPen(), fm);
00446         break;
00447 
00448     case QwtScaleDraw::Left:
00449         d_scaleDraw->setGeometry(r.right() - d_baseDist, r.y() + bd0,
00450                             r.height() - bd0 - bd1, QwtScaleDraw::Left);
00451         d_titleOffset = d_baseDist + d_titleDist +
00452                         d_scaleDraw->maxWidth(QPen(), fm);
00453         break;
00454 
00455     case QwtScaleDraw::Right:
00456         d_scaleDraw->setGeometry(r.x() + d_baseDist, r.y() + bd0,
00457                             r.height() - bd0 - bd1, QwtScaleDraw::Right);
00458         d_titleOffset = d_baseDist + d_titleDist +
00459                           d_scaleDraw->maxWidth(QPen(), fm);
00460         break;
00461     default:
00462         break;
00463     }
00464     if ( update_geometry )
00465     {
00466       updateGeometry();
00467       update();
00468     }
00469 }
00470 
00480 void QwtScale::drawTitle(QPainter *painter, QwtScaleDraw::Orientation o,
00481     const QRect &rect, int align, const QString &text)
00482 {
00483     QRect r;
00484     double angle;
00485 
00486     switch(o)
00487     {
00488         case QwtScaleDraw::Left:
00489             align |= AlignTop;
00490             angle = -90.0;
00491             r.setRect(rect.left(), rect.bottom(),
00492                 rect.height(), rect.width());
00493             break;
00494         case QwtScaleDraw::Right:
00495             align |= AlignTop;
00496             angle = 90.0;
00497             r.setRect(rect.left() + rect.width(), rect.top(),
00498                 rect.height(), rect.width());
00499             break;
00500         case QwtScaleDraw::Top:
00501             align |= AlignTop;
00502             angle = 0.0;
00503             r = rect;
00504             break;
00505         case QwtScaleDraw::Bottom:
00506         default:
00507             align |= AlignBottom;
00508             angle = 0.0;
00509             r = rect;
00510             break;
00511     }
00512 
00513     painter->save();
00514     painter->translate(r.x(), r.y());
00515     if (angle != 0.0)
00516       painter->rotate(angle);
00517     QwtPainter::drawText(painter, 0, 0, r.width(), r.height(), align, text);
00518     painter->restore();
00519 }
00520 
00528 void QwtScale::scaleChange()
00529 {
00530     layoutScale();
00531 }
00532 
00537 QSizePolicy QwtScale::sizePolicy() const
00538 {
00539     QSizePolicy sp;
00540     if ( d_scaleDraw->orientation() == QwtScaleDraw::Left ||
00541         d_scaleDraw->orientation() == QwtScaleDraw::Right )
00542     {
00543         sp.setHorData( QSizePolicy::Fixed );
00544         sp.setVerData( QSizePolicy::MinimumExpanding );
00545     }
00546     else
00547     {
00548         sp.setHorData( QSizePolicy::MinimumExpanding );
00549         sp.setVerData( QSizePolicy::Fixed );
00550     }
00551     return sp;
00552 }
00553 
00557 QSize QwtScale::sizeHint() const
00558 {
00559     return minimumSizeHint();
00560 }
00561 
00565 QSize QwtScale::minimumSizeHint() const
00566 {
00567     Qt::Orientation o = Qt::Horizontal;
00568     if ( d_scaleDraw->orientation() == QwtScaleDraw::Left ||
00569         d_scaleDraw->orientation() == QwtScaleDraw::Right )
00570     {
00571         o = Qt::Vertical;
00572     }
00573 
00574     // Border Distance cannot be less than the scale minBorderDist
00575     // Note, the minBorderDist is already included in minHeight/minWidth
00576     int length = 0;
00577     int mbd1, mbd2;
00578     minBorderDist(mbd1, mbd2);
00579     length += qwtMax( 0, d_borderDist[0] - mbd1 );
00580     length += qwtMax( 0, d_borderDist[1] - mbd2 );
00581 
00582   const QFontMetrics fm(font());
00583     if ( o == Qt::Vertical )
00584         length += d_scaleDraw->minHeight(QPen(), fm);
00585     else
00586         length += d_scaleDraw->minWidth(QPen(), fm);
00587 
00588   QSimpleRichText dummy(d_title, d_titleFont);
00589   dummy.setWidth(length);
00590   if ( dummy.widthUsed() > length )
00591     length = dummy.widthUsed();
00592 
00593     int dim = dimForLength(length, QFontMetrics(d_titleFont), fm);
00594     if ( length < dim )
00595     {
00596       // compensate for long titles
00597       length = dim;
00598       dim = dimForLength(length, QFontMetrics(d_titleFont), fm);
00599     }
00600 
00601     QSize size(length + 2, dim);
00602     if ( o == Qt::Vertical )
00603         size.transpose();
00604 
00605     return size;
00606 }
00607 
00618 int QwtScale::dimForLength(int length,
00619     const QFontMetrics &titleFontMetrics,
00620     const QFontMetrics &scaleFontMetrics) const
00621 {
00622     int dim = d_baseDist;
00623     if ( d_scaleDraw->orientation() == QwtScaleDraw::Left ||
00624         d_scaleDraw->orientation() == QwtScaleDraw::Right )
00625     {
00626         dim += d_scaleDraw->minWidth(QPen(), scaleFontMetrics);
00627     }
00628     else
00629         dim += d_scaleDraw->minHeight(QPen(), scaleFontMetrics);
00630 
00631     if ( !d_title.isEmpty() )
00632     {
00633         QRect titleRect = titleFontMetrics.boundingRect(0, 0, 
00634             length, QCOORD_MAX, d_titleAlign, d_title);
00635 
00636         dim += titleRect.height() + d_titleDist;
00637     }
00638     return dim;
00639 }
00640 
00651 void QwtScale::minBorderDist(int &start, int &end) const
00652 {
00653   const QFontMetrics fm(font());
00654     d_scaleDraw->minBorderDist(fm, start, end);
00655 }
00656 
00665 void QwtScale::fontChange(const QFont &oldFont)
00666 {
00667     QWidget::fontChange( oldFont );
00668     layoutScale();
00669 }
00670 
00679 void QwtScale::setScaleDiv(const QwtScaleDiv &sd)
00680 {
00681     if (d_scaleDraw->scaleDiv() != sd)
00682     {
00683         d_scaleDraw->setScale(sd);
00684         layoutScale();
00685     }
00686 }

Generated on Fri Nov 7 14:11:46 2003 for Qwt Developer's Guide by doxygen 1.3.2