Skip to content

Commit

Permalink
Popup displays autocalc result or error message.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikko Syrjä committed Sep 13, 2018
1 parent e8d13b9 commit 5c8b763
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 45 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,8 @@ results are stored there. Pointing history list line inserts result to the expre
Pointing and holding history line recalls the whole expression for editing.

Below the history list is expression editing field. It can be edited either by calculator key panes
or normal virtual keyboard.

xxx Below the expression editor is label field used for function usage parameters and autocalc results.
or normal virtual keyboard. Popup message above edit field displays current result or possible
error message.

#### Keyboard and pulley menu

Expand Down
41 changes: 33 additions & 8 deletions android-speedcrunch.pro
Original file line number Diff line number Diff line change
@@ -1,16 +1,35 @@
# This file is part of the SpeedCrunch project
# Copyright (C) 2014 @qwazix
# Copyright (C) 2018 Mikko Syrjä
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; see the file COPYING. If not, write to
# the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301, USA.

QT += quick qml
CONFIG += c++11

# PKGCONFIG += sailfishapp
# INCLUDEPATH += /usr/include/sailfishapp
# The following define makes your compiler emit warnings if you use
# any feature of Qt which as been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

#TARGETPATH = /usr/bin
#target.path = $$TARGETPATH

#DEPLOYMENT_PATH = /usr/share/$$TARGET
#qml.files = qml
#qml.path = $$DEPLOYMENT_PATH
# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0

DEFINES += SPEEDCRUNCH_VERSION=\\\"master\\\"

Expand Down Expand Up @@ -121,6 +140,12 @@ DISTFILES += \

RESOURCES += qml.qrc

# Additional import path used to resolve QML modules in Qt Creator's code model
QML_IMPORT_PATH =

# Additional import path used to resolve QML modules just for Qt Quick Designer
QML_DESIGNER_IMPORT_PATH =

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
Expand Down
40 changes: 20 additions & 20 deletions qml/Calculator.qml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import QtQuick.Controls 2.3
Page
{
property int keyboardheight: parent.height * (landscape ? 60 : 45) / 100
property string notification: ""

property alias history: historyview
property alias editor: textfield
Expand Down Expand Up @@ -118,6 +119,23 @@ Page
inputMethodHints: Qt.ImhNoPredictiveText | Qt.ImhNoAutoUppercase;
placeholderText: "expression"
cursorVisible: true
ToolTip
{
parent: textfield
x: parent.x
font.pixelSize: fontsizelist
closePolicy: Popup.NoAutoClose
visible: (swipe.currentIndex === 1 && notification !== "" && textfield.text !== "")
text: notification
}
onTextChanged:
{
var result = manager.autoCalc(text);
if ( manager.autoCalc(text) !== "NaN" )
notification = "= " + result
else
notification = manager.getError()
}
Keys.onReturnPressed: { setDefaultFocus() }
}
Rectangle
Expand Down Expand Up @@ -209,29 +227,11 @@ Page
if ( textfield.text !== "" )
{
var result = manager.calculate(textfield.text)
var assign = manager.getAssignId()
if ( result === "NaN" )
{
var error = manager.getError()
if ( error.length )
{
// notification.previewSummary = "Evaluation error"
// notification.previewBody = error
}
else if ( assign.length )
{
latestExpression = manager.autoFix(textfield.text)
latestResult = ""
historyview.updateHistory()
functions.updateFunctions()
// notification.previewSummary = "Function added"
// notification.previewBody = ""
textfield.text = ""
}
// notification.publish()
}
notification = manager.getError()
else
{
var assign = manager.getAssignId()
latestExpression = manager.autoFix(textfield.text)
latestResult = result
historyview.updateHistory()
Expand Down
12 changes: 9 additions & 3 deletions qml/Functions.qml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,13 @@ Page
background: Rectangle { radius: cornerradius; color: settingscolor }
font.pixelSize: fontsize
model: [ "All", "Functions", "Units", "Constants", "User defined" ]
delegate: ItemDelegate { text: modelData; font.pixelSize: fontsize }
delegate: ItemDelegate
{
text: modelData;
width: parent.width
font.pixelSize: fontsize
highlighted: ListView.isCurrentItem
}
onCurrentIndexChanged:
{
if ( currentIndex == 0 ) { filtertype = "a" }
Expand Down Expand Up @@ -139,8 +145,8 @@ Page
functionlist.currentIndex = -1
if ( popupmenu.opened )
popupmenu.close()
else if ( acceptclic )
insert()
// else if ( acceptclic )
// insert()
}
onPressed:
{
Expand Down
40 changes: 35 additions & 5 deletions qml/Settings.qml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,13 @@ Page
"Scientific decimal", "Binary", "Octal", "Hexadecimal" ]
// model: [ "General decimal", "Fixed decimal", "Engineering decimal",
// "Scientific decimal", "Binary", "Octal", "Hexadecimal", "Sexagesimal" ]
delegate: ItemDelegate { text: modelData; font.pixelSize: fontsize }
delegate: ItemDelegate
{
text: modelData;
width: parent.width
font.pixelSize: fontsize
highlighted: ListView.isCurrentItem
}
onCurrentIndexChanged:
{
if ( initialized )
Expand Down Expand Up @@ -87,7 +93,13 @@ Page
background: Rectangle { radius: cornerradius; color: settingscolor }
font.pixelSize: fontsize
model: [ "Automatic", "0", "1", "2", "3", "4", "6", "8", "12", "16", "20" ]
delegate: ItemDelegate { text: modelData; font.pixelSize: fontsize }
delegate: ItemDelegate
{
text: modelData;
width: parent.width
font.pixelSize: fontsize
highlighted: ListView.isCurrentItem
}
onCurrentIndexChanged:
{
if ( initialized ) switch ( currentIndex )
Expand Down Expand Up @@ -139,7 +151,13 @@ Page
font.pixelSize: fontsize
model: [ "Degree", "Radian" ]
// model: [ "Degree", "Radian", "Gradian" ]
delegate: ItemDelegate { text: modelData; font.pixelSize: fontsize }
delegate: ItemDelegate
{
text: modelData;
width: parent.width
font.pixelSize: fontsize
highlighted: ListView.isCurrentItem
}
onCurrentIndexChanged:
{
if ( initialized )
Expand Down Expand Up @@ -173,7 +191,13 @@ Page
background: Rectangle { radius: cornerradius; color: settingscolor }
font.pixelSize: fontsize
model: [ "Disabled", "Cartesian", "Polar" ]
delegate: ItemDelegate { text: modelData; font.pixelSize: fontsize }
delegate: ItemDelegate
{
text: modelData;
width: parent.width
font.pixelSize: fontsize
highlighted: ListView.isCurrentItem
}
onCurrentIndexChanged:
{
if ( initialized )
Expand Down Expand Up @@ -219,7 +243,13 @@ Page
background: Rectangle { radius: cornerradius; color: settingscolor }
font.pixelSize: fontsize
model: [ "Small", "Medium", "Large" ]
delegate: ItemDelegate { text: modelData; font.pixelSize: fontsize }
delegate: ItemDelegate
{
text: modelData;
width: parent.width
font.pixelSize: fontsize
highlighted: ListView.isCurrentItem
}
onCurrentIndexChanged:
{
if ( initialized )
Expand Down
2 changes: 2 additions & 0 deletions qml/android-speedcrunch.qml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ ApplicationWindow
property int cornerradius: width / buttoncols / 15
property int itemspacing: width / buttoncols / 15

property alias functions: functions
property alias history: calculator.history
property alias editor: calculator.editor
property alias keyboard: calculator.keyboard
Expand Down Expand Up @@ -119,6 +120,7 @@ ApplicationWindow
editor.cursorPosition = pos + value.length
}
}
// MenuSeparator { }
MenuItem
{
text: qsTr("Clear history")
Expand Down
16 changes: 10 additions & 6 deletions src/manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,14 +136,14 @@ void Manager::saveSession()
//! Auto calculate expression.
/*!
\param input Expression.
\return Result string.
\return Result string or NaN for error.
*/
QString Manager::autoCalc(const QString& input)
{
const QString expression = evaluator->autoFix(input);
evaluator->setExpression(expression);
Quantity quantity = evaluator->evalNoAssign();
if ( quantity.isNan() )
if ( quantity.isNan() || !evaluator->error().isEmpty() )
return "NaN";
return NumberFormatter::format(quantity);
}
Expand All @@ -161,20 +161,22 @@ QString Manager::autoFix(const QString& input)
//! Calculate expression.
/*!
\param input Expression.
\return Result string.
\return Result string or NaN for error.
*/
QString Manager::calculate(const QString& input)
{
const QString expression = evaluator->autoFix(input);
QString expression = evaluator->autoFix(input);
evaluator->setExpression(expression);
Quantity quantity = evaluator->evalUpdateAns();
if ( !evaluator->error().isEmpty() )
return "NaN";
if ( quantity.isNan() )
{
if ( evaluator->isUserFunctionAssign() )
{
updateRecent(evaluator->getAssignId() + "()");
if ( evaluator->error().isEmpty() )
session->addHistoryEntry(HistoryEntry(expression, quantity));
session->addHistoryEntry(HistoryEntry(expression, quantity));
return "0";
}
return "NaN";
}
Expand All @@ -196,6 +198,8 @@ QString Manager::getError()
{
QString error = evaluator->error();
error.remove("<b>").remove("</b>");
if ( error.isEmpty() && !evaluator->isValid() )
error = "compile error";
return error;
}

Expand Down

0 comments on commit 5c8b763

Please sign in to comment.