xref: /glogg/src/quickfindwidget.cpp (revision 8b941e123ddc1953f679547d44fb51d36b42e416)
1 /*
2  * Copyright (C) 2010, 2013 Nicolas Bonnefon and other contributors
3  *
4  * This file is part of glogg.
5  *
6  * glogg is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * glogg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with glogg.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #include "log.h"
21 
22 #include <QCoreApplication>
23 #include <QToolButton>
24 #include <QLabel>
25 #include <QCheckBox>
26 #include <QLineEdit>
27 #include <QHBoxLayout>
28 
29 #include "configuration.h"
30 #include "qfnotifications.h"
31 
32 #include "quickfindwidget.h"
33 
34 const int QuickFindWidget::NOTIFICATION_TIMEOUT = 5000;
35 
36 const QString QFNotification::REACHED_EOF = "Reached end of file, no occurence found.";
37 const QString QFNotification::REACHED_BOF = "Reached beginning of file, no occurence found.";
38 
39 QuickFindWidget::QuickFindWidget( QWidget* parent ) : QWidget( parent )
40 {
41     // ui_.setupUi( this );
42     // setFocusProxy(ui_.findEdit);
43     // setProperty("topBorder", true);
44     QHBoxLayout *layout = new QHBoxLayout( this );
45 
46     layout->setMargin( 0 );
47     layout->setSpacing( 6 );
48 
49     closeButton_ = setupToolButton(
50             QLatin1String(""), QLatin1String( ":/images/darkclosebutton.png" ) );
51     layout->addWidget( closeButton_ );
52 
53     editQuickFind_ = new QLineEdit( this );
54     // FIXME: set MinimumSize might be to constraining
55     editQuickFind_->setMinimumSize( QSize( 150, 0 ) );
56     layout->addWidget( editQuickFind_ );
57 
58     ignoreCaseCheck_ = new QCheckBox( "Ignore &case" );
59     layout->addWidget( ignoreCaseCheck_ );
60 
61     previousButton_ = setupToolButton( QLatin1String("Previous"),
62             QLatin1String( ":/images/arrowup.png" ) );
63     layout->addWidget( previousButton_ );
64 
65     nextButton_ = setupToolButton( QLatin1String("Next"),
66             QLatin1String( ":/images/arrowdown.png" ) );
67     layout->addWidget( nextButton_ );
68 
69     notificationText_ = new QLabel( "" );
70     // FIXME: set MinimumSize might be too constraining
71     int width = QFNotification::maxWidth( notificationText_ );
72     notificationText_->setMinimumSize( width, 0 );
73     layout->addWidget( notificationText_ );
74 
75     setMinimumWidth( minimumSizeHint().width() );
76 
77     // Behaviour
78     connect( closeButton_, SIGNAL( clicked() ), SLOT( closeHandler() ) );
79     connect( editQuickFind_, SIGNAL( textEdited( QString ) ),
80              this, SLOT( textChanged() ) );
81     connect( ignoreCaseCheck_, SIGNAL( stateChanged( int ) ),
82              this, SLOT( textChanged() ) );
83     /*
84     connect( editQuickFind_. SIGNAL( textChanged( QString ) ), this,
85             SLOT( updateButtons() ) );
86     */
87     connect( editQuickFind_, SIGNAL( returnPressed() ),
88              this, SLOT( returnHandler() ) );
89     connect( previousButton_, SIGNAL( clicked() ),
90             this, SLOT( doSearchBackward() ) );
91     connect( nextButton_, SIGNAL( clicked() ),
92             this, SLOT( doSearchForward() ) );
93 
94     // Notification timer:
95     notificationTimer_ = new QTimer( this );
96     notificationTimer_->setSingleShot( true );
97     connect( notificationTimer_, SIGNAL( timeout() ),
98             this, SLOT( notificationTimeout() ) );
99 }
100 
101 void QuickFindWidget::userActivate()
102 {
103     userRequested_ = true;
104     QWidget::show();
105     editQuickFind_->setFocus( Qt::ShortcutFocusReason );
106 }
107 
108 //
109 // SLOTS
110 //
111 
112 void QuickFindWidget::changeDisplayedPattern( const QString& newPattern )
113 {
114     editQuickFind_->setText( newPattern );
115 }
116 
117 void QuickFindWidget::notify( const QFNotification& message )
118 {
119     LOG(logDEBUG) << "QuickFindWidget::notify()";
120 
121     notificationText_->setText( message.message() );
122     QWidget::show();
123     notificationTimer_->start( NOTIFICATION_TIMEOUT );
124 
125     // Poor man's asynchronous op.: check for events!
126     QCoreApplication::processEvents();
127 }
128 
129 void QuickFindWidget::clearNotification()
130 {
131     LOG(logDEBUG) << "QuickFindWidget::clearNotification()";
132 
133     notificationText_->setText( "" );
134 }
135 
136 // User clicks forward arrow
137 void QuickFindWidget::doSearchForward()
138 {
139     LOG(logDEBUG) << "QuickFindWidget::doSearchForward()";
140 
141     // The user has clicked on a button, so we assume she wants
142     // the widget to stay visible.
143     userRequested_ = true;
144 
145     emit patternConfirmed( editQuickFind_->text(), isIgnoreCase() );
146     emit searchForward();
147 }
148 
149 // User clicks backward arrow
150 void QuickFindWidget::doSearchBackward()
151 {
152     LOG(logDEBUG) << "QuickFindWidget::doSearchBackward()";
153 
154     // The user has clicked on a button, so we assume she wants
155     // the widget to stay visible.
156     userRequested_ = true;
157 
158     emit patternConfirmed( editQuickFind_->text(), isIgnoreCase() );
159     emit searchBackward();
160 }
161 
162 // Close and search when the user presses Return
163 void QuickFindWidget::returnHandler()
164 {
165     emit patternConfirmed( editQuickFind_->text(), isIgnoreCase() );
166     // Close the widget
167     userRequested_ = false;
168     this->hide();
169     emit close();
170 }
171 
172 // Close and reset flag when the user clicks 'close'
173 void QuickFindWidget::closeHandler()
174 {
175     userRequested_ = false;
176     this->hide();
177     emit close();
178     emit cancelSearch();
179 }
180 
181 void QuickFindWidget::notificationTimeout()
182 {
183     // We close the widget if the user hasn't explicitely requested it.
184     if ( userRequested_ == false )
185         this->hide();
186 }
187 
188 void QuickFindWidget::textChanged()
189 {
190     emit patternUpdated( editQuickFind_->text(), isIgnoreCase() );
191 }
192 
193 //
194 // Private functions
195 //
196 QToolButton* QuickFindWidget::setupToolButton(
197         const QString &text, const QString &icon)
198 {
199     QToolButton *toolButton = new QToolButton(this);
200 
201     toolButton->setText(text);
202     toolButton->setAutoRaise(true);
203     toolButton->setIcon(QIcon(icon));
204     toolButton->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
205 
206     return toolButton;
207 }
208 
209 bool QuickFindWidget::isIgnoreCase() const
210 {
211     return ( ignoreCaseCheck_->checkState() == Qt::Checked );
212 }
213