/*
	Copyright (C) 2024-2025 Mark Tyler

	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 in the file COPYING.
*/

#include "wasp_qt.h"



void MainWindow::create_shape_edit ( QTabWidget * tabWidget )
{
	QWidget		* w;
	QVBoxLayout	* vbox, * vb;
	QHBoxLayout	* hbox, * hb;
	QGroupBox	* gbox;
	QPushButton	* button;
	QGridLayout	* grid;
	QSplitter	* split;

	QWidget * tab = new QWidget;
	tabWidget->addTab ( tab, "Shape" );

	vbox = new QVBoxLayout;
	tab->setLayout ( vbox );

	vbox->setContentsMargins ( 5, 5, 5, 5 );

/// ----------------------------------------------------------------------------

	hb = new QHBoxLayout;
	vbox->addLayout ( hb );

	gbox = new QGroupBox ( "Control" );
	hb->addWidget ( gbox );

	vb = new QVBoxLayout;
	gbox->setLayout ( vb );

	hbox = new QHBoxLayout;
	vb->addLayout ( hbox );

	button = new QPushButton ( "&Play" );
	hbox->addWidget ( button );
	connect( button, SIGNAL( clicked() ), this, SLOT(press_audio_play()));

	button = new QPushButton ( "&Stop" );
	hbox->addWidget ( button );
	connect( button, SIGNAL( clicked() ), this, SLOT(press_audio_stop()));

	hbox = new QHBoxLayout;
	vb->addLayout ( hbox );

	grid = new QGridLayout;
	hbox->addLayout ( grid );
	grid->addWidget ( new QLabel ( "Duration (secs)" ), 0, 0 );
	grid->addWidget ( new QLabel ( "Octave" ), 1, 0 );

	m_spin_wave_duration = new QDoubleSpinBox;
	m_spin_wave_duration->setDecimals ( 3 );
	m_spin_wave_duration->setRange ( MTWASP_WAVE_DURATION_MIN,
		MTWASP_WAVE_DURATION_MAX );
	m_spin_wave_duration->setValue ( MTWASP_WAVE_DURATION_DEFAULT );
	grid->addWidget ( m_spin_wave_duration, 0, 1 );

	m_spin_wave_octave = new QSpinBox;
	m_spin_wave_octave->setRange ( MTWASP_NOTE_OCTAVE_MIN,
		MTWASP_NOTE_OCTAVE_MAX );
	m_spin_wave_octave->setValue ( MTWASP_NOTE_OCTAVE_DEFAULT );
	grid->addWidget ( m_spin_wave_octave, 1, 1 );

	// Add a padding widget to keep the grid to an optimal size
	w = new QWidget;
	hbox->addWidget ( w );
	w->setSizePolicy ( QSizePolicy::Preferred, QSizePolicy::Preferred );

	// Add a padding widget to keep the widgets above to an optimal size
	w = new QWidget;
	vb->addWidget ( w );

/// ----------------------------------------------------------------------------

	gbox = new QGroupBox ( "Volume Envelope" );
	hb->addWidget ( gbox );

	hbox = new QHBoxLayout;
	gbox->setLayout ( hbox );

	vb = new QVBoxLayout;
	hbox->addLayout ( vb );

	grid = new QGridLayout;
	vb->addLayout ( grid );

	grid->addWidget ( new QLabel ( "P1" ), 0, 1, Qt::AlignHCenter );
	grid->addWidget ( new QLabel ( "P2" ), 0, 2, Qt::AlignHCenter );
	grid->addWidget ( new QLabel ( "X" ), 1, 0 );
	grid->addWidget ( new QLabel ( "Y" ), 2, 0 );

	grid->addWidget ( m_volume_view->get_spin_p1x(), 1, 1 );
	grid->addWidget ( m_volume_view->get_spin_p1y(), 2, 1 );
	grid->addWidget ( m_volume_view->get_spin_p2x(), 1, 2 );
	grid->addWidget ( m_volume_view->get_spin_p2y(), 2, 2 );

	// Add a padding widget to keep the widgets above to an optimal size
	w = new QWidget;
	vb->addWidget ( w );

	m_volume_view->setMinimumSize ( 128, 64 );
	m_volume_view->setSizePolicy ( QSizePolicy::Expanding,
		QSizePolicy::Preferred );
	hbox->addWidget ( m_volume_view );

/// ----------------------------------------------------------------------------

	gbox = new QGroupBox ( "Waveform" );
	vbox->addWidget ( gbox );
	gbox->setSizePolicy ( QSizePolicy::Preferred, QSizePolicy::Expanding );

	vb = new QVBoxLayout;
	gbox->setLayout ( vb );

	split = new QSplitter;
	vb->addWidget ( split );

	m_text_wave_function = new QTextEdit;
	split->addWidget ( m_text_wave_function );
	m_text_wave_function->setText ( m_project.get_wave_function() );
	connect ( m_text_wave_function, SIGNAL ( textChanged () ), this,
		SLOT ( changed_text_wave_function () ) );

	m_function_view = new ShapeFunctionView ( m_project );
	m_function_view->setSizePolicy ( QSizePolicy::Expanding,
		QSizePolicy::Expanding );
	split->addWidget ( m_function_view );
}

