Parent: [868527] (diff)

Child: [be485e] (diff)

Download this file

preview.ui.h    469 lines (413 with data), 12.6 kB

  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
/****************************************************************************
** ui.h extension file, included from the uic-generated form implementation.
**
** If you want to add, delete, or rename functions or slots, use
** Qt Designer to update this file, preserving your code.
**
** You should not define a constructor or destructor in this file.
** Instead, write your code in functions called init() and destroy().
** These will automatically be called by the form's constructor and
** destructor.
*****************************************************************************/
#include <unistd.h>
#include <time.h>
#include <list>
#include <utility>
#ifndef NO_NAMESPACES
using std::pair;
#endif /* NO_NAMESPACES */
#include <qmessagebox.h>
#include <qprogressdialog.h>
#include <qthread.h>
#include "debuglog.h"
#include "pathut.h"
#include "internfile.h"
#include "recoll.h"
#include "plaintorich.h"
extern int recollNeedsExit;
// We keep a list of data associated to each tab
class TabData {
public:
string fn; // filename for this tab
string ipath; // Internal doc path inside file
QWidget *w; // widget for setCurrent
TabData(QWidget *wi) : w(wi) {}
};
#define TABDATA ((list<TabData> *)tabData)
void Preview::init()
{
connect(pvTab, SIGNAL(currentChanged(QWidget *)),
this, SLOT(currentChanged(QWidget *)));
searchTextLine->installEventFilter(this);
dynSearchActive = false;
canBeep = true;
tabData = new list<TabData>;
TABDATA->push_back(TabData(pvTab->currentPage()));
}
void Preview::destroy()
{
delete TABDATA;
}
void Preview::closeEvent(QCloseEvent *e)
{
emit previewClosed(this);
QWidget::closeEvent(e);
}
bool Preview::eventFilter(QObject *target, QEvent *event)
{
if (event->type() != QEvent::KeyPress)
return QWidget::eventFilter(target, event);
LOGDEB1(("Preview::eventFilter: keyEvent\n"));
QKeyEvent *keyEvent = (QKeyEvent *)event;
if (keyEvent->key() == Key_Q && (keyEvent->state() & ControlButton)) {
recollNeedsExit = 1;
return true;
} else if (keyEvent->key() ==Key_W &&(keyEvent->state() & ControlButton)) {
// LOGDEB(("Preview::eventFilter: got ^W\n"));
closeCurrentTab();
return true;
} else if (dynSearchActive) {
if (keyEvent->key() == Key_F3) {
doSearch(true, false);
return true;
}
if (target != searchTextLine)
return QApplication::sendEvent(searchTextLine, event);
} else {
QWidget *tw = pvTab->currentPage();
QWidget *e = 0;
if (tw)
e = (QTextEdit *)tw->child("pvEdit");
LOGDEB1(("Widget: %p, edit %p, target %p\n", tw, e, target));
if (e && target == tw && keyEvent->key() == Key_Slash) {
searchTextLine->setFocus();
dynSearchActive = true;
return true;
}
}
return QWidget::eventFilter(target, event);
}
void Preview::searchTextLine_textChanged(const QString & text)
{
LOGDEB1(("search line text changed. text: '%s'\n", text.ascii()));
if (text.isEmpty()) {
dynSearchActive = false;
nextButton->setEnabled(false);
prevButton->setEnabled(false);
clearPB->setEnabled(false);
} else {
dynSearchActive = true;
nextButton->setEnabled(true);
prevButton->setEnabled(true);
clearPB->setEnabled(true);
doSearch(false, false);
}
}
QTextEdit * Preview::getCurrentEditor()
{
QWidget *tw = pvTab->currentPage();
QTextEdit *edit = 0;
if (tw) {
edit = (QTextEdit*)tw->child("pvEdit");
}
return edit;
}
// Perform text search. If next is true, we look for the next match of the
// current search, trying to advance and possibly wrapping around. If next is
// false, the search string has been modified, we search for the new string,
// starting from the current position
void Preview::doSearch(bool next, bool reverse)
{
LOGDEB1(("Preview::doSearch: next %d rev %d\n", int(next), int(reverse)));
QTextEdit *edit = getCurrentEditor();
if (edit == 0) {
// ??
return;
}
bool matchCase = matchCheck->isChecked();
int mspara, msindex, mepara, meindex;
edit->getSelection(&mspara, &msindex, &mepara, &meindex);
if (mspara == -1)
mspara = msindex = mepara = meindex = 0;
if (next) {
// We search again, starting from the current match
if (reverse) {
// when searching backwards, have to move back one char
if (msindex > 0)
msindex --;
else if (mspara > 0) {
mspara --;
msindex = edit->paragraphLength(mspara);
}
} else {
// Forward search: start from end of selection
mspara = mepara;
msindex = meindex;
LOGDEB1(("New para: %d index %d\n", mspara, msindex));
}
}
bool found = edit->find(searchTextLine->text(), matchCase, false,
!reverse, &mspara, &msindex);
if (!found && next && true) { // need a 'canwrap' test here
if (reverse) {
mspara = edit->paragraphs();
msindex = edit->paragraphLength(mspara);
} else {
mspara = msindex = 0;
}
found = edit->find(searchTextLine->text(), matchCase, false,
!reverse, &mspara, &msindex);
}
if (found) {
canBeep = true;
} else {
if (canBeep)
QApplication::beep();
canBeep = false;
}
}
void Preview::nextPressed()
{
doSearch(true, false);
}
void Preview::prevPressed()
{
doSearch(true, true);
}
void Preview::currentChanged(QWidget * tw)
{
QObject *o = tw->child("pvEdit");
LOGDEB1(("Preview::currentChanged(). Edit %p\n", o));
if (o == 0) {
LOGERR(("Editor child not found\n"));
} else {
tw->installEventFilter(this);
o->installEventFilter(this);
((QWidget*)o)->setFocus();
}
}
void Preview::closeCurrentTab()
{
if (pvTab->count() > 1) {
QWidget *tw = pvTab->currentPage();
if (!tw)
return;
pvTab->removePage(tw);
// Have to remove from tab data list
if (tabData == 0)
return;
for (list<TabData>::iterator it = TABDATA->begin();
it != TABDATA->end(); it++) {
if (it->w == tw) {
TABDATA->erase(it);
return;
}
}
} else {
close();
}
}
QTextEdit * Preview::addEditorTab()
{
QWidget *anon = new QWidget((QWidget *)pvTab);
QVBoxLayout *anonLayout = new QVBoxLayout(anon, 1, 1, "anonLayout");
QTextEdit *editor = new QTextEdit(anon, "pvEdit");
editor->setReadOnly( TRUE );
editor->setUndoRedoEnabled( FALSE );
anonLayout->addWidget(editor);
pvTab->addTab(anon, "Tab");
pvTab->showPage(anon);
if (tabData)
TABDATA->push_back(TabData(anon));
return editor;
}
void Preview::setCurTabProps(const string &fn, const Rcl::Doc &doc)
{
QString title = QString::fromUtf8(doc.title.c_str(),
doc.title.length());
if (title.length() > 20) {
title = title.left(10) + "..." + title.right(10);
}
QWidget *w = pvTab->currentPage();
pvTab->changeTab(w, title);
char datebuf[100];
datebuf[0] = 0;
if (!doc.fmtime.empty() || !doc.dmtime.empty()) {
time_t mtime = doc.dmtime.empty() ?
atol(doc.fmtime.c_str()) : atol(doc.dmtime.c_str());
struct tm *tm = localtime(&mtime);
strftime(datebuf, 99, "%Y-%m-%d %H:%M:%S", tm);
}
string tiptxt = doc.url + string("\n");
tiptxt += doc.mimetype + " " + string(datebuf) + "\n";
if (!doc.title.empty())
tiptxt += doc.title + "\n";
pvTab->setTabToolTip(w,QString::fromUtf8(tiptxt.c_str(), tiptxt.length()));
for (list<TabData>::iterator it = TABDATA->begin();
it != TABDATA->end(); it++) {
if (it->w == w) {
it->fn = fn;
it->ipath = doc.ipath;
break;
}
}
}
bool Preview::makeDocCurrent(const string &fn, const Rcl::Doc &doc)
{
LOGDEB(("Preview::makeFileCurrent: %s\n", fn.c_str()));
for (list<TabData>::iterator it = TABDATA->begin();
it != TABDATA->end(); it++) {
LOGDEB2(("Preview::makeFileCurrent: compare to w %p, file %s\n",
it->w, it->fn.c_str()));
if (!it->fn.compare(fn) && !it->ipath.compare(doc.ipath)) {
pvTab->showPage(it->w);
return true;
}
}
return false;
}
/*
Code for loading a file into an editor window. The operations that
we call have no provision to indicate progression, and it would be
complicated or impossible to modify them to do so (Ie: for external
format converters).
We implement a complicated and ugly mechanism based on threads to indicate
to the user that the app is doing things: lengthy operations are done in
threads and we update a progress indicator while they proceed (but we have
no estimate of their total duration).
An auxiliary thread object is used for short waits. Another option would be
to use signals/slots and return to the event-loop instead, but this would
be even more complicated, and we probably don't want the user to click on
things during this time anyway.
No cancel button is implemented, but this could conceivably be done
*/
/* A thread to to the file reading / format conversion */
class LoadThread : public QThread {
int *statusp;
Rcl::Doc *out;
string filename;
string ipath;
string *mtype;
public:
LoadThread(int *stp, Rcl::Doc *odoc, string fn, string ip, string *mt)
: statusp(stp), out(odoc), filename(fn), ipath(ip), mtype(mt)
{}
virtual void run()
{
DebugLog::getdbl()->setloglevel(DEBDEB1);
FileInterner interner(filename, rclconfig, tmpdir, mtype);
if (interner.internfile(*out, ipath) != FileInterner::FIDone) {
*statusp = -1;
} else {
*statusp = 0;
}
}
};
/* A thread to convert to rich text (mark search terms) */
class ToRichThread : public QThread {
string &in;
list<string> &terms;
list<pair<int, int> > &termoffsets;
QString &out;
public:
ToRichThread(string &i, list<string> &trms,
list<pair<int, int> > &toffs, QString &o)
: in(i), terms(trms), termoffsets(toffs), out(o)
{}
virtual void run()
{
DebugLog::getdbl()->setloglevel(DEBDEB1);
string rich = plaintorich(in, terms, termoffsets);
out = QString::fromUtf8(rich.c_str(), rich.length());
}
};
/* A thread to implement short waiting. There must be a better way ! */
class WaiterThread : public QThread {
int ms;
public:
WaiterThread(int millis) : ms(millis) {}
virtual void run() {
msleep(ms);
}
};
void Preview::loadFileInCurrentTab(string fn, size_t sz, const Rcl::Doc &idoc)
{
Rcl::Doc doc = idoc;
if (doc.title.empty())
doc.title = path_getsimple(doc.url);
setCurTabProps(fn, doc);
char csz[20];
sprintf(csz, "%lu", (unsigned long)sz);
QString msg = QString("Loading: %1 (size %2 bytes)")
.arg(fn)
.arg(csz);
// Create progress dialog and aux objects
const int nsteps = 10;
QProgressDialog progress( msg, "", nsteps, this, "Loading", TRUE );
QPushButton *pb = new QPushButton("", this);
pb->setEnabled(false);
progress.setCancelButton(pb);
progress.setMinimumDuration(1000);
WaiterThread waiter(100);
// Load and convert file
Rcl::Doc fdoc;
int status = 1;
LoadThread lthr(&status, &fdoc, fn, doc.ipath, &doc.mimetype);
lthr.start();
int i;
for (i = 1;;i++) {
waiter.start();
waiter.wait();
if (lthr.finished())
break;
progress.setProgress(i , i <= nsteps-1 ? nsteps : i+1);
qApp->processEvents();
if (i >= 5)
sleep(1);
}
if (status != 0) {
QMessageBox::warning(0, "Recoll",
tr("Can't turn doc into internal rep for ") +
doc.mimetype.c_str());
return;
}
// Highlight search terms:
progress.setLabelText(tr("Creating preview text"));
list<string> terms;
rcldb->getQueryTerms(terms);
list<pair<int, int> > termoffsets;
QString str;
ToRichThread rthr(fdoc.text, terms, termoffsets, str);
rthr.start();
for (;;i++) {
waiter.start();
waiter.wait();
if (rthr.finished())
break;
progress.setProgress(i , i <= nsteps-1 ? nsteps : i+1);
qApp->processEvents();
if (i >= 5)
sleep(1);
}
LOGDEB(("Plaintorich done\n"));
// Load into editor
QTextEdit *editor = getCurrentEditor();
QStyleSheetItem *item =
new QStyleSheetItem(editor->styleSheet(), "termtag" );
item->setColor("blue");
item->setFontWeight(QFont::Bold);
progress.setLabelText(tr("Loading preview text into editor"));
qApp->processEvents();
editor->setText(str);
int para = 0, index = 1;
if (!termoffsets.empty()) {
index = (termoffsets.begin())->first;
LOGDEB(("Set cursor position: para %d, character index %d\n",
para,index));
editor->setCursorPosition(0, index);
}
editor->ensureCursorVisible();
editor->getCursorPosition(&para, &index);
LOGDEB(("PREVIEW len %d paragraphs: %d. Cpos: %d %d\n",
editor->length(), editor->paragraphs(), para, index));
}