<commit_before>#include <gloperate-qtwidgets/ImageExporterResolutionWidget.h>

#include <gloperate-qt/qt-includes-begin.h>
#include "ui_ImageExporterResolutionWidget.h"
#include <gloperate-qt/qt-includes-end.h>

#define CM_PER_INCH 2.54
#define INCH_PER_CM 1 / CM_PER_INCH

namespace gloperate_qtwidgets
{

const QString ppiString = "ppi";
const QString pixelsPerCmString = "px/cm";
const QString pixelString = "pixel";
const QString inchString = "inch";
const QString cmString = "cm";

ImageExporterResolutionWidget::ImageExporterResolutionWidget(QWidget *parent)
:	QWidget(parent)
,	m_widthState(new ResolutionState(1920.0, pixelString))
,	m_heightState(new ResolutionState(1080.0, pixelString))
,	m_resolutionState(new ResolutionState(72, ppiString))
,	m_ui(new Ui_ImageExporterResolutionWidget)
{
    m_ui->setupUi(this);

    void (QComboBox:: *comboBoxCurrentIndexChanged)(const QString &) = &QComboBox::currentIndexChanged;
    connect(m_ui->widthComboBox, comboBoxCurrentIndexChanged,
        this, &ImageExporterResolutionWidget::widthUnitChanged);
    connect(m_ui->heightComboBox, comboBoxCurrentIndexChanged,
        this, &ImageExporterResolutionWidget::heightUnitChanged);
    connect(m_ui->resolutionComboBox, comboBoxCurrentIndexChanged,
        this, &ImageExporterResolutionWidget::resolutionUnitChanged);

    void (QDoubleSpinBox:: *doubleSpinBoxValueChanged)(double d) = &QDoubleSpinBox::valueChanged;
    void (QSpinBox:: *spinBoxValueChanged)(int i) = &QSpinBox::valueChanged;
    connect(m_ui->widthDoubleSpinBox, doubleSpinBoxValueChanged,
        this, &ImageExporterResolutionWidget::widthValueChanged);
    connect(m_ui->heightDoubleSpinBox, doubleSpinBoxValueChanged,
        this, &ImageExporterResolutionWidget::heightValueChanged);
    connect(m_ui->resolutionSpinBox, spinBoxValueChanged,
        this, &ImageExporterResolutionWidget::resolutionValueChanged);

    m_ui->widthDoubleSpinBox->setRange(0.01, 100000.0);
    m_ui->heightDoubleSpinBox->setRange(0.01, 100000.0);

    QStringList units{ pixelString, inchString, cmString };
    m_ui->widthComboBox->addItems(units);
    m_ui->heightComboBox->addItems(units);

    QStringList resolutions{ ppiString, pixelsPerCmString };
    bool oldComboBoxSignalStatus = m_ui->resolutionComboBox->blockSignals(true);
    m_ui->resolutionComboBox->addItems(resolutions);
    m_ui->resolutionComboBox->blockSignals(oldComboBoxSignalStatus);

    bool oldSpinBoxSignalStatus = m_ui->resolutionSpinBox->blockSignals(true);
    m_ui->resolutionSpinBox->setValue(72);
    m_ui->resolutionSpinBox->blockSignals(oldSpinBoxSignalStatus);
}

void ImageExporterResolutionWidget::updateResolutionSummary()
{
    // TODO: detect unsigned long long overflow
    int height{ static_cast<int>(std::round(toPixels(m_heightState->value, m_heightState->type))) };
    int width{ static_cast<int>(std::round(toPixels(m_widthState->value, m_widthState->type))) };
    emit resolutionChanged(QSize(width, height));

    unsigned long long pixelNumber{ static_cast<unsigned long long>(width) * static_cast<unsigned long long>(height) };
    QString unit;
    double byte;
    if (pixelNumber * 4 < 1024)
    {
        unit = "Byte";
        byte = pixelNumber * 4;
    }
    else if (pixelNumber * 4 < pow<unsigned long long>(1024, 2))
    {
        unit = "KiB";
        byte = pixelNumber * 4 / 1024;
    }
    else if (pixelNumber * 4 < pow<unsigned long long>(1024, 3))
    {
        unit = "MiB";
        byte = pixelNumber * 4 / pow<unsigned long long>(1024, 2);
    }
    else if (pixelNumber * 4 < pow<unsigned long long>(1024, 4))
    {
        unit = "GiB";
        byte = pixelNumber * 4 / pow<unsigned long long>(1024, 3);
    }
    else //if (pixelNumber * 4 < pow<unsigned long long>(1024, 5))
    {
        unit = "TiB";
        byte = pixelNumber * 4 / pow<unsigned long long>(1024, 4);
    }
    QString summary{ QString::number(pixelNumber) + " pixels (" + QString::number(std::round(byte * 100) / 100) + " " + unit + " uncompressed)" };
    //m_ui->resolutionSummaryLabel->setText(summary);
    emit resolutionSummaryChanged(summary);
}

double ImageExporterResolutionWidget::inchToPixels(double value)
{
    if (m_resolutionState->type == ppiString)
        value *= m_resolutionState->value;
    else if (m_resolutionState->type == pixelsPerCmString)<commit_msg>Fix conversion warnings in ImageExporterResolutionWidget<commit_after>#include <gloperate-qtwidgets/ImageExporterResolutionWidget.h>

#include <gloperate-qt/qt-includes-begin.h>
#include "ui_ImageExporterResolutionWidget.h"
#include <gloperate-qt/qt-includes-end.h>

#define CM_PER_INCH 2.54
#define INCH_PER_CM 1 / CM_PER_INCH

namespace gloperate_qtwidgets
{

const QString ppiString = "ppi";
const QString pixelsPerCmString = "px/cm";
const QString pixelString = "pixel";
const QString inchString = "inch";
const QString cmString = "cm";

ImageExporterResolutionWidget::ImageExporterResolutionWidget(QWidget *parent)
:	QWidget(parent)
,	m_widthState(new ResolutionState(1920.0, pixelString))
,	m_heightState(new ResolutionState(1080.0, pixelString))
,	m_resolutionState(new ResolutionState(72, ppiString))
,	m_ui(new Ui_ImageExporterResolutionWidget)
{
    m_ui->setupUi(this);

    void (QComboBox:: *comboBoxCurrentIndexChanged)(const QString &) = &QComboBox::currentIndexChanged;
    connect(m_ui->widthComboBox, comboBoxCurrentIndexChanged,
        this, &ImageExporterResolutionWidget::widthUnitChanged);
    connect(m_ui->heightComboBox, comboBoxCurrentIndexChanged,
        this, &ImageExporterResolutionWidget::heightUnitChanged);
    connect(m_ui->resolutionComboBox, comboBoxCurrentIndexChanged,
        this, &ImageExporterResolutionWidget::resolutionUnitChanged);

    void (QDoubleSpinBox:: *doubleSpinBoxValueChanged)(double d) = &QDoubleSpinBox::valueChanged;
    void (QSpinBox:: *spinBoxValueChanged)(int i) = &QSpinBox::valueChanged;
    connect(m_ui->widthDoubleSpinBox, doubleSpinBoxValueChanged,
        this, &ImageExporterResolutionWidget::widthValueChanged);
    connect(m_ui->heightDoubleSpinBox, doubleSpinBoxValueChanged,
        this, &ImageExporterResolutionWidget::heightValueChanged);
    connect(m_ui->resolutionSpinBox, spinBoxValueChanged,
        this, &ImageExporterResolutionWidget::resolutionValueChanged);

    m_ui->widthDoubleSpinBox->setRange(0.01, 100000.0);
    m_ui->heightDoubleSpinBox->setRange(0.01, 100000.0);

    QStringList units{ pixelString, inchString, cmString };
    m_ui->widthComboBox->addItems(units);
    m_ui->heightComboBox->addItems(units);

    QStringList resolutions{ ppiString, pixelsPerCmString };
    bool oldComboBoxSignalStatus = m_ui->resolutionComboBox->blockSignals(true);
    m_ui->resolutionComboBox->addItems(resolutions);
    m_ui->resolutionComboBox->blockSignals(oldComboBoxSignalStatus);

    bool oldSpinBoxSignalStatus = m_ui->resolutionSpinBox->blockSignals(true);
    m_ui->resolutionSpinBox->setValue(72);
    m_ui->resolutionSpinBox->blockSignals(oldSpinBoxSignalStatus);
}

void ImageExporterResolutionWidget::updateResolutionSummary()
{
    // TODO: detect unsigned long long overflow
    int height{ static_cast<int>(std::round(toPixels(m_heightState->value, m_heightState->type))) };
    int width{ static_cast<int>(std::round(toPixels(m_widthState->value, m_widthState->type))) };
    emit resolutionChanged(QSize(width, height));

    unsigned long long pixelNumber{ static_cast<unsigned long long>(width) * static_cast<unsigned long long>(height) };
    QString unit;
    double byte;
    if (pixelNumber * 4 < 1024)
    {
        unit = "Byte";
        byte = static_cast<double>(pixelNumber) * 4;
    }
    else if (pixelNumber * 4 < static_cast<unsigned long long>(pow<unsigned long long>(1024, 2)))
    {
        unit = "KiB";
        byte = static_cast<double>(pixelNumber) * 4 / 1024;
    }
    else if (pixelNumber * 4 < static_cast<unsigned long long>(pow<unsigned long long>(1024, 3)))
    {
        unit = "MiB";
        byte = static_cast<double>(pixelNumber) * 4 / pow<unsigned long long>(1024, 2);
    }
    else if (pixelNumber * 4 < static_cast<unsigned long long>(pow<unsigned long long>(1024, 4)))
    {
        unit = "GiB";
        byte = static_cast<double>(pixelNumber) * 4 / pow<unsigned long long>(1024, 3);
    }
    else //if (pixelNumber * 4 < static_cast<unsigned long long>(pow<unsigned long long>(1024, 5)))
    {
        unit = "TiB";
        byte = static_cast<double>(pixelNumber) * 4 / pow<unsigned long long>(1024, 4);
    }
    QString summary{ QString::number(pixelNumber) + " pixels (" + QString::number(std::round(byte * 100) / 100) + " " + unit + " uncompressed)" };
    //m_ui->resolutionSummaryLabel->setText(summary);
    emit resolutionSummaryChanged(summary);
}

double ImageExporterResolutionWidget::inchToPixels(double value)
{
    if (m_resolutionState->type == ppiString)
        value *= m_resolutionState->value;
    else if (m_resolutionState->type == pixelsPerCmString)
{'fraction_non_alphanumeric': 0.11966956910024559, 'fraction_numerical': 0.02165661978120116, 'mean_word_length': 4.9449236894492365, 'pattern_counts': {'":': 0, '<': 50, '<?xml version=': 0, '>': 112, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2015440', 'n_tokens_mistral': 2983, 'n_tokens_neox': 2725, 'n_words': 505}