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