textoutput.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #include <QtGui>
00039
00040 #include <iostream>
00041 #include <gecode/gist/textoutput.hh>
00042 #include <gecode/gist/gecodelogo.hh>
00043
00044 namespace Gecode { namespace Gist {
00045
00047 class GistOutputStream
00048 : public std::basic_ostream<char, std::char_traits<char> > {
00050 class Buf
00051 : public std::basic_streambuf<char, std::char_traits<char> > {
00052 QString buffer;
00053 QTextEdit* editor;
00054 public:
00055 void flush(void) {
00056 QTextBlockFormat bf = editor->textCursor().blockFormat();
00057 bf.setBottomMargin(0);
00058 editor->textCursor().setBlockFormat(bf);
00059 editor->append(buffer);
00060 buffer.clear();
00061 }
00062 virtual int overflow(int v = std::char_traits<char>::eof()) {
00063 if (v == '\n') {
00064 flush();
00065 } else {
00066 buffer += (char)v;
00067 }
00068 return v;
00069 }
00070 Buf(QTextEdit* e) : editor(e) {}
00071 };
00072
00073 Buf _buf;
00074 public:
00075 GistOutputStream(QTextEdit* editor)
00076 : std::basic_ostream<char, std::char_traits<char> >(&_buf),
00077 _buf(editor) {
00078 clear();
00079 }
00080 void flush(void) {
00081 _buf.flush();
00082 }
00083 };
00084
00085 TextOutputI::TextOutputI(const std::string& name, QWidget *parent)
00086 : QMainWindow(parent) {
00087 Logos logos;
00088
00089 QPixmap myPic;
00090 myPic.loadFromData(logos.gistLogo, logos.gistLogoSize);
00091 setWindowIcon(myPic);
00092
00093 QFont font;
00094 QString fontFamily("Courier");
00095 font.setFamily(fontFamily);
00096 font.setFixedPitch(true);
00097 font.setPointSize(12);
00098
00099
00100 editor = new QTextEdit;
00101 editor->setFont(font);
00102 editor->setReadOnly(true);
00103 editor->setLineWrapMode(QTextEdit::FixedColumnWidth);
00104 editor->setLineWrapColumnOrWidth(80);
00105 os = new GistOutputStream(editor);
00106
00107 QAction* clearText = new QAction("Clear", this);
00108 clearText->setShortcut(QKeySequence("Ctrl+K"));
00109 this->addAction(clearText);
00110 connect(clearText, SIGNAL(triggered()), editor,
00111 SLOT(clear()));
00112
00113 QAction* closeWindow = new QAction("Close window", this);
00114 closeWindow->setShortcut(QKeySequence("Ctrl+W"));
00115 this->addAction(closeWindow);
00116 connect(closeWindow, SIGNAL(triggered()), this,
00117 SLOT(close()));
00118
00119 QToolBar* t = addToolBar("Tools");
00120 t->setFloatable(false);
00121 t->setMovable(false);
00122 t->addAction(clearText);
00123
00124 stayOnTop = new QAction("Stay on top", this);
00125 stayOnTop->setCheckable(true);
00126 t->addAction(stayOnTop);
00127 connect(stayOnTop, SIGNAL(changed()), this,
00128 SLOT(changeStayOnTop()));
00129
00130 changeStayOnTop();
00131 setCentralWidget(editor);
00132 setWindowTitle(QString((std::string("Gist Console: ") + name).c_str()));
00133
00134 setAttribute(Qt::WA_QuitOnClose, false);
00135 setAttribute(Qt::WA_DeleteOnClose, false);
00136 resize(600,300);
00137 }
00138
00139 TextOutputI::~TextOutputI(void) {
00140 delete os;
00141 }
00142
00143 std::ostream&
00144 TextOutputI::getStream(void) {
00145 return *os;
00146 }
00147
00148 void
00149 TextOutputI::flush(void) {
00150 static_cast<GistOutputStream*>(os)->flush();
00151 }
00152
00153 void
00154 TextOutputI::insertHtml(const QString& s) {
00155 QTextBlockFormat bf = editor->textCursor().blockFormat();
00156 bf.setBottomMargin(0);
00157 editor->textCursor().setBlockFormat(bf);
00158 editor->insertHtml(s);
00159 editor->ensureCursorVisible();
00160 }
00161
00162 void TextOutputI::changeStayOnTop(void) {
00163 QPoint p = pos();
00164 if (stayOnTop->isChecked()) {
00165 setWindowFlags(windowFlags() | Qt::WindowStaysOnTopHint);
00166 } else {
00167 setWindowFlags(windowFlags() & ~Qt::WindowStaysOnTopHint);
00168 }
00169 move(p);
00170 show();
00171 }
00172
00173 }}
00174
00175