1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2010 Hugues Delorme
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
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
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 ** 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.
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
32 **************************************************************************/
34 #include "bazaarcommitwidget.h"
35 #include "branchinfo.h"
37 #include <texteditor/texteditorsettings.h>
38 #include <texteditor/fontsettings.h>
39 #include <texteditor/texteditorconstants.h>
41 #include <QtGui/QSyntaxHighlighter>
42 #include <QtGui/QTextEdit>
44 #include <QtCore/QDebug>
45 #include <QtCore/QRegExp>
47 //see the git submit widget for details of the syntax Highlighter
49 //TODO Check to see when the Highlighter has been moved to a base class and use that instead
54 // Retrieve the comment char format from the text editor.
55 static QTextCharFormat commentFormat()
57 const TextEditor::FontSettings settings = TextEditor::TextEditorSettings::instance()->fontSettings();
58 return settings.toTextCharFormat(QLatin1String(TextEditor::Constants::C_COMMENT));
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
67 explicit BazaarSubmitHighlighter(QTextEdit *parent);
68 virtual void highlightBlock(const QString &text);
71 enum State { Header, Comment, Other };
72 const QTextCharFormat m_commentFormat;
73 const QRegExp m_keywordPattern;
74 const QChar m_hashChar;
77 BazaarSubmitHighlighter::BazaarSubmitHighlighter(QTextEdit * parent) :
78 QSyntaxHighlighter(parent),
79 m_commentFormat(commentFormat()),
80 m_keywordPattern(QLatin1String("^\\w+:")),
81 m_hashChar(QLatin1Char('#'))
83 Q_ASSERT(m_keywordPattern.isValid());
86 void BazaarSubmitHighlighter::highlightBlock(const QString &text)
88 // figure out current state
90 const QTextBlock block = currentBlock();
91 if (block.position() == 0) {
94 if (text.startsWith(m_hashChar))
100 QTextCharFormat charFormat = format(0);
101 charFormat.setFontWeight(QFont::Bold);
102 setFormat(0, text.size(), charFormat);
106 setFormat(0, text.size(), m_commentFormat);
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);
120 BazaarCommitWidget::BazaarCommitWidget(QWidget *parent) :
121 Utils::SubmitEditorWidget(parent),
122 m_bazaarCommitPanel(new QWidget)
124 m_bazaarCommitPanelUi.setupUi(m_bazaarCommitPanel);
125 insertTopWidget(m_bazaarCommitPanel);
126 new BazaarSubmitHighlighter(descriptionEdit());
129 void BazaarCommitWidget::setFields(const BranchInfo &branch,
130 const QString &userName, const QString &email)
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);
138 QString BazaarCommitWidget::committer() const
140 const QString author = m_bazaarCommitPanelUi.authorLineEdit->text();
141 const QString email = m_bazaarCommitPanelUi.emailLineEdit->text();
142 if (author.isEmpty())
145 QString user = author;
146 if (!email.isEmpty()) {
147 user += QLatin1String(" <");
149 user += QLatin1Char('>');
154 QStringList BazaarCommitWidget::fixedBugs() const
156 return m_bazaarCommitPanelUi.fixedBugsLineEdit->text().split(QRegExp("\\s+"));
159 bool BazaarCommitWidget::isLocalOptionEnabled() const
161 return m_bazaarCommitPanelUi.isLocalCheckBox->isChecked();
164 } // namespace Internal
165 } // namespace Bazaar