Generated on Tue Apr 18 10:21:46 2017 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  *  Last modified:
00010  *     $Date: 2016-04-19 17:19:45 +0200 (Tue, 19 Apr 2016) $ by $Author: schulte $
00011  *     $Revision: 14967 $
00012  *
00013  *  This file is part of Gecode, the generic constraint
00014  *  development environment:
00015  *     http://www.gecode.org
00016  *
00017  * Permission is hereby granted, free of charge, to any person obtaining
00018  * a copy of this software and associated documentation files (the
00019  * "Software"), to deal in the Software without restriction, including
00020  * without limitation the rights to use, copy, modify, merge, publish,
00021  * distribute, sublicense, and/or sell copies of the Software, and to
00022  * permit persons to whom the Software is furnished to do so, subject to
00023  * the following conditions:
00024  *
00025  * The above copyright notice and this permission notice shall be
00026  * included in all copies or substantial portions of the Software.
00027  *
00028  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00029  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00030  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00031  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
00032  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
00033  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00034  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
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 // STATISTICS: gist-any