Skip to content

Commit

Permalink
Правки "калькулятора"
Browse files Browse the repository at this point in the history
  • Loading branch information
XRay3D committed Mar 17, 2024
1 parent 83fab7c commit 0365749
Show file tree
Hide file tree
Showing 10 changed files with 1,043 additions and 327 deletions.
1 change: 1 addition & 0 deletions plugins/file/gerber/gbr_aperture.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
*******************************************************************************/
#pragma once
#include "gbr_types.h"
#define MT 3
#include "mathparser.h"

#include <QtMath>
Expand Down
10 changes: 6 additions & 4 deletions plugins/gcode/pocketoffset/pocketoffset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,10 +236,11 @@ File::File(GCode::Params&& gcp, Pathss&& toolPathss, Paths&& pocketPaths)
}

void File::genGcodeAndTile() {
const QRectF rect = App::project().worckRect();
for(size_t x = 0; x < App::project().stepsX(); ++x) {
for(size_t y = 0; y < App::project().stepsY(); ++y) {
const QPointF offset((rect.width() + App::project().spaceX()) * x, (rect.height() + App::project().spaceY()) * y);
auto& proj = App::project();
const QRectF rect = proj.worckRect();
for(size_t x{}; x < proj.stepsX(); ++x) {
for(size_t y{}; y < proj.stepsY(); ++y) {
const QPointF offset{(rect.width() + proj.spaceX()) * x, (rect.height() + proj.spaceY()) * y};
if(toolType() == Tool::Laser)
saveLaserPocket(offset);
else
Expand All @@ -265,4 +266,5 @@ void File::createGi() {

itemGroup()->setVisible(true);
}

} // namespace PocketOffset
5 changes: 3 additions & 2 deletions static_libs/common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ set(CMAKE_AUTORCC ON)

find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Core Gui Widgets REQUIRED)

file(GLOB_RECURSE HEADERS ${CMAKE_CURRENT_SOURCE_DIR}/*.h)
file(GLOB_RECURSE SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp)
file(GLOB HEADERS ${CMAKE_CURRENT_SOURCE_DIR}/*.h)
file(GLOB SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp)

add_library(common STATIC
${HEADERS}
Expand All @@ -43,6 +43,7 @@ target_link_libraries(common PRIVATE
Qt${QT_VERSION_MAJOR}::Widgets
)

add_subdirectory(doublespinbox)

#add_module_library(QT qt.cpp)

Expand Down
65 changes: 32 additions & 33 deletions static_libs/common/app.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,37 +23,38 @@
#include <assert.h>
#include <map>

namespace Shapes {
class Plugin;
class Handle;
} // namespace Shapes
class AbstractFilePlugin;

namespace Drilling {
class Form;
} // namespace Drilling

namespace FileTree {
class View;
class Model;
} // namespace FileTree

namespace GCode {
class Plugin;
class PropertiesForm;
class Settings;
} // namespace GCode

class AbstractFilePlugin;
namespace GCodeShapes {
class Plugin;
class Handle;
} // namespace GCodeShapes

namespace FileTree {
class View;
class Model;
} // namespace FileTree

namespace Drilling {
class Form;
} // namespace Drilling

namespace Gi {
class Marker;
class Pin;
} // namespace Gi

namespace Shapes {
class Plugin;
class Handle;
} // namespace Shapes

using Handlers = mvector<Shapes::Handle*>;

using FilePluginMap = std::map<uint32_t, AbstractFilePlugin*, std::less<>>;
Expand Down Expand Up @@ -85,26 +86,24 @@ class App {
inline static App* app{};

// clang-format off
SINGLETON(Drilling::Form, setDrillForm, drillForm )
SINGLETON(FileTree::Model, setFileModel, fileModel )
SINGLETON(FileTree::View, setFileTreeView, fileTreeView )
SINGLETON(GCode::PropertiesForm, setGCodePropertiesForm, gcPropertiesForm)
SINGLETON(QUndoStack, setUndoStack, undoStack )
SINGLETON(GraphicsView, setGraphicsView, grView )
SINGLETON(LayoutFrames, setLayoutFrames, layoutFrames )
SINGLETON(MainWindow, setMainWindow, mainWindow )
SINGLETON(Project, setProject, project )
SINGLETON(QSplashScreen, setSplashScreen, splashScreen )
SINGLETON(GCode::Settings, setGcSettings, gcSettings )

SINGLETON(Gi::Marker, setHome, home )
SINGLETON(Gi::Marker, setZero, zero )

SINGLETON(Gi::Pin, setPin0, pin0 )
SINGLETON(Gi::Pin, setPin1, pin1 )
SINGLETON(Gi::Pin, setPin2, pin2 )
SINGLETON(Gi::Pin, setPin3, pin3 )

SINGLETON(Drilling::Form, setDrillForm, drillForm )
SINGLETON(FileTree::Model, setFileModel, fileModel )
SINGLETON(FileTree::View, setFileTreeView, fileTreeView )
SINGLETON(GCode::Settings, setGcSettings, gcSettings )
SINGLETON(GraphicsView, setGraphicsView, grView )
SINGLETON(LayoutFrames, setLayoutFrames, layoutFrames )
SINGLETON(MainWindow, setMainWindow, mainWindow )
SINGLETON(Project, setProject, project )
SINGLETON(QSplashScreen, setSplashScreen, splashScreen )
SINGLETON(QUndoStack, setUndoStack, undoStack )

SINGLETON(Gi::Marker, setHome, home)
SINGLETON(Gi::Marker, setZero, zero)
SINGLETON(Gi::Pin, setPin0, pin0)
SINGLETON(Gi::Pin, setPin1, pin1)
SINGLETON(Gi::Pin, setPin2, pin2)
SINGLETON(Gi::Pin, setPin3, pin3)
// clang-format on

FilePluginMap filePlugins_;
Expand Down
150 changes: 134 additions & 16 deletions static_libs/common/doublespinbox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,100 @@
#include <cmath>
#include <numbers>

#if 0
struct Validator : QValidator {
using QValidator::QValidator;
Validator(QJSEngine* jsEngine, QObject* parent = nullptr)
: QValidator{parent}
, jsEngine{jsEngine} { }
QJSEngine* const jsEngine;
static auto fixMath(QString str) {
QElapsedTimer timer;
timer.start();
if constexpr(0) {
static const std::pair<QString, QString> array[]{
{ "E", "Math.E"}, // const
{ "LN10", "Math.LN10"}, // const
{ "LN2", "Math.LN2"}, // const
{ "LOG10E", "Math.LOG10E"}, // const
{ "LOG2E", "Math.LOG2E"}, // const
{ "PI", "Math.PI"}, // const
{"SQRT1_2", "Math.SQRT1_2"}, // const
{ "SQRT2", "Math.SQRT2"}, // const
{ "abs", "Math.abs"}, // func
{ "acos", "Math.acos"}, // func
{ "acosh", "Math.acosh"}, // func
{ "asin", "Math.asin"}, // func
{ "asinh", "Math.asinh"}, // func
{ "atan", "Math.atan"}, // func
{ "atan2", "Math.atan2"}, // func
{ "atanh", "Math.atanh"}, // func
{ "cbrt", "Math.cbrt"}, // func
{ "ceil", "Math.ceil"}, // func
{ "clz32", "Math.clz32"}, // func
{ "cos", "Math.cos"}, // func
{ "cosh", "Math.cosh"}, // func
{ "exp", "Math.exp"}, // func
{ "expm1", "Math.expm1"}, // func
{ "floor", "Math.floor"}, // func
{ "fround", "Math.fround"}, // func
{ "hypot", "Math.hypot"}, // func
{ "imul", "Math.imul"}, // func
{ "log", "Math.log"}, // func
{ "log10", "Math.log10"}, // func
{ "log1p", "Math.log1p"}, // func
{ "log2", "Math.log2"}, // func
{ "max", "Math.max"}, // func
{ "min", "Math.min"}, // func
{ "pow", "Math.pow"}, // func
{ "random", "Math.random"}, // func
{ "round", "Math.round"}, // func
{ "sign", "Math.sign"}, // func
{ "sin", "Math.sin"}, // func
{ "sinh", "Math.sinh"}, // func
{ "sqrt", "Math.sqrt"}, // func
{ "tan", "Math.tan"}, // func
{ "tanh", "Math.tanh"}, // func
{ "trunc", "Math.trunc"}, // func
};
for(auto&& [from, to]: array)
str.replace(from, to, Qt::CaseInsensitive);
} else {
static QRegularExpression re{
"(E|LN10|LN2|LOG10E|LOG2E|PI|SQRT1_2|SQRT2|"
"abs|acos|acosh|asin|asinh|atan|atan2|atanh|"
"cbrt|ceil|clz32|cos|cosh|exp|expm1|floor|"
"fround|hypot|imul|log|log10|log1p|log2|max|"
"min|pow|random|round|sign|sin|sinh|sqrt|tan|tanh|trunc)",
QRegularExpression::CaseInsensitiveOption};
str.replace(re, R"(Math.\1)");
}
qWarning() << timer.nsecsElapsed() / 1000 << "us" << str;
return str;
}
const std::set<QChar> set{'.', '/', '*', '-', '+'};
// QValidator interface
State validate(QString& str, int& pos) const override {
// Invalid 0 Строка явно недействительна.
// Intermediate 1 Строка является вероятным промежуточным значением.
// Acceptable 2 Строка приемлема в качестве конечного результата; то есть это действительно.
if(str.size() == 0) return Intermediate;
if(str.count('(') != str.count(')')) return Intermediate;
if(pos) { // skip duplicates
auto ch = str[pos - 1];
if(pos > 1 && set.contains(ch) && str[pos - 2] == ch)
return Invalid;
if(pos < str.size() && set.contains(ch) && str[pos] == ch)
return Invalid;
}
auto val = jsEngine->evaluate(fixMath(str));
if(val.errorType())
qDebug() << val.toString();
return val.errorType() ? Intermediate : Acceptable;
}
};
#endif

DoubleSpinBox::DoubleSpinBox(QWidget* parent)
: QDoubleSpinBox(parent) {
lineEdit()->installEventFilter(this);
Expand Down Expand Up @@ -82,23 +176,44 @@ double DoubleSpinBox::valueFromText(const QString& text) const {
str = text.mid(0, text.size() - suffix().size());
double val{};
try {
static VarMap varmap{
{ "e", std::numbers::e},
{ "egamma", std::numbers::egamma},
{ "inv_pi", std::numbers::inv_pi},
{ "inv_sqrt3", std::numbers::inv_sqrt3},
{"inv_sqrtpi", std::numbers::inv_sqrtpi},
{ "ln10", std::numbers::ln10},
{ "ln2", std::numbers::ln2},
{ "log10e", std::numbers::log10e},
{ "log2e", std::numbers::log2e},
{ "phi", std::numbers::phi},
{ "pi", std::numbers::pi},
{ "sqrt2", std::numbers::sqrt2},
{ "sqrt3", std::numbers::sqrt3},
// static VarMap varmap{
// { "e", std::numbers::e},
// { "egamma", std::numbers::egamma},
// { "inv_pi", std::numbers::inv_pi},
// { "inv_sqrt3", std::numbers::inv_sqrt3},
// {"inv_sqrtpi", std::numbers::inv_sqrtpi},
// { "ln10", std::numbers::ln10},
// { "ln2", std::numbers::ln2},
// { "log10e", std::numbers::log10e},
// { "log2e", std::numbers::log2e},
// { "phi", std::numbers::phi},
// { "pi", std::numbers::pi},
// { "sqrt2", std::numbers::sqrt2},
// { "sqrt3", std::numbers::sqrt3},
// };
// if(str.size())
// val = MathParser(&varmap).parse(str.replace(',', '.'));

static MathParser parser{
{
{"e", std::numbers::e},
{"egamma", std::numbers::egamma},
{"inv_pi", std::numbers::inv_pi},
{"inv_sqrt3", std::numbers::inv_sqrt3},
{"inv_sqrtpi", std::numbers::inv_sqrtpi},
{"ln10", std::numbers::ln10},
{"ln2", std::numbers::ln2},
{"log10e", std::numbers::log10e},
{"log2e", std::numbers::log2e},
{"phi", std::numbers::phi},
{"pi", std::numbers::pi},
{"sqrt2", std::numbers::sqrt2},
{"sqrt3", std::numbers::sqrt3},
}
};
if(str.size())
val = MathParser(&varmap).parse(str.replace(',', '.'));
val = parser.parse(str.replace(',', '.').toStdString());

if(std::isnan(val) || std::isinf(val))
return value();
} catch(...) { } //
Expand All @@ -110,7 +225,10 @@ QString DoubleSpinBox::textFromValue(double value) const {
}

QValidator::State DoubleSpinBox::validate(QString& input, int& pos) const {
// qDebug() << input << pos;
// bool ok{};
// QString{input}.toDouble(&ok);
// qInfo() << input << pos;
// return ok ? QValidator::Intermediate : QValidator::Acceptable;
return QValidator::Acceptable;
}

Expand Down
76 changes: 76 additions & 0 deletions static_libs/common/doublespinbox/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
cmake_minimum_required(VERSION 3.5)

project(doublespinbox VERSION 0.1 LANGUAGES CXX)

set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Widgets)
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Widgets)

set(PROJECT_SOURCES
main.cpp
../doublespinbox.cpp
../doublespinbox.h
../mathparser.cpp
../mathparser.h
# widget.cpp
# widget.h
# widget.ui
)

include_directories(../)

if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)
qt_add_executable(doublespinbox
MANUAL_FINALIZATION
${PROJECT_SOURCES}
)
# Define target properties for Android with Qt 6 as:
# set_property(TARGET doublespinbox APPEND PROPERTY QT_ANDROID_PACKAGE_SOURCE_DIR
# ${CMAKE_CURRENT_SOURCE_DIR}/android)
# For more information, see https://doc.qt.io/qt-6/qt-add-executable.html#target-creation
else()
if(ANDROID)
add_library(doublespinbox SHARED
${PROJECT_SOURCES}
)
# Define properties for Android with Qt 5 after find_package() calls as:
# set(ANDROID_PACKAGE_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/android")
else()
add_executable(doublespinbox
${PROJECT_SOURCES}
)
endif()
endif()

target_link_libraries(doublespinbox PRIVATE Qt${QT_VERSION_MAJOR}::Widgets)

# Qt for iOS sets MACOSX_BUNDLE_GUI_IDENTIFIER automatically since Qt 6.1.
# If you are developing for iOS or macOS you should consider setting an
# explicit, fixed bundle identifier manually though.
if(${QT_VERSION} VERSION_LESS 6.1.0)
set(BUNDLE_ID_OPTION MACOSX_BUNDLE_GUI_IDENTIFIER com.example.doublespinbox)
endif()
set_target_properties(doublespinbox PROPERTIES
${BUNDLE_ID_OPTION}
MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}
MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
MACOSX_BUNDLE TRUE
WIN32_EXECUTABLE TRUE
)

include(GNUInstallDirs)
install(TARGETS doublespinbox
BUNDLE DESTINATION .
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)

if(QT_VERSION_MAJOR EQUAL 6)
qt_finalize_executable(doublespinbox)
endif()
Loading

0 comments on commit 0365749

Please sign in to comment.