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
|
/***************************************************************************
* Copyright (C) 2007 by Rajko Albrecht *
* ral@alwins-world.de *
* *
* 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. *
* *
* This program 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 General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
***************************************************************************/
#include "diffbrowser.h"
#include "diffbrowserdata.h"
#include "tdesvnsettings.h"
#include <tdeglobalsettings.h>
#include <tdeglobal.h>
#include <kdebug.h>
#include <tdefiledialog.h>
#include <tdemessagebox.h>
#include <tdeapplication.h>
#include <tdelocale.h>
#include <tqfont.h>
#include <tqtooltip.h>
#include <tqwhatsthis.h>
#include <tqtextcodec.h>
/*!
\fn DiffBrowser::DiffBrowser(TQWidget*parent=0,const char*name=0)
*/
DiffBrowser::DiffBrowser(TQWidget*parent,const char*name)
: KTextBrowser( parent, name)
{
setTextFormat(TQt::PlainText);
setFont(TDEGlobalSettings::fixedFont());
m_Data = new DiffBrowserData;
setWordWrap(TQTextEdit::NoWrap);
m_Data->m_Syntax = new DiffSyntax(this);
TQToolTip::add(this,i18n("Ctrl-F for search, F3 or Shift-F3 for search again."));
TQWhatsThis::add(this,i18n("<b>Display differences between files</b><p>You may search inside text with Ctrl-F.</p><p>F3 for search forward again, Shift-F3 for search backward again.</p><p>You may save the (original) output with Ctrl-S.</p>"));
setFocus();
}
/*!
\fn DiffBrowser::~DiffBrowser()
*/
DiffBrowser::~DiffBrowser()
{
delete m_Data;
}
void DiffBrowser::setText(const TQString&aText)
{
m_Data->m_content.setRawData(aText.local8Bit(),aText.local8Bit().size());
KTextBrowser::setText(aText);
setCursorPosition(0,0);
}
void DiffBrowser::setText(const TQByteArray&aText)
{
m_Data->m_content=aText;
printContent();
setCursorPosition(0,0);
}
void DiffBrowser::printContent()
{
TQTextCodec * cc = TQTextCodec::codecForName(Kdesvnsettings::locale_for_diff().ascii());
if (!cc) {
KTextBrowser::setText(TQString::fromLocal8Bit(m_Data->m_content,m_Data->m_content.size()));
} else {
KTextBrowser::setText(cc->toUnicode(m_Data->m_content,m_Data->m_content.size()));
}
}
/*!
\fn DiffBrowser::saveDiff()
*/
void DiffBrowser::saveDiff()
{
TQString saveTo = KFileDialog::getSaveFileName(TQString(),"text/x-diff");
if (saveTo.isEmpty()) {
return;
}
TQFile tfile(saveTo);
if (tfile.exists()) {
if (KMessageBox::warningYesNo(TQT_TQWIDGET(TDEApplication::activeModalWidget()),
i18n("File %1 exists - overwrite?").arg(saveTo))
!=KMessageBox::Yes) {
return;
}
}
tfile.open(IO_Truncate|IO_WriteOnly|IO_Raw);
TQDataStream stream( &tfile );
stream.writeRawBytes(m_Data->m_content.data(),m_Data->m_content.size());
}
void DiffBrowser::keyPressEvent(TQKeyEvent*ev)
{
if ( ev->key() == Key_Return) {
ev->ignore();
return;
}
if (ev->key() == Key_F3) {
if (ev->state() == ShiftButton) {
searchagainback_slot();
} else {
searchagain_slot();
}
} else if (ev->key()==Key_F && ev->state() == ControlButton) {
startSearch();
} else if (ev->key()==Key_S && ev->state() == ControlButton) {
saveDiff();
} else {
KTextBrowser::keyPressEvent(ev);
}
}
void DiffBrowser::startSearch()
{
if( !m_Data->srchdialog ) {
m_Data->srchdialog = new KEdFind( this, "searchdialog", false);
connect(m_Data->srchdialog,TQT_SIGNAL(search()),this,TQT_SLOT(search_slot()));
connect(m_Data->srchdialog,TQT_SIGNAL(done()),this,TQT_SLOT(searchdone_slot()));
}
TQString _st = m_Data->srchdialog->getText();
m_Data->srchdialog->setText(_st.isEmpty() ? m_Data->pattern : _st);
m_Data->srchdialog->show();
m_Data->srchdialog->result();
}
/*!
\fn DiffBrowser::search_slot()
*/
void DiffBrowser::search_slot()
{
if( !m_Data->srchdialog ) {
return;
}
TQString to_find_string = m_Data->srchdialog->getText();
doSearch(to_find_string,m_Data->srchdialog->case_sensitive(),m_Data->srchdialog->get_direction());
}
void DiffBrowser::doSearch(const TQString&to_find_string,bool case_sensitive,bool back)
{
if( !m_Data->srchdialog ) {
return;
}
int line, col;
getCursorPosition(&line,&col);
if (m_Data->last_search != DiffBrowserData::NONE && !back) {
col = col+1;
}
while (1) {
bool result = find(to_find_string,case_sensitive,false,
(!back),&line,&col);
if (result) {
m_Data->last_search = back?DiffBrowserData::BACKWARD:DiffBrowserData::FORWARD;
m_Data->pattern=to_find_string;
break;
}
TQWidget * _parent = m_Data->srchdialog->isVisible()?m_Data->srchdialog:parentWidget();
if (!m_Data->srchdialog->get_direction()) {
// forward
int query = KMessageBox::questionYesNo(
_parent,
i18n("End of document reached.\n"\
"Continue from the beginning?"),
i18n("Find"),KStdGuiItem::cont(),i18n("Stop"));
if (query == KMessageBox::Yes){
line = 0;
col = 0;
m_Data->last_search = DiffBrowserData::FORWARD;
} else {
break;
}
} else {
int query = KMessageBox::questionYesNo(
_parent,
i18n("Beginning of document reached.\n"\
"Continue from the end?"),
i18n("Find"),KStdGuiItem::cont(),i18n("Stop"));
if (query == KMessageBox::Yes){
line = lines()-1;
TQString string = text(line);
col = string.length();
if (col>0) {
--col;
}
m_Data->last_search = DiffBrowserData::BACKWARD;
} else {
break;
}
}
}
}
/*!
\fn DiffBrowser::searchdone_slot()
*/
void DiffBrowser::searchdone_slot()
{
if (!m_Data->srchdialog)
return;
m_Data->srchdialog->hide();
setFocus();
m_Data->last_finished_search = m_Data->last_search;
m_Data->last_search = DiffBrowserData::NONE;
m_Data->cs = m_Data->srchdialog->case_sensitive();
}
void DiffBrowser::searchagain_slot()
{
doSearchAgain(false);
}
void DiffBrowser::searchagainback_slot()
{
doSearchAgain(true);
}
void DiffBrowser::doSearchAgain(bool back)
{
if (!m_Data->srchdialog || m_Data->pattern.isEmpty()) {
startSearch();
} else {
m_Data->last_search = m_Data->last_finished_search;
doSearch(m_Data->pattern,m_Data->cs,back);
m_Data->last_finished_search = m_Data->last_search;
m_Data->last_search = DiffBrowserData::NONE;
}
}
void DiffBrowser::slotTextCodecChanged(const TQString&codec)
{
if (Kdesvnsettings::locale_for_diff()!=codec) {
Kdesvnsettings::setLocale_for_diff(codec);
printContent();
Kdesvnsettings::self()->writeConfig();
}
}
#include "diffbrowser.h.moc"
|