/***************************************************************************
 *   Copyright (C) 2008 by Peter Dahlberg   *
 *   pdahlberg@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 2 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, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include "homobfieldwidget.h"
#include "homobfielditem.h"
#include "simulscene.h"
#include "expdoublespinbox.h"
#include <QVBoxLayout>
#include <QtDebug>
#include <QDial>
#include <QGroupBox>
#include <QLabel>
#include <QRadioButton>
#include <QButtonGroup>
#include <QPushButton>
HomoBFieldWidget::HomoBFieldWidget(QWidget* parent, Qt::WindowFlags f, HomoBFieldItem* bField):QWidget(parent, f) {

	homoBField = bField;
	createWidget();

}
void HomoBFieldWidget::createWidget() {

	QDoubleSpinBox *heightBox = new QDoubleSpinBox;
	heightBox->setRange(HomoBFieldItem::MinimumHeight, 5000);
	heightBox->setKeyboardTracking(false);
	heightBox->setSuffix(" px");
	connect(homoBField ,SIGNAL(heightChanged(double)), heightBox, SLOT(setValue(double)) );
	connect(heightBox, SIGNAL(valueChanged(double)),homoBField ,SLOT(setRectFHeight(double)) );

	QDoubleSpinBox *widthBox = new QDoubleSpinBox;
	widthBox->setRange(HomoBFieldItem::MinimumWidth, 5000);
	widthBox->setKeyboardTracking(false);
	widthBox->setSuffix(" px");
	connect(homoBField ,SIGNAL(widthChanged(double)),widthBox, SLOT(setValue(double)) );
	connect(widthBox, SIGNAL(valueChanged(double)),homoBField ,SLOT(setRectFWidth(double)) );

	QLabel* xLabel = new QLabel(tr("x:"));
	QLabel* yLabel = new QLabel(tr("y:"));
	QLabel* widthLabel = new QLabel(tr("Breite(l):"));
	QLabel* heightLabel = new QLabel(tr("H\366he(d):"));

	QDoubleSpinBox *posXBox = new QDoubleSpinBox;
	posXBox->setRange(-5000, 5000);
	posXBox->setKeyboardTracking(false);
	posXBox->setSuffix(" px");
	connect(homoBField ,SIGNAL(ScenePosXChanged(double)),posXBox, SLOT(setValue(double)) );
	connect(posXBox, SIGNAL(valueChanged(double)),homoBField ,SLOT(setScenePosX(double)) );

	QDoubleSpinBox *posYBox = new QDoubleSpinBox;
	posYBox->setRange(-5000, 5000);
	posYBox->setKeyboardTracking(false);
	posYBox->setSuffix(" px");
	posYBox->setStatusTip("Position des Feldes in y-Richtung");
	connect(homoBField ,SIGNAL(ScenePosYChanged(double)),posYBox, SLOT(setValue(double)) );
	connect(posYBox, SIGNAL(valueChanged(double)),homoBField ,SLOT(setScenePosY(double)) );


	QGridLayout* geomGridLayout = new QGridLayout;
	//geomGridLayout->setSizeConstraint(QLayout::SetFixedSize);
	geomGridLayout->addWidget(heightBox,3,1);
	geomGridLayout->addWidget(posYBox,1,1);
	geomGridLayout->addWidget(posXBox,0,1);
	geomGridLayout->addWidget(xLabel,0,0,Qt::AlignRight);
	geomGridLayout->addWidget(yLabel,1,0,Qt::AlignRight);
	geomGridLayout->addWidget(heightLabel,3,0,Qt::AlignRight);
	geomGridLayout->addWidget(widthLabel,2,0,Qt::AlignRight);
	geomGridLayout->addWidget(widthBox,2,1);



	QGroupBox* geometryBox = new QGroupBox(tr("Geometrie"));
	geometryBox->setLayout(geomGridLayout);

	QString fieldFluxDensityString = tr("Die Manetische Flussdichte B in Tesla");
	QLabel* fieldFluxDensityLabel = new QLabel(tr("Flussdichte(B):"));
	fieldFluxDensityLabel->setStatusTip(fieldFluxDensityString);

	ExpDoubleSpinBox* fieldFluxDensityBox = new ExpDoubleSpinBox;
	fieldFluxDensityBox->setRange(0,1e+200);
	fieldFluxDensityBox->setDecimals(40);
	fieldFluxDensityBox->setDisplayDecimals(3);
	fieldFluxDensityBox->setKeyboardTracking(false);
	fieldFluxDensityBox->setSuffix(" T");
	fieldFluxDensityBox->setStatusTip(fieldFluxDensityString);
	connect(homoBField ,SIGNAL(fluxDensityChanged(double)),fieldFluxDensityBox, SLOT(setValue(double)) );
	connect(fieldFluxDensityBox, SIGNAL(valueChanged(double)),homoBField ,SLOT(setFluxDensity(double)) );

	QGridLayout* constGridLayout = new QGridLayout;
	//constGridLayout->setSizeConstraint(QLayout::SetFixedSize);
	constGridLayout->addWidget(fieldFluxDensityBox,0,1);
	constGridLayout->addWidget(fieldFluxDensityLabel,0,0,Qt::AlignRight);

	QGroupBox* fieldConstantsBox = new QGroupBox(tr("Feldkonstanten"));
	fieldConstantsBox->setLayout(constGridLayout);



	QRadioButton* intoPlaneButton = new QRadioButton(tr("in die Ebene hinein"));
	QRadioButton* outOfPlaneButton = new QRadioButton(tr("aus der Ebene heraus"));
	intoPlaneButton->setChecked(homoBField->getIsDirectionIntoPlane());
	outOfPlaneButton->setChecked(!homoBField->getIsDirectionIntoPlane());
	connect(homoBField ,SIGNAL(directionChanged(bool)),intoPlaneButton, SLOT(setChecked(bool)) );
	connect(intoPlaneButton, SIGNAL(toggled(bool)),homoBField ,SLOT(setIsDirectionIntoPlane(bool)) );


	QButtonGroup* directionGroup = new QButtonGroup();
	directionGroup->addButton(intoPlaneButton);
	directionGroup->addButton(outOfPlaneButton);


	QGridLayout* directionGridLayout = new QGridLayout;
	directionGridLayout->addWidget(intoPlaneButton,0,0);
	directionGridLayout->addWidget(outOfPlaneButton,1,0);

	QGroupBox* fieldDirectionBox = new QGroupBox(tr("Feldrichtung"));
	fieldDirectionBox->setLayout(directionGridLayout);

	//SimulScene* theScene = dynamic_cast<SimulScene*>(homoBField->scene());
	//qDebug()<<theScene;
	//QPushButton* downButton = new QPushButton("down");
	//connect( downButton,SIGNAL(clicked()),theScene, SLOT(moveSelectedFieldItemDown()) );

	QVBoxLayout * mainLayout = new QVBoxLayout(this);
	mainLayout -> addWidget(geometryBox);
	mainLayout -> addWidget(fieldDirectionBox);
	mainLayout -> addWidget(fieldConstantsBox);
	//mainLayout -> addWidget(downButton);
	mainLayout -> addStretch();

	QWidget::setTabOrder (posXBox, posYBox);
	QWidget::setTabOrder (posYBox, widthBox);
	QWidget::setTabOrder (widthBox, heightBox);

	this -> setLayout(mainLayout);
}

HomoBFieldWidget::~HomoBFieldWidget() {
}

