Generated on Thu Mar 13 2014 04:39:28 for Gecode by doxygen 1.8.1.2
textoutput.cpp
Go to the documentation of this file.
1 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2 /*
3  * Main authors:
4  * Guido Tack <tack@gecode.org>
5  *
6  * Copyright:
7  * Guido Tack, 2006
8  *
9  * Last modified:
10  * $Date: 2011-08-25 18:43:31 +1000 (Thu, 25 Aug 2011) $ by $Author: tack $
11  * $Revision: 12352 $
12  *
13  * This file is part of Gecode, the generic constraint
14  * development environment:
15  * http://www.gecode.org
16  *
17  * Permission is hereby granted, free of charge, to any person obtaining
18  * a copy of this software and associated documentation files (the
19  * "Software"), to deal in the Software without restriction, including
20  * without limitation the rights to use, copy, modify, merge, publish,
21  * distribute, sublicense, and/or sell copies of the Software, and to
22  * permit persons to whom the Software is furnished to do so, subject to
23  * the following conditions:
24  *
25  * The above copyright notice and this permission notice shall be
26  * included in all copies or substantial portions of the Software.
27  *
28  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
30  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
31  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
32  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
33  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
34  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35  *
36  */
37 
38 #include <QtGui>
39 
40 #include <iostream>
43 
44 namespace Gecode { namespace Gist {
45 
48  : public std::basic_ostream<char, std::char_traits<char> > {
50  class Buf
51  : public std::basic_streambuf<char, std::char_traits<char> > {
52  QString buffer;
53  QTextEdit* editor;
54  public:
55  void flush(void) {
56  QTextBlockFormat bf = editor->textCursor().blockFormat();
57  bf.setBottomMargin(0);
58  editor->textCursor().setBlockFormat(bf);
59  editor->append(buffer);
60  buffer.clear();
61  }
62  virtual int overflow(int v = std::char_traits<char>::eof()) {
63  if (v == '\n') {
64  flush();
65  } else {
66  buffer += (char)v;
67  }
68  return v;
69  }
70  Buf(QTextEdit* e) : editor(e) {}
71  };
72 
73  Buf _buf;
74  public:
75  GistOutputStream(QTextEdit* editor)
76  : std::basic_ostream<char, std::char_traits<char> >(&_buf),
77  _buf(editor) {
78  clear();
79  }
80  void flush(void) {
81  _buf.flush();
82  }
83  };
84 
85  TextOutputI::TextOutputI(const std::string& name, QWidget *parent)
86  : QMainWindow(parent) {
87  Logos logos;
88 
89  QPixmap myPic;
90  myPic.loadFromData(logos.gistLogo, logos.gistLogoSize);
91  setWindowIcon(myPic);
92 
93  QFont font;
94  QString fontFamily("Courier");
95  font.setFamily(fontFamily);
96  font.setFixedPitch(true);
97  font.setPointSize(12);
98 
99 
100  editor = new QTextEdit;
101  editor->setFont(font);
102  editor->setReadOnly(true);
103  editor->setLineWrapMode(QTextEdit::FixedColumnWidth);
104  editor->setLineWrapColumnOrWidth(80);
105  os = new GistOutputStream(editor);
106 
107  QAction* clearText = new QAction("Clear", this);
108  clearText->setShortcut(QKeySequence("Ctrl+K"));
109  this->addAction(clearText);
110  connect(clearText, SIGNAL(triggered()), editor,
111  SLOT(clear()));
112 
113  QAction* closeWindow = new QAction("Close window", this);
114  closeWindow->setShortcut(QKeySequence("Ctrl+W"));
115  this->addAction(closeWindow);
116  connect(closeWindow, SIGNAL(triggered()), this,
117  SLOT(close()));
118 
119  QToolBar* t = addToolBar("Tools");
120  t->setFloatable(false);
121  t->setMovable(false);
122  t->addAction(clearText);
123 
124  stayOnTop = new QAction("Stay on top", this);
125  stayOnTop->setCheckable(true);
126  t->addAction(stayOnTop);
127  connect(stayOnTop, SIGNAL(changed()), this,
128  SLOT(changeStayOnTop()));
129 
130  changeStayOnTop();
131  setCentralWidget(editor);
132  setWindowTitle(QString((std::string("Gist Console: ") + name).c_str()));
133 
134  setAttribute(Qt::WA_QuitOnClose, false);
135  setAttribute(Qt::WA_DeleteOnClose, false);
136  resize(600,300);
137  }
138 
140  delete os;
141  }
142 
143  std::ostream&
145  return *os;
146  }
147 
148  void
150  static_cast<GistOutputStream*>(os)->flush();
151  }
152 
153  void
154  TextOutputI::insertHtml(const QString& s) {
155  QTextBlockFormat bf = editor->textCursor().blockFormat();
156  bf.setBottomMargin(0);
157  editor->textCursor().setBlockFormat(bf);
158  editor->insertHtml(s);
159  editor->ensureCursorVisible();
160  }
161 
163  QPoint p = pos();
164  if (stayOnTop->isChecked()) {
165  setWindowFlags(windowFlags() | Qt::WindowStaysOnTopHint);
166  } else {
167  setWindowFlags(windowFlags() & ~Qt::WindowStaysOnTopHint);
168  }
169  move(p);
170  show();
171  }
172 
173 }}
174 
175 // STATISTICS: gist-any