From 994d37f99d619c456d15cf2352e0ce1a2210fc5c Mon Sep 17 00:00:00 2001 From: Samir Aguiar Date: Wed, 11 Mar 2015 01:29:05 +0100 Subject: [PATCH] preferencesdialog: Add NULL fields style options to Data Browser tab This adds the ability to customize the text, font colour and background colour of NULL fields. --- src/PreferencesDialog.cpp | 71 +++++++++++++++ src/PreferencesDialog.h | 7 +- src/PreferencesDialog.ui | 176 ++++++++++++++++++++++++++++++++++++++ src/sqlitetablemodel.cpp | 8 +- 4 files changed, 257 insertions(+), 5 deletions(-) diff --git a/src/PreferencesDialog.cpp b/src/PreferencesDialog.cpp index e65495fe..295ccdfb 100644 --- a/src/PreferencesDialog.cpp +++ b/src/PreferencesDialog.cpp @@ -7,6 +7,7 @@ #include #include #include +#include QHash PreferencesDialog::m_hCache; @@ -17,6 +18,9 @@ PreferencesDialog::PreferencesDialog(QWidget* parent) ui->setupUi(this); ui->treeSyntaxHighlighting->setColumnHidden(0, true); + ui->frameNullBgColour->installEventFilter(this); + ui->frameNullFgColour->installEventFilter(this); + #ifndef CHECKNEWVERSION ui->labelUpdates->setVisible(false); ui->checkUpdates->setVisible(false); @@ -54,6 +58,18 @@ void PreferencesDialog::loadSettings() ui->foreignKeysCheckBox->setChecked(getSettingsValue("db", "foreignkeys").toBool()); ui->spinPrefetchSize->setValue(getSettingsValue("db", "prefetchsize").toInt()); + QPalette palette = ui->frameNullBgColour->palette(); + palette.setColor(ui->frameNullBgColour->backgroundRole(), + QColor(getSettingsValue("databrowser", "null_bg_colour").toString())); + ui->frameNullBgColour->setPalette(palette); + + palette = ui->frameNullFgColour->palette(); + palette.setColor(ui->frameNullFgColour->backgroundRole(), + QColor(getSettingsValue("databrowser", "null_fg_colour").toString())); + ui->frameNullFgColour->setPalette(palette); + + ui->txtNull->setText(getSettingsValue("databrowser", "null_text").toString()); + for(int i=0; i < ui->treeSyntaxHighlighting->topLevelItemCount(); ++i) { QString name = ui->treeSyntaxHighlighting->topLevelItem(i)->text(0); @@ -86,6 +102,12 @@ void PreferencesDialog::saveSettings() setSettingsValue("checkversion", "enabled", ui->checkUpdates->isChecked()); + setSettingsValue("databrowser", "null_bg_colour", + ui->frameNullBgColour->palette().color(ui->frameNullBgColour->backgroundRole())); + setSettingsValue("databrowser", "null_fg_colour", + ui->frameNullFgColour->palette().color(ui->frameNullFgColour->backgroundRole())); + setSettingsValue("databrowser", "null_text", ui->txtNull->text()); + for(int i=0; i < ui->treeSyntaxHighlighting->topLevelItemCount(); ++i) { QString name = ui->treeSyntaxHighlighting->topLevelItem(i)->text(0); @@ -190,6 +212,17 @@ QVariant PreferencesDialog::getSettingsDefaultValue(const QString& group, const if(group == "checkversion" && name == "enabled") return true; + // Data Browser/NULL Fields + if(group == "databrowser") + { + if (name == "null_text") + return "NULL"; + if (name == "null_fg_colour") + return QColor(Qt::lightGray).name(); + if (name == "null_bg_colour") + return QColor(Qt::white).name(); + } + // syntaxhighlighter? if(group == "syntaxhighlighter") { @@ -255,6 +288,44 @@ void PreferencesDialog::showColourDialog(QTreeWidgetItem* item, int column) } } +bool PreferencesDialog::eventFilter(QObject *obj, QEvent *event) +{ + // Use mouse click and enter press on the frames to pop up a colour dialog + if (obj == ui->frameNullBgColour || obj == ui->frameNullFgColour) + { + if (event->type() == QEvent::KeyPress) + { + QKeyEvent *key = static_cast(event); + // Not interesting, so send to the parent (might be shortcuts) + if((key->key() != Qt::Key_Enter) && (key->key() != Qt::Key_Return)) + { + return false; + } + } + else if (event->type() != QEvent::MouseButtonPress) + { + // Not a key event neither a mouse event, send to the parent + return false; + } + + QFrame *frame = qobject_cast(obj); + QColor oldColour = frame->palette().color(frame->backgroundRole()); + QColor colour = QColorDialog::getColor(oldColour, frame); + + if (colour.isValid()) + { + QPalette palette = frame->palette(); + palette.setColor(frame->backgroundRole(), colour); + frame->setPalette(palette); + } + // Consume + return true; + } + + // Send any other events to the parent + return QDialog::eventFilter(obj, event); +} + void PreferencesDialog::addExtension() { QString file = QFileDialog::getOpenFileName( diff --git a/src/PreferencesDialog.h b/src/PreferencesDialog.h index dab9517a..78a15ce9 100644 --- a/src/PreferencesDialog.h +++ b/src/PreferencesDialog.h @@ -38,10 +38,13 @@ private: // This works similar to getSettingsValue but returns the default value instead of the value set by the user static QVariant getSettingsDefaultValue(const QString& group, const QString& name); - void fillLanguageBox(); - // Cache for storing the settings to avoid repeatedly reading the settings file all the time static QHash m_hCache; + + void fillLanguageBox(); + +protected: + bool eventFilter(QObject *obj, QEvent *event); }; #endif diff --git a/src/PreferencesDialog.ui b/src/PreferencesDialog.ui index e28c17e8..5214cb66 100644 --- a/src/PreferencesDialog.ui +++ b/src/PreferencesDialog.ui @@ -228,6 +228,179 @@ Data &Browser + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + + + false + + + QGroupBox { + border: 1px solid gray; + border-radius: 4px; + margin-top: 1ex; +} + +QGroupBox::title { + subcontrol-origin: margin; + subcontrol-position: top left; + left: 10px; + padding: 0 3px 0 3px; +} + + + NULL fields + + + false + + + + 50 + + + 50 + + + + + + 70 + 20 + + + + Qt::StrongFocus + + + true + + + QFrame::Box + + + QFrame::Plain + + + + + + + + + + 15 + + + + + + + + 70 + 20 + + + + Qt::StrongFocus + + + true + + + QFrame::Box + + + QFrame::Plain + + + + + + + Text &colour + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + frameNullFgColour + + + + + + + &Text + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + txtNull + + + + + + + Bac&kground colour + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + frameNullBgColour + + + + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + @@ -530,6 +703,9 @@ foreignKeysCheckBox checkHideSchemaLinebreaks spinPrefetchSize + txtNull + frameNullFgColour + frameNullBgColour treeSyntaxHighlighting spinEditorFontSize spinLogFontSize diff --git a/src/sqlitetablemodel.cpp b/src/sqlitetablemodel.cpp index 7352b84b..85df8dec 100644 --- a/src/sqlitetablemodel.cpp +++ b/src/sqlitetablemodel.cpp @@ -231,6 +231,8 @@ QVariant SqliteTableModel::data(const QModelIndex &index, int role) const if(role == Qt::DisplayRole && isBinary(index)) return "BLOB"; + else if(m_data.at(index.row()).at(index.column()).isNull() || isBinary(index)) + return PreferencesDialog::getSettingsValue("databrowser", "null_text").toString(); else return m_data.at(index.row()).at(index.column()); } else if(role == Qt::FontRole) { @@ -240,11 +242,11 @@ QVariant SqliteTableModel::data(const QModelIndex &index, int role) const return font; } else if(role == Qt::TextColorRole) { if(m_data.at(index.row()).at(index.column()).isNull() || isBinary(index)) - return QColor(Qt::gray); - return QColor(Qt::black); + return QColor(PreferencesDialog::getSettingsValue("databrowser", "null_fg_colour").toString()); + return QVariant(); } else if (role == Qt::BackgroundRole) { if(m_data.at(index.row()).at(index.column()).isNull() || isBinary(index)) - return QColor(PreferencesDialog::getSettingsValue("syntaxhighlighter", "null_colour").toString()); + return QColor(PreferencesDialog::getSettingsValue("databrowser", "null_bg_colour").toString()); return QVariant(); } else { return QVariant();