OSDN Git Service

Added missing include.
[qt-creator-jp/qt-creator-jp.git] / src / plugins / quickopen / basefilefilter.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
8 **
9 ** Commercial Usage
10 **
11 ** Licensees holding valid Qt Commercial licenses may use this file in
12 ** accordance with the Qt Commercial License Agreement provided with the
13 ** Software or, alternatively, in accordance with the terms contained in
14 ** a written agreement between you and Nokia.
15 **
16 ** GNU Lesser General Public License Usage
17 **
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file.  Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** If you are unsure which license is appropriate for your use, please
26 ** contact the sales department at http://www.qtsoftware.com/contact.
27 **
28 **************************************************************************/
29
30 #include "basefilefilter.h"
31
32 #include <coreplugin/editormanager/editormanager.h>
33
34 #include <QtCore/QDir>
35 #include <QtCore/QStringMatcher>
36
37 using namespace Core;
38 using namespace QuickOpen;
39
40 BaseFileFilter::BaseFileFilter()
41   : m_forceNewSearchList(false)
42 {
43 }
44
45 QList<FilterEntry> BaseFileFilter::matchesFor(const QString &origEntry)
46 {
47     QList<FilterEntry> matches;
48     QList<FilterEntry> badMatches;
49     QString needle = trimWildcards(origEntry);
50     QStringMatcher matcher(needle, Qt::CaseInsensitive);
51     const QRegExp regexp("*"+needle+"*", Qt::CaseInsensitive, QRegExp::Wildcard);
52     if (!regexp.isValid())
53         return matches;
54     bool hasWildcard = (needle.contains('*') || needle.contains('?'));
55     QStringList searchListPaths;
56     QStringList searchListNames;
57     if (!m_previousEntry.isEmpty() && !m_forceNewSearchList && needle.contains(m_previousEntry)) {
58         searchListPaths = m_previousResultPaths;
59         searchListNames = m_previousResultNames;
60     } else {
61         searchListPaths = m_files;
62         searchListNames = m_fileNames;
63     }
64     m_previousResultPaths.clear();
65     m_previousResultNames.clear();
66     m_forceNewSearchList = false;
67     m_previousEntry = needle;
68     QStringListIterator paths(searchListPaths);
69     QStringListIterator names(searchListNames);
70     while (paths.hasNext() && names.hasNext()) {
71         QString path = paths.next();
72         QString name = names.next();
73         if ((hasWildcard && regexp.exactMatch(name))
74                 || (!hasWildcard && matcher.indexIn(name) != -1)) {
75             QFileInfo fi(path);
76             FilterEntry entry(this, fi.fileName(), path);
77             entry.extraInfo = QDir::toNativeSeparators(fi.path());
78             entry.resolveFileIcon = true;
79             if (name.startsWith(needle))
80                 matches.append(entry);
81             else
82                 badMatches.append(entry);
83             m_previousResultPaths.append(path);
84             m_previousResultNames.append(name);
85         }
86     }
87    
88     matches.append(badMatches); 
89     return matches;
90 }
91
92 void BaseFileFilter::accept(QuickOpen::FilterEntry selection) const
93 {
94     Core::EditorManager *em = Core::EditorManager::instance();
95     em->openEditor(selection.internalData.toString());
96     em->ensureEditorManagerVisible();
97 }
98
99 void BaseFileFilter::generateFileNames()
100 {
101     m_fileNames.clear();
102     foreach (const QString &fileName, m_files) {
103         QFileInfo fi(fileName);
104         m_fileNames.append(fi.fileName());
105     }
106     m_forceNewSearchList = true;
107 }