Make syntax highlighting configurable

Add new options to the preferences dialog to change the colours and text
styles used in the SQL syntax highlighter.
This commit is contained in:
Martin Kleusberg
2013-03-30 15:06:26 +01:00
parent 340af96deb
commit d941073c25
6 changed files with 291 additions and 86 deletions

View File

@@ -26,6 +26,9 @@ MainWindow::MainWindow(QWidget* parent)
: QMainWindow(parent),
ui(new Ui::MainWindow),
browseTableModel(new QStandardItemModel(this)),
sqliteHighlighterTabSql(0),
sqliteHighlighterLogUser(0),
sqliteHighlighterLogApp(0),
editWin(new EditDialog(this)),
findWin(0)
{
@@ -47,8 +50,6 @@ void MainWindow::init()
{
// Init the SQL log dock
db.mainWindow = this;
sqliteHighlighterLogUser = new SQLiteSyntaxHighlighter(ui->editLogUser->document());
sqliteHighlighterLogApp = new SQLiteSyntaxHighlighter(ui->editLogApplication->document());
// Set up the db tree widget
ui->dbTreeWidget->setColumnHidden(1, true);
@@ -58,8 +59,8 @@ void MainWindow::init()
gotoValidator = new QIntValidator(0, 0, this);
ui->editGoto->setValidator(gotoValidator);
// Create the SQL sytax highlighter
sqliteHighlighterTabSql = new SQLiteSyntaxHighlighter(ui->sqlTextEdit->document());
// Create the SQL sytax highlighters
createSyntaxHighlighters();
// Set up DB models
ui->dataTable->setModel(browseTableModel);
@@ -983,10 +984,22 @@ void MainWindow::openPreferences()
PreferencesDialog dialog(this);
if(dialog.exec())
{
createSyntaxHighlighters();
populateStructure();
resetBrowser();
}
}
void MainWindow::createSyntaxHighlighters()
{
delete sqliteHighlighterLogApp;
delete sqliteHighlighterLogUser;
delete sqliteHighlighterTabSql;
sqliteHighlighterLogApp = new SQLiteSyntaxHighlighter(ui->editLogApplication->document());
sqliteHighlighterLogUser = new SQLiteSyntaxHighlighter(ui->editLogUser->document());
sqliteHighlighterTabSql = new SQLiteSyntaxHighlighter(ui->sqlTextEdit->document());
}
//******************************************************************
//** Tree Events
//******************************************************************

View File

@@ -140,6 +140,7 @@ private slots:
virtual void savePragmas();
virtual void mainTabSelected( int tabindex );
virtual void browseTableHeaderClicked(int logicalindex);
virtual void createSyntaxHighlighters();
};
#endif

View File

@@ -3,6 +3,7 @@
#include <QDir>
#include <QSettings>
#include <QFileDialog>
#include <QColorDialog>
#include "sqlitedb.h"
QHash<QString, QVariant> PreferencesDialog::m_hCache;
@@ -41,6 +42,21 @@ void PreferencesDialog::loadSettings()
ui->encodingComboBox->setCurrentIndex(ui->encodingComboBox->findText(getSettingsValue("db", "defaultencoding").toString(), Qt::MatchFixedString));
ui->locationEdit->setText(getSettingsValue("db", "defaultlocation").toString());
ui->foreignKeysCheckBox->setChecked(getSettingsValue("db", "foreignkeys").toBool());
for(int i=0;i<ui->treeSyntaxHighlighting->topLevelItemCount();i++)
{
QString name;
if(i == 0) name = "keyword";
else if(i == 1) name = "table";
else if(i == 2) name = "comment";
else if(i == 3) name = "identifier";
else if(i == 4) name = "string";
ui->treeSyntaxHighlighting->topLevelItem(i)->setText(1, getSettingsValue("syntaxhighlighter", name + "_colour").toString());
ui->treeSyntaxHighlighting->topLevelItem(i)->setCheckState(2, getSettingsValue("syntaxhighlighter", name + "_bold").toBool() ? Qt::Checked : Qt::Unchecked);
ui->treeSyntaxHighlighting->topLevelItem(i)->setCheckState(3, getSettingsValue("syntaxhighlighter", name + "_italic").toBool() ? Qt::Checked : Qt::Unchecked);
ui->treeSyntaxHighlighting->topLevelItem(i)->setCheckState(4, getSettingsValue("syntaxhighlighter", name + "_underline").toBool() ? Qt::Checked : Qt::Unchecked);
}
}
void PreferencesDialog::saveSettings()
@@ -48,6 +64,22 @@ void PreferencesDialog::saveSettings()
setSettingsValue("db", "defaultencoding", ui->encodingComboBox->currentText());
setSettingsValue("db", "defaultlocation", ui->locationEdit->text());
setSettingsValue("db", "foreignkeys", ui->foreignKeysCheckBox->isChecked());
for(int i=0;i<ui->treeSyntaxHighlighting->topLevelItemCount();i++)
{
QString name;
if(i == 0) name = "keyword";
else if(i == 1) name = "table";
else if(i == 2) name = "comment";
else if(i == 3) name = "identifier";
else if(i == 4) name = "string";
setSettingsValue("syntaxhighlighter", name + "_colour", ui->treeSyntaxHighlighting->topLevelItem(i)->text(1));
setSettingsValue("syntaxhighlighter", name + "_bold", ui->treeSyntaxHighlighting->topLevelItem(i)->checkState(2) == Qt::Checked);
setSettingsValue("syntaxhighlighter", name + "_italic", ui->treeSyntaxHighlighting->topLevelItem(i)->checkState(3) == Qt::Checked);
setSettingsValue("syntaxhighlighter", name + "_underline", ui->treeSyntaxHighlighting->topLevelItem(i)->checkState(4) == Qt::Checked);
}
accept();
}
@@ -111,6 +143,47 @@ QVariant PreferencesDialog::getSettingsDefaultValue(const QString& group, const
if(group == "General" && name == "recentFileList")
return QStringList();
// syntaxhighlighter?
if(group == "syntaxhighlighter")
{
// Bold? Only tables and keywords are bold by default
if(name.right(4) == "bold")
return name == "keyword_bold" || name == "table_bold";
// Italic? Nothing by default
if(name.right(6) == "italic")
return false;
// Underline? Nothing by default
if(name.right(9) == "underline")
return false;
// Colour?
if(name.right(6) == "colour")
{
if(name == "keyword_colour")
return QColor(Qt::darkBlue).name();
else if(name == "table_colour")
return QColor(Qt::darkCyan).name();
else if(name == "comment_colour")
return QColor(Qt::darkGreen).name();
else if(name == "identifier_colour")
return QColor(Qt::darkMagenta).name();
else if(name == "string_colour")
return QColor(Qt::red).name();
}
}
// Unknown combination of group and name? Return an invalid QVariant!
return QVariant();
}
void PreferencesDialog::showColourDialog(QTreeWidgetItem* item, int column)
{
if(item->text(column).left(1) != "#")
return;
QColor colour = QColorDialog::getColor(QColor(item->text(column)), this);
if(colour.isValid())
item->setText(column, colour.name());
}

View File

@@ -4,6 +4,7 @@
#include <QDialog>
#include <QVariant>
#include <QHash>
class QTreeWidgetItem;
namespace Ui {
class PreferencesDialog;
@@ -25,6 +26,7 @@ private slots:
virtual void chooseLocation();
virtual void loadSettings();
virtual void saveSettings();
virtual void showColourDialog(QTreeWidgetItem* item, int column);
private:
Ui::PreferencesDialog *ui;

View File

@@ -7,7 +7,7 @@
<x>0</x>
<y>0</y>
<width>492</width>
<height>135</height>
<height>276</height>
</rect>
</property>
<property name="windowTitle">
@@ -18,83 +18,166 @@
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<layout class="QFormLayout" name="formLayout">
<property name="fieldGrowthPolicy">
<enum>QFormLayout::ExpandingFieldsGrow</enum>
<widget class="QTabWidget" name="tabWidget">
<property name="currentIndex">
<number>0</number>
</property>
<item row="0" column="0">
<widget class="QLabel" name="label">
<property name="text">
<string>Database Encoding</string>
<widget class="QWidget" name="tab">
<attribute name="title">
<string>&amp;Database</string>
</attribute>
<layout class="QFormLayout" name="formLayout">
<property name="fieldGrowthPolicy">
<enum>QFormLayout::ExpandingFieldsGrow</enum>
</property>
<property name="buddy">
<cstring>encodingComboBox</cstring>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QComboBox" name="encodingComboBox">
<item>
<property name="text">
<string notr="true">UTF-8</string>
</property>
</item>
<item>
<property name="text">
<string notr="true">UTF-16</string>
</property>
</item>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_5">
<property name="toolTip">
<string>Open databases with foreign keys enabled.</string>
</property>
<property name="text">
<string>Foreign keys</string>
</property>
<property name="buddy">
<cstring>foreignKeysCheckBox</cstring>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QCheckBox" name="foreignKeysCheckBox">
<property name="text">
<string>enabled</string>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QLabel" name="label_4">
<property name="text">
<string>Default location</string>
</property>
<property name="buddy">
<cstring>locationEdit</cstring>
</property>
</widget>
</item>
<item row="2" column="1">
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QLineEdit" name="locationEdit">
<property name="enabled">
<bool>false</bool>
<item row="0" column="0">
<widget class="QLabel" name="label">
<property name="text">
<string>Database &amp;encoding</string>
</property>
<property name="buddy">
<cstring>encodingComboBox</cstring>
</property>
</widget>
</item>
<item>
<widget class="QToolButton" name="setLocationButton">
<property name="text">
<string>...</string>
<item row="0" column="1">
<widget class="QComboBox" name="encodingComboBox">
<item>
<property name="text">
<string notr="true">UTF-8</string>
</property>
</item>
<item>
<property name="text">
<string notr="true">UTF-16</string>
</property>
</item>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_5">
<property name="toolTip">
<string>Open databases with foreign keys enabled.</string>
</property>
<property name="text">
<string>&amp;Foreign keys</string>
</property>
<property name="buddy">
<cstring>foreignKeysCheckBox</cstring>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QCheckBox" name="foreignKeysCheckBox">
<property name="text">
<string>enabled</string>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QLabel" name="label_4">
<property name="text">
<string>Default &amp;location</string>
</property>
<property name="buddy">
<cstring>locationEdit</cstring>
</property>
</widget>
</item>
<item row="2" column="1">
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QLineEdit" name="locationEdit">
<property name="enabled">
<bool>false</bool>
</property>
</widget>
</item>
<item>
<widget class="QToolButton" name="setLocationButton">
<property name="text">
<string>...</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
<widget class="QWidget" name="tab_2">
<attribute name="title">
<string>&amp;SQL</string>
</attribute>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<widget class="QTreeWidget" name="treeSyntaxHighlighting">
<property name="showDropIndicator" stdset="0">
<bool>false</bool>
</property>
<property name="rootIsDecorated">
<bool>false</bool>
</property>
<column>
<property name="text">
<string>Context</string>
</property>
</column>
<column>
<property name="text">
<string>Colour</string>
</property>
</column>
<column>
<property name="text">
<string>Bold</string>
</property>
</column>
<column>
<property name="text">
<string>Italic</string>
</property>
</column>
<column>
<property name="text">
<string>Underline</string>
</property>
</column>
<item>
<property name="text">
<string>Keyword</string>
</property>
<property name="text">
<string/>
</property>
<property name="text">
<string/>
</property>
</item>
<item>
<property name="text">
<string>Table</string>
</property>
</item>
<item>
<property name="text">
<string>Comment</string>
</property>
</item>
<item>
<property name="text">
<string>Identifier</string>
</property>
</item>
<item>
<property name="text">
<string>String</string>
</property>
</item>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
</widget>
</item>
<item>
<widget class="QDialogButtonBox" name="buttonBox">
@@ -109,10 +192,12 @@
</layout>
</widget>
<tabstops>
<tabstop>tabWidget</tabstop>
<tabstop>encodingComboBox</tabstop>
<tabstop>foreignKeysCheckBox</tabstop>
<tabstop>locationEdit</tabstop>
<tabstop>setLocationButton</tabstop>
<tabstop>treeSyntaxHighlighting</tabstop>
<tabstop>buttonBox</tabstop>
</tabstops>
<resources/>
@@ -124,8 +209,8 @@
<slot>saveSettings()</slot>
<hints>
<hint type="sourcelabel">
<x>256</x>
<y>214</y>
<x>260</x>
<y>265</y>
</hint>
<hint type="destinationlabel">
<x>155</x>
@@ -140,8 +225,8 @@
<slot>reject()</slot>
<hints>
<hint type="sourcelabel">
<x>324</x>
<y>214</y>
<x>328</x>
<y>265</y>
</hint>
<hint type="destinationlabel">
<x>286</x>
@@ -156,8 +241,8 @@
<slot>chooseLocation()</slot>
<hints>
<hint type="sourcelabel">
<x>485</x>
<y>134</y>
<x>478</x>
<y>107</y>
</hint>
<hint type="destinationlabel">
<x>459</x>
@@ -165,9 +250,26 @@
</hint>
</hints>
</connection>
<connection>
<sender>treeSyntaxHighlighting</sender>
<signal>itemDoubleClicked(QTreeWidgetItem*,int)</signal>
<receiver>PreferencesDialog</receiver>
<slot>showColourDialog(QTreeWidgetItem*,int)</slot>
<hints>
<hint type="sourcelabel">
<x>223</x>
<y>92</y>
</hint>
<hint type="destinationlabel">
<x>395</x>
<y>-1</y>
</hint>
</hints>
</connection>
</connections>
<slots>
<slot>saveSettings()</slot>
<slot>chooseLocation()</slot>
<slot>showColourDialog(QTreeWidgetItem*,int)</slot>
</slots>
</ui>

View File

@@ -1,15 +1,20 @@
#include "SQLiteSyntaxHighlighter.h"
#include "PreferencesDialog.h"
SQLiteSyntaxHighlighter::SQLiteSyntaxHighlighter(QTextDocument *parent) :
QSyntaxHighlighter(parent)
{
HighlightingRule rule;
tableFormat.setForeground(Qt::darkCyan);
tableFormat.setFontWeight(QFont::Bold);
tableFormat.setForeground(QColor(PreferencesDialog::getSettingsValue("syntaxhighlighter", "table_colour").toString()));
tableFormat.setFontWeight(PreferencesDialog::getSettingsValue("syntaxhighlighter", "table_bold").toBool() ? QFont::Bold : QFont::Normal);
tableFormat.setFontItalic(PreferencesDialog::getSettingsValue("syntaxhighlighter", "table_italic").toBool());
tableFormat.setFontUnderline(PreferencesDialog::getSettingsValue("syntaxhighlighter", "table_underline").toBool());
keywordFormat.setForeground(Qt::darkBlue);
keywordFormat.setFontWeight(QFont::Bold);
keywordFormat.setForeground(QColor(PreferencesDialog::getSettingsValue("syntaxhighlighter", "keyword_colour").toString()));
keywordFormat.setFontWeight(PreferencesDialog::getSettingsValue("syntaxhighlighter", "keyword_bold").toBool() ? QFont::Bold : QFont::Normal);
keywordFormat.setFontItalic(PreferencesDialog::getSettingsValue("syntaxhighlighter", "keyword_italic").toBool());
keywordFormat.setFontUnderline(PreferencesDialog::getSettingsValue("syntaxhighlighter", "keyword_underline").toBool());
QStringList keywordPatterns;
keywordPatterns
<< "\\bREINDEX\\b" << "\\bINDEXED\\b" << "\\bINDEX\\b"
@@ -62,13 +67,19 @@ SQLiteSyntaxHighlighter::SQLiteSyntaxHighlighter(QTextDocument *parent) :
}
// single line comment
singleLineCommentFormat.setForeground(Qt::darkGreen);
singleLineCommentFormat.setForeground(QColor(PreferencesDialog::getSettingsValue("syntaxhighlighter", "comment_colour").toString()));
singleLineCommentFormat.setFontWeight(PreferencesDialog::getSettingsValue("syntaxhighlighter", "comment_bold").toBool() ? QFont::Bold : QFont::Normal);
singleLineCommentFormat.setFontItalic(PreferencesDialog::getSettingsValue("syntaxhighlighter", "comment_italic").toBool());
singleLineCommentFormat.setFontUnderline(PreferencesDialog::getSettingsValue("syntaxhighlighter", "comment_underline").toBool());
rule.pattern = QRegExp("--[^\n]*");
rule.format = singleLineCommentFormat;
highlightingRules.append(rule);
// identifiers
identifierFormat.setForeground(Qt::darkMagenta);
identifierFormat.setForeground(QColor(PreferencesDialog::getSettingsValue("syntaxhighlighter", "identifier_colour").toString()));
identifierFormat.setFontWeight(PreferencesDialog::getSettingsValue("syntaxhighlighter", "identifier_bold").toBool() ? QFont::Bold : QFont::Normal);
identifierFormat.setFontItalic(PreferencesDialog::getSettingsValue("syntaxhighlighter", "identifier_italic").toBool());
identifierFormat.setFontUnderline(PreferencesDialog::getSettingsValue("syntaxhighlighter", "identifier_underline").toBool());
rule.pattern = QRegExp("\"[^\"]*\""); // ""
rule.format = identifierFormat;
highlightingRules.append(rule);
@@ -80,7 +91,10 @@ SQLiteSyntaxHighlighter::SQLiteSyntaxHighlighter(QTextDocument *parent) :
highlightingRules.append(rule);
// string
stringFormat.setForeground(Qt::red);
stringFormat.setForeground(QColor(PreferencesDialog::getSettingsValue("syntaxhighlighter", "string_colour").toString()));
stringFormat.setFontWeight(PreferencesDialog::getSettingsValue("syntaxhighlighter", "string_bold").toBool() ? QFont::Bold : QFont::Normal);
stringFormat.setFontItalic(PreferencesDialog::getSettingsValue("syntaxhighlighter", "string_italic").toBool());
stringFormat.setFontUnderline(PreferencesDialog::getSettingsValue("syntaxhighlighter", "string_underline").toBool());
rule.pattern = QRegExp("'[^']*'"); // ''
rule.format = stringFormat;
highlightingRules.append(rule);