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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
|
/* -------------------------------------------------------------
queryview.cpp (part of The KDE Dictionary Client)
Copyright (C) 2000-2001 Christian Gebauer <gebauer@kde.org>
This file is distributed under the Artistic License.
See LICENSE for details.
-------------------------------------------------------------
BrowseData data structure used for caching definitions
DictHTMLPart handling of middle mouse button clicks
QueryView widget that displays the definitions
------------------------------------------------------------- */
#include <tqclipboard.h>
#include <tqtimer.h>
#include <kfiledialog.h>
#include <ktempfile.h>
#include <kio/netaccess.h>
#include <kcursor.h>
#include <kpopupmenu.h>
#include <kmessagebox.h>
#include <klocale.h>
#include <khtml_events.h>
#include <khtmlview.h>
#include "actions.h"
#include "options.h"
#include "dict.h"
#include "queryview.h"
#include <kapplication.h>
#include <kiconloader.h>
//******** SaveHelper *******************************************
TQString SaveHelper::lastPath;
SaveHelper::SaveHelper(const TQString &saveName, const TQString &filter, TQWidget *parent)
: p_arent(parent), s_aveName(saveName), f_ilter(filter), file(0), tmpFile(0)
{
}
SaveHelper::~SaveHelper()
{
if (file) { // local filesystem, just close the file
delete file;
} else
if (tmpFile) { // network location, initiate transaction
tmpFile->close();
if (KIO::NetAccess::upload(tmpFile->name(),url, p_arent) == false)
KMessageBox::error(global->topLevel, i18n("Unable to save remote file."));
tmpFile->unlink(); // delete temp file
delete tmpFile;
}
}
TQFile* SaveHelper::getFile(const TQString &dialogTitle)
{
url = KFileDialog::getSaveURL(lastPath+s_aveName,f_ilter,p_arent,dialogTitle);
if (url.isEmpty())
return 0;
lastPath = url.url(-1);
lastPath.truncate(lastPath.length()-url.fileName().length());
if (url.isLocalFile()) {
if (TQFileInfo(url.path()).exists() &&
(KMessageBox::warningContinueCancel(global->topLevel,
i18n("A file named %1 already exists.\nDo you want to replace it?").arg(url.path()),
dialogTitle, i18n("&Replace")) != KMessageBox::Continue)) {
return 0;
}
file = new TQFile(url.path());
if(!file->open(IO_WriteOnly)) {
KMessageBox::error(global->topLevel, i18n("Unable to save file."));
delete file;
file = 0;
}
return file;
} else {
tmpFile = new KTempFile();
if (tmpFile->status()!=0)
KMessageBox::error(global->topLevel, i18n("Unable to create temporary file.")); {
delete tmpFile;
tmpFile = 0;
return 0;
}
return tmpFile->file();
}
}
//**** BrowseData ****************************************************
BrowseData::BrowseData(const TQString &Nhtml, const TQString &NqueryText)
: html(Nhtml),queryText(NqueryText),xPos(0),yPos(0)
{}
//********* DictHTMLPart ******************************************
DictHTMLPart::DictHTMLPart(TQWidget *parentWidget, const char *widgetname)
: KHTMLPart(parentWidget,widgetname)
{}
DictHTMLPart::~DictHTMLPart()
{}
void DictHTMLPart::khtmlMouseReleaseEvent(khtml::MouseReleaseEvent *event)
{
if (event->qmouseEvent()->button()==MidButton)
emit(middleButtonClicked());
else
KHTMLPart::khtmlMouseReleaseEvent(event);
}
//********* QueryView ******************************************
QueryView::QueryView(TQWidget *_parent)
: TQVBox( _parent), actBack(0L), actForward(0L), actQueryCombo(0L), browsePos(0), isRendering(false)
{
browseList.setAutoDelete(true);
part=new DictHTMLPart(this);
part->setDNDEnabled(false);
part->setJScriptEnabled(false);
part->setJavaEnabled(false);
part->setURLCursor(KCursor::handCursor());
setFocusPolicy(TQWidget::NoFocus);
connect(part, TQT_SIGNAL(completed()), TQT_SLOT(partCompleted()));
connect(part, TQT_SIGNAL(middleButtonClicked()), TQT_SLOT(middleButtonClicked()));
rightBtnMenu = new KPopupMenu(this);
connect(part,TQT_SIGNAL(popupMenu(const TQString &, const TQPoint &)),this,TQT_SLOT(buildPopupMenu(const TQString &, const TQPoint &)));
connect(part->browserExtension(),TQT_SIGNAL(openURLRequest(const KURL &,const KParts::URLArgs &)),
this,TQT_SLOT(slotURLRequest(const KURL &,const KParts::URLArgs &)));
connect(part->browserExtension(),TQT_SIGNAL(enableAction(const char *,bool)),this,TQT_SLOT(enableAction(const char *,bool)));
TQTimer::singleShot( 0, this, TQT_SLOT(optionsChanged()) );
connect(interface,TQT_SIGNAL(resultReady(const TQString &, const TQString &)), TQT_SLOT(resultReady(const TQString &, const TQString &)));
}
QueryView::~QueryView()
{}
void QueryView::setActions(KToolBarPopupAction* NactBack, KToolBarPopupAction* NactForward, DictComboAction* NactQueryCombo)
{
actBack = NactBack;
connect(actBack->popupMenu(),TQT_SIGNAL(activated(int)),TQT_SLOT(browseBack(int)));
actForward = NactForward;
connect(actForward->popupMenu(),TQT_SIGNAL(activated(int)),TQT_SLOT(browseForward(int)));
actQueryCombo = NactQueryCombo;
}
bool QueryView::browseBackPossible() const
{
return (browsePos > 0)? true:false;
}
bool QueryView::browseForwardPossible() const
{
return (browsePos+1 < browseList.count()) ? true:false;
}
void QueryView::optionsChanged()
{
saveCurrentResultPos();
currentHTMLHeader = TQString("<html><head><style type=\"text/css\">\n");
currentHTMLHeader += TQString("body { background-color:%1; color:%2; }\n").arg(global->backgroundColor().name()).arg(global->textColor().name());
currentHTMLHeader += TQString("a:link, a:active { color:%1; text-decoration:none; }\n").arg(global->linksColor().name());
currentHTMLHeader += TQString("a:visited { color:%1; text-decoration:none; }\n").arg(global->visitedLinksColor().name());
currentHTMLHeader += TQString("a:hover { color:%1; text-decoration:underline; }\n").arg(global->linksColor().name());
currentHTMLHeader += TQString("p.heading { margin-left:0mm; margin-top:2mm; margin-bottom:2mm; padding:1mm; background-color:%1; color:%2; font-family:%3; font-size:%4pt; ").arg(global->headingsBackgroundColor().name()).arg(global->headingsTextColor().name()).arg(global->headingsFont().family()).arg(global->headingsFont().pointSize());
if (global->headingsFont().italic())
currentHTMLHeader += TQString("font-style:italic; ");
if (global->headingsFont().bold())
currentHTMLHeader += TQString("font-weight:bold; ");
currentHTMLHeader += TQString("}\n");
currentHTMLHeader += TQString("p.definition { margin-left:1.5mm; margin-top:2mm; margin-bottom:2mm;}\n");
currentHTMLHeader += TQString("table { margin-left:1.5mm; margin-top:2mm; margin-bottom:2mm;}\n");
currentHTMLHeader += TQString("pre { font-family:%1; font-size:%2pt; ").arg(global->textFont().family()).arg(global->textFont().pointSize());
if (global->textFont().italic())
currentHTMLHeader += TQString("font-style:italic; ");
if (global->textFont().bold())
currentHTMLHeader += TQString("font-weight:bold; ");
currentHTMLHeader += TQString("}\n");
currentHTMLHeader += TQString("</style></head>\n");
showResult(); // apply changes directly
}
void QueryView::stop()
{
if (isRendering == true) {
part->closeURL();
isRendering = false;
emit(renderingStopped());
}
}
// print current result
void QueryView::printQuery()
{
part->view()->print();
}
// save the current result in an .html file
void QueryView::saveQuery()
{
if (!browseList.isEmpty()) {
BrowseData* brw = browseList.at(browsePos);
TQString fName = brw->queryText+".html";
fName.replace(TQRegExp("[\\s/]"),"_");
SaveHelper helper(fName,"*.html",global->topLevel);
TQFile *file = helper.getFile(TQString::null);
if (file) {
TQTextStream stream(file);
stream.setEncoding(TQTextStream::Locale);
stream << currentHTMLHeader+brw->html;
}
}
}
void QueryView::browseBack()
{
if (browseBackPossible()) {
saveCurrentResultPos();
browsePos--;
actQueryCombo->setEditText(browseList.at(browsePos)->queryText);
showResult();
updateBrowseActions();
}
}
void QueryView::browseForward()
{
if (browseForwardPossible()) {
saveCurrentResultPos();
browsePos++;
actQueryCombo->setEditText(browseList.at(browsePos)->queryText);
showResult();
updateBrowseActions();
}
}
void QueryView::selectAll()
{
part->selectAll();
}
void QueryView::copySelection()
{
kapp->clipboard()->setText(part->selectedText());
}
void QueryView::showFindDialog()
{
KAction *act = part->actionCollection()->action("find");
if (act)
act->activate();
}
void QueryView::paletteChange ( const TQPalette & )
{
optionsChanged();
}
void QueryView::fontChange ( const TQFont & )
{
optionsChanged();
}
void QueryView::saveCurrentResultPos()
{
if (!browseList.isEmpty()) {
browseList.at(browsePos)->xPos = part->view()->contentsX();
browseList.at(browsePos)->yPos = part->view()->contentsY();
}
}
void QueryView::showResult()
{
if (!isRendering) {
isRendering = true;
emit(renderingStarted());
}
part->begin();
if (browseList.isEmpty()) {
part->write(currentHTMLHeader+"<body></body></html>");
part->end();
} else {
BrowseData* brw = browseList.at(browsePos);
emit(newCaption(getShortString(brw->queryText.simplifyWhiteSpace(),70)));
part->write(currentHTMLHeader+brw->html);
part->end();
part->view()->setFocus();
}
}
void QueryView::resultReady(const TQString &result, const TQString &query)
{
BrowseData* brw = new BrowseData(result,query);
if (browseList.isEmpty()) {
browsePos = 0;
browseList.append(brw);
} else {
saveCurrentResultPos();
while (browseList.count()>browsePos+1)
browseList.removeLast();
browseList.append(brw);
browsePos++;
while (browseList.count()>global->maxBrowseListEntrys) {
browseList.removeFirst();
browsePos--;
}
}
showResult();
emit(enablePrintSave());
actQueryCombo->selectAll();
updateBrowseActions();
}
void QueryView::partCompleted()
{
if (!browseList.isEmpty())
part->view()->setContentsPos(browseList.at(browsePos)->xPos,browseList.at(browsePos)->yPos);
if (isRendering) {
emit(renderingStopped());
isRendering = false;
}
}
void QueryView::slotURLRequest (const KURL &url, const KParts::URLArgs &)
{
TQString type = url.host();
TQString urlText = url.prettyURL();
urlText.remove(0,8+type.length());
if (type.length()) { // valid url
if(type=="define")
emit(defineRequested(urlText));
if(type=="dbinfo")
interface->showDbInfo(urlText.utf8());
if(type=="realhttp")
kapp->invokeBrowser("http://"+urlText);
if(type=="realftp")
kapp->invokeBrowser("ftp://"+urlText);
}
}
void QueryView::middleButtonClicked()
{
emit(clipboardRequested());
}
// construct the right-mouse-button-popup-menu on demand
void QueryView::buildPopupMenu(const TQString &url, const TQPoint &point)
{
rightBtnMenu->clear();
if (!url.isEmpty()) { // menuitem if mouse is over link
KURL u(url);
TQString type = u.host();
popupLink = u.prettyURL();
popupLink.remove(0,8+type.length());
if (type.length()) { // valid url
if(type=="define") {
rightBtnMenu->insertItem(i18n("Define &Synonym"),
this,TQT_SLOT(popupDefineLink()));
rightBtnMenu->insertItem(i18n("M&atch Synonym"),
this,TQT_SLOT(popupMatchLink()));
rightBtnMenu->insertSeparator();
}
if(type=="dbinfo") {
rightBtnMenu->insertItem(i18n("D&atabase Information"),this,TQT_SLOT(popupDbInfo()));
rightBtnMenu->insertSeparator();
}
if(type=="realhttp") {
popupLink.prepend("http://");
rightBtnMenu->insertItem(SmallIcon("fileopen"),
i18n("&Open Link"),
this,TQT_SLOT(popupOpenLink()));
rightBtnMenu->insertSeparator();
}
if(type=="realftp") {
popupLink.prepend("ftp://");
rightBtnMenu->insertItem(SmallIcon("fileopen"),
i18n("&Open Link"),
this,TQT_SLOT(popupOpenLink()));
rightBtnMenu->insertSeparator();
}
}
}
if (part->hasSelection()) {
popupSelect = part->selectedText();
rightBtnMenu->insertItem(i18n("&Define Selection"),
this,TQT_SLOT(popupDefineSelect()));
rightBtnMenu->insertItem(i18n("&Match Selection"),
this,TQT_SLOT(popupMatchSelect()));
rightBtnMenu->insertSeparator();
} else {
kapp->clipboard()->setSelectionMode(false);
TQString text = kapp->clipboard()->text();
if (text.isEmpty()) {
kapp->clipboard()->setSelectionMode(true);
text = kapp->clipboard()->text();
}
if (!text.isEmpty()) {
popupSelect = TQApplication::clipboard()->text();
rightBtnMenu->insertItem(SmallIcon("define_clip"),
i18n("&Define Clipboard Content"),
this,TQT_SLOT(popupDefineSelect()));
rightBtnMenu->insertItem(i18n("&Match Clipboard Content"),
this,TQT_SLOT(popupMatchSelect()));
rightBtnMenu->insertSeparator();
}
}
int ID;
if (browseBackPossible()) { // if possible, show string
if (browseList.at(browsePos-1)->queryText.isEmpty())
rightBtnMenu->insertItem(SmallIcon("back"),
i18n("&Back: Information"),
this,TQT_SLOT(browseBack()));
else
rightBtnMenu->insertItem(SmallIcon("back"),
i18n("&Back: '%1'").arg(getShortString(browseList.at(browsePos-1)->queryText,25)),
this,TQT_SLOT(browseBack()));
} else {
ID = rightBtnMenu->insertItem(SmallIcon("back"), i18n("&Back"), this, TQT_SLOT(browseBack()));
rightBtnMenu->setItemEnabled(ID,false);
}
if (browseForwardPossible()) { // if possible, show string
if (browseList.at(browsePos+1)->queryText.isEmpty())
rightBtnMenu->insertItem(SmallIcon("forward"),
i18n("&Forward: Information"),
this,TQT_SLOT(browseForward()));
else
rightBtnMenu->insertItem(SmallIcon("forward"),
i18n("&Forward: '%1'").arg(getShortString(browseList.at(browsePos+1)->queryText,25)),
this,TQT_SLOT(browseForward()));
} else {
ID = rightBtnMenu->insertItem(SmallIcon("forward"),i18n("&Forward"),this,TQT_SLOT(browseForward()));
rightBtnMenu->setItemEnabled(ID,false);
}
rightBtnMenu->popup(point);
}
void QueryView::popupDefineLink()
{
emit(defineRequested(popupLink));
}
void QueryView::popupMatchLink()
{
emit(matchRequested(popupLink));
}
void QueryView::popupOpenLink()
{
kapp->invokeBrowser(popupLink);
}
void QueryView::popupDefineSelect()
{
emit(defineRequested(popupSelect));
}
void QueryView::popupMatchSelect()
{
emit(matchRequested(popupSelect));
}
void QueryView::popupDbInfo()
{
interface->showDbInfo(popupLink.utf8());
}
void QueryView::enableAction(const char * name, bool enabled)
{
if (!strcmp(name,"copy"))
emit(enableCopy(enabled));
}
void QueryView::browseBack(int index)
{
int x = browsePos-index;
if (x>=0) {
saveCurrentResultPos();
browsePos = x;
actQueryCombo->setEditText(browseList.at(browsePos)->queryText);
showResult();
TQTimer::singleShot(0, this, TQT_SLOT(updateBrowseActions())); // don't clear the menu in this slot
}
}
void QueryView::browseForward(int index)
{
int x = browsePos+index;
if (x < (int)(browseList.count())) {
saveCurrentResultPos();
browsePos = x;
actQueryCombo->setEditText(browseList.at(browsePos)->queryText);
showResult();
TQTimer::singleShot(0, this, TQT_SLOT(updateBrowseActions())); // don't clear the menu in this slot
}
}
void QueryView::updateBrowseActions()
{
if (browseBackPossible()) {
actBack->setEnabled(true);
if (browseList.at(browsePos-1)->queryText.isEmpty())
actBack->setText(i18n("&Back: Information"));
else
actBack->setText(i18n("&Back: '%1'").arg(getShortString(browseList.at(browsePos-1)->queryText,25)));
actBack->popupMenu()->clear();
int i = browsePos-1;
int num = 1;
TQString s;
while ((i>=0)&&(num<=10)) {
s = browseList.at(i)->queryText;
if (s.isEmpty()) s = i18n("Information");
actBack->popupMenu()->insertItem(s,num);
num++;
i--;
}
} else {
actBack->setEnabled(false);
actBack->setText(i18n("&Back"));
actBack->popupMenu()->clear();
}
if (browseForwardPossible()) {
actForward->setEnabled(true);
if (browseList.at(browsePos+1)->queryText.isEmpty())
actForward->setText(i18n("&Forward: Information"));
else
actForward->setText(i18n("&Forward: '%1'").arg(getShortString(browseList.at(browsePos+1)->queryText,25)));
actForward->popupMenu()->clear();
int i = browsePos+1;
int num = 1;
TQString s;
while ((i<(int)(browseList.count()))&&(num<=10)) {
s = browseList.at(i)->queryText;
if (s.isEmpty()) s = i18n("Information");
actForward->popupMenu()->insertItem(s,num);
num++;
i++;
}
} else {
actForward->setEnabled(false);
actForward->setText(i18n("&Forward"));
actForward->popupMenu()->clear();
}
}
//--------------------------------
#include "queryview.moc"
|