• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

Konsole

TerminalCharacterDecoder.cpp

Go to the documentation of this file.
00001 /*
00002     This file is part of Konsole, an X terminal.
00003     
00004     Copyright 2006-2008 by Robert Knight <robertknight@gmail.com>
00005     
00006     This program is free software; you can redistribute it and/or modify
00007     it under the terms of the GNU Lesser General Public License as published by
00008     the Free Software Foundation; either version 2 of the License, or
00009     (at your option) any later version.
00010 
00011     This program is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014     GNU General Public License for more details.
00015 
00016     You should have received a copy of the GNU Lesser General Public License
00017     along with this program; if not, write to the Free Software
00018     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00019     02110-1301  USA.
00020 */
00021 
00022 // Own
00023 #include "TerminalCharacterDecoder.h"
00024 
00025 // Qt
00026 #include <QtCore/QTextStream>
00027 
00028 // KDE
00029 #include <kdebug.h>
00030 
00031 // Konsole
00032 #include "konsole_wcwidth.h"
00033 
00034 using namespace Konsole;
00035 PlainTextDecoder::PlainTextDecoder()
00036  : _output(0)
00037  , _includeTrailingWhitespace(true)
00038  , _recordLinePositions(false)
00039 {
00040 
00041 }
00042 void PlainTextDecoder::setTrailingWhitespace(bool enable)
00043 {
00044     _includeTrailingWhitespace = enable;
00045 }
00046 bool PlainTextDecoder::trailingWhitespace() const
00047 {
00048     return _includeTrailingWhitespace;
00049 }
00050 void PlainTextDecoder::begin(QTextStream* output)
00051 {
00052    _output = output;
00053    if (!_linePositions.isEmpty())
00054        _linePositions.clear();
00055 }
00056 void PlainTextDecoder::end()
00057 {
00058     _output = 0;
00059 }
00060 
00061 void PlainTextDecoder::setRecordLinePositions(bool record)
00062 {
00063     _recordLinePositions = record;
00064 }
00065 QList<int> PlainTextDecoder::linePositions() const
00066 {
00067     return _linePositions;
00068 }
00069 void PlainTextDecoder::decodeLine(const Character* const characters, int count, LineProperty /*properties*/
00070                              )
00071 {
00072     Q_ASSERT( _output );
00073 
00074     if (_recordLinePositions && _output->string())
00075     {
00076         int pos = _output->string()->count();
00077         _linePositions << pos;
00078     }
00079 
00080     //TODO should we ignore or respect the LINE_WRAPPED line property?
00081 
00082     //note:  we build up a QString and send it to the text stream rather writing into the text
00083     //stream a character at a time because it is more efficient.
00084     //(since QTextStream always deals with QStrings internally anyway)
00085     QString plainText;
00086     plainText.reserve(count);
00087    
00088     int outputCount = count;
00089 
00090     // if inclusion of trailing whitespace is disabled then find the end of the
00091     // line
00092     if ( !_includeTrailingWhitespace )
00093     {
00094         for (int i = count-1 ; i >= 0 ; i--)
00095         {
00096             if ( characters[i].character != ' '  )
00097                 break;
00098             else
00099                 outputCount--;
00100         }
00101     }
00102     
00103     for (int i=0;i<outputCount;)
00104     {
00105         plainText.append( QChar(characters[i].character) );
00106         i += qMax(1,konsole_wcwidth(characters[i].character));
00107     }
00108     *_output << plainText;
00109 }
00110 
00111 HTMLDecoder::HTMLDecoder() :
00112         _output(0)
00113     ,_colorTable(base_color_table)
00114        ,_innerSpanOpen(false)
00115        ,_lastRendition(DEFAULT_RENDITION)
00116 {
00117     
00118 }
00119 
00120 void HTMLDecoder::begin(QTextStream* output)
00121 {
00122     _output = output;
00123 
00124     QString text;
00125 
00126     //open monospace span
00127     openSpan(text,"font-family:monospace");
00128 
00129     *output << text;
00130 }
00131 
00132 void HTMLDecoder::end()
00133 {
00134     Q_ASSERT( _output );
00135 
00136     QString text;
00137 
00138     closeSpan(text);
00139 
00140     *_output << text;
00141 
00142     _output = 0;
00143 
00144 }
00145 
00146 //TODO: Support for LineProperty (mainly double width , double height)
00147 void HTMLDecoder::decodeLine(const Character* const characters, int count, LineProperty /*properties*/
00148                             )
00149 {
00150     Q_ASSERT( _output );
00151 
00152     QString text;
00153 
00154     int spaceCount = 0;
00155         
00156     for (int i=0;i<count;i++)
00157     {
00158         QChar ch(characters[i].character);
00159 
00160         //check if appearance of character is different from previous char
00161         if ( characters[i].rendition != _lastRendition  ||
00162              characters[i].foregroundColor != _lastForeColor  ||
00163              characters[i].backgroundColor != _lastBackColor )
00164         {
00165             if ( _innerSpanOpen )
00166                     closeSpan(text);
00167 
00168             _lastRendition = characters[i].rendition;
00169             _lastForeColor = characters[i].foregroundColor;
00170             _lastBackColor = characters[i].backgroundColor;
00171             
00172             //build up style string
00173             QString style;
00174 
00175             bool useBold;
00176             ColorEntry::FontWeight weight = characters[i].fontWeight(_colorTable);
00177             if (weight == ColorEntry::UseCurrentFormat)
00178                 useBold = _lastRendition & RE_BOLD;
00179             else
00180                 useBold = weight == ColorEntry::Bold;
00181             
00182             if (useBold)
00183                 style.append("font-weight:bold;");
00184 
00185             if ( _lastRendition & RE_UNDERLINE )
00186                     style.append("font-decoration:underline;");
00187         
00188             //colours - a colour table must have been defined first
00189             if ( _colorTable )    
00190             {
00191                 style.append( QString("color:%1;").arg(_lastForeColor.color(_colorTable).name() ) );
00192 
00193                 if (!characters[i].isTransparent(_colorTable))
00194                 {
00195                     style.append( QString("background-color:%1;").arg(_lastBackColor.color(_colorTable).name() ) );
00196                 }
00197             }
00198         
00199             //open the span with the current style    
00200             openSpan(text,style);
00201             _innerSpanOpen = true;
00202         }
00203 
00204         //handle whitespace
00205         if (ch.isSpace())
00206             spaceCount++;
00207         else
00208             spaceCount = 0;
00209         
00210 
00211         //output current character
00212         if (spaceCount < 2)
00213         {
00214             //escape HTML tag characters and just display others as they are
00215             if ( ch == '<' )
00216                 text.append("&lt;");
00217             else if (ch == '>')
00218                     text.append("&gt;");
00219             else    
00220                     text.append(ch);
00221         }
00222         else
00223         {
00224             text.append("&nbsp;"); //HTML truncates multiple spaces, so use a space marker instead
00225         }
00226         
00227     }
00228 
00229     //close any remaining open inner spans
00230     if ( _innerSpanOpen )
00231         closeSpan(text);
00232 
00233     //start new line
00234     text.append("<br>");
00235     
00236     *_output << text;
00237 }
00238 void HTMLDecoder::openSpan(QString& text , const QString& style)
00239 {
00240     text.append( QString("<span style=\"%1\">").arg(style) );
00241 }
00242 
00243 void HTMLDecoder::closeSpan(QString& text)
00244 {
00245     text.append("</span>");
00246 }
00247 
00248 void HTMLDecoder::setColorTable(const ColorEntry* table)
00249 {
00250     _colorTable = table;
00251 }

Konsole

Skip menu "Konsole"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

API Reference

Skip menu "API Reference"
  • Konsole
  • Libraries
  •   libkonq
Generated for API Reference by doxygen 1.5.7
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal