summaryrefslogtreecommitdiffstats
path: root/parts/documentation/indexview.cpp
blob: 3f323fc58ff0fcf6ca4c10a14f69198f0ecf1260 (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
/***************************************************************************
 *   Copyright (C) 2004 by Alexander Dymo                                  *
 *   [email protected]                                                 *
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.             *
 ***************************************************************************/
#include "indexview.h"

#include <tqapplication.h>
#include <tqevent.h>
#include <tqlayout.h>
#include <tqlabel.h>

#include <klineedit.h>
#include <kiconloader.h>
#include <klocale.h>
#include <kurl.h>
#include <kdebug.h>

#include <kdevpartcontroller.h>
#include <kdevdocumentationplugin.h>

#include "docutils.h"
#include "selecttopic.h"
#include "documentation_part.h"
#include "documentation_widget.h"

IndexView::IndexView(DocumentationWidget *parent, const char *name)
     :TQWidget(parent, name), m_widget(parent)
{
    TQVBoxLayout *l = new TQVBoxLayout(this, 0, 0);

    TQHBoxLayout *hl = new TQHBoxLayout(this, 0, 0);
    m_edit = new KLineEdit(this);
    hl->addWidget(new TQLabel(m_edit, i18n("&Look for:"), this));
    hl->addWidget(m_edit);
    l->addLayout(hl);

    m_index = new IndexBox(this);
    l->addWidget(m_index);

    connect(m_index, TQT_SIGNAL(executed(TQListBoxItem* )), this, TQT_SLOT(searchInIndex(TQListBoxItem* )));
    connect(m_index, TQT_SIGNAL(mouseButtonPressed(int, TQListBoxItem*, const TQPoint& )),
        this, TQT_SLOT(itemMouseButtonPressed(int, TQListBoxItem*, const TQPoint& )));
    connect(m_edit, TQT_SIGNAL(returnPressed()), this, TQT_SLOT(searchInIndex()));
    connect(m_edit, TQT_SIGNAL(textChanged(const TQString&)), this, TQT_SLOT(showIndex(const TQString&)));

    m_edit->installEventFilter(this);
}

IndexView::~IndexView()
{
}

void IndexView::searchInIndex()
{
    if (m_index->currentItem() != -1)
        searchInIndex(m_index->item(m_index->currentItem()));
}

void IndexView::searchInIndex(TQListBoxItem *item)
{
    kdDebug() << "IndexView::searchInIndex" << endl;
    IndexItem *indexItem = dynamic_cast<IndexItem*>(item);
    if (!indexItem)
        return;

    IndexItem::List urls = indexItem->urls();
    if (urls.count() == 1)
        m_widget->part()->partController()->showDocument(urls.first().second);
    else if (urls.count() == 0) ;
    else
    {
        SelectTopic *dia = new SelectTopic(urls);
        dia->topicLabel->setText(dia->topicLabel->text().tqarg(item->text()));
        if (dia->exec())
            m_widget->part()->partController()->showDocument(dia->selectedURL());
        delete dia;
    }
}

void IndexView::showIndex(const TQString &term)
{
    TQListBoxItem *i = m_index->firstItem();
    TQString sl = term.lower();
    while (i)
    {
        TQString t = i->text();
        if ((t.length() >= sl.length()) && (i->text().left(term.length()).lower() == sl))
        {
            m_index->setCurrentItem(i);
            m_index->setTopItem(m_index->index(i));
            break;
        }
        i = i->next();
    }
/*
    for (uint i = 0; i < m_index->count(); ++ i)
    {
        if (m_index->text(i).startsWith(term))
        {
            m_index->setCurrentItem(i);
            m_index->setTopItem(i);
        }
    }*/
}

bool IndexView::eventFilter(TQObject *watched, TQEvent *e)
{
    if (!watched || !e)
        return true;

    if ((TQT_BASE_OBJECT(watched) == TQT_BASE_OBJECT(m_edit)) && (e->type() == TQEvent::KeyPress))
    {
        TQKeyEvent *ke = (TQKeyEvent*)e;
        if (ke->key() == Key_Up)
        {
            int i = m_index->currentItem();
            if (--i >= 0)
            {
                m_index->setCurrentItem(i);
                m_edit->blockSignals(true);
                m_edit->setText(m_index->currentText());
                m_edit->blockSignals(false);
            }
            return true;
        } else if (ke->key() == Key_Down)
        {
            int i = m_index->currentItem();
            if ( ++i < int(m_index->count()) )
            {
                m_index->setCurrentItem(i);
                m_edit->blockSignals(true);
                m_edit->setText(m_index->currentText());
                m_edit->blockSignals(false);
            }
            return true;
        } else if ((ke->key() == Key_Next) || (ke->key() == Key_Prior))
        {
            TQApplication::sendEvent(m_index, e);
            m_edit->blockSignals(true);
            m_edit->setText(m_index->currentText());
            m_edit->blockSignals(false);
        }
    }

    return TQWidget::eventFilter(watched, e);
}

void IndexView::itemMouseButtonPressed(int button, TQListBoxItem *item, const TQPoint &pos)
{
    if ((button != Qt::RightButton) || (!item))
        return;
    IndexItem *docItem = dynamic_cast<IndexItem*>(item);
    if (!docItem)
        return;

    DocUtils::docItemPopup(m_widget->part(), docItem, pos, false, true);
}

void IndexView::setSearchTerm(const TQString &term)
{
    m_edit->setFocus();
    m_edit->setText(term);
}

void IndexView::askSearchTerm( )
{
    m_edit->setFocus();
}

void IndexView::focusInEvent(TQFocusEvent */*e*/)
{
    m_edit->setFocus();
}

#include "indexview.moc"