From d941073c257e0ab3af6a64eb9237a8f47c676167 Mon Sep 17 00:00:00 2001 From: Martin Kleusberg Date: Sat, 30 Mar 2013 15:06:26 +0100 Subject: [PATCH] Make syntax highlighting configurable Add new options to the preferences dialog to change the colours and text styles used in the SQL syntax highlighter. --- src/MainWindow.cpp | 21 ++- src/MainWindow.h | 1 + src/PreferencesDialog.cpp | 73 +++++++++ src/PreferencesDialog.h | 2 + src/PreferencesDialog.ui | 252 ++++++++++++++++++++++---------- src/SQLiteSyntaxHighlighter.cpp | 28 +++- 6 files changed, 291 insertions(+), 86 deletions(-) diff --git a/src/MainWindow.cpp b/src/MainWindow.cpp index 1c5f22f8..94575d82 100644 --- a/src/MainWindow.cpp +++ b/src/MainWindow.cpp @@ -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 //****************************************************************** diff --git a/src/MainWindow.h b/src/MainWindow.h index 53812d3a..2a9c38f1 100644 --- a/src/MainWindow.h +++ b/src/MainWindow.h @@ -140,6 +140,7 @@ private slots: virtual void savePragmas(); virtual void mainTabSelected( int tabindex ); virtual void browseTableHeaderClicked(int logicalindex); + virtual void createSyntaxHighlighters(); }; #endif diff --git a/src/PreferencesDialog.cpp b/src/PreferencesDialog.cpp index 9858ca2b..481d4b91 100644 --- a/src/PreferencesDialog.cpp +++ b/src/PreferencesDialog.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include "sqlitedb.h" QHash 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;itreeSyntaxHighlighting->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;itreeSyntaxHighlighting->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()); +} diff --git a/src/PreferencesDialog.h b/src/PreferencesDialog.h index 229643e1..a35b325c 100644 --- a/src/PreferencesDialog.h +++ b/src/PreferencesDialog.h @@ -4,6 +4,7 @@ #include #include #include +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; diff --git a/src/PreferencesDialog.ui b/src/PreferencesDialog.ui index bdb42048..dd45d4d5 100644 --- a/src/PreferencesDialog.ui +++ b/src/PreferencesDialog.ui @@ -7,7 +7,7 @@ 0 0 492 - 135 + 276 @@ -18,83 +18,166 @@ - - - QFormLayout::ExpandingFieldsGrow + + + 0 - - - - Database Encoding + + + &Database + + + + QFormLayout::ExpandingFieldsGrow - - encodingComboBox - - - - - - - - UTF-8 - - - - - UTF-16 - - - - - - - - Open databases with foreign keys enabled. - - - Foreign keys - - - foreignKeysCheckBox - - - - - - - enabled - - - - - - - Default location - - - locationEdit - - - - - - - - - false + + + + Database &encoding + + + encodingComboBox - - - - ... + + + + + UTF-8 + + + + + UTF-16 + + + + + + + + Open databases with foreign keys enabled. + + &Foreign keys + + + foreignKeysCheckBox + + + + + + + enabled + + + + + + + Default &location + + + locationEdit + + + + + + + + + false + + + + + + + ... + + + + + + + + + + &SQL + + + + + + false + + + false + + + + Context + + + + + Colour + + + + + Bold + + + + + Italic + + + + + Underline + + + + + Keyword + + + + + + + + + + + Table + + + + + Comment + + + + + Identifier + + + + + String + + - - + + @@ -109,10 +192,12 @@ + tabWidget encodingComboBox foreignKeysCheckBox locationEdit setLocationButton + treeSyntaxHighlighting buttonBox @@ -124,8 +209,8 @@ saveSettings() - 256 - 214 + 260 + 265 155 @@ -140,8 +225,8 @@ reject() - 324 - 214 + 328 + 265 286 @@ -156,8 +241,8 @@ chooseLocation() - 485 - 134 + 478 + 107 459 @@ -165,9 +250,26 @@ + + treeSyntaxHighlighting + itemDoubleClicked(QTreeWidgetItem*,int) + PreferencesDialog + showColourDialog(QTreeWidgetItem*,int) + + + 223 + 92 + + + 395 + -1 + + + saveSettings() chooseLocation() + showColourDialog(QTreeWidgetItem*,int) diff --git a/src/SQLiteSyntaxHighlighter.cpp b/src/SQLiteSyntaxHighlighter.cpp index f9ccb7e3..18f313ee 100644 --- a/src/SQLiteSyntaxHighlighter.cpp +++ b/src/SQLiteSyntaxHighlighter.cpp @@ -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);