summaryrefslogtreecommitdiffstats
path: root/konversation/src/searchbar.cpp
blob: 7f8c00495ccde69695e9462703d99426e7eb54fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/*
  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.
*/

/*
  Copyright (C) 2005 Renchi Raju <renchi@pooh.tam.uiuc.edu>
  Copyright (C) 2006 Peter Simonsson <psn@linux.se>
*/

#include "searchbar.h"

#include <tqcheckbox.h>
#include <tqtimer.h>
#include <tqpalette.h>
#include <tqaccel.h>
#include <tqlabel.h>
#include <tqpixmap.h>
#include <tqobjectlist.h>
#include <tqtoolbutton.h>
#include <tqpopupmenu.h>
#include <tqwidgetstack.h>

#include <kdebug.h>
#include <kapplication.h>
#include <kiconloader.h>
#include <klineedit.h>
#include <kpushbutton.h>
#include <klocale.h>

#define SEARCH_FORWARD_MENU 1
#define MATCH_CASE_MENU 2
#define WHOLE_WORDS_ONLY_MENU 3
#define FROM_CURSOR_MENU 4


SearchBar::SearchBar(TQWidget* parent)
: SearchBarBase(parent)
{
    m_searchFoward = false;
    m_matchCase = false;
    m_wholeWords = false;
    m_fromCursor = false;

    setFocusProxy(m_searchEdit);
    KIconLoader* iconLoader = kapp->iconLoader();
    m_closeButton->setIconSet(iconLoader->loadIconSet("fileclose", KIcon::Toolbar, 16));
    m_findNextButton->setIconSet(iconLoader->loadIconSet("up", KIcon::Toolbar, 16));
    m_findPreviousButton->setIconSet(iconLoader->loadIconSet("down", KIcon::Toolbar, 16));
    m_statusPixLabel->hide();
    m_statusTextLabel->hide();

    m_timer = new TQTimer(this);

    TQAccel* accel = new TQAccel(this);
    accel->connectItem( accel->insertItem(Qt::Key_Escape), this, TQT_SLOT(hide()));

    connect(m_timer, TQT_SIGNAL(timeout()), TQT_SLOT(slotFind()));
    connect(m_searchEdit, TQT_SIGNAL(textChanged(const TQString&)), TQT_SLOT(slotTextChanged()));
    connect(m_searchEdit, TQT_SIGNAL(returnPressed()), TQT_SLOT(slotFindNext()));
    connect(m_findNextButton, TQT_SIGNAL(clicked()), TQT_SLOT(slotFindNext()));
    connect(m_findPreviousButton, TQT_SIGNAL(clicked()), TQT_SLOT(slotFindPrevious()));
    connect(m_closeButton, TQT_SIGNAL(clicked()), TQT_SLOT(hide()));
    connect(m_optionsButton, TQT_SIGNAL(clicked()), this, TQT_SLOT(showOptionsMenu()));

    m_optionsMenu = new TQPopupMenu(m_optionsButton, "options_menu");
    m_optionsMenu->setCheckable(true);
    m_optionsMenu->insertItem(i18n("Find Forward"), this, TQT_SLOT(toggleSearchFoward()), 0, SEARCH_FORWARD_MENU);
    m_optionsMenu->insertItem(i18n("Case Sensitive"), this, TQT_SLOT(toggleMatchCase()), 0, MATCH_CASE_MENU);
    m_optionsMenu->insertItem(i18n("Whole Words Only"), this, TQT_SLOT(toggleWholeWords()), 0, WHOLE_WORDS_ONLY_MENU);
    m_optionsMenu->insertItem(i18n("From Cursor"), this, TQT_SLOT(toggleFromCursor()), 0, FROM_CURSOR_MENU);

    m_optionsButton->setPopup(m_optionsMenu);
}

SearchBar::~SearchBar()
{
}

void SearchBar::showEvent(TQShowEvent *e)
{
    SearchBarBase::showEvent(e);
    m_searchEdit->selectAll();
}

bool SearchBar::focusedChild()
{
    TQObjectList *l = queryList("TQWidget", 0,0, true);
    TQObjectListIt it( *l ); 
    TQObject *obj;
    bool has=false;

    while ((obj = it.current()) != 0)
    {
        ++it;
        if (((TQWidget*)obj)->hasFocus())
        {
            has=true;
            break;
        }
    }
    delete l;
    return has;
}

void SearchBar::hide()
{
    m_timer->stop();
    SearchBarBase::hide();

    if (focusedChild())
        emit hidden();
}

void SearchBar::slotTextChanged()
{
    m_timer->start(50, true);
}

void SearchBar::slotFind()
{
    if (m_searchEdit->text().isEmpty())
    {
        m_searchEdit->unsetPalette();
        m_findNextButton->setEnabled(false);
        m_findPreviousButton->setEnabled(false);
        setStatus(TQPixmap(), "");
        return;
    }

    emit signalSearchChanged(m_searchEdit->text());
}

void SearchBar::slotFindNext()
{
    if (m_searchEdit->text().isEmpty())
    {
        m_searchEdit->unsetPalette();
        m_findNextButton->setEnabled(false);
        m_findPreviousButton->setEnabled(false);
        setStatus(TQPixmap(), "");
        return;
    }

    emit signalSearchNext();
}

void SearchBar::slotFindPrevious()
{
    if (m_searchEdit->text().isEmpty())
    {
        m_searchEdit->unsetPalette();
        m_findNextButton->setEnabled(false);
        m_findPreviousButton->setEnabled(false);
        setStatus(TQPixmap(), "");
        return;
    }

    emit signalSearchPrevious();
}

void SearchBar::setHasMatch(bool value)
{
    TQPalette pal = m_searchEdit->palette();
    pal.setColor(TQPalette::Active, TQColorGroup::Base, value ? Qt::green : Qt::red);
    m_searchEdit->setPalette(pal);
    m_findNextButton->setEnabled(value);
    m_findPreviousButton->setEnabled(value);
}

void SearchBar::setStatus(const TQPixmap& pix, const TQString& text)
{
    if(!text.isEmpty()) {
        m_statusPixLabel->show();
        m_statusTextLabel->show();
    } else {
        m_statusPixLabel->hide();
        m_statusTextLabel->hide();
    }

    m_statusPixLabel->setPixmap(pix);
    m_statusTextLabel->setText(text);
}

TQString SearchBar::pattern() const
{
    return m_searchEdit->text();
}

bool SearchBar::searchForward() const
{
    return m_searchFoward;
}

bool SearchBar::caseSensitive() const
{
    return m_matchCase;
}

bool SearchBar::wholeWords() const
{
    return m_wholeWords;
}

bool SearchBar::fromCursor() const
{
    return m_fromCursor;
}

void SearchBar::toggleSearchFoward()
{
    m_searchFoward = !m_searchFoward;
    m_optionsMenu->setItemChecked(SEARCH_FORWARD_MENU, m_searchFoward);
    slotTextChanged();
}

void SearchBar::toggleMatchCase()
{
    m_matchCase = !m_matchCase;
    m_optionsMenu->setItemChecked(MATCH_CASE_MENU, m_matchCase);
    slotTextChanged();
}

void SearchBar::toggleWholeWords()
{
    m_wholeWords = !m_wholeWords;
    m_optionsMenu->setItemChecked(WHOLE_WORDS_ONLY_MENU, m_wholeWords);
    slotTextChanged();
}

void SearchBar::toggleFromCursor()
{
    m_fromCursor = !m_fromCursor;
    m_optionsMenu->setItemChecked(FROM_CURSOR_MENU, m_fromCursor);
    slotTextChanged();
}

void SearchBar::showOptionsMenu()
{
  m_optionsButton->openPopup();
}

#include "searchbar.moc"