1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the Qt Assistant 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 http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://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 2.1 or version 3 as published by the Free
20 ** Software Foundation and appearing in the file LICENSE.LGPLv21 and
21 ** LICENSE.LGPLv3 included in the packaging of this file. Please review the
22 ** following information to ensure the GNU Lesser General Public License
23 ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
24 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 **
26 ** As a special exception, The Qt Company gives you certain additional
27 ** rights. These rights are described in The Qt Company LGPL Exception
28 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 **
30 ** GNU General Public License Usage
31 ** Alternatively, this file may be used under the terms of the GNU
32 ** General Public License version 3.0 as published by the Free Software
33 ** Foundation and appearing in the file LICENSE.GPL included in the
34 ** packaging of this file.  Please review the following information to
35 ** ensure the GNU General Public License version 3.0 requirements will be
36 ** met: http://www.gnu.org/copyleft/gpl.html.
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 #include "tracer.h"
42 #include "findwidget.h"
43 
44 #include <QtGui/QApplication>
45 #include <QtGui/QCheckBox>
46 #include <QtGui/QHideEvent>
47 #include <QtGui/QKeyEvent>
48 #include <QtGui/QLabel>
49 #include <QtGui/QLayout>
50 #include <QtGui/QLineEdit>
51 #include <QtGui/QToolButton>
52 
53 QT_BEGIN_NAMESPACE
54 
FindWidget(QWidget * parent)55 FindWidget::FindWidget(QWidget *parent)
56     : QWidget(parent)
57     , appPalette(qApp->palette())
58 {
59     TRACE_OBJ
60     installEventFilter(this);
61     QHBoxLayout *hboxLayout = new QHBoxLayout(this);
62     QString resourcePath = QLatin1String(":/trolltech/assistant/images/");
63 
64 #ifndef Q_OS_MAC
65     hboxLayout->setMargin(0);
66     hboxLayout->setSpacing(6);
67     resourcePath.append(QLatin1String("win"));
68 #else
69     resourcePath.append(QLatin1String("mac"));
70 #endif
71 
72     toolClose = setupToolButton(QLatin1String(""),
73         resourcePath + QLatin1String("/closetab.png"));
74     hboxLayout->addWidget(toolClose);
75     connect(toolClose, SIGNAL(clicked()), SLOT(hide()));
76 
77     editFind = new QLineEdit(this);
78     hboxLayout->addWidget(editFind);
79     editFind->setMinimumSize(QSize(150, 0));
80     connect(editFind, SIGNAL(textChanged(QString)), this,
81         SLOT(textChanged(QString)));
82     connect(editFind, SIGNAL(returnPressed()), this, SIGNAL(findNext()));
83     connect(editFind, SIGNAL(textChanged(QString)), this, SLOT(updateButtons()));
84 
85     toolPrevious = setupToolButton(tr("Previous"),
86         resourcePath + QLatin1String("/previous.png"));
87     connect(toolPrevious, SIGNAL(clicked()), this, SIGNAL(findPrevious()));
88 
89     hboxLayout->addWidget(toolPrevious);
90 
91     toolNext = setupToolButton(tr("Next"),
92         resourcePath + QLatin1String("/next.png"));
93     hboxLayout->addWidget(toolNext);
94     connect(toolNext, SIGNAL(clicked()), this, SIGNAL(findNext()));
95 
96     checkCase = new QCheckBox(tr("Case Sensitive"), this);
97     hboxLayout->addWidget(checkCase);
98 
99     labelWrapped = new QLabel(this);
100     labelWrapped->setScaledContents(true);
101     labelWrapped->setTextFormat(Qt::RichText);
102     labelWrapped->setMinimumSize(QSize(0, 20));
103     labelWrapped->setMaximumSize(QSize(105, 20));
104     labelWrapped->setAlignment(Qt::AlignLeading | Qt::AlignLeft | Qt::AlignVCenter);
105     labelWrapped->setText(tr("<img src=\":/trolltech/assistant/images/wrap.png\""
106         ">&nbsp;Search wrapped"));
107     hboxLayout->addWidget(labelWrapped);
108 
109     QSpacerItem *spacerItem = new QSpacerItem(20, 20, QSizePolicy::Expanding,
110         QSizePolicy::Minimum);
111     hboxLayout->addItem(spacerItem);
112     setMinimumWidth(minimumSizeHint().width());
113     labelWrapped->hide();
114 
115     updateButtons();
116 }
117 
~FindWidget()118 FindWidget::~FindWidget()
119 {
120     TRACE_OBJ
121 }
122 
show()123 void FindWidget::show()
124 {
125     TRACE_OBJ
126     QWidget::show();
127     editFind->selectAll();
128     editFind->setFocus(Qt::ShortcutFocusReason);
129 }
130 
showAndClear()131 void FindWidget::showAndClear()
132 {
133     TRACE_OBJ
134     show();
135     editFind->clear();
136 }
137 
text() const138 QString FindWidget::text() const
139 {
140     TRACE_OBJ
141     return editFind->text();
142 }
143 
caseSensitive() const144 bool FindWidget::caseSensitive() const
145 {
146     TRACE_OBJ
147     return checkCase->isChecked();
148 }
149 
setPalette(bool found)150 void FindWidget::setPalette(bool found)
151 {
152     TRACE_OBJ
153     QPalette palette = editFind->palette();
154     palette.setColor(QPalette::Active, QPalette::Base, found ? Qt::white
155         : QColor(255, 102, 102));
156     editFind->setPalette(palette);
157 }
158 
setTextWrappedVisible(bool visible)159 void FindWidget::setTextWrappedVisible(bool visible)
160 {
161     TRACE_OBJ
162     labelWrapped->setVisible(visible);
163 }
164 
hideEvent(QHideEvent * event)165 void FindWidget::hideEvent(QHideEvent* event)
166 {
167     TRACE_OBJ
168 #if !defined(QT_NO_WEBKIT)
169     // TODO: remove this once webkit supports setting the palette
170     if (!event->spontaneous())
171         qApp->setPalette(appPalette);
172 #else
173     Q_UNUSED(event);
174 #endif
175 }
176 
showEvent(QShowEvent * event)177 void FindWidget::showEvent(QShowEvent* event)
178 {
179     TRACE_OBJ
180 #if !defined(QT_NO_WEBKIT)
181     // TODO: remove this once webkit supports setting the palette
182     if (!event->spontaneous()) {
183         QPalette p = appPalette;
184         p.setColor(QPalette::Inactive, QPalette::Highlight,
185             p.color(QPalette::Active, QPalette::Highlight));
186         p.setColor(QPalette::Inactive, QPalette::HighlightedText,
187             p.color(QPalette::Active, QPalette::HighlightedText));
188         qApp->setPalette(p);
189     }
190 #else
191     Q_UNUSED(event);
192 #endif
193 }
194 
updateButtons()195 void FindWidget::updateButtons()
196 {
197     TRACE_OBJ
198     const bool enable = !editFind->text().isEmpty();
199     toolNext->setEnabled(enable);
200     toolPrevious->setEnabled(enable);
201 }
202 
textChanged(const QString & text)203 void FindWidget::textChanged(const QString &text)
204 {
205     TRACE_OBJ
206     emit find(text, true, true);
207 }
208 
eventFilter(QObject * object,QEvent * e)209 bool FindWidget::eventFilter(QObject *object, QEvent *e)
210 {
211     TRACE_OBJ
212     if (e->type() == QEvent::KeyPress) {
213         if ((static_cast<QKeyEvent*>(e))->key() == Qt::Key_Escape) {
214             hide();
215             emit escapePressed();
216         }
217     }
218     return QWidget::eventFilter(object, e);
219 }
220 
setupToolButton(const QString & text,const QString & icon)221 QToolButton* FindWidget::setupToolButton(const QString &text, const QString &icon)
222 {
223     TRACE_OBJ
224     QToolButton *toolButton = new QToolButton(this);
225 
226     toolButton->setText(text);
227     toolButton->setAutoRaise(true);
228     toolButton->setIcon(QIcon(icon));
229     toolButton->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
230 
231     return toolButton;
232 }
233 
234 QT_END_NAMESPACE
235