From 4070de76935c1f94467cbf4495df8af765495ae6 Mon Sep 17 00:00:00 2001 From: serso Date: Sat, 27 Feb 2016 00:26:53 +0100 Subject: [PATCH] Plot from display --- .../android/calculator/DisplayFragment.java | 29 +++++++++++++++++-- .../plot/PlotEditFunctionFragment.java | 19 ++++-------- 2 files changed, 33 insertions(+), 15 deletions(-) diff --git a/app/src/main/java/org/solovyev/android/calculator/DisplayFragment.java b/app/src/main/java/org/solovyev/android/calculator/DisplayFragment.java index fd5d9538..844a2085 100644 --- a/app/src/main/java/org/solovyev/android/calculator/DisplayFragment.java +++ b/app/src/main/java/org/solovyev/android/calculator/DisplayFragment.java @@ -37,11 +37,17 @@ import butterknife.ButterKnife; import com.squareup.otto.Bus; import jscl.NumeralBase; import jscl.math.Generic; +import jscl.math.function.Constant; +import jscl.math.function.CustomFunction; import org.solovyev.android.calculator.converter.ConverterFragment; import org.solovyev.android.calculator.jscl.JsclOperation; +import org.solovyev.android.calculator.plot.ExpressionFunction; +import org.solovyev.android.plotter.Plotter; import javax.annotation.Nonnull; import javax.inject.Inject; +import java.util.ArrayList; +import java.util.List; public class DisplayFragment extends BaseFragment implements View.OnClickListener, MenuItem.OnMenuItemClickListener { @@ -80,10 +86,16 @@ public class DisplayFragment extends BaseFragment implements View.OnClickListene @Inject SharedPreferences preferences; @Inject + ErrorReporter errorReporter; + @Inject Display display; @Inject + ActivityLauncher launcher; + @Inject Bus bus; @Inject + Plotter plotter; + @Inject Calculator calculator; public DisplayFragment() { @@ -134,7 +146,8 @@ public class DisplayFragment extends BaseFragment implements View.OnClickListene addMenu(menu, R.string.c_convert, this); } } - if (Locator.getInstance().getPlotter().isPlotPossibleFor(result)) { + final int parameters = CalculatorUtils.getNotSystemConstants(result).size(); + if (parameters >= 0 && parameters <= 2) { addMenu(menu, R.string.c_plot, this); } } @@ -204,7 +217,19 @@ public class DisplayFragment extends BaseFragment implements View.OnClickListene return true; case R.string.c_plot: if (result != null) { - Locator.getInstance().getPlotter().plot(result); + try { + final List parameters = new ArrayList<>(); + for (Constant parameter : CalculatorUtils.getNotSystemConstants(result)) { + parameters.add(parameter.getName()); + } + new CustomFunction.Builder().setParameterNames(parameters).setContent(state.text).create(); + final CustomFunction f = new CustomFunction.Builder().setName("").setParameterNames(parameters).setContent(result.toString()).create(); + final ExpressionFunction ef = new ExpressionFunction(f, false); + plotter.add(ef); + launcher.showPlotter(); + } catch (RuntimeException e) { + errorReporter.onException(e); + } } return true; default: diff --git a/app/src/main/java/org/solovyev/android/calculator/plot/PlotEditFunctionFragment.java b/app/src/main/java/org/solovyev/android/calculator/plot/PlotEditFunctionFragment.java index 724628b7..a924303c 100644 --- a/app/src/main/java/org/solovyev/android/calculator/plot/PlotEditFunctionFragment.java +++ b/app/src/main/java/org/solovyev/android/calculator/plot/PlotEditFunctionFragment.java @@ -9,7 +9,8 @@ import android.view.LayoutInflater; import android.view.View; import android.widget.SeekBar; import android.widget.TextView; - +import butterknife.Bind; +import jscl.math.function.CustomFunction; import org.solovyev.android.calculator.App; import org.solovyev.android.calculator.AppComponent; import org.solovyev.android.calculator.R; @@ -20,18 +21,13 @@ import org.solovyev.android.plotter.PlotFunction; import org.solovyev.android.plotter.PlotIconView; import org.solovyev.android.plotter.Plotter; import org.solovyev.android.plotter.meshes.MeshSpec; - -import java.util.ArrayList; -import java.util.List; +import uz.shift.colorpicker.LineColorPicker; +import uz.shift.colorpicker.OnColorChangedListener; import javax.annotation.Nonnull; import javax.inject.Inject; - -import butterknife.Bind; -import jscl.math.function.Constant; -import jscl.math.function.CustomFunction; -import uz.shift.colorpicker.LineColorPicker; -import uz.shift.colorpicker.OnColorChangedListener; +import java.util.ArrayList; +import java.util.List; public class PlotEditFunctionFragment extends BaseFunctionFragment implements SeekBar.OnSeekBarChangeListener { @@ -157,9 +153,6 @@ public class PlotEditFunctionFragment extends BaseFunctionFragment protected boolean applyData(@Nonnull CppFunction function) { try { - final List parameters = function.getParameters(); - final Constant x = parameters.size() > 0 ? new Constant(parameters.get(0)) : null; - final Constant y = parameters.size() > 1 ? new Constant(parameters.get(1)) : null; final ExpressionFunction expressionFunction = new ExpressionFunction(function.toJsclBuilder().create(), false); final PlotFunction plotFunction = PlotFunction.create(expressionFunction,