Files
lcd-image-converter/controls/setup/dialogconvert2.cpp
2012-08-09 00:35:02 +06:00

448 lines
15 KiB
C++

/*
* LCD Image Converter. Converts images and fonts for embedded applciations.
* Copyright (C) 2012 riuson
* mailto: riuson@gmail.com
*
* 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
* (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. If not, see <http://www.gnu.org/licenses/
*/
#include "dialogconvert2.h"
#include "ui_dialogconvert2.h"
//-----------------------------------------------------------------------------
#include <QList>
#include <QSettings>
#include <QStringList>
#include <QInputDialog>
#include "idatacontainer.h"
#include "converterhelper.h"
#include "dialogpreview.h"
#include "matrixpreviewmodel.h"
#include "conversionmatrixoptions.h"
#include "bitmaphelper.h"
//-----------------------------------------------------------------------------
DialogConvert2::DialogConvert2(IDataContainer *dataContainer, QWidget *parent) :
QDialog(parent),
ui(new Ui::DialogConvert2)
{
ui->setupUi(this);
this->mPreview = NULL;
this->mData = dataContainer;
this->mMatrix = new QList<quint32>();
this->ui->comboBoxConversionType->addItem(tr("Monochrome"), ConversionTypeMonochrome);
this->ui->comboBoxConversionType->addItem(tr("Grayscale"), ConversionTypeGrayscale);
this->ui->comboBoxConversionType->addItem(tr("Color"), ConversionTypeColor);
this->ui->comboBoxMonochromeType->addItem(tr("Edge"), MonochromeTypeEdge);
this->ui->comboBoxMonochromeType->addItem(tr("Diffuse Dither"), MonochromeTypeDiffuseDither);
this->ui->comboBoxMonochromeType->addItem(tr("Ordered Dither"), MonochromeTypeOrderedDither);
this->ui->comboBoxMonochromeType->addItem(tr("Threshold Dither"), MonochromeTypeThresholdDither);
this->ui->comboBoxBlockSize->addItem(tr("8 bit"), Data8);
this->ui->comboBoxBlockSize->addItem(tr("16 bit"), Data16);
this->ui->comboBoxBlockSize->addItem(tr("24 bit"), Data24);
this->ui->comboBoxBlockSize->addItem(tr("32 bit"), Data32);
this->ui->comboBoxRotate->addItem(tr("None"), QVariant(RotateNone));
this->ui->comboBoxRotate->addItem(tr("90\u00b0 Clockwise"), QVariant(Rotate90));
this->ui->comboBoxRotate->addItem(tr("180\u00b0"), QVariant(Rotate180));
this->ui->comboBoxRotate->addItem(tr("90\u00b0 Counter-Clockwise"), QVariant(Rotate270));
//ConverterHelper::createMatrixMono(this->mMatrix);
//ConverterHelper::createMatrixGrayscale(this->mMatrix);
ConverterHelper::createMatrixColor(this->mMatrix);
this->mMatrixModel = new MatrixPreviewModel(this->mMatrix, this);
this->ui->tableViewOperations->setModel(this->mMatrixModel);
this->ui->tableViewOperations->resizeColumnsToContents();
this->ui->tableViewOperations->resizeRowsToContents();
this->fillPresetsList();
}
//-----------------------------------------------------------------------------
DialogConvert2::~DialogConvert2()
{
if (this->mPreview != NULL)
delete this->mPreview;
delete ui;
delete this->mMatrixModel;
delete this->mMatrix;
}
//-----------------------------------------------------------------------------
void DialogConvert2::fillPresetsList()
{
QString current = this->ui->comboBoxPresets->currentText();
this->ui->comboBoxPresets->clear();
QSettings sett;
sett.beginGroup("presets");
QStringList names = sett.childGroups();
sett.endGroup();
this->ui->comboBoxPresets->addItems(names);
if (names.contains(current))
{
this->ui->comboBoxPresets->setCurrentIndex(names.indexOf(current));
}
}
//-----------------------------------------------------------------------------
void DialogConvert2::presetLoad(const QString &name)
{
if (name.isEmpty())
return;
QSettings sett;
sett.beginGroup("presets");
if (sett.childGroups().contains(name))
{
sett.beginGroup(name);
QString strFlags = sett.value("flags", QString("00000000")).toString();
QString strMaskUsed = sett.value("maskUsed", QString("ffffffff")).toString();
QString strMaskAnd = sett.value("maskAnd", QString("ffffffff")).toString();
QString strMaskOr = sett.value("maskOr", QString("00000000")).toString();
bool ok;
quint32 flags, maskUsed, maskAnd, maskOr;
flags = strFlags.toUInt(&ok, 16);
if (ok)
{
maskUsed = strMaskUsed.toUInt(&ok, 16);
if (ok)
{
maskAnd = strMaskAnd.toUInt(&ok, 16);
if (ok)
{
maskOr = strMaskOr.toUInt(&ok, 16);
this->mMatrix->clear();
this->mMatrix->append(flags);
this->mMatrix->append(maskUsed);
this->mMatrix->append(maskAnd);
this->mMatrix->append(maskOr);
int operations = sett.beginReadArray("matrix");
for (int i = 0; i < operations; i++)
{
sett.setArrayIndex(i);
QString strMask = sett.value("mask", QString("00000000")).toString();
QString strShift = sett.value("shift", QString("00000000")).toString();
quint32 mask, shift;
if (ok)
{
mask = strMask.toUInt(&ok, 16);
if (ok)
{
shift = strShift.toUInt(&ok, 16);
if (ok)
{
this->mMatrix->append(mask);
this->mMatrix->append(shift);
}
}
}
}
sett.endArray();;
}
}
}
sett.endGroup();
}
sett.endGroup();
\
// update gui
{
this->ui->tableViewOperations->setModel(NULL);
this->ui->tableViewOperations->setModel(this->mMatrixModel);
this->ui->tableViewOperations->update();
this->ui->tableViewOperations->resizeColumnsToContents();
ConversionMatrixOptions options(this->mMatrix);
int index = this->ui->comboBoxConversionType->findData(options.convType());
if (index >= 0)
this->ui->comboBoxConversionType->setCurrentIndex(index);
index = this->ui->comboBoxMonochromeType->findData(options.monoType());
if (index >= 0)
this->ui->comboBoxMonochromeType->setCurrentIndex(index);
index = this->ui->comboBoxBlockSize->findData(options.blockSize());
if (index >= 0)
this->ui->comboBoxBlockSize->setCurrentIndex(index);
index = this->ui->comboBoxRotate->findData(options.rotate());
if (index >= 0)
this->ui->comboBoxRotate->setCurrentIndex(index);
this->ui->horizontalScrollBarEdge->setValue(options.edge());
this->ui->checkBoxFlipHorizontal->setChecked(options.flipHorizontal());
this->ui->checkBoxFlipVertical->setChecked(options.flipVertical());
this->ui->checkBoxInverse->setChecked(options.inverse());
}
}
//-----------------------------------------------------------------------------
void DialogConvert2::presetSaveAs(const QString &name)
{
QSettings sett;
sett.beginGroup("presets");
sett.beginGroup(name);
sett.remove("");
int operations = this->mMatrix->length() - ConversionMatrixOptions::OperationsStartIndex;
operations = operations >> 1;
sett.setValue("flags", QString("%1").arg(this->mMatrix->at(0), 8, 16, QChar('0')));
sett.setValue("maskUsed", QString("%1").arg(this->mMatrix->at(1), 8, 16, QChar('0')));
sett.setValue("maskAnd", QString("%1").arg(this->mMatrix->at(2), 8, 16, QChar('0')));
sett.setValue("maskOr", QString("%1").arg(this->mMatrix->at(3), 8, 16, QChar('0')));
sett.beginWriteArray("matrix");
for (int i = 0; i < operations; i++)
{
sett.setArrayIndex(i);
sett.setValue("mask", QString("%1").arg(this->mMatrix->at(ConversionMatrixOptions::OperationsStartIndex + (i << 1)), 8, 16, QChar('0')));
sett.setValue("shift", QString("%1").arg(this->mMatrix->at(ConversionMatrixOptions::OperationsStartIndex + (i << 1) + 1), 8, 16, QChar('0')));
}
sett.endArray();
sett.endGroup();
sett.endGroup();
this->fillPresetsList();
}
//-----------------------------------------------------------------------------
void DialogConvert2::presetRemove(const QString &name)
{
QSettings sett;
sett.beginGroup("presets");
sett.beginGroup(name);
sett.remove("");
sett.endGroup();
this->fillPresetsList();
}
//-----------------------------------------------------------------------------
void DialogConvert2::updatePreview()
{
if (this->mData != NULL)
{
this->ui->tableViewOperations->update();
this->ui->tableViewOperations->resizeColumnsToContents();
if (this->mPreview != NULL)
this->mPreview->updatePreview();
}
}
//-----------------------------------------------------------------------------
void DialogConvert2::on_pushButtonPreview_clicked()
{
if (this->mPreview == NULL)
{
this->mPreview = new DialogPreview(this->mData, this->mMatrix, this);
QObject::connect(this->mPreview, SIGNAL(accepted()), this, SLOT(previewClosed()));
QObject::connect(this->mPreview, SIGNAL(rejected()), this, SLOT(previewClosed()));
}
this->mPreview->show();
}
//-----------------------------------------------------------------------------
void DialogConvert2::on_comboBoxConversionType_currentIndexChanged(int index)
{
QVariant data = this->ui->comboBoxConversionType->itemData(index);
bool ok;
int a = data.toInt(&ok);
if (ok)
{
ConversionMatrixOptions options(this->mMatrix);
options.setConvType((ConversionType)a);
if (options.convType() == ConversionTypeMonochrome)
{
this->ui->comboBoxMonochromeType->setEnabled(true);
if (options.monoType() == MonochromeTypeEdge)
this->ui->horizontalScrollBarEdge->setEnabled(true);
else
this->ui->horizontalScrollBarEdge->setEnabled(false);
}
else
{
this->ui->comboBoxMonochromeType->setEnabled(false);
this->ui->horizontalScrollBarEdge->setEnabled(false);
}
this->updatePreview();
}
}
//-----------------------------------------------------------------------------
void DialogConvert2::on_comboBoxMonochromeType_currentIndexChanged(int index)
{
QVariant data = this->ui->comboBoxMonochromeType->itemData(index);
bool ok;
int a = data.toInt(&ok);
if (ok)
{
ConversionMatrixOptions options(this->mMatrix);
options.setMonoType((MonochromeType)a);
if (options.convType() == ConversionTypeMonochrome)
{
this->ui->comboBoxMonochromeType->setEnabled(true);
if (options.monoType() == MonochromeTypeEdge)
this->ui->horizontalScrollBarEdge->setEnabled(true);
else
this->ui->horizontalScrollBarEdge->setEnabled(false);
}
else
{
this->ui->comboBoxMonochromeType->setEnabled(false);
this->ui->horizontalScrollBarEdge->setEnabled(false);
}
this->updatePreview();
}
}
//-----------------------------------------------------------------------------
void DialogConvert2::on_comboBoxBlockSize_currentIndexChanged(int index)
{
QVariant data = this->ui->comboBoxBlockSize->itemData(index);
bool ok;
int a = data.toInt(&ok);
if (ok)
{
ConversionMatrixOptions options(this->mMatrix);
options.setBlockSize((DataBlockSize)a);
this->updatePreview();
}
}
//-----------------------------------------------------------------------------
void DialogConvert2::on_comboBoxRotate_currentIndexChanged(int index)
{
QVariant data = this->ui->comboBoxRotate->itemData(index);
bool ok;
int a = data.toInt(&ok);
if (ok)
{
ConversionMatrixOptions options(this->mMatrix);
Rotate rotate = (Rotate)a;
options.setRotate(rotate);
this->updatePreview();
}
}
//-----------------------------------------------------------------------------
void DialogConvert2::on_checkBoxFlipHorizontal_toggled(bool value)
{
ConversionMatrixOptions options(this->mMatrix);
options.setFlipHorizontal(value);
this->updatePreview();
}
//-----------------------------------------------------------------------------
void DialogConvert2::on_checkBoxFlipVertical_toggled(bool value)
{
ConversionMatrixOptions options(this->mMatrix);
options.setFlipVertical(value);
this->updatePreview();
}
//-----------------------------------------------------------------------------
void DialogConvert2::on_checkBoxInverse_toggled(bool value)
{
ConversionMatrixOptions options(this->mMatrix);
options.setInverse(value);
this->updatePreview();
}
//-----------------------------------------------------------------------------
void DialogConvert2::on_pushButtonPresetSaveAs_clicked()
{
QSettings sett;
sett.beginGroup("presets");
QStringList names = sett.childGroups();
sett.endGroup();
QInputDialog dialog(this);
dialog.setComboBoxItems(names);
dialog.setComboBoxEditable(true);
QString current = this->ui->comboBoxPresets->currentText();
bool ok;
QString result = dialog.getItem(this, tr("Enter preset name"), tr("Preset name:"), names, names.indexOf(current), true, &ok);
if (ok && !result.isEmpty())
{
this->presetSaveAs(result);
}
this->fillPresetsList();
this->updatePreview();
}
//-----------------------------------------------------------------------------
void DialogConvert2::on_pushButtonPresetRemove_clicked()
{
QString name = this->ui->comboBoxPresets->currentText();
this->presetRemove(name);
this->fillPresetsList();
}
//-----------------------------------------------------------------------------
void DialogConvert2::on_comboBoxPresets_currentIndexChanged(int index)
{
QString name = this->ui->comboBoxPresets->itemText(index);
this->presetLoad(name);
this->updatePreview();
}
//-----------------------------------------------------------------------------
void DialogConvert2::on_horizontalScrollBarEdge_valueChanged(int value)
{
ConversionMatrixOptions options(this->mMatrix);
options.setEdge(value);
this->updatePreview();
}
//-----------------------------------------------------------------------------
void DialogConvert2::previewClosed()
{
if (this->mPreview != NULL)
{
delete this->mPreview;
this->mPreview = NULL;
}
}
//-----------------------------------------------------------------------------