OSDN Git Service

Add the identifiers to the completion box.
authorRoberto Raggi <roberto.raggi@nokia.com>
Fri, 19 Nov 2010 14:15:12 +0000 (15:15 +0100)
committerRoberto Raggi <roberto.raggi@nokia.com>
Fri, 19 Nov 2010 14:15:49 +0000 (15:15 +0100)
Well, this is just a temporary hack :)

src/plugins/glsleditor/glslcodecompletion.cpp
src/plugins/glsleditor/glsleditor.cpp
src/plugins/glsleditor/glsleditor.h

index 359e483..79e7b15 100644 (file)
 **************************************************************************/
 #include "glslcodecompletion.h"
 #include "glsleditor.h"
+#include <QtGui/QIcon>
+#include <QtGui/QPainter>
 #include <QtCore/QDebug>
 
 using namespace GLSLEditor;
 
+// Temporary workaround until we have proper icons for QML completion items
+static QIcon iconForColor(const QColor &color)
+{
+    QPixmap pix(6, 6);
+
+    int pixSize = 20;
+    QBrush br(color);
+
+    QPixmap pm(2 * pixSize, 2 * pixSize);
+    QPainter pmp(&pm);
+    pmp.fillRect(0, 0, pixSize, pixSize, Qt::lightGray);
+    pmp.fillRect(pixSize, pixSize, pixSize, pixSize, Qt::lightGray);
+    pmp.fillRect(0, pixSize, pixSize, pixSize, Qt::darkGray);
+    pmp.fillRect(pixSize, 0, pixSize, pixSize, Qt::darkGray);
+    pmp.fillRect(0, 0, 2 * pixSize, 2 * pixSize, color);
+    br = QBrush(pm);
+
+    QPainter p(&pix);
+    int corr = 1;
+    QRect r = pix.rect().adjusted(corr, corr, -corr, -corr);
+    p.setBrushOrigin((r.width() % pixSize + pixSize) / 2 + corr, (r.height() % pixSize + pixSize) / 2 + corr);
+    p.fillRect(r, br);
+
+    p.fillRect(r.width() / 4 + corr, r.height() / 4 + corr,
+               r.width() / 2, r.height() / 2,
+               QColor(color.rgb()));
+    p.drawRect(pix.rect().adjusted(0, 0, -1, -1));
+
+    return pix;
+}
+
 static const char *glsl_keywords[] =
 { // ### TODO: get the keywords from the lexer
   "attribute",
@@ -163,9 +196,11 @@ CodeCompletion::CodeCompletion(QObject *parent)
       m_startPosition(-1),
       m_restartCompletion(false)
 {
+    const QIcon keywordIcon = iconForColor(Qt::darkYellow);
     for (const char **it = glsl_keywords; *it; ++it) {
         TextEditor::CompletionItem item(this);
         item.text = QString::fromLatin1(*it);
+        item.icon = keywordIcon;
         m_keywordCompletions.append(item);
     }
 }
@@ -207,8 +242,18 @@ int CodeCompletion::startCompletion(TextEditor::ITextEditable *editor)
     while (ch.isLetterOrNumber())
         ch = editor->characterAt(--pos);
 
+    const QIcon symbolIcon = iconForColor(Qt::darkCyan);
     m_completions += m_keywordCompletions;
 
+    if (GLSLTextEditor *ed = qobject_cast<GLSLTextEditor *>(m_editor->widget())) {
+        foreach (const QString &id, ed->identifiers()) {
+            TextEditor::CompletionItem item(this);
+            item.text = id;
+            item.icon = symbolIcon;
+            m_completions.append(item);
+        }
+    }
+
     m_startPosition = pos + 1;
     return m_startPosition;
 }
index 69e4933..586894d 100644 (file)
@@ -124,6 +124,11 @@ bool GLSLTextEditor::isOutdated() const
     return false;
 }
 
+QSet<QString> GLSLTextEditor::identifiers() const
+{
+    return m_identifiers;
+}
+
 Core::IEditor *GLSLEditorEditable::duplicate(QWidget *parent)
 {
     GLSLTextEditor *newEditor = new GLSLTextEditor(parent);
@@ -285,4 +290,7 @@ void GLSLTextEditor::updateDocumentNow()
 
     // ### process the ast
     (void) ast;
+
+    // refresh the identifiers.
+    m_identifiers = engine.identifiers();
 }
index 67da79a..f184780 100644 (file)
@@ -37,6 +37,7 @@
 #include <texteditor/quickfix.h>
 
 #include <QtCore/QSharedPointer>
+#include <QtCore/QSet>
 
 QT_BEGIN_NAMESPACE
 class QComboBox;
@@ -62,6 +63,8 @@ public:
     int editorRevision() const;
     bool isOutdated() const;
 
+    QSet<QString> identifiers() const;
+
 public slots:
     virtual void setFontSettings(const TextEditor::FontSettings &);
 
@@ -82,6 +85,7 @@ private:
 
     QTimer *m_updateDocumentTimer;
     QComboBox *m_outlineCombo;
+    QSet<QString> m_identifiers;
 };
 
 } // namespace GLSLEditor