Skip to content

Commit

Permalink
Dialog and foreground configuration for conditional formats
Browse files Browse the repository at this point in the history
A new dialog for editing conditional formats that can be invoked from the
filter line editor or from the data browser contextual menus. The dialog
allows adding and removing conditional formats, changing the priority order
and editing foreground colour, background colour and filter condition.

The conditional formats have been expanded to allow defining the foreground
colour. By default is the setting configured by user.

This is a continuation of the functionality introduced in PR sqlitebrowser#1503.
  • Loading branch information
mgrojo committed Mar 23, 2019
1 parent 2d35206 commit 217563f
Show file tree
Hide file tree
Showing 21 changed files with 466 additions and 34 deletions.
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ set(SQLB_MOC_HDR
src/FindReplaceDialog.h
src/ExtendedScintilla.h
src/FileExtensionManager.h
src/CondFormatManager.h
src/CipherSettings.h
src/DotenvFormat.h
src/Palette.h
Expand Down Expand Up @@ -200,6 +201,7 @@ set(SQLB_SRC
src/FindReplaceDialog.cpp
src/ExtendedScintilla.cpp
src/FileExtensionManager.cpp
src/CondFormatManager.cpp
src/Data.cpp
src/CipherSettings.cpp
src/DotenvFormat.cpp
Expand Down Expand Up @@ -228,6 +230,7 @@ set(SQLB_FORMS
src/RemotePushDialog.ui
src/FindReplaceDialog.ui
src/FileExtensionManager.ui
src/CondFormatManager.ui
)

set(SQLB_RESOURCES
Expand Down
8 changes: 5 additions & 3 deletions src/CondFormat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
#include "Settings.h"
#include "Data.h"

CondFormat::CondFormat(const QString& filter, const QColor& color, const QString& encoding)
CondFormat::CondFormat(const QString& filter, const QColor& foreground, const QColor& background, const QString& encoding)
: m_filter(filter),
m_color(color)
m_fgColor(foreground),
m_bgColor(background)
{
m_sqlCondition = filterToSqlCondition(filter, encoding);
if (!filter.isEmpty())
m_sqlCondition = filterToSqlCondition(filter, encoding);
}

QString CondFormat::filterToSqlCondition(const QString& value, const QString& encoding)
Expand Down
8 changes: 5 additions & 3 deletions src/CondFormat.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,21 @@ class CondFormat
{
public:
CondFormat() {};
explicit CondFormat(const QString& filter, const QColor& color, const QString& encoding = QString());
explicit CondFormat(const QString& filter, const QColor& foreground, const QColor& background, const QString& encoding = QString());

static QString filterToSqlCondition(const QString& value, const QString& encoding = QString());

private:
QString m_sqlCondition;
QString m_filter;
QColor m_color;
QColor m_bgColor;
QColor m_fgColor;

public:
QString sqlCondition() const { return m_sqlCondition; };
QString filter() const { return m_filter; };
QColor color() const { return m_color; };
QColor backgroundColor() const { return m_bgColor; };
QColor foregroundColor() const { return m_fgColor; };

};

Expand Down
124 changes: 124 additions & 0 deletions src/CondFormatManager.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
#include "CondFormatManager.h"
#include "ui_CondFormatManager.h"
#include "CondFormat.h"
#include "Settings.h"

#include "QColorDialog"

CondFormatManager::CondFormatManager(const QVector<CondFormat>& condFormats, const QString& encoding, QWidget *parent) :
QDialog(parent),
ui(new Ui::CondFormatManager),
m_condFormats(condFormats),
m_encoding(encoding)
{
ui->setupUi(this);

for(const CondFormat& aCondFormat : condFormats)
addItem(aCondFormat);

ui->tableCondFormats->setEditTriggers(QAbstractItemView::AllEditTriggers);

connect(ui->buttonAdd, SIGNAL(clicked(bool)), this, SLOT(addNewItem()));
connect(ui->buttonRemove, SIGNAL(clicked(bool)), this, SLOT(removeItem()));

connect(ui->buttonDown, SIGNAL(clicked(bool)), this, SLOT(downItem()));
connect(ui->buttonUp, SIGNAL(clicked(bool)), this, SLOT(upItem()));

connect(ui->tableCondFormats, &QTreeWidget::itemDoubleClicked, this, &CondFormatManager::itemDoubleClicked);
}

CondFormatManager::~CondFormatManager()
{
delete ui;
}

void CondFormatManager::addNewItem()
{
CondFormat newCondFormat("", QColor(Settings::getValue("databrowser", "reg_fg_colour").toString()),
m_condFormatPalette.nextSerialColor(Palette::appHasDarkTheme()),
m_encoding);
addItem(newCondFormat);
}

void CondFormatManager::addItem(const CondFormat& aCondFormat)
{
int i = ui->tableCondFormats->topLevelItemCount();
QTreeWidgetItem *newItem = new QTreeWidgetItem({aCondFormat.foregroundColor().name(),
aCondFormat.backgroundColor().name(), aCondFormat.filter()});
newItem->setForeground(ColumnForeground, aCondFormat.foregroundColor());
newItem->setBackground(ColumnForeground, aCondFormat.foregroundColor());
newItem->setForeground(ColumnBackground, aCondFormat.backgroundColor());
newItem->setBackground(ColumnBackground, aCondFormat.backgroundColor());
ui->tableCondFormats->insertTopLevelItem(i, newItem);
ui->tableCondFormats->openPersistentEditor(newItem, ColumnFilter);
}

void CondFormatManager::removeItem()
{
QTreeWidgetItem* item = ui->tableCondFormats->takeTopLevelItem(ui->tableCondFormats->currentIndex().row());
delete item;
}

void CondFormatManager::upItem()
{
if (ui->tableCondFormats->selectedItems().isEmpty()) return;

int selectedRow = ui->tableCondFormats->currentIndex().row();
if(selectedRow == 0)
return;

QTreeWidgetItem* item;
item = ui->tableCondFormats->takeTopLevelItem(selectedRow);
ui->tableCondFormats->insertTopLevelItem(selectedRow-1, item);
ui->tableCondFormats->setCurrentIndex(ui->tableCondFormats->currentIndex().sibling(selectedRow-1,
ui->tableCondFormats->currentIndex().column()));
}

void CondFormatManager::downItem()
{
if (ui->tableCondFormats->selectedItems().isEmpty()) return;

int selectedRow = ui->tableCondFormats->currentIndex().row();
if(selectedRow == ui->tableCondFormats->topLevelItemCount() - 1)
return;

QTreeWidgetItem* item;
item = ui->tableCondFormats->takeTopLevelItem(selectedRow);
ui->tableCondFormats->insertTopLevelItem(selectedRow+1, item);
ui->tableCondFormats->setCurrentIndex(ui->tableCondFormats->currentIndex().sibling(selectedRow+1,
ui->tableCondFormats->currentIndex().column()));
}

QVector<CondFormat> CondFormatManager::getCondFormats()
{
QVector<CondFormat> result;
for (int i = 0; i < ui->tableCondFormats->topLevelItemCount(); ++i)
{
QTreeWidgetItem* item = ui->tableCondFormats->topLevelItem(i);
CondFormat aCondFormat(item->text(ColumnFilter),
item->background(ColumnForeground).color(),
item->background(ColumnBackground).color(), m_encoding);
result.append(aCondFormat);
}
return result;
}


void CondFormatManager::itemDoubleClicked(QTreeWidgetItem* item, int column)
{
switch (column) {
case ColumnForeground:
case ColumnBackground: {
QColor color = QColorDialog::getColor(item->background(column).color(), this);
if(color.isValid()) {
item->setTextColor(column, color);
item->setBackgroundColor(column, color);
item->setText(column, color.name());
}
break;
}
case ColumnFilter:
// Nothing to do
break;
}
}
46 changes: 46 additions & 0 deletions src/CondFormatManager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#ifndef CONDFORMATMANAGER_H
#define CONDFORMATMANAGER_H

#include <QDialog>

#include "Palette.h"

namespace Ui {
class CondFormatManager;
}

class CondFormat;
class QTreeWidgetItem;

class CondFormatManager : public QDialog
{
Q_OBJECT

public:
explicit CondFormatManager(const QVector<CondFormat>& condFormats, const QString& encoding, QWidget *parent = nullptr);
~CondFormatManager() override;

QVector<CondFormat> getCondFormats();
private:
enum Columns {
ColumnForeground = 0,
ColumnBackground = 1,
ColumnFilter = 2
};
Ui::CondFormatManager *ui;
QVector<CondFormat> m_condFormats;
Palette m_condFormatPalette;
QString m_encoding;

private slots:
void addNewItem();
void addItem(const CondFormat& aCondFormat);
void removeItem();
void upItem();
void downItem();

public slots:
void itemDoubleClicked(QTreeWidgetItem* item, int column);
};

#endif // CONDFORMATMANAGER_H
Loading

0 comments on commit 217563f

Please sign in to comment.