/*
 *  Copyright (C) 2007-2008 CMM Sigma Andrzej Chmielowiec <cmmsigma@cmmsigma.eu>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 3
 *  as published by the Free Software Foundation.
 *
 *  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.
 */

#include "RbgWidget.h"

RbgWidget::RbgWidget(QWidget *parent): QWidget(parent)
{
	QLayout *layout;
	
	mLayout = new QGridLayout;
	
	QGroupBox *rbgBitsBox = new QGroupBox(tr("Number of octets"));
	layout = new QVBoxLayout;
	mNum = new QSpinBox;
	mNum->setMaximum(100000000L);
	layout->addWidget(mNum);
	rbgBitsBox->setLayout(layout);
	
	QGroupBox *rbgFmtBox = new QGroupBox(tr("Save format"));
	layout = new QVBoxLayout;
	mBinaryAsciiFmt = new QRadioButton(tr("binary ASCII"));
	mBinaryAsciiFmt->setChecked(true);
	mHexAsciiFmt = new QRadioButton(tr("hexadecimal ASCII"));
	mBinaryFmt = new QRadioButton(tr("binary"));
	layout->addWidget(mBinaryAsciiFmt);
	layout->addWidget(mHexAsciiFmt);
	layout->addWidget(mBinaryFmt);
	rbgFmtBox->setLayout(layout);
	
	mGenerate = new QPushButton(QIcon(QString(":img/random.png")), tr("Generate to file"));
	
	mLayout->addWidget(rbgBitsBox, 0, 0, 1, 3);
	mLayout->addWidget(rbgFmtBox, 1, 0, 1, 3);
	mLayout->addWidget(mGenerate, 2, 2);
	mLayout->setColumnStretch(1, 1);
	mLayout->setRowStretch(3, 1);
	
	setLayout(mLayout);
	
	connect(mGenerate, SIGNAL(pressed()), this, SLOT(generateSlot()));
}

void
RbgWidget::generateSlot()
{
	QString fileName;
	int octets = 0;
	int fmt = 0;
	
	octets = mNum->value();
	
	if (mBinaryAsciiFmt->isChecked())
		fmt = BinaryAsciiFmt;
	
	else if (mHexAsciiFmt->isChecked())
		fmt = HexAsciiFmt;
	
	else if (mBinaryFmt->isChecked())
		fmt = BinaryFmt;
	
	fileName = QFileDialog::getSaveFileName(this, tr("Save as"));
	
	if (fileName != QString())
		emit generateSig(octets, fmt, fileName);
}
