Back to index

scribus-ng  1.3.4.dfsg+svn20071115
Public Member Functions | Private Member Functions | Private Attributes
TextFilter Class Reference

#include <textfilter.h>

Collaboration diagram for TextFilter:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 TextFilter (const QString &fname, const QString &enc, gtWriter *w)
 ~TextFilter ()

Private Member Functions

void loadText ()
void toUnicode ()
void write ()
void replace (QString *text)
void replaceHex (QString *text)

Private Attributes

PrefsContext * prefs
QString text
QString encoding
QString filename
gtWriter * writer
std::vector< tfFilter * > * filters

Detailed Description

Definition at line 22 of file textfilter.h.


Constructor & Destructor Documentation

TextFilter::TextFilter ( const QString &  fname,
const QString &  enc,
gtWriter *  w 
)

Definition at line 41 of file textfilter.cpp.

{
       filename = fname;
       encoding = enc;
       writer = w;
       writer->setOverridePStyleFont(false);
       prefs = PrefsManager::instance()->prefsFile->getPluginContext("TextFilter");
       tfDia* tfdia = new tfDia();
       if (tfdia->exec())
       {
              filters = &(tfdia->filters);
              loadText();
              toUnicode();
              write();
       }
       delete tfdia;
}

Here is the call graph for this function:

Definition at line 268 of file textfilter.cpp.

{

}

Member Function Documentation

void TextFilter::loadText ( ) [private]

Definition at line 60 of file textfilter.cpp.

{
       text = "";
       QFile f(filename);
       QFileInfo fi(f);
       if (!fi.exists())
              return;
       uint posi;
//     bool ret;
       QByteArray bb(f.size());
       if (f.open(IO_ReadOnly))
       {
              f.readBlock(bb.data(), f.size());
              f.close();
              for (posi = 0; posi < bb.size(); ++posi)
                     text += QChar(bb[posi]);
       }
}

Here is the caller graph for this function:

void TextFilter::replace ( QString *  text) [private]

Definition at line 210 of file textfilter.cpp.

{
       text->replace("\\\\", "__SLASH_HERE__");
       text->replace("\\", "\\");
       text->replace("__SLASH_HERE__", "\\\\");

       text->replace("\\\\t", "__|TABCHAR|__");
       text->replace("\\t", "\t");
       text->replace("__|TABCHAR|__", "\\t");

       text->replace("\\\\n", "__|L-C|__");
       text->replace("\\n", "\n");
       text->replace("__|L-C|__", "\\n");

       text->replace("\\\\f", "__|F-CHAR|__");
       text->replace("\\f", "\f");
       text->replace("__|F-CHAR|__", "\\f");

       text->replace("\\\\r", "__|R-CHAR|__");
       text->replace("\\r", "\r");
       text->replace("__|R-CHAR|__", "\\r");

       text->replace("\\\\v", "__|V-CHAR|__");
       text->replace("\\v", "\v");
       text->replace("__|V-CHAR|__", "\\v");

       replaceHex(text);
}

Here is the call graph for this function:

Here is the caller graph for this function:

void TextFilter::replaceHex ( QString *  text) [private]

Definition at line 239 of file textfilter.cpp.

{
       int index;
       int pos = 0;
       QString hexS;
       int hex;
       bool ok = false;
       do
       {
              index = text->find("\\x", pos);
              if (index != -1)
              {
                     if ((text->length() - index + 1) > 6)
                     {
                            hexS = text->mid(index + 2, 4);
                            hex = hexS.toInt(&ok, 16);
                            if (ok)
                            {
                                   text->replace("\\x" + hexS, QChar(hex));
                            }
                     }
                     else
                            index = -1;
                     pos += 2;
              }
       }
       while (index != -1);
}

Here is the caller graph for this function:

void TextFilter::toUnicode ( ) [private]

Definition at line 79 of file textfilter.cpp.

{
       QTextCodec *codec;
       if (encoding.isEmpty())
              codec = QTextCodec::codecForLocale();
       else
              codec = QTextCodec::codecForName(encoding);
       QString dec = codec->toUnicode(text);
       text = dec;
}

Here is the caller graph for this function:

void TextFilter::write ( ) [private]

Definition at line 90 of file textfilter.cpp.

{
       QMap<QString, gtParagraphStyle*> pstyles;
       gtFrameStyle* fstyle = writer->getDefaultStyle();
       for (int i = 0; i < static_cast<int>(filters->size()); ++i)
       {
              if ((*filters)[i]->isEnabled())
              {
                     int action = (*filters)[i]->getAction();
                     QString regExp = (*filters)[i]->regExp();
                     QString replaceWith = (*filters)[i]->replaceWith();
                     bool useRegexp = (*filters)[i]->isRegExp();
                     if (useRegexp)
                            replace(&replaceWith);
                     QString pstyle = (*filters)[i]->getPStyleName();
                     QRegExp rx = QRegExp(regExp);
                     rx.setMinimal(true);
                     switch (action)
                     {
                            case REMOVE: 
                                   if (useRegexp)
                                          text = text.remove(rx);
                                   else
                                          text = text.remove(regExp);
                                   break;
                            case REPLACE:
                                   if (useRegexp)
                                          text = text.replace(rx, replaceWith); 
                                   else
                                          text = text.replace(regExp, replaceWith);
                                   break;
                            case APPLY: 
                                   pstyles[pstyle] = new gtParagraphStyle(*fstyle);
                                   pstyles[pstyle]->setName(pstyle);
                                   break;
                     }
              }
       }
       if (pstyles.size() == 0)
              writer->append(text);
       else
       {
              QStringList list = QStringList::split("\n", text, true);
              gtParagraphStyle *useStyle = NULL;
              for (int i = 0; i < static_cast<int>(list.size()); ++i)
              {
                     QString tmpText = list[i];
                     QString tmpText2 = tmpText;
                     tmpText2.simplifyWhiteSpace();
                     int numberOfWords = tmpText2.contains(" ");
                     ++numberOfWords;
                     useStyle = NULL;
                     for (int j = 0; j < static_cast<int>(filters->size()); ++j)
                     {
                            if ((*filters)[j]->isEnabled())
                            {
                                   int action = (*filters)[j]->getAction();
                                   QString regExp = (*filters)[j]->regExp();
                                   QString replaceWith = (*filters)[j]->replaceWith();
                                   bool useRegexp = (*filters)[j]->isRegExp();
                                   if (useRegexp)
                                          replace(&replaceWith);
                                   QString pstyle = (*filters)[j]->getPStyleName();
                                   int lessThan = (*filters)[j]->getLessThan();
                                   int moreThan = (*filters)[j]->getMoreThan();
                                   int style = (*filters)[j]->getStyle();
                                   bool removeMatch = (*filters)[j]->removeMatch();
                                   QRegExp rx = QRegExp(regExp);
                                   if (!pstyle.isEmpty())
                                   {
                                          switch (action)
                                          {
                                                 case APPLY:
                                                        switch (style)
                                                        {
                                                        case ALL_PARAGRAPHS:
                                                               useStyle = pstyles[pstyle];
                                                               break;
                                                        case STARTS_WITH:
                                                               if (useRegexp)
                                                               {
                                                                      if (tmpText2.find(rx) == 0)
                                                                      {
                                                                             useStyle = pstyles[pstyle];
                                                                             if (removeMatch)
                                                                                    tmpText.remove(rx);
                                                                      }
                                                               }
                                                               else
                                                               {
                                                                      if (tmpText2.find(regExp) == 0)
                                                                      {
                                                                             useStyle = pstyles[pstyle];
                                                                             if (removeMatch)
                                                                                    tmpText.remove(regExp);
                                                                      }
                                                               }
                                                               break;
                                                        case LESS_THAN:
                                                               if ((lessThan != -1) && (numberOfWords < lessThan))
                                                                      useStyle = pstyles[pstyle];
                                                               break;
                                                        case MORE_THAN:
                                                               if ((moreThan != -1) && (numberOfWords > moreThan))
                                                                      useStyle = pstyles[pstyle];
                                                               break;
                                                        }
                                                        break;
                                          }
                                   }
                            }
                     }
                     if (i == static_cast<int>(list.size()) - 1)
                            writer->append(tmpText, useStyle);
                     else
                            writer->append(tmpText + "\n", useStyle);
              }
       }
}

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

QString TextFilter::encoding [private]

Definition at line 27 of file textfilter.h.

QString TextFilter::filename [private]

Definition at line 28 of file textfilter.h.

std::vector<tfFilter*>* TextFilter::filters [private]

Definition at line 30 of file textfilter.h.

PrefsContext* TextFilter::prefs [private]

Definition at line 25 of file textfilter.h.

QString TextFilter::text [private]

Definition at line 26 of file textfilter.h.

gtWriter* TextFilter::writer [private]

Definition at line 29 of file textfilter.h.


The documentation for this class was generated from the following files: