summaryrefslogtreecommitdiffstats
path: root/kregexpeditor/charselector.cpp
blob: 5f6047553049d937c070bed58b86722e37dd8772 (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
/*
 *  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 <klocale.h>
#include "charselector.moc"
#endif

#include "charselector.h"
#include "limitedcharlineedit.h"
#include "regexpconverter.h"
#include <tqlayout.h>
#include <tqwidgetstack.h>
#include <tqcombobox.h>

/**
   In the class CharSelector, three LimitedCharLineEdit are used.
   These widgets are all used in a TQWidgetStack. The LimitedCharLineEdit
   class is basically a TQLineEdit, which is limited to a certain
   number of characters. This conflicts with the TQWidgetStack, as this
   class expects the widgets on the stack to take up all space.
   StackContainer fills in this gab.
*/
class StackContainer :public TQWidget
{
public:
    StackContainer( TQWidget* child, TQWidget* parent ) : TQWidget( parent )
        {
            TQHBoxLayout* tqlayout = new TQHBoxLayout( this );
            child->reparent( this, TQPoint(0,0), false );
            tqlayout->addWidget( child );
            tqlayout->addStretch( 1 );
        }
};

CharSelector::CharSelector( TQWidget* parent, const char* name )
    :TQWidget( parent, name ), _oldIndex(0)
{
  TQStringList items;
  TQHBoxLayout* tqlayout = new TQHBoxLayout( this, 0, 6 );

  _type = new TQComboBox( this, "_type" );
  items << i18n("Normal Character")
        << i18n("Unicode Char in Hex.")
        << i18n("Unicode Char in Oct.")
        << TQString::tqfromLatin1("----")
        << i18n("The Bell Character (\\a)")
        << i18n("The Form Feed Character (\\f)")
        << i18n("The Line Feed Character (\\n)")
        << i18n("The Carriage Return Character (\\r)")
        << i18n("TheQt::Horizontal Tab Character (\\t)")
        << i18n("TheQt::Vertical Tab Character (\\v)");
  _type->insertStringList( items );
  tqlayout->addWidget( _type );

  _stack = new TQWidgetStack( this, "_stack" );
  tqlayout->addWidget( _stack );

  _normal = new LimitedCharLineEdit( LimitedCharLineEdit::NORMAL, 0, "_normal" );
  _stack->addWidget( new StackContainer( _normal, _stack ), 0 );

  _hex = new LimitedCharLineEdit( LimitedCharLineEdit::HEX, _stack, "_hex" );
  _stack->addWidget( new StackContainer( _hex, _stack ), 1 );

  _oct = new LimitedCharLineEdit( LimitedCharLineEdit::OCT, _stack, "_oct" );
  _stack->addWidget( new StackContainer( _oct, _stack ), 2 );

  _stack->raiseWidget( 0 );

  connect( _type, TQT_SIGNAL( activated( int ) ), this, TQT_SLOT(slotNewItem( int ) ) );
}

void CharSelector::slotNewItem( int which )
{
  _type->setCurrentItem( which );
  if ( which <= 2 ) {
    _stack->raiseWidget( which );
    _normal->setEnabled( true );
    _hex->setEnabled( true );
    _oct->setEnabled( true );
  }
  else if ( which == 3 ) {
    _type->setCurrentItem( _oldIndex );
    slotNewItem(_oldIndex);
    return;
  }
  else {
    _normal->setEnabled( false );
    _hex->setEnabled( false );
    _oct->setEnabled( false );
  }

    _oldIndex = which;
}

void CharSelector::setText( TQString text )
{
    // This is the best I can do about missing character range features, unless all of
    // textrangeregexp is to be reworked. The problem is that textrangeregexp only allows to
    // get the characters, which would be something like \a, but \a does not work with say Emacs
    // style regexps -- ko28 Sep. 2003 10:55 -- Jesper K. Pedersen
    bool enabled = ( RegExpConverter::current()->features() & RegExpConverter::ExtRange );
    _type->setEnabled( enabled );

  if ( text.at(0) == TQChar('\\') ) {
    if ( text.at(1) == TQChar('x') ) {
      _hex->setText(text.mid(2));
      slotNewItem( 1 );
    }
    else if ( text.at(1) == TQChar('0') ) {
      _oct->setText(text.mid(2));
      slotNewItem( 2 );
    }
    else if ( text.at(1) == TQChar('a') )
      slotNewItem(4);
    else if ( text.at(1) == TQChar('f') )
      slotNewItem(5);
    else if ( text.at(1) == TQChar('n') )
      slotNewItem(6);
    else if ( text.at(1) == TQChar('r') )
      slotNewItem(7);
    else if ( text.at(1) == TQChar('t') )
      slotNewItem(8);
    else if ( text.at(1) == TQChar('v') )
      slotNewItem(9);
    else {
      qWarning("Warning %s:%d Unknown escape %s", __FILE__, __LINE__, text.latin1() );
    }
  }
  else {
    slotNewItem(0);
    _normal->setText(text);
  }
}

bool CharSelector::isEmpty() const
{
  return ( _type->currentItem() == 0 && _normal->text().isEmpty() ) ||
    ( _type->currentItem() == 1 && _hex->text().isEmpty() ) ||
    ( _type->currentItem() == 2 && _oct->text().isEmpty() );
}

TQString CharSelector::text() const
{
  switch ( _type->currentItem() ) {
  case 0: // Normal Character
    return _normal->text();
  case 1: // Hex
    return TQString::fromLocal8Bit("\\x") + _hex->text();
  case 2: // Oct
      return TQString::fromLocal8Bit("\\0") + _oct->text();
  case 3: // The seperator
    break;
  case 4:
    return TQString::tqfromLatin1("\\a");
  case 5:
    return TQString::tqfromLatin1("\\f");
  case 6:
    return TQString::tqfromLatin1("\\n");
  case 7:
    return TQString::tqfromLatin1("\\r");
  case 8:
    return TQString::tqfromLatin1("\\t");
  case 9:
    return TQString::tqfromLatin1("\\v");
  }
  return TQString();
}