Generated on Mon Jul 6 18:09:02 2009 for Gecode by doxygen 1.5.9

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: 2009-03-24 04:14:40 +0100 (Tue, 24 Mar 2009) $ by $Author: tack $
00011  *     $Revision: 8522 $
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 
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     editor = new QTextEdit;
00095     editor->setFont(font);
00096     editor->setReadOnly(true);
00097     editor->setLineWrapMode(QTextEdit::FixedColumnWidth);
00098     editor->setLineWrapColumnOrWidth(80);
00099     os = new GistOutputStream(editor);
00100 
00101     QAction* clearText = new QAction("Clear", this);
00102     clearText->setShortcut(QKeySequence("Ctrl+K"));
00103     this->addAction(clearText);
00104     connect(clearText, SIGNAL(triggered()), editor,
00105                        SLOT(clear()));
00106 
00107     QAction* closeWindow = new QAction("Close window", this);
00108     closeWindow->setShortcut(QKeySequence("Ctrl+W"));
00109     this->addAction(closeWindow);
00110     connect(closeWindow, SIGNAL(triggered()), this,
00111                          SLOT(close()));
00112 
00113     setCentralWidget(editor);
00114     setWindowTitle(QString((std::string("Gist Console: ") + name).c_str()));
00115 
00116     setAttribute(Qt::WA_QuitOnClose, false);
00117     setAttribute(Qt::WA_DeleteOnClose, false);
00118     resize(600,300);
00119   }
00120 
00121   TextOutput::~TextOutput(void) {
00122     delete os;
00123   }
00124 
00125   std::ostream&
00126   TextOutput::getStream(void) {
00127     return *os;
00128   }
00129 
00130   void
00131   TextOutput::insertHtml(const QString& s) {
00132     QTextBlockFormat bf = editor->textCursor().blockFormat();
00133     bf.setBottomMargin(0);
00134     editor->textCursor().setBlockFormat(bf);
00135     editor->insertHtml(s);
00136   }
00137 
00138 }}
00139 
00140 // STATISTICS: gist-any