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