~mrp/aerodramus

ea167b3f19e9dedf3a3c77a0fa5729465791c0c7 — Mark Penner 6 months ago 84613be
add calculator page
M src/CMakeLists.txt => src/CMakeLists.txt +1 -0
@@ 2,6 2,7 @@
# SPDX-License-Identifier: BSD-2-Clause

add_executable(aerodramus-gui
    CalculatorPage.cpp
    ImpulseResponsePage.cpp
    main.cpp
    MainWindow.cpp

A src/CalculatorPage.cpp => src/CalculatorPage.cpp +72 -0
@@ 0,0 1,72 @@
// SPDX-FileCopyrightText: 2023 Mark Penner <mrpenner@mailbox.org>
// SPDX-License-Identifier: GPL-3.0-or-later

#include "CalculatorPage.h"
#include "lib/aerodramus.h"
#include <KLocalizedString>
#include <QFormLayout>
#include <QPushButton>
#include <QStatusBar>

CalculatorPage::CalculatorPage(QWidget *parent, KXmlGuiWindow *mainWindow)
    : QWidget(parent)
    , m_mainWindow{mainWindow}
    , m_sensitivitySpinBox{new QDoubleSpinBox}
    , m_powerSpinBox{new QDoubleSpinBox}
    , m_distanceSpinBox{new QDoubleSpinBox}
    , m_dBSpinBox{new QDoubleSpinBox}
{
    QFormLayout *formLayout{new QFormLayout()};

    m_sensitivitySpinBox->setRange(0.0, 200.0);
    m_sensitivitySpinBox->setSingleStep(0.1);
    m_sensitivitySpinBox->setSuffix(QStringLiteral("dB"));
    formLayout->addRow(i18n("Sensitivity (1W @ 1m)"), m_sensitivitySpinBox);

    m_powerSpinBox->setRange(0.0, 10000.0);
    m_powerSpinBox->setSingleStep(10.0);
    m_powerSpinBox->setSuffix(QStringLiteral("W"));
    formLayout->addRow(i18n("Power"), m_powerSpinBox);

    m_distanceSpinBox->setRange(0.0, 10000.0);
    m_distanceSpinBox->setSuffix(QStringLiteral("m"));
    formLayout->addRow(i18n("Distance"), m_distanceSpinBox);

    m_dBSpinBox->setRange(0.0, 200.0);
    m_dBSpinBox->setSuffix(QStringLiteral("dB"));
    formLayout->addRow(i18n("dB @ distance"), m_dBSpinBox);

    QPushButton *calculateButton{new QPushButton(i18n("Calculate"), this)};
    connect(calculateButton, SIGNAL(clicked()), this, SLOT(calculate()));
    formLayout->addRow(calculateButton);

    this->setLayout(formLayout);
}

void CalculatorPage::calculate()
{
    // check if only one field is 0, if so, calculate that field
    enum { Sensitivity = 1, Power = 2, Distance = 4, dB = 8 };
    int emptySensitivity = static_cast<bool>(m_sensitivitySpinBox->value()) ? 0 : Sensitivity;
    int emptyPower = static_cast<bool>(m_powerSpinBox->value()) ? 0 : Power;
    int emptyDistance = static_cast<bool>(m_distanceSpinBox->value()) ? 0 : Distance;
    int emptyDB = static_cast<bool>(m_dBSpinBox->value()) ? 0 : dB;
    int whichFieldEmpty = emptySensitivity | emptyPower | emptyDistance | emptyDB;
    switch (whichFieldEmpty) {
    case Sensitivity:
        m_mainWindow->statusBar()->showMessage(i18n("Not implemented"), 5000);
        break;
    case Power:
        m_powerSpinBox->setValue(Aerodramus::watts_needed_to_point_source(m_sensitivitySpinBox->value(), m_distanceSpinBox->value(), m_dBSpinBox->value()));
        break;
    case Distance:
        m_mainWindow->statusBar()->showMessage(i18n("Not implemented"), 5000);
        break;
    case dB:
        m_dBSpinBox->setValue(Aerodramus::dB_from_point_source(m_sensitivitySpinBox->value(), m_distanceSpinBox->value(), m_powerSpinBox->value()));
        break;
    default:
        m_mainWindow->statusBar()->showMessage(i18n("If only one field is 0 it will get calculated"), 5000);
        break;
    }
}

A src/CalculatorPage.h => src/CalculatorPage.h +26 -0
@@ 0,0 1,26 @@
// SPDX-FileCopyrightText: 2023 Mark Penner <mrpenner@mailbox.org>
// SPDX-License-Identifier: GPL-3.0-or-later

#ifndef CALCULATORPAGE_H
#define CALCULATORPAGE_H

#include <KXmlGuiWindow>
#include <QDoubleSpinBox>
#include <QWidget>

class CalculatorPage : public QWidget
{
    Q_OBJECT

public:
    CalculatorPage(QWidget *parent = nullptr, KXmlGuiWindow *mainWindow = nullptr);

private:
    KXmlGuiWindow *m_mainWindow;
    Q_SLOT void calculate();
    QDoubleSpinBox *m_sensitivitySpinBox;
    QDoubleSpinBox *m_powerSpinBox;
    QDoubleSpinBox *m_distanceSpinBox;
    QDoubleSpinBox *m_dBSpinBox;
};
#endif

M src/MainWindow.cpp => src/MainWindow.cpp +13 -0
@@ 3,6 3,7 @@

#include "MainWindow.h"

#include "CalculatorPage.h"
#include "ImpulseResponsePage.h"
#include <KActionCollection>
#include <KLocalizedString>


@@ 35,6 36,12 @@ void MainWindow::setupActions()
    KStandardAction::open(this, &MainWindow::openFile, actionCollection());
    KStandardAction::quit(qApp, &QCoreApplication::quit, actionCollection());

    QAction *addCalcPageAction = new QAction(this);
    addCalcPageAction->setText(i18n("&Add Calculator"));
    addCalcPageAction->setIcon(QIcon::fromTheme(QStringLiteral("accessories-calculator")));
    actionCollection()->addAction(QStringLiteral("addCalcPage"), addCalcPageAction);
    connect(addCalcPageAction, &QAction::triggered, this, &MainWindow::addCalcPage);

    QAction *closeCurrentPageAction = new QAction(this);
    closeCurrentPageAction->setText(i18n("&Close Page"));
    closeCurrentPageAction->setIcon(QIcon::fromTheme(QStringLiteral("window-close")));


@@ 51,6 58,12 @@ void MainWindow::openFile()
    addPage(fileNameFromDialog, page);
}

void MainWindow::addCalcPage()
{
    CalculatorPage *page = new CalculatorPage(nullptr, this);
    addPage(QStringLiteral("Calculator"), page);
}

void MainWindow::addPage(const QString &title, QWidget *widget)
{
    m_pageStack->addWidget(widget);

M src/MainWindow.h => src/MainWindow.h +1 -0
@@ 24,6 24,7 @@ private:
private Q_SLOTS:
    void openFile();
    void addPage(const QString &title, QWidget *widget);
    void addCalcPage();
    void closeCurrentPage();
};


M src/aerodramusui.rc => src/aerodramusui.rc +2 -0
@@ 7,12 7,14 @@
                         http://www.kde.org/standards/kxmlgui/1.0/kxmlgui.xsd">
  <MenuBar>
    <Menu name="view" >
      <Action name="addCalcPage" />
      <Action name="closeCurrentPage" />
    </Menu>
  </MenuBar>

  <ToolBar name="mainToolBar" >
    <text>Main Toolbar</text>
    <Action name="addCalcPage" />
    <Action name="closeCurrentPage" />
  </ToolBar>
</gui>