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
|
/***************************************************************************
* Copyright (C) 2001 by Harald Fernengel *
* harry@tdevelop.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. *
* *
***************************************************************************/
#include <tqlayout.h>
#include <tqtextedit.h>
#include <tqpopupmenu.h>
#include <tqcursor.h>
#include <tqfile.h>
#include <kconfig.h>
#include <kapplication.h>
#include <klocale.h>
#include <kservice.h>
#include <ktempfile.h>
#include <kpopupmenu.h>
#include <kiconloader.h>
#include <kfiledialog.h>
#include <kmessagebox.h>
#include <kparts/componentfactory.h>
#include <kparts/part.h>
#include <kio/jobclasses.h>
#include <kio/job.h>
#include <kdevmainwindow.h>
#include "diffpart.h"
#include "diffwidget.h"
// yup, magic value for the popupmenu-id
static const int POPUP_BASE = 130977;
TQStringList KDiffTextEdit::extParts;
TQStringList KDiffTextEdit::extPartsTranslated;
KDiffTextEdit::KDiffTextEdit( TQWidget* parent, const char* name ): TQTextEdit( parent, name )
{
KConfig* config = kapp->config();
config->setGroup( "Diff" );
_highlight = config->readBoolEntry( "Highlight", true );
searchExtParts();
}
KDiffTextEdit::~KDiffTextEdit()
{
KConfig* config = kapp->config();
config->setGroup( "Diff" );
config->writeEntry( "Highlight", _highlight );
}
TQPopupMenu* KDiffTextEdit::createPopupMenu()
{
return createPopupMenu( TQPoint() );
}
TQPopupMenu* KDiffTextEdit::createPopupMenu( const TQPoint& p )
{
TQPopupMenu* popup = TQTextEdit::createPopupMenu( p );
if ( !popup )
popup = new TQPopupMenu( this );
int i = 0;
for ( TQStringList::Iterator it = extPartsTranslated.begin(); it != extPartsTranslated.end(); ++it ) {
popup->insertItem( i18n( "Show in %1" ).tqarg( *it ), i + POPUP_BASE, i );
i++;
}
if ( !extPartsTranslated.isEmpty() )
popup->insertSeparator( i );
connect( popup, TQT_SIGNAL(activated(int)), this, TQT_SLOT(popupActivated(int)) );
popup->insertItem( SmallIconSet( "filesaveas" ), i18n( "&Save As..." ), this, TQT_SLOT(saveAs()), CTRL + Key_S, POPUP_BASE - 2, 0 );
popup->setItemEnabled( POPUP_BASE - 2, length() > 0 );
popup->insertSeparator( 1 );
popup->insertItem( i18n( "Highlight Syntax" ), this, TQT_SLOT(toggleSyntaxHighlight()), 0, POPUP_BASE - 1, 2 );
popup->setItemChecked( POPUP_BASE - 1, _highlight );
popup->insertSeparator( 3 );
popup->insertSeparator();
popup->insertItem( i18n("Hide view"), parent(), TQT_SLOT(hideView()) );
return popup;
}
void KDiffTextEdit::saveAs()
{
TQString fName = KFileDialog::getSaveFileName();
if ( fName.isEmpty() )
return;
TQFile f( fName );
if ( f.open( IO_WriteOnly ) ) {
TQTextStream stream( &f );
int pCount = paragraphs();
for ( int i = 0; i < pCount; ++i )
stream << text( i ) << "\n";
f.close();
} else {
KMessageBox::sorry( 0, i18n("Unable to open file."), i18n("Diff Frontend") );
}
}
void KDiffTextEdit::toggleSyntaxHighlight()
{
_highlight = !_highlight;
if ( _highlight )
applySyntaxHighlight();
else
clearSyntaxHighlight();
}
void KDiffTextEdit::applySyntaxHighlight()
{
// the diff has been loaded so we apply a simple highlighting
static TQColor cAdded( 190, 190, 237);
static TQColor cRemoved( 190, 237, 190 );
if ( !_highlight )
return;
int paragCount = paragraphs();
for ( int i = 0; i < paragCount; ++i ) {
TQString txt = text( i );
if ( txt.length() > 0 ) {
if ( txt.startsWith( "+" ) || txt.startsWith( ">" ) ) {
setParagraphBackgroundColor( i, cAdded );
} else if ( txt.startsWith( "-" ) || txt.startsWith( "<" ) ) {
setParagraphBackgroundColor( i, cRemoved );
}
}
}
}
void KDiffTextEdit::clearSyntaxHighlight()
{
int paragCount = paragraphs();
for ( int i = 0; i < paragCount; ++i ) {
clearParagraphBackground( i );
}
}
void KDiffTextEdit::searchExtParts()
{
// only execute once
static bool init = false;
if ( init )
return;
init = true;
// search all parts that can handle text/x-diff
KTrader::OfferList offers = KTrader::self()->query("text/x-diff", "('KParts/ReadOnlyPart' in ServiceTypes) and ('text/x-diff' in ServiceTypes) and (DesktopEntryName != 'katepart')");
KTrader::OfferList::const_iterator it;
for ( it = offers.begin(); it != offers.end(); ++it ) {
KService::Ptr ptr = (*it);
extPartsTranslated << ptr->name();
extParts << ptr->desktopEntryName();
}
return;
}
void KDiffTextEdit::popupActivated( int id )
{
id -= POPUP_BASE;
if ( id < 0 || id > (int)extParts.count() )
return;
emit externalPartRequested( extParts[ id ] );
}
DiffWidget::DiffWidget( DiffPart * part, TQWidget *parent, const char *name, WFlags f ):
TQWidget( parent, name, f ), m_part( part ), tempFile( 0 )
{
job = 0;
extPart = 0;
te = new KDiffTextEdit( this, "Main Diff Viewer" );
te->setReadOnly( true );
te->setTextFormat( TQTextEdit::PlainText );
// te->setMinimumSize( 300, 200 );
connect( te, TQT_SIGNAL(externalPartRequested(const TQString&)), this, TQT_SLOT(loadExtPart(const TQString&)) );
TQVBoxLayout* tqlayout = new TQVBoxLayout( this );
tqlayout->addWidget( te );
}
DiffWidget::~DiffWidget()
{
m_part = 0;
delete tempFile;
}
void DiffWidget::setExtPartVisible( bool visible )
{
if ( !extPart || !extPart->widget() ) {
te->show();
return;
}
if ( visible ) {
te->hide();
extPart->widget()->show();
} else {
te->show();
extPart->widget()->hide();
}
}
void DiffWidget::loadExtPart( const TQString& partName )
{
if ( extPart ) {
setExtPartVisible( false );
delete extPart;
extPart = 0;
}
KService::Ptr extService = KService::serviceByDesktopName( partName );
if ( !extService )
return;
extPart = KParts::ComponentFactory::createPartInstanceFromService<KParts::ReadOnlyPart>( extService, this, 0, TQT_TQOBJECT(this), 0 );
if ( !extPart || !extPart->widget() )
return;
tqlayout()->add( extPart->widget() );
setExtPartVisible( true );
if ( te->paragraphs() > 0 )
populateExtPart();
}
void DiffWidget::slotClear()
{
rawDiff = TQString();
te->clear();
if ( extPart )
extPart->closeURL();
}
// internally for the TextEdit only!
void DiffWidget::slotAppend( const TQString& str )
{
te->append( str );
}
// internally for the TextEdit only!
void DiffWidget::slotAppend( KIO::Job*, const TQByteArray& ba )
{
slotAppend( TQString( ba ) );
}
void DiffWidget::populateExtPart()
{
if ( !extPart )
return;
bool ok = false;
int paragCount = te->paragraphs();
if ( extPart->openStream( "text/plain", KURL() ) ) {
for ( int i = 0; i < paragCount; ++i )
extPart->writeStream( rawDiff.local8Bit() );
ok = extPart->closeStream();
} else {
// workaround for parts that cannot handle streams
delete tempFile;
tempFile = new KTempFile();
tempFile->setAutoDelete( true );
*(tempFile->textStream()) << rawDiff.local8Bit() << endl;
tempFile->close();
ok = extPart->openURL( KURL::fromPathOrURL( tempFile->name() ) );
}
if ( !ok )
setExtPartVisible( false );
}
// internally for the TextEdit only!
void DiffWidget::slotFinished()
{
te->applySyntaxHighlight();
populateExtPart();
}
void DiffWidget::setDiff( const TQString& diff )
{
slotClear();
rawDiff = diff;
slotAppend( diff );
slotFinished();
}
void DiffWidget::openURL( const KURL& url )
{
if ( job )
job->kill();
KIO::TransferJob* job = KIO::get( url );
if ( !job )
return;
connect( job, TQT_SIGNAL(data( KIO::Job *, const TQByteArray & )),
this, TQT_SLOT(slotAppend( KIO::Job*, const TQByteArray& )) );
connect( job, TQT_SIGNAL(result( KIO::Job * )),
this, TQT_SLOT(slotFinished()) );
}
void DiffWidget::contextMenuEvent( TQContextMenuEvent* /* e */ )
{
TQPopupMenu* popup = new TQPopupMenu( this );
if ( !te->isVisible() )
{
popup->insertItem( i18n("Display &Raw Output"), this, TQT_SLOT(showTextEdit()) );
popup->insertSeparator();
popup->insertItem( i18n("Hide view"), this, TQT_SLOT(hideView()) );
}
popup->exec( TQCursor::pos() );
delete popup;
}
void DiffWidget::showExtPart()
{
setExtPartVisible( true );
}
void DiffWidget::showTextEdit()
{
setExtPartVisible( false );
}
void DiffWidget::hideView()
{
m_part->mainWindow()->setViewAvailable( this, false );
}
#include "diffwidget.moc"
|