Qutepart’s Documentation

Qutepart is a code editor widget for C++/Qt5. It doesn’t have any external dependencies.

Qutepart highlightes your code and implements many advanced editor features.

The project has a sibling implemented in Python. See Python Qutepart

Features

All features are configurable

  • Syntax highlighting for 196 languages

  • Smart indentation algorithms:
    • Generic

    • Python

    • Ruby

    • XML

    • Lisp

    • Scheme

    • C, C++

    • JavaScript

    • Java

    • PHP

    • Go

    • And many others

  • Line numbers

  • Bracket highlighting

  • Visible witespaces

  • Marker for too long lines

  • Bookmarks

  • Current line highlighting

  • Advanced edit operations:
    • Delete selection or line

    • Duplicate selection or line

    • Move selected lines up or down

    • Copy-paste current or selected lines

    • Join lines

Basic usage

#include "qutepart.h"

Qutepart::Qutepart qutepart;

Qutepart::LangInfo langInfo = Qutepart::chooseLanguage(
    QString::null, QString::null, filePath);
if (langInfo.isValid()) {
    qutepart.setHighlighter(langInfo.id);
    qutepart.setIndentAlgorithm(langInfo.indentAlg);
}

qutepart.setPlainText("int foo(int bar)\n{}");

qutepart.show()

Detecting programming language

The function Qutepart::chooseLanguage( ) detects syntax using next parameters. Later LangInfo can be used to apply syntax highlighter and indenter.

LangInfo Qutepart::chooseLanguage(const QString &mimeType = QString::null, const QString &languageName = QString::null, const QString &sourceFilePath = QString::null, const QString &firstLine = QString::null)

Choose language by available parameters. First parameters have higher priority. Returns QString::null if can not detect the language.

Fill as much parameters as you can. Set QString::null for unknown parameters.

Parameters
  • mimeType: The file MIME type. i.e. text/html

  • languageName: The language name as written in the language DB

  • sourceFilePath: The path to the file which is edited.

  • firstLine: Contents of the first line of the file which is going to be edited.

struct Qutepart::LangInfo

Programming language ID and related information.

This structure is returned by chooseLanguage()

Public Functions

bool isValid() const

Check if the struct is valid (filled with meaningfull info)

Public Members

QString id

Internal unique language ID. Pass to Qutepart::Qutepart::setHighlighter()

QStringList names

User readable language names.

IndentAlg indentAlg

Indenter algorithm for the language. Pass to Qutepart::Qutepart::setIndentAlgorithm()

Smart indentation

Qutepart supports smart indentation algorithms for many languages.

enum Qutepart::IndentAlg

Indentation algorithm. Returned by chooseLanguage().

Passed to Qutepart::Qutepart::setIndentAlgorithm()

Values:

enumerator INDENT_ALG_NONE

Do not apply any algorithm. Default text editor behaviour.

enumerator INDENT_ALG_NORMAL

Insert to new lines indentation equal to previous line.

enumerator INDENT_ALG_CSTYLE

Algorithm for C-style languages where curly brackets are used to mark code blocks. C, C++, PHP, Java, JS, …

enumerator INDENT_ALG_LISP

Lisp indentation.

enumerator INDENT_ALG_SCHEME

Scheme indentation.

enumerator INDENT_ALG_XML

XML indentation.

enumerator INDENT_ALG_PYTHON

Python indentation.

enumerator INDENT_ALG_RUBY

Ruby indentation.

Accessing document lines

Text of the edited document is accessible with method toPlainText(), but the method is quite heavy-weight, do not use it unless you need whole file contents.

Individual lines can be accessed using QTextDocument, QTextCursor, QTextBlock files, but this API is quite low-level. Qutepart provides simpler API which can be used to do many basic operations with the text.

Printing lines one by one:

Qutepart::Qutepart qutepart;
qutepart.setPlainText(fileContents);
Qutepart::Lines lines;

for (Qutepart::Line line: lines) {
    qDebug() << line.text();
}

Inserting new line before the last line

int lastLineIndex = lines.count() - 1;
lines.insertAt(lastLineIndex - 1, "New line text");
class Qutepart::Lines

A convenience class which provides high level interface to work with the document lines.

Returned by Qutepart::Qutepart::lines()

Lines is a performance-effective document representation. Getting whole text of document with QPlainTextEdit::toPlainText()` requires a lot of memory allocations and copying. This class accesses the text line by line without copying whole document.

Public Functions

int count() const

Line count in the document.

Line at(int index) const

Get line by index.

LineIterator begin()

begin() method for STL iteration support

LineIterator end()

end() method for STL iteration support

Line first() const

First line of the document.

Line last() const

Last line of the document.

void append(const QString &lineText)

Append line to the end of the document.

class Qutepart::Line

Document line.

A convenience class to programmatically edit the document

Public Functions

QString text() const

Get line text.

int length() const

Get line length not including EOL symbol.

void remove(int pos, int count)

Remove the line from the document.

class LineIterator

STL-compatible iterator implementation to work with document lines (blocks)

Returns Qutepart::Line objects

Cursor position

struct Qutepart::TextCursorPosition

Cursor position

A convenience class, which is more friendly than low level QTextCursor API.

Returned by Qutepart::Qutepart::textCursorPosition()

Public Members

int line

Current line. First line is 0.

int column

Current column. First column is 0.

Atomic operations

It is often necessary to make multiple changes in a file which can be undo/redo as a single operation. Use helper class AtomicEditOperation.

class AtomicEditOperation

A helper class which allows to group edit operations on Qutepart using RAII approach. Operations are undo-redoble as a single change. Example:

{
    AtomicEditOperation op(qutepart);
    qutepart.lines().insertAt(3, "line three");
    qutepart.lines().insertAt(4, "line four");
}

Whole Qutepart class API

The widget is based on QPlainTextEdit. Read parent class documentation for general understanding how it works. Quteparts own methods provide additional features and convenience APIs.

class Qutepart::Qutepart : public QPlainTextEdit

Code editor widget

Public Functions

Qutepart(QWidget *parent = nullptr, const QString &text = QString::null)
Qutepart(const Qutepart&) = delete
Qutepart &operator=(const Qutepart&) = delete
Qutepart(Qutepart&&) = delete
Qutepart &operator=(Qutepart&&) = delete
~Qutepart()
Lines lines() const

High-performance access to document lines. See Qutepart::Lines.

void setHighlighter(const QString &languageId)

Set highlighter. Use Qutepart::chooseLanguage() to choose the language

Parameters

void setIndentAlgorithm(IndentAlg indentAlg)

Set indenter algorithm. Use Qutepart::chooseLanguage() to choose the algorithm.

Parameters

TextCursorPosition textCursorPosition() const

Convenience method to get text cursor position.

void goTo(int line, int column = 0)

Go to specified line and column. First line and first column have index 0.

void goTo(const TextCursorPosition &pos)

Go to text position specified by Qutepart::TextCursorPosition.

void autoIndentCurrentLine()

Indent current line using current smart indentation algorithm.

bool indentUseTabs() const

Use Tabs instead of spaces for indentation.

void setIndentUseTabs(bool)

Use Tabs instead of spaces for indentation.

int indentWidth() const

Indentation width. Count of inserted spaces, Tab symbol display width.

void setIndentWidth(int)

Indentation width. Count of inserted spaces, Tab symbol display width.

bool drawIndentations() const

Visual option. Draw indentation symbols.

void setDrawIndentations(bool)

Visual option. Draw indentation symbols.

bool drawAnyWhitespace() const

Visual option. Draw any whitespace symbol.

void setDrawAnyWhitespace(bool)

Visual option. Draw any whitespace symbol.

bool drawIncorrectIndentation() const

Visual option. Draw incorrent indentation. i.e. at end of line or Tab after spaces.

void setDrawIncorrectIndentation(bool)

Visual option. Draw incorrent indentation. i.e. at end of line or Tab after spaces.

bool drawSolidEdge() const

Visual option. Draw solid line length marker (usually after column 80)

void setDrawSolidEdge(bool)

Visual option. Draw solid line length marker (usually after column 80)

int lineLengthEdge() const

Visual option. Column on which line lendth marker is drawn.

void setLineLengthEdge(int)

Visual option. Column on which line lendth marker is drawn.

QColor lineLengthEdgeColor() const

Visual option. Color of line lendth edge.

void setLineLengthEdgeColor(QColor)

Visual option. Color of line lendth edge.

QColor currentLineColor() const

Visual option. Color of current line highlighting. QColor() if disabled.

void setCurrentLineColor(QColor)

Visual option. Color of current line highlighting. QColor() if disabled.

bool bracketHighlightingEnabled() const
void setBracketHighlightingEnabled(bool value)
bool lineNumbersVisible() const
void setLineNumbersVisible(bool value)
void setCompletionEnabled(bool)
bool completionEnabled() const
void setCompletionThreshold(int)
int completionThreshold() const
QAction *increaseIndentAction() const
QAction *decreaseIndentAction() const
QAction *toggleBookmarkAction() const
QAction *prevBookmarkAction() const
QAction *nextBookmarkAction() const
QAction *invokeCompletionAction() const
QAction *scrollDownAction() const
QAction *scrollUpAction() const
QAction *duplicateSelectionAction() const
QAction *moveLineUpAction() const
QAction *moveLineDownAction() const
QAction *deleteLineAction() const
QAction *cutLineAction() const
QAction *copyLineAction() const
QAction *pasteLineAction() const
QAction *insertLineAboveAction() const
QAction *insertLineBelowAction() const
QAction *joinLinesAction() const
QAction *zoomInAction() const

Zoom In the document by scaling fonts.

QAction *zoomOutAction() const

Zoom Out the document by scaling fonts.

void resetSelection()

Using only the syntax highlighter

In some cases it might be useful to use only syntax highligher but not other Qutepart functionality. It is possible to create a QSyntaxHighlighter subclass instance and apply it to QPlainTextEdit.

#include "qutepart.h"
#include "hl_factory.h"

Qutepart::LangInfo langInfo =
    Qutepart::chooseLanguage(QString::null, QString::null, filePath);

QPlainTextEdit textEdit;
QSyntaxHighlighter* highlighter =
    Qutepart::makeHighlighter(langInfo.id, textEdit.document());
namespace Qutepart

Functions

QSyntaxHighlighter *makeHighlighter(QObject *parent, const QString &languageId)

Choose and load a highlighter.

Set as much parameters at posiible to detect language correctly

See QSyntaxHighlighter::QSyntaxHighlighter(..) documentation.

QSyntaxHighlighter *makeHighlighter(QTextDocument *parent, const QString &langugeId)