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

qwt_plot_canvas.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 <qstyle.h>
00012 #include "qwt_painter.h"
00013 #include "qwt_math.h"
00014 #include "qwt_plot.h"
00015 #include "qwt_paint_buffer.h"
00016 #include "qwt_plot_canvas.h"
00017 
00018 
00020 
00021 QwtPlotCanvas::QwtPlotCanvas(QwtPlot *plot):
00022     QFrame(plot, "canvas", WRepaintNoErase|WResizeNoErase),
00023     d_outlineEnabled(FALSE),
00024     d_outlineActive(FALSE),
00025     d_mousePressed(FALSE),
00026     d_outline(Qwt::Rect),
00027     d_pen(red)
00028 {
00029     setCursor(crossCursor);
00030 }
00031 
00033 void QwtPlotCanvas::frameChanged()
00034 {
00035     QFrame::frameChanged();
00036 
00037     // frame changes change the size of the contents rect, what
00038     // is related to the axes. So we have to update the layout.
00039 
00040     ((QwtPlot *)parent())->updateLayout();
00041 }
00042 
00044 void QwtPlotCanvas::drawContents(QPainter *painter)
00045 {
00046     QwtPaintBuffer paintBuffer(this, 
00047         painter->clipRegion().boundingRect(), painter);
00048 
00049     QPainter *bufferedPainter = paintBuffer.painter();
00050 
00051     bufferedPainter->save();
00052     ((QwtPlot *)parent())->drawCanvas(bufferedPainter);
00053     bufferedPainter->restore();
00054 
00055     if ( d_outlineActive )
00056         drawOutline(*bufferedPainter); // redraw outline
00057 
00058 
00059     if ( hasFocus() )
00060     {
00061         const QRect rect = contentsRect();
00062 
00063 #if QT_VERSION < 300
00064         style().drawFocusRect(bufferedPainter, rect, colorGroup());
00065 #else
00066         style().drawPrimitive(QStyle::PE_FocusRect, bufferedPainter,
00067             rect, colorGroup());
00068 #endif
00069     }
00070 }
00071 
00073 void QwtPlotCanvas::mousePressEvent(QMouseEvent *e)
00074 {
00075 
00076     if (d_outlineActive)
00077     {
00078         QPainter p(this);
00079         drawOutline(p); // Delete active outlines
00080     }
00081 
00082     d_outlineActive = FALSE;
00083 
00084     //
00085     // store this point as entry point
00086     //
00087     d_lastPoint = e->pos();
00088     d_entryPoint = e->pos();
00089 
00090     if (d_outlineEnabled)
00091     {
00092         QPainter p(this);
00093         drawOutline(p); // draw new outline
00094         d_outlineActive = TRUE;
00095     }
00096 
00097     d_mousePressed = TRUE;
00098 
00099     QMouseEvent m(QEvent::MouseButtonPress, 
00100         e->pos() - rect().topLeft(), e->button(), e->state());
00101 
00102     emit mousePressed(m);
00103 }
00104 
00106 void QwtPlotCanvas::mouseReleaseEvent(QMouseEvent *e)
00107 {
00108     if (d_outlineActive)
00109     {
00110         QPainter p(this);
00111         drawOutline(p);
00112     }
00113 
00114     d_outlineActive = FALSE;
00115     d_mousePressed = FALSE;
00116 
00117     QMouseEvent m(QEvent::MouseButtonRelease, 
00118         e->pos() - rect().topLeft(), e->button(), e->state());
00119 
00120     emit mouseReleased(m);
00121 }
00122 
00124 void QwtPlotCanvas::mouseMoveEvent(QMouseEvent *e)
00125 {
00126     if (d_outlineActive)
00127     {
00128         QPainter p(this);
00129         drawOutline(p);
00130         d_lastPoint = e->pos();
00131         drawOutline(p);
00132     }
00133 
00134     QMouseEvent m(QEvent::MouseMove, 
00135         e->pos() - rect().topLeft(), e->button(), e->state());
00136 
00137     emit mouseMoved(m);
00138 }
00139 
00151 void QwtPlotCanvas::enableOutline(bool tf)
00152 {
00153 
00154     //
00155     //  If the mouse is pressed, erase existing outline
00156     //  or draw new outline if 'tf' changes the 'enabled' state.
00157     //
00158     if ((tf != d_outlineEnabled) && d_mousePressed)
00159     {
00160         QPainter p(this);
00161         drawOutline(p);
00162         d_outlineActive = tf;
00163     }
00164     d_outlineEnabled = tf;
00165 }
00166 
00172 bool QwtPlotCanvas::outlineEnabled() const 
00173 { 
00174     return d_outlineEnabled; 
00175 }
00176 
00208 void QwtPlotCanvas::setOutlineStyle(Qwt::Shape os)
00209 {
00210     if (d_outlineActive)
00211     {
00212         QPainter p(this); // erase old outline
00213         drawOutline(p);
00214     }
00215 
00216     d_outline = os;
00217 
00218     if (d_outlineActive)
00219     {
00220         QPainter p(this);
00221         drawOutline(p); // draw new outline
00222     }
00223 }
00224 
00229 Qwt::Shape QwtPlotCanvas::outlineStyle() const 
00230 { 
00231     return d_outline; 
00232 }
00233 
00240 void QwtPlotCanvas::setOutlinePen(const QPen &pen)
00241 {
00242     d_pen = pen;
00243 }
00244 
00250 const QPen& QwtPlotCanvas::outlinePen() const 
00251 { 
00252     return d_pen; 
00253 }
00254 
00258 void QwtPlotCanvas::drawOutline(QPainter &p)
00259 {
00260     const QRect &r = contentsRect();
00261 
00262     QColor bg = ((QwtPlot *)parent())->canvasBackground();
00263 
00264     QPen pn = d_pen;
00265     pn.setColor(QColor(0, (bg.pixel() ^ d_pen.color().pixel())));
00266 
00267     p.setPen(pn);
00268     p.setRasterOp(XorROP);
00269     p.setClipRect(r);
00270     p.setClipping(TRUE);
00271 
00272     switch(d_outline)
00273     {
00274         case Qwt::VLine:
00275             QwtPainter::drawLine(&p, d_lastPoint.x(), 
00276                 r.top(), d_lastPoint.x(), r.bottom());
00277             break;
00278         
00279         case Qwt::HLine:
00280             QwtPainter::drawLine(&p, r.left(), 
00281                 d_lastPoint.y(), r.right(), d_lastPoint.y());
00282             break;
00283         
00284         case Qwt::Cross:
00285             QwtPainter::drawLine(&p, r.left(), 
00286                 d_lastPoint.y(), r.right(), d_lastPoint.y());
00287             QwtPainter::drawLine(&p, d_lastPoint.x(), 
00288                 r.top(), d_lastPoint.x(), r.bottom());
00289             break;
00290 
00291         case Qwt::Rect:
00292             QwtPainter::drawRect(&p, d_entryPoint.x(), d_entryPoint.y(),
00293                d_lastPoint.x() - d_entryPoint.x() + 1,
00294                d_lastPoint.y() - d_entryPoint.y() + 1);
00295             break;
00296         
00297         case Qwt::Ellipse:
00298             p.drawEllipse(d_entryPoint.x(), d_entryPoint.y(),
00299                d_lastPoint.x() - d_entryPoint.x() + 1,
00300                d_lastPoint.y() - d_entryPoint.y() + 1);
00301             break;
00302 
00303         default:
00304             break;
00305     }
00306 }

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