-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtreetree.cpp
521 lines (437 loc) · 11.2 KB
/
treetree.cpp
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
/* Treecle
*
* Copyright (c) Kartik Patel
* E-mail: [email protected]
* Download from: https://letapk.wordpress.com
*
* 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 "treecle.h"
void collapse (QTreeWidgetItem *t);
void expand (QTreeWidgetItem *t);
void srch_sub(QTreeWidgetItem *t, QString s);
QTreeWidgetItem *srch_cat (QTreeWidgetItem *t, QString s);
void MainWindow::tree_addbranch()
{
QTreeWidgetItem *b;
QString s;
b = new QTreeWidgetItem (tree);
tree->addTopLevelItem(b);
s.clear();
s.append(tr(""));
b->setText(0, s);
s.clear();
s.append(tr("New Category<p></p>Enter the category description here. The first line becomes the name in the tree on the left."));
b->setText(1, s);
cur_branch = b;
cur_leaf = b;
catflag = 1;
tree->setCurrentItem(b);
statustext->setText(tr("Added a new category"));
show_branch_data ();
leafview->triggerPageAction(QWebPage::SelectAll, false);
textSize();
fontFamily();
leafview->findText("");
fmodified = true;
}
void MainWindow::tree_addsubbranch()
{
QString s;
QTreeWidgetItem *b;
if (tree->topLevelItemCount() == 0) {
statustext->setText(tr("No category present. Add one first."));
return;
}
b = new QTreeWidgetItem ();
s.clear();
s.append(tr(""));
b->setText(0, s);
s.clear();
s.append(tr("New Branch<p></p>Enter the branch data here. The first line becomes the name in the tree on the left."));
b->setText(1, s);
if (catflag == 0){//subbranch
cur_leaf->addChild(b);
}
else {//category
cur_branch->addChild(b);
catflag = 0;
}
cur_leaf = b;
tree->setCurrentItem(b);
statustext->setText(tr("Added a new branch"));
show_branch_data ();
leafview->triggerPageAction(QWebPage::SelectAll, false);
textSize();
fontFamily();
leafview->findText("");
fmodified = true;
}
void MainWindow::tree_delbranch()
{
int i, j, childcount;
QTreeWidgetItem *b;
if (tree->topLevelItemCount() == 0) {
statustext->setText(tr("The tree is empty"));
return;
}
childcount = cur_leaf->childCount();
if (childcount > 0) {
QMessageBox msgBox;
msgBox.setText(tr("The branch contains a sub-branch. Delete it first.\n""Click OK to continue"));
msgBox.exec();
return;
}
if (catflag == 0){//subbranch
b = cur_leaf->parent();
cur_leaf->setText(0, "");
cur_leaf->setText(1, "");
b->removeChild(cur_leaf);
statustext->setText(tr("Deleted sub-branch"));
set_branch(b);
fmodified = true;
return;
}
else {//category
i = tree->indexOfTopLevelItem(cur_branch);
cur_branch->setText(0, "");
cur_branch->setText(1, "");
if (i > 0) {
cur_branch = tree->topLevelItem(i-1);
}
else {
cur_branch = tree->topLevelItem(1);
}
tree->takeTopLevelItem(i);
statustext->setText(tr("Deleted category"));
}
j = tree->topLevelItemCount();
if (j > 0) {
cur_leaf = cur_branch;
set_branch(cur_branch);
}
else {
statustext->setText(tr("The tree is empty"));
leafview->setHtml("<p></p>");
}
catflag = 1;
fmodified = true;
}
void MainWindow::tree_cutbranch()
{
if (tree->topLevelItemCount() == 0) {
statustext->setText(tr("The tree is empty"));
return;
}
tree_copybranch();
tree_delbranch_after_copy();
fmodified = true;
}
void MainWindow::tree_delbranch_after_copy()
{
int i;
if (catflag == 0){//subbranch
cur_branch = cur_leaf->parent();
cur_leaf->setText(0, "");
cur_leaf->setText(1, "");
cur_branch->removeChild(cur_leaf);
statustext->setText(tr("Deleted sub-branch"));
}
else {//category
i = tree->indexOfTopLevelItem(cur_branch);
cur_branch->setText(0, "");
cur_branch->setText(1, "");
if (i > 0) {
cur_branch = tree->topLevelItem(i-1);
}
else {
cur_branch = tree->topLevelItem(1);
}
tree->takeTopLevelItem(i);
statustext->setText(tr("Deleted category"));
}
if (tree->topLevelItemCount() > 0) {
cur_leaf = cur_branch;
set_branch(cur_branch);
show_branch_data ();
}
else {
statustext->setText(tr("The tree is empty"));
leafview->setHtml("<p></p>");
}
catflag = 1;
fmodified = true;
}
QTreeWidgetItem *copy_branch;
void MainWindow::tree_copybranch()
{
if (tree->topLevelItemCount() == 0) {
statustext->setText(tr("The tree is empty"));
return;
}
copy_branch = new QTreeWidgetItem ();
if (catflag == 0){//subbranch
copy_branch = cur_leaf->clone();
statustext->setText(tr("Copied sub-branch"));
}
else {//category
copy_branch = cur_branch->clone();
statustext->setText(tr("Copied category"));
}
fmodified = true;
}
void MainWindow::tree_pastebranch()
{
if (tree->topLevelItemCount() == 0) {
statustext->setText(tr("The tree is empty"));
return;
}
cur_leaf->insertChild(0, copy_branch);
statustext->setText(tr("Copied branch"));
fmodified = true;
}
void MainWindow::set_branch (QTreeWidgetItem *b)
//item clicked
{
//QString s;
if (b->parent() != NULL) {//leaf item
//leaf and branch are different
cur_branch = b->parent();
cur_leaf = b;
catflag = 0;
}
else {//category item
//leaf and branch are the same
cur_branch = b;
cur_leaf = b;
catflag = 1;
}
show_branch_data ();
tree->setCurrentItem(cur_leaf);
cur_leaf->setSelected(true);
}
void MainWindow::show_branch_data()
{
QString s;
QByteArray d;
s.clear();
if (catflag == 0){//subbranch selected
s.append(cur_leaf->text(1));
modify_name (cur_leaf);
}
else {//category selected
s.append(cur_branch->text(1));
modify_name(cur_branch);
}
d = s.toUtf8();
leafview->setContent(d, "text/html");
leafview->page()->setContentEditable(true);
leafview->page()->setLinkDelegationPolicy(QWebPage::DelegateAllLinks);
fmodified = leafview->isModified();
}
void MainWindow::modify_name (QTreeWidgetItem *b)
{
QString s1, s;
QTextDocument doc;
int i;
s1 = b->text(1);
doc.setHtml(s1);
s = doc.toPlainText();
for (i = 0; i < s.length(); i++){
if (s[i] == QChar ('\n'))
s.chop (s.length() - i);
}
b->setText(0, s);
}
void MainWindow::get_data_from_leaf()
{
QString s;
//QTextEdit *textEdit = new QTextEdit;
if (tree->topLevelItemCount() == 0) {
statustext->setText(tr("The tree is empty"));
leafview->setHtml("<p></p>");
fmodified = leafview->isModified();
return;
}
s.clear();
s = leafview->page()->mainFrame()->toHtml();
if (catflag == 1) {
cur_branch->setText(1, s);
modify_name(cur_branch);
}
else {
cur_leaf->setText(1, s);
modify_name(cur_leaf);
}
fmodified = leafview->isModified();
}
void MainWindow::expand_tree()
{
int i, tlc;
QTreeWidgetItem *t;
tlc = tree->topLevelItemCount();
if (tlc == 0) {
statustext->setText(tr("Nothing to expand"));
return;
}
for (i = 0; i < tlc; i++) {
t = tree->topLevelItem(i);
expand (t);
}
}
void expand (QTreeWidgetItem *t)
{
int i, childcount;
t->setExpanded (true);
childcount = t->childCount();
if (childcount > 0) {
for (i = 0; i < childcount; i++) {
expand (t->child(i));
}
}
}
void MainWindow::collapse_tree()
{
int i, tlc;
QTreeWidgetItem *t;
tlc = tree->topLevelItemCount();
if (tlc == 0) {
statustext->setText(tr("Nothing to collapse"));
return;
}
for (i = 0; i < tlc; i++) {
t = tree->topLevelItem(i);
collapse(t);
}
}
void collapse (QTreeWidgetItem *t)
{
int i, childcount;
t->setExpanded (false);
childcount = t->childCount();
if (childcount > 0) {
for (i = 0; i < childcount; i++) {
collapse (t->child(i));
}
}
}
void MainWindow::sort_asc_tree()
{
int tlc;
tlc = tree->topLevelItemCount();
if (tlc == 0) {
statustext->setText(tr("Nothing to sort"));
return;
}
tree->sortItems(0, Qt::AscendingOrder);
}
void MainWindow::sort_desc_tree()
{
int tlc;
tlc = tree->topLevelItemCount();
if (tlc == 0) {
statustext->setText(tr("Nothing to sort"));
return;
}
tree->sortItems(0, Qt::DescendingOrder);
}
QList<QTreeWidgetItem *> srchlst;
static int srch_idx;
void MainWindow::tree_srch_nxt()
{
QTreeWidgetItem *cat;
QString s, s1;
int i, sz;
catcount = tree->topLevelItemCount();
if (catcount == 0) {
statustext->setText(tr("The tree is empty"));
return;
}
s = srchbox->text();
if (s.isEmpty() == true) {
statustext->setText(tr("Please enter text in search box"));
return;
}
foreach (cat, srchlst) {
cat->setSelected(false);
}
srchlst.clear();
//loop over categories
for (i = 0; i < catcount; i++){
//next top level category
cat = tree->topLevelItem(i);
srch_sub (cat, s);
}
sz = srchlst.size();
if (sz > 0) {
set_branch(srchlst[srch_idx]);
s1 = QString(tr("Displaying %1 of %2 occurences")).arg(srch_idx+1).arg(sz);
statustext->setText(s1);
}
else
statustext->setText("Text not found");
//printf ("size=%i, idx=%i\n", sz, srch_idx);
srch_idx++;
if (srch_idx >= sz)
srch_idx = 0;
}
void MainWindow::tree_srch_pre()
{
QTreeWidgetItem *cat;
QString s, s1;
int i, sz;
catcount = tree->topLevelItemCount();
if (catcount == 0) {
statustext->setText(tr("The tree is empty"));
return;
}
s = srchbox->text();
if (s.isEmpty() == true) {
statustext->setText(tr("Please enter text in search box"));
return;
}
foreach (cat, srchlst) {
cat->setSelected(false);
}
srchlst.clear();
//loop over categories
for (i = 0; i < catcount; i++){
//next top level category
cat = tree->topLevelItem(i);
srch_sub (cat, s);
}
sz = srchlst.size();
if (sz > 0) {
set_branch(srchlst[sz - srch_idx - 1]);
s1 = QString(tr("Displaying %1 of %2 occurences")).arg(sz - srch_idx).arg(sz);
statustext->setText(s1);
}
else
statustext->setText(tr("Text not found"));
//printf ("size=%i, idx=%i\n", sz, srch_idx);
srch_idx++;
if (srch_idx >= sz)
srch_idx = 0;
}
void srch_sub (QTreeWidgetItem *t, QString s)
{
QString s1;
int i, childcount;
s1 = t->text(1);
t->setSelected(false);
//check current branch
if (s1.contains(&s, Qt::CaseInsensitive) == true) {
srchlst.append(t);
}
//go down the sub-branches
childcount = t->childCount();
if (childcount > 0) {
for (i = 0; i < childcount; i++) {
srch_sub (t->child(i), s);
}
}
}