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 protected:
00055 virtual int overflow(int v = std::char_traits<char>::eof()) {
00056 if (v == '\n') {
00057 QTextBlockFormat bf = editor->textCursor().blockFormat();
00058 bf.setBottomMargin(0);
00059 editor->textCursor().setBlockFormat(bf);
00060 editor->append(buffer);
00061 buffer.clear();
00062 } else {
00063 buffer += (char)v;
00064 }
00065 return v;
00066 }
00067 public:
00068 Buf(QTextEdit* e) : editor(e) {}
00069 };
00070
00071 Buf _buf;
00072 public:
00073 GistOutputStream(QTextEdit* editor)
00074 : std::basic_ostream<char, std::char_traits<char> >(&_buf),
00075 _buf(editor) {
00076 clear();
00077 }
00078 };
00079
00080 TextOutput::TextOutput(const std::string& name, QWidget *parent)
00081 : QMainWindow(parent) {
00082 Logos logos;
00083
00084 QPixmap myPic;
00085 myPic.loadFromData(logos.gistLogo, logos.gistLogoSize);
00086 setWindowIcon(myPic);
00087
00088 QFont font;
00089 QString fontFamily("Courier");
00090 font.setFamily(fontFamily);
00091 font.setFixedPitch(true);
00092 font.setPointSize(12);
00093
00094
00095 editor = new QTextEdit;
00096 editor->setFont(font);
00097 editor->setReadOnly(true);
00098 editor->setLineWrapMode(QTextEdit::FixedColumnWidth);
00099 editor->setLineWrapColumnOrWidth(80);
00100 os = new GistOutputStream(editor);
00101
00102 QAction* clearText = new QAction("Clear", this);
00103 clearText->setShortcut(QKeySequence("Ctrl+K"));
00104 this->addAction(clearText);
00105 connect(clearText, SIGNAL(triggered()), editor,
00106 SLOT(clear()));
00107
00108 QAction* closeWindow = new QAction("Close window", this);
00109 closeWindow->setShortcut(QKeySequence("Ctrl+W"));
00110 this->addAction(closeWindow);
00111 connect(closeWindow, SIGNAL(triggered()), this,
00112 SLOT(close()));
00113
00114 QToolBar* t = addToolBar("Tools");
00115 t->setFloatable(false);
00116 t->setMovable(false);
00117 t->addAction(clearText);
00118
00119 stayOnTop = new QAction("Stay on top", this);
00120 stayOnTop->setCheckable(true);
00121 t->addAction(stayOnTop);
00122 connect(stayOnTop, SIGNAL(changed()), this,
00123 SLOT(changeStayOnTop()));
00124
00125 changeStayOnTop();
00126 setCentralWidget(editor);
00127 setWindowTitle(QString((std::string("Gist Console: ") + name).c_str()));
00128
00129 setAttribute(Qt::WA_QuitOnClose, false);
00130 setAttribute(Qt::WA_DeleteOnClose, false);
00131 resize(600,300);
00132 }
00133
00134 TextOutput::~TextOutput(void) {
00135 delete os;
00136 }
00137
00138 std::ostream&
00139 TextOutput::getStream(void) {
00140 return *os;
00141 }
00142
00143 void
00144 TextOutput::insertHtml(const QString& s) {
00145 QTextBlockFormat bf = editor->textCursor().blockFormat();
00146 bf.setBottomMargin(0);
00147 editor->textCursor().setBlockFormat(bf);
00148 editor->insertHtml(s);
00149 editor->ensureCursorVisible();
00150 }
00151
00152 void TextOutput::changeStayOnTop(void) {
00153 QPoint p = pos();
00154 if (stayOnTop->isChecked()) {
00155 setWindowFlags(windowFlags() | Qt::WindowStaysOnTopHint);
00156 } else {
00157 setWindowFlags(windowFlags() & ~Qt::WindowStaysOnTopHint);
00158 }
00159 move(p);
00160 show();
00161 }
00162
00163 }}
00164
00165