1 /*
2 For general Scribus (>=1.3.2) copyright and licensing information please refer
3 to the COPYING file provided with the program. Following this notice may exist
4 a copyright and/or license notice that predates the release of Scribus 1.3.2
5 for which a new license (GPL+exception) is in place.
6 */
7 #include "tffilter.h"
8 
9 #include <QBoxLayout>
10 #include <QCheckBox>
11 #include <QComboBox>
12 #include <QFrame>
13 #include <QGridLayout>
14 #include <QHBoxLayout>
15 #include <QLabel>
16 #include <QPixmap>
17 #include <QPushButton>
18 #include <QToolTip>
19 #include <QVBoxLayout>
20 
21 #include "scribusapi.h"
22 #include "scribuscore.h"
23 #include "scribusdoc.h"
24 #include "prefsmanager.h"
25 #include "prefsfile.h"
26 #include "iconmanager.h"
27 #include "util.h"
28 
tfFilter(QWidget * parent,const char * name,int action,const QString & regExp,const QString & replace,const QString & pstyleName,int less,int more,int style,bool match,bool enabled,bool regexp)29 tfFilter::tfFilter(QWidget *parent, const char *name,
30 				   int action, const QString& regExp, const QString& replace, const QString& pstyleName,
31                    int less, int more, int style, bool match, bool enabled, bool regexp)
32                    : QWidget(parent)
33 {
34 	createWidget();
35 	setObjectName(name);
36 	firstChanged(action);
37 	currentAction = action;
38 	firstCombo->setCurrentIndex(action);
39 	if (action == APPLY)
40 	{
41 		setCurrentComboItem(thirdCombo, pstyleName);
42 		fourthChanged(style);
43 		fourthCombo->setCurrentIndex(style);
44 		if (style == STARTS_WITH)
45 		{
46 			fifthCombo->setEditText(regExp);
47 			fifthRegexpCheck->setChecked(regexp);
48 			if (match)
49 				sixthCombo->setCurrentIndex(0);
50 			else
51 				sixthCombo->setCurrentIndex(1);
52 		}
53 		else if (style == LESS_THAN)
54 		{
55 			if (less > 0)
56 				fifthCombo->setEditText(QString("%1").arg(less));
57 		}
58 		else if (style == MORE_THAN)
59 		{
60 			if (more > 0)
61 				fifthCombo->setEditText(QString("%1").arg(more));
62 		}
63 	}
64 	else if (action == REMOVE)
65 	{
66 		secondCombo->setEditText(regExp);
67 		secondRegexpCheck->setChecked(regexp);
68 	}
69 	else if (action == REPLACE)
70 	{
71 		secondCombo->setEditText(regExp);
72 		secondRegexpCheck->setChecked(regexp);
73 		thirdCombo->setEditText(replace);
74 	}
75 	enableCheck->setChecked(enabled);
76 	enableToggled(enabled);
77 }
78 
tfFilter(QWidget * parent,const char * name)79 tfFilter::tfFilter(QWidget *parent, const char *name) : QWidget(parent)
80 {
81 	createWidget();
82 	setObjectName(name);
83 }
84 
createWidget()85 void tfFilter::createWidget()
86 {
87 	firstCombo = nullptr;
88 	firstLabel = nullptr;
89 	secondCombo = nullptr;
90 	secondLabel = nullptr;
91 	thirdCombo = nullptr;
92 	thirdLabel = nullptr;
93 	fourthCombo = nullptr;
94 	fourthLabel = nullptr;
95 	fifthCombo = nullptr;
96 	fifthLabel = nullptr;
97 	sixthCombo = nullptr;
98 	secondRegexpCheck = nullptr;
99 // 	thirdRegexpCheck = nullptr;
100 	fifthRegexpCheck = nullptr;
101 
102 	prefs = PrefsManager::instance().prefsFile->getPluginContext("TextFilter");
103 	history = prefs->getTable("history");
104 
105 	QHBoxLayout *layout = new QHBoxLayout(this);
106 	layout->setContentsMargins(0, 0, 0, 0);
107 	layout->setSpacing(6);
108 
109 	enableCheck = new QCheckBox(this);
110 	enableCheck->setMinimumSize(QSize(25,25));
111 	enableCheck->setChecked(true);
112 	enableCheck->setToolTip( tr("Disable or enable this filter row"));
113 	layout->addWidget(enableCheck, 0, Qt::AlignTop);
114 
115 	actionFrame = new QFrame(this);
116 	layout->addWidget(actionFrame, 0, Qt::AlignTop);
117 
118 	QBoxLayout* layout2 = new QVBoxLayout(actionFrame);
119 	layout2->setContentsMargins(0, 0, 0, 0);
120 	layout2->setSpacing(6);
121 	alayout = new QHBoxLayout();
122 	alayout->setContentsMargins(0, 0, 0, 0);
123 	alayout->setSpacing(6);
124 	layout2->addLayout(alayout);
125 	layout2->addSpacing(4);
126 	blayout = new QHBoxLayout();
127 	blayout->setSpacing(6);
128 	blayout->setContentsMargins(0, 0, 0, 0);
129 	layout2->addLayout(blayout);
130 
131 	currentAction = REMOVE;
132 	getFirstCombo();
133 
134 // 	layout->addStretch(10);
135 
136 	layout->addSpacing(20);
137 	removeButton = new QPushButton(IconManager::instance().loadIcon("22/list-remove.png"), nullptr, this);
138 	removeButton->setToolTip( tr("Remove this filter row"));
139 	removeButton->setMaximumSize(QSize(25,25));
140 	removeButton->setMinimumSize(QSize(25,25));
141 	layout->addWidget(removeButton, 0, Qt::AlignTop);
142 	addButton = new QPushButton(IconManager::instance().loadIcon("22/list-add.png"), nullptr, this);
143 	addButton->setToolTip( tr("Add a new filter row"));
144 	addButton->setMaximumSize(QSize(25,25));
145 	addButton->setMinimumSize(QSize(25,25));
146 	layout->addWidget(addButton, 0, Qt::AlignTop);
147 
148 	connect(enableCheck, SIGNAL(toggled(bool)), this, SLOT(enableToggled(bool)));
149 	connect(addButton, SIGNAL(clicked()), this, SLOT(addClick()));
150 	connect(removeButton, SIGNAL(clicked()), this, SLOT(removeClick()));
151 }
152 
setRemovable(bool b)153 void tfFilter::setRemovable(bool b)
154 {
155 	removeButton->setEnabled(b);
156 }
157 
enableToggled(bool on)158 void tfFilter::enableToggled(bool on)
159 {
160 	actionFrame->setEnabled(on);
161 }
162 
addClick()163 void tfFilter::addClick()
164 {
165 	emit addClicked(this);
166 }
167 
removeClick()168 void tfFilter::removeClick()
169 {
170 	emit removeClicked(this);
171 }
172 
firstChanged(int index)173 void tfFilter::firstChanged(int index)
174 {
175 	currentAction = index;
176 	getSecondCombo();
177 	emit actionChanged(this);
178 }
179 
secondChanged(int)180 void tfFilter::secondChanged(int)
181 {
182 
183 }
184 
thirdChanged(int)185 void tfFilter::thirdChanged(int)
186 {
187 
188 }
189 
fourthChanged(int index)190 void tfFilter::fourthChanged(int index)
191 {
192 	switch (currentAction)
193 	{
194 		case APPLY:
195 			thirdLabel->setText( tr("to"));
196 			thirdLabel->show();
197 // 			thirdRegexpCheck->hide();
198 			switch (index)
199 			{
200 				case ALL_PARAGRAPHS:
201 					fourthLabel->hide();
202 					fifthCombo->hide();
203 					fifthLabel->hide();
204 					sixthCombo->hide();
205 					fifthRegexpCheck->hide();
206 					break;
207 				case STARTS_WITH:
208 					fourthLabel->hide();
209 					fifthCombo->clear();
210 					fifthCombo->setEditable(true);
211 					fifthCombo->show();
212 					fifthRegexpCheck->show();
213 					fifthLabel->setText( tr("and"));
214 					fifthLabel->show();
215 					sixthCombo->clear();
216 					sixthCombo->setEditable(false);
217 					sixthCombo->addItem( tr("remove match"));
218 					sixthCombo->addItem( tr("do not remove match"));
219 					sixthCombo->show();
220 					break;
221 				case LESS_THAN:
222 				case MORE_THAN:
223 					fourthLabel->hide();
224 					fifthCombo->clear();
225 					fifthCombo->setEditable(true);
226 					fifthCombo->show();
227 					fifthRegexpCheck->hide();
228 					fifthLabel->setText( tr("words"));
229 					fifthLabel->show();
230 					sixthCombo->hide();
231 					break;
232 			}
233 			break;
234 	}
235 }
236 
fifthChanged(int)237 void tfFilter::fifthChanged(int)
238 {
239 
240 }
241 
sixthChanged(int)242 void tfFilter::sixthChanged(int)
243 {
244 
245 }
246 
getFirstCombo()247 void tfFilter::getFirstCombo()
248 {
249 	resetBRow();
250 	if (!firstCombo)
251 	{
252 		firstCombo = new QComboBox(actionFrame);
253 		firstCombo->addItem("");
254 		firstCombo->show();
255 		alayout->addWidget(firstCombo, -1);
256 		alayout->setSpacing(5);
257 		connect(firstCombo, SIGNAL(activated(int)), this, SLOT(firstChanged(int)));
258 	}
259 	if (!firstLabel)
260 	{
261 		firstLabel = new QLabel(actionFrame);
262 		alayout->addWidget(firstLabel, -1);
263 		firstLabel->hide();
264 	}
265 	firstCombo->clear();
266 	firstCombo->setMinimumSize(QSize(120, 0));
267 	firstCombo->addItem( tr("Remove"));
268 	firstCombo->addItem( tr("Replace"));
269 	firstCombo->addItem( tr("Apply"));
270 	firstCombo->show();
271 	getSecondCombo();
272 }
273 
getSecondCombo()274 void tfFilter::getSecondCombo()
275 {
276 	resetBRow();
277 	if (!secondCombo)
278 	{
279 		secondCombo = new QComboBox(actionFrame);
280 		secondCombo->addItem("");
281 		secondCombo->show();
282 		alayout->addWidget(secondCombo, 8);
283 		connect(secondCombo, SIGNAL(activated(int)), this, SLOT(secondChanged(int)));
284 	}
285 	if (!secondRegexpCheck)
286 	{
287 		secondRegexpCheck = new QCheckBox(actionFrame);
288 		secondRegexpCheck->setToolTip( tr("Value at the left is a regular expression"));
289 		secondRegexpCheck->show();
290 		alayout->addWidget(secondRegexpCheck, -1);
291 	}
292 	if (!secondLabel)
293 	{
294 		secondLabel = new QLabel(actionFrame);
295 		secondLabel->hide();
296 		alayout->addWidget(secondLabel, -1);
297 	}
298 	switch (currentAction)
299 	{
300 		case REPLACE:
301 			firstLabel->hide();
302 			secondLabel->setText( tr("with"));
303 			secondLabel->show();
304 			secondCombo->setEditable(true);
305 			secondCombo->clear();
306 			secondCombo->show();
307 			secondRegexpCheck->show();
308 			break;
309 		case APPLY:
310 			firstLabel->hide();
311 			secondLabel->hide();
312 			secondCombo->setEditable(false);
313 			secondCombo->clear();
314 			secondCombo->addItem( tr("paragraph style"));
315 			secondRegexpCheck->hide();
316 			break;
317 		case REMOVE:
318 			firstLabel->setText( tr("all instances of"));
319 			firstLabel->show();
320 			secondCombo->clear();
321 			secondCombo->setEditable(true);
322 			secondCombo->show();
323 			secondLabel->hide();
324 			secondRegexpCheck->show();
325 			break;
326 	}
327 	getThirdCombo(secondCombo->currentIndex());
328 }
329 
getThirdCombo(int)330 void tfFilter::getThirdCombo(int)
331 {
332 	if (!thirdCombo)
333 	{
334 		thirdCombo = new QComboBox(actionFrame);
335 		thirdCombo->addItem("");
336 		thirdCombo->hide();
337 		alayout->addWidget(thirdCombo, 8);
338 		connect(thirdCombo, SIGNAL(activated(int)), this, SLOT(thirdChanged(int)));
339 	}
340 // 	if (!thirdRegexpCheck)
341 // 	{
342 // 		thirdRegexpCheck = new QCheckBox(actionFrame, "secondRegexpCheck");
343 // 		thirdRegexpCheck->setToolTip( tr("Value is treated as a regular expression"));
344 // 		thirdRegexpCheck->hide();
345 // 		alayout->addWidget(thirdRegexpCheck, -1);
346 // 	}
347 	if (!thirdLabel)
348 	{
349 		thirdLabel = new QLabel(actionFrame);
350 		thirdLabel->hide();
351 		blayout->addWidget(thirdLabel, -1);
352 		blayout->addSpacing(5);
353 	}
354 	switch (currentAction)
355 	{
356 		case REPLACE:
357 			thirdCombo->clear();
358 			thirdCombo->setEditable(true);
359 			thirdCombo->show();
360 // 			thirdRegexpCheck->show();
361 			break;
362 		case APPLY:
363 			thirdCombo->clear();
364 			getParagraphStyles();
365 			thirdCombo->setEditable(true);
366 			thirdCombo->show();
367 // 			thirdRegexpCheck->hide();
368 			getFourthCombo();
369 			break;
370 		case REMOVE:
371 			thirdCombo->hide();
372 // 			thirdRegexpCheck->hide();
373 			break;
374 	}
375 }
376 
getFourthCombo()377 void tfFilter::getFourthCombo()
378 {
379 	if (!fourthCombo)
380 	{
381 		fourthCombo = new QComboBox(actionFrame);
382 		fourthCombo->addItem("");
383 		fourthCombo->hide();
384 		blayout->addWidget(fourthCombo, 8);
385 		connect(fourthCombo, SIGNAL(activated(int)), this, SLOT(fourthChanged(int)));
386 	}
387 	if (!fourthLabel)
388 	{
389 		fourthLabel = new QLabel(actionFrame);
390 		fourthLabel->hide();
391 		blayout->addWidget(fourthLabel, -1);
392 		blayout->addSpacing(5);
393 	}
394 	switch (currentAction)
395 	{
396 		case APPLY:
397 			thirdLabel->setText( tr("to"));
398 			thirdLabel->show();
399 			fourthCombo->clear();
400 			fourthCombo->addItem( tr("all paragraphs"));
401 			fourthCombo->addItem( tr("paragraphs starting with"));
402 			fourthCombo->addItem( tr("paragraphs with less than"));
403 			fourthCombo->addItem( tr("paragraphs with more than"));
404 			fourthCombo->setEditable(false);
405 			fourthCombo->show();
406 			fourthLabel->hide();
407 			getFifthCombo();
408 			break;
409 	}
410 }
411 
getFifthCombo()412 void tfFilter::getFifthCombo()
413 {
414 	if (!fifthCombo)
415 	{
416 		fifthCombo = new QComboBox(actionFrame);
417 		fifthCombo->addItem("");
418 		fifthCombo->hide();
419 		blayout->addWidget(fifthCombo, 8);
420 		blayout->addSpacing(5);
421 		connect(fifthCombo, SIGNAL(activated(int)), this, SLOT(fifthChanged(int)));
422 	}
423 	if (!fifthRegexpCheck)
424 	{
425 		fifthRegexpCheck = new QCheckBox(actionFrame);
426 		fifthRegexpCheck->setToolTip( tr("Value at the left is a regular expression"));
427 		fifthRegexpCheck->hide();
428 		blayout->addWidget(fifthRegexpCheck, -1);
429 		blayout->addSpacing(5);
430 	}
431 	if (!fifthLabel)
432 	{
433 		fifthLabel = new QLabel(actionFrame);
434 		fifthLabel->hide();
435 		blayout->addWidget(fifthLabel, -1);
436 		blayout->addSpacing(5);
437 	}
438 	getSixthCombo();
439 }
440 
getSixthCombo()441 void tfFilter::getSixthCombo()
442 {
443 	if (!sixthCombo)
444 	{
445 		sixthCombo = new QComboBox(actionFrame);
446 		sixthCombo->addItem("");
447 		sixthCombo->hide();
448 		blayout->addWidget(sixthCombo, 7);
449 		connect(sixthCombo, SIGNAL(activated(int)), this, SLOT(sixthChanged(int)));
450 	}
451 }
452 
resetBRow()453 void tfFilter::resetBRow()
454 {
455 	if (thirdLabel)
456 	{
457 		thirdLabel->hide();
458 		thirdLabel->setText("");
459 	}
460 	if (fourthCombo)
461 	{
462 		fourthCombo->hide();
463 		fourthCombo->clear();
464 	}
465 	if (fourthLabel)
466 	{
467 		fourthLabel->hide();
468 		fourthLabel->setText("");
469 	}
470 	if (fifthCombo)
471 	{
472 		fifthCombo->hide();
473 		fifthCombo->clear();
474 	}
475 	if (fifthRegexpCheck)
476 	{
477 		fifthRegexpCheck->hide();
478 	}
479 	if (fifthLabel)
480 	{
481 		fifthLabel->hide();
482 		fifthLabel->setText("");
483 	}
484 	if (sixthCombo)
485 	{
486 		sixthCombo->hide();
487 		sixthCombo->clear();
488 	}
489 }
490 
getParagraphStyles()491 void tfFilter::getParagraphStyles()
492 {
493 	thirdCombo->addItem("");
494 	for (int i = 0; i < ScCore->primaryMainWindow()->doc->paragraphStyles().count(); ++i)
495 	{
496 		thirdCombo->addItem(ScCore->primaryMainWindow()->doc->paragraphStyles()[i].name());
497 	}
498 }
499 
getAction()500 int tfFilter::getAction()
501 {
502 	return currentAction;
503 }
504 
regExp()505 QString tfFilter::regExp()
506 {
507 	switch (currentAction)
508 	{
509 		case REMOVE:
510 		case REPLACE:
511 			if (!secondCombo)
512 				return "";
513 			return QString(secondCombo->currentText());
514 			break;
515 		case APPLY:
516 			if (!fifthCombo)
517 				return "";
518 			return QString(fifthCombo->currentText());
519 			break;
520 		default:
521 			return "";
522 			break;
523 	}
524 	return "";
525 }
526 
replaceWith()527 QString tfFilter::replaceWith()
528 {
529 	if (!thirdCombo)
530 		return "";
531 	return QString(thirdCombo->currentText());
532 }
533 
getPStyleName()534 QString tfFilter::getPStyleName()
535 {
536 	if (!thirdCombo)
537 		return "";
538 	return QString(thirdCombo->currentText());
539 }
540 
getLessThan()541 int tfFilter::getLessThan()
542 {
543 	if (!fifthCombo)
544 		return -1;
545 	bool ok = false;
546 	QString text = QString(fifthCombo->currentText());
547 	int i = text.toInt(&ok);
548 	if (ok)
549 		return i;
550 	return -1;
551 }
552 
getStyle()553 int tfFilter::getStyle()
554 {
555 	if (!fourthCombo)
556 		return 0;
557 	return fourthCombo->currentIndex();
558 }
559 
removeMatch()560 bool tfFilter::removeMatch()
561 {
562 	if (!sixthCombo)
563 		return false;
564 	return sixthCombo->currentIndex() == 0;
565 }
566 
getMoreThan()567 int tfFilter::getMoreThan()
568 {
569 	return getLessThan();
570 }
571 
isEnabled()572 bool tfFilter::isEnabled()
573 {
574 	return enableCheck->isChecked();
575 }
576 
isRegExp()577 bool tfFilter::isRegExp()
578 {
579 	bool checked = false;
580 	if ((currentAction == REPLACE) || (currentAction == REMOVE))
581 		checked = secondRegexpCheck->isChecked();
582 	else if (currentAction == APPLY)
583 		checked = fifthRegexpCheck->isChecked();
584 	return checked;
585 }
586 
~tfFilter()587 tfFilter::~tfFilter()
588 {
589 
590 }
591