OSDN Git Service

Update license.
[qt-creator-jp/qt-creator-jp.git] / src / plugins / texteditor / findincurrentfile.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
8 **
9 **
10 ** GNU Lesser General Public License Usage
11 **
12 ** This file may be used under the terms of the GNU Lesser General Public
13 ** License version 2.1 as published by the Free Software Foundation and
14 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
15 ** Please review the following information to ensure the GNU Lesser General
16 ** Public License version 2.1 requirements will be met:
17 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 **
19 ** In addition, as a special exception, Nokia gives you certain additional
20 ** rights. These rights are described in the Nokia Qt LGPL Exception
21 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 **
23 ** Other Usage
24 **
25 ** Alternatively, this file may be used in accordance with the terms and
26 ** conditions contained in a signed written agreement between you and Nokia.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **************************************************************************/
32
33 #include "findincurrentfile.h"
34 #include "itexteditor.h"
35
36 #include <coreplugin/icore.h>
37 #include <coreplugin/editormanager/editormanager.h>
38
39 #include <QtCore/QtDebug>
40 #include <QtCore/QSettings>
41 #include <QtCore/QDirIterator>
42 #include <QtGui/QPushButton>
43 #include <QtGui/QFileDialog>
44 #include <QtGui/QVBoxLayout>
45
46 using namespace Find;
47 using namespace TextEditor;
48 using namespace TextEditor::Internal;
49
50 FindInCurrentFile::FindInCurrentFile(SearchResultWindow *resultWindow)
51   : BaseFileFind(resultWindow),
52     m_configWidget(0),
53     m_currentFile(0)
54 {
55     connect(Core::ICore::instance()->editorManager(), SIGNAL(currentEditorChanged(Core::IEditor*)),
56             this, SLOT(handleFileChange(Core::IEditor*)));
57     handleFileChange(Core::ICore::instance()->editorManager()->currentEditor());
58 }
59
60 QString FindInCurrentFile::id() const
61 {
62     return "Current File";
63 }
64
65 QString FindInCurrentFile::displayName() const
66 {
67     return tr("Current File");
68 }
69
70 Utils::FileIterator *FindInCurrentFile::files() const
71 {
72     Q_ASSERT(isEnabled());
73     QString fileName = m_currentFile->fileName();
74     QMap<QString, QTextCodec *> openEditorEncodings = ITextEditor::openedTextEditorsEncodings();
75     QTextCodec *codec = openEditorEncodings.value(fileName);
76     if (!codec)
77         codec = Core::EditorManager::instance()->defaultTextCodec();
78     return new Utils::FileIterator(QStringList() << fileName, QList<QTextCodec *>() << codec);
79 }
80
81 bool FindInCurrentFile::isEnabled() const
82 {
83     return m_currentFile && !m_currentFile->fileName().isEmpty();
84 }
85
86 void FindInCurrentFile::handleFileChange(Core::IEditor *editor)
87 {
88     if (!editor) {
89         if (m_currentFile) {
90             m_currentFile = 0;
91             emit changed();
92         }
93     } else {
94         Core::IFile *file = editor->file();
95         if (file != m_currentFile) {
96             m_currentFile = file;
97             emit changed();
98         }
99     }
100 }
101
102
103 QWidget *FindInCurrentFile::createConfigWidget()
104 {
105     if (!m_configWidget) {
106         m_configWidget = new QWidget;
107         QGridLayout * const gridLayout = new QGridLayout(m_configWidget);
108         gridLayout->setMargin(0);
109         m_configWidget->setLayout(gridLayout);
110         // just for the layout HACK
111         QLabel * const filePatternLabel = new QLabel;
112         filePatternLabel->setMinimumWidth(80);
113         filePatternLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Preferred);
114         gridLayout->addWidget(filePatternLabel, 0, 0);
115     }
116     return m_configWidget;
117 }
118
119 void FindInCurrentFile::writeSettings(QSettings *settings)
120 {
121     settings->beginGroup("FindInCurrentFile");
122     writeCommonSettings(settings);
123     settings->endGroup();
124 }
125
126 void FindInCurrentFile::readSettings(QSettings *settings)
127 {
128     settings->beginGroup("FindInCurrentFile");
129     readCommonSettings(settings, "*.cpp,*.h");
130     settings->endGroup();
131 }