OSDN Git Service

Debug progress bar removed form Symbian debugging
[qt-creator-jp/qt-creator-jp.git] / src / plugins / qt4projectmanager / profileeditor.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2010 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://qt.nokia.com/contact.
27 **
28 **************************************************************************/
29
30 #include "profileeditor.h"
31
32 #include "profilehighlighter.h"
33 #include "qt4projectmanager.h"
34 #include "qt4projectmanagerconstants.h"
35 #include "profileeditorfactory.h"
36 #include "addlibrarywizard.h"
37
38 #include <coreplugin/icore.h>
39 #include <coreplugin/actionmanager/actionmanager.h>
40 #include <coreplugin/uniqueidmanager.h>
41 #include <coreplugin/actionmanager/actioncontainer.h>
42 #include <texteditor/fontsettings.h>
43 #include <texteditor/texteditoractionhandler.h>
44 #include <texteditor/texteditorconstants.h>
45 #include <texteditor/texteditorsettings.h>
46
47 #include <QtCore/QFileInfo>
48 #include <QtCore/QDir>
49 #include <QtGui/QMenu>
50
51 using namespace Qt4ProjectManager;
52 using namespace Qt4ProjectManager::Internal;
53
54 //
55 // ProFileEditorEditable
56 //
57
58 ProFileEditorEditable::ProFileEditorEditable(ProFileEditor *editor)
59   : BaseTextEditorEditable(editor),
60     m_context(Qt4ProjectManager::Constants::C_PROFILEEDITOR,
61               TextEditor::Constants::C_TEXTEDITOR)
62 {
63 //    m_contexts << uidm->uniqueIdentifier(Qt4ProjectManager::Constants::PROJECT_KIND);
64 }
65
66 Core::Context ProFileEditorEditable::context() const
67 {
68     return m_context;
69 }
70
71 Core::IEditor *ProFileEditorEditable::duplicate(QWidget *parent)
72 {
73     ProFileEditor *ret = new ProFileEditor(parent, qobject_cast<ProFileEditor*>(editor())->factory(),
74                                            qobject_cast<ProFileEditor*>(editor())->actionHandler());
75     ret->duplicateFrom(editor());
76     TextEditor::TextEditorSettings::instance()->initializeEditor(ret);
77     return ret->editableInterface();
78 }
79
80 QString ProFileEditorEditable::id() const
81 {
82     return QLatin1String(Qt4ProjectManager::Constants::PROFILE_EDITOR_ID);
83 }
84
85 //
86 // ProFileEditorEditor
87 //
88
89 ProFileEditor::ProFileEditor(QWidget *parent, ProFileEditorFactory *factory, TextEditor::TextEditorActionHandler *ah)
90     : BaseTextEditor(parent), m_factory(factory), m_ah(ah)
91 {
92     ProFileDocument *doc = new ProFileDocument();
93     doc->setMimeType(QLatin1String(Qt4ProjectManager::Constants::PROFILE_MIMETYPE));
94     setBaseTextDocument(doc);
95
96     ah->setupActions(this);
97
98     baseTextDocument()->setSyntaxHighlighter(new ProFileHighlighter);
99     m_commentDefinition.clearCommentStyles();
100     m_commentDefinition.setSingleLine(QString(QLatin1Char('#')));
101 }
102
103 ProFileEditor::~ProFileEditor()
104 {
105 }
106
107 void ProFileEditor::unCommentSelection()
108 {
109     Utils::unCommentSelection(this, m_commentDefinition);
110 }
111
112 static bool isValidFileNameChar(const QChar &c)
113 {
114     if (c.isLetterOrNumber()
115             || c == QLatin1Char('.')
116             || c == QLatin1Char('_')
117             || c == QLatin1Char('-')
118             || c == QLatin1Char('/')
119             || c == QLatin1Char('\\'))
120         return true;
121     return false;
122 }
123
124 ProFileEditor::Link ProFileEditor::findLinkAt(const QTextCursor &cursor,
125                                       bool /* resolveTarget */)
126 {
127     Link link;
128
129     int lineNumber = 0, positionInBlock = 0;
130     convertPosition(cursor.position(), &lineNumber, &positionInBlock);
131
132     const QString block = cursor.block().text();
133
134     // check if the current position is commented out
135     const int hashPos = block.indexOf(QLatin1Char('#'));
136     if (hashPos >= 0 && hashPos < positionInBlock)
137         return link;
138
139     // find the beginning of a filename
140     QString buffer;
141     int beginPos = positionInBlock - 1;
142     while (beginPos >= 0) {
143         QChar c = block.at(beginPos);
144         if (isValidFileNameChar(c)) {
145             buffer.prepend(c);
146             beginPos--;
147         } else {
148             break;
149         }
150     }
151
152     // find the end of a filename
153     int endPos = positionInBlock;
154     while (endPos < block.count()) {
155         QChar c = block.at(endPos);
156         if (isValidFileNameChar(c)) {
157             buffer.append(c);
158             endPos++;
159         } else {
160             break;
161         }
162     }
163
164     if (buffer.isEmpty())
165         return link;
166
167     // remove trailing '\' since it can be line continuation char
168     if (buffer.at(buffer.size() - 1) == QLatin1Char('\\')) {
169         buffer.chop(1);
170         endPos--;
171     }
172
173     // if the buffer starts with $$PWD accept it
174     if (buffer.startsWith(QLatin1String("PWD/")) ||
175             buffer.startsWith(QLatin1String("PWD\\"))) {
176         if (beginPos > 0 && block.mid(beginPos - 1, 2) == QLatin1String("$$")) {
177             beginPos -=2;
178             buffer = buffer.mid(4);
179         }
180     }
181
182     QDir dir(QFileInfo(file()->fileName()).absolutePath());
183     QString fileName = dir.filePath(buffer);
184     QFileInfo fi(fileName);
185     if (fi.exists()) {
186         if (fi.isDir()) {
187             QDir subDir(fi.absoluteFilePath());
188             QString subProject = subDir.filePath(subDir.dirName() + QLatin1String(".pro"));
189             if (QFileInfo(subProject).exists())
190                 fileName = subProject;
191             else
192                 return link;
193         }
194         link.fileName = fileName;
195         link.begin = cursor.position() - positionInBlock + beginPos + 1;
196         link.end = cursor.position() - positionInBlock + endPos;
197     }
198     return link;
199 }
200
201 TextEditor::BaseTextEditorEditable *ProFileEditor::createEditableInterface()
202 {
203     return new ProFileEditorEditable(this);
204 }
205
206 void ProFileEditor::contextMenuEvent(QContextMenuEvent *e)
207 {
208     QMenu *menu = new QMenu();
209
210     Core::ActionManager *am = Core::ICore::instance()->actionManager();
211     Core::ActionContainer *mcontext = am->actionContainer(Qt4ProjectManager::Constants::M_CONTEXT);
212     QMenu *contextMenu = mcontext->menu();
213
214     foreach (QAction *action, contextMenu->actions())
215         menu->addAction(action);
216
217     appendStandardContextMenuActions(menu);
218
219     menu->exec(e->globalPos());
220     delete menu;
221 }
222
223 void ProFileEditor::setFontSettings(const TextEditor::FontSettings &fs)
224 {
225     TextEditor::BaseTextEditor::setFontSettings(fs);
226     ProFileHighlighter *highlighter = qobject_cast<ProFileHighlighter*>(baseTextDocument()->syntaxHighlighter());
227     if (!highlighter)
228         return;
229
230     static QVector<QString> categories;
231     if (categories.isEmpty()) {
232         categories << QLatin1String(TextEditor::Constants::C_TYPE)
233                    << QLatin1String(TextEditor::Constants::C_KEYWORD)
234                    << QLatin1String(TextEditor::Constants::C_COMMENT)
235                    << QLatin1String(TextEditor::Constants::C_VISUAL_WHITESPACE);
236     }
237
238     const QVector<QTextCharFormat> formats = fs.toTextCharFormats(categories);
239     highlighter->setFormats(formats.constBegin(), formats.constEnd());
240     highlighter->rehighlight();
241 }
242
243 void ProFileEditor::addLibrary()
244 {
245     AddLibraryWizard wizard(file()->fileName(), this);
246     if (wizard.exec() != QDialog::Accepted)
247         return;
248
249     TextEditor::BaseTextEditorEditable *editable = editableInterface();
250     const int endOfDoc = editable->position(TextEditor::ITextEditor::EndOfDoc);
251     editable->setCurPos(endOfDoc);
252     QString snippet = wizard.snippet();
253
254     // add extra \n in case the last line is not empty
255     int line, column;
256     editable->convertPosition(endOfDoc, &line, &column);
257     if (!editable->textAt(endOfDoc - column, column).simplified().isEmpty())
258         snippet = QLatin1Char('\n') + snippet;
259
260     editable->insert(snippet);
261 }
262
263 void ProFileEditor::jumpToFile()
264 {
265     openLink(findLinkAt(textCursor()));
266 }
267
268 //
269 // ProFileDocument
270 //
271
272 ProFileDocument::ProFileDocument()
273         : TextEditor::BaseTextDocument()
274 {
275 }
276
277 QString ProFileDocument::defaultPath() const
278 {
279     QFileInfo fi(fileName());
280     return fi.absolutePath();
281 }
282
283 QString ProFileDocument::suggestedFileName() const
284 {
285     QFileInfo fi(fileName());
286     return fi.fileName();
287 }