1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2018 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the QtSCriptTools module of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:LGPL$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU Lesser General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
21 | ** packaging of this file. Please review the following information to |
22 | ** ensure the GNU Lesser General Public License version 3 requirements |
23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
24 | ** |
25 | ** GNU General Public License Usage |
26 | ** Alternatively, this file may be used under the terms of the GNU |
27 | ** General Public License version 2.0 or (at your option) the GNU General |
28 | ** Public license version 3 or any later version approved by the KDE Free |
29 | ** Qt Foundation. The licenses are as published by the Free Software |
30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
31 | ** included in the packaging of this file. Please review the following |
32 | ** information to ensure the GNU General Public License requirements will |
33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
35 | ** |
36 | ** $QT_END_LICENSE$ |
37 | ** |
38 | ****************************************************************************/ |
39 | |
40 | #include "qscriptdebuggercodefinderwidget_p.h" |
41 | #include "qscriptdebuggercodefinderwidgetinterface_p_p.h" |
42 | |
43 | #include <QtWidgets/qboxlayout.h> |
44 | #include <QtWidgets/qlineedit.h> |
45 | #include <QtWidgets/qcheckbox.h> |
46 | #include <QtWidgets/qlabel.h> |
47 | #include <QtWidgets/qtoolbutton.h> |
48 | #include <QtGui/qevent.h> |
49 | #include <QtCore/qdebug.h> |
50 | |
51 | QT_BEGIN_NAMESPACE |
52 | |
53 | class QScriptDebuggerCodeFinderWidgetPrivate |
54 | : public QScriptDebuggerCodeFinderWidgetInterfacePrivate |
55 | { |
56 | Q_DECLARE_PUBLIC(QScriptDebuggerCodeFinderWidget) |
57 | public: |
58 | QScriptDebuggerCodeFinderWidgetPrivate(); |
59 | ~QScriptDebuggerCodeFinderWidgetPrivate(); |
60 | |
61 | // private slots |
62 | void _q_updateButtons(); |
63 | void _q_onTextChanged(const QString &); |
64 | void _q_next(); |
65 | void _q_previous(); |
66 | |
67 | int findOptions() const; |
68 | |
69 | QLineEdit *editFind; |
70 | QCheckBox *checkCase; |
71 | QLabel *labelWrapped; |
72 | QToolButton *toolNext; |
73 | QToolButton *toolClose; |
74 | QToolButton *toolPrevious; |
75 | QCheckBox *checkWholeWords; |
76 | }; |
77 | |
78 | QScriptDebuggerCodeFinderWidgetPrivate::QScriptDebuggerCodeFinderWidgetPrivate() |
79 | { |
80 | } |
81 | |
82 | QScriptDebuggerCodeFinderWidgetPrivate::~QScriptDebuggerCodeFinderWidgetPrivate() |
83 | { |
84 | } |
85 | |
86 | void QScriptDebuggerCodeFinderWidgetPrivate::_q_updateButtons() |
87 | { |
88 | if (editFind->text().isEmpty()) { |
89 | toolPrevious->setEnabled(false); |
90 | toolNext->setEnabled(false); |
91 | } else { |
92 | toolPrevious->setEnabled(true); |
93 | toolNext->setEnabled(true); |
94 | } |
95 | } |
96 | |
97 | int QScriptDebuggerCodeFinderWidgetPrivate::findOptions() const |
98 | { |
99 | int flags = 0; |
100 | if (checkCase->isChecked()) |
101 | flags |= QTextDocument::FindCaseSensitively; |
102 | if (checkWholeWords->isChecked()) |
103 | flags |= QTextDocument::FindWholeWords; |
104 | return flags; |
105 | } |
106 | |
107 | void QScriptDebuggerCodeFinderWidgetPrivate::_q_onTextChanged(const QString &text) |
108 | { |
109 | emit q_func()->findRequest(exp: text, options: findOptions() | 0x100); |
110 | } |
111 | |
112 | void QScriptDebuggerCodeFinderWidgetPrivate::_q_next() |
113 | { |
114 | emit q_func()->findRequest(exp: editFind->text(), options: findOptions()); |
115 | } |
116 | |
117 | void QScriptDebuggerCodeFinderWidgetPrivate::_q_previous() |
118 | { |
119 | emit q_func()->findRequest(exp: editFind->text(), options: findOptions() | QTextDocument::FindBackward); |
120 | } |
121 | |
122 | QScriptDebuggerCodeFinderWidget::QScriptDebuggerCodeFinderWidget(QWidget *parent) |
123 | : QScriptDebuggerCodeFinderWidgetInterface( |
124 | *new QScriptDebuggerCodeFinderWidgetPrivate, parent, {}) |
125 | { |
126 | Q_D(QScriptDebuggerCodeFinderWidget); |
127 | QString system = QLatin1String("win" ); |
128 | QHBoxLayout *hboxLayout = new QHBoxLayout(this); |
129 | #ifdef Q_OS_MAC |
130 | system = QLatin1String("mac" ); |
131 | #else |
132 | hboxLayout->setSpacing(6); |
133 | hboxLayout->setContentsMargins(left: 0, top: 0, right: 0, bottom: 0); |
134 | #endif |
135 | |
136 | d->toolClose = new QToolButton(this); |
137 | d->toolClose->setIcon(QIcon(QString::fromUtf8(str: ":/qt/scripttools/debugging/images/%1/closetab.png" ).arg(a: system))); |
138 | d->toolClose->setAutoRaise(true); |
139 | d->toolClose->setText(tr(s: "Close" )); |
140 | hboxLayout->addWidget(d->toolClose); |
141 | |
142 | d->editFind = new QLineEdit(this); |
143 | d->editFind->setMinimumSize(QSize(150, 0)); |
144 | connect(sender: d->editFind, SIGNAL(textChanged(QString)), |
145 | receiver: this, SLOT(_q_updateButtons())); |
146 | connect(sender: d->editFind, SIGNAL(returnPressed()), |
147 | receiver: this, SLOT(_q_next())); |
148 | hboxLayout->addWidget(d->editFind); |
149 | |
150 | d->toolPrevious = new QToolButton(this); |
151 | d->toolPrevious->setAutoRaise(true); |
152 | d->toolPrevious->setText(tr(s: "Previous" )); |
153 | d->toolPrevious->setToolButtonStyle(Qt::ToolButtonTextBesideIcon); |
154 | d->toolPrevious->setIcon(QIcon(QString::fromUtf8(str: ":/qt/scripttools/debugging/images/%1/previous.png" ).arg(a: system))); |
155 | hboxLayout->addWidget(d->toolPrevious); |
156 | |
157 | d->toolNext = new QToolButton(this); |
158 | d->toolNext->setAutoRaise(true); |
159 | d->toolNext->setText(tr(s: "Next" )); |
160 | d->toolNext->setToolButtonStyle(Qt::ToolButtonTextBesideIcon); |
161 | d->toolNext->setIcon(QIcon(QString::fromUtf8(str: ":/qt/scripttools/debugging/images/%1/next.png" ).arg(a: system))); |
162 | hboxLayout->addWidget(d->toolNext); |
163 | |
164 | d->checkCase = new QCheckBox(tr(s: "Case Sensitive" ), this); |
165 | hboxLayout->addWidget(d->checkCase); |
166 | |
167 | d->checkWholeWords = new QCheckBox(tr(s: "Whole words" ), this); |
168 | hboxLayout->addWidget(d->checkWholeWords); |
169 | |
170 | d->labelWrapped = new QLabel(this); |
171 | d->labelWrapped->setMinimumSize(QSize(0, 20)); |
172 | d->labelWrapped->setMaximumSize(QSize(115, 20)); |
173 | d->labelWrapped->setTextFormat(Qt::RichText); |
174 | d->labelWrapped->setScaledContents(true); |
175 | d->labelWrapped->setAlignment(Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter); |
176 | d->labelWrapped->setText(tr(s: "<img src=\":/qt/scripttools/debugging/images/wrap.png\"> Search wrapped" )); |
177 | hboxLayout->addWidget(d->labelWrapped); |
178 | |
179 | QSpacerItem *spacerItem = new QSpacerItem(20, 20, QSizePolicy::Expanding, QSizePolicy::Minimum); |
180 | hboxLayout->addItem(spacerItem); |
181 | setMinimumWidth(minimumSizeHint().width()); |
182 | d->labelWrapped->hide(); |
183 | |
184 | d->_q_updateButtons(); |
185 | |
186 | setFocusProxy(d->editFind); |
187 | QObject::connect(sender: d->toolClose, SIGNAL(clicked()), receiver: this, SLOT(hide())); |
188 | QObject::connect(sender: d->editFind, SIGNAL(textChanged(QString)), |
189 | receiver: this, SLOT(_q_onTextChanged(QString))); |
190 | QObject::connect(sender: d->toolNext, SIGNAL(clicked()), receiver: this, SLOT(_q_next())); |
191 | QObject::connect(sender: d->toolPrevious, SIGNAL(clicked()), receiver: this, SLOT(_q_previous())); |
192 | } |
193 | |
194 | QScriptDebuggerCodeFinderWidget::~QScriptDebuggerCodeFinderWidget() |
195 | { |
196 | } |
197 | |
198 | int QScriptDebuggerCodeFinderWidget::findOptions() const |
199 | { |
200 | Q_D(const QScriptDebuggerCodeFinderWidget); |
201 | return d->findOptions(); |
202 | } |
203 | |
204 | QString QScriptDebuggerCodeFinderWidget::text() const |
205 | { |
206 | Q_D(const QScriptDebuggerCodeFinderWidget); |
207 | return d->editFind->text(); |
208 | } |
209 | |
210 | void QScriptDebuggerCodeFinderWidget::setText(const QString &text) |
211 | { |
212 | Q_D(const QScriptDebuggerCodeFinderWidget); |
213 | d->editFind->setText(text); |
214 | } |
215 | |
216 | void QScriptDebuggerCodeFinderWidget::setOK(bool ok) |
217 | { |
218 | Q_D(QScriptDebuggerCodeFinderWidget); |
219 | QPalette p = d->editFind->palette(); |
220 | QColor c; |
221 | if (ok) |
222 | c = Qt::white; |
223 | else |
224 | c = QColor(255, 102, 102); |
225 | p.setColor(acg: QPalette::Active, acr: QPalette::Base, acolor: c); |
226 | d->editFind->setPalette(p); |
227 | if (!ok) |
228 | d->labelWrapped->hide(); |
229 | } |
230 | |
231 | void QScriptDebuggerCodeFinderWidget::setWrapped(bool wrapped) |
232 | { |
233 | Q_D(QScriptDebuggerCodeFinderWidget); |
234 | d->labelWrapped->setVisible(wrapped); |
235 | } |
236 | |
237 | void QScriptDebuggerCodeFinderWidget::keyPressEvent(QKeyEvent *e) |
238 | { |
239 | if (e->key() == Qt::Key_Escape) |
240 | hide(); |
241 | else |
242 | QScriptDebuggerCodeFinderWidgetInterface::keyPressEvent(event: e); |
243 | } |
244 | |
245 | QT_END_NAMESPACE |
246 | |
247 | #include "moc_qscriptdebuggercodefinderwidget_p.cpp" |
248 | |