new plotter

This commit is contained in:
Sergey Solovyev 2013-01-19 21:41:13 +04:00
parent 220952ef9b
commit 8d2a0458e2
9 changed files with 207 additions and 249 deletions

View File

@ -317,6 +317,8 @@
<string name="cpp_plot_range_x_min">X Min</string> <string name="cpp_plot_range_x_min">X Min</string>
<string name="cpp_plot_range_x_max">X Max</string> <string name="cpp_plot_range_x_max">X Max</string>
<string name="cpp_invalid_number">Invalid number!</string> <string name="cpp_invalid_number">Invalid number!</string>
<string name="cpp_plot_boundaries_should_differ">Graph boundaries should not be the same!</string>
<string name="cpp_apply">Apply</string>
</resources> </resources>

View File

@ -55,6 +55,7 @@
<activity android:label="@string/c_plot_graph" android:name=".plot.CalculatorPlotActivity"/> <activity android:label="@string/c_plot_graph" android:name=".plot.CalculatorPlotActivity"/>
<activity android:launchMode="singleTop" android:label="@string/cpp_plot_functions" android:name=".plot.CalculatorPlotFunctionsActivity" android:theme="@style/cpp_gray_dialog_theme"/> <activity android:launchMode="singleTop" android:label="@string/cpp_plot_functions" android:name=".plot.CalculatorPlotFunctionsActivity" android:theme="@style/cpp_gray_dialog_theme"/>
<activity android:launchMode="singleTop" android:label="@string/cpp_plot_function_settings" android:name=".plot.CalculatorPlotFunctionSettingsActivity" android:theme="@style/cpp_gray_dialog_theme"/> <activity android:launchMode="singleTop" android:label="@string/cpp_plot_function_settings" android:name=".plot.CalculatorPlotFunctionSettingsActivity" android:theme="@style/cpp_gray_dialog_theme"/>
<activity android:launchMode="singleTop" android:label="@string/cpp_plot_range" android:name=".plot.CalculatorPlotRangeActivity" android:theme="@style/cpp_gray_dialog_theme"/>
<!-- todo serso: strings--> <!-- todo serso: strings-->
<activity android:label="@string/c_plot_graph" android:name=".matrix.CalculatorMatrixActivity"/> <activity android:label="@string/c_plot_graph" android:name=".matrix.CalculatorMatrixActivity"/>

View File

@ -45,8 +45,8 @@
a:layout_width="match_parent" a:layout_width="match_parent"
a:layout_height="wrap_content"/> a:layout_height="wrap_content"/>
<Button a:id="@+id/cpp_ok_button" <Button a:id="@+id/cpp_apply_button"
a:text="@string/ok" a:text="@string/cpp_apply"
a:layout_width="match_parent" a:layout_width="match_parent"
a:layout_height="wrap_content"/> a:layout_height="wrap_content"/>

View File

@ -3,34 +3,61 @@
<LinearLayout xmlns:a="http://schemas.android.com/apk/res/android" <LinearLayout xmlns:a="http://schemas.android.com/apk/res/android"
a:id="@+id/main_fragment_layout" a:id="@+id/main_fragment_layout"
style="?cpp_fragment_layout_style" style="?cpp_fragment_layout_style"
a:layout_width="wrap_content" a:layout_width="match_parent"
a:layout_gravity="center_horizontal" a:layout_gravity="center_horizontal"
a:layout_height="match_parent" a:layout_height="match_parent"
a:orientation="vertical"> a:orientation="vertical">
<include layout="@layout/ad" /> <include layout="@layout/ad" />
<TextView a:text="@string/cpp_plot_range_x_min" <LinearLayout
a:layout_width="match_parent"
a:layout_height="wrap_content"
a:orientation="horizontal"
a:baselineAligned="false">
<LinearLayout
a:layout_width="0dp"
a:layout_height="wrap_content"
a:layout_weight="1"
a:orientation="vertical">
<TextView
a:text="@string/cpp_plot_range_x_min"
a:layout_width="match_parent" a:layout_width="match_parent"
a:layout_height="wrap_content" /> a:layout_height="wrap_content" />
<EditText a:id="@+id/cpp_plot_range_x_min_editext" <EditText
a:inputType="numberDecimal" a:id="@+id/cpp_plot_range_x_min_editext"
a:inputType="numberDecimal|numberSigned"
a:layout_width="match_parent"
a:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
a:layout_width="0dp"
a:layout_height="wrap_content"
a:layout_weight="1"
a:orientation="vertical">
<TextView
a:text="@string/cpp_plot_range_x_max"
a:layout_width="match_parent" a:layout_width="match_parent"
a:layout_height="wrap_content" /> a:layout_height="wrap_content" />
<TextView a:text="@string/cpp_plot_range_x_max" <EditText
a:layout_width="match_parent" a:id="@+id/cpp_plot_range_x_max_editext"
a:layout_height="wrap_content"/> a:inputType="numberDecimal|numberSigned"
<EditText a:id="@+id/cpp_plot_range_x_max_editext"
a:inputType="numberDecimal"
a:layout_width="match_parent" a:layout_width="match_parent"
a:layout_height="wrap_content" /> a:layout_height="wrap_content" />
</LinearLayout>
<Button a:id="@+id/cpp_ok_button" </LinearLayout>
a:text="@string/ok"
<Button
a:id="@+id/cpp_apply_button"
a:text="@string/cpp_apply"
a:layout_width="match_parent" a:layout_width="match_parent"
a:layout_height="wrap_content" /> a:layout_height="wrap_content" />

View File

@ -18,6 +18,11 @@
a:icon="@drawable/ab_plot_3d" a:icon="@drawable/ab_plot_3d"
a:showAsAction="always"/> a:showAsAction="always"/>
<item a:id="@+id/menu_plot_range"
a:title="@string/cpp_plot_range"
a:icon="@drawable/ab_plot_range"
a:showAsAction="always"/>
<item a:id="@+id/menu_plot_functions" <item a:id="@+id/menu_plot_functions"
a:title="@string/cpp_plot_functions" a:title="@string/cpp_plot_functions"
a:icon="@drawable/ab_list" a:icon="@drawable/ab_list"

View File

@ -184,6 +184,7 @@ public abstract class AbstractCalculatorPlotFragment extends CalculatorFragment
super.onCreateOptionsMenu(menu, inflater); super.onCreateOptionsMenu(menu, inflater);
final List<IdentifiableMenuItem<MenuItem>> menuItems = new ArrayList<IdentifiableMenuItem<MenuItem>>(); final List<IdentifiableMenuItem<MenuItem>> menuItems = new ArrayList<IdentifiableMenuItem<MenuItem>>();
menuItems.add(PlotMenu.range);
menuItems.add(PlotMenu.preferences); menuItems.add(PlotMenu.preferences);
menuItems.add(PlotMenu.functions); menuItems.add(PlotMenu.functions);
@ -307,6 +308,13 @@ public abstract class AbstractCalculatorPlotFragment extends CalculatorFragment
private static enum PlotMenu implements IdentifiableMenuItem<MenuItem> { private static enum PlotMenu implements IdentifiableMenuItem<MenuItem> {
range(R.id.menu_plot_range) {
@Override
public void onClick(@NotNull MenuItem data, @NotNull Context context) {
context.startActivity(new Intent(context, CalculatorPlotRangeActivity.class));
}
},
functions(R.id.menu_plot_functions) { functions(R.id.menu_plot_functions) {
@Override @Override
public void onClick(@NotNull MenuItem data, @NotNull Context context) { public void onClick(@NotNull MenuItem data, @NotNull Context context) {

View File

@ -4,12 +4,9 @@ import android.app.Activity;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button; import android.widget.Button;
import android.widget.SeekBar; import android.widget.SeekBar;
import android.widget.Spinner; import android.widget.Spinner;
@ -65,34 +62,12 @@ public class CalculatorPlotFunctionSettingsActivity extends SherlockFragmentActi
********************************************************************** **********************************************************************
*/ */
public static final int LINE_WIDTH_DELAY = 1000;
public static final int MESSAGE_ID = 1;
@Nullable @Nullable
private PlotFunction plotFunction; private PlotFunction plotFunction;
@NotNull @NotNull
private final CalculatorPlotter plotter = Locator.getInstance().getPlotter(); private final CalculatorPlotter plotter = Locator.getInstance().getPlotter();
@NotNull
private final Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
if (plotFunction != null) {
switch (msg.what) {
case MESSAGE_ID:
final PlotLineDef newPlotLineDef = PlotLineDef.changeLineWidth(plotFunction.getPlotLineDef(), msg.arg1);
final PlotFunction newPlotFunction = PlotFunction.changePlotLineDef(plotFunction, newPlotLineDef);
if(plotter.updateFunction(newPlotFunction)) {
plotFunction = newPlotFunction;
}
break;
}
}
}
};
public CalculatorPlotFunctionSettingsFragment() { public CalculatorPlotFunctionSettingsFragment() {
super(CalculatorFragmentType.plotter_function_settings); super(CalculatorFragmentType.plotter_function_settings);
} }
@ -123,95 +98,61 @@ public class CalculatorPlotFunctionSettingsActivity extends SherlockFragmentActi
final Spinner plotLineColorTypeSpinner = (Spinner) root.findViewById(R.id.cpp_plot_function_line_color_type_spinner); final Spinner plotLineColorTypeSpinner = (Spinner) root.findViewById(R.id.cpp_plot_function_line_color_type_spinner);
final Spinner plotLineStyleSpinner = (Spinner) root.findViewById(R.id.cpp_plot_function_line_style_spinner); final Spinner plotLineStyleSpinner = (Spinner) root.findViewById(R.id.cpp_plot_function_line_style_spinner);
final SeekBar plotLineWidthSeekBar = (SeekBar) root.findViewById(R.id.cpp_plot_functions_line_width_seekbar); final SeekBar plotLineWidthSeekBar = (SeekBar) root.findViewById(R.id.cpp_plot_functions_line_width_seekbar);
final Button okButton = (Button)root.findViewById(R.id.cpp_ok_button); final Button okButton = (Button) root.findViewById(R.id.cpp_apply_button);
plotLineWidthSeekBar.setMax(10); plotLineWidthSeekBar.setMax(10);
if (plotFunction != null) { if (plotFunction != null) {
plotLineWidthSeekBar.setProgress((int) plotFunction.getPlotLineDef().getLineWidth()); plotLineWidthSeekBar.setProgress((int) plotFunction.getPlotLineDef().getLineWidth());
plotLineColorSpinner.setSelection(PlotLineColor.valueOf(plotFunction.getPlotLineDef().getLineColor()).ordinal()); plotLineColorSpinner.setSelection(PlotLineColor.valueOf(plotFunction.getPlotLineDef().getLineColor()).ordinal());
plotLineColorSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
final PlotLineColor newPlotLineColor = PlotLineColor.values()[position];
int newLineColor = newPlotLineColor.getColor();
if ( newLineColor != plotFunction.getPlotLineDef().getLineColor() ) {
final PlotLineDef newPlotLineDef = PlotLineDef.changeLineColor(plotFunction.getPlotLineDef(), newLineColor);
final PlotFunction newPlotFunction = PlotFunction.changePlotLineDef(plotFunction, newPlotLineDef);
if(plotter.updateFunction(newPlotFunction)) {
plotFunction = newPlotFunction;
}
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
plotLineColorTypeSpinner.setSelection(plotFunction.getPlotLineDef().getLineColorType().ordinal()); plotLineColorTypeSpinner.setSelection(plotFunction.getPlotLineDef().getLineColorType().ordinal());
plotLineColorTypeSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
final PlotLineColorType newPlotLineColorType = PlotLineColorType.values()[position];
if ( newPlotLineColorType != plotFunction.getPlotLineDef().getLineColorType() ) {
final PlotLineDef newPlotLineDef = PlotLineDef.changeLineColorType(plotFunction.getPlotLineDef(), newPlotLineColorType);
final PlotFunction newPlotFunction = PlotFunction.changePlotLineDef(plotFunction, newPlotLineDef);
if(plotter.updateFunction(newPlotFunction)) {
plotFunction = newPlotFunction;
}
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
plotLineStyleSpinner.setSelection(plotFunction.getPlotLineDef().getLineStyle().ordinal()); plotLineStyleSpinner.setSelection(plotFunction.getPlotLineDef().getLineStyle().ordinal());
plotLineStyleSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
final PlotLineStyle newPlotLineStyle = PlotLineStyle.values()[position];
if ( newPlotLineStyle != plotFunction.getPlotLineDef().getLineStyle() ) {
final PlotLineDef newPlotLineDef = PlotLineDef.changeLineStyle(plotFunction.getPlotLineDef(), newPlotLineStyle);
final PlotFunction newPlotFunction = PlotFunction.changePlotLineDef(plotFunction, newPlotLineDef);
if(plotter.updateFunction(newPlotFunction)) {
plotFunction = newPlotFunction;
}
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
plotLineWidthSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
// remove old messages
handler.removeMessages(MESSAGE_ID);
// send new message
handler.sendMessageDelayed(Message.obtain(handler, MESSAGE_ID, progress, 0), LINE_WIDTH_DELAY);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
okButton.setOnClickListener(new View.OnClickListener() { okButton.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
PlotFunction plotFunction = CalculatorPlotFunctionSettingsFragment.this.plotFunction;
// color
final PlotLineColor newPlotLineColor = PlotLineColor.values()[plotLineColorSpinner.getSelectedItemPosition()];
int newLineColor = newPlotLineColor.getColor();
if (newLineColor != plotFunction.getPlotLineDef().getLineColor()) {
final PlotLineDef newPlotLineDef = PlotLineDef.changeLineColor(plotFunction.getPlotLineDef(), newLineColor);
plotFunction = PlotFunction.changePlotLineDef(plotFunction, newPlotLineDef);
}
// color type
final PlotLineColorType newPlotLineColorType = PlotLineColorType.values()[plotLineColorTypeSpinner.getSelectedItemPosition()];
if (newPlotLineColorType != CalculatorPlotFunctionSettingsFragment.this.plotFunction.getPlotLineDef().getLineColorType()) {
final PlotLineDef newPlotLineDef = PlotLineDef.changeLineColorType(plotFunction.getPlotLineDef(), newPlotLineColorType);
plotFunction = PlotFunction.changePlotLineDef(plotFunction, newPlotLineDef);
}
// line style
final PlotLineStyle newPlotLineStyle = PlotLineStyle.values()[plotLineStyleSpinner.getSelectedItemPosition()];
if (newPlotLineStyle != plotFunction.getPlotLineDef().getLineStyle()) {
final PlotLineDef newPlotLineDef = PlotLineDef.changeLineStyle(plotFunction.getPlotLineDef(), newPlotLineStyle);
plotFunction = PlotFunction.changePlotLineDef(plotFunction, newPlotLineDef);
}
// width
final int newPlotLineWidth = plotLineWidthSeekBar.getProgress();
if (((float) newPlotLineWidth) != plotFunction.getPlotLineDef().getLineWidth()) {
final PlotLineDef newPlotLineDef = PlotLineDef.changeLineWidth(plotFunction.getPlotLineDef(), newPlotLineWidth);
plotFunction = PlotFunction.changePlotLineDef(plotFunction, newPlotLineDef);
}
if (plotFunction != CalculatorPlotFunctionSettingsFragment.this.plotFunction) {
// if has been changed
if (plotter.updateFunction(plotFunction)) {
CalculatorPlotFunctionSettingsFragment.this.plotFunction = plotFunction;
}
}
final Activity activity = getActivity(); final Activity activity = getActivity();
if (activity != null) { if (activity != null) {
activity.finish(); activity.finish();

View File

@ -1,15 +1,13 @@
package org.solovyev.android.calculator.plot; package org.solovyev.android.calculator.plot;
import android.os.Bundle; import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View; import android.view.View;
import android.widget.EditText; import android.widget.EditText;
import com.actionbarsherlock.app.SherlockFragmentActivity; import com.actionbarsherlock.app.SherlockFragmentActivity;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.solovyev.android.calculator.CalculatorFragment;
import org.solovyev.android.calculator.CalculatorFragmentType; import org.solovyev.android.calculator.CalculatorFragmentType;
import org.solovyev.android.calculator.CalculatorListFragment;
import org.solovyev.android.calculator.Locator; import org.solovyev.android.calculator.Locator;
import org.solovyev.android.calculator.R; import org.solovyev.android.calculator.R;
import org.solovyev.android.fragments.FragmentUtils; import org.solovyev.android.fragments.FragmentUtils;
@ -31,7 +29,7 @@ public class CalculatorPlotRangeActivity extends SherlockFragmentActivity {
FragmentUtils.createFragment(this, CalculatorPlotRangeFragment.class, R.id.dialog_layout, "plot-range"); FragmentUtils.createFragment(this, CalculatorPlotRangeFragment.class, R.id.dialog_layout, "plot-range");
} }
public static class CalculatorPlotRangeFragment extends CalculatorListFragment { public static class CalculatorPlotRangeFragment extends CalculatorFragment {
public CalculatorPlotRangeFragment() { public CalculatorPlotRangeFragment() {
super(CalculatorFragmentType.plotter_range); super(CalculatorFragmentType.plotter_range);
@ -52,53 +50,29 @@ public class CalculatorPlotRangeActivity extends SherlockFragmentActivity {
xMinEditText.setText(String.valueOf(boundaries.getXMin())); xMinEditText.setText(String.valueOf(boundaries.getXMin()));
xMaxEditText.setText(String.valueOf(boundaries.getXMax())); xMaxEditText.setText(String.valueOf(boundaries.getXMax()));
xMinEditText.addTextChangedListener(new TextWatcher() { root.findViewById(R.id.cpp_apply_button).setOnClickListener(new View.OnClickListener() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
try {
final Float newXMin = Float.valueOf(s.toString());
plotter.setPlotBoundaries(PlotBoundaries.newInstance(newXMin, boundaries.getXMax()));
} catch (NumberFormatException e) {
Locator.getInstance().getNotifier().showMessage(R.string.cpp_invalid_number, MessageType.error);
xMinEditText.setText(String.valueOf(boundaries.getXMin()));
}
}
});
xMaxEditText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
try {
final Float newXMax = Float.valueOf(s.toString());
plotter.setPlotBoundaries(PlotBoundaries.newInstance(boundaries.getXMin(), newXMax));
} catch (NumberFormatException e) {
Locator.getInstance().getNotifier().showMessage(R.string.cpp_invalid_number, MessageType.error);
xMaxEditText.setText(String.valueOf(boundaries.getXMax()));
}
}
});
root.findViewById(R.id.cpp_ok_button).setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
try {
final Float xMin = Float.valueOf(xMinEditText.getText().toString());
final Float xMax = Float.valueOf(xMaxEditText.getText().toString());
if ( xMin.equals(xMax) ) {
throw new IllegalArgumentException();
}
plotter.setPlotBoundaries(PlotBoundaries.newInstance(xMin, xMax));
CalculatorPlotRangeFragment.this.getActivity().finish(); CalculatorPlotRangeFragment.this.getActivity().finish();
} catch (IllegalArgumentException e) {
if (e instanceof NumberFormatException) {
Locator.getInstance().getNotifier().showMessage(R.string.cpp_invalid_number, MessageType.error);
} else {
Locator.getInstance().getNotifier().showMessage(R.string.cpp_plot_boundaries_should_differ, MessageType.error);
}
}
} }
}); });
} }

View File

@ -25,10 +25,10 @@ public final class PlotBoundaries implements Serializable {
} }
PlotBoundaries(float xMin, float xMax, float yMin, float yMax) { PlotBoundaries(float xMin, float xMax, float yMin, float yMax) {
this.xMin = xMin; this.xMin = Math.min(xMin, xMax);
this.xMax = xMax; this.xMax = Math.max(xMin, xMax);
this.yMin = yMin; this.yMin = Math.min(yMin, yMax);
this.yMax = yMax; this.yMax = Math.max(yMin, yMax);
} }
@NotNull @NotNull