1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
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.
16 ** GNU Lesser General Public License Usage
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.
25 ** If you are unsure which license is appropriate for your use, please
26 ** contact the sales department at http://www.qtsoftware.com/contact.
28 **************************************************************************/
30 #include "basefilefilter.h"
32 #include <coreplugin/editormanager/editormanager.h>
34 #include <QtCore/QDir>
35 #include <QtCore/QStringMatcher>
38 using namespace QuickOpen;
40 BaseFileFilter::BaseFileFilter()
41 : m_forceNewSearchList(false)
45 QList<FilterEntry> BaseFileFilter::matchesFor(const QString &origEntry)
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())
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;
61 searchListPaths = m_files;
62 searchListNames = m_fileNames;
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)) {
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);
82 badMatches.append(entry);
83 m_previousResultPaths.append(path);
84 m_previousResultNames.append(name);
88 matches.append(badMatches);
92 void BaseFileFilter::accept(QuickOpen::FilterEntry selection) const
94 Core::EditorManager *em = Core::EditorManager::instance();
95 em->openEditor(selection.internalData.toString());
96 em->ensureEditorManagerVisible();
99 void BaseFileFilter::generateFileNames()
102 foreach (const QString &fileName, m_files) {
103 QFileInfo fi(fileName);
104 m_fileNames.append(fi.fileName());
106 m_forceNewSearchList = true;