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

@@ -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());
}