Generated on Fri Oct 19 11:25:00 2018 for Gecode by doxygen 1.6.3

textoutput.cpp

Go to the documentation of this file.
00001 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
00002 /*
00003  *  Main authors:
00004  *     Guido Tack <tack@gecode.org>
00005  *
00006  *  Copyright:
00007  *     Guido Tack, 2006
00008  *
00009  *  This file is part of Gecode, the generic constraint
00010  *  development environment:
00011  *     http://www.gecode.org
00012  *
00013  * Permission is hereby granted, free of charge, to any person obtaining
00014  * a copy of this software and associated documentation files (the
00015  * "Software"), to deal in the Software without restriction, including
00016  * without limitation the rights to use, copy, modify, merge, publish,
00017  * distribute, sublicense, and/or sell copies of the Software, and to
00018  * permit persons to whom the Software is furnished to do so, subject to
00019  * the following conditions:
00020  *
00021  * The above copyright notice and this permission notice shall be
00022  * included in all copies or substantial portions of the Software.
00023  *
00024  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00025  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00026  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00027  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
00028  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
00029  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00030  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
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 // STATISTICS: gist-any