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)
50 setWindowTitle(i18n(
"Add/Edit a function"));
52 QVBoxLayout *topLayout =
new QVBoxLayout(
this);
53 topLayout->setMargin(2);
54 topLayout->setSpacing(5);
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);
69 m_valid->setPalette(p);
71 m_validIcon =
new QLabel(
this);
72 m_validIcon->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
73 QLayout* validLayout=
new QHBoxLayout;
74 validLayout->addWidget(m_validIcon);
75 validLayout->addWidget(m_valid);
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);
88 m_graph->setViewport(QRect(QPoint(-5, 7), QPoint(5, -7)));
89 m_graph->setFocusPolicy(Qt::NoFocus);
90 m_graph->setMouseTracking(
false);
91 m_graph->setFramed(
true);
92 m_graph->setReadOnly(
true);
93 m_graph->setSquares(
false);
94 m_graph->setTicksShown(Qt::Orientation(0));
96 m_viewTabs->addTab(m_graph, QIcon::fromTheme(
"document-preview"), i18n(
"Preview"));
98 options->setLayout(
new QVBoxLayout);
99 m_uplimit=
new ExpressionEdit(options);
100 m_downlimit=
new ExpressionEdit(options);
101 m_uplimit->setText(
"2*pi");
102 m_downlimit->setText(
"0");
103 options->layout()->addWidget(
new QLabel(i18n(
"From:"), options));
104 options->layout()->addWidget(m_downlimit);
105 options->layout()->addWidget(
new QLabel(i18n(
"To:"), options));
106 options->layout()->addWidget(m_uplimit);
107 options->layout()->addItem(
new QSpacerItem(0,0, QSizePolicy::Expanding, QSizePolicy::Expanding));
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()));
112 QHBoxLayout *m_butts =
new QHBoxLayout;
113 m_ok =
new QPushButton(i18n(
"OK"),
this);
114 m_ok->setIcon(QIcon::fromTheme(
"dialog-ok"));
115 m_remove =
new QPushButton(i18nc(
"@action:button",
"Remove"),
this);
116 m_remove->setIcon(QIcon::fromTheme(
"list-remove"));
117 connect(m_ok, SIGNAL(clicked()),
this, SLOT(ok()));
120 topLayout->addWidget(m_name);
121 topLayout->addWidget(m_func);
122 topLayout->addWidget(m_color);
123 topLayout->addLayout(validLayout);
124 topLayout->addWidget(m_viewTabs);
125 topLayout->addLayout(m_butts);
129 m_butts->addWidget(m_ok);
130 m_butts->addWidget(m_remove);
133 m_ok->setEnabled(
false);
135 QFont errorFont=m_valid->font();
136 errorFont.setBold(
true);
137 m_valid->setFont(errorFont);
145 m_func->setText(QString());
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)
209 QFontMetrics fm(m_valid->font());
210 m_valid->setText(fm.elidedText(text, Qt::ElideRight, m_valid->width()));
211 m_valid->setToolTip(text);
213 KColorScheme scheme(QPalette::Normal);
214 KColorScheme::ForegroundRole role = negative? KColorScheme::NegativeText : KColorScheme::PositiveText;
216 QPalette p=m_valid->palette();
217 p.setColor(foregroundRole(), scheme.foreground(role).color());
218 m_valid->setPalette(p);
221 m_validIcon->setPixmap(QIcon::fromTheme(
"flag-red").pixmap(QSize(16,16)));
223 m_validIcon->setPixmap(QIcon::fromTheme(
"flag-green").pixmap(QSize(16,16)));
227 void FunctionEdit::edit()
229 if(m_func->text().isEmpty()) {
230 m_func->setCorrect(
true);
231 m_ok->setEnabled(
false);
233 m_valid->setToolTip(QString());
234 m_validIcon->setPixmap(QIcon::fromTheme(
"flag-yellow").pixmap(QSize(16,16)));
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);
264 setState(QString(
"%1:=%2")
265 .arg(m_name->text()).arg(f->expression().toString()),
false);
267 QStringList errors = req.errors();
269 errors = f->errors();
270 Q_ASSERT(!errors.isEmpty());
272 setState(errors.first(),
true);
273 m_valid->setToolTip(errors.join(
"<br />"));
276 m_func->setCorrect(added);
277 m_ok->setEnabled(added);
280 void FunctionEdit::ok()
282 if(m_ok->isEnabled())
286 void FunctionEdit::focusInEvent(QFocusEvent *)
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);
315 QFontMetrics fm(m_valid->font());
316 m_valid->setText(fm.elidedText(m_valid->toolTip(), Qt::ElideRight, m_valid->width()));
322 m_remove->setVisible(m);
void setOptionsShown(bool shown)
static double calcExp(const Analitza::Expression &exp, Analitza::Variables *v, bool *corr)
FunctionEdit(QWidget *parent=0)
Constructor.
~FunctionEdit()
Destructor.
Analitza::Expression expression() const
Retrieves the resulting expression text.
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 accept()
Tells that the result has been accepted.
virtual void resizeEvent(QResizeEvent *ev)
QColor color() const
Retrieves the selected color for the function.
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.