From f6184092941e4357a46c087368562f156c3a587b Mon Sep 17 00:00:00 2001
From: Alexey Kutumov <pixel@cruel-machine.fooking-weendoz>
Date: Sun, 13 May 2012 18:09:59 +0700
Subject: [PATCH] Added mechanism for parsing Doxygen keywords.
 Also added pre-defined settings for doxygen keywords

---
 src/plugins/todo/constants.h       |    7 ++++
 src/plugins/todo/keyword.cpp       |   16 ++++++++-
 src/plugins/todo/keyword.h         |    8 +++++
 src/plugins/todo/keyworddialog.cpp |    7 ++++
 src/plugins/todo/keyworddialog.ui  |    9 +++++-
 src/plugins/todo/lineparser.cpp    |    3 +-
 src/plugins/todo/optionsdialog.cpp |   47 ++++++++++++++++-----------
 src/plugins/todo/optionsdialog.h   |    4 ++
 src/plugins/todo/settings.cpp      |   61 +++++++++++++++++++++++++++++++++---
 src/plugins/todo/settings.h        |    5 +++
 10 files changed, 138 insertions(+), 29 deletions(-)

diff --git a/src/plugins/todo/constants.h b/src/plugins/todo/constants.h
index 983e1a7..784bfa0 100644
--- a/src/plugins/todo/constants.h
+++ b/src/plugins/todo/constants.h
@@ -35,6 +35,7 @@
 #define CONSTANTS_H
 
 #include <QtGlobal>
+#include <Qt>
 
 namespace Todo {
 namespace Constants {
@@ -69,6 +70,12 @@ const char SCANNING_SCOPE[] = "ScanningScope";
 const char ITEMS_DISPLAY_PLACE[] = "ItemsDisplayPlace";
 const char KEYWORDS_LIST[] = "Keywords";
 
+
+enum KeywordRoles {
+    IconResourceRole = Qt::UserRole,
+    KeywordStyleRole
+};
+
 // TODO Output TreeWidget columns
 enum OutputColumnIndex {
     OUTPUT_COLUMN_TEXT,
diff --git a/src/plugins/todo/keyword.cpp b/src/plugins/todo/keyword.cpp
index e37f965..cbcc49c 100644
--- a/src/plugins/todo/keyword.cpp
+++ b/src/plugins/todo/keyword.cpp
@@ -36,7 +36,7 @@
 namespace Todo {
 namespace Internal {
 
-Keyword::Keyword() : color(Qt::white)
+Keyword::Keyword() : color(Qt::white), style(DefaultKeywordStyle)
 {
 }
 
@@ -44,7 +44,19 @@ bool Keyword::equals(const Keyword &other) const
 {
     return (this->name == other.name)
         && (this->iconResource == other.iconResource)
-        && (this->color == other.color);
+        && (this->color == other.color)
+        && (this->style == other.style);
+}
+
+
+QString Keyword::searchingText() const
+{
+    if (style == DefaultKeywordStyle)
+        return name + QLatin1Char(':');
+    else {
+        Q_ASSERT(style == DoxygenKeywordStyle && "keywordStyle is not properly configured");
+        return QLatin1Char('@') + name.toLower();
+    }
 }
 
 bool operator ==(Keyword &k1, Keyword &k2)
diff --git a/src/plugins/todo/keyword.h b/src/plugins/todo/keyword.h
index 48533de..63c4880 100644
--- a/src/plugins/todo/keyword.h
+++ b/src/plugins/todo/keyword.h
@@ -42,6 +42,12 @@
 namespace Todo {
 namespace Internal {
 
+enum KeywordStyle {
+    DefaultKeywordStyle,
+    DoxygenKeywordStyle
+};
+
+
 class Keyword
 {
 public:
@@ -50,7 +56,9 @@ public:
     QString name;
     QString iconResource;
     QColor color;
+    KeywordStyle style;
     bool equals(const Keyword &other) const;
+    QString searchingText() const;
 };
 
 typedef QList<Keyword> KeywordList;
diff --git a/src/plugins/todo/keyworddialog.cpp b/src/plugins/todo/keyworddialog.cpp
index 898ccdd..bff0163 100644
--- a/src/plugins/todo/keyworddialog.cpp
+++ b/src/plugins/todo/keyworddialog.cpp
@@ -49,6 +49,10 @@ KeywordDialog::KeywordDialog(const Keyword &keyword, QWidget *parent) :
     setupListWidget(keyword.iconResource);
     setupColorWidgets(keyword.color);
     ui->keywordNameEdit->setText(keyword.name);
+    if (keyword.style == DoxygenKeywordStyle)
+        ui->doxygenStyleCheckBox->setCheckState(Qt::Checked);
+    else
+        ui->doxygenStyleCheckBox->setCheckState(Qt::Unchecked);
 }
 
 KeywordDialog::~KeywordDialog()
@@ -62,6 +66,9 @@ Keyword KeywordDialog::keyword()
     result.name = ui->keywordNameEdit->text();
     result.iconResource = ui->listWidget->currentItem()->data(Qt::UserRole).toString();
     result.color = ui->colorEdit->text();
+    result.style = (ui->doxygenStyleCheckBox->checkState() == Qt::Checked)
+        ? DoxygenKeywordStyle
+        : DefaultKeywordStyle;
 
     return result;
 }
diff --git a/src/plugins/todo/keyworddialog.ui b/src/plugins/todo/keyworddialog.ui
index 8d73589..60abdc8 100644
--- a/src/plugins/todo/keyworddialog.ui
+++ b/src/plugins/todo/keyworddialog.ui
@@ -68,10 +68,17 @@
          <property name="title">
           <string>Keyword</string>
          </property>
-         <layout class="QHBoxLayout" name="horizontalLayout">
+         <layout class="QVBoxLayout" name="verticalLayout_2">
           <item>
            <widget class="QLineEdit" name="keywordNameEdit"/>
           </item>
+          <item>
+           <widget class="QCheckBox" name="doxygenStyleCheckBox">
+            <property name="text">
+             <string>Doxygen keyword</string>
+            </property>
+           </widget>
+          </item>
          </layout>
         </widget>
        </item>
diff --git a/src/plugins/todo/lineparser.cpp b/src/plugins/todo/lineparser.cpp
index 84dad94..bcfc76d 100644
--- a/src/plugins/todo/lineparser.cpp
+++ b/src/plugins/todo/lineparser.cpp
@@ -66,8 +66,7 @@ LineParser::KeywordEntryCandidates LineParser::findKeywordEntryCandidates(const
     for (int i = 0; i < m_keywords.count(); ++i) {
         int searchFrom = -1;
         forever {
-            const int index = line.lastIndexOf(m_keywords.at(i).name
-                                               + QLatin1Char(':'), searchFrom);
+            const int index = line.lastIndexOf(m_keywords.at(i).searchingText(), searchFrom);
 
             if (index == -1)
                 break; // 'forever' loop exit condition
diff --git a/src/plugins/todo/optionsdialog.cpp b/src/plugins/todo/optionsdialog.cpp
index e643296..106a635 100644
--- a/src/plugins/todo/optionsdialog.cpp
+++ b/src/plugins/todo/optionsdialog.cpp
@@ -63,11 +63,33 @@ void OptionsDialog::setSettings(const Settings &settings)
     uiFromSettings(settings);
 }
 
-void OptionsDialog::addToKeywordsList(const Keyword &keyword)
+
+void OptionsDialog::itemFromKeyword(const Keyword &keyword, QListWidgetItem *item)
 {
-    QListWidgetItem *item = new QListWidgetItem(QIcon(keyword.iconResource), keyword.name);
-    item->setData(Qt::UserRole, keyword.iconResource);
+    item->setIcon(QIcon(keyword.iconResource));
+    item->setText(keyword.name);
+    item->setData(Constants::IconResourceRole, keyword.iconResource);
+    item->setData(Constants::KeywordStyleRole, keyword.style);
     item->setBackgroundColor(keyword.color);
+}
+
+
+Keyword OptionsDialog::keywordFromItem(const QListWidgetItem *item)
+{
+    Keyword keyword;
+
+    keyword.name = item->text();
+    keyword.iconResource = item->data(Constants::IconResourceRole).toString();
+    keyword.color = item->backgroundColor();
+    keyword.style = static_cast<KeywordStyle>(item->data(Constants::KeywordStyleRole).toInt());
+
+    return keyword;
+}
+
+void OptionsDialog::addToKeywordsList(const Keyword &keyword)
+{
+    QListWidgetItem *item = new QListWidgetItem;
+    itemFromKeyword(keyword, item);
     ui->keywordsList->addItem(item);
 }
 
@@ -90,18 +112,11 @@ void OptionsDialog::editButtonClicked()
 {
     QListWidgetItem *item = ui->keywordsList->currentItem();
 
-    Keyword keyword;
-    keyword.name = item->text();
-    keyword.iconResource = item->data(Qt::UserRole).toString();
-    keyword.color = item->backgroundColor();
-
+    Keyword keyword = keywordFromItem(item);
     KeywordDialog *addKeywordDialog = new KeywordDialog(keyword, this);
     if (addKeywordDialog->exec() == QDialog::Accepted) {
         keyword = addKeywordDialog->keyword();
-        item->setIcon(QIcon(keyword.iconResource));
-        item->setText(keyword.name);
-        item->setData(Qt::UserRole, keyword.iconResource);
-        item->setBackgroundColor(keyword.color);
+        itemFromKeyword(keyword, item);
     }
 }
 
@@ -146,13 +161,7 @@ void OptionsDialog::setButtonsEnabled()
      settings.keywords.clear();
      for (int i = 0; i < ui->keywordsList->count(); ++i) {
          QListWidgetItem *item = ui->keywordsList->item(i);
-
-         Keyword keyword;
-         keyword.name = item->text();
-         keyword.iconResource = item->data(Qt::UserRole).toString();
-         keyword.color = item->backgroundColor();
-
-         settings.keywords << keyword;
+         settings.keywords << keywordFromItem(item);
      }
 
      return settings;
diff --git a/src/plugins/todo/optionsdialog.h b/src/plugins/todo/optionsdialog.h
index 5a876e8..70789e1 100644
--- a/src/plugins/todo/optionsdialog.h
+++ b/src/plugins/todo/optionsdialog.h
@@ -36,6 +36,8 @@
 
 #include <QWidget>
 
+class QListWidgetItem;
+
 namespace Todo {
 namespace Internal {
 
@@ -67,6 +69,8 @@ private:
     void uiFromSettings(const Settings &settings);
     Settings settingsFromUi();
     void addToKeywordsList(const Keyword &keyword);
+    void itemFromKeyword(const Keyword &keyword, QListWidgetItem *item);
+    Keyword keywordFromItem(const QListWidgetItem *item);
 
     Ui::OptionsDialog *ui;
 };
diff --git a/src/plugins/todo/settings.cpp b/src/plugins/todo/settings.cpp
index c20cd22..4f8609d 100644
--- a/src/plugins/todo/settings.cpp
+++ b/src/plugins/todo/settings.cpp
@@ -49,11 +49,16 @@ void Settings::save(QSettings *settings) const
         const QString nameKey = QLatin1String("name");
         const QString colorKey = QLatin1String("color");
         const QString iconResourceKey = QLatin1String("iconResource");
+        const QString styleKey = QLatin1String("style");
+
         for (int i = 0; i < size; ++i) {
+            const Keyword & keyword = keywords.at(i);
+
             settings->setArrayIndex(i);
-            settings->setValue(nameKey, keywords.at(i).name);
-            settings->setValue(colorKey, keywords.at(i).color);
-            settings->setValue(iconResourceKey, keywords.at(i).iconResource);
+            settings->setValue(nameKey, keyword.name);
+            settings->setValue(colorKey, keyword.color);
+            settings->setValue(iconResourceKey, keyword.iconResource);
+            settings->setValue(styleKey, keyword.style);
         }
     }
     settings->endArray();
@@ -77,12 +82,16 @@ void Settings::load(QSettings *settings)
         const QString nameKey = QLatin1String("name");
         const QString colorKey = QLatin1String("color");
         const QString iconResourceKey = QLatin1String("iconResource");
+        const QString styleKey = QLatin1String("style");
+
         for (int i = 0; i < size; ++i) {
             settings->setArrayIndex(i);
             Keyword keyword;
             keyword.name = settings->value(nameKey).toString();
             keyword.color = settings->value(colorKey).value<QColor>();
             keyword.iconResource = settings->value(iconResourceKey).toString();
+            keyword.style = static_cast<KeywordStyle>(settings->value(styleKey, DefaultKeywordStyle).toInt());
+
             newKeywords << keyword;
         }
         keywords = newKeywords;
@@ -92,37 +101,80 @@ void Settings::load(QSettings *settings)
     settings->endGroup();
 }
 
+
 void Settings::setDefault()
 {
     scanningScope = ScanningScopeCurrentFile;
-
     keywords.clear();
 
+    setDefaultKeywordStyle();
+    setDoxygenKeywordStyle();
+}
+
+
+
+void Settings::setDefaultKeywordStyle()
+{
     Keyword keyword;
 
     keyword.name = QLatin1String("TODO");
     keyword.iconResource = QLatin1String(Constants::ICON_WARNING);
     keyword.color = QColor(QLatin1String(Constants::COLOR_TODO_BG));
+    keyword.style = DefaultKeywordStyle;
     keywords.append(keyword);
 
     keyword.name = QLatin1String("NOTE");
     keyword.iconResource = QLatin1String(Constants::ICON_INFO);
     keyword.color = QColor(QLatin1String(Constants::COLOR_NOTE_BG));
+    keyword.style = DefaultKeywordStyle;
     keywords.append(keyword);
 
     keyword.name = QLatin1String("FIXME");
     keyword.iconResource = QLatin1String(Constants::ICON_ERROR);
     keyword.color = QColor(QLatin1String(Constants::COLOR_FIXME_BG));
+    keyword.style = DefaultKeywordStyle;
     keywords.append(keyword);
 
     keyword.name = QLatin1String("BUG");
     keyword.iconResource = QLatin1String(Constants::ICON_ERROR);
     keyword.color = QColor(QLatin1String(Constants::COLOR_BUG_BG));
+    keyword.style = DefaultKeywordStyle;
     keywords.append(keyword);
 
     keyword.name = QLatin1String("WARNING");
     keyword.iconResource = QLatin1String(Constants::ICON_WARNING);
     keyword.color = QColor(QLatin1String(Constants::COLOR_WARNING_BG));
+    keyword.style = DefaultKeywordStyle;
+    keywords.append(keyword);
+}
+
+
+void Settings::setDoxygenKeywordStyle()
+{
+    Keyword keyword;
+
+    keyword.name = QLatin1String("todo");
+    keyword.iconResource = QLatin1String(Constants::ICON_WARNING);
+    keyword.color = QColor(QLatin1String(Constants::COLOR_TODO_BG));
+    keyword.style = DoxygenKeywordStyle;
+    keywords.append(keyword);
+
+    keyword.name = QLatin1String("note");
+    keyword.iconResource = QLatin1String(Constants::ICON_INFO);
+    keyword.color = QColor(QLatin1String(Constants::COLOR_NOTE_BG));
+    keyword.style = DoxygenKeywordStyle;
+    keywords.append(keyword);
+
+    keyword.name = QLatin1String("bug");
+    keyword.iconResource = QLatin1String(Constants::ICON_ERROR);
+    keyword.color = QColor(QLatin1String(Constants::COLOR_BUG_BG));
+    keyword.style = DoxygenKeywordStyle;
+    keywords.append(keyword);
+
+    keyword.name = QLatin1String("warning");
+    keyword.iconResource = QLatin1String(Constants::ICON_WARNING);
+    keyword.color = QColor(QLatin1String(Constants::COLOR_WARNING_BG));
+    keyword.style = DefaultKeywordStyle;
     keywords.append(keyword);
 }
 
@@ -130,7 +182,6 @@ bool Settings::equals(const Settings &other) const
 {
     return (keywords == other.keywords)
         && (scanningScope == other.scanningScope);
-
 }
 
 bool operator ==(Settings &s1, Settings &s2)
diff --git a/src/plugins/todo/settings.h b/src/plugins/todo/settings.h
index 80ce333..e120fb3 100644
--- a/src/plugins/todo/settings.h
+++ b/src/plugins/todo/settings.h
@@ -54,6 +54,11 @@ public:
     void load(QSettings *settings);
     void setDefault();
     bool equals(const Settings &other) const;
+
+
+private:
+    void setDefaultKeywordStyle();
+    void setDoxygenKeywordStyle();
 };
 
 bool operator ==(Settings &s1, Settings &s2);
-- 
1.7.2.5

