Computer Assited Medical Intervention Tool Kit  version 4.1
ConsoleStream.h
Go to the documentation of this file.
1 /*****************************************************************************
2  * $CAMITK_LICENCE_BEGIN$
3  *
4  * CamiTK - Computer Assisted Medical Intervention ToolKit
5  * (c) 2001-2018 Univ. Grenoble Alpes, CNRS, TIMC-IMAG UMR 5525 (GMCAO)
6  *
7  * Visit http://camitk.imag.fr for more information
8  *
9  * This file is part of CamiTK.
10  *
11  * CamiTK is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * CamiTK is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU Lesser General Public License version 3 for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * version 3 along with CamiTK. If not, see <http://www.gnu.org/licenses/>.
22  *
23  * $CAMITK_LICENCE_END$
24  ****************************************************************************/
25 
26 #ifndef ConsoleStream_H
27 #define ConsoleStream_H
28 
29 // -- stl stuff
30 #include <iostream>
31 #include <streambuf>
32 #include <string>
33 
34 // -- QT stuff
35 #include <QTextEdit>
36 
37 namespace camitk {
71 class ConsoleStream : public std::basic_streambuf<char> {
72 public:
74  ConsoleStream(std::ostream* stream, QTextEdit* textEdit) {
75  init(stream, textEdit);
76  }
77 
80  previousBuffer = nullptr;
81  logTextEdit = nullptr;
82  myStream = nullptr;
83  }
84 
87  // output anything that is left
88  if (!myString.empty()) {
89  logTextEdit->append(myString.c_str());
90  }
91 
92  free();
93  }
94 
96  void setStream(std::ostream* stream) {
97  free();
98  myStream = stream;
99  previousBuffer = stream->rdbuf();
100  stream->rdbuf(this);
101  }
102 
104  void setTextEdit(QTextEdit* text_edit) {
105  logTextEdit = text_edit;
106  }
107 
109  void init(std::ostream* stream, QTextEdit* textEdit) {
110  setTextEdit(textEdit);
111  setStream(stream);
112  }
113 
115  void free() {
116  if (previousBuffer != nullptr && myStream != nullptr) {
117  myStream->rdbuf(previousBuffer);
118  }
119  }
120 protected:
122  int_type overflow(int_type v) {
123  if (v == '\n') {
124  logTextEdit->append(myString.c_str());
125  myString.erase(myString.begin(), myString.end());
126  }
127  else {
128  myString += v;
129  }
130 
131  return v;
132  }
133 
135  std::streamsize xsputn(const char* p, std::streamsize n) {
136  myString.append(p, p + n);
137 
138  std::string::size_type pos = 0;
139  while (pos != std::string::npos) {
140  pos = myString.find('\n');
141  if (pos != std::string::npos) {
142  std::string tmp(myString.begin(), myString.begin() + pos);
143  logTextEdit->append(tmp.c_str());
144  myString.erase(myString.begin(), myString.begin() + pos + 1);
145  }
146  }
147 
148  return n;
149  }
150 
151 private:
152  std::ostream* myStream;
153  std::streambuf* previousBuffer;
154  std::string myString;
155  QTextEdit* logTextEdit;
156 };
157 
158 }
159 
160 
161 
162 #endif
int_type overflow(int_type v)
rewriting of the inherited method overflow
Definition: ConsoleStream.h:122
~ConsoleStream()
destructor: use free() to restore previous stream output buffer
Definition: ConsoleStream.h:86
void init(std::ostream *stream, QTextEdit *textEdit)
initialize ConsoleStream using both input stream and output text edit
Definition: ConsoleStream.h:109
QTextEdit * logTextEdit
Definition: ConsoleStream.h:155
std::ostream * myStream
Definition: ConsoleStream.h:152
void setStream(std::ostream *stream)
set the value for the buffer to be replaced by the ConsoleStream
Definition: ConsoleStream.h:96
Definition: Action.cpp:36
void free()
reset the state as it was before (stream use the old buffer again)
Definition: ConsoleStream.h:115
Provides a console windows, within the CamiTK application.
Definition: ConsoleStream.h:71
ConsoleStream()
default constructor, init(..) have to be called later, before first use
Definition: ConsoleStream.h:79
std::streambuf * previousBuffer
Definition: ConsoleStream.h:153
std::string myString
Definition: ConsoleStream.h:154
ConsoleStream(std::ostream *stream, QTextEdit *textEdit)
constructor to use when you are sure about both paramaters
Definition: ConsoleStream.h:74
std::streamsize xsputn(const char *p, std::streamsize n)
rewriting of the inherited method xsputn
Definition: ConsoleStream.h:135
void setTextEdit(QTextEdit *text_edit)
set the log QTextEdit
Definition: ConsoleStream.h:104