]> git.cworth.org Git - apitrace/commitdiff
Add an option to indent the glsl code in the shader viewer.
authorZack Rusin <zack@kde.org>
Sat, 24 Sep 2011 23:39:59 +0000 (19:39 -0400)
committerZack Rusin <zack@kde.org>
Sat, 24 Sep 2011 23:39:59 +0000 (19:39 -0400)
Requires 'astyle' but works very well and makes reading wonky
shaders a /lot/ easier.

gui/glsledit.cpp
gui/glsledit.h

index 5c77882e65122e0b7008c327cbfe9e874fb70902..f0e461383ea733f3b391c328a336e3f20b008bb3 100644 (file)
@@ -961,4 +961,44 @@ void GLSLEdit::mark(const QString &str, Qt::CaseSensitivity sens)
     d_ptr->highlighter->mark(str, sens);
 }
 
+void GLSLEdit::indent()
+{
+    QTemporaryFile file(QLatin1String("shader.glsl"));
+    if (!file.open()) {
+        qDebug()<<"Couldn't create temporary file "<<file.fileName();
+        return;
+    }
+    file.write(toPlainText().toUtf8());
+    file.flush();
+
+    QString tempFileName =
+            QDir::toNativeSeparators(QFileInfo(file).canonicalFilePath());
+
+    QProcess astyle;
+    astyle.setStandardInputFile(tempFileName);
+    astyle.start("astyle");
+    if (!astyle.waitForStarted()) {
+        qDebug()<<"Couldn't start the 'astyle' process!";
+        return;
+    }
+
+    if (!astyle.waitForFinished()) {
+        qDebug()<<"Couldn't finish the 'astyle' process";
+        return;
+    }
+
+    QByteArray result = astyle.readAll();
+    setPlainText(QString::fromUtf8(result));
+}
+
+void GLSLEdit::contextMenuEvent(QContextMenuEvent *e)
+{
+    QMenu *menu = createStandardContextMenu();
+
+    menu->addAction(tr("Indent Code"), this, SLOT(indent()));
+
+    menu->exec(e->globalPos());
+    delete menu;
+}
+
 #include "glsledit.moc"
index e4412a26a36706121b43fa6ed850fe03d2fab775..1fc452a18e501edf6c8784dc4d6b477cb1c90d08 100644 (file)
@@ -89,10 +89,13 @@ public slots:
     void fold(int line);
     void unfold(int line);
     void toggleFold(int line);
+    void indent();
 
 protected:
-    void resizeEvent(QResizeEvent *e);
-    void wheelEvent(QWheelEvent *e);
+    virtual void resizeEvent(QResizeEvent *e);
+    virtual void wheelEvent(QWheelEvent *e);
+    virtual void contextMenuEvent(QContextMenuEvent *e);
+
 
 private slots:
     void updateCursor();