~ubuntu-branches/ubuntu/dapper/ksystemlog/dapper

« back to all changes in this revision

Viewing changes to ksystemlog/src/findManager.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2005-07-07 16:00:00 UTC
  • Revision ID: james.westby@ubuntu.com-20050707160000-a104d769ph3yfkg4
Tags: upstream-0.3.2
ImportĀ upstreamĀ versionĀ 0.3.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// C++ Implementation: findManager
 
3
//
 
4
// Description: 
 
5
//
 
6
//
 
7
// Author: Nicolas Ternisien <nicolas.ternisien@gmail.com>, (C) 2005
 
8
//
 
9
// Copyright: See COPYING file that comes with this distribution
 
10
//
 
11
//
 
12
 
 
13
//Project includes
 
14
#include "findManager.h"
 
15
 
 
16
#include "ksystemlog.h"
 
17
 
 
18
 
 
19
FindManager::FindManager(KSystemLog* parent, const char* name) :
 
20
        QObject(parent, name),
 
21
        main(parent),
 
22
        findDialog(NULL),
 
23
        findManager(NULL),
 
24
        previousItemFound(NULL),
 
25
        currentItemFound(NULL) {
 
26
        
 
27
        
 
28
}
 
29
 
 
30
FindManager::~FindManager() {
 
31
        if (findDialog!=NULL)
 
32
                delete findDialog;
 
33
                
 
34
        if (findManager!=NULL)
 
35
                delete findManager;
 
36
}
 
37
 
 
38
void FindManager::slotFind() {
 
39
 
 
40
        if (findManager!=NULL) {
 
41
                delete findManager;
 
42
                findManager=NULL;
 
43
        }
 
44
        
 
45
        if (findDialog==NULL) {
 
46
                kdDebug() << "KFindDialog creation" << endl;
 
47
                //TODO Save the Find options to Config file
 
48
                findDialog=new KFindDialog(false, main, "find_dialog");
 
49
                connect(findDialog, SIGNAL(okClicked()), this, SLOT(slotFindNext()));
 
50
        
 
51
                findDialog->show();
 
52
        }
 
53
        else {
 
54
                findDialog->show();
 
55
        
 
56
                //KWin::activateWindow(findDialog->winId());
 
57
                //return;
 
58
        }
 
59
 
 
60
}
 
61
 
 
62
 
 
63
void FindManager::slotFirstFind() {
 
64
        kdDebug() << "First Find" << endl;
 
65
        
 
66
        LogManager* currentManager=main->activeLogManager();
 
67
        KListView* list=currentManager->getView()->getLogList();
 
68
        
 
69
        //Delete the previous KFind object (if it exists)
 
70
        if (findManager!=NULL) {
 
71
                delete findManager;
 
72
                findManager=NULL;
 
73
        }
 
74
 
 
75
        //TODO Is useful?
 
76
        if (previousItemFound!=NULL) {
 
77
                list->setSelected(previousItemFound, false);
 
78
        }
 
79
        
 
80
        //This creates a find-next-prompt dialog if needed.
 
81
        findManager=new KFind(findDialog->pattern(), findDialog->options(), main, findDialog);
 
82
        connect(findManager, SIGNAL(highlight(const QString&, int, int)), this, SLOT(highlightSearch(const QString&, int, int)));
 
83
        connect(findManager, SIGNAL(findNext()), this, SLOT(slotFindNext()));
 
84
        
 
85
        //Do not keep the KFindDialog open
 
86
        findDialog->hide();
 
87
        
 
88
        
 
89
        //findManager->closeFindNextDialog();
 
90
        
 
91
        QListViewItemIterator it(list, QListViewItemIterator::Selected);
 
92
        int itemPos=-1;
 
93
        int currentPos;
 
94
        while (it.current()) {
 
95
                list->setSelected(it.current(), false);
 
96
                
 
97
                currentPos=list->itemPos(it.current());
 
98
                if (currentPos>=itemPos) {
 
99
                        itemPos=currentPos;
 
100
                        previousItemFound=static_cast<LogListItem*> (it.current());
 
101
                }
 
102
                
 
103
                ++it;
 
104
        }
 
105
        //Unselect all previous items (the last one if always selected)
 
106
        if (previousItemFound!=NULL)
 
107
                list->setSelected(previousItemFound, true);
 
108
        
 
109
        
 
110
        
 
111
        //If we search from the cursor, the iterator goes to the first selected item
 
112
        if (findDialog->options() & KFindDialog::FromCursor) {
 
113
                currentItemFound=currentManager->getView()->getFirstSelectedItem();
 
114
                
 
115
                //If nothing is selected, then we initialize the iterator classicaly
 
116
                if (currentItemFound==NULL) {
 
117
                        currentItemFound=static_cast<LogListItem*> (list->firstChild());
 
118
                }
 
119
                
 
120
                previousItemFound=currentItemFound;
 
121
        }
 
122
        //If we search from the last, the iterator is initialized to the last item
 
123
        else if (findDialog->options() & KFindDialog::FindBackwards) {
 
124
                currentItemFound=static_cast<LogListItem*> (list->lastItem());
 
125
        }
 
126
        //Default initialization
 
127
        else {
 
128
                currentItemFound=static_cast<LogListItem*> (list->firstChild());
 
129
        }
 
130
        
 
131
}
 
132
 
 
133
void FindManager::slotFindNext() {
 
134
        //If we attempt to go to the next search, without have opened the find dialog, we open it
 
135
        if (findDialog==NULL) {
 
136
                slotFind();
 
137
                return;
 
138
        }
 
139
        
 
140
        //If the KFind object has not been initialized, we call the right method for that
 
141
        if (findManager==NULL) {
 
142
                slotFirstFind();
 
143
        }
 
144
 
 
145
        
 
146
        //Keep the Find Dialog opened
 
147
        /*
 
148
        if (findManager->options() != findDialog->options() || findManager->pattern()!=findDialog->pattern()) {
 
149
                kdDebug() << "Reinitialize the search..." << endl;
 
150
                slotFirstFind();
 
151
        }
 
152
        */
 
153
 
 
154
 
 
155
        KFind::Result res = KFind::NoMatch;
 
156
        while (res==KFind::NoMatch && currentItemFound!=NULL) {
 
157
 
 
158
                //Always need new data, because a line is only selected one time
 
159
                //if (findManager->needData())
 
160
                        findManager->setData(currentItemFound->exportToText());
 
161
 
 
162
                //Let KFind inspect the text fragment, and display a dialog if a match is found
 
163
                res=findManager->find();
 
164
 
 
165
                //Always need new data, because a line is only selected one time
 
166
                //if (res==KFind::NoMatch ) {
 
167
                        if (findDialog->options() & KFindDialog::FindBackwards)
 
168
                                currentItemFound=static_cast<LogListItem*> (currentItemFound->itemAbove());
 
169
                        else
 
170
                                currentItemFound=static_cast<LogListItem*> (currentItemFound->itemBelow());
 
171
                //}
 
172
        }
 
173
 
 
174
 
 
175
        //End of the view
 
176
        if (res==KFind::NoMatch) {
 
177
                if (findManager->shouldRestart()) {
 
178
                        delete findManager;
 
179
                        findManager=NULL;
 
180
                        findDialog->setOptions(findDialog->options() & !KFindDialog::FromCursor);
 
181
                        slotFindNext();
 
182
                }
 
183
                else {
 
184
                        findManager->closeFindNextDialog();
 
185
                }
 
186
        }
 
187
 
 
188
}
 
189
 
 
190
 
 
191
void FindManager::highlightSearch(const QString& /*text*/, int /*matchingIndex*/, int /*matchingLength*/) {
 
192
        LogManager* currentManager=main->activeLogManager();
 
193
        KListView* list=currentManager->getView()->getLogList();
 
194
                
 
195
        if (previousItemFound!=NULL) {
 
196
                list->setSelected(previousItemFound, false);
 
197
        }
 
198
        
 
199
        if (currentItemFound!=NULL) {
 
200
                list->setSelected(currentItemFound, true);
 
201
                list->ensureItemVisible(currentItemFound);
 
202
                previousItemFound=currentItemFound;
 
203
        }
 
204
 
 
205
}
 
206
 
 
207
 
 
208
#include "findManager.moc"