QCodeEdit  2.2
lib/snippets/qsnippet_p.h
Go to the documentation of this file.
00001 /****************************************************************************
00002 **
00003 ** Copyright (C) 2006-2009 fullmetalcoder <fullmetalcoder@hotmail.fr>
00004 **
00005 ** This file is part of the Edyuk project <http://edyuk.org>
00006 ** 
00007 ** This file may be used under the terms of the GNU General Public License
00008 ** version 3 as published by the Free Software Foundation and appearing in the
00009 ** file GPL.txt included in the packaging of this file.
00010 **
00011 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00012 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00013 **
00014 ****************************************************************************/
00015 
00016 #ifndef _QSNIPPET_P_H_
00017 #define _QSNIPPET_P_H_
00018 
00024 #include "qsnippet.h"
00025 #include "qsnippetpatternloader.h"
00026 
00027 #include "qeditor.h"
00028 #include "qdocument.h"
00029 #include "qdocumentcursor.h"
00030 #include "qdocumentcommand.h"
00031 
00032 class QSnippetInsertionCommand : public QDocumentCommandBlock
00033 {
00034     public:
00035         QSnippetInsertionCommand(QEditor *e);
00036         virtual ~QSnippetInsertionCommand();
00037         
00038         void addPlaceHolder(const QEditor::PlaceHolder& ph);
00039         
00040         virtual void addCommand(QDocumentCommand *c);
00041         virtual void removeCommand(QDocumentCommand *c);
00042         
00043         virtual void redo();
00044         virtual void undo();
00045         
00046     private:
00047         QEditor *m_editor;
00048         QDocumentCursor m_cursor;
00049         QList<QEditor::PlaceHolder> m_placeHolders;
00050 };
00051 
00052 #define Q_SNIPPET(T)                            \
00053     friend class Loader;                        \
00054     public:                                     \
00055     class Loader : public QSnippetPatternLoader \
00056     {                                           \
00057         public:                                 \
00058             virtual QString type() const { return ""#T; }                               \
00059             virtual QSnippet* loadSnippet(const QString& pattern) const                 \
00060             {                                                                           \
00061                 T *snip = new T(this);                                                  \
00062                 snip->m_pattern = pattern;                                              \
00063                 bool ok = reloadSnippet(snip, pattern);                                 \
00064                 if ( !ok ) { delete snip; snip = 0; }                                   \
00065                 return snip;                                                            \
00066             }                                                                           \
00067             virtual bool reloadSnippet(QSnippet* snip, const QString& pattern) const    \
00068             { return T::loadSnippet(snip, pattern); }                                   \
00069     };                                                                                  \
00070     inline T(const QSnippetPatternLoader *pl) : QSnippet(pl) {}                         \
00071     private:                                                                            \
00072     
00073 
00074 namespace QCE
00075 {
00076     namespace Snippets
00077     {
00078         class PlainText : public QSnippet
00079         {
00080             Q_SNIPPET(PlainText)
00081             
00082             public:
00083                 virtual void insert(QEditor *e) const;
00084                 
00085                 static bool loadSnippet(QSnippet *snip, const QString& pattern);
00086                 
00087                 QString m_data;
00088         };
00089 
00090         class Simple : public QSnippet
00091         {
00092             Q_SNIPPET(Simple)
00093             
00094             public:
00095                 struct Anchor
00096                 {
00097                     Anchor() : lineOffset(0), columnOffset(0) {}
00098                     
00099                     int lineOffset;
00100                     int columnOffset;
00101                 };
00102                 
00103                 struct PlaceHolder : public Anchor
00104                 {
00105                     PlaceHolder() : length(-1) {}
00106                     
00107                     int length;
00108                     QString defaultValue;
00109                     QList<Anchor> mirrors;
00110                     QList<int> unresolvedMirrors;
00111                 };
00112                 
00113                 virtual void insert(QEditor *e) const;
00114                 
00115                 static bool loadSnippet(QSnippet *snip, const QString& pattern);
00116                 
00117                 QString m_base;
00118                 QList<PlaceHolder> m_placeHolders;
00119         };
00120     }
00121 }
00122 
00123 #endif