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