<commit_before>// -*- Mode: C++; tab-width: 2; -*-
// vi: set ts=2:
//
// $Id: pyWidget.C,v 1.25 2004/04/19 16:51:12 amoll Exp $
//

// This include has to be first in order to avoid collisions.
#include <Python.h>

#include <BALL/VIEW/WIDGETS/pyWidget.h>
#include <BALL/VIEW/KERNEL/mainControl.h>
#include <BALL/VIEW/DIALOGS/pythonSettings.h>
#include <BALL/PYTHON/pyInterpreter.h>
#include <BALL/VIEW/DIALOGS/preferences.h>
#include <BALL/FORMAT/lineBasedFile.h>

#include <qscrollbar.h>
#include <qfiledialog.h>

namespace BALL
{
	namespace VIEW
	{

		PyWidgetData::PyWidgetData(QWidget* parent, const char* name)
			: QTextEdit(parent, name),
				stop_script_(false)
		{
			setWrapPolicy(QTextEdit::Anywhere);
		}

		PyWidgetData::PyWidgetData(const PyWidgetData& /*widget*/)
			:	QTextEdit(),
				stop_script_(false)
		{
		}

		PyWidgetData::~PyWidgetData()
			throw()
		{	
		}


		void PyWidgetData::stopInterpreter()
		{
			PyInterpreter::finalize();
		}


		void PyWidgetData::startInterpreter()
		{
			// initialize the interpreter
			PyInterpreter::initialize();

			// print the PyBALL version and clear
			// the widget's contents in case of a restart
			setText((String("BALL ") + VersionInfo::getVersion()).c_str());

			// print the first prompt 
			multi_line_mode_ = false;
			newPrompt_();
			history_position_ = history_.size() + 1;
		}


		void PyWidgetData::retrieveHistoryLine_(Position index)
		{
			if (index > history_.size()) 
			{
				history_position_ = history_.size();
				return;
			}

			int row, col;
			getCursorPosition(&row, &col);

			if (index == history_.size())
			{
				history_position_ = history_.size();
				removeParagraph(row);
				insertParagraph(getPrompt_(), row);
				setCursorPosition(paragraphs()-1, col);
				QScrollBar* sb = verticalScrollBar();
				if (sb != 0)
				{
					sb->setValue(sb->maxValue());
				}
				removeParagraph(row +1);
				return;
			}

			String line = getPrompt_()+ history_[index];

			// replace the line's contents
			removeParagraph(row);
			insertParagraph(line.c_str(), row);
			setCursorPosition(row, line.size()); 
			QScrollBar* sb = verticalScrollBar();
			if (sb != 0)
			{
				sb->setValue(sb->maxValue());
			}

			// update the history position
			history_position_ = index;
			removeParagraph(row +1);
		}


		bool PyWidgetData::returnPressed()
		{
			// check for an empty line (respect the prompt)
			int row, col;
			getCursorPosition(&row, &col);
			current_line_ = getCurrentLine_();
			if (col < 5)
			{
				if (multi_line_mode_)
				{
					// in multi line mode: end of input - parse it!
					QTextEdit::returnPressed();
					parseLine_();
				}
				else	
				{
					// return on an empty line is handled 
					// as in the interactive interpreter: do nothing and 
					// print another prompt
					QTextEdit::returnPressed();
					newPrompt_();
				}
			} 
			else 
			{	
				// parse the line
				QTextEdit::returnPressed();	
				parseLine_();
			}

			return false;
		}


		bool PyWidgetData::parseLine_(String line)
		{
			if (!Py_IsInitialized())
			{
				append("ERROR: no interpreter running!\n");
				return false;
			}

			if (stop_script_) 
			{
				stop_script_ = false;
				return true;
			}

			// check for comments<commit_msg>no message<commit_after>// -*- Mode: C++; tab-width: 2; -*-
// vi: set ts=2:
//
// $Id: pyWidget.C,v 1.26 2004/04/19 17:27:56 amoll Exp $
//

// This include has to be first in order to avoid collisions.
#include <Python.h>

#include <BALL/VIEW/WIDGETS/pyWidget.h>
#include <BALL/VIEW/KERNEL/mainControl.h>
#include <BALL/VIEW/DIALOGS/pythonSettings.h>
#include <BALL/PYTHON/pyInterpreter.h>
#include <BALL/VIEW/DIALOGS/preferences.h>
#include <BALL/FORMAT/lineBasedFile.h>

#include <qscrollbar.h>
#include <qfiledialog.h>

namespace BALL
{
	namespace VIEW
	{

		PyWidgetData::PyWidgetData(QWidget* parent, const char* name)
			: QTextEdit(parent, name),
				stop_script_(false)
		{
			setWrapPolicy(QTextEdit::Anywhere);
		}

		PyWidgetData::PyWidgetData(const PyWidgetData& /*widget*/)
			:	QTextEdit(),
				stop_script_(false)
		{
		}

		PyWidgetData::~PyWidgetData()
			throw()
		{	
		}


		void PyWidgetData::stopInterpreter()
		{
			PyInterpreter::finalize();
		}


		void PyWidgetData::startInterpreter()
		{
			// initialize the interpreter
			PyInterpreter::initialize();

			// print the PyBALL version and clear
			// the widget's contents in case of a restart
			setText((String("BALL ") + VersionInfo::getVersion()).c_str());

			// print the first prompt 
			multi_line_mode_ = false;
			newPrompt_();
			history_position_ = history_.size() + 1;
		}


		void PyWidgetData::retrieveHistoryLine_(Position index)
		{
			if (index > history_.size()) 
			{
				history_position_ = history_.size();
				return;
			}

			int row, col;
			getCursorPosition(&row, &col);

			if (index == history_.size())
			{
				history_position_ = history_.size();
				removeParagraph(row);
				insertParagraph(getPrompt_(), row);
				setCursorPosition(paragraphs()-1, col);
				QScrollBar* sb = verticalScrollBar();
				if (sb != 0)
				{
					sb->setValue(sb->maxValue());
				}
				removeParagraph(row +1);
				return;
			}

			String line = getPrompt_()+ history_[index];

			// replace the line's contents
			removeParagraph(row);
			insertParagraph(line.c_str(), row);
			setCursorPosition(row, line.size()); 
			QScrollBar* sb = verticalScrollBar();
			if (sb != 0)
			{
				sb->setValue(sb->maxValue());
			}

			// update the history position
			history_position_ = index;
			removeParagraph(row +1);
		}


		bool PyWidgetData::returnPressed()
		{
			// check for an empty line (respect the prompt)
			int row, col;
			getCursorPosition(&row, &col);
			current_line_ = getCurrentLine_();
			QTextEdit::returnPressed();
			if (col < 5)
			{
				if (multi_line_mode_)
				{
					// in multi line mode: end of input - parse it!
					parseLine_();
				}
				else	
				{
					// return on an empty line is handled 
					// as in the interactive interpreter: do nothing and 
					// print another prompt
					newPrompt_();
				}
			} 
			else 
			{	
				// parse the line
				parseLine_();
			}

			return false;
		}


		bool PyWidgetData::parseLine_(String line)
		{
			if (!Py_IsInitialized())
			{
				append("ERROR: no interpreter running!\n");
				return false;
			}

			if (stop_script_) 
			{
				stop_script_ = false;
				return true;
			}

			// check for comments
{'fraction_non_alphanumeric': 0.1370376151084751, 'fraction_numerical': 0.008116122990479164, 'mean_word_length': 2.9801242236024845, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3603636', 'n_tokens_mistral': 2746, 'n_tokens_neox': 2194, 'n_words': 484}