Computer Assited Medical Intervention Tool Kit  version 3.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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-2014 UJF-Grenoble 1, 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
38 {
72 class ConsoleStream : public std::basic_streambuf<char>
73 {
74 public:
76  ConsoleStream(std::ostream *stream, QTextEdit* textEdit) {
77  init(stream, textEdit);
78  }
79 
82  previousBuffer = NULL;
83  logTextEdit = NULL;
84  myStream = NULL;
85  }
86 
89  // output anything that is left
90  if (!myString.empty())
91  logTextEdit->append(myString.c_str());
92 
93  free();
94  }
95 
97  void setStream(std::ostream *stream) {
98  free();
99  myStream = stream;
100  previousBuffer = stream->rdbuf();
101  stream->rdbuf(this);
102  }
103 
105  void setTextEdit(QTextEdit* text_edit) {
106  logTextEdit = text_edit;
107  }
108 
110  void init(std::ostream *stream, QTextEdit* textEdit) {
111  setTextEdit(textEdit);
112  setStream(stream);
113  }
114 
116  void free() {
117  if (previousBuffer != NULL && myStream != NULL)
118  myStream->rdbuf(previousBuffer);
119  }
120 protected:
122  virtual 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  return v;
131  }
132 
134  virtual std::streamsize xsputn(const char *p, std::streamsize n) {
135  myString.append(p, p + n);
136 
137  std::string::size_type pos = 0;
138  while (pos != std::string::npos) {
139  pos = myString.find('\n');
140  if (pos != std::string::npos) {
141  std::string tmp(myString.begin(), myString.begin() + pos);
142  logTextEdit->append(tmp.c_str());
143  myString.erase(myString.begin(), myString.begin() + pos + 1);
144  }
145  }
146 
147  return n;
148  }
149 
150 private:
151  std::ostream *myStream;
152  std::streambuf *previousBuffer;
153  std::string myString;
154  QTextEdit* logTextEdit;
155 };
156 
157 }
158 
159 
160 
161 #endif
~ConsoleStream()
destructor: use free() to restore previous stream output buffer
Definition: ConsoleStream.h:88
void init(std::ostream *stream, QTextEdit *textEdit)
initialize ConsoleStream using both input stream and output text edit
Definition: ConsoleStream.h:110
QTextEdit * logTextEdit
Definition: ConsoleStream.h:154
std::ostream * myStream
Definition: ConsoleStream.h:151
virtual int_type overflow(int_type v)
rewriting of the inherited method overflow
Definition: ConsoleStream.h:122
void setStream(std::ostream *stream)
set the value for the buffer to be replaced by the ConsoleStream
Definition: ConsoleStream.h:97
Definition: Action.h:40
void free()
reset the state as it was before (stream use the old buffer again)
Definition: ConsoleStream.h:116
Provides a console windows, within the CamiTK application.
Definition: ConsoleStream.h:72
ConsoleStream()
default constructor, init(..) have to be called later, before first use
Definition: ConsoleStream.h:81
std::streambuf * previousBuffer
Definition: ConsoleStream.h:152
virtual std::streamsize xsputn(const char *p, std::streamsize n)
rewriting of the inherited method xsputn
Definition: ConsoleStream.h:134
std::string myString
Definition: ConsoleStream.h:153
ConsoleStream(std::ostream *stream, QTextEdit *textEdit)
constructor to use when you are sure about both paramaters
Definition: ConsoleStream.h:76
void setTextEdit(QTextEdit *text_edit)
set the log QTextEdit
Definition: ConsoleStream.h:105