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

kalgebra

  • sources
  • kde-4.14
  • 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(-10.0, 10.0), QSizeF(20.0, -20.0));
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  drawGrid(&m_buffer);
58  drawFunctions(&m_buffer);
59  m_dirty=false;
60  }
61 
62  p->drawPixmap(QPoint(0,0), m_buffer);
63 }
64 
65 void Graph2DMobile::forceRepaint()
66 {
67  m_dirty=true;
68  update();
69 }
70 
71 void Graph2DMobile::resetViewport()
72 {
73  setViewport(defViewport);
74 }
75 
76 void Graph2DMobile::modelChanged()
77 {
78  connect(model(), SIGNAL(dataChanged( const QModelIndex&, const QModelIndex& )),
79  this, SLOT(updateFuncs(const QModelIndex&, const QModelIndex)));
80  connect(model(), SIGNAL( rowsInserted ( const QModelIndex &, int, int ) ),
81  this, SLOT(addFuncs(const QModelIndex&, int, int)));
82  connect(model(), SIGNAL( rowsRemoved ( const QModelIndex &, int, int ) ),
83  this, SLOT(removeFuncs(const QModelIndex&, int, int)));
84 }
85 
86 void Graph2DMobile::addFuncs(const QModelIndex& parent, int start, int end) { updateFunctions(parent, start, end); }
87 
88 void Graph2DMobile::removeFuncs(const QModelIndex&, int, int) { forceRepaint(); }
89 void Graph2DMobile::updateFuncs(const QModelIndex& start, const QModelIndex& end) { updateFunctions(QModelIndex(), start.row(), end.row()); }
90 
91 void Graph2DMobile::scale(qreal s, int x, int y)
92 {
93  QRectF userViewport = lastUserViewport();
94  if(s<1 || (userViewport.height() < -3. && userViewport.width() > 3.)) {
95  scaleViewport(s, QPoint(x,y));
96  }
97 }
98 
99 void Graph2DMobile::translate(qreal x, qreal y)
100 {
101  moveViewport(QPoint(x,y));
102 }
103 
104 QColor randomFunctionColor() { return QColor::fromHsv(qrand()%255, 255, 225); }
105 
106 QStringList Graph2DMobile::addFunction(const QString& expression, Analitza::Variables* vars)
107 {
108  Analitza::Expression e(expression, Analitza::Expression::isMathML(expression));
109  PlotsModel* plotsmodel = qobject_cast<PlotsModel*>(model());
110  if(!plotsmodel)
111  qWarning() << "only can add plots to a PlotsModel instance";
112  QString fname;
113  do {
114  fname = plotsmodel->freeId();
115  } while(vars && vars->contains(fname));
116  QColor fcolor = randomFunctionColor();
117 
118  QStringList err;
119  PlotBuilder req = PlotsFactory::self()->requestPlot(e, Dim2D, vars);
120  if(req.canDraw()) {
121  PlaneCurve* it = static_cast<PlaneCurve*>(req.create(fcolor, fname));
122 
123  if(it->isCorrect())
124  plotsmodel->addPlot(it);
125  else {
126  err = it->errors();
127  delete it;
128  }
129  }
130 
131  return err;
132 }
Graph2DMobile::modelChanged
virtual void modelChanged()
Definition: graph2dmobile.cpp:76
QModelIndex
Graph2DMobile::translate
void translate(qreal x, qreal y)
Definition: graph2dmobile.cpp:99
QPixmap::size
QSize size() const
QWidget
Graph2DMobile::resetViewport
void resetViewport()
Definition: graph2dmobile.cpp:71
QPixmap::fill
void fill(const QColor &color)
QGraphicsItem::setFlag
void setFlag(GraphicsItemFlag flag, bool enabled)
QRectF::size
QSizeF size() const
Graph2DMobile::model
QAbstractItemModel model
Definition: graph2dmobile.h:33
QPoint
QGraphicsItem::update
void update(const QRectF &rect)
QPointF
QColor::fromHsv
QColor fromHsv(int h, int s, int v, int a)
randomFunctionColor
QColor randomFunctionColor()
Definition: graph2dmobile.cpp:104
QPainter::drawPixmap
void drawPixmap(const QRectF &target, const QPixmap &pixmap, const QRectF &source)
QPainter
QModelIndex::row
int row() const
Graph2DMobile::Graph2DMobile
Graph2DMobile(QDeclarativeItem *parent=0)
Definition: graph2dmobile.cpp:31
QString
QColor
QStringList
QPixmap
QPixmap::isNull
bool isNull() const
QRectF::width
qreal width() const
QSizeF
QGraphicsItem::boundingRect
virtual QRectF boundingRect() const =0
QRectF
Graph2DMobile::addFunction
QStringList addFunction(const QString &expression, Analitza::Variables *vars=0)
Definition: graph2dmobile.cpp:106
Graph2DMobile::forceRepaint
virtual void forceRepaint()
Definition: graph2dmobile.cpp:65
QStyleOptionGraphicsItem
QRectF::height
qreal height() const
QDeclarativeItem
Graph2DMobile::paint
virtual void paint(QPainter *p, const QStyleOptionGraphicsItem *options, QWidget *w)
Definition: graph2dmobile.cpp:42
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
graph2dmobile.h
QGraphicsItem::scale
qreal scale() const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:11:49 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
  • 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