21 #include <QVBoxLayout>
22 #include <QHBoxLayout>
28 #include <KColorScheme>
30 #include <analitzagui/expressionedit.h>
31 #include <analitza/analyzer.h>
32 #include <analitza/expression.h>
33 #include <analitza/variables.h>
34 #include <analitza/value.h>
35 #include <analitzagui/algebrahighlighter.h>
36 #include <analitzagui/plotsview2d.h>
37 #include <analitzaplot/planecurve.h>
38 #include <analitzaplot/plotsmodel.h>
39 #include <analitzaplot/plotsfactory.h>
41 using namespace Analitza;
44 static const int resolution = 200;
48 :
QWidget(parent), m_calcUplimit(0), m_calcDownlimit(0)
56 m_name =
new KLineEdit(
this);
58 m_func =
new ExpressionEdit(
this);
59 m_func->setExamples(PlotsFactory::self()->examples(Dim2D));
61 connect(m_func, SIGNAL(textChanged()),
this, SLOT(edit()));
62 connect(m_func, SIGNAL(returnPressed()),
this, SLOT(ok()));
64 m_valid =
new QLabel(
this);
65 m_valid->
setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Fixed);
68 p.
setColor(QPalette::Active, QPalette::Base, Qt::white);
71 m_validIcon =
new QLabel(
this);
72 m_validIcon->
setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
77 m_color =
new KColorCombo(
this);
78 m_color->setColor(
QColor(0,150,0));
79 connect(m_color, SIGNAL(currentIndexChanged(
int)),
this, SLOT(colorChange(
int)));
81 m_funcsModel=
new PlotsModel(
this);
82 m_funcsModel->setResolution(resolution);
84 m_viewTabs=
new KTabWidget(
this);
86 m_graph =
new PlotsView2D(m_viewTabs);
87 m_graph->setModel(m_funcsModel);
89 m_graph->setFocusPolicy(Qt::NoFocus);
90 m_graph->setMouseTracking(
false);
91 m_graph->setFramed(
true);
92 m_graph->setReadOnly(
true);
93 m_graph->setShowGrid(
false);
94 m_graph->setTicksShown(Qt::Orientation(0));
96 m_viewTabs->addTab(m_graph,
QIcon::fromTheme(
"document-preview"), i18n(
"Preview"));
99 m_uplimit=
new ExpressionEdit(options);
100 m_downlimit=
new ExpressionEdit(options);
101 m_uplimit->setText(
"2*pi");
102 m_downlimit->setText(
"0");
108 m_viewTabs->addTab(options,
QIcon::fromTheme(
"configure"), i18n(
"Options"));
109 connect(m_uplimit, SIGNAL(textChanged()),
this, SLOT(updateUplimit()));
110 connect(m_downlimit, SIGNAL(textChanged()),
this, SLOT(updateDownlimit()));
115 m_remove =
new QPushButton(i18nc(
"@action:button",
"Remove"),
this);
117 connect(m_ok, SIGNAL(clicked()),
this, SLOT(ok()));
146 m_funcsModel->clear();
152 m_func->setText(newText);
153 m_func->document()->setModified(
true);
158 m_color->setColor(newColor);
159 if(m_funcsModel->rowCount()>0)
160 m_funcsModel->setData(m_funcsModel->index(0), newColor);
163 void FunctionEdit::colorChange(
int)
168 static double calcExp(
const Analitza::Expression& exp, Analitza::Variables* v,
bool* corr)
170 Q_ASSERT(exp.isCorrect());
171 Analitza::Analyzer d(v);
172 d.setExpression(exp);
173 Analitza::Expression r=d.calculate();
175 *corr=r.isCorrect() && r.isReal();
178 return r.toReal().value();
183 void FunctionEdit::updateUplimit()
185 bool corr = m_uplimit->isCorrect();
187 Analitza::Expression e=m_uplimit->expression();
188 m_calcUplimit=
calcExp(e, m_vars, &corr);
189 m_uplimit->setCorrect(corr);
195 void FunctionEdit::updateDownlimit()
197 bool corr = m_downlimit->isCorrect();
199 Analitza::Expression e=m_downlimit->expression();
200 m_calcDownlimit=
calcExp(e, m_vars, &corr);
201 m_downlimit->setCorrect(corr);
207 void FunctionEdit::setState(
const QString& text,
bool negative)
210 m_valid->
setText(fm.elidedText(text, Qt::ElideRight, m_valid->
width()));
213 KColorScheme scheme(QPalette::Normal);
214 KColorScheme::ForegroundRole role = negative? KColorScheme::NegativeText : KColorScheme::PositiveText;
227 void FunctionEdit::edit()
229 if(m_func->text().isEmpty()) {
230 m_func->setCorrect(
true);
236 m_funcsModel->clear();
237 m_graph->forceRepaint();
241 if(!m_uplimit->isCorrect() || !m_downlimit->isCorrect()) {
242 setState(i18n(
"The options you specified are not correct"),
true);
246 if(m_calcDownlimit>m_calcUplimit) {
247 setState(i18n(
"Downlimit cannot be greater than uplimit"),
true);
253 PlotBuilder req = PlotsFactory::self()->requestPlot(
expression(), Dim2D, m_vars);
257 if(f && f->isCorrect())
258 f->update(
QRect(-10, 10, 20, -20));
260 m_funcsModel->clear();
261 if(f && f->isCorrect()) {
262 m_funcsModel->addPlot(f);
265 .arg(m_name->text()).arg(f->expression().toString()),
false);
269 errors = f->errors();
272 setState(errors.
first(),
true);
276 m_func->setCorrect(added);
280 void FunctionEdit::ok()
293 PlotBuilder req = PlotsFactory::self()->requestPlot(
expression(), Dim2D, m_vars);
294 PlaneCurve* curve =
static_cast<PlaneCurve*
>(req.create(
color(),
name()));
295 curve->setResolution(resolution);
296 if(m_calcUplimit != m_calcDownlimit) {
297 foreach(
const QString& var, curve->parameters())
298 curve->setInterval(var, m_calcUplimit, m_calcDownlimit);
305 return m_func->expression();
310 m_viewTabs->setVisible(shown);
void setOptionsShown(bool shown)
static double calcExp(const Analitza::Expression &exp, Analitza::Variables *v, bool *corr)
virtual void addItem(QLayoutItem *item)=0
void setColor(ColorGroup group, ColorRole role, const QColor &color)
void setPixmap(const QPixmap &)
FunctionEdit(QWidget *parent=0)
Constructor.
QString join(const QString &separator) const
~FunctionEdit()
Destructor.
Analitza::Expression expression() const
Retrieves the resulting expression text.
void setBold(bool enable)
void setEditing(bool m)
Sets whether we are editing or adding a function.
void clear()
Clears the dialog.
void removeEditingPlot()
asks the currently edited plot to be removed.
void setText(const QString &)
void setMargin(int margin)
void accept()
Tells that the result has been accepted.
virtual void resizeEvent(QResizeEvent *ev)
QColor color() const
Retrieves the selected color for the function.
QIcon fromTheme(const QString &name, const QIcon &fallback)
void setFunction(const QString &newText)
Sets an expression text to the ExpressionEdit widget.
Analitza::PlaneCurve * createFunction() const
void setColor(const QColor &newColor)
Sets the selected color for the function.
QString name() const
Retrieves a name for the function.
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
void setSpacing(int spacing)
void addLayout(QLayout *layout, int stretch)