Computer Assited Medical Intervention Tool Kit  version 4.0
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-2016 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 = NULL;
81  logTextEdit = NULL;
82  myStream = NULL;
83  }
84 
87  // output anything that is left
88  if (!myString.empty())
89  logTextEdit->append(myString.c_str());
90 
91  free();
92  }
93 
95  void setStream(std::ostream *stream) {
96  free();
97  myStream = stream;
98  previousBuffer = stream->rdbuf();
99  stream->rdbuf(this);
100  }
101 
103  void setTextEdit(QTextEdit* text_edit) {
104  logTextEdit = text_edit;
105  }
106 
108  void init(std::ostream *stream, QTextEdit* textEdit) {
109  setTextEdit(textEdit);
110  setStream(stream);
111  }
112 
114  void free() {
115  if (previousBuffer != NULL && myStream != NULL)
116  myStream->rdbuf(previousBuffer);
117  }
118 protected:
120  virtual int_type overflow(int_type v) {
121  if (v == '\n') {
122  logTextEdit->append(myString.c_str());
123  myString.erase(myString.begin(), myString.end());
124  } else
125  myString += v;
126 
127  return v;
128  }
129 
131  virtual std::streamsize xsputn(const char *p, std::streamsize n) {
132  myString.append(p, p + n);
133 
134  std::string::size_type pos = 0;
135  while (pos != std::string::npos) {
136  pos = myString.find('\n');
137  if (pos != std::string::npos) {
138  std::string tmp(myString.begin(), myString.begin() + pos);
139  logTextEdit->append(tmp.c_str());
140  myString.erase(myString.begin(), myString.begin() + pos + 1);
141  }
142  }
143 
144  return n;
145  }
146 
147 private:
148  std::ostream *myStream;
149  std::streambuf *previousBuffer;
150  std::string myString;
151  QTextEdit* logTextEdit;
152 };
153 
154 }
155 
156 
157 
158 #endif
~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:108
QTextEdit * logTextEdit
Definition: ConsoleStream.h:151
std::ostream * myStream
Definition: ConsoleStream.h:148
virtual int_type overflow(int_type v)
rewriting of the inherited method overflow
Definition: ConsoleStream.h:120
void setStream(std::ostream *stream)
set the value for the buffer to be replaced by the ConsoleStream
Definition: ConsoleStream.h:95
Definition: Action.cpp:40
void free()
reset the state as it was before (stream use the old buffer again)
Definition: ConsoleStream.h:114
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:149
virtual std::streamsize xsputn(const char *p, std::streamsize n)
rewriting of the inherited method xsputn
Definition: ConsoleStream.h:131
std::string myString
Definition: ConsoleStream.h:150
ConsoleStream(std::ostream *stream, QTextEdit *textEdit)
constructor to use when you are sure about both paramaters
Definition: ConsoleStream.h:74
void setTextEdit(QTextEdit *text_edit)
set the log QTextEdit
Definition: ConsoleStream.h:103