summaryrefslogtreecommitdiffstats
path: root/kbabel/kbabel/hidingmsgedit.cpp
blob: a3dcb3c3a559dc1b2a0c41ea5b42363d903492c8 (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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
/***************************************************************************
                          hidingmsgedit.cpp  -  description
                             -------------------
    begin                : So nov 2 2002
    copyright            : (C) 2002 by Stanislav Visnovsky
    email                : visnovsky@kde.org
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
  In addition, as a special exception, the copyright holders give
  permission to link the code of this program with any edition of
  the TQt library by Trolltech AS, Norway (or with modified versions
  of TQt that use the same license as TQt), and distribute linked
  combinations including the two.  You must obey the GNU General
  Public License in all respects for all of the code used other than
  TQt. If you modify this file, you may extend this exception to
  your version of the file, but you are not obligated to do so.  If
  you do not wish to do so, delete this exception statement from
  your version.

 ***************************************************************************/

#include "resources.h"
#include "hidingmsgedit.h"
#include "mymultilineedit.h"
#include "editcmd.h"

#include <tqptrlist.h>
#include <tqsizepolicy.h>
#include <tqstringlist.h>
#include <tqtabwidget.h>

#include <kdebug.h>
#include <klocale.h>

using namespace KBabel;

HidingMsgEdit::HidingMsgEdit(uint numberOfPlurals, TQWidget* eventFilter, KSpell* spell, TQWidget *parent, const char *name ) : 
    TQWidgetStack(parent,name)
    , _singleEdit(0)
    , _multipleEdit(0) 
    , _eventFilter(eventFilter)
    , _currentEdit(0)
    , _numberOfPlurals(numberOfPlurals)
    , _spell(spell)
{

  _allEdits.clear();
  setNumberOfPlurals( _numberOfPlurals );
  
  connect( _multipleEdit, TQT_SIGNAL(currentChanged( TQWidget* )),
    this, TQT_SLOT( newCurrentMultiple( TQWidget* )));
    
  showSingle();
}

HidingMsgEdit::~HidingMsgEdit(){
}

void HidingMsgEdit::setText(TQStringList texts, TQString msgctxt){
  if( texts.count() == 0 )
  {
      kdWarning() << "HidingMsgEdit::setText with empty text" << endl;
      _singleEdit->clear();
      showSingle();
      return;
  }

  if (!msgctxt.isEmpty())
      msgctxt = "\n>>>>> " + i18n("Context inserted by KBabel, do not translate:") + "\n" + msgctxt;
  
  if( texts.count() == 1 )
  {
      _singleEdit->setText(*texts.at(0) + msgctxt);
      showSingle();
  }
  else
  {
      if( _numberOfPlurals )
      {
          TQStringList::iterator text = texts.begin();
	  uint i;
          for( i=0 ; i < _numberOfPlurals && text!= texts.end() ; i++, text++ )
	  {
	      static_cast<MsgMultiLineEdit *>(_multipleEdit->page(i))->setText(*text + msgctxt);
	  }
	  
	  // clean the non-initialized ones
	  while (i < _numberOfPlurals)
	  {
	      static_cast<MsgMultiLineEdit *>(_multipleEdit->page(i))->setText("");
	    i++;
	  }
      }
      showMultiple();
  }
}

void HidingMsgEdit::showSingle(){
    raiseWidget(_singleEdit);
    _currentEdit=_singleEdit;
    _currentEdit->setFocus();

    emit currentFormChanged ( 0 );
}

void HidingMsgEdit::showMultiple(){
    raiseWidget(_multipleEdit);
    _currentEdit=static_cast<MsgMultiLineEdit*>(_multipleEdit->currentPage());
    _currentEdit->setFocus();

    emit currentFormChanged ( _multipleEdit->currentPageIndex () );
}

void HidingMsgEdit::showPlurals( bool on )
{
    if( on ) showMultiple();
    else showSingle();
}

void HidingMsgEdit::showForm(int form)
{
    if( _currentEdit==_singleEdit && form>0 )
    {
	showMultiple();
	_multipleEdit->setCurrentPage(form);
	_currentEdit=static_cast<MsgMultiLineEdit*>(_multipleEdit->currentPage());
	emit currentFormChanged ( form );
    }
    else
    if( _currentEdit!=_singleEdit )
    {
	_multipleEdit->setCurrentPage(form);
	_currentEdit=static_cast<MsgMultiLineEdit*>(_multipleEdit->currentPage());
	emit currentFormChanged ( 0 );
    }
    _currentEdit->setFocus();
}

void HidingMsgEdit::setNumberOfPlurals( uint numberOfPlurals )
{
  _numberOfPlurals = numberOfPlurals;
  
  // find out the current shown version
  bool plurals = _currentEdit != _singleEdit;
  bool readonly = _currentEdit  ? _currentEdit->isReadOnly () : true;
  
  // cleanup old
  _currentEdit=0;
  _allEdits.clear();
  if( _singleEdit ) 
  {
    removeWidget( _singleEdit );
    delete _singleEdit;
  }
  
  if( _multipleEdit )
  {
    removeWidget( _multipleEdit );
    delete _multipleEdit;
  }
  
  // create new
  _singleEdit = new MsgMultiLineEdit( 0, _spell, this, "singleEdit" );
  _allEdits.append( _singleEdit );
  if( _eventFilter )
	_singleEdit->installEventFilter(_eventFilter);
  
  _multipleEdit = new TQTabWidget( this );

  MsgMultiLineEdit* pl;
  for(uint i=0 ; i< _numberOfPlurals ; i++)
  {
     pl = new MsgMultiLineEdit( i, _spell, _multipleEdit, TQString("multipleEdit %1").arg(i).local8Bit());
     _allEdits.append(pl);
     _multipleEdit->addTab( pl, i18n("Plural %1").arg(i+1));
     if( _eventFilter )
        pl->installEventFilter(_eventFilter);
  }
  
  addWidget(_singleEdit);
  addWidget(_multipleEdit);
  
  for( MsgMultiLineEdit* e = _allEdits.first() ; e ; e = _allEdits.next() )
  {
      connect( e, TQT_SIGNAL( signalUndoCmd( KBabel::EditCommand* )), 
        this, TQT_SIGNAL( signalUndoCmd( KBabel::EditCommand* )));
      connect( e, TQT_SIGNAL( textChanged() ) , this, TQT_SIGNAL( textChanged() ));
      connect( e, TQT_SIGNAL( textChanged() ) , this, TQT_SLOT( emitTextChanged() ));
      connect( e, TQT_SIGNAL( cursorPositionChanged( int, int )), 
        this, TQT_SLOT( emitCursorPositionChanged( int, int )) );
  }
  
  showPlurals( plurals );
  
  _currentEdit->setReadOnly (readonly);
}

void HidingMsgEdit::emitTextChanged()
{
    emit textChanged(_currentEdit->text());
}

uint HidingMsgEdit::currentForm()
{
    if( _currentEdit == _singleEdit ) return 0;
    else return _multipleEdit->currentPageIndex();
}

void HidingMsgEdit::newCurrentMultiple( TQWidget* widget )
{
    _currentEdit = dynamic_cast<MsgMultiLineEdit*>(widget);
    emit currentFormChanged ( _multipleEdit->currentPageIndex () );
}

bool HidingMsgEdit::isModified() {
    for( MsgMultiLineEdit* e = _allEdits.first(); e ; e = _allEdits.next() )
    {
	if( e->isModified() ) 
	{
	    return true;
	}
    }
    return false;
}

void HidingMsgEdit::setReadOnly(bool on) {
    for( MsgMultiLineEdit* e = _allEdits.first(); e ; e = _allEdits.next() )
    {
	e->setReadOnly( on );
    }
}

void HidingMsgEdit::processCommand(EditCommand* cmd, bool undo)
{
    if( _currentEdit == _singleEdit )
    {
	_singleEdit->processCommand(cmd,undo);
    }
    else
    {
	if( cmd->terminator() == 0)
	{
	    static_cast<MsgMultiLineEdit*>(
		_multipleEdit->page(static_cast<DelTextCmd*>(cmd)->pluralNumber)
	    )->processCommand(cmd,undo);
	}
    }
}

void HidingMsgEdit::setSpacePoints(bool on)
{
    for( MsgMultiLineEdit* e = _allEdits.first(); e ; e = _allEdits.next() )
    {
	e->setSpacePoints( on );
    }
}

void HidingMsgEdit::setHighlightSyntax( bool on )
{
    for( MsgMultiLineEdit* e = _allEdits.first(); e ; e = _allEdits.next() )
    {
	e->setHighlightSyntax( on );
    }
}

void HidingMsgEdit::setQuotes(bool on)
{
    for( MsgMultiLineEdit* e = _allEdits.first(); e ; e = _allEdits.next() )
    {
	e->setQuotes( on );
    }
}

void HidingMsgEdit::setBgColor( const TQColor& color)
{
    for( MsgMultiLineEdit* e = _allEdits.first(); e ; e = _allEdits.next() )
    {
	e->setBgColor( color );
    }
}

void HidingMsgEdit::setHighlightColors(const TQColor& quoteColor, const TQColor& unquoteColor
        , const TQColor& cformatColor, const TQColor& accelColor, const TQColor& tagColor)
{
    for( MsgMultiLineEdit* e = _allEdits.first(); e ; e = _allEdits.next() )
    {
	e->setHighlightColors( quoteColor, unquoteColor, cformatColor,
	    accelColor, tagColor );
    }
}

void HidingMsgEdit::setOverwriteMode( bool b )
{
    for( MsgMultiLineEdit* e = _allEdits.first(); e ; e = _allEdits.next() )
    {
	e->setOverwriteMode( b );
    }
}

void HidingMsgEdit::setModified( bool b )
{
    for( MsgMultiLineEdit* e = _allEdits.first(); e ; e = _allEdits.next() )
    {
	e->setModified( b );
    }
}

void HidingMsgEdit::setCleverEditing( bool on )
{
    for( MsgMultiLineEdit* e = _allEdits.first(); e ; e = _allEdits.next() )
    {
	e->setCleverEditing( on );
    }
}

void HidingMsgEdit::setHighlightBg( bool on )
{
    for( MsgMultiLineEdit* e = _allEdits.first(); e ; e = _allEdits.next() )
    {
	e->setHighlightBg( on );
    }
}

void HidingMsgEdit::setContextMenu( TQPopupMenu *menu )
{
    for( MsgMultiLineEdit* e = _allEdits.first(); e ; e = _allEdits.next() )
    {
	e->setContextMenu( menu );
    }
}

void HidingMsgEdit::setCurrentColor( const MsgMultiLineEdit::TextColor color)
{
    for( MsgMultiLineEdit* e = _allEdits.first(); e ; e = _allEdits.next() )
    {
	e->setCurrentColor( color );
    }
}

bool HidingMsgEdit::hasFocus ()
{
    for( MsgMultiLineEdit* e = _allEdits.first(); e ; e = _allEdits.next() )
    {
	if( e->hasFocus() ) return true;
    }
    
    return _multipleEdit->hasFocus() || TQWidgetStack::hasFocus();
}

void HidingMsgEdit::setDiffColors(const TQColor& addColor, const TQColor& delColor)
{
    for( MsgMultiLineEdit* e = _allEdits.first(); e ; e = _allEdits.next() )
    {
	e->setDiffColors( addColor, delColor );
    }
}

void HidingMsgEdit::setDiffMode(bool on)
{
    for( MsgMultiLineEdit* e = _allEdits.first(); e ; e = _allEdits.next() )
    {
	e->setDiffMode( on );
    }
}

void HidingMsgEdit::setDiffDisplayMode(bool underlineAdded, bool strikeOutDeleted)
{
    for( MsgMultiLineEdit* e = _allEdits.first(); e ; e = _allEdits.next() )
    {
	e->setDiffDisplayMode( underlineAdded, strikeOutDeleted );
    }
}
	 
void HidingMsgEdit::setFont(const TQFont& font)
{
    for( MsgMultiLineEdit* e = _allEdits.first(); e ; e = _allEdits.next() )
    {
	e->setFont( font );
    }
}
	 
void HidingMsgEdit::setSpellChecker(KSpell* spell)
{
    for( MsgMultiLineEdit* e = _allEdits.first(); e ; e = _allEdits.next() )
    {
	e->setSpellChecker( spell );
    }
    _spell = spell;
}

void HidingMsgEdit::emitCursorPositionChanged(int line, int col ) 
{
   int linestart = 0;
    int indexline = _currentEdit->lineOfChar( line, col );
    if ( indexline > 0 )
    {
        int min = 0, max = col;
        int i = (min + max)/2;
        int iline = _currentEdit->lineOfChar( line, i );
        while ( iline != indexline-1 ||
                _currentEdit->lineOfChar( line, i+1 ) != indexline )
        {
            Q_ASSERT( min != max && min != i && max != i );
            if ( iline < indexline )
                min = i;
            else
                max = i;
            i = (min + max)/2;
            iline = _currentEdit->lineOfChar( line, i );
        }
        linestart = i+1;
    }
    Q_ASSERT( linestart >= 0 );

    emit cursorPositionChanged(line + _currentEdit->lineOfChar( line, col ) ,col-linestart);
}
	 
#include "hidingmsgedit.moc"