summaryrefslogtreecommitdiffstats
path: root/kregexpeditor/widgetfactory.cpp
blob: a5866d62905d984c186b3a3ac688c3b07576d610 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
 *  Copyright (c) 2002-2003 Jesper K. Pedersen <[email protected]>
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Library General Public
 *  License version 2 as published by the Free Software Foundation.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Library General Public License for more details.
 *
 *  You should have received a copy of the GNU Library General Public License
 *  along with this library; see the file COPYING.LIB.  If not, write to
 *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 *  Boston, MA 02110-1301, USA.
 **/
#ifdef TQT_ONLY
  #include "compat.h"
#else
  #include <kmessagebox.h>
#endif

#include "widgetfactory.h"
#include "repeatwidget.h"
#include "textwidget.h"
#include "characterswidget.h"
#include "altnwidget.h"
#include "zerowidgets.h"
#include "compoundwidget.h"
#include "concwidget.h"
#include "lookaheadwidget.h"

#include "textregexp.h"
#include "textrangeregexp.h"
#include "repeatregexp.h"
#include "lookaheadregexp.h"
#include "concregexp.h"
#include "altnregexp.h"
#include "positionregexp.h"
#include "dotregexp.h"
#include "kregexpeditorgui.h"

bool WidgetFactory::isContainer( RegExpType tp )
{
  return ( tp == REPEAT || tp == ALTN || tp == COMPOUND );
}

RegExpWidget* WidgetFactory::createWidget( RegExpEditorWindow* win, TQWidget* parent,
                                           RegExpType type )
{
  RegExpWidget* widget = 0;

  switch (type) {
  case TEXT:
    return new TextWidget( win, parent ); break;
  case ALTN:
    return  new AltnWidget( win, parent ); break;
  case DOT:
    return  new AnyCharWidget( win, parent ); break;
  case BEGLINE:
    return new BegLineWidget( win, parent ); break;
  case ENDLINE:
    return new EndLineWidget( win, parent ); break;
  case WORDBOUNDARY:
    return new WordBoundaryWidget( win, parent ); break;
  case NONWORDBOUNDARY:
    return new NonWordBoundaryWidget( win, parent ); break;
  case POSLOOKAHEAD:
  case NEGLOOKAHEAD:
    return new LookAheadWidget( win, type, parent ); break;
  case REPEAT:
    widget = new RepeatWidget( win, parent ); break;
  case CHARSET:
    widget = new CharactersWidget( win, parent ); break;
  case COMPOUND:
    widget = new CompoundWidget( win, parent ); break;
  default:
    qFatal("It should not be possible to get here!");
    return 0;
  }

  if ( widget->edit() == TQDialog::Rejected ) {
    delete widget;
    return 0;
  }
  return widget;
}

RegExpWidget* WidgetFactory::createWidget( RegExp* regexp, RegExpEditorWindow* editorWindow,
                                           TQWidget* parent )
{
  if ( regexp == 0 ) {
    qFatal("%s:%d Regexp is 0", __FILE__, __LINE__ );
  }
  else if ( TextRegExp* reg = dynamic_cast<TextRegExp*>( regexp ) )
    return new TextWidget( reg, editorWindow, parent );
  else if ( TextRangeRegExp* reg = dynamic_cast<TextRangeRegExp*>( regexp ) )
    return new CharactersWidget( reg, editorWindow, parent );
  else if ( RepeatRegExp* reg = dynamic_cast<RepeatRegExp*>( regexp ) )
    return new RepeatWidget( reg, editorWindow, parent );
  else if ( LookAheadRegExp* reg = dynamic_cast<LookAheadRegExp*>( regexp ) ) {
    if ( reg->lookAheadType() == LookAheadRegExp::POSITIVE )
      return new LookAheadWidget( reg, editorWindow, POSLOOKAHEAD, parent );
    else
      return new LookAheadWidget( reg, editorWindow, NEGLOOKAHEAD, parent );
  }
  else if ( ConcRegExp* reg = dynamic_cast<ConcRegExp*>( regexp ) )
    return new ConcWidget( reg, editorWindow, parent );
  else if ( AltnRegExp* reg = dynamic_cast<AltnRegExp*>( regexp ) )
    return new AltnWidget( reg, editorWindow, parent );
  else if ( PositionRegExp* reg = dynamic_cast<PositionRegExp*>( regexp ) ) {
    switch ( reg->position() ) {
    case PositionRegExp::BEGLINE:
      return new BegLineWidget( editorWindow, parent );
    case PositionRegExp::ENDLINE:
      return new EndLineWidget( editorWindow, parent );
    case PositionRegExp::WORDBOUNDARY:
      return new WordBoundaryWidget( editorWindow, parent );
    case PositionRegExp::NONWORDBOUNDARY:
      return new NonWordBoundaryWidget( editorWindow, parent );
    }
  }
  else if ( dynamic_cast<DotRegExp*>( regexp ) )
    return new AnyCharWidget( editorWindow, parent );
  else if ( CompoundRegExp* reg = dynamic_cast<CompoundRegExp*>( regexp ) )
    return new CompoundWidget( reg, editorWindow, parent );
  else {
    qFatal("%s:%d Internal Error: Unknown RegExp type", __FILE__, __LINE__);
  }
  return 0;
}

RegExp* WidgetFactory::createRegExp( TQDomElement node, const TQString& version )
{
  TQString tag = node.tagName();
  RegExp* regexp;
  if ( tag == TQString::fromLocal8Bit( "TextRange" ) )
    regexp = new TextRangeRegExp( false );
  else if ( tag == TQString::fromLocal8Bit( "Text" ) )
    regexp = new TextRegExp( false );
  else if ( tag == TQString::fromLocal8Bit( "Concatenation" ) )
    regexp = new ConcRegExp( false );
  else if ( tag == TQString::fromLocal8Bit( "Alternatives" ) )
    regexp = new AltnRegExp( false );
  else if ( tag == TQString::fromLocal8Bit( "BegLine" ) )
    regexp = new PositionRegExp( false, PositionRegExp::BEGLINE );
  else if ( tag == TQString::fromLocal8Bit( "EndLine" ) )
    regexp = new PositionRegExp( false, PositionRegExp::ENDLINE );
  else if ( tag == TQString::fromLocal8Bit( "WordBoundary" ) )
    regexp = new PositionRegExp( false, PositionRegExp::WORDBOUNDARY );
  else if ( tag == TQString::fromLocal8Bit( "NonWordBoundary" ) )
    regexp = new PositionRegExp( false, PositionRegExp::NONWORDBOUNDARY );
  else if ( tag == TQString::fromLocal8Bit( "PositiveLookAhead" ) )
    regexp = new LookAheadRegExp( false, LookAheadRegExp::POSITIVE );
  else if ( tag == TQString::fromLocal8Bit( "NegativeLookAhead" ) )
    regexp = new LookAheadRegExp( false, LookAheadRegExp::NEGATIVE );
  else if ( tag == TQString::fromLocal8Bit( "Compound" ) )
    regexp = new CompoundRegExp( false );
  else if ( tag == TQString::fromLocal8Bit( "AnyChar" ) )
    regexp = new DotRegExp( false );
  else if ( tag == TQString::fromLocal8Bit( "Repeat" ) )
    regexp = new RepeatRegExp( false );
  else {
    KMessageBox::sorry( 0, i18n("<p>Unknown tag while reading XML. Tag was <b>%1</b></p>").tqarg(tag),
                        i18n("Error While Loading From XML File") ) ;

    return 0;
  }

  bool ok = regexp->load( node, version );
  if (ok)
    return regexp;
  else {
    delete regexp;
    return 0;
  }
  return 0;
}

RegExp* WidgetFactory::createRegExp( TQString str )
{
  TQDomDocument doc;
  bool ok = doc.setContent( str );
  if ( !ok ) {
    KMessageBox::sorry( 0, i18n("Error while loading regular expression from XML. Most probably the regular expression had unmatched tags."),
                        i18n("Error While Loading Regular Expression From XML") ) ;
  }


  // Read the RegularExpression element, and extract the version.
  TQDomElement top = doc.documentElement();
  if (! (top.tagName() == TQString::fromLocal8Bit("RegularExpression")) ) {
    KMessageBox::sorry( 0, i18n("<p>XML file did not contain a <b>%1</b> tag.</p>").tqarg(TQString::fromLatin1("RegularExpression")),
                        i18n("Error While Loading From XML File") ) ;
  }
  TQString version = top.attribute( TQString::fromLocal8Bit("version"), KRegExpEditorGUI::version );
  TQDomNode child = top.firstChild();
  if ( ! child.isElement() ) {
    KMessageBox::sorry( 0, i18n("<p>Error while reading XML file. The element just below the tag "
                                "<b>%1</b> was not an element.</p>").tqarg(TQString::fromLatin1("RegularExpression")),
                        i18n("Error While Loading From XML File") ) ;
  }

  RegExp* regexp = WidgetFactory::createRegExp( child.toElement(), version );

  return regexp;
}