Skip to content

Commit

Permalink
Merge pull request #8 from zwim/answerVariable
Browse files Browse the repository at this point in the history
Answer variable
  • Loading branch information
zwim committed Jun 20, 2021
2 parents 42018d8 + c777473 commit 799ee06
Show file tree
Hide file tree
Showing 12 changed files with 465 additions and 152 deletions.
18 changes: 17 additions & 1 deletion ChangeLog
@@ -1,7 +1,23 @@
Version: 0.9.2
Bug: Variable ans works now

Added automatic load of "init.calc"
Added fileselector for loading and saving files
Added logical xor and not
Added bitwise or, and, nand and xor
Added %= operator
Better handling (editing) of formulas
Improved keyboard layout (popups)
Improved handling of greek letters
Alphabetically sorting of variables
Added Engineering mode (Exponentials multiply of three)
Better presentation of results (rounding, trimming)
Changed rounding to significant places

Version: 0.9.1
Use Unicode symbols instead of text (Translations!!!)
Typo in help text 3<4 -> true
Release package: Add `calculator.koplugin` folder
Release package: Add `calculator.koplugin` folder

Version: 0.9.0
Initial commit
5 changes: 4 additions & 1 deletion Makefile
@@ -1,5 +1,8 @@
VERSION=$(shell grep Version: VERSION | sed 's/Version: //g')


all: test clean zip

test:
cd formulaparser && lua parser-test.lua && cd ..

Expand All @@ -21,11 +24,11 @@ zip: clean
--exclude '*.swp' \
--exclude '*.zip' \
--exclude '*install-plugin.sh' \
--exclude '*.sh' \
--exclude 'tmp*' \
. tmp/calculator.koplugin

cd tmp && 7z a -l -mx9 -mfb=256 -mmt=on \
../calculator.koplugin-$(VERSION).zip *


all: test clean zip
26 changes: 16 additions & 10 deletions README.md
Expand Up @@ -4,7 +4,7 @@ A calculator plugin for KOReader.
## Installation
Go to https://github.com/zwim/calculator.koplugin/releases and download the desired `koreader.plugin-x.x.x.zip` and unpack it to `koreader/plugins/calculator.koplugin`.

If you want predefined physical constants, then move `init.calc` to `koreader/`, otherwise you can delete `init.calc`.
Predefined physical constants, are in `init.calc`. You can autoload them (see settings menu). If you don't need them delete `init.calc`.

The file `VERSION` is used for update-informations. If you delete it, you don't get a notification update.

Expand All @@ -13,17 +13,19 @@ The file `VERSION` is used for update-informations. If you delete it, you don't

You find the calculator in `More tools/calculator` additionally you can set a gesture (in the device submenu) to call it.

Enter a calculation and press `Calc`. You can see your inputs in lines starting with `ixxx:` and the results in lines with `oxxx:`.
Enter a calculation and press `Σ=`. You can see your inputs in lines starting with `ixxx:` and the results in lines with `oxxx:`.

If you change something in an old line, this line will be used for the next calculation.

The results are stored in variables `oxxx` and the last result is additional in the variable `ans`.
The results are stored in variables `o1, o2, ...` and the last result is additional in the variable `ans`.

All the entered calculations and the result can be saved to `koreader/output.calc`.
Most of the time you can skip the opening braces in a function (e.g. sin40+66 is replaces with sin(40+6); but sin40)+60 gives sin(40)+60; try it out).

Predefined expressions my be loaded from `koreader/init.calc`
All the entered calculations and the result can be saved with the `⇩'` button.

You find the settings in the Hamburger menu.
Predefined expressions my be loaded with the `` button.

You change the settings in the Hamburger menu.


![grafik](https://user-images.githubusercontent.com/36999612/121774303-3af12000-cb82-11eb-94c9-a0248b33c060.png)
Expand All @@ -32,15 +34,15 @@ You can set the output to different modi: For example if the evaluation yields 1

`Scientific` -> 1.23456789E+7

`Engineer` -> 12.3456789E+6 (not implemented yet)
`Engineer` -> 12.3456789E+6

`Auto` -> switches to Scientific if the absolute value is greater than 1000000 or less than 0.0001

`Native` -> show what lua calculates

`Programmer` -> show the result in `Auto` plus the value in HEX.

Additional the maximum number of decimal places can be set with `Round`.
Additional the maximum number of significant places can be set with `Significance ≈`. (e.g. significance set to 2: 0.0001234 -> 0.00012)

![grafik](https://user-images.githubusercontent.com/36999612/121774734-139b5280-cb84-11eb-8a80-85df783ea1f4.png)

Expand Down Expand Up @@ -79,7 +81,12 @@ The following operators are supported with increasing priority:
"?:" ternary like in C
"&&" logical and, the lua way
"||" logical or, the lua way
"!&" logical nand, the lua way
"##" logical nand, the lua way, -> logical not
"~~" logical nand, the lua way
"&" bitwise and
"|" bitwise or
"#" bitwise nand -> bitwise not
"~" bitwise nand
"<="
"=="
">="
Expand Down Expand Up @@ -135,4 +142,3 @@ Examples:
x=2,y=4 -> 4, set x=2 and y=4
1>2 || 2<10 && 7 -> 7
```

4 changes: 2 additions & 2 deletions VERSION
@@ -1,2 +1,2 @@
Version: 0.9.1
Date: 20210613
Version: 1.0.0
Date: 20210620
Binary file added calculator.koplugin-0.9.2.zip
Binary file not shown.
Binary file added calculator.koplugin-1.0.0.zip
Binary file not shown.
171 changes: 153 additions & 18 deletions calculatorsettingsdialog.lua
Expand Up @@ -11,7 +11,9 @@ local Geom = require("ui/geometry")
local HorizontalGroup = require("ui/widget/horizontalgroup")
local HorizontalSpan = require("ui/widget/horizontalspan")
local InputContainer = require("ui/widget/container/inputcontainer")
local InputDialog = require("ui/widget/inputdialog")
local LineWidget = require("ui/widget/linewidget")
local PathChooser = require("ui/widget/pathchooser")
local RadioButtonTable = require("ui/widget/radiobuttontable")
local Size = require("ui/size")
local TextWidget = require("ui/widget/textwidget")
Expand All @@ -20,6 +22,8 @@ local VerticalGroup = require("ui/widget/verticalgroup")
local VerticalSpan = require("ui/widget/verticalspan")
local _ = require("gettext")
local Screen = require("device").screen
local lfs = require("libs/libkoreader-lfs")
local util = require("util")

local Parser = require("formulaparser/formulaparser")

Expand Down Expand Up @@ -79,26 +83,51 @@ function CalculatorSettingsDialog:init()
})
end

local radio_buttons_round = {}
local radio_buttons_init = {}
table.insert(radio_buttons_init, {
{
text = "yes",
checked = self.parent.use_init_file == "yes",
provider = "yes",
},
})
table.insert(radio_buttons_init, {
{
text = "no",
checked = self.parent.use_init_file == "no",
provider = "no",
},
})

local radio_buttons_significant = {}
for i = 0,10 do
table.insert(radio_buttons_round, {
table.insert(radio_buttons_significant, {
{
text = i,
checked = self.parent.significant_places == i,
provider = i,
},
})
end
for i = 12,16,2 do
table.insert(radio_buttons_significant, {
{
text = i,
checked = self.parent.round_places == i,
checked = self.parent.significant_places == i,
provider = i,
},
})
end

table.insert(buttons, {
{
text = _("Cancel"),
text = "", --close
callback = function()
UIManager:close(self.parent.settings_dialog)
end,
},
{
text = _("Save"),
text ="", --ok
is_enter_default = true,
callback = function()
UIManager:close(self.parent.settings_dialog)
Expand All @@ -123,13 +152,19 @@ function CalculatorSettingsDialog:init()
self.parent.status_line = self.parent:getStatusLine()
end

local new_round = self.parent.settings_dialog.radio_button_table_round.checked_button.provider
if new_round ~= self.parent.round_places then
self.parent.round_places = new_round
G_reader_settings:saveSetting("calculator_round_places", new_round)
local new_significant = self.parent.settings_dialog.radio_button_table_significant.checked_button.provider
if new_significant ~= self.parent.significant_places then
self.parent.significant_places = new_significant
G_reader_settings:saveSetting("calculator_significant_places", new_significant)
self.parent.status_line = self.parent:getStatusLine()
end

local new_init_file = self.parent.settings_dialog.radio_button_table_init.checked_button.provider
if new_init_file ~= self.parent.use_init_file then
self.parent.use_init_file = new_init_file
G_reader_settings:saveSetting("calculator_use_init_file", new_init_file)
end

UIManager:close(self.parent.input_dialog)
self.parent:onCalculatorStart()
end,
Expand All @@ -154,8 +189,17 @@ function CalculatorSettingsDialog:init()
face = self.face,
}

self.radio_button_table_round = RadioButtonTable:new{
radio_buttons = radio_buttons_round,
self.radio_button_table_init = RadioButtonTable:new{
radio_buttons = radio_buttons_init,
width = math.floor(self.width * 0.4),
focused = true,
scroll = false,
parent = self,
face = self.face,
}

self.radio_button_table_significant = RadioButtonTable:new{
radio_buttons = radio_buttons_significant,
width = math.floor(self.width * 0.4),
focused = true,
scroll = false,
Expand Down Expand Up @@ -186,7 +230,7 @@ function CalculatorSettingsDialog:init()
HorizontalGroup:new{
dimen = Geom:new{
w = self.title_bar:getSize().w,
h = self.radio_button_table_round:getSize().h,
h = self.radio_button_table_significant:getSize().h,
},
VerticalGroup:new{ -- angle and format
align = "center",
Expand All @@ -202,7 +246,7 @@ function CalculatorSettingsDialog:init()
},
self.radio_button_table_angle,
},
VerticalSpan:new{width = Size.span.vertical_large*6},
VerticalSpan:new{width = Size.span.vertical_large*4},
TextWidget:new{
text = _("Number format"),
face = self.text_face,
Expand All @@ -214,22 +258,34 @@ function CalculatorSettingsDialog:init()
},
self.radio_button_table_format,
},
VerticalSpan:new{width = Size.span.vertical_large*4},
TextWidget:new{
text = _("Autoload\ninit.calc"),
face = self.text_face,
},
CenterContainer:new{
dimen = Geom:new{
w = self.title_bar:getSize().w * 0.4,
h = self.radio_button_table_init:getSize().h,
},
self.radio_button_table_init,
},
},
HorizontalSpan:new{width=self.title_bar:getSize().w * 0.1},
VerticalGroup:new{ -- rounding
VerticalGroup:new{ -- significance
align = "center",
-- round
-- significant
TextWidget:new{
text = _("Rounding"),
text = _("Significance"),
face = self.text_face,
},

CenterContainer:new{
dimen = Geom:new{
w = self.title_bar:getSize().w * 0.4,
h = self.radio_button_table_round:getSize().h,
h = self.radio_button_table_significant:getSize().h,
},
self.radio_button_table_round,
self.radio_button_table_significant,
},
},
},
Expand Down Expand Up @@ -267,4 +323,83 @@ function CalculatorSettingsDialog:onCloseWidget()
end)
end


--[[--
chooses a path or (an existing) file (borrowed from coverimage)
@touchmenu_instance for updating of the menu
@string key is the G_reader_setting key which is used and changed
@boolean folder_only just selects a path, no file handling
@boolean new_file allows to enter a new filename, or use just an existing file
@function migrate(a,b) callback to a function to mangle old folder/file with new folder/file.
Can be used for migrating the contents of the old path to the new one
]]
function CalculatorSettingsDialog:choosePathFile(touchmenu_instance, key, folder_only, new_file, migrate)
local old_path, dummy = util.splitFilePathName(self[key])
UIManager:show(PathChooser:new{
select_directory = folder_only or new_file,
select_file = not folder_only,
height = Screen:getHeight(),
path = old_path,
onConfirm = function(dir_path)
local mode = lfs.attributes(dir_path, "mode")
if folder_only then -- just select a folder
if not dir_path:find("/$") then
dir_path = dir_path .. "/"
end
if migrate then
migrate(self, self[key], dir_path)
end
self[key] = dir_path
G_reader_settings:saveSetting(key, dir_path)
if touchmenu_instance then
touchmenu_instance:updateItems()
end
elseif new_file and mode == "directory" then -- new filename should be entered or a file could be selected
local file_input
file_input = InputDialog:new{
title = _("Append filename"),
input = dir_path .. "/",
buttons = {{
{
text = _("Cancel"),
callback = function()
UIManager:close(file_input)
end,
},
{
text = _("Save"),
callback = function()
local file = file_input:getInputText()
print("xxxxxxxx " .. tostring(key) .. " " .. tostring(self[key]))
if migrate and self[key] and self[key] ~= "" then
print("xxxxxxxxxxxxxxxx migrate")
migrate(self, self[key], file)
end
self[key] = file
G_reader_settings:saveSetting(key, file)
if touchmenu_instance then
touchmenu_instance:updateItems()
end
UIManager:close(file_input)
end,
},
}},
}
UIManager:show(file_input)
file_input:onShowKeyboard()
elseif mode == "file" then -- just select an existing file
if migrate then
migrate(self, self[key], dir_path)
end
self[key] = dir_path
G_reader_settings:saveSetting(key, dir_path)
if touchmenu_instance then
touchmenu_instance:updateItems()
end
end
end,
})
end

return CalculatorSettingsDialog
2 changes: 1 addition & 1 deletion formulaparser
Submodule formulaparser updated from 8b94c5 to 5c2687

0 comments on commit 799ee06

Please sign in to comment.