// @(#)root/gt:$Id$
// Author: Valery Fine      18/01/2007

/****************************************************************************
** $Id$
**
** Copyright (C) 2007 by Valeri Fine. Brookhaven National Laboratory.
**                                    All rights reserved.
**
**
*****************************************************************************/
///////////////////////////////////////////////////////////////////////////
//
// The TQRootSlot singleton class introduces the global SLOT to invoke
// the  ROOT command line from the GUI signals
// Optionally one can execute TApplication::Terminate method directly
//
// It provides a Qt slot to attach the the CINT C++ interpreter
// to any Qt signal
// To execute any C++ statement from the GUI one should connect
// one's Qt signal with the Qt slot of the global instance of this class
//
//  connect(GUI object, SIGNAL(const char *editedLine),TQtRootSlot::CintSlot(),SLOT(ProcessLine(const char*)))
//
//  To terminate the ROOT from Qt GUI element connect the signal with
//  the Terminate  or TerminateAndQuite slot.
//  For example to terminate ROOT and Qt smoothly do
//
//  connect(qApp,SIGNAL(lastWindowClosed()),TQtRootSlot::CintSlot(),SLOT(TerminateAndQuit())
//
//  To terminate just ROOT (in case the Qt is terminated by the other means)
//  connect(qApp,SIGNAL(lastWindowClosed()),TQtRootSlot::CintSlot(),SLOT(Terminate())
//
///////////////////////////////////////////////////////////////////////////

#include "TQtRootSlot.h"
#include "TROOT.h"
#include "TApplication.h"
#include "TInterpreter.h"
#include <qapplication.h>
#include <QString>

TQtRootSlot *TQtRootSlot::fgTQtRootSlot = 0;
//____________________________________________________
TQtRootSlot *TQtRootSlot::CintSlot()
{
   // create and return the singleton
   if (!fgTQtRootSlot) fgTQtRootSlot = new TQtRootSlot();
   return fgTQtRootSlot;
}
//____________________________________________________
void TQtRootSlot::EndOfLine()
{
   // slot to perform the standard "EndOfLine" ROOT action
   // it used to update the current gPad
   if (gInterpreter)  gInterpreter->EndOfLineAction();
}

//____________________________________________________
void TQtRootSlot::ProcessLine(const QString &command)
{
     // execute the arbitrary ROOT /CINt command via
     // CINT C++ interpreter and emit the result
   std::string cmd = command.toStdString();
   ProcessLine(cmd.c_str());
}

//____________________________________________________
void TQtRootSlot::ProcessLine(const char *command)
{
     // execute the arbitrary ROOT /CINt command via
     // CINT C++ interpreter and emit the result
     int error;
     gROOT->ProcessLine(command,&error);
     emit Error(error);
}
//____________________________________________________
void TQtRootSlot::Terminate(int status)const
{
   // the dedicated slot to terminate the ROOT application
   // with "status"
   if (gApplication) gApplication->Terminate(status);
}

//____________________________________________________
void TQtRootSlot::Terminate()const
{
   // the dedicated slot to terminate the ROOT application
   // and return the "0" status
   Terminate(0);
}

//____________________________________________________
void TQtRootSlot::TerminateAndQuit() const
{
    // the dedicated  slot to terminate the ROOT application
    // and quit the Qt Application if any

   Bool_t rtrm = kTRUE;
   if (gApplication) {
      rtrm = gApplication->ReturnFromRun();
      gApplication->SetReturnFromRun(kTRUE);
      gApplication->Terminate(0);
   }
   if (qApp) qApp->quit();
   else if (!rtrm && gApplication ) {
      gApplication->SetReturnFromRun(rtrm);
      // to make sure the ROOT event loop is terminated
      gROOT->ProcessLine(".q");
   }
}

//__________________________________________________________________
bool QConnectCint(const QObject * sender, const char * signal)
{
   // Connect the Qt signal to the "execute C++ statement" via CINT SLOT
   // The first parameter of the Qt signal must be "const char*"
   return
   QObject::connect(sender,signal
      ,TQtRootSlot::CintSlot(),SLOT(ProcessLine(const char*)));
}

//__________________________________________________________________
bool QConnectTerminate(const QObject * sender, const char * signal)
{
   // Connect the Qt signal to the "TApplication::Terminate" method
   // Any extra parameters of the Qt signal are discarded
   return
   QObject::connect(sender,signal
      ,TQtRootSlot::CintSlot(),SLOT(Terminate()));
}
 TQtRootSlot.cxx:1
 TQtRootSlot.cxx:2
 TQtRootSlot.cxx:3
 TQtRootSlot.cxx:4
 TQtRootSlot.cxx:5
 TQtRootSlot.cxx:6
 TQtRootSlot.cxx:7
 TQtRootSlot.cxx:8
 TQtRootSlot.cxx:9
 TQtRootSlot.cxx:10
 TQtRootSlot.cxx:11
 TQtRootSlot.cxx:12
 TQtRootSlot.cxx:13
 TQtRootSlot.cxx:14
 TQtRootSlot.cxx:15
 TQtRootSlot.cxx:16
 TQtRootSlot.cxx:17
 TQtRootSlot.cxx:18
 TQtRootSlot.cxx:19
 TQtRootSlot.cxx:20
 TQtRootSlot.cxx:21
 TQtRootSlot.cxx:22
 TQtRootSlot.cxx:23
 TQtRootSlot.cxx:24
 TQtRootSlot.cxx:25
 TQtRootSlot.cxx:26
 TQtRootSlot.cxx:27
 TQtRootSlot.cxx:28
 TQtRootSlot.cxx:29
 TQtRootSlot.cxx:30
 TQtRootSlot.cxx:31
 TQtRootSlot.cxx:32
 TQtRootSlot.cxx:33
 TQtRootSlot.cxx:34
 TQtRootSlot.cxx:35
 TQtRootSlot.cxx:36
 TQtRootSlot.cxx:37
 TQtRootSlot.cxx:38
 TQtRootSlot.cxx:39
 TQtRootSlot.cxx:40
 TQtRootSlot.cxx:41
 TQtRootSlot.cxx:42
 TQtRootSlot.cxx:43
 TQtRootSlot.cxx:44
 TQtRootSlot.cxx:45
 TQtRootSlot.cxx:46
 TQtRootSlot.cxx:47
 TQtRootSlot.cxx:48
 TQtRootSlot.cxx:49
 TQtRootSlot.cxx:50
 TQtRootSlot.cxx:51
 TQtRootSlot.cxx:52
 TQtRootSlot.cxx:53
 TQtRootSlot.cxx:54
 TQtRootSlot.cxx:55
 TQtRootSlot.cxx:56
 TQtRootSlot.cxx:57
 TQtRootSlot.cxx:58
 TQtRootSlot.cxx:59
 TQtRootSlot.cxx:60
 TQtRootSlot.cxx:61
 TQtRootSlot.cxx:62
 TQtRootSlot.cxx:63
 TQtRootSlot.cxx:64
 TQtRootSlot.cxx:65
 TQtRootSlot.cxx:66
 TQtRootSlot.cxx:67
 TQtRootSlot.cxx:68
 TQtRootSlot.cxx:69
 TQtRootSlot.cxx:70
 TQtRootSlot.cxx:71
 TQtRootSlot.cxx:72
 TQtRootSlot.cxx:73
 TQtRootSlot.cxx:74
 TQtRootSlot.cxx:75
 TQtRootSlot.cxx:76
 TQtRootSlot.cxx:77
 TQtRootSlot.cxx:78
 TQtRootSlot.cxx:79
 TQtRootSlot.cxx:80
 TQtRootSlot.cxx:81
 TQtRootSlot.cxx:82
 TQtRootSlot.cxx:83
 TQtRootSlot.cxx:84
 TQtRootSlot.cxx:85
 TQtRootSlot.cxx:86
 TQtRootSlot.cxx:87
 TQtRootSlot.cxx:88
 TQtRootSlot.cxx:89
 TQtRootSlot.cxx:90
 TQtRootSlot.cxx:91
 TQtRootSlot.cxx:92
 TQtRootSlot.cxx:93
 TQtRootSlot.cxx:94
 TQtRootSlot.cxx:95
 TQtRootSlot.cxx:96
 TQtRootSlot.cxx:97
 TQtRootSlot.cxx:98
 TQtRootSlot.cxx:99
 TQtRootSlot.cxx:100
 TQtRootSlot.cxx:101
 TQtRootSlot.cxx:102
 TQtRootSlot.cxx:103
 TQtRootSlot.cxx:104
 TQtRootSlot.cxx:105
 TQtRootSlot.cxx:106
 TQtRootSlot.cxx:107
 TQtRootSlot.cxx:108
 TQtRootSlot.cxx:109
 TQtRootSlot.cxx:110
 TQtRootSlot.cxx:111
 TQtRootSlot.cxx:112
 TQtRootSlot.cxx:113
 TQtRootSlot.cxx:114
 TQtRootSlot.cxx:115
 TQtRootSlot.cxx:116
 TQtRootSlot.cxx:117
 TQtRootSlot.cxx:118
 TQtRootSlot.cxx:119
 TQtRootSlot.cxx:120
 TQtRootSlot.cxx:121
 TQtRootSlot.cxx:122
 TQtRootSlot.cxx:123
 TQtRootSlot.cxx:124
 TQtRootSlot.cxx:125
 TQtRootSlot.cxx:126
 TQtRootSlot.cxx:127
 TQtRootSlot.cxx:128
 TQtRootSlot.cxx:129
 TQtRootSlot.cxx:130
 TQtRootSlot.cxx:131