File: editreplacewidget.cpp

package info (click to toggle)
ktikz 0.11~git20161122-1
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 5,248 kB
  • ctags: 1,225
  • sloc: cpp: 10,302; xml: 701; sh: 150; makefile: 29
file content (154 lines) | stat: -rw-r--r-- 5,220 bytes parent folder | download | duplicates (5)
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
/***************************************************************************
 *   Copyright (C) 2008, 2011, 2014 by Glad Deschrijver                    *
 *     <glad.deschrijver@gmail.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.                                   *
 *                                                                         *
 *   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, see <http://www.gnu.org/licenses/>.  *
 ***************************************************************************/

#include "editreplacewidget.h"

#if QT_VERSION >= QT_VERSION_CHECK(5, 2, 0)
#include <QtWidgets/QLineEdit>
#endif

#ifdef KTIKZ_USE_KDE
#include <KCompletion>
#endif
#include <QtGui/QKeyEvent>

#include "../common/utils/icon.h"
#include "../common/utils/lineedit.h"

ReplaceWidget::ReplaceWidget(QWidget *parent) : QWidget(parent)
{
	ui.setupUi(this);
#if QT_VERSION >= QT_VERSION_CHECK(5, 2, 0)
	ui.comboBoxFind->setEditable(true);
	ui.comboBoxReplace->setEditable(true);
	ui.comboBoxFind->lineEdit()->setClearButtonEnabled(true);
	ui.comboBoxReplace->lineEdit()->setClearButtonEnabled(true);
#else
	ui.comboBoxFind->setLineEdit(new LineEdit(this));
	ui.comboBoxReplace->setLineEdit(new LineEdit(this));
#endif
	ui.pushButtonClose->setIcon(Icon(QLatin1String("dialog-cancel")));
	ui.pushButtonBackward->setIcon(Icon(QLatin1String("go-up")));
	ui.pushButtonForward->setIcon(Icon(QLatin1String("go-down")));
#ifdef KTIKZ_USE_KDE
	// activate completion
	KCompletion *completion = ui.comboBoxFind->completionObject();
	connect(ui.comboBoxFind, SIGNAL(returnPressed(QString)), completion, SLOT(addItem(QString)));
	completion = ui.comboBoxReplace->completionObject();
	connect(ui.comboBoxReplace, SIGNAL(returnPressed(QString)), completion, SLOT(addItem(QString)));
#endif

	setFocusProxy(ui.comboBoxFind);

	connect(ui.pushButtonBackward, SIGNAL(clicked()), this, SLOT(setBackward()));
	connect(ui.pushButtonForward, SIGNAL(clicked()), this, SLOT(setForward()));
	connect(ui.pushButtonFind, SIGNAL(clicked()), this, SLOT(doFind()));
	connect(ui.pushButtonReplace, SIGNAL(clicked()), this, SLOT(doReplace()));
	connect(ui.pushButtonClose, SIGNAL(clicked()), this, SLOT(hide()));
}

ReplaceWidget::~ReplaceWidget()
{
}

void ReplaceWidget::setBackward()
{
	ui.pushButtonBackward->setChecked(true);
	ui.pushButtonForward->setChecked(false);
}

void ReplaceWidget::setForward()
{
	ui.pushButtonBackward->setChecked(false);
	ui.pushButtonForward->setChecked(true);
}

void ReplaceWidget::setForward(bool forward)
{
	if (forward)
		setForward();
	else
		setBackward();
}

void ReplaceWidget::hide()
{
	setVisible(false);
	Q_EMIT focusEditor();
}

void ReplaceWidget::doFind()
{
	const QString currentText = ui.comboBoxFind->currentText();
	if (currentText.isEmpty())
		return;
	if (ui.comboBoxFind->findText(currentText) < 0)
		ui.comboBoxFind->addItem(currentText);

	QTextDocument::FindFlags flags = 0;
	if (ui.checkBoxCaseSensitive->isChecked())
		flags |= QTextDocument::FindCaseSensitively;
	if (ui.checkBoxWholeWords->isChecked())
		flags |= QTextDocument::FindWholeWords;
	if (!ui.pushButtonForward->isChecked())
		flags |= QTextDocument::FindBackward;
	Q_EMIT search(currentText, flags);
}

void ReplaceWidget::doReplace()
{
	const QString currentText = ui.comboBoxFind->currentText();
	if (currentText.isEmpty())
		return;
	const QString replacementText = ui.comboBoxReplace->currentText();
	if (ui.comboBoxFind->findText(currentText) < 0)
		ui.comboBoxFind->addItem(currentText);
	if (ui.comboBoxReplace->findText(replacementText) < 0)
		ui.comboBoxReplace->addItem(replacementText);

	QTextDocument::FindFlags flags = 0;
	if (ui.checkBoxCaseSensitive->isChecked())
		flags |= QTextDocument::FindCaseSensitively;
	if (ui.checkBoxWholeWords->isChecked())
		flags |= QTextDocument::FindWholeWords;
	if (!ui.pushButtonForward->isChecked())
		flags |= QTextDocument::FindBackward;
	Q_EMIT replace(currentText, replacementText, flags);
}

void ReplaceWidget::setText(const QString &text)
{
	ui.comboBoxFind->lineEdit()->setText(text);
	ui.comboBoxFind->setFocus();
	ui.comboBoxFind->lineEdit()->selectAll();
}

void ReplaceWidget::showEvent(QShowEvent *event)
{
	ui.comboBoxFind->setFocus();
	QWidget::showEvent(event);
}

void ReplaceWidget::keyPressEvent(QKeyEvent *event)
{
	if (event->key() == Qt::Key_Escape)
		hide();
	else if (event->key() == Qt::Key_Return)
		doFind();
	QWidget::keyPressEvent(event);
}