• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdeedu API Reference
  • KDE Home
  • Contact Us
 

kalgebra

  • sources
  • kde-4.12
  • kdeedu
  • kalgebra
  • mobile
  • declarative
graph2dmobile.cpp
Go to the documentation of this file.
1 /*************************************************************************************
2  * Copyright (C) 2011 by Aleix Pol <aleixpol@kde.org> *
3  * *
4  * This program is free software; you can redistribute it and/or *
5  * modify it under the terms of the GNU General Public License *
6  * as published by the Free Software Foundation; either version 2 *
7  * of the License, or (at your option) any later version. *
8  * *
9  * This program is distributed in the hope that it will be useful, *
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12  * GNU General Public License for more details. *
13  * *
14  * You should have received a copy of the GNU General Public License *
15  * along with this program; if not, write to the Free Software *
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA *
17  *************************************************************************************/
18 
19 #include "graph2dmobile.h"
20 #include <QPainter>
21 #include <QStyleOption>
22 #include <QEvent>
23 #include <analitza/variables.h>
24 #include <analitzaplot/planecurve.h>
25 #include <analitzaplot/plotsmodel.h>
26 #include <analitzaplot/plotsfactory.h>
27 #include <KDebug>
28 
29 using namespace Analitza;
30 
31 Graph2DMobile::Graph2DMobile(QDeclarativeItem* parent)
32  : QDeclarativeItem(parent), Plotter2D(boundingRect().size())
33  , m_dirty(true), m_currentFunction(-1)
34 {
35  setSize(QSizeF(100,100));
36  setFlag(QGraphicsItem::ItemHasNoContents, false);
37 
38  defViewport = QRectF(QPointF(-12., 10.), QSizeF(24., -20.));
39  resetViewport();
40 }
41 
42 void Graph2DMobile::paint(QPainter* p, const QStyleOptionGraphicsItem* options, QWidget* )
43 {
44 // qDebug() << "hellooo" << boundingRect();
45  if(boundingRect().size().isEmpty())
46  return;
47 
48  if(m_buffer.size()!=boundingRect().size()) {
49  m_buffer = QPixmap(boundingRect().size().toSize());
50  setPaintedSize(boundingRect().size().toSize());
51  }
52 
53  Q_ASSERT(!m_buffer.isNull());
54 
55  if(m_dirty) {
56  m_buffer.fill(Qt::transparent);
57  drawFunctions(&m_buffer);
58  m_dirty=false;
59  }
60 
61  p->drawPixmap(QPoint(0,0), m_buffer);
62 }
63 
64 void Graph2DMobile::forceRepaint()
65 {
66  m_dirty=true;
67  update();
68 }
69 
70 void Graph2DMobile::resetViewport()
71 {
72  setViewport(defViewport);
73 }
74 
75 void Graph2DMobile::modelChanged()
76 {
77  connect(model(), SIGNAL(dataChanged( const QModelIndex&, const QModelIndex& )),
78  this, SLOT(updateFuncs(const QModelIndex&, const QModelIndex)));
79  connect(model(), SIGNAL( rowsInserted ( const QModelIndex &, int, int ) ),
80  this, SLOT(addFuncs(const QModelIndex&, int, int)));
81  connect(model(), SIGNAL( rowsRemoved ( const QModelIndex &, int, int ) ),
82  this, SLOT(removeFuncs(const QModelIndex&, int, int)));
83 }
84 
85 void Graph2DMobile::addFuncs(const QModelIndex& parent, int start, int end) { updateFunctions(parent, start, end); }
86 
87 void Graph2DMobile::removeFuncs(const QModelIndex&, int, int) { forceRepaint(); }
88 void Graph2DMobile::updateFuncs(const QModelIndex& start, const QModelIndex& end) { updateFunctions(QModelIndex(), start.row(), end.row()); }
89 
90 void Graph2DMobile::scale(qreal s, int x, int y)
91 {
92  QRectF userViewport = lastUserViewport();
93  if(s<1 || (userViewport.height() < -3. && userViewport.width() > 3.)) {
94  scaleViewport(s, QPoint(x,y));
95  }
96 }
97 
98 void Graph2DMobile::translate(qreal x, qreal y)
99 {
100  moveViewport(QPoint(x,y));
101 }
102 
103 QColor randomFunctionColor() { return QColor::fromHsv(qrand()%255, 255, 225); }
104 
105 QStringList Graph2DMobile::addFunction(const QString& expression, Analitza::Variables* vars)
106 {
107  Analitza::Expression e(expression, Analitza::Expression::isMathML(expression));
108  PlotsModel* plotsmodel = qobject_cast<PlotsModel*>(model());
109  if(!plotsmodel)
110  qWarning() << "only can add plots to a PlotsModel instance";
111  QString fname;
112  do {
113  fname = plotsmodel->freeId();
114  } while(vars && vars->contains(fname));
115  QColor fcolor = randomFunctionColor();
116 
117  QStringList err;
118  PlotBuilder req = PlotsFactory::self()->requestPlot(e, Dim2D, vars);
119  if(req.canDraw()) {
120  PlaneCurve* it = static_cast<PlaneCurve*>(req.create(fcolor, fname));
121 
122  if(it->isCorrect())
123  plotsmodel->addPlot(it);
124  else {
125  err = it->errors();
126  delete it;
127  }
128  }
129 
130  return err;
131 }
Graph2DMobile::modelChanged
virtual void modelChanged()
Definition: graph2dmobile.cpp:75
Graph2DMobile::translate
void translate(qreal x, qreal y)
Definition: graph2dmobile.cpp:98
Graph2DMobile::resetViewport
void resetViewport()
Definition: graph2dmobile.cpp:70
Graph2DMobile::scale
void scale(qreal s, int x, int y)
Definition: graph2dmobile.cpp:90
QWidget
Graph2DMobile::model
QAbstractItemModel model
Definition: graph2dmobile.h:33
randomFunctionColor
QColor randomFunctionColor()
Definition: graph2dmobile.cpp:103
QDeclarativeItem
Graph2DMobile::Graph2DMobile
Graph2DMobile(QDeclarativeItem *parent=0)
Definition: graph2dmobile.cpp:31
Graph2DMobile::addFunction
QStringList addFunction(const QString &expression, Analitza::Variables *vars=0)
Definition: graph2dmobile.cpp:105
Graph2DMobile::forceRepaint
virtual void forceRepaint()
Definition: graph2dmobile.cpp:64
Graph2DMobile::paint
virtual void paint(QPainter *p, const QStyleOptionGraphicsItem *options, QWidget *w)
Definition: graph2dmobile.cpp:42
graph2dmobile.h
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:35:27 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kalgebra

Skip menu "kalgebra"
  • Main Page
  • Namespace List
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdeedu API Reference

Skip menu "kdeedu API Reference"
  • Analitza
  •     lib
  • kalgebra
  • kalzium
  •   libscience
  • kanagram
  • kig
  •   lib
  • klettres
  • kstars
  • libkdeedu
  •   keduvocdocument
  • marble
  • parley
  • rocs
  •   App
  •   RocsCore
  •   VisualEditor
  •   stepcore

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal