Commit 13650926 authored by David Saxton's avatar David Saxton

Tidying of source (using enumerations, moving duplicated code into separate

functions, fixing abuse of function parameters, access XParser via static
function and giving some member variables better names).

svn path=/trunk/KDE/kdeedu/kmplot/; revision=528344
parent f20c5622
......@@ -119,3 +119,7 @@ kmplot/kmplotprogress.cpp
kmplot/kmplotprogress.h
kmplot/equationedit.cpp
kmplot/equationedit.h
/plotter.h
/plotter.cpp
kmplot/plotter.cpp
kmplot/plotter.h
......@@ -57,13 +57,25 @@
#include "settingspagefonts.h"
#include "ksliderwindow.h"
#include <assert.h>
class XParser;
class KmPlotIO;
bool MainDlg::oldfileversion;
MainDlg * MainDlg::m_self = 0;
MainDlg::MainDlg(QWidget *parentWidget, QObject *parent ) : DCOPObject( "MainDlg" ), KParts::ReadOnlyPart( parent ), m_recentFiles( 0 ), m_modified(false), m_parent(parentWidget)
MainDlg::MainDlg(QWidget *parentWidget, QObject *parent ) :
DCOPObject( "MainDlg" ),
KParts::ReadOnlyPart( parent ),
m_recentFiles( 0 ),
m_modified(false),
m_parent(parentWidget)
{
assert( !m_self ); // this class should only be constructed once
m_self = this;
// we need an instance
setInstance( KmPlotPartFactory::instance() );
......@@ -83,7 +95,7 @@ MainDlg::MainDlg(QWidget *parentWidget, QObject *parent ) : DCOPObject( "MainDl
coordsDialog = 0;
m_popupmenu = new KMenu( parentWidget );
m_newPlotMenu = new KMenu( parentWidget );
(void) new View( m_readonly, m_modified, m_popupmenu, parentWidget, actionCollection(), this );
(void) new View( m_readonly, m_modified, m_popupmenu, parentWidget, actionCollection() );
connect( View::self(), SIGNAL( setStatusBarText(const QString &)), this, SLOT( setReadOnlyStatusBarText(const QString &) ) );
if ( !m_readonly )
......@@ -97,7 +109,7 @@ MainDlg::MainDlg(QWidget *parentWidget, QObject *parent ) : DCOPObject( "MainDl
minmaxdlg = new KMinMax(m_parent);
View::self()->setMinMaxDlg(minmaxdlg);
setupActions();
View::self()->parser()->constants()->load();
XParser::self()->constants()->load();
kmplotio = new KmPlotIO();
m_config = KGlobal::config();
m_recentFiles->loadEntries( m_config );
......@@ -134,7 +146,7 @@ MainDlg::MainDlg(QWidget *parentWidget, QObject *parent ) : DCOPObject( "MainDl
MainDlg::~MainDlg()
{
m_recentFiles->saveEntries( m_config );
View::self()->parser()->constants()->save();
XParser::self()->constants()->save();
delete kmplotio;
}
......@@ -425,7 +437,7 @@ void MainDlg::slotExport()
if( url.fileName().right(4).toLower()==".svg")
{
QPicture pic;
View::self()->draw(&pic, 2);
View::self()->draw(&pic, View::SVG);
if (url.isLocalFile() )
pic.save( url.path(), "SVG");
else
......@@ -441,7 +453,7 @@ void MainDlg::slotExport()
else if( url.fileName().right(4).toLower()==".bmp")
{
QPixmap pic(100, 100);
View::self()->draw(&pic, 3);
View::self()->draw(&pic, View::Pixmap);
if (url.isLocalFile() )
pic.save( url.path(), "BMP");
else
......@@ -457,7 +469,7 @@ void MainDlg::slotExport()
else if( url.fileName().right(4).toLower()==".png")
{
QPixmap pic(100, 100);
View::self()->draw(&pic, 3);
View::self()->draw(&pic, View::Pixmap);
if (url.isLocalFile() )
pic.save( url.path(), "PNG");
else
......@@ -524,7 +536,7 @@ void MainDlg::slotPrint()
if ( prt.setup( m_parent, i18n( "Print Plot" ) ) )
{
prt.setFullPage( true );
View::self()->draw(&prt, 1);
View::self()->draw(&prt, View::Printer);
}
}
......@@ -533,7 +545,7 @@ void MainDlg::editAxes()
// create a config dialog and add a axes page
if ( !coordsDialog)
{
coordsDialog = new CoordsConfigDialog( View::self()->parser(), m_parent);
coordsDialog = new CoordsConfigDialog( XParser::self(), m_parent);
// User edited the configuration - update your local copies of the
// configuration data
connect( coordsDialog, SIGNAL( settingsChanged(const QString &) ), this, SLOT(updateSettings() ) );
......@@ -606,25 +618,25 @@ void MainDlg::updateSettings()
void MainDlg::getYValue()
{
minmaxdlg->init(2);
minmaxdlg->init( KMinMax::CalculateY );
minmaxdlg->show();
}
void MainDlg::findMinimumValue()
{
minmaxdlg->init(0);
minmaxdlg->init( KMinMax::FindMinimum );
minmaxdlg->show();
}
void MainDlg::findMaximumValue()
{
minmaxdlg->init(1);
minmaxdlg->init( KMinMax::FindMaximum );
minmaxdlg->show();
}
void MainDlg::graphArea()
{
minmaxdlg->init(3);
minmaxdlg->init( KMinMax::CalculateArea );
minmaxdlg->show();
}
......
......@@ -82,12 +82,16 @@ class MainDlg : public KParts::ReadOnlyPart, virtual public MainDlgIface
{
Q_OBJECT
public:
/** Constuctor.
* @param parentWidget parent widget for this part
* @param parent parent object
*/
MainDlg(QWidget *parentWidget, QObject *parent );
public:
/** Constuctor.
* @param parentWidget parent widget for this part
* @param parent parent object
*/
MainDlg(QWidget *parentWidget, QObject *parent );
/// Initialized as a pointer to this MainDlg object on creation
static MainDlg * self() { return m_self; }
/// Cleaning up a bit.
virtual ~MainDlg();
/// This class needs access to private members, too.
......@@ -202,6 +206,9 @@ private:
KAction * m_undoAction;
/// The redo action
KAction * m_redoAction;
/// A pointer to ourself
static MainDlg * m_self;
protected slots:
/**
......
This diff is collapsed.
......@@ -35,7 +35,6 @@
#include <QResizeEvent>
#include <QKeyEvent>
#include <QEvent>
#include <QPaintEvent>
// KDE includes
#include <dcopclient.h>
......@@ -55,8 +54,25 @@
class KMinMax;
class KSliderWindow;
class MainDlg;
class XParser;
class QPaintEvent;
class QTime;
class XParser;
/**
* For drawing the area of a (Cartesian) plot.
*/
class IntegralDrawSettings
{
public:
IntegralDrawSettings();
int functionID;
Function::PMode pMode;
double dmin, dmax;
QString parameter; ///< parameter for the function
};
/**
* @short This class contains the plots.
......@@ -67,37 +83,47 @@ class QTime;
class View : public QWidget, virtual public ViewIface
{
Q_OBJECT
public:
/// Contructor sets up the parser, too.
View(bool, bool &, KMenu *, QWidget* parent, KActionCollection *ac, MainDlg * mainDlg );
void setMinMaxDlg(KMinMax *);
virtual ~View();
public:
/// Contructor sets up the parser, too.
View( bool readOnly, bool & modified, KMenu * functionPopup, QWidget* parent, KActionCollection *ac );
void setMinMaxDlg(KMinMax *);
virtual ~View();
/// There is only one view.
static View * self() { return m_self; }
/// There is only one view.
static View * self() { return m_self; }
/// Reimplemented to draw all stuff to the view.
void draw(QPaintDevice *, int const);
/// Getting all relevant settings using KConfig XT class Settings.
void getSettings();
/// Clears all functions in the parser and gets default settings.
/// @see getSettings
void init();
/// Finding the minimum or maximum value
void findMinMaxValue(Function *, Function::PMode, bool, double &, double &,const QString &);
/// get a y-value from a x-value
void getYValue(Function * , Function::PMode, double , double &,const QString &);
/// draw and calculate the area between the graph and the x-axis.
void areaUnderGraph(Function *, Function::PMode, double &, double &, const QString &, QPainter* );
enum PlotMedium
{
Screen,
Printer,
SVG,
Pixmap,
};
/// Reimplemented to draw all stuff to the view.
void draw( QPaintDevice *, PlotMedium medium );
/// Getting all relevant settings using KConfig XT class Settings.
void getSettings();
/// Clears all functions in the parser and gets default settings.
/// @see getSettings
void init();
enum ExtremaType { Minimum, Maximum };
/**
* Finding the minimum or maximum value.
* \return The (x,y) coordinates of the extrema point.
*/
QPointF findMinMaxValue( Function * function, Function::PMode p_mode, ExtremaType type, double dmin, double dmax,const QString & parameter );
/// get a y-value from a x-value
double getYValue( Function * function, Function::PMode p_mode, double x, double y, const QString & parameter );
/**
* Calculates the area between the given plot and the x-axis
* (from x = \p dmin to x = \p dmax). The area will also be colored in.
* \return the area.
*/
double areaUnderGraph( IntegralDrawSettings settings );
/// the calculation was cancelled by the user
bool isCalculationStopped();
/// Returns a pointer to the private parser instance m_parser.
/// @see m_parser
XParser* parser();
/// Returns a pointer to the MainDlg
MainDlg * mainDlg() const { return m_mainDlg; }
/// Slider controlling parameter values
QPointer<KSliderWindow> m_sliderWindow;
/// Menu actions for the sliders
......@@ -107,13 +133,14 @@ public:
/// Convert a width in mm to a suitable QPen width for drawing
double mmToPenWidth( double width_mm, bool antialias ) const;
/** Current plot range endge. */
static double xmin;
static double xmax;
/** Current plot range endge. */
double m_xmin;
double m_xmax;
/// trace mode stuff, must be accessible in KMinMax
int csmode, csparam;
Function::PMode cstype;
/// trace mode stuff, must be accessible in KMinMax
int m_currentFunctionID;
int m_currentFunctionParameter;
Function::PMode m_currentFunctionPlot;
public slots:
/// Called when the user want to cancel the drawing
......@@ -204,15 +231,15 @@ private:
*/
void getPlotUnderMouse();
/**
* Finds the closest point to \p real_x and \p real_y to the given function.
* Finds the closest point to \p pos to the given function.
* \return the parametization (angle or t) that gives the closest point.
*/
double getClosestPoint( double real_x, double real_y, Function * function, Function::PMode mode );
double getClosestPoint( const QPointF & pos, Function * function, Function::PMode mode );
/**
* Calculates the pixel distance from \p real_x and \p real_y to the display
* point of the given function at \p x.
* Calculates the pixel distance from \p pos to the display point of the
* given function at \p x.
*/
double pixelDistance( double real_x, double real_y, Function * function, Function::PMode mode, double x );
double pixelDistance( const QPointF & pos, Function * function, Function::PMode mode, double x );
/**
* @return a string for displaying the x or y coordinate in the statusbar.
* \param delta is the amount by which the value varies over one pixel in
......@@ -220,30 +247,37 @@ private:
* moving the cursor shows a nice change in the string.
*/
QString posToString( double x, double delta ) const;
/**
* Convenience function for calculating the value of \p eq using the
* given \p mode (which cannot be Function::Integral).
*/
double value( Equation * eq, Function::PMode mode, double x );
/// for areadrawing when printing
bool areaDraw;
Function * areaFunction;
Function::PMode areaPMode;
double areaMin, areaMax;
QString areaParameter;
/// The central parser instance.
/// @see parser()
XParser *m_parser;
int w, h;
float s;
/**
* Convenience function for calculating the value of \p eq using the
* given \p mode
*/
double value( Equation * eq, Function::PMode mode, double x );
/**
* \return the real position of the function (similar to calling
* value(), but returns both coordinates).
*/
QPointF realValue( Function * function, Function::PMode mode, double x );
/**
* \return an appropriate xmin value for the given function
* plotting.
*/
double getXmin( Function * function );
/**
* \return an appropriate xmax value for the given function for
* plotting.
*/
double getXmax( Function * function );
/// for areadrawing
IntegralDrawSettings m_integralDrawSettings;
bool m_drawIntegral;
QPointF m_crosshairPixelCoords;
float csxpos; ///< y-position of the cross hair (real coordinates)
float csypos; ///< x-position of the cross hair (real coordinates)
double m_width, m_height;
float m_scaler;
QPointF m_crosshairPixelCoords;
QPointF m_crosshairPosition; ///< in real coordinates
/// The t- or x- (angle) coordinate of the traced curve
double m_trace_x;
......@@ -251,8 +285,8 @@ private:
/// trace mode stuff
bool rootflg;
/// @return whether csxpos is in the range of the view or in the custom range for the given \p plot
bool csxposValid( Function * plot ) const;
/// @return whether cspos is in the range of the view or in the custom range for the given \p plot
bool crosshairPositionValid( Function * plot ) const;
CDiagr dgr; ///< Coordinate system
QPoint ref;
......@@ -262,7 +296,6 @@ private:
double tlgx, tlgy, drskalx, drskaly;
QString tlgxstr, tlgystr, drskalxstr, drskalystr;
double stepWidth; ///< Absolute step width
/** @name Plotrange
* There are 4 predefined plot ranges:
......@@ -283,12 +316,12 @@ private:
*/
void coordToMinMax( const int koord, const QString &minStr, const QString &maxStr,
double &min, double &max );
//@{
/** Current plot range endge. */
double ymin;
double ymax;
//@}
//@}
//@{
/** Current plot range endge. */
double m_ymin;
double m_ymax;
//@}
//@}
void setScaling();
/// represents the KPrinter option app-kmplot-printtable.
......@@ -348,7 +381,6 @@ private:
enum Cursor { CursorWait, CursorBlank, CursorArrow, CursorCross, CursorMagnify, CursorLessen, CursorMove };
Cursor m_prevCursor;
MainDlg * m_mainDlg;
static View * m_self;
};
......
......@@ -115,7 +115,15 @@ void CDiagr::Plot(QPainter* pDC)
}
double CDiagr::TransxToPixel( double x, bool clipToEdge ) // reale x-Koordinate
QPointF CDiagr::toPixel( const QPointF & real, ClipBehaviour clipBehaviour )
{
double x = xToPixel( real.x(), clipBehaviour );
double y = yToPixel( real.y(), clipBehaviour );
return QPointF( x, y );
}
double CDiagr::xToPixel( double x, ClipBehaviour clipBehaviour ) // reale x-Koordinate
{
double xi; // transformierte x-Koordinate
static double lastx; // vorherige x-Koordinate
......@@ -138,12 +146,12 @@ double CDiagr::TransxToPixel( double x, bool clipToEdge ) // reale x-Koordinate
xi=PlotArea.right();
}
else if ( (x<xmin) && clipToEdge )
else if ( (x<xmin) && (clipBehaviour == ClipAll) )
{
xclipflg=1;
xi=PlotArea.left();
}
else if ( (x>xmax) && clipToEdge )
else if ( (x>xmax) && (clipBehaviour == ClipAll) )
{
xclipflg=1;
xi=PlotArea.right();
......@@ -159,7 +167,7 @@ double CDiagr::TransxToPixel( double x, bool clipToEdge ) // reale x-Koordinate
}
double CDiagr::TransyToPixel( double y, bool clipToEdge ) // reale y-Koordinate
double CDiagr::yToPixel( double y, ClipBehaviour clipBehaviour ) // reale y-Koordinate
{
double yi; // transformierte y-Koordinate
static double lasty; // vorherige y-Koordinate
......@@ -183,12 +191,12 @@ double CDiagr::TransyToPixel( double y, bool clipToEdge ) // reale y-Koordinate
yi=PlotArea.top();
}
else if ( (y<ymin) && clipToEdge )
else if ( (y<ymin) && (clipBehaviour == ClipAll) )
{
yclipflg=1;
yi=PlotArea.bottom();
}
else if ( (y>ymax) && clipToEdge )
else if ( (y>ymax) && (clipBehaviour == ClipAll) )
{
yclipflg=1;
yi=PlotArea.top();
......@@ -204,12 +212,20 @@ double CDiagr::TransyToPixel( double y, bool clipToEdge ) // reale y-Koordinate
}
double CDiagr::TransxToReal(double x) // Bildschirmkoordinate
QPointF CDiagr::toReal( const QPointF & pixel )
{
double x = xToReal( pixel.x() );
double y = yToReal( pixel.y() );
return QPointF( x, y );
}
double CDiagr::xToReal(double x) // Bildschirmkoordinate
{ return (x-ox)/skx; // reale x-Koordinate
}
double CDiagr::TransyToReal(double y) // Bildschirmkoordinate
double CDiagr::yToReal(double y) // Bildschirmkoordinate
{ return (oy-y)/sky; // reale y-Koordinate
}
......@@ -223,7 +239,7 @@ void CDiagr::drawAxes( QPainter* pDC ) // draw axes
{
pDC->setPen( QPen( QColor(axesColor), View::self()->mmToPenWidth(axesLineWidth, true) ) );
a=PlotArea.right();
b=TransyToPixel(0.);
b=yToPixel(0.);
pDC->Lineh(PlotArea.left(), b, a); // x-Achse
if( Settings::showArrows()) // ARROWS
{ int const dx=40;
......@@ -232,7 +248,7 @@ void CDiagr::drawAxes( QPainter* pDC ) // draw axes
pDC->Line( QPointF( a, b ), QPointF( a-dx, b-dy) );
}
a=TransxToPixel(0.);
a=xToPixel(0.);
b=PlotArea.top();
pDC->Linev(a, PlotArea.bottom(), b); // y-Achse
if( Settings::showArrows() ) // ARROWS
......@@ -268,7 +284,7 @@ void CDiagr::drawAxes( QPainter* pDC ) // draw axes
while(d<xmd-ex/2.)
{
pDC->Linev(TransxToPixel(d), a, b);
pDC->Linev(xToPixel(d), a, b);
d+=ex;
}
......@@ -293,7 +309,7 @@ void CDiagr::drawAxes( QPainter* pDC ) // draw axes
while(d<ymd-ey/2.)
{
pDC->Lineh(a, TransyToPixel(d), b);
pDC->Lineh(a, yToPixel(d), b);
d+=ey;
}
}
......@@ -304,8 +320,8 @@ void CDiagr::drawAxes( QPainter* pDC ) // draw axes
d=tsx;
while(d<xmd)
{
pDC->Linev(TransxToPixel(d), PlotArea.bottom(), a);
pDC->Linev(TransxToPixel(d), PlotArea.top(), b);
pDC->Linev(xToPixel(d), PlotArea.bottom(), a);
pDC->Linev(xToPixel(d), PlotArea.top(), b);
d+=ex;
}
......@@ -314,8 +330,8 @@ void CDiagr::drawAxes( QPainter* pDC ) // draw axes
d=tsy;
while(d<ymd)
{
pDC->Lineh(PlotArea.left(), TransyToPixel(d), a);
pDC->Lineh(PlotArea.right(), TransyToPixel(d), b);
pDC->Lineh(PlotArea.left(), yToPixel(d), a);
pDC->Lineh(PlotArea.right(), yToPixel(d), b);
d+=ey;
}
}
......@@ -334,13 +350,13 @@ void CDiagr::drawGrid( QPainter* pDC )
d=tsx;
while(d<xmd)
{
pDC->Linev(TransxToPixel(d), PlotArea.bottom(), PlotArea.top());
pDC->Linev(xToPixel(d), PlotArea.bottom(), PlotArea.top());
d+=ex;
}
d=tsy;
while(d<ymd)
{
pDC->Lineh(PlotArea.left(), TransyToPixel(d), PlotArea.right());
pDC->Lineh(PlotArea.left(), yToPixel(d), PlotArea.right());
d+=ey;
}
}
......@@ -351,10 +367,10 @@ void CDiagr::drawGrid( QPainter* pDC )
for(x=tsx; x<xmd; x+=ex)
{
a=TransxToPixel(x);
a=xToPixel(x);
for(y=tsy; y<ymd; y+=ey)
{
b=TransyToPixel(y);
b=yToPixel(y);
pDC->Lineh(a-dx, b, a+dx);
pDC->Linev(a, b-dy, b+dy);
}
......@@ -404,8 +420,8 @@ void CDiagr::drawLabels(QPainter* pDC)
int const dy=40;
QFont const font=QFont( Settings::axesFont(), Settings::axesFontSize() );
pDC->setFont(font);
double const x=TransxToPixel(0.);
double const y=TransyToPixel(0.);
double const x=xToPixel(0.);
double const y=yToPixel(0.);
double d;
int n;
QString s;
......@@ -478,7 +494,7 @@ void CDiagr::drawLabels(QPainter* pDC)
if ( (s != "-") && (s != "+") )
{
swidth = test.width(s);
if ( TransxToPixel(d)-x<swidth && TransxToPixel(d)-x>-swidth && draw_next==0)
if ( xToPixel(d)-x<swidth && xToPixel(d)-x>-swidth && draw_next==0)
{
draw_next=1;
continue;
......@@ -493,8 +509,8 @@ void CDiagr::drawLabels(QPainter* pDC)
else
draw_next=0;
}
// kDebug() << "d="<<d<<" TransxToPixel(d)="<<TransxToPixel(d)<<endl;
QRectF drawRect( TransxToPixel(d), y+dy, 0, 0 );
// kDebug() << "d="<<d<<" xToPixel(d)="<<xToPixel(d)<<endl;
QRectF drawRect( xToPixel(d), y+dy, 0, 0 );
if ( xclipflg )
continue;
pDC->drawText( drawRect, Qt::AlignCenter|Qt::TextDontClip, s);
......@@ -502,9 +518,9 @@ void CDiagr::drawLabels(QPainter* pDC)
}
if(ymax<0 && xmax<0)
pDC->drawText( QRectF( TransxToPixel(xmax)-(4*dx), y+(dy-20), 0, 0 ), Qt::AlignCenter|Qt::TextDontClip, "x");
pDC->drawText( QRectF( xToPixel(xmax)-(4*dx), y+(dy-20), 0, 0 ), Qt::AlignCenter|Qt::TextDontClip, "x");
else
pDC->drawText( QRectF( TransxToPixel(xmax)-dx, y+dy, 0, 0 ), Qt::AlignCenter|Qt::TextDontClip, "x");
pDC->drawText( QRectF( xToPixel(xmax)-dx, y+dy, 0, 0 ), Qt::AlignCenter|Qt::TextDontClip, "x");
for(d=tsy, n=(int)ceil(ymin/ey); d<ymd; d+=ey, ++n)
{
......@@ -569,14 +585,14 @@ void CDiagr::drawLabels(QPainter* pDC)
{