Add UiGuiHighlighter code

Signed-off-by: Michele Calgaro <michele.calgaro@yahoo.it>
pull/3/head
Michele Calgaro 1 year ago
parent dd4c8a461d
commit 455025d23a
Signed by: MicheleC
GPG Key ID: 2A75B7CA8ADED5CF

@ -34,6 +34,7 @@ set( ${target}_SRCS
SettingsPaths.cpp
TemplateBatchScript.cpp
UiGuiErrorMessage.cpp
UiGuiHighlighter.cpp
UiGuiIniFileParser.cpp
UiGuiVersion.cpp

@ -32,7 +32,7 @@
#include "UiGuiVersion.h"
#include "ToolBarWidget.h"
///-- #include "AboutDialogGraphicsView.h"
///-- #include "UiGuiHighlighter.h"
#include "UiGuiHighlighter.h"
#include <tqaction.h>
#include <tqapplication.h>
@ -79,10 +79,11 @@ MainWindow::MainWindow(TQString file2OpenOnStart, TQWidget *parent) :
MainWindowBase(parent), m_aboutDialog(nullptr), m_qSciSourceCodeEditor(nullptr),
m_uiGuiTranslator(nullptr), m_textEditLineColumnInfoLabel(nullptr),
m_oldLinesNumber(0), m_openEncodingActions(), m_saveEncodingActions(),
m_encodingActionGroup(nullptr), m_saveEncodedActionGroup(nullptr),
m_highlighterActionGroup(nullptr), m_documentModified(false), m_previewToggled(true),
m_indentHandler(nullptr), m_centralSplitter(nullptr), m_settingsDialog(nullptr)
///- m_highlighter(nullptr), _aboutDialogGraphicsView(nullptr)
m_encodingActionGroup(nullptr), m_saveEncodedActionGroup(nullptr),
m_highlighterActionGroup(nullptr), m_documentModified(false), m_previewToggled(true),
m_indentHandler(nullptr), m_centralSplitter(nullptr), m_settingsDialog(nullptr),
m_highlighter(nullptr), m_highlightingActions()
///_aboutDialogGraphicsView(nullptr)
///- m_textEditVScrollBar(nullptr), _toolBarWidget(nullptr)
{
// Init of some variables.
@ -113,7 +114,6 @@ MainWindow::MainWindow(TQString file2OpenOnStart, TQWidget *parent) :
createEncodingMenu();
createHighlighterMenu();
// Generate about dialog box
//m_aboutDialog = new AboutDialog(this, WStyle_Splash);
m_aboutDialog = new AboutDialog(this);
@ -135,7 +135,7 @@ MainWindow::MainWindow(TQString file2OpenOnStart, TQWidget *parent) :
loadLastOpenedFile();
}
updateSourceView();
updateSyntaxHighlighting();
///-- // Enable accept dropping of files.
///-- setAcceptDrops(true);
@ -271,7 +271,7 @@ void MainWindow::initToolBar()
m_toolBarWidget = new ToolBarWidget(toolBar);
// Connect the tool bar widgets to their functions.
///-- _settings->registerObjectProperty(_toolBarWidget->cbEnableSyntaxHL, "checked",
///-- m_settings->registerObjectProperty(_toolBarWidget->cbEnableSyntaxHL, "checked",
///-- "SyntaxHighlightingEnabled");
m_toolBarWidget->cbEnableSyntaxHL->hide();
@ -312,7 +312,7 @@ void MainWindow::initTextEditor()
exit(1);
}
// Make some _settings for the TQScintilla widget.
// Make some settings for the TQScintilla widget.
m_qSciSourceCodeEditor->setUtf8(true);
m_qSciSourceCodeEditor->setMarginLineNumbers(1, true);
m_qSciSourceCodeEditor->setMarginWidth(1, TQString("10000"));
@ -351,24 +351,22 @@ void MainWindow::initTextEditor()
numberOfLinesChanged();
}
void MainWindow::updateSyntaxHighlighting()
{
turnHighlightOnOff(actionEnableSyntaxHighlighting->isOn());
}
/*
\brief Create and init the syntax m_highlighter and set it to use the TQScintilla edit component.
\brief Create and init the syntax highlighter and set it to use the TQScintilla edit component.
*/
void MainWindow::initSyntaxHighlighter()
{
///-- // Create the highlighter.
///-- m_highlighter = new UiGuiHighlighter(qSciSourceCodeEditor);
///--
///-- // Handle if syntax highlighting is enabled
///-- bool syntaxHighlightningEnabled = m_settings->getValueByName("SyntaxHighlightningEnabled").toBool();
///-- if (syntaxHighlightningEnabled)
///-- {
///-- highlighter->turnHighlightOn();
///-- }
///-- else
///-- {
///-- highlighter->turnHighlightOff();
///-- }
// Create the highlighter.
m_highlighter = new UiGuiHighlighter(m_qSciSourceCodeEditor);
// Handle if syntax highlighting is enabled
bool syntaxHighlightningEnabled = m_settings->getValueByName("SyntaxHighlightningEnabled").toBool();
turnHighlightOnOff(syntaxHighlightningEnabled);
}
/*
@ -442,7 +440,7 @@ void MainWindow::initIndenter()
m_indentHandler = new IndentHandler(m_currentIndenterID, this, m_centralSplitter);
m_centralSplitter->moveToFirst(m_indentHandler);
// If _settings for the indenter have changed, let the main window know aboud it.
// If settings for the indenter have changed, let the main window know aboud it.
connect(m_indentHandler, SIGNAL(indenterSettingsChanged()), this,
SLOT(indentSettingsChangedSlot()));
@ -451,7 +449,7 @@ void MainWindow::initIndenter()
m_previewToggled = true;
///-- // Handle if indenter parameter tool tips are enabled
///-- _settings->registerObjectProperty(actionIndenterParameterTooltipsEnabled,
///-- m_settings->registerObjectProperty(actionIndenterParameterTooltipsEnabled,
///-- "checked", "indenterParameterTooltipsEnabled");
}
@ -481,8 +479,18 @@ TQString MainWindow::loadFile(const TQString &filePath)
TQFileInfo fileInfo(filePath);
m_currentSourceFileExtension = fileInfo.extension(false);
///-- int indexOfHighlighter = _highlighter->setLexerForExtension(m_currentSourceFileExtension);
///-- m_highlighterActionGroup->actions().at(indexOfHighlighter)->setChecked(true);
int indexOfHighlighter = m_highlighter->setLexer(m_currentSourceFileExtension);
// Mark the selected highlighter in the corresponding menu
int idx = 0;
for (TQAction *action : m_highlightingActions)
{
if (idx == indexOfHighlighter)
{
action->setOn(true);
break;
}
++idx;
}
}
return fileContent;
}
@ -670,16 +678,16 @@ void MainWindow::callIndenter()
*/
void MainWindow::turnHighlightOnOff(bool turnOn)
{
///-- if (turnOn)
///-- {
///-- _highlighter->turnHighlightOn();
///-- }
///-- else
///-- {
///-- _highlighter->turnHighlightOff();
///-- }
///-- m_previewToggled = true;
///-- updateSourceView();
if (turnOn)
{
m_highlighter->turnHighlightOn();
}
else
{
m_highlighter->turnHighlightOff();
}
m_previewToggled = true;
updateSourceView();
}
/*
@ -1059,9 +1067,9 @@ void MainWindow::saveSettings()
m_settings->setValueByName("LastOpenedFiles", m_recentlyOpenedList.join("|"));
///-- m_settings->setValueByName("MainWindowState", saveState());
///--
///-- // Also save the syntax highlight style for all lexers.
///-- _highlighter->writeCurrentSettings("");
// Also save the syntax highlight style for all lexers.
m_highlighter->writeCurrentSettings();
}
/*
@ -1142,7 +1150,7 @@ void MainWindow::createEncodingMenu()
m_saveEncodedActionGroup = new TQActionGroup(this);
// Loop for each available encoding
for(const TQString &encodingName : encodingsList)
for (const TQString &encodingName : encodingsList)
{
// Create actions for the "reopen" menu
TQAction *encodingAction = new TQAction(m_encodingActionGroup);
@ -1237,23 +1245,21 @@ void MainWindow::encodingChanged(TQAction *encodingAction)
*/
void MainWindow::createHighlighterMenu()
{
///--- m_highlighterActionGroup = new TQActionGroup(this);
///---
///--- // Loop for each known highlighter
///--- for(const TQString &highlighterName: m_highlighter->getAvailableHighlighters())
///--- {
///--- TQAction *highlighterAction = new TQAction(m_highlighterActionGroup);
///--- highlighterAction->setText(highlighterName);
///--- highlighterAction->setStatusTip(tr("Set the syntax highlighting to ") + highlighterName);
///--- highlighterAction->setToggleAction(true);
///--- }
///---
///--- m_highlighterActionGroup ->addTo(popupMenuHighlighter);
///--- connect(m_highlighterActionGroup , SIGNAL(selected(TQAction*)),
///--- m_highlighter, SLOT(setHighlighterByAction(TQAction*)));
///---
///--- _mainWindowForm->menuSettings->insertMenu(actionIndenterParameterTooltipsEnabled,
///--- _mainWindowForm->popupMenuHighlighter);
m_highlighterActionGroup = new TQActionGroup(this);
// Loop for each known highlighter
for (const TQString &highlighterName : m_highlighter->getAvailableHighlighters())
{
TQAction *highlighterAction = new TQAction(m_highlighterActionGroup);
highlighterAction->setText(highlighterName);
highlighterAction->setStatusTip(tr("Set the syntax highlighting to ") + highlighterName);
highlighterAction->setToggleAction(true);
m_highlightingActions.append(highlighterAction);
}
m_highlighterActionGroup->addTo(popupMenuHighlighter);
connect(m_highlighterActionGroup , SIGNAL(selected(TQAction*)),
m_highlighter, SLOT(setHighlighterByAction(TQAction*)));
}
/*

@ -27,7 +27,7 @@
class AboutDialog;
/// class AboutDialogGraphicsView;
/// class UiGuiHighlighter;
class UiGuiHighlighter;
class IndentHandler;
class UiGuiSettings;
class UiGuiSettingsDialog;
@ -82,6 +82,7 @@ class MainWindow : public MainWindowBase
private:
TQString loadFile(const TQString &filePath);
void updateWindowTitle();
void updateSyntaxHighlighting();
void loadLastOpenedFile();
void saveSettings();
bool maybeSave();
@ -98,17 +99,17 @@ class MainWindow : public MainWindowBase
///-- void dragEnterEvent(TQDragEnterEvent *event);
///-- void dropEvent(TQDropEvent *event);
TQSplitter *m_centralSplitter;
TQextScintilla *m_qSciSourceCodeEditor;
UiGuiSettings *m_settings;
TQSplitter *m_centralSplitter;
TQextScintilla *m_qSciSourceCodeEditor;
UiGuiSettings *m_settings;
TQString m_currentEncoding;
TQString m_sourceFileContent;
TQString m_sourceFormattedContent;
TQString m_sourceViewContent;
///-- UiGuiHighlighter *m_highlighter;
TQString m_currentEncoding;
TQString m_sourceFileContent;
TQString m_sourceFormattedContent;
TQString m_sourceViewContent;
UiGuiHighlighter *m_highlighter;
///-- TQScrollBar *m_textEditVScrollBar;
AboutDialog *m_aboutDialog;
AboutDialog *m_aboutDialog;
///-- AboutDialogGraphicsView *_aboutDialogGraphicsView;
UiGuiSettingsDialog *m_settingsDialog;
int m_actionClearRecentlyOpenedListId;
@ -125,6 +126,7 @@ class MainWindow : public MainWindowBase
TQActionGroup *m_highlighterActionGroup;
TQValueList<TQAction*> m_openEncodingActions;
TQValueList<TQAction*> m_saveEncodingActions;
TQValueList<TQAction*> m_highlightingActions;
TQTranslator *m_uiGuiTranslator;
bool m_sourceCodeChanged;

@ -0,0 +1,458 @@
/***************************************************************************
* Copyright (C) 2006-2012 by Thomas Schweitzer *
* thomas-schweitzer(at)arcor.de *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License version 2.0 as *
* published by the Free Software Foundation. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program in the file LICENSE.GPL; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include "UiGuiHighlighter.h"
#include "SettingsPaths.h"
#include <tqaction.h>
#include <tqsettings.h>
#include <tqextscintilla.h>
#include <tqextscintillalexer.h>
#include <tqextscintillalexerbash.h>
#include <tqextscintillalexerbatch.h>
#include <tqextscintillalexercpp.h>
#include <tqextscintillalexercsharp.h>
#include <tqextscintillalexercss.h>
#include <tqextscintillalexerdiff.h>
#include <tqextscintillalexerhtml.h>
#include <tqextscintillalexeridl.h>
#include <tqextscintillalexerjava.h>
#include <tqextscintillalexerjavascript.h>
#include <tqextscintillalexerlua.h>
#include <tqextscintillalexermakefile.h>
#include <tqextscintillalexerperl.h>
#include <tqextscintillalexerpov.h>
#include <tqextscintillalexerproperties.h>
#include <tqextscintillalexerpython.h>
#include <tqextscintillalexerruby.h>
#include <tqextscintillalexersql.h>
#include <tqextscintillalexertex.h>
/*
\class UiGuiHighlighter
\ingroup grp_EditorComponent
\brief UiGuiHighlighter used for selecting the syntax highlighter/lexer for the TQextScintilla component.
*/
/*
\brief The constructor initializes some regular expressions and keywords to identify cpp tokens
*/
UiGuiHighlighter::UiGuiHighlighter(TQextScintilla *parent) :
TQObject((TQObject*)parent)
{
m_qsciEditorParent = parent;
// Create the highlighter m_settings object from the uiguisyntaxhighlightconfigrc file.
m_settings = new TQSettings(TQSettings::Ini);
// The next lines make user the settings are always stored in
// $HOME/.universalindentgui/uiguisyntaxhighlightconfigrc
m_settings->insertSearchPath(TQSettings::Unix, SettingsPaths::getSettingsPath());
m_settings->setPath("UniversalIndentGUI", "UniversalIndentGUI", TQSettings::User);
m_highlightingIsOn = true;
m_fileExtensions["Bash"] = TQStringList() << "sh";
m_fileExtensions["Batch"] = TQStringList() << "bat";
m_fileExtensions["C++"] = TQStringList() << "c" << "h" << "cpp" << "hpp" << "cxx"<< "hxx";
m_fileExtensions["C#"] = TQStringList() << "cs";
m_fileExtensions["CSS"] = TQStringList() << "css";
m_fileExtensions["Diff"] = TQStringList() << "diff";
m_fileExtensions["HTML"] = TQStringList() << "html" << "htm";
m_fileExtensions["IDL"] = TQStringList() << "idl";
m_fileExtensions["Java"] = TQStringList() << "java";
m_fileExtensions["JavaScript"] = TQStringList() << "js";
m_fileExtensions["LUA"] = TQStringList() << "lua";
m_fileExtensions["Makefile"] = TQStringList() << "makefile";
m_fileExtensions["Perl"] = TQStringList() << "perl" << "pl" << "pm";
m_fileExtensions["PHP"] = TQStringList() << "php";
m_fileExtensions["POV"] = TQStringList() << "pov";
m_fileExtensions["Ini"] = TQStringList() << "ini";
m_fileExtensions["Python"] = TQStringList() << "py";
m_fileExtensions["Ruby"] = TQStringList() << "rub" << "rb";
m_fileExtensions["SQL"] = TQStringList() << "sql";
m_fileExtensions["TeX"] = TQStringList() << "tex";
m_fileExtensions["XML"] = TQStringList() << "xml";
m_lexer = NULL;
// Set default highlighter to C++ highlighter.
setLexer("cpp");
}
UiGuiHighlighter::~UiGuiHighlighter()
{
delete m_settings;
}
/*
\brief Returns the available highlighters as TQStringList.
*/
TQStringList UiGuiHighlighter::getAvailableHighlighters()
{
return m_fileExtensions.keys();
}
/*
\brief This slot handles signals coming from selecting another syntax highlighter.
*/
void UiGuiHighlighter::setHighlighterByAction(TQAction *highlighterAction)
{
TQString highlighterName = highlighterAction->text();
setLexer(m_fileExtensions[highlighterName].first());
// TODO: This is really no nice way. How do it better?
// Need to do this "text update" to update the syntax highlighting. Otherwise highlighting is
// wrong.
// TODO not available in TQScintilla 1.71
//--- int scrollPos = m_qsciEditorParent->verticalScrollBar()->value();
m_qsciEditorParent->setText(m_qsciEditorParent->text());
//--- m_qsciEditorParent->verticalScrollBar()->setValue(scrollPos);
}
/*
\brief Turns the syntax parser on.
*/
void UiGuiHighlighter::turnHighlightOn()
{
m_highlightingIsOn = true;
m_qsciEditorParent->setLexer(m_lexer);
readCurrentSettings();
}
/*
\brief Turns the syntax parser off.
*/
void UiGuiHighlighter::turnHighlightOff()
{
m_highlightingIsOn = false;
m_qsciEditorParent->setLexer();
m_qsciEditorParent->setFont(TQFont("Monospace", 10, TQFont::Normal));
m_qsciEditorParent->setMarginsFont(TQFont("Monospace", 10, TQFont::Normal));
}
/*
\brief Read the settings for the current lexer from the settings file.
*/
bool UiGuiHighlighter::readCurrentSettings()
{
// settings are read from the uiguisyntaxhighlightconfigrc file, group <lexer name>
m_settings->resetGroup();
m_settings->beginGroup(TQString("uiguisyntaxhighlightconfig/") + m_lexer->language());
bool ok, flag, rc = true;
int num;
// Reset lists containing fonts and colors for each style
m_fontForStyles.clear();
m_colorForStyles.clear();
// Read the styles.
for (int i = 0; i < 128; ++i)
{
// Ignore invalid styles.
if (m_lexer->description(i).isEmpty())
{
continue;
}
TQString key = TQString();
key.sprintf("style%02d/", i);
key.replace("+", "p");
// Read the foreground color.
num = m_settings->readNumEntry(key + "color", 0, &ok);
if (ok)
{
setColor(TQColor((num >> 16) & 0xff, (num >> 8) & 0xff, num & 0xff), i);
}
else
{
rc = false;
}
// Read the end-of-line fill.
flag = m_settings->readBoolEntry(key + "eolfill", false, &ok);
if (ok)
{
m_lexer->setEolFill(flag, i);
}
else
{
rc = false;
}
// Read the font
TQStringList fdesc;
fdesc = m_settings->readListEntry(key + "font", &ok);
if (ok && fdesc.count() == 5)
{
TQFont f;
f.setFamily(fdesc[0]);
f.setPointSize(fdesc[1].toInt());
f.setBold(fdesc[2].toInt());
f.setItalic(fdesc[3].toInt());
f.setUnderline(fdesc[4].toInt());
setFont(f, i);
}
else
{
rc = false;
}
// Read the background color.
num = m_settings->readNumEntry(key + "paper", 0, &ok);
if (ok)
{
m_lexer->setPaper(TQColor((num >> 16) & 0xff, (num >> 8) & 0xff, num & 0xff), i);
}
else
{
rc = false;
}
}
// Read the properties.
m_lexer->refreshProperties();
return rc;
}
/*
\brief Write the settings for the current lexer to the settings file.
*/
void UiGuiHighlighter::writeCurrentSettings()
{
// settings are stored in the uiguisyntaxhighlightconfigrc file, group <lexer name>
m_settings->resetGroup();
m_settings->beginGroup(TQString("uiguisyntaxhighlightconfig/") + m_lexer->language());
// Write the styles.
for (int i = 0; i < 128; ++i)
{
// Ignore invalid styles.
if (m_lexer->description(i).isEmpty())
{
continue;
}
int num;
TQColor c;
TQString key = TQString();
key.sprintf("style%02d/", i);
key.replace("+", "p");
// Write style name
m_settings->writeEntry(key + "", m_lexer->description(i));
// Write the foreground color.
if (m_colorForStyles.contains(i))
{
c = m_colorForStyles[i];
}
else
{
c = m_lexer->color(i);
}
num = (c.red() << 16) | (c.green() << 8) | c.blue();
m_settings->writeEntry(key + "color", num);
// Write the end-of-line fill.
m_settings->writeEntry(key + "eolfill", m_lexer->eolFill(i));
// Write the font
TQStringList fdesc;
TQString fmt("%1");
TQFont f;
if (m_fontForStyles.contains(i))
{
f = m_fontForStyles[i];
}
else
{
f = m_lexer->font(i);
}
fdesc += f.family();
fdesc += fmt.arg(f.pointSize());
// The casts are for Borland.
fdesc += fmt.arg((int)f.bold());
fdesc += fmt.arg((int)f.italic());
fdesc += fmt.arg((int)f.underline());
m_settings->writeEntry(key + "font", fdesc);
// Write the background color.
c = m_lexer->paper(i);
num = (c.red() << 16) | (c.green() << 8) | c.blue();
m_settings->writeEntry(key + "paper", num);
}
}
/*
\brief Sets the \a color for the given \a style.
*/
void UiGuiHighlighter::setColor(const TQColor &color, int style)
{
m_colorForStyles[style] = color;
m_lexer->setColor(color, style);
}
/*
\brief Sets the \a font for the given \a style.
*/
void UiGuiHighlighter::setFont(const TQFont &font, int style)
{
m_fontForStyles[style] = font;
m_lexer->setFont(font, style);
}
/*
\brief Sets the proper highlighter/lexer for the given file \a extension. Returns the index of the used lexer in the list.
*/
int UiGuiHighlighter::setLexer(TQString extension)
{
int indexOfHighlighter = 0;
extension = extension.lower();
if (m_lexer)
{
writeCurrentSettings();
delete m_lexer;
m_lexer = nullptr;
}
if (extension == "cpp" || extension == "hpp" || extension == "c" || extension == "h" ||
extension == "cxx" || extension == "hxx")
{
m_lexer = new TQextScintillaLexerCPP();
}
else if (extension == "sh")
{
m_lexer = new TQextScintillaLexerBash();
}
else if (extension == "bat")
{
m_lexer = new TQextScintillaLexerBatch();
}
else if (extension == "cs")
{
m_lexer = new TQextScintillaLexerCSharp();
}
else if (extension == "css")
{
m_lexer = new TQextScintillaLexerCSS();
}
else if (extension == "diff")
{
m_lexer = new TQextScintillaLexerDiff();
}
else if (extension == "html" || extension == "htm")
{
m_lexer = new TQextScintillaLexerHTML();
}
else if (extension == "idl")
{
m_lexer = new TQextScintillaLexerIDL();
}
else if (extension == "java")
{
m_lexer = new TQextScintillaLexerJava();
}
else if (extension == "js")
{
m_lexer = new TQextScintillaLexerJavaScript();
}
else if (extension == "lua")
{
m_lexer = new TQextScintillaLexerLua();
}
else if (extension == "makefile")
{
m_lexer = new TQextScintillaLexerMakefile();
}
else if (extension == "perl" || extension == "pl" || extension == "pm")
{
m_lexer = new TQextScintillaLexerPerl();
}
else if (extension == "php")
{
m_lexer = new TQextScintillaLexerHTML();
}
else if (extension == "pov")
{
m_lexer = new TQextScintillaLexerPOV();
}
else if (extension == "ini")
{
m_lexer = new TQextScintillaLexerProperties();
}
else if (extension == "py")
{
m_lexer = new TQextScintillaLexerPython();
}
else if (extension == "rub" || extension == "rb")
{
m_lexer = new TQextScintillaLexerRuby();
}
else if (extension == "sql")
{
m_lexer = new TQextScintillaLexerSQL();
}
else if (extension == "tex")
{
m_lexer = new TQextScintillaLexerTeX();
}
else if (extension == "xml")
{
m_lexer = new TQextScintillaLexerHTML();
}
else
{
m_lexer = new TQextScintillaLexerCPP();
extension = "cpp";
}
// Find the index of the selected m_lexer.
indexOfHighlighter = 0;
for (const TQString &hlExtList : m_fileExtensions.keys())
{
if (m_fileExtensions[hlExtList].contains(extension))
{
break;
}
indexOfHighlighter++;
}
// Set the m_lexer for the TQScintilla widget.
if (m_highlightingIsOn)
{
m_qsciEditorParent->setLexer(m_lexer);
}
// Read the m_settings for the m_lexer properties from file.
readCurrentSettings();
return indexOfHighlighter;
}
#include "UiGuiHighlighter.moc"

@ -20,16 +20,17 @@
#ifndef UIGUIHIGHLIGHTER_H
#define UIGUIHIGHLIGHTER_H
#include <tntqobject.h>
#include <tntqmap.h>
#include <tntqfont.h>
#include <tntqcolor.h>
#include <tqcolor.h>
#include <tqfont.h>
#include <tqobject.h>
#include <tqmap.h>
#include <tqstringlist.h>
class TQAction;
class TQSettings;
class TQextScintilla;
class QsciLexer;
class TQextScintillaLexer;
class UiGuiHighlighter : public TQObject
@ -38,12 +39,13 @@ class UiGuiHighlighter : public TQObject
public:
UiGuiHighlighter(TQextScintilla *parent);
~UiGuiHighlighter();
void turnHighlightOff();
void turnHighlightOn();
bool readCurrentSettings(const char *prefix);
void writeCurrentSettings(const char *prefix);
bool readCurrentSettings();
void writeCurrentSettings();
TQStringList getAvailableHighlighters();
public slots:
@ -56,20 +58,18 @@ class UiGuiHighlighter : public TQObject
void setFont(const TQFont &font, int style = -1);
// Sets the lexer that is responsible for the given \a extension.
int setLexerForExtension(TQString extension);
void setLexerByName(TQString lexerName);
int setLexer(TQString extension);
void setHighlighterByAction(TQAction *highlighterAction);
private:
bool _highlightingIsOn;
TQextScintilla *_qsciEditorParent;
TQMap<int, TQFont> _fontForStyles;
TQMap<int, TQColor> _colorForStyles;
QsciLexer *_lexer;
TQSettings *_settings;
TQMap<TQString, TQStringList> _mapHighlighternameToExtension;
bool m_highlightingIsOn;
TQextScintilla *m_qsciEditorParent;
TQMap<int, TQFont> m_fontForStyles;
TQMap<int, TQColor> m_colorForStyles;
TQextScintillaLexer *m_lexer;
TQSettings *m_settings;
TQMap<TQString, TQStringList> m_fileExtensions;
};
#endif // UIGUIHIGHLIGHTER_H

@ -166,7 +166,7 @@ void UiGuiSettingsDialog::updateSettings()
//--- "(Will be implemented soon)" +
//--- groupBoxSyntaxHighlighterProperties->toolTip());
//---
//--- TQStringList languageShortList = _settings->getAvailableTranslations();
//--- TQStringList languageShortList = m_settings->getAvailableTranslations();
//---
//--- // Now retranslate every entry in the language selection box.
//--- for (int i = 0; i < languageShortList.size(); i++)

@ -1,595 +0,0 @@
/***************************************************************************
* Copyright (C) 2006-2012 by Thomas Schweitzer *
* thomas-schweitzer(at)arcor.de *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License version 2.0 as *
* published by the Free Software Foundation. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program in the file LICENSE.GPL; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include "UiGuiHighlighter.h"
#include "SettingsPaths.h"
#include <tntqsettings.h>
#include <tqmenu.h>
#include <tntqscrollbar.h>
#include <tqcoreapplication.h>
#include <Qsci/qsciscintilla.h>
#include <Qsci/qscilexer.h>
#include <Qsci/qscilexerbash.h>
#include <Qsci/qscilexerbatch.h>
#include <Qsci/qscilexercmake.h>
#include <Qsci/qscilexercpp.h>
#include <Qsci/qscilexercsharp.h>
#include <Qsci/qscilexercss.h>
#include <Qsci/qscilexerd.h>
#include <Qsci/qscilexerdiff.h>
#if (TQSCINTILLA_VERSION >= 0x020300)
#include <Qsci/qscilexerfortran.h>
#include <Qsci/qscilexerfortran77.h>
#endif
#include <Qsci/qscilexerhtml.h>
#include <Qsci/qscilexeridl.h>
#include <Qsci/qscilexerjava.h>
#include <Qsci/qscilexerjavascript.h>
#include <Qsci/qscilexerlua.h>
#include <Qsci/qscilexermakefile.h>
#if (TQSCINTILLA_VERSION >= 0x020300)
#include <Qsci/qscilexerpascal.h>
#endif
#include <Qsci/qscilexerperl.h>
#if (TQSCINTILLA_VERSION >= 0x020300)
#include <Qsci/qscilexerpostscript.h>
#endif
#include <Qsci/qscilexerpov.h>
#include <Qsci/qscilexerproperties.h>
#include <Qsci/qscilexerpython.h>
#include <Qsci/qscilexerruby.h>
#if (TQSCINTILLA_VERSION >= 0x020400)
#include <Qsci/qscilexerspice.h>
#endif
#include <Qsci/qscilexersql.h>
#if (TQSCINTILLA_VERSION >= 0x020300)
#include <Qsci/qscilexertcl.h>
#endif
#include <Qsci/qscilexertex.h>
#if (TQSCINTILLA_VERSION >= 0x020400)
#include <Qsci/qscilexerverilog.h>
#endif
#include <Qsci/qscilexervhdl.h>
#if (TQSCINTILLA_VERSION >= 0x020300)
#include <Qsci/qscilexerxml.h>
#include <Qsci/qscilexeryaml.h>
#endif
// \defgroup grp_EditorComponent All concerning editor widget.
/*
\class UiGuiHighlighter
\ingroup grp_EditorComponent
\brief UiGuiHighlighter used for selecting the syntax highlighter/lexer for the TQextScintilla component.
*/
/*
\brief The constructor initializes some regular expressions and keywords to identify cpp tokens
*/
UiGuiHighlighter::UiGuiHighlighter(TQextScintilla *parent) :
TQObject(parent)
{
_qsciEditorParent = parent;
// Create the highlighter _settings object from the UiGuiSyntaxHighlightConfig.ini file.
_settings = new TQSettings(
SettingsPaths::getSettingsPath() + "/UiGuiSyntaxHighlightConfig.ini", TQSettings::IniFormat,
this);
_highlightingIsOn = true;
_mapHighlighternameToExtension["Bash"] = TQStringList() << "sh";
_mapHighlighternameToExtension["Batch"] = TQStringList() << "bat";
_mapHighlighternameToExtension["CMake"] = TQStringList() << "cmake";
_mapHighlighternameToExtension["C++"] = TQStringList() << "c" << "h" << "cpp" << "hpp" <<
"cxx" << "hxx";
_mapHighlighternameToExtension["C#"] = TQStringList() << "cs";
_mapHighlighternameToExtension["CSS"] = TQStringList() << "css";
_mapHighlighternameToExtension["D"] = TQStringList() << "d";
_mapHighlighternameToExtension["Diff"] = TQStringList() << "diff";
#if (TQSCINTILLA_VERSION >= 0x020300)
_mapHighlighternameToExtension["Fortran"] = TQStringList() << "f" << "for" << "f90";
_mapHighlighternameToExtension["Fortran77"] = TQStringList() << "f77";
#endif
_mapHighlighternameToExtension["HTML"] = TQStringList() << "html" << "htm";
_mapHighlighternameToExtension["IDL"] = TQStringList() << "idl";
_mapHighlighternameToExtension["Java"] = TQStringList() << "java";
_mapHighlighternameToExtension["JavaScript"] = TQStringList() << "js";
_mapHighlighternameToExtension["LUA"] = TQStringList() << "lua";
_mapHighlighternameToExtension["Makefile"] = TQStringList() << "makefile";
#if (TQSCINTILLA_VERSION >= 0x020300)
_mapHighlighternameToExtension["Pascal"] = TQStringList() << "pas";
#endif
_mapHighlighternameToExtension["Perl"] = TQStringList() << "perl" << "pl" << "pm";
_mapHighlighternameToExtension["PHP"] = TQStringList() << "php";
#if (TQSCINTILLA_VERSION >= 0x020300)
_mapHighlighternameToExtension["PostScript"] = TQStringList() << "ps" << "eps" << "pdf" << "ai" <<
"fh";
#endif
_mapHighlighternameToExtension["POV"] = TQStringList() << "pov";
_mapHighlighternameToExtension["Ini"] = TQStringList() << "ini";
_mapHighlighternameToExtension["Python"] = TQStringList() << "py";
_mapHighlighternameToExtension["Ruby"] = TQStringList() << "rub" << "rb";
#if (TQSCINTILLA_VERSION >= 0x020400)
_mapHighlighternameToExtension["Spice"] = TQStringList() << "cir";
#endif
_mapHighlighternameToExtension["SQL"] = TQStringList() << "sql";
#if (TQSCINTILLA_VERSION >= 0x020300)
_mapHighlighternameToExtension["TCL"] = TQStringList() << "tcl";
#endif
_mapHighlighternameToExtension["TeX"] = TQStringList() << "tex";
#if (TQSCINTILLA_VERSION >= 0x020400)
_mapHighlighternameToExtension["Verilog"] = TQStringList() << "v" << "vh";
#endif
_mapHighlighternameToExtension["VHDL"] = TQStringList() << "vhdl";
_mapHighlighternameToExtension["XML"] = TQStringList() << "xml";
#if (TQSCINTILLA_VERSION >= 0x020300)
_mapHighlighternameToExtension["YAML"] = TQStringList() << "yaml";
#endif
_lexer = NULL;
// This code is only for testing.
/*
foreach(TQStringList extensionList, _mapHighlighternameToExtension.values() ) {
setLexerForExtension( extensionList.at(0) );
}
*/
// Set default highlighter to C++ highlighter.
setLexerForExtension("cpp");
}
/*
\brief Returns the available highlighters as TQStringList.
*/
TQStringList UiGuiHighlighter::getAvailableHighlighters()
{
return _mapHighlighternameToExtension.keys();
}
/*
\brief This slot handles signals coming from selecting another syntax highlighter.
*/
void UiGuiHighlighter::setHighlighterByAction(TQAction *highlighterAction)
{
TQString highlighterName = highlighterAction->text();
setLexerForExtension(_mapHighlighternameToExtension[highlighterName].first());
//TODO: This is really no nice way. How do it better?
// Need to do this "text update" to update the syntax highlighting. Otherwise highlighting is
// wrong.
int scrollPos = _qsciEditorParent->verticalScrollBar()->value();
_qsciEditorParent->setText(_qsciEditorParent->text());
_qsciEditorParent->verticalScrollBar()->setValue(scrollPos);
}
/*
\brief Turns the syntax parser on.
*/
void UiGuiHighlighter::turnHighlightOn()
{
_highlightingIsOn = true;
_qsciEditorParent->setLexer(_lexer);
readCurrentSettings("");
}
/*
\brief Turns the syntax parser off.
*/
void UiGuiHighlighter::turnHighlightOff()
{
_highlightingIsOn = false;
_qsciEditorParent->setLexer();
_qsciEditorParent->setFont(TQFont("Monospace", 10, TQFont::Normal));
_qsciEditorParent->setMarginsFont(TQFont("Monospace", 10, TQFont::Normal));
}
/*
\brief Read the settings for the current lexer from the settings file.
*/
//TODO: Refactor this function so that the coding style and variable names suit better.
bool UiGuiHighlighter::readCurrentSettings(const char *prefix)
{
bool ok, flag, rc = true;
int num;
TQString key;
// Reset lists containing fonts and colors for each style
_fontForStyles.clear();
_colorForStyles.clear();
// Read the styles.
for (int i = 0; i < 128; ++i)
{
// Ignore invalid styles.
if (_lexer->description(i).isEmpty())
{
continue;
}
key.sprintf("%s/%s/style%d/", prefix, _lexer->language(), i);
key.replace("+", "p");
// Read the foreground color.
ok = _settings->contains(key + "color");
num = _settings->value(key + "color", 0).toInt();
if (ok)
{
setColor(TQColor((num >> 16) & 0xff, (num >> 8) & 0xff, num & 0xff), i);
}
else
{
rc = false;
}
// Read the end-of-line fill.
ok = _settings->contains(key + "eolfill");
flag = _settings->value(key + "eolfill", false).toBool();
if (ok)
{
_lexer->setEolFill(flag, i);
}
else
{
rc = false;
}
// Read the font
TQStringList fdesc;
ok = _settings->contains(key + "font");
fdesc = _settings->value(key + "font").toStringList();
if (ok && fdesc.count() == 5)
{
TQFont f;
if (fdesc[0].contains("courier", TQt::CaseInsensitive))
{
f.setFamily("Monospace");
}
else
{
f.setFamily(fdesc[0]);
}
f.setPointSize(fdesc[1].toInt());
f.setBold(fdesc[2].toInt());
f.setItalic(fdesc[3].toInt());
f.setUnderline(fdesc[4].toInt());
setFont(f, i);
}
else
{
rc = false;
}
// Read the background color.
ok = _settings->contains(key + "paper");
num = _settings->value(key + "paper", 0).toInt();
if (ok)
{
_lexer->setPaper(TQColor((num >> 16) & 0xff, (num >> 8) & 0xff, num & 0xff), i);
}
else
{
rc = false;
}
}
// Read the properties.
key.sprintf("%s/%s/properties/", prefix, _lexer->language());
_lexer->refreshProperties();
return rc;
}
/*
\brief Write the settings for the current lexer to the settings file.
*/
void UiGuiHighlighter::writeCurrentSettings(const char *prefix)
{
TQString key;
// Write the styles.
for (int i = 0; i < 128; ++i)
{
// Ignore invalid styles.
if (_lexer->description(i).isEmpty())
{
continue;
}
int num;
TQColor c;
key.sprintf("%s/%s/style%d/", prefix, _lexer->language(), i);
key.replace("+", "p");
// Write style name
_settings->setValue(key + "", _lexer->description(i));
// Write the foreground color.
if (_colorForStyles.contains(i))
{
c = _colorForStyles[i];
}
else
{
c = _lexer->color(i);
}
num = (c.red() << 16) | (c.green() << 8) | c.blue();
_settings->setValue(key + "color", num);
// Write the end-of-line fill.
_settings->setValue(key + "eolfill", _lexer->eolFill(i));
// Write the font
TQStringList fdesc;
TQString fmt("%1");
TQFont f;
if (_fontForStyles.contains(i))
{
f = _fontForStyles[i];
}
else
{
f = _lexer->font(i);
}
fdesc += f.family();
fdesc += fmt.arg(f.pointSize());
// The casts are for Borland.
fdesc += fmt.arg((int)f.bold());
fdesc += fmt.arg((int)f.italic());
fdesc += fmt.arg((int)f.underline());
_settings->setValue(key + "font", fdesc);
// Write the background color.
c = _lexer->paper(i);
num = (c.red() << 16) | (c.green() << 8) | c.blue();
_settings->setValue(key + "paper", num);
}
}
/*
\brief Sets the \a color for the given \a style.
*/
void UiGuiHighlighter::setColor(const TQColor &color, int style)
{
_colorForStyles[style] = color;
_lexer->setColor(color, style);
}
/*
\brief Sets the \a font for the given \a style.
*/
void UiGuiHighlighter::setFont(const TQFont &font, int style)
{
_fontForStyles[style] = font;
_lexer->setFont(font, style);
}
/*
\brief Sets the to be used lexer by giving his name.
*/
void UiGuiHighlighter::setLexerByName(TQString lexerName)
{
setLexerForExtension(_mapHighlighternameToExtension[lexerName].first());
}
/*
\brief Sets the proper highlighter / lexer for the given file \a extension. Returns the index of the used lexer in the list.
*/
int UiGuiHighlighter::setLexerForExtension(TQString extension)
{
int indexOfHighlighter = 0;
extension = extension.toLower();
if (_lexer != NULL)
{
writeCurrentSettings("");
delete _lexer;
}
if (extension == "cpp" || extension == "hpp" || extension == "c" || extension == "h" ||
extension == "cxx" || extension == "hxx")
{
_lexer = new QsciLexerCPP();
}
else if (extension == "sh")
{
_lexer = new QsciLexerBash();
}
else if (extension == "bat")
{
_lexer = new QsciLexerBatch();
}
else if (extension == "cmake")
{
_lexer = new QsciLexerCMake();
}
else if (extension == "cs")
{
_lexer = new QsciLexerCSharp();
}
else if (extension == "css")
{
_lexer = new QsciLexerCSS();
}
else if (extension == "d")
{
_lexer = new QsciLexerD();
}
else if (extension == "diff")
{
_lexer = new QsciLexerDiff();
}
#if (TQSCINTILLA_VERSION >= 0x020300)
else if (extension == "f" || extension == "for" || extension == "f90")
{
_lexer = new QsciLexerFortran();
}
else if (extension == "f77")
{
_lexer = new QsciLexerFortran77();
}
#endif
else if (extension == "html" || extension == "htm")
{
_lexer = new QsciLexerHTML();
}
else if (extension == "idl")
{
_lexer = new QsciLexerIDL();
}
else if (extension == "java")
{
_lexer = new QsciLexerJava();
}
else if (extension == "js")
{
_lexer = new QsciLexerJavaScript();
}
else if (extension == "lua")
{
_lexer = new QsciLexerLua();
}
else if (extension == "makefile")
{
_lexer = new QsciLexerMakefile();
}
#if (TQSCINTILLA_VERSION >= 0x020300)
else if (extension == "pas")
{
_lexer = new QsciLexerPascal();
}
#endif
else if (extension == "perl" || extension == "pl" || extension == "pm")
{
_lexer = new QsciLexerPerl();
}
else if (extension == "php")
{
_lexer = new QsciLexerHTML();
}
#if (TQSCINTILLA_VERSION >= 0x020300)
else if (extension == "ps" || extension == "eps" || extension == "pdf" || extension == "ai" ||
extension == "fh")
{
_lexer = new QsciLexerPostScript();
}
#endif
else if (extension == "pov")
{
_lexer = new QsciLexerPOV();
}
else if (extension == "ini")
{
_lexer = new QsciLexerProperties();
}
else if (extension == "py")
{
_lexer = new QsciLexerPython();
}
else if (extension == "rub" || extension == "rb")
{
_lexer = new QsciLexerRuby();
}
#if (TQSCINTILLA_VERSION >= 0x020400)
else if (extension == "spice?")
{
_lexer = new QsciLexerSpice();
}
#endif
else if (extension == "sql")
{
_lexer = new QsciLexerSQL();
}
#if (TQSCINTILLA_VERSION >= 0x020300)
else if (extension == "tcl")
{
_lexer = new QsciLexerTCL();
}
#endif
else if (extension == "tex")
{
_lexer = new QsciLexerTeX();
}
#if (TQSCINTILLA_VERSION >= 0x020400)
else if (extension == "vlog?")
{
_lexer = new QsciLexerVerilog();
}
#endif
else if (extension == "vhdl")
{
_lexer = new QsciLexerVHDL();
}
else if (extension == "xml")
{
#if (TQSCINTILLA_VERSION >= 0x020300)
_lexer = new QsciLexerXML();
#else
_lexer = new QsciLexerHTML();
#endif
}
#if (TQSCINTILLA_VERSION >= 0x020300)
else if (extension == "yaml")
{
_lexer = new QsciLexerYAML();
}
#endif
else
{
_lexer = new QsciLexerCPP();
extension = "cpp";
}
// Find the index of the selected _lexer.
indexOfHighlighter = 0;
while (!_mapHighlighternameToExtension.values().at(indexOfHighlighter).contains(extension))
{
indexOfHighlighter++;
}
// Set the _lexer for the TQScintilla widget.
if (_highlightingIsOn)
{
_qsciEditorParent->setLexer(_lexer);
}
// Read the _settings for the _lexer properties from file.
readCurrentSettings("");
return indexOfHighlighter;
}
Loading…
Cancel
Save