Add memory

master
omame 2021-02-19 19:56:25 +01:00
parent c7da4aaf09
commit 78d4b89f79
No known key found for this signature in database
GPG Key ID: AFD5E32187DF8FC7
9 changed files with 219 additions and 4 deletions

View File

@ -16,6 +16,7 @@ find_package(Qt5 REQUIRED ${QT})
set(SRCS
main.cpp
calcengine.cpp
memory.cpp
engine/constants.cpp
engine/evaluator.cpp
engine/functions.cpp

View File

@ -24,6 +24,7 @@
#include <QLocale>
#include <QFile>
#include "calcengine.h"
#include "memory.h"
int main(int argc, char *argv[])
{
@ -31,6 +32,7 @@ int main(int argc, char *argv[])
QGuiApplication app(argc, argv);
qmlRegisterType<CalcEngine>("org.cyber.calculator", 1, 0, "CalcEngine");
qmlRegisterType<Memory>("org.cyber.calculator", 1, 0, "Memory");
QQmlApplicationEngine engine;

61
memory.cpp 100644
View File

@ -0,0 +1,61 @@
/*
* Copyright (C) 2021 CyberOS Team.
*
* Author: omame <me@omame.tech>
*
* 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 3 of the License, or
* 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. If not, see <http://www.gnu.org/licenses/>.
*/
#include "memory.h"
Memory::Memory(QObject *parent)
: QObject(parent),
m_text(QString(""))
{}
QString Memory::text() {
return m_text;
}
void Memory::setText(const QString &text) {
if (text != m_text) {
m_text = text;
emit textChanged();
}
}
void Memory::increase() {
bool success;
int temp = m_text.toInt(&success);
if (!success) {
qDebug() << "Failed to increase memory: String to int conversion failed.";
return;
}
temp += 1;
m_text = QString::number(temp);
emit textChanged();
}
void Memory::decrease() {
bool success;
if (m_text == "") return;
int temp = m_text.toInt(&success);
if (!success) {
qDebug() << "Failed to decrease memory: String to int conversion failed.";
return;
}
temp -= 1;
m_text = QString::number(temp);
emit textChanged();
}

50
memory.h 100644
View File

@ -0,0 +1,50 @@
/*
* Copyright (C) 2021 CyberOS Team.
*
* Author: omame <me@omame.tech>
*
* 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 3 of the License, or
* 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. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef MEMORY_H
#define MEMORY_H
#include <QObject>
#include <QString>
#include <QDebug>
#include <qqml.h>
class Memory : public QObject
{
Q_OBJECT
Q_PROPERTY(QString text READ text WRITE setText NOTIFY textChanged)
QML_ELEMENT
public:
explicit Memory(QObject *parent = nullptr);
QString text();
void setText(const QString &text);
Q_INVOKABLE void increase();
Q_INVOKABLE void decrease();
signals:
void textChanged();
private:
QString m_text;
};
#endif // MEMORY_H

View File

@ -4,5 +4,6 @@
<file>qml/ButtonsView.qml</file>
<file>qml/Zone.qml</file>
<file>qml/CTextField.qml</file>
<file>qml/MemoryButtonsView.qml</file>
</qresource>
</RCC>

View File

@ -24,7 +24,7 @@ Item {
model: buttonsView.labels
MouseArea {
id: buttonRect
id: buttonRect
width: buttonsView.width / grid.columns - Meui.Units.smallSpacing / 2
height: buttonsView.height / grid.rows - Meui.Units.smallSpacing / 2
onClicked: buttonsView.buttonClicked(targets[index])

View File

@ -0,0 +1,67 @@
import QtQuick 2.9
import QtQuick.Layouts 1.3
import QtQuick.Controls 2.5
import MeuiKit 1.0 as Meui
Item {
id: buttonsView
property var labels
property var targets
property int rowsCount: 1
signal buttonClicked(string strToAppend)
signal buttonLongPressed(string strToAppend)
Grid {
id: grid
anchors.centerIn: parent
anchors.margins: Meui.Units.smallSpacing
columns: getColumnsCount()
rows: buttonsView.rowsCount
Repeater {
model: buttonsView.labels
MouseArea {
id: buttonRect
width: buttonsView.width / grid.columns - Meui.Units.smallSpacing / 2
height: buttonsView.height / grid.rows - Meui.Units.smallSpacing / 2
onClicked: buttonsView.buttonClicked(targets[index])
onPressAndHold: buttonsView.buttonLongPressed(targets[index])
Rectangle {
anchors.centerIn: parent
radius: Meui.Theme.smallRadius
width: parent.width - radius
height: parent.height - radius
color: buttonRect.pressed ? Meui.Theme.highlightColor : Meui.Theme.backgroundColor
border.width: 1
border.color: Meui.Theme.darkMode ? Qt.lighter(Meui.Theme.backgroundColor, 1.1) : Qt.darker(Meui.Theme.backgroundColor, 1.1)
Behavior on color {
ColorAnimation {
duration: 50
}
}
}
Text {
anchors.fill: parent
text: modelData
horizontalAlignment: Qt.AlignHCenter
verticalAlignment: Qt.AlignVCenter
fontSizeMode: Text.Fit
minimumPointSize: Math.round(buttonRect.height / 5)
font.pointSize: Math.round(buttonRect.height / 5)
color: buttonRect.pressed ? Meui.Theme.highlightedTextColor : Meui.Theme.textColor
}
}
}
}
function getColumnsCount() {
return Math.ceil(buttonsView.labels.length / buttonsView.rowsCount);
}
}

View File

@ -3,6 +3,7 @@ import QtQuick.Layouts 1.3
import QtQuick.Controls 2.5
import QtQuick.Controls.Styles 1.4
import MeuiKit 1.0 as Meui
import org.cyber.calculator 1.0
Item {
id: zone
@ -61,6 +62,10 @@ Item {
}
}
Memory {
id: memory
}
function appendToTextField(text) {
if (text === '=') {
var res = calculate(textField.text)
@ -83,6 +88,26 @@ Item {
} else if (text === 'BACK') {
// backspace
textField.remove(textField.cursorPosition, textField.cursorPosition - 1)
} else if (text.startsWith('M')) {
switch(text) {
case 'MC':
memory.text = ""
break
case 'MR':
textField.insert(textField.cursorPosition, memory.text)
break
case 'M+':
memory.increase()
break
case 'M-':
memory.decrease()
break
case 'MS':
var res = calculate(textField.text)
if (res == "") break
memory.text = res
break
}
} else {
textField.insert(textField.cursorPosition, text)
}

View File

@ -7,9 +7,9 @@ import MeuiKit 1.0 as Meui
Meui.Window {
visible: true
width: 350
height: 550
height: 650
minimumWidth: 350
minimumHeight: 550
minimumHeight: 650
title: qsTr("Calculator")
id: rootWindow
@ -48,6 +48,15 @@ Meui.Window {
Layout.preferredHeight: parent.height * 0.35
}
MemoryButtonsView {
id: memoryButtons
Layout.fillWidth: true
Layout.preferredHeight: 48
labels: ['MC', 'MR', 'M+', 'M-', 'MS']
targets: ['MC', 'MR', 'M+', 'M-', 'MS']
onButtonClicked: zone.appendToTextField(strToAppend)
}
ButtonsView {
id: buttons
Layout.fillWidth: true
@ -62,5 +71,4 @@ Meui.Window {
var res = calcEngine.eval(evalText)
return res
}
}