• 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
  • src
varedit.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 "varedit.h"
20 #include <analitza/analyzer.h>
21 #include <analitza/expression.h>
22 #include <analitza/variables.h>
23 #include <analitzagui/expressionedit.h>
24 
25 #include <KLocale>
26 
27 #include <QPushButton>
28 #include <QLabel>
29 #include <QVBoxLayout>
30 #include <QGroupBox>
31 #include <KPushButton>
32 
33 VarEdit::VarEdit(QWidget *parent, bool modal) :
34  KDialog(parent), vars(0), m_correct(false)
35 {
36  QWidget *widget = new QWidget( this );
37  setCaption(i18n("Add/Edit a variable"));
38  setModal(modal);
39  setButtons(KDialog::User1 | KDialog::Ok | KDialog::Cancel);
40  enableButtonApply( false );
41  setMainWidget( widget );
42 
43  setButtonText(KDialog::User1, i18n("Remove Variable"));
44 
45  setButtonIcon(KDialog::User1, KIcon("edit-table-delete-row"));
46  setButtonIcon(KDialog::Ok, KIcon("dialog-ok"));
47  setButtonIcon(KDialog::Cancel, KIcon("dialog-cancel"));
48 
49  connect(button(KDialog::User1), SIGNAL(clicked(bool)), SLOT(removeVariable()));
50 
51  connect( this, SIGNAL(applyClicked()), this, SLOT(accept()) );
52  connect( this, SIGNAL(okClicked()), this, SLOT(reject()) );
53 
54  m_exp = new Analitza::ExpressionEdit(widget);
55  connect(m_exp, SIGNAL(textChanged()), this, SLOT(edit()));
56  connect(m_exp, SIGNAL(returnPressed()), this, SLOT(ok()));
57 
58  m_valid = new QLabel(widget);
59 
60  QVBoxLayout *topLayout = new QVBoxLayout(widget);
61  topLayout->addWidget(m_exp);
62  topLayout->addWidget(m_valid);
63 
64  m_exp->setFocus();
65 }
66 
67 void VarEdit::setName(const QString& newVar)
68 {
69  m_var=newVar;
70  setWindowTitle(i18n("Edit '%1' value", newVar));
71  if(!vars)
72  m_exp->setText(i18n("not available"));
73  else {
74  m_exp->setExpression(Analitza::Expression(vars->value(newVar)->copy()));
75  }
76 
77  button(KDialog::User1)->setEnabled(canRemove(newVar));
78 }
79 
80 bool VarEdit::canRemove(const QString& name) const
81 {
82  QHash<QString, Analitza::Object*>::const_iterator it = vars->constBegin(), itEnd = vars->constEnd();
83  for(; it!=itEnd; ++it) {
84  QStringList deps = AnalitzaUtils::dependencies(*it, QStringList());
85  if(deps.contains(name)) {
86  return false;
87  }
88  }
89  return true;
90 }
91 
92 Analitza::Expression VarEdit::val()
93 {
94  Analitza::Expression val;
95  Analitza::Analyzer a(vars);
96 
97  a.setExpression(m_exp->expression());
98 
99  if(a.isCorrect()) {
100  a.simplify();
101  val=a.expression();
102  }
103 
104  m_correct = a.isCorrect();
105  if(m_correct) {
106  m_valid->setText(i18n("<b style='color:#090'>%1 := %2</b>", m_var, val.toString()));
107  m_valid->setToolTip(QString());
108  } else {
109  m_valid->setText(i18n("<b style='color:red'>WRONG</b>"));
110  m_valid->setToolTip(a.errors().join("\n"));
111  }
112  button(Ok)->setEnabled(m_correct);
113  m_exp->setCorrect(m_correct);
114 
115  return val;
116 }
117 
118 void VarEdit::edit()
119 {
120  val();
121 }
122 
123 void VarEdit::ok()
124 {
125  if(m_correct)
126  accept();
127 }
128 
129 void VarEdit::setAnalitza(Analitza::Analyzer * na)
130 {
131  vars= na->variables();
132  m_exp->setAnalitza(na);
133 }
134 
135 void VarEdit::removeVariable()
136 {
137  vars->remove(m_var);
138  close();
139 }
QWidget
varedit.h
QStringList::contains
bool contains(const QString &str, Qt::CaseSensitivity cs) const
KDialog
QBoxLayout::addWidget
void addWidget(QWidget *widget, int stretch, QFlags< Qt::AlignmentFlag > alignment)
VarEdit::VarEdit
VarEdit(QWidget *parent=0, bool modal=false)
Constructor.
Definition: varedit.cpp:33
VarEdit::setAnalitza
void setAnalitza(Analitza::Analyzer *na)
Sets an Analitza which will evaluate it.
Definition: varedit.cpp:129
QVBoxLayout
QLabel::setText
void setText(const QString &)
QString
VarEdit::setName
void setName(const QString &newVar)
Sets the editing variable name.
Definition: varedit.cpp:67
QStringList
VarEdit::val
Analitza::Expression val()
Returns the resulting variable expression.
Definition: varedit.cpp:92
QHash::const_iterator
QWidget::setToolTip
void setToolTip(const QString &)
QLabel
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