Computer Assited Medical Intervention Tool Kit
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros 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-2013 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 {
68 class ConsoleStream : public std::basic_streambuf<char>
69 {
70 public:
72  ConsoleStream(std::ostream *stream, QTextEdit* textEdit) {
73  init(stream, textEdit);
74  }
75 
78  previousBuffer = NULL;
79  logTextEdit = NULL;
80  myStream = NULL;
81  }
82 
85  // output anything that is left
86  if (!myString.empty())
87  logTextEdit->append(myString.c_str());
88 
89  free();
90  }
91 
93  void setStream(std::ostream *stream) {
94  free();
95  myStream = stream;
96  previousBuffer = stream->rdbuf();
97  stream->rdbuf(this);
98  }
99 
101  void setTextEdit(QTextEdit* text_edit) {
102  logTextEdit = text_edit;
103  }
104 
106  void init(std::ostream *stream, QTextEdit* textEdit) {
107  setTextEdit(textEdit);
108  setStream(stream);
109  }
110 
112  void free() {
113  if (previousBuffer != NULL && myStream != NULL)
114  myStream->rdbuf(previousBuffer);
115  }
116 protected:
118  virtual int_type overflow(int_type v) {
119  if (v == '\n') {
120  logTextEdit->append(myString.c_str());
121  myString.erase(myString.begin(), myString.end());
122  }
123  else
124  myString += v;
125 
126  return v;
127  }
128 
130  virtual std::streamsize xsputn(const char *p, std::streamsize n) {
131  myString.append(p, p + n);
132 
133  std::string::size_type pos = 0;
134  while (pos != std::string::npos) {
135  pos = myString.find('\n');
136  if (pos != std::string::npos) {
137  std::string tmp(myString.begin(), myString.begin() + pos);
138  logTextEdit->append(tmp.c_str());
139  myString.erase(myString.begin(), myString.begin() + pos + 1);
140  }
141  }
142 
143  return n;
144  }
145 
146 private:
147  std::ostream *myStream;
148  std::streambuf *previousBuffer;
149  std::string myString;
150  QTextEdit* logTextEdit;
151 };
152 
153 }
154 
155 
156 
157 #endif