• Main Page
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

C:/CVUT/diplomka/Automata_editor/sources/stringProcessor.h

Go to the documentation of this file.
00001 #ifndef STRING_PROCESSOR_H_46554745872
00002 #define STRING_PROCESSOR_H_46554745872
00003 
00004 class Editor;
00005 class LabelX;
00006 
00007 class QString;
00008 class QChar;
00009 class QPointF;
00010 class QPainter;
00011 class QFont;
00012 class QFontMetrics;
00013 class QGraphicsView;
00014 class QLineEdit;
00015 class QTimer;
00016 class QStringList;
00017 class QCheckBox;
00018 
00019 #include <QDialog>
00020 #include <QPair>
00021 #include <QtDebug>
00022 #include <exception>
00023 
00024 class StringProcessorException : public std::exception
00025 {
00026 public:
00027     StringProcessorException(const QString &what = "") : m_what(what) {}
00028 
00029     virtual ~StringProcessorException() throw() {}
00030 
00031     virtual const char* what() const throw()
00032     {
00033         return m_what.toStdString().c_str();
00034     }
00035     
00036 protected:
00037     QString m_what;
00038 };
00039 
00040 class StringProcessorBraceMatchException : public StringProcessorException
00041 {
00042 public:
00043     virtual ~StringProcessorBraceMatchException() throw() {}
00044 
00045     virtual const char * what() const throw()
00046     {
00047         return "Matching braces failed - too many '}' found.";
00048     }
00049 };
00050 
00051 class StringProcessor
00052 {
00053 public:
00054     //! determines if change is needed
00055     enum EChangeType { eStringToChar = 0,   //!< image is in string2CharMap
00056                        eNoChange };         //!< no change needed or implemented
00057 
00058     //! list of fonts
00059     enum EFont { eNormalFont = 0,
00060                  eSymbolFont };
00061     
00062     //! according to LaTeX
00063     enum EFontSize { eTextStyle = 0,
00064                      eDisplayStyle,
00065                      eScriptStyle,
00066                      eScriptScriptStyle };
00067 
00068     //! list of symbol modifiers
00069     enum EModifier { eBar = 0,
00070                      eUnderline,
00071                      eOverline,
00072                      eNoModifier };
00073 
00074     struct CharacterInfo
00075     {
00076         EChangeType changeType;             //!< type of change to do
00077         int         textIdx;                //!< idx to original string, where change starts
00078         QString     character;              //!< character to be used
00079         EFontSize   fontSize;               //!< font size changer
00080         EModifier   modifier;               //!< symbol modifier
00081         
00082         explicit CharacterInfo(EChangeType type, int idx, const QString &ch, 
00083                                EFontSize size, EModifier mod);
00084     };
00085     
00086     typedef QList<CharacterInfo>        TCharacterList;
00087 
00088     StringProcessor(const QString& text = "", int fontSize = 0);    
00089     
00090     bool setText(const QString& text);
00091     const QString& text() const {return m_text;}
00092 
00093     //! Related to eTextStyle.
00094     //! \sa EFontSize
00095     void setFontSize(int fontSize);
00096     int getWidth() const { return m_width; }
00097     int getHeight() const;
00098     int getDescent() const;
00099     int getAscent() const;
00100 
00101     // TODO: should be moved to LabelX class? StringProcessor shouldn't draw ...
00102     //! Draw text using painter, final position will be stored to point, 
00103     //! so it is possible to append next drawing next to drawed text
00104     void drawText(QPainter *painter, QPointF& point) const;
00105 
00106     //! Checks if given symbol matches with keyword regex.
00107     static bool         isSpecialSymbol(const QString &symb);    
00108     //! Convert symbol to single character and get font info for printing out.
00109     static QString      getSymbolPrintInfo(const QString &symb, QString &fontFamily);
00110     //! Parse symbols to list (each character as one string).
00111     static QStringList  parseSymbols(const QString &input);
00112     //! Parse symbols to list.
00113     static QStringList  parseSymbols(const TCharacterList &characterList);
00114     
00115     //! Returns character list parsed from input text.
00116     QStringList getCharacters() const;
00117     
00118     //! Process input string to TCharacterList
00119     static TCharacterList computeCharacterList(const QString &input);
00120     
00121     //! Returns character list computed for current instance of SP.
00122     TCharacterList getCharacterList() const;
00123     
00124     static const QRegExp        keywordRegex;
00125     static const QRegExp        specCharacterRegex;
00126     static const QRegExp        whitespaceRegex;
00127     static const QRegExp        escapedCharacterRegex;
00128     
00129 #ifdef USE_UNKNOWN_CONVENTION
00130     static const QChar          unknownCharacter;
00131     static const QString        unknownString;
00132 #endif
00133     
00134 #ifdef STRING_PROCESSOR_TEST
00135     static void runTest();    
00136 #endif
00137     
00138 protected:
00139     typedef QPair<QChar, EFont>         TCharPair;
00140     typedef QMap<QString, TCharPair>    TString2CharMap;    
00141     typedef QList<QFont>                TFontList;    
00142     typedef QList<QFontMetrics>         TMetricsList;
00143     typedef QMap<QString, int>          TString2KwTypeMap;
00144     
00145     enum EKeywordType { eString2Char = 0,
00146                         eFontSize,
00147                         eModifier };
00148     
00149     static void initialize();
00150     static void addToMaps(const QString &str, const TCharPair &charPair);
00151     void processText();
00152     void fillFontLists();
00153     void computeMetrics();
00154 
00155     //! Runs computeCharacterList on text inside balanced '{', '}'
00156     static TCharacterList computeCharacterListInBlock
00157         (const QString &input, int &startIdx, EFontSize fs, EModifier m);
00158     static TCharacterList computeCharacterList
00159         (const QString &input, int &startIdx, EFontSize fs, EModifier m, bool useModifierOnce = false);
00160         
00161     static bool parseFontSize
00162         (const QString &symb, EFontSize &fontSize);
00163     static bool parseModifier
00164         (const QString &symb, EModifier &modifier);
00165             
00166     //! Updates given font according to given charcter, returns true if font was changed
00167     bool updateFont(QFont &font, const CharacterInfo &c) const;
00168 
00169     static TString2KwTypeMap string2KwTypeMap;    
00170 
00171     //! First in pair is required character, second is index to fontList
00172     static TString2CharMap      string2CharMap;
00173     static const QStringList    fontFamilyList;
00174     
00175     TFontList    m_fontList;    //!< index 0 (== eNormalFont) is standard font used in editor
00176                                 //!< index 1 (== eSymbolFont) is font Symbol
00177 
00178     TMetricsList m_metricsList;
00179     
00180     //! List of parsed characters
00181     TCharacterList m_characterList;
00182 
00183     QString m_text;
00184     int     m_fontSize;
00185     int     m_width;
00186     int     m_height;       //!< max height in parsed m_text
00187     int     m_ascent;       //!< max ascent in parsed m_text
00188     int     m_descent;      //!< max descentf in parsed m_text
00189 };
00190 
00191 class LabelSyntaxChecker;
00192 
00193 #if !defined(QT_NO_DEBUG_OUTPUT)
00194         QDebug operator<<(QDebug dbg, const StringProcessor::CharacterInfo &c);
00195 #endif
00196 
00197 class StringProcessorTestDialog : public QDialog
00198 {
00199     Q_OBJECT
00200 public:    
00201 
00202 protected:
00203 #ifdef STRING_PROCESSOR_TEST
00204     friend void StringProcessor::runTest();
00205     StringProcessorTestDialog(QWidget* parent = 0);
00206     ~StringProcessorTestDialog();
00207 #endif
00208 
00209     QLineEdit           *m_edtText;
00210     QGraphicsView       *m_view;
00211     LabelX              *m_label;
00212     QCheckBox           *m_checkTimer;
00213     QCheckBox           *m_checkSyntax;
00214     QTimer              *m_timer;
00215     int                 m_idx;
00216     QStringList         m_greekList;
00217     LabelSyntaxChecker  *m_syntaxChecker;
00218 
00219 protected slots:
00220 #ifdef STRING_PROCESSOR_TEST
00221     void changeText();
00222     void timerChange(int);
00223     void timerEvent();
00224 #endif
00225 };
00226 
00227 #endif //STRING_PROCESSOR_H_46554745872

Generated on Tue Jan 4 2011 03:03:24 for Autoamata editor by  doxygen 1.7.0