QCodeEdit  2.2
Tutorial
[Previous:License] [Next:Examples]

Sumarry :

  1. Installation
  2. Getting started
  3. Examples

Installation

Installing QCodeEdit is really simple.

Building from sources

-# Make sure you have Qt4 installed and a supported compiler
-# Get %QCodeEdit sources : http://sourceforge.net/projects/edyuk/files
-# Uncompress the archive into the directory of your choice
-# Step into that directory and open a shell and type :\verbatim $ qmake && make 
  1. Once the compilation is finished type :
    $ su -c "make install" 

Windows : Using the binary installer

-# Make sure you have Qt4 installed
-# Get %QCodeEdit windows installer : http://sourceforge.net/projects/edyuk/files
-# Run it

Getting started

QCodeEdit API has been modelled after that of QTextEdit/QTextDocument so porting should be relatively easy.

The demo application gives a quick overview of what comes "for free" with QCodeEdit and how to set it up quickly. Some explanations are given below.

For those interested in using some more advanced features of QCodeEdit, the examples section awaits you.

Above all, you need to configure your project to use QCodeEdit. There are several possible ways of doing that :

  • Embedding QCodeEdit code into your application : simply copy the lib directory where you want and add a proper include directive inside your project (probably something like : include(lib/lib.pri) )
  • Using the installed QCodeEdit library : simply add CONFIG += qcodeedit to your project and qmake will take care of everything, provided QCodeEdit has been installed properly beforehand
  • Tweaking your project files by hand : if you choose this way you probably don't need any advice on the topic. Just inspect the qcodeedit.prf file to figure out what "tweaks" are required.

Here is the code, taken from the demo application, used to setup QCodeEdit :

m_formats = new QFormatScheme("qxs/formats.qxf", this);
QLineMarksInfoCenter::instance()->loadMarkTypes("qxs/marks.qxm");
m_languages = new QLanguageFactory(m_formats, this);
m_languages->addDefinitionPath("qxs");

It is quite simple really. First we load a format scheme from a file and set it as the default format scheme used by documents to draw themselves when no other is provided.

It used to be necessary when the format scheme was common to all languages. However, in newer version of QCodeEdit (starting with 2.2) this is only a recommended operation to prevent highlighting from being useless when some languages do not come with their own format scheme.

Then we load the line marks definition. Lines marks can be customized in any way you like through the use of such a file or even directly using the various methods provided by QLineMarksInfoCenter. Again, this is not required but highly recommended, if you want to take advantage of QCodeEdit features.

Finally we create a language factory and feed it with a path to search for language definitions. This language factory can later be used to easily adjust the language definition of a document / editor. All the files recognized as language definitions within the given path will be loaded and support for a set of languages will be added automagically :)

Now, lets create a neat widget with which the user will be able to actually edit some text :

m_editControl = new QCodeEdit(this);
m_editControl
->addPanel("Line Mark Panel", QCodeEdit::West, true)
->setShortcut(QKeySequence("F6"));
m_editControl
->addPanel("Line Number Panel", QCodeEdit::West, true)
->setShortcut(QKeySequence("F11"));
m_editControl
->addPanel("Fold Panel", QCodeEdit::West, true)
->setShortcut(QKeySequence("F9"));
m_editControl
->addPanel("Line Change Panel", QCodeEdit::West, true)
m_editControl
->addPanel("Status Panel", QCodeEdit::South, true);
m_editControl
->addPanel("Search Replace Panel", QCodeEdit::South);

So creating a managed editor only takes one line. All the rest is just about adding fancy panels to make the editor more user/friendly.

As you can see the QCodeEdit::addPanel() method returns a QAction object which behaves in the exact same way the toggle view action of a QDockWidget do : un/check it to hide/show the panel.

Some of these toggle view actions are given shortcuts to make it easier to show them. The boolean argument passed to addPanel() specifies whether the toggle view action should be added to the context menu of the editor.

If you think it takes too much space you can also specify the panel layout in its serialized form. The code below has the exact same effect as the one above except that it does not bother creating actions, let alone setting shortcuts.

m_editControl = new QCodeEdit(
"0{Line Mark Panel, Line Number Panel, Fold Panel, Line Change Panel}"
"2{Status Panel, Search Replace Panel}",
this
);

Ok, real good, but we still haven't loaded a file... Here's how to proceed :

m_languages->setLanguage(m_editControl->editor(), filename);
m_editControl->editor()->load(filename);

The first step is to set the proper language definition for the editor. This is achieved by a single call to the language factory we created earlier. We pass it the filename but a simple suffix or even a language name would work just as well.

At last, we load the content of the file into the editor. The user can now edit it (once the widget will be displayed of course...)

[Previous:License] [Next:Examples]