#include "Qwt3D_graph.h"

//#include "Form_main_window.h"
//#include "Qt_application.h"
//#include "Form_gnuplot_graph.h"
//#include "Collect_scalar_to_graph.h"
//#include "User.h"
//#include "Sleep.h"

//#include <qpixmap.h>
//#include <qlabel.h>
//#include <fstream>

//#ifdef MM_WIN32
//#include <process.h>
//#endif
//
//#include <stdio.h>
////#include <windows.h>

namespace MM
{

Qwt3D_graph::
Qwt3D_graph (Text const & name)
:
    Graph(name)//,
    //widget_ (0)
{
}

bool Qwt3D_graph::
is_visible() const
{
    //return  Form_gnuplot_graph::exist() 
    //    &&  Form_gnuplot_graph::singleton()->current_graph_ == this
    //    ;
    return true;
}

void Qwt3D_graph::
show (Collect_scalar_to_graph &/*source*/)
{
    //Form_gnuplot_graph::pop_up ();
    //Form_gnuplot_graph::singleton()->current_graph_ = this;

    ///*if (widget_ != 0)
    //    return;//*/

    //update (source);
}

void Qwt3D_graph::
update (Collect_scalar_to_graph & /*source*/)
{
//    if (! is_visible())
//        return;
//
//    Form_gnuplot_graph * widget = Form_gnuplot_graph::singleton();
//
//    int width  = widget->label->contentsRect().width () - widget->label->frameWidth() - 1;
//    int height = widget->label->contentsRect().height() - widget->label->frameWidth() - 3;//fix
//    //int width  = widget->label->geometry().width ();
//    //int height = widget->label->geometry().height() - 3;//fix
//
//    {
//        using namespace std;
//        ofstream dat ("_gnuplot.opt");
//
//        dat     << "set output \"_gnuplot.png\"" 
//#ifdef MM_WIN32
//        << endl << "set terminal png small size "
//            << width <<", " << height
//#endif
//#ifdef MM_UNIX
//        << endl << "set terminal png small picsize "
//            << width <<" " << height
//#endif
//        << endl << "set grid"
//        << endl
//        << endl << "plot '"<<source.title()<<"' smooth frequency t \"A\", \\" 
////        << endl << "plot '"<<source.title()<<"' t \"A\", \\" 
//        << endl << "'"<<source.title()
//            <<"' using 1:3 smooth frequency t \"<A>\""
////        << endl << "'"<<source.title()<<"' using 1:3:4 with yerrorbars t \"<A>\""
//        << endl;
 //   }


 //   /*STARTUPINFO si;
	//PROCESS_INFORMATION pi;
	//ZeroMemory(&si, sizeof(si));
	//si.cb = sizeof(si);
	//// We want its console window to be invisible to the user.
	//si.dwFlags = STARTF_USESHOWWINDOW;
	//si.wShowWindow = SW_HIDE;

	//int returnValue = CreateProcess (NULL, L"wgnuplot _gnuplot.opt", NULL, NULL, FALSE,
	//	CREATE_SUSPENDED | IDLE_PRIORITY_CLASS, NULL, NULL, &si, &pi);

	//if (returnValue != 0)
	//{
	//	ResumeThread(pi.hThread);

 //   	WaitForSingleObject( pi.hProcess, INFINITE );

	//	CloseHandle(pi.hProcess);
	//	CloseHandle(pi.hThread);
	//}//*/


 //   //int result = spawnl (P_WAIT, "gnuplot", "gnuplot", "_gnuplot.opt", NULL);
 //   //int result = system ("start /B wgnuplot _gnuplot.opt");


 //   //int result = spawnl (P_WAIT, "RunQuiet", "RunQuiet", "/w", "/c", "\"wgnuplot _gnuplot.opt\"", NULL);

 //   #ifdef MM_WIN32
 //   int result = spawnl (P_WAIT, "wgnuplot", "wgnuplot", "_gnuplot.opt", NULL);
 //   #endif
 //   #ifdef MM_UNIX
 //   int result = system ("./gnuplot _gnuplot.opt");
 //   #endif

 //   if (result != 0)
 //   {
 //       to_user().error (Text("Error: can not execute wgnuplot. Err: ") + result);
 //   }//*/

 //   widget->title->setText (name().c_str());
 //   widget->buffer_.load (QString ("_gnuplot.png")); //fix
 //   widget->label->setPixmap (widget->buffer_);      //fix
 //   widget->update ();
}

}//MM
