~ubuntu-branches/ubuntu/oneiric/konsole/oneiric-updates

« back to all changes in this revision

Viewing changes to src/IncrementalSearchBar.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Philip Muškovac
  • Date: 2011-07-08 12:41:10 UTC
  • Revision ID: james.westby@ubuntu.com-20110708124110-kbbocwi2k1lgodlg
Tags: upstream-4.6.90+repack
Import upstream version 4.6.90+repack

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    Copyright 2006-2008 by Robert Knight <robertknight@gmail.com>
 
3
 
 
4
    This program is free software; you can redistribute it and/or modify
 
5
    it under the terms of the GNU General Public License as published by
 
6
    the Free Software Foundation; either version 2 of the License, or
 
7
    (at your option) any later version.
 
8
 
 
9
    This program is distributed in the hope that it will be useful,
 
10
    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
    GNU General Public License for more details.
 
13
 
 
14
    You should have received a copy of the GNU General Public License
 
15
    along with this program; if not, write to the Free Software
 
16
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 
17
    02110-1301  USA.
 
18
*/
 
19
 
 
20
// Own
 
21
#include "IncrementalSearchBar.h"
 
22
 
 
23
// Qt
 
24
#include <QtGui/QCheckBox>
 
25
#include <QtGui/QBoxLayout>
 
26
#include <QtGui/QLabel>
 
27
#include <QtGui/QKeyEvent>
 
28
#include <QtCore/QTimer>
 
29
#include <QtGui/QToolButton>
 
30
#include <QtGui/QMenu>
 
31
 
 
32
// KDE
 
33
#include <KColorScheme>
 
34
#include <KLineEdit>
 
35
#include <KLocale>
 
36
#include <KIcon>
 
37
 
 
38
using namespace Konsole;
 
39
 
 
40
IncrementalSearchBar::IncrementalSearchBar(QWidget* parent)
 
41
    : QWidget(parent)
 
42
    , _foundMatch(false)
 
43
    , _searchEdit(0)
 
44
    ,_caseSensitive(0)
 
45
    ,_regExpression(0)
 
46
    ,_highlightMatches(0)
 
47
{
 
48
    QHBoxLayout* layout = new QHBoxLayout(this);
 
49
  
 
50
    QToolButton* close = new QToolButton(this);
 
51
    close->setObjectName( QLatin1String("close-button" ));
 
52
    close->setToolTip( i18n("Close the search bar") );
 
53
    close->setAutoRaise(true);
 
54
    close->setIcon(KIcon("dialog-close"));
 
55
    connect( close , SIGNAL(clicked()) , this , SIGNAL(closeClicked()) );
 
56
 
 
57
    QLabel* findLabel = new QLabel(i18n("Find:"),this);
 
58
    _searchEdit = new KLineEdit(this);
 
59
    _searchEdit->setClearButtonShown(true);
 
60
    _searchEdit->installEventFilter(this);
 
61
    _searchEdit->setObjectName( QLatin1String("search-edit" ));
 
62
    _searchEdit->setToolTip( i18n("Enter the text to search for here") );
 
63
 
 
64
    // text box may be a minimum of 6 characters wide and a maximum of 10 characters wide
 
65
    // (since the maxWidth metric is used here, more characters probably will fit in than 6
 
66
    //  and 10)
 
67
    QFontMetrics metrics(_searchEdit->font());
 
68
    int maxWidth = metrics.maxWidth();
 
69
    _searchEdit->setMinimumWidth(maxWidth*6);
 
70
    _searchEdit->setMaximumWidth(maxWidth*10);
 
71
 
 
72
    _searchTimer = new QTimer(this);
 
73
    _searchTimer->setInterval(250);
 
74
    _searchTimer->setSingleShot(true);
 
75
    connect( _searchTimer , SIGNAL(timeout()) , this , SLOT(notifySearchChanged()) );
 
76
    connect( _searchEdit , SIGNAL(clearButtonClicked()) , this , SLOT(clearLineEdit()) );
 
77
    connect( _searchEdit , SIGNAL(textChanged(const QString&)) , _searchTimer , SLOT(start()));
 
78
 
 
79
    QToolButton* findNext = new QToolButton(this);
 
80
    findNext->setObjectName( QLatin1String("find-next-button" ));
 
81
    findNext->setText(i18nc("@action:button Go to the next phrase", "Next"));
 
82
    findNext->setIcon( KIcon("go-down-search") );
 
83
    findNext->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
 
84
    findNext->setToolTip( i18n("Find the next match for the current search phrase") );
 
85
    connect( findNext , SIGNAL(clicked()) , this , SIGNAL(findNextClicked()) );
 
86
 
 
87
    QToolButton* findPrev = new QToolButton(this);
 
88
    findPrev->setObjectName( QLatin1String("find-previous-button" ));
 
89
    findPrev->setText(i18nc("@action:button Go to the previous phrase", "Previous"));
 
90
    findPrev->setIcon( KIcon("go-up-search") );
 
91
    findPrev->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
 
92
    findPrev->setToolTip( i18n("Find the previous match for the current search phrase") );
 
93
    connect( findPrev , SIGNAL(clicked()) , this , SIGNAL(findPreviousClicked()) );
 
94
 
 
95
    QToolButton* optionsButton = new QToolButton(this);
 
96
    optionsButton->setObjectName( QLatin1String("find-options-button" ));
 
97
    optionsButton->setText(i18nc("@action:button Display options menu", "Options"));
 
98
    optionsButton->setCheckable(false);
 
99
    optionsButton->setPopupMode(QToolButton::InstantPopup);
 
100
    optionsButton->setArrowType(Qt::DownArrow);
 
101
    optionsButton->setToolButtonStyle(Qt::ToolButtonTextOnly);
 
102
    optionsButton->setToolTip( i18n("Display the options menu") );
 
103
 
 
104
    layout->addWidget(close);
 
105
    layout->addWidget(findLabel);
 
106
    layout->addWidget(_searchEdit);
 
107
    layout->addWidget(findNext);
 
108
    layout->addWidget(findPrev);
 
109
    layout->addWidget(optionsButton);
 
110
 
 
111
    // Fill the options menu
 
112
    QMenu* optionsMenu = new QMenu(this);
 
113
    optionsButton->setMenu(optionsMenu);
 
114
 
 
115
    _caseSensitive = optionsMenu->addAction(i18n("Case sensitive"));
 
116
    _caseSensitive->setCheckable(true);
 
117
    _caseSensitive->setToolTip(i18n("Sets whether the search is case sensitive"));
 
118
    connect(_caseSensitive, SIGNAL(toggled(bool)),
 
119
            this, SIGNAL(matchCaseToggled(bool)) );
 
120
 
 
121
    _regExpression = optionsMenu->addAction(i18n("Match regular expression"));
 
122
    _regExpression->setCheckable(true);
 
123
    connect(_regExpression, SIGNAL(toggled(bool)),
 
124
            this, SIGNAL(matchRegExpToggled(bool)));
 
125
 
 
126
    _highlightMatches = optionsMenu->addAction(i18n("Highlight all matches"));
 
127
    _highlightMatches->setCheckable(true);
 
128
    _highlightMatches->setToolTip(i18n("Sets whether matching text should be highlighted"));
 
129
    _highlightMatches->setChecked(true);
 
130
    connect(_highlightMatches, SIGNAL(toggled(bool)),
 
131
            this, SIGNAL(highlightMatchesToggled(bool)) );
 
132
 
 
133
    layout->addStretch();
 
134
 
 
135
    layout->setContentsMargins(4, 4, 4, 4);
 
136
 
 
137
    setLayout(layout);
 
138
}
 
139
void IncrementalSearchBar::notifySearchChanged()
 
140
{
 
141
    emit searchChanged( searchText() );
 
142
}
 
143
QString IncrementalSearchBar::searchText()
 
144
{
 
145
    return _searchEdit->text();
 
146
}
 
147
 
 
148
bool IncrementalSearchBar::eventFilter(QObject* watched , QEvent* event)
 
149
{
 
150
    if ( watched == _searchEdit )
 
151
    {
 
152
        if ( event->type() == QEvent::KeyPress )
 
153
        {
 
154
            QKeyEvent* keyEvent = static_cast<QKeyEvent*>(event);
 
155
 
 
156
            if ( keyEvent->key() == Qt::Key_Escape )
 
157
            {
 
158
                emit closeClicked();
 
159
                return true;
 
160
            }
 
161
        }        
 
162
    }
 
163
        
 
164
    return QWidget::eventFilter(watched,event);
 
165
}
 
166
 
 
167
void IncrementalSearchBar::setVisible(bool visible)
 
168
{
 
169
    QWidget::setVisible(visible);
 
170
 
 
171
    if ( visible )
 
172
    {
 
173
        //TODO - Check if this is the correct reason value to use here
 
174
        _searchEdit->setFocus( Qt::ActiveWindowFocusReason );
 
175
        _searchEdit->selectAll();
 
176
    }
 
177
}
 
178
 
 
179
void IncrementalSearchBar::setFoundMatch( bool match )
 
180
{
 
181
    if ( !match && !_searchEdit->text().isEmpty() )
 
182
    {
 
183
        KStatefulBrush backgroundBrush(KColorScheme::View,KColorScheme::NegativeBackground);
 
184
 
 
185
        QString styleSheet = QString("QLineEdit{ background-color:%1 }")
 
186
                             .arg(backgroundBrush.brush(_searchEdit).color().name());
 
187
 
 
188
        _searchEdit->setStyleSheet( styleSheet );
 
189
    }
 
190
    else
 
191
    {
 
192
        _searchEdit->setStyleSheet( QString() );
 
193
    }
 
194
}
 
195
 
 
196
void IncrementalSearchBar::clearLineEdit()
 
197
{
 
198
    _searchEdit->setStyleSheet( QString() );
 
199
}
 
200
 
 
201
const QBitArray IncrementalSearchBar::optionsChecked()
 
202
{
 
203
    QBitArray options(3, 0);
 
204
 
 
205
    if (_caseSensitive->isChecked()) options.setBit(MatchCase);
 
206
    if (_regExpression->isChecked()) options.setBit(RegExp);
 
207
    if (_highlightMatches->isChecked()) options.setBit(HighlightMatches);
 
208
 
 
209
    return options;
 
210
}
 
211
 
 
212
 
 
213
#include "IncrementalSearchBar.moc"