• 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
  • src
kalgebra.cpp
Go to the documentation of this file.
1 /*************************************************************************************
2  * Copyright (C) 2007 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 "kalgebra.h"
20 #include "varedit.h"
21 #include "consolehtml.h"
22 #include "dictionary.h"
23 #include "askname.h"
24 #include "variablesdelegate.h"
25 #include "viewportwidget.h"
26 #include "functionedit.h"
27 #ifdef HAVE_OPENGL
28 # include <analitzagui//plotsview3d.h>
29 #endif
30 
31 #include <analitzagui/operatorsmodel.h>
32 #include <analitzagui/expressionedit.h>
33 #include <analitzagui/variablesmodel.h>
34 #include <analitzagui/plotsview2d.h>
35 #include <analitzaplot/plotsmodel.h>
36 #include <analitzaplot/functiongraph.h>
37 #include <analitzaplot/planecurve.h>
38 #include <analitza/variables.h>
39 #include <analitza/value.h>
40 
41 #include <QVBoxLayout>
42 #include <QHeaderView>
43 #include <QDockWidget>
44 #include <QTableView>
45 #include <QPrinter>
46 #include <KAction>
47 #include <KHTMLView>
48 #include <KHelpMenu>
49 #include <KFileDialog>
50 #include <KMenu>
51 #include <KMenuBar>
52 #include <KStatusBar>
53 #include <KLocale>
54 #include <KStandardAction>
55 #include <KProcess>
56 #include <KRecentFilesAction>
57 #include <KApplication>
58 #include <KToggleFullScreenAction>
59 #include <kabstractfilewidget.h>
60 #include <analitzaplot/plotsfactory.h>
61 
62 class Add2DOption : public InlineOptions
63 {
64  public:
65  Add2DOption(KAlgebra* c)
66  : m_kalgebra(c)
67  {}
68 
69  virtual QString id() const { return "add2d"; }
70  virtual bool matchesExpression(const Analitza::Expression& exp, const Analitza::ExpressionType& functype) const {
71  return Analitza::PlotsFactory::self()->requestPlot(exp, Analitza::Dim2D).canDraw();
72  }
73 
74  virtual QString caption() const { return i18n("Plot 2D"); }
75 
76  virtual void triggerOption(const Analitza::Expression& exp) { m_kalgebra->add2D(exp); }
77 
78  private:
79  KAlgebra* m_kalgebra;
80 };
81 
82 #ifdef HAVE_OPENGL
83 class Add3DOption : public InlineOptions
84 {
85  public:
86  Add3DOption(KAlgebra* c)
87  : m_kalgebra(c)
88  {}
89 
90  virtual QString id() const { return "add3d"; }
91  virtual bool matchesExpression(const Analitza::Expression& exp, const Analitza::ExpressionType& functype) const
92  {
93  return Analitza::PlotsFactory::self()->requestPlot(exp, Analitza::Dim3D).canDraw();
94  }
95 
96  virtual QString caption() const { return i18n("Plot 3D"); }
97 
98  virtual void triggerOption(const Analitza::Expression& exp) { m_kalgebra->add3D(exp); }
99 
100  private:
101  KAlgebra* m_kalgebra;
102 };
103 #endif
104 
105 QColor randomFunctionColor() { return QColor::fromHsv(qrand()%255, 255, 255); }
106 
107 KAlgebra::KAlgebra(QWidget *parent) : KMainWindow(parent)
108 {
109  resize(900, 500);
110 
111  m_tabs = new QTabWidget;
112  setCentralWidget(m_tabs);
113 
114  setStatusBar(new KStatusBar(this));
115  setMenuBar(new KMenuBar(this));
116 
117  KToggleFullScreenAction* fullScreenAction = KStandardAction::fullScreen(this, SLOT(fullScreen(bool)), this, this);
118 
119  QMenu* g_menu = menuBar()->addMenu(i18n("Session"));
120  g_menu->addAction(KStandardAction::openNew(this, SLOT(newInstance()), this));
121  g_menu->addAction(fullScreenAction);
122  g_menu->addAction(KStandardAction::quit(this, SLOT(close()), this));
123 
124  QToolButton* fullScreenButton = new QToolButton(this);
125  fullScreenButton->setDefaultAction(fullScreenAction);
126  m_tabs->setCornerWidget(fullScreenButton);
127 
128  m_status = new QLabel(this);
129  statusBar()->insertWidget(0, m_status);
130  menuBar()->addMenu("|")->setEnabled(false);
131 
133  QWidget *console = new QWidget(m_tabs);
134  QVBoxLayout *c_layo = new QVBoxLayout(console);
135  c_results = new ConsoleHtml(console);
136  c_results->setFocusPolicy(Qt::NoFocus);
137  c_dock_vars = new QDockWidget(i18n("Variables"), this);
138  c_dock_vars->setFeatures(QDockWidget::DockWidgetFloatable|QDockWidget::DockWidgetMovable);
139  addDockWidget(Qt::RightDockWidgetArea, c_dock_vars);
140 
141  c_varsModel=new Analitza::VariablesModel(c_results->analitza()->variables());
142  c_varsModel->setEditable(false);
143 
144  c_variables = new QTreeView(c_dock_vars);
145  c_variables->setModel(c_varsModel);
146  c_variables->setRootIsDecorated(false);
147  c_variables->header()->setStretchLastSection(true);
148  c_variables->setSelectionBehavior(QAbstractItemView::SelectRows);
149  c_variables->setSelectionMode(QAbstractItemView::SingleSelection);
150 
151  c_exp = new Analitza::ExpressionEdit(console);
152  c_exp->setAnalitza(c_results->analitza());
153  c_exp->setExamples(QStringList() << "square:=x->x**2" << "fib:=n->piecewise { eq(n,0)?0, eq(n,1)?1, ?fib(n-1)+fib(n-2) }");
154  c_dock_vars->setWidget(c_variables);
155 
156  m_tabs->addTab(console, i18n("&Console"));
157  console->setLayout(c_layo);
158  c_layo->addWidget(c_results);
159  c_layo->addWidget(c_exp);
160 
161  connect(c_exp, SIGNAL(returnPressed()), this, SLOT(operate()));
162  connect(c_results, SIGNAL(status(QString)), this, SLOT(changeStatusBar(QString)));
163  connect(c_results, SIGNAL(changed()), this, SLOT(updateInformation()));
164  connect(c_results, SIGNAL(changed()), c_exp, SLOT(updateCompleter()));
165  connect(c_results, SIGNAL(paste(QString)), c_exp, SLOT(insertText(QString)));
166  connect(c_variables, SIGNAL(clicked(QModelIndex)), this, SLOT(edit_var(QModelIndex)));
168  c_menu = menuBar()->addMenu(i18n("C&onsole"));
169 
170  c_menu->addAction(KIcon("document-open"), i18nc("@item:inmenu", "&Load Script..."),
171  this, SLOT(loadScript()), Qt::CTRL+Qt::Key_L);
172  c_recentScripts=new KRecentFilesAction(KIcon("document-open-recent"), i18n("Recent Scripts"), this);
173  connect(c_recentScripts, SIGNAL(urlSelected(KUrl)), this, SLOT(loadScript(KUrl)));
174  c_menu->addAction(c_recentScripts);
175 
176  c_menu->addAction(KIcon("document-save"), i18nc("@item:inmenu", "&Save Script..."),
177  this, SLOT(saveScript()), Qt::CTRL+Qt::Key_G);
178  c_menu->addAction(KIcon("document-save"), i18nc("@item:inmenu", "&Export Log..."),
179  this, SLOT(saveLog()), QKeySequence::Save);
180  c_menu->addSeparator()->setText(i18n("Execution Mode"));
181  QActionGroup *execGroup = new QActionGroup(c_menu);
182  QAction* calc = c_menu->addAction(i18nc("@item:inmenu", "Calculate"), this, SLOT(consoleCalculate()));
183  QAction* eval = c_menu->addAction(i18nc("@item:inmenu", "Evaluate"), this, SLOT(consoleEvaluate()));
184 
185  calc->setCheckable(true);
186  eval->setCheckable(true);
187  eval->setChecked(true);
188  execGroup->addAction(calc);
189  execGroup->addAction(eval);
190  c_menu->addSeparator();
191  c_menu->addAction(KStandardAction::clear(c_results, SLOT(clear()), this));
192  initializeRecentScripts();
195 
197  b_funcsModel=new Analitza::PlotsModel(this);
198 
199  m_graph2d = new Analitza::PlotsView2D(m_tabs);
200  m_graph2d->setTicksShown(0);
201  m_graph2d->setModel(b_funcsModel);
202 
203  b_dock_funcs = new QDockWidget(i18n("Functions"), this);
204  b_tools = new QTabWidget(b_dock_funcs);
205  b_tools->setTabPosition(QTabWidget::South);
206  addDockWidget(Qt::RightDockWidgetArea, b_dock_funcs);
207 
208  b_funcs = new QTreeView(b_tools);
209  b_funcs->setRootIsDecorated(false);
210  b_funcs->setModel(b_funcsModel);
211  b_funcs->header()->resizeSections(QHeaderView::ResizeToContents);
212  b_funcs->setSelectionMode(QAbstractItemView::SingleSelection);
213  m_graph2d->setSelectionModel(b_funcs->selectionModel());
214 
215  b_tools->addTab(b_funcs, i18n("List"));
216 
217  b_funced = new FunctionEdit(b_tools);
218  b_funced->setVariables(c_varsModel->variables());
219  connect(b_funced, SIGNAL(accept()), this, SLOT(new_func()));
220  connect(b_funced, SIGNAL(removeEditingPlot()), this, SLOT(remove_func()));
221  b_tools->addTab(b_funced, KIcon("list-add"), i18n("&Add"));
222 
223  QTableView* b_varsView=new QTableView(b_tools);
224  b_varsModel=new Analitza::VariablesModel(b_funced->variables());
225  b_varsView->setModel(b_varsModel);
226  b_varsView->setShowGrid(false);
227  b_varsView->verticalHeader()->hide();
228  b_varsView->horizontalHeader()->setStretchLastSection(true);
229  b_varsView->setSelectionBehavior(QAbstractItemView::SelectRows);
230  b_varsView->setContextMenuPolicy(Qt::CustomContextMenu);
231  VariablesDelegate* delegate=new VariablesDelegate(b_varsView);
232  b_varsView->setItemDelegate(delegate);
233  b_tools->addTab(b_varsView, i18n("Variables"));
234 
235  ViewportWidget* b_viewport = new ViewportWidget(this);
236  b_viewport->setViewport(m_graph2d->definedViewport());
237  b_tools->addTab(b_viewport, i18n("Viewport"));
238 
239  b_dock_funcs->setWidget(b_tools);
240  b_dock_funcs->setFeatures(QDockWidget::DockWidgetFloatable | QDockWidget::DockWidgetMovable);
241  m_tabs->addTab(m_graph2d, i18n("&2D Graph"));
242  c_results->addOptionsObserver(new Add2DOption(this));
243 
244  connect(b_varsModel, SIGNAL(dataChanged(QModelIndex,QModelIndex)), SLOT(valueChanged()));
245  connect(b_funcs, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(edit_func(QModelIndex)));
246  connect(b_tools, SIGNAL(currentChanged(int)), this, SLOT(functools(int)));
247  connect(m_graph2d, SIGNAL(status(QString)), this, SLOT(changeStatusBar(QString)));
248  connect(m_graph2d, SIGNAL(viewportChanged(QRectF)), b_viewport, SLOT(setViewport(QRectF)));
249  connect(b_viewport, SIGNAL(viewportChange(QRectF)), m_graph2d, SLOT(changeViewport(QRectF)));
250  connect(b_varsView, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(varsContextMenu(QPoint)));
251 
253  b_menu = menuBar()->addMenu(i18n("2&D Graph"));
254  QAction* b_actions[6];
255  b_actions[0] = b_menu->addAction(i18n("&Grid"), this, SLOT(toggleSquares()));
256  b_actions[1] = b_menu->addAction(i18n("&Keep Aspect Ratio"), this, SLOT(toggleKeepAspect()));
257  b_menu->addAction(KStandardAction::save(this, SLOT(saveGraph()), this));
258  b_menu->addSeparator();
259  b_menu->addAction(KStandardAction::zoomIn(m_graph2d, SLOT(zoomIn()), this));
260  b_menu->addAction(KStandardAction::zoomOut(m_graph2d, SLOT(zoomOut()), this));
261  KAction* ac=KStandardAction::actualSize(m_graph2d, SLOT(resetViewport()), this);
262  ac->setShortcut(Qt::ControlModifier + Qt::Key_0);
263  b_menu->addAction(ac);
264  b_menu->addSeparator()->setText(i18n("Resolution"));
265  b_actions[2] = b_menu->addAction(i18nc("@item:inmenu", "Poor"), this, SLOT(set_res_low()));
266  b_actions[3] = b_menu->addAction(i18nc("@item:inmenu", "Normal"), this, SLOT(set_res_std()));
267  b_actions[4] = b_menu->addAction(i18nc("@item:inmenu", "Fine"), this, SLOT(set_res_fine()));
268  b_actions[5] = b_menu->addAction(i18nc("@item:inmenu", "Very Fine"), this, SLOT(set_res_vfine()));
269  m_graph2d->setContextMenuPolicy(Qt::ActionsContextMenu);
270  m_graph2d->addActions(b_menu->actions());
271 
272  QActionGroup *res = new QActionGroup(b_menu);
273  res->addAction(b_actions[2]);
274  res->addAction(b_actions[3]);
275  res->addAction(b_actions[4]);
276  res->addAction(b_actions[5]);
277 
278  b_actions[0]->setCheckable(true);
279  b_actions[0]->setChecked(true);
280  b_actions[1]->setCheckable(true);
281  b_actions[1]->setChecked(true);
282  b_actions[2]->setCheckable(true);
283  b_actions[3]->setCheckable(true);
284  b_actions[3]->setChecked(true);
285  b_actions[4]->setCheckable(true);
286  b_actions[5]->setCheckable(true);
287  set_res_std();
289 
291 #ifdef HAVE_OPENGL
292  QWidget *tridim = new QWidget(m_tabs);
293  QVBoxLayout *t_layo = new QVBoxLayout(tridim);
294  t_exp = new Analitza::ExpressionEdit(tridim);
295  t_exp->setExamples(QStringList() << "sin x+sin y" << "(x,y)->x" << "x*y");
296  t_exp->setAns("x");
297  t_model3d = new Analitza::PlotsModel(this);
298  m_graph3d = new Analitza::PlotsView3D(tridim);
299  m_graph3d->setModel(t_model3d);
300  m_graph3d->setUseSimpleRotation(true);
301 
302  tridim->setLayout(t_layo);
303  m_tabs->addTab(tridim, i18n("&3D Graph"));
304  t_layo->addWidget(m_graph3d);
305  t_layo->addWidget(t_exp);
306 
307  connect(t_exp, SIGNAL(returnPressed()), this, SLOT(new_func3d()));
308  c_results->addOptionsObserver(new Add3DOption(this));
309 
311  t_menu = menuBar()->addMenu(i18n("3D &Graph"));
312  QAction* t_actions[5];
313  t_menu->addAction(KStandardAction::save(this, SLOT(save3DGraph()), this));
314  t_menu->addAction(KIcon("zoom-original"), i18n("&Reset View"), m_graph3d, SLOT(resetView()));
315  t_menu->addSeparator();
316  t_actions[2] = t_menu->addAction(i18n("Dots"), this, SLOT(set_dots()));
317  t_actions[3] = t_menu->addAction(i18n("Lines"), this, SLOT(set_lines()));
318  t_actions[4] = t_menu->addAction(i18n("Solid"), this, SLOT(set_solid()));
319 
320  QActionGroup *t_type = new QActionGroup(t_menu);
321  t_type->addAction(t_actions[2]);
322  t_type->addAction(t_actions[3]);
323  t_type->addAction(t_actions[4]);
324 
325  t_actions[2]->setCheckable(true);
326  t_actions[3]->setCheckable(true);
327  t_actions[4]->setCheckable(true);
328  t_actions[4]->setChecked(true);
329 #endif
330  menuBar()->addMenu("|")->setEnabled(false);
333 
334  //Dictionary tab
335  d_dock = new QDockWidget(i18n("Operations"), this);
336  d_dock->setFeatures(QDockWidget::DockWidgetFloatable | QDockWidget::DockWidgetMovable);
337  addDockWidget(Qt::RightDockWidgetArea, d_dock);
338  Dictionary *dic = new Dictionary(m_tabs);
339  m_tabs->addTab(dic, i18n("&Dictionary"));
340 
341  QWidget *w=new QWidget;
342  QLayout *leftLayo=new QVBoxLayout(w);
343  d_filter=new KLineEdit(w);
344  d_filter->setClearButtonShown(true);
345  d_filter->setSizePolicy(QSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed));
346  connect(d_filter, SIGNAL(textChanged(QString)), dic, SLOT(setFilter(QString)));
347  connect(d_filter, SIGNAL(textChanged(QString)), this, SLOT(dictionaryFilterChanged(QString)));
348  d_list = new QListView(w);
349  d_list->setModel(dic->model());
350  d_list->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding));
351  leftLayo->addWidget(new QLabel(i18n("Look for:"), d_dock));
352  leftLayo->addWidget(d_filter);
353  leftLayo->addWidget(d_list);
354  d_dock->setWidget(w);
355 
356  connect(d_list->selectionModel(), SIGNAL(currentChanged(QModelIndex,QModelIndex)),
357  dic, SLOT(activated(QModelIndex,QModelIndex)));
358 
359  //EODictionary
360  //Ego's reminder
361  KHelpMenu* help = new KHelpMenu(this, KGlobal::mainComponent().aboutData());
362  menuBar()->addMenu(help->menu());
363 
364 #ifdef __GNUC__
365 #warning TODO: Port to PlotsModel
366 #endif
367 // connect(b_funcsModel, SIGNAL(functionModified(QString,Analitza::Expression)),
368 // c_results, SLOT(modifyVariable(QString,Analitza::Expression)));
369 // connect(b_funcsModel, SIGNAL(functionRemoved(QString)),
370 // c_results, SLOT(removeVariable(QString)));
371 
372  connect(m_tabs, SIGNAL(currentChanged(int)), this, SLOT(tabChanged(int)));
373  tabChanged(0);
374 }
375 
376 KAlgebra::~KAlgebra()
377 {
378  KConfig conf("kalgebrarc");
379  KConfigGroup config(&conf, "Default");
380  QStringList urls;
381  foreach(const KUrl& url, c_recentScripts->urls())
382  urls += url.prettyUrl();
383 
384  config.writeEntry("recent", urls);
385 }
386 
387 void KAlgebra::initializeRecentScripts()
388 {
389  KConfig conf("kalgebrarc");
390  KConfigGroup config(&conf, "Default");
391 
392  QStringList urls=config.readEntry("recent", QStringList());
393  foreach(const QString& url, urls) {
394  c_recentScripts->addUrl(KUrl(url));
395  }
396 }
397 
398 void KAlgebra::newInstance()
399 {
400  KProcess::startDetached(QApplication::applicationFilePath());
401 }
402 
403 void KAlgebra::add2D(const Analitza::Expression& exp)
404 {
405  qDebug() << "adding" << exp.toString();
406 
407  Analitza::PlotBuilder req = Analitza::PlotsFactory::self()->requestPlot(exp, Analitza::Dim2D, c_results->analitza()->variables());
408  Analitza::PlotItem* curve = req.create(randomFunctionColor(), b_funcsModel->freeId());
409  b_funcsModel->addPlot(curve);
410 
411  m_tabs->setCurrentIndex(1);
412 }
413 
414 void KAlgebra::new_func()
415 {
416  Analitza::FunctionGraph* f=b_funced->createFunction();
417 
418  if(b_funced->editing()) {
419  QModelIndex idx = b_funcsModel->indexForName(f->name());
420  b_funcsModel->updatePlot(idx.row(), f);
421  } else {
422  b_funcsModel->addPlot(f);
423  }
424 
425  b_funced->setEditing(false);
426  b_funced->clear();
427  b_tools->setCurrentIndex(0);
428  b_funcs->setCurrentIndex(b_funcsModel->indexForName(f->name()));
429  m_graph2d->setFocus();
430 }
431 
432 void KAlgebra::remove_func()
433 {
434  Q_ASSERT(b_funced->editing());
435  b_funcsModel->removeRow(b_funcs->currentIndex().row());
436 
437  b_funced->setEditing(false);
438  b_funced->clear();
439  b_tools->setCurrentIndex(0);
440  b_funcs->setCurrentIndex(QModelIndex());
441  m_graph2d->setFocus();
442 }
443 
444 void KAlgebra::edit_func(const QModelIndex &idx)
445 {
446  b_tools->setTabText(1, i18n("&Editing"));
447  b_tools->setCurrentIndex(1);
448  b_funced->setName(b_funcsModel->data(idx.sibling(idx.row(), 0)).toString());
449  b_funced->setFunction(b_funcsModel->data(idx.sibling(idx.row(), 1)).toString());
450  b_funced->setEditing(true);
451  b_funced->setFocus();
452 }
453 
454 void KAlgebra::functools(int i)
455 {
456  if(i==0)
457  b_tools->setTabText(1, i18n("&Add"));
458  else {
459  b_funced->setName(b_funcsModel->freeId());
460  b_funced->setColor(randomFunctionColor());
461  b_funced->setEditing(false);
462  b_funced->setFocus();
463  }
464 }
465 
466 void KAlgebra::edit_var(const QModelIndex &idx)
467 {
468  if(idx.column()==0) {
469  c_exp->insertText(idx.data().toString());
470  } else {
471  QModelIndex idxName=idx.sibling(idx.row(), 0);
472 
473  QPointer<VarEdit> e(new VarEdit(this, false));
474  QString var = c_variables->model()->data(idxName, Qt::DisplayRole).toString();
475 
476  e->setAnalitza(c_results->analitza());
477  e->setName(var);
478 
479  if(e->exec() == QDialog::Accepted) {
480  QString str=var+" := "+e->val().toString();
481  c_results->addOperation(Analitza::Expression(str, false), str);
482  }
483  delete e;
484  }
485 }
486 
487 void KAlgebra::operate()
488 {
489  if(!c_exp->text().isEmpty()){
490  c_exp->setCorrect(c_results->addOperation(c_exp->expression(), c_exp->toPlainText()));
491  c_exp->selectAll();
492  }
493 }
494 
495 void KAlgebra::changeStatusBar(const QString& msg)
496 {
497 // statusBar()->showMessage(msg);
498  m_status->setText(msg);
499 }
500 
501 void KAlgebra::loadScript()
502 {
503  KUrl path = KFileDialog::getOpenUrl(KUrl(), "*.kal|"+i18n("Script (*.kal)"), this, i18n("Choose a script"));
504 
505  if(!path.isEmpty())
506  loadScript(path);
507 }
508 
509 void KAlgebra::loadScript(const KUrl& path)
510 {
511  bool loaded=c_results->loadScript(path);
512 
513  if(loaded)
514  c_recentScripts->addUrl(path);
515 }
516 
517 void KAlgebra::saveScript()
518 {
519  KUrl path = KFileDialog::getSaveUrl(KUrl(), "*.kal|"+i18n("Script (*.kal)"), this, QString(), KFileDialog::ConfirmOverwrite);
520  bool loaded=false;
521  if(!path.isEmpty())
522  loaded=c_results->saveScript(path);
523 
524  if(loaded)
525  c_recentScripts->addUrl(path);
526 }
527 
528 void KAlgebra::saveLog()
529 {
530  KUrl path = KFileDialog::getSaveUrl(KUrl(), "*.html|"+i18n("HTML File (*.html)"), this, QString(), KFileDialog::ConfirmOverwrite);
531  if(!path.isEmpty())
532  c_results->saveLog(path);
533 }
534 
535 void KAlgebra::set_res_low() { b_funcsModel->setResolution(416); }
536 void KAlgebra::set_res_std() { b_funcsModel->setResolution(832); }
537 void KAlgebra::set_res_fine() { b_funcsModel->setResolution(1664);}
538 void KAlgebra::set_res_vfine() { b_funcsModel->setResolution(3328);}
539 
540 void KAlgebra::new_func3d()
541 {
542 #ifdef HAVE_OPENGL
543  Analitza::Expression exp = t_exp->expression();
544  Analitza::PlotBuilder plot = Analitza::PlotsFactory::self()->requestPlot(exp, Analitza::Dim3D, c_results->analitza()->variables());
545  if(plot.canDraw()) {
546  t_model3d->clear();
547  t_model3d->addPlot(plot.create(Qt::yellow, "func3d"));
548  } else
549  changeStatusBar(i18n("Errors: %1", plot.errors().join(i18n(", "))));
550 #endif
551 }
552 
553 void KAlgebra::set_dots()
554 {
555 #ifdef HAVE_OPENGL
556  m_graph3d->setPlotStyle(Analitza::Dots);
557 #endif
558 }
559 
560 void KAlgebra::set_lines()
561 {
562 #ifdef HAVE_OPENGL
563  m_graph3d->setPlotStyle(Analitza::Wired);
564 #endif
565 }
566 
567 void KAlgebra::set_solid()
568 {
569 #ifdef HAVE_OPENGL
570  m_graph3d->setPlotStyle(Analitza::Solid);
571 #endif
572 }
573 
574 void KAlgebra::save3DGraph()
575 {
576 #ifdef HAVE_OPENGL
577  QString path = KFileDialog::getSaveFileName(KUrl(), i18n("*.png|PNG File\n*.pdf|PDF Document"), this, QString(), KFileDialog::ConfirmOverwrite);
578  if(!path.isEmpty()) {
579  QPixmap px = m_graph3d->renderPixmap();
580  if(path.endsWith(".pdf")) {
581  QPrinter printer;
582  printer.setOutputFormat(QPrinter::PdfFormat);
583  printer.setOutputFileName(path);
584  printer.setPaperSize(px.size(), QPrinter::DevicePixel);
585  printer.setPageMargins(0,0,0,0, QPrinter::DevicePixel);
586  QPainter painter;
587  painter.begin(&printer);
588  painter.drawPixmap(QPoint(0,0), px);
589  painter.end();
590  } else {
591  px.save(path);
592  }
593  }
594 #endif
595 }
596 
597 void KAlgebra::toggleSquares()
598 {
599  m_graph2d->setSquares(!m_graph2d->squares());
600 }
601 
602 void KAlgebra::toggleKeepAspect()
603 {
604  m_graph2d->setKeepAspectRatio(!m_graph2d->keepAspectRatio());
605 }
606 
607 void KAlgebra::saveGraph()
608 {
609  QPointer<KFileDialog> dialog=new KFileDialog(KUrl(), i18n("*.png|Image File\n*.svg|SVG File"), this);
610  dialog->setOperationMode(KFileDialog::Saving);
611  dialog->setConfirmOverwrite(true);
612 
613  if(dialog->exec()) {
614  QString filter = dialog->fileWidget()->currentFilter();
615  QString filename = dialog->selectedFile();
616 
617  bool isSvg = filename.endsWith(".svg") || (!filename.endsWith(".png") && filter.mid(2, 3)=="svg");
618  Analitza::PlotsView2D::Format f = isSvg ? Analitza::PlotsView2D::PNG : Analitza::PlotsView2D::SVG;
619  m_graph2d->toImage(filename, f);
620  }
621  delete dialog;
622 }
623 
624 void menuEnabledHelper(QMenu* m, bool enabled)
625 {
626  m->setEnabled(enabled);
627  foreach(QAction* action, m->actions()) {
628  action->setEnabled(enabled);
629  }
630 }
631 
632 void KAlgebra::tabChanged(int n)
633 {
634  c_dock_vars->hide();
635  b_dock_funcs->hide();
636  d_dock->hide();
637 
638  menuEnabledHelper(c_menu, n==0);
639  menuEnabledHelper(b_menu, n==1);
640 #ifdef HAVE_OPENGL
641  menuEnabledHelper(t_menu, n==2);
642 #endif
643  m_status->clear();
644 
645  switch(n) {
646  case 0:
647  c_dock_vars->show();
648  c_dock_vars->raise();
649  c_exp->setFocus();
650  break;
651  case 1:
652  b_dock_funcs->show();
653  b_dock_funcs->raise();
654 
655  if(b_funcsModel->rowCount()==0)
656  b_tools->setCurrentIndex(1); //We set the Add tab
657 // b_add->setFocus();
658  break;
659 #ifdef HAVE_OPENGL
660  case 2:
661  t_exp->setFocus();
662  break;
663 #endif
664  case 3:
665  d_filter->setFocus();
666  d_dock->show();
667  d_dock->raise();
668  default:
669  break;
670  }
671  changeStatusBar(i18nc("@info:status", "Ready"));
672 }
673 
674 void KAlgebra::select(const QModelIndex & idx)
675 {
676  b_funcs->selectionModel()->setCurrentIndex(idx, QItemSelectionModel::ClearAndSelect);
677 }
678 
679 void KAlgebra::updateInformation()
680 {
681  c_varsModel->updateInformation();
682  c_variables->header()->resizeSections(QHeaderView::ResizeToContents);
683 }
684 
685 void KAlgebra::consoleCalculate()
686 {
687  c_results->setMode(ConsoleHtml::Calculation);
688 }
689 
690 void KAlgebra::consoleEvaluate()
691 {
692  c_results->setMode(ConsoleHtml::Evaluation);
693 }
694 
695 void KAlgebra::valueChanged()
696 {
697  //FIXME: Should only repaint the affected ones.
698  if(b_funcsModel->rowCount()>0)
699  m_graph2d->updateFunctions(QModelIndex(), 0, b_funcsModel->rowCount()-1);
700 }
701 
702 void KAlgebra::varsContextMenu(const QPoint& p)
703 {
704  QPointer<QMenu> m=new QMenu;
705  m->addAction(i18n("Add variable"));
706  QAction* ac=m->exec(b_dock_funcs->widget()->mapToGlobal(p));
707 
708  if(ac) {
709  QPointer<AskName> a=new AskName(i18n("Enter a name for the new variable"), 0);
710 
711  if(a->exec()==QDialog::Accepted)
712  b_varsModel->insertVariable(a->name(), Analitza::Expression(Analitza::Cn(0)));
713  delete a;
714  }
715  delete m;
716 }
717 
718 void KAlgebra::add3D(const Analitza::Expression& exp)
719 {
720 #ifdef HAVE_OPENGL
721  t_model3d->clear();
722  t_model3d->addPlot(Analitza::PlotsFactory::self()->requestPlot(exp, Analitza::Dim3D, c_results->analitza()->variables())
723  .create(Qt::yellow, "func3d_console"));
724  m_tabs->setCurrentIndex(2);
725 #endif
726 }
727 
728 void KAlgebra::dictionaryFilterChanged(const QString&)
729 {
730  if(d_list->model()->rowCount()==1)
731  d_list->setCurrentIndex(d_list->model()->index(0,0));
732 }
733 
734 void KAlgebra::fullScreen(bool isFull)
735 {
736  m_tabs->setDocumentMode(isFull);
737  m_tabs->setTabEnabled(3, !isFull);
738  if(isFull) {
739  hide();
740  m_tabs->setParent(0);
741  setCentralWidget(0);
742  m_tabs->showFullScreen();
743  } else {
744  setCentralWidget(m_tabs);
745  show();
746  }
747 }
AskName
Definition: askname.h:29
ViewportWidget
Definition: viewportwidget.h:25
randomFunctionColor
QColor randomFunctionColor()
Definition: kalgebra.cpp:105
varedit.h
KAlgebra::add3D
void add3D(const Analitza::Expression &exp)
Definition: kalgebra.cpp:718
InlineOptions::id
virtual QString id() const =0
VarEdit
The VarEdit provides a dialog to allow users to edit/create a variable.
Definition: varedit.h:42
QWidget
ConsoleHtml::analitza
Analitza::Analyzer * analitza()
Retrieves a pointer to the Analitza calculator associated.
Definition: consolehtml.h:62
KAlgebra::~KAlgebra
~KAlgebra()
Definition: kalgebra.cpp:376
ConsoleHtml::addOperation
bool addOperation(const Analitza::Expression &e, const QString &input)
Adds the operation defined by the expression e.
Definition: consolehtml.cpp:97
functionedit.h
VariablesDelegate
Definition: variablesdelegate.h:24
ConsoleHtml::addOptionsObserver
void addOptionsObserver(InlineOptions *opt)
Definition: consolehtml.h:70
consolehtml.h
Dictionary::model
QSortFilterProxyModel * model() const
Definition: dictionary.h:47
ConsoleHtml
The Console widget is able to receive an operation, solve it and show the value.
Definition: consolehtml.h:45
Dictionary
Definition: dictionary.h:40
KMainWindow
FunctionEdit::setEditing
void setEditing(bool m)
Sets whether we are editing or adding a function.
Definition: functionedit.cpp:319
ViewportWidget::setViewport
void setViewport(const QRectF &current)
Definition: viewportwidget.cpp:64
menuEnabledHelper
void menuEnabledHelper(QMenu *m, bool enabled)
Definition: kalgebra.cpp:624
FunctionEdit::clear
void clear()
Clears the dialog.
Definition: functionedit.cpp:143
ConsoleHtml::setMode
void setMode(ConsoleMode newMode)
Sets a newMode console mode.
Definition: consolehtml.h:65
KAlgebra::add2D
void add2D(const Analitza::Expression &exp)
Definition: kalgebra.cpp:403
viewportwidget.h
InlineOptions::matchesExpression
virtual bool matchesExpression(const Analitza::Expression &exp, const Analitza::ExpressionType &functype) const =0
ConsoleHtml::loadScript
bool loadScript(const KUrl &path)
Loads a script from path.
Definition: consolehtml.cpp:169
ConsoleHtml::saveLog
bool saveLog(const KUrl &path) const
Saves a log to path.
Definition: consolehtml.cpp:216
ConsoleHtml::saveScript
bool saveScript(const KUrl &path) const
Save a script yo path.
Definition: consolehtml.cpp:194
FunctionEdit
The FunctionEdit dialog provides a way to specify functions.
Definition: functionedit.h:46
InlineOptions::caption
virtual QString caption() const =0
InlineOptions
Definition: consolehtml.h:28
FunctionEdit::setName
void setName(const QString &name)
Sets a name to the function.
Definition: functionedit.h:77
variablesdelegate.h
KAlgebra::KAlgebra
KAlgebra(QWidget *parent=0)
Definition: kalgebra.cpp:107
KAlgebra
Definition: kalgebra.h:45
kalgebra.h
FunctionEdit::variables
Analitza::Variables * variables() const
Definition: functionedit.h:85
InlineOptions::triggerOption
virtual void triggerOption(const Analitza::Expression &exp)=0
FunctionEdit::setVariables
void setVariables(Analitza::Variables *v)
Sets the variables class to be used with the graph functions.
Definition: functionedit.h:83
ConsoleHtml::Calculation
Calculates everything, if it finds a not defined variable shows an error.
Definition: consolehtml.h:52
FunctionEdit::setFunction
void setFunction(const QString &newText)
Sets an expression text to the ExpressionEdit widget.
Definition: functionedit.cpp:150
FunctionEdit::createFunction
Analitza::PlaneCurve * createFunction() const
Definition: functionedit.cpp:291
FunctionEdit::setColor
void setColor(const QColor &newColor)
Sets the selected color for the function.
Definition: functionedit.cpp:156
askname.h
dictionary.h
FunctionEdit::editing
bool editing() const
Returns whether we are editing or adding a function.
Definition: functionedit.h:71
ConsoleHtml::Evaluation
Simplifies the expression, tries to simplify when sees a variable not defined.
Definition: consolehtml.h:51
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