RedstartCoreOutputPlainTextEdit.h 2.23 KB
Newer Older
Dipl.-Ing. Jonas Stienen's avatar
WIP    
Dipl.-Ing. Jonas Stienen committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/*
*  --------------------------------------------------------------------------------------------
*
*    VVV        VVV A           Virtual Acoustics (VA) | http://www.virtualacoustics.org
*     VVV      VVV AAA          Licensed under the Apache License, Version 2.0
*      VVV    VVV   AAA
*       VVV  VVV     AAA        Copyright 2015-2017
*        VVVVVV       AAA       Institute of Technical Acoustics (ITA)
*         VVVV         AAA      RWTH Aachen University
*
*  --------------------------------------------------------------------------------------------
*/
#ifndef IW_REDSTART_CORE_OUTPUT_PLAIN_TEXT_EDIT
#define IW_REDSTART_CORE_OUTPUT_PLAIN_TEXT_EDIT

#include <QPlainTextEdit>
#include <QStringListModel>
Dipl.-Ing. Jonas Stienen's avatar
WIP    
Dipl.-Ing. Jonas Stienen committed
18
#include <QMutex>
Dipl.-Ing. Jonas Stienen's avatar
WIP    
Dipl.-Ing. Jonas Stienen committed
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36

#include <iostream>


class RedstartCoreOutputPlainTextEdit : public QPlainTextEdit
{
	class CStreamBuffer : public std::streambuf
	{
	public:
		CStreamBuffer( RedstartCoreOutputPlainTextEdit* pParent )
			: m_pParent( pParent )
		{
			setp( 0, 0 );
			setg( 0, 0, 0 );
		};

		virtual int_type overflow( int_type c )
		{
37
			if( c != EOF && c != '\n' )
Dipl.-Ing. Jonas Stienen's avatar
WIP    
Dipl.-Ing. Jonas Stienen committed
38
				m_pParent->write( ( char ) c );
Dipl.-Ing. Jonas Stienen's avatar
Testing    
Dipl.-Ing. Jonas Stienen committed
39
40
			else
				m_pParent->append();
Dipl.-Ing. Jonas Stienen's avatar
WIP    
Dipl.-Ing. Jonas Stienen committed
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72

			return c;
		};

	private:
		RedstartCoreOutputPlainTextEdit* m_pParent;
	};

	class COutStream : public std::ostream
	{
	public:
		inline COutStream( RedstartCoreOutputPlainTextEdit* pParent )
			: sb( pParent )
			, std::ostream( &sb )
			, std::ios( 0 )
		{
		};

		inline ~COutStream() 
		{
			sb.pubsync();
		};

	private:
		CStreamBuffer sb;
	};

	Q_OBJECT
public:
	inline RedstartCoreOutputPlainTextEdit( QWidget* pParent )
		: QPlainTextEdit( pParent )
	{
Dipl.-Ing. Jonas Stienen's avatar
WIP    
Dipl.-Ing. Jonas Stienen committed
73
		m_pOutStream = new COutStream( this );		
Dipl.-Ing. Jonas Stienen's avatar
WIP    
Dipl.-Ing. Jonas Stienen committed
74
75
76
77
78
79
80
81
82
83
84
85
86
	};

	inline ~RedstartCoreOutputPlainTextEdit()
	{
		delete m_pOutStream;
		m_pOutStream = NULL;
	};

	inline std::ostream* GetOutputStream()
	{
		return m_pOutStream;
	};

Dipl.-Ing. Jonas Stienen's avatar
Testing    
Dipl.-Ing. Jonas Stienen committed
87
88
	inline void append()
	{
Dipl.-Ing. Jonas Stienen's avatar
WIP    
Dipl.-Ing. Jonas Stienen committed
89
90
		// Append a copy
		m_qLock.lock();
Dipl.-Ing. Jonas Stienen's avatar
Testing    
Dipl.-Ing. Jonas Stienen committed
91
		appendPlainText( m_sLine );
92
		m_sLine.clear();
Dipl.-Ing. Jonas Stienen's avatar
WIP    
Dipl.-Ing. Jonas Stienen committed
93
		m_qLock.unlock();
Dipl.-Ing. Jonas Stienen's avatar
Testing    
Dipl.-Ing. Jonas Stienen committed
94
95
	};

Dipl.-Ing. Jonas Stienen's avatar
WIP    
Dipl.-Ing. Jonas Stienen committed
96
97
	inline void write( char c )
	{
Dipl.-Ing. Jonas Stienen's avatar
Testing    
Dipl.-Ing. Jonas Stienen committed
98
		m_sLine += QString( 1, c );
Dipl.-Ing. Jonas Stienen's avatar
WIP    
Dipl.-Ing. Jonas Stienen committed
99
100
101
102
	};

	inline void write( const std::string& str )
	{
Dipl.-Ing. Jonas Stienen's avatar
Testing    
Dipl.-Ing. Jonas Stienen committed
103
		m_sLine += QString( str.c_str() );
Dipl.-Ing. Jonas Stienen's avatar
WIP    
Dipl.-Ing. Jonas Stienen committed
104
105
106
107
108
109
	};

	public slots :

private:
	COutStream* m_pOutStream;
Dipl.-Ing. Jonas Stienen's avatar
Testing    
Dipl.-Ing. Jonas Stienen committed
110
	QString m_sLine;
Dipl.-Ing. Jonas Stienen's avatar
WIP    
Dipl.-Ing. Jonas Stienen committed
111
	QMutex m_qLock;
Dipl.-Ing. Jonas Stienen's avatar
WIP    
Dipl.-Ing. Jonas Stienen committed
112
113
114
115
};


#endif // IW_REDSTART_CORE_OUTPUT_PLAIN_TEXT_EDIT