OSDN Git Service

b6890b50f8dcc2dec74d70acede2bc6c5df52fa6
[qt-creator-jp/qt-creator-jp.git] / src / plugins / bazaar / bazaarcommitwidget.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2010 Hugues Delorme
6 **
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
8 **
9 ** No Commercial Usage
10 **
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
14 ** this package.
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 ** In addition, as a special exception, Nokia gives you certain additional
26 ** rights.  These rights are described in the Nokia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
31 **
32 **************************************************************************/
33
34 #include "bazaarcommitwidget.h"
35 #include "branchinfo.h"
36
37 #include <texteditor/texteditorsettings.h>
38 #include <texteditor/fontsettings.h>
39 #include <texteditor/texteditorconstants.h>
40
41 #include <QtGui/QSyntaxHighlighter>
42 #include <QtGui/QTextEdit>
43
44 #include <QtCore/QDebug>
45 #include <QtCore/QRegExp>
46
47 //see the git submit widget for details of the syntax Highlighter
48
49 //TODO Check to see when the Highlighter has been moved to a base class and use that instead
50
51 namespace Bazaar {
52 namespace Internal {
53
54 // Retrieve the comment char format from the text editor.
55 static QTextCharFormat commentFormat()
56 {
57     const TextEditor::FontSettings settings = TextEditor::TextEditorSettings::instance()->fontSettings();
58     return settings.toTextCharFormat(QLatin1String(TextEditor::Constants::C_COMMENT));
59 }
60
61 // Highlighter for Bazaar submit messages. Make the first line bold, indicates
62 // comments as such (retrieving the format from the text editor) and marks up
63 // keywords (words in front of a colon as in 'Task: <bla>').
64 class BazaarSubmitHighlighter : QSyntaxHighlighter
65 {
66 public:
67     explicit BazaarSubmitHighlighter(QTextEdit *parent);
68     virtual void highlightBlock(const QString &text);
69
70 private:
71     enum State { Header, Comment, Other };
72     const QTextCharFormat m_commentFormat;
73     const QRegExp m_keywordPattern;
74     const QChar m_hashChar;
75 };
76
77 BazaarSubmitHighlighter::BazaarSubmitHighlighter(QTextEdit * parent) :
78     QSyntaxHighlighter(parent),
79     m_commentFormat(commentFormat()),
80     m_keywordPattern(QLatin1String("^\\w+:")),
81     m_hashChar(QLatin1Char('#'))
82 {
83     Q_ASSERT(m_keywordPattern.isValid());
84 }
85
86 void BazaarSubmitHighlighter::highlightBlock(const QString &text)
87 {
88     // figure out current state
89     State state = Other;
90     const QTextBlock block = currentBlock();
91     if (block.position() == 0) {
92         state = Header;
93     } else {
94         if (text.startsWith(m_hashChar))
95             state = Comment;
96     }
97     // Apply format.
98     switch (state) {
99     case Header: {
100         QTextCharFormat charFormat = format(0);
101         charFormat.setFontWeight(QFont::Bold);
102         setFormat(0, text.size(), charFormat);
103     }
104     break;
105     case Comment:
106         setFormat(0, text.size(), m_commentFormat);
107         break;
108     case Other:
109         // Format key words ("Task:") italic
110         if (m_keywordPattern.indexIn(text, 0, QRegExp::CaretAtZero) == 0) {
111             QTextCharFormat charFormat = format(0);
112             charFormat.setFontItalic(true);
113             setFormat(0, m_keywordPattern.matchedLength(), charFormat);
114         }
115         break;
116     }
117 }
118
119
120 BazaarCommitWidget::BazaarCommitWidget(QWidget *parent) :
121     Utils::SubmitEditorWidget(parent),
122     m_bazaarCommitPanel(new QWidget)
123 {
124     m_bazaarCommitPanelUi.setupUi(m_bazaarCommitPanel);
125     insertTopWidget(m_bazaarCommitPanel);
126     new BazaarSubmitHighlighter(descriptionEdit());
127 }
128
129 void BazaarCommitWidget::setFields(const BranchInfo &branch,
130                                    const QString &userName, const QString &email)
131 {
132     m_bazaarCommitPanelUi.branchLineEdit->setText(branch.branchLocation);
133     m_bazaarCommitPanelUi.isLocalCheckBox->setVisible(branch.isBoundToBranch);
134     m_bazaarCommitPanelUi.authorLineEdit->setText(userName);
135     m_bazaarCommitPanelUi.emailLineEdit->setText(email);
136 }
137
138 QString BazaarCommitWidget::committer() const
139 {
140     const QString author = m_bazaarCommitPanelUi.authorLineEdit->text();
141     const QString email = m_bazaarCommitPanelUi.emailLineEdit->text();
142     if (author.isEmpty())
143         return QString();
144
145     QString user = author;
146     if (!email.isEmpty()) {
147         user += QLatin1String(" <");
148         user += email;
149         user += QLatin1Char('>');
150     }
151     return user;
152 }
153
154 QStringList BazaarCommitWidget::fixedBugs() const
155 {
156     return m_bazaarCommitPanelUi.fixedBugsLineEdit->text().split(QRegExp("\\s+"));
157 }
158
159 bool BazaarCommitWidget::isLocalOptionEnabled() const
160 {
161     return m_bazaarCommitPanelUi.isLocalCheckBox->isChecked();
162 }
163
164 } // namespace Internal
165 } // namespace Bazaar