Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@ set(SQLB_MOC_HDR
src/FileExtensionManager.h
src/CipherSettings.h
src/DotenvFormat.h
src/Palette.h
src/CondFormat.h
)

set(SQLB_SRC
Expand Down Expand Up @@ -191,6 +193,8 @@ set(SQLB_SRC
src/Data.cpp
src/CipherSettings.cpp
src/DotenvFormat.cpp
src/Palette.cpp
src/CondFormat.cpp
)

set(SQLB_FORMS
Expand Down
111 changes: 111 additions & 0 deletions src/CondFormat.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
#include "CondFormat.h"
#include "Settings.h"
#include "Data.h"

CondFormat::CondFormat(const QString& filter, const QColor& color, const QString& encoding)
: m_filter(filter),
m_color(color)
{
m_sqlCondition = filterToSqlCondition(filter, encoding);
}

QString CondFormat::filterToSqlCondition(const QString& value, const QString& encoding)
{
// Check for any special comparison operators at the beginning of the value string. If there are none default to LIKE.
QString op = "LIKE";
QString val, val2;
QString escape;
bool numeric = false, ok = false;

// range/BETWEEN operator
if (value.contains("~")) {
int sepIdx = value.indexOf('~');
val = value.mid(0, sepIdx);
val2 = value.mid(sepIdx+1);
val.toFloat(&ok);
if (ok) {
val2.toFloat(&ok);
ok = ok && (val.toFloat() < val2.toFloat());
}
}
if (ok) {
op = "BETWEEN";
numeric = true;
} else {
val.clear();
val2.clear();
if(value.left(2) == ">=" || value.left(2) == "<=" || value.left(2) == "<>")
{
// Check if we're filtering for '<> NULL'. In this case we need a special comparison operator.
if(value.left(2) == "<>" && value.mid(2) == "NULL")
{
// We are filtering for '<>NULL'. Override the comparison operator to search for NULL values in this column. Also treat search value (NULL) as number,
// in order to avoid putting quotes around it.
op = "IS NOT";
numeric = true;
val = "NULL";
} else if(value.left(2) == "<>" && value.mid(2) == "''") {
// We are filtering for "<>''", i.e. for everything which is not an empty string
op = "<>";
numeric = true;
val = "''";
} else {
value.mid(2).toFloat(&numeric);
op = value.left(2);
val = value.mid(2);
}
} else if(value.left(1) == ">" || value.left(1) == "<") {
value.mid(1).toFloat(&numeric);
op = value.left(1);
val = value.mid(1);
} else if(value.left(1) == "=") {
val = value.mid(1);

// Check if value to compare with is 'NULL'
if(val != "NULL")
{
// It's not, so just compare normally to the value, whatever it is.
op = "=";
} else {
// It is NULL. Override the comparison operator to search for NULL values in this column. Also treat search value (NULL) as number,
// in order to avoid putting quotes around it.
op = "IS";
numeric = true;
}
} else {
// Keep the default LIKE operator

// Set the escape character if one has been specified in the settings dialog
QString escape_character = Settings::getValue("databrowser", "filter_escape").toString();
if(escape_character == "'") escape_character = "''";
if(escape_character.length())
escape = QString("ESCAPE '%1'").arg(escape_character);

// Add % wildcards at the start and at the beginning of the filter query, but only if there weren't set any
// wildcards manually. The idea is to assume that a user who's just typing characters expects the wildcards to
// be added but a user who adds them herself knows what she's doing and doesn't want us to mess up her query.
if(!value.contains("%"))
{
val = value;
val.prepend('%');
val.append('%');
}
}
}
if(val.isEmpty())
val = value;

if(val == "" || val == "%" || val == "%%")
return QString();
else {
// Quote and escape value, but only if it's not numeric and not the empty string sequence
if(!numeric && val != "''")
val = QString("'%1'").arg(val.replace("'", "''"));

QString whereClause(op + " " + QString(encodeString(val.toUtf8(), encoding)));
if (!val2.isEmpty())
whereClause += " AND " + QString(encodeString(val2.toUtf8(), encoding));
whereClause += " " + escape;
return whereClause;
}
}
28 changes: 28 additions & 0 deletions src/CondFormat.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#ifndef CONDFORMAT_H
#define CONDFORMAT_H

#include <QString>
#include <QColor>

// Conditional formatting for given format to table cells based on a specified condition.
class CondFormat
{
public:
CondFormat() {};
explicit CondFormat(const QString& filter, const QColor& color, const QString& encoding = QString());

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

private:
QString m_sqlCondition;
QString m_filter;
QColor m_color;

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

};

#endif // CONDFORMAT_H
19 changes: 17 additions & 2 deletions src/Data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ bool isTextOnly(QByteArray data, const QString& encoding, bool quickTest)
data = data.left(512);

// Convert to Unicode if necessary
if(!encoding.isEmpty())
data = QTextCodec::codecForName(encoding.toUtf8())->toUnicode(data).toUtf8();
data = decodeString(data, encoding);

// Perform check
return QString(data).toUtf8() == data;
Expand Down Expand Up @@ -65,3 +64,19 @@ QStringList toStringList(const QList<QByteArray> list) {
}
return strings;
}

QByteArray encodeString(const QByteArray& str, const QString& encoding)
{
if(encoding.isEmpty())
return str;
else
return QTextCodec::codecForName(encoding.toUtf8())->fromUnicode(str);
}

QByteArray decodeString(const QByteArray& str, const QString& encoding)
{
if(encoding.isEmpty())
return str;
else
return QTextCodec::codecForName(encoding.toUtf8())->toUnicode(str).toUtf8();
}
5 changes: 5 additions & 0 deletions src/Data.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define DATA_H

#include <QString>
#include <QByteArray>

// This returns false if the data in the data parameter contains binary data. If it is text only, the function returns
// true. If the second parameter is specified, it will be used to convert the data from the given encoding to Unicode
Expand All @@ -19,4 +20,8 @@ QByteArray removeBom(QByteArray& data);

QStringList toStringList(const QList<QByteArray> list);

QByteArray encodeString(const QByteArray& str, const QString& encoding);

QByteArray decodeString(const QByteArray& str, const QString& encoding);

#endif
11 changes: 11 additions & 0 deletions src/FilterLineEdit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,15 @@ void FilterLineEdit::showContextMenu(const QPoint &pos)
// This has to be created here, otherwise the set of enabled options would not update accordingly.
QMenu* editContextMenu = createStandardContextMenu();
editContextMenu->addSeparator();
QString conditionalFormatLabel = text().isEmpty() ? tr("Clear All Conditional Formats") : tr("Use for Conditional Format");
QAction* conditionalFormatAction = new QAction(conditionalFormatLabel, editContextMenu);
connect(conditionalFormatAction, &QAction::triggered, [&]() {
if (text().isEmpty())
emit clearAllCondFormats();
else
emit addFilterAsCondFormat(text());
});
editContextMenu->addSeparator();

QMenu* filterMenu = editContextMenu->addMenu(tr("Set Filter Expression"));

Expand Down Expand Up @@ -164,6 +173,8 @@ void FilterLineEdit::showContextMenu(const QPoint &pos)
setFilterHelper(QString ("?~"));
});

editContextMenu->addAction(conditionalFormatAction);

filterMenu->addAction(whatsThisAction);
filterMenu->addSeparator();
filterMenu->addAction(isNullAction);
Expand Down
2 changes: 2 additions & 0 deletions src/FilterLineEdit.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ private slots:

signals:
void delayedTextChanged(QString text);
void addFilterAsCondFormat(QString text);
void clearAllCondFormats();

protected:
void keyReleaseEvent(QKeyEvent* event) override;
Expand Down
14 changes: 14 additions & 0 deletions src/FilterTableHeader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ void FilterTableHeader::generateFilters(int number, bool showFirst)
else
l->setVisible(true);
connect(l, SIGNAL(delayedTextChanged(QString)), this, SLOT(inputChanged(QString)));
connect(l, SIGNAL(addFilterAsCondFormat(QString)), this, SLOT(addFilterAsCondFormat(QString)));
connect(l, SIGNAL(clearAllCondFormats()), this, SLOT(clearAllCondFormats()));
filterWidgets.push_back(l);
}

Expand Down Expand Up @@ -86,6 +88,18 @@ void FilterTableHeader::inputChanged(const QString& new_value)
emit filterChanged(sender()->property("column").toInt(), new_value);
}

void FilterTableHeader::addFilterAsCondFormat(const QString& filter)
{
// Just get the column number and the new value and send them to anybody interested in new conditional formatting
emit addCondFormat(sender()->property("column").toInt(), filter);
}

void FilterTableHeader::clearAllCondFormats()
{
// Just get the column number and send it to anybody responsible or interested in clearing conditional formatting
emit clearAllCondFormats(sender()->property("column").toInt());
}

void FilterTableHeader::clearFilters()
{
for(FilterLineEdit* filterLineEdit : filterWidgets)
Expand Down
4 changes: 4 additions & 0 deletions src/FilterTableHeader.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,16 @@ public slots:

signals:
void filterChanged(int column, QString value);
void addCondFormat(int column, QString filter);
void clearAllCondFormats(int column);

protected:
void updateGeometries() override;

private slots:
void inputChanged(const QString& new_value);
void addFilterAsCondFormat(const QString& filter);
void clearAllCondFormats();

private:
QList<FilterLineEdit*> filterWidgets;
Expand Down
52 changes: 51 additions & 1 deletion src/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "RemoteDatabase.h"
#include "FindReplaceDialog.h"
#include "Data.h"
#include "CondFormat.h"

#include <QFile>
#include <QApplication>
Expand Down Expand Up @@ -127,6 +128,8 @@ void MainWindow::init()

// Set up filters
connect(ui->dataTable->filterHeader(), SIGNAL(filterChanged(int,QString)), this, SLOT(updateFilter(int,QString)));
connect(ui->dataTable->filterHeader(), SIGNAL(addCondFormat(int,QString)), this, SLOT(addCondFormat(int,QString)));
connect(ui->dataTable->filterHeader(), SIGNAL(clearAllCondFormats(int)), this, SLOT(clearAllCondFormats(int)));
connect(m_browseTableModel, SIGNAL(dataChanged(QModelIndex,QModelIndex)), this, SLOT(dataTableSelectionChanged(QModelIndex)));

// Select in table the rows correspoding to the selected points in plot
Expand Down Expand Up @@ -716,7 +719,12 @@ void MainWindow::applyBrowseTableSettings(BrowseDataTableSettings storedData, bo
bool oldState = filterHeader->blockSignals(true);
for(auto filterIt=storedData.filterValues.constBegin();filterIt!=storedData.filterValues.constEnd();++filterIt)
filterHeader->setFilter(filterIt.key(), filterIt.value());
filterHeader->blockSignals(oldState);

// Conditional formats
for(auto formatIt=storedData.condFormats.constBegin(); formatIt!=storedData.condFormats.constEnd(); ++formatIt)
m_browseTableModel->setCondFormats(formatIt.key(), formatIt.value());

filterHeader->blockSignals(oldState);
}

// Encoding
Expand Down Expand Up @@ -2438,6 +2446,21 @@ static void loadBrowseDataTableSettings(BrowseDataTableSettings& settings, QXmlS
xml.skipCurrentElement();
}
}
} else if(xml.name() == "conditional_formats") {
while(xml.readNext() != QXmlStreamReader::EndElement && xml.name() != "conditional_formats") {
if (xml.name() == "column") {
int index = xml.attributes().value("index").toInt();
while(xml.readNext() != QXmlStreamReader::EndElement && xml.name() != "column") {
if(xml.name() == "format") {
CondFormat newCondFormat(xml.attributes().value("condition").toString(),
QColor(xml.attributes().value("color").toString()),
settings.encoding);
settings.condFormats[index].append(newCondFormat);
xml.skipCurrentElement();
}
}
}
}
} else if(xml.name() == "display_formats") {
while(xml.readNext() != QXmlStreamReader::EndElement && xml.name() != "display_formats") {
if (xml.name() == "column") {
Expand Down Expand Up @@ -2700,6 +2723,19 @@ static void saveBrowseDataTableSettings(const BrowseDataTableSettings& object, Q
xml.writeEndElement();
}
xml.writeEndElement();
xml.writeStartElement("conditional_formats");
for(auto iter=object.condFormats.constBegin(); iter!=object.condFormats.constEnd(); ++iter) {
xml.writeStartElement("column");
xml.writeAttribute("index", QString::number(iter.key()));
for(auto format : iter.value()) {
xml.writeStartElement("format");
xml.writeAttribute("condition", format.filter());
xml.writeAttribute("color", format.color().name());
xml.writeEndElement();
}
xml.writeEndElement();
}
xml.writeEndElement();
xml.writeStartElement("display_formats");
for(auto iter=object.displayFormats.constBegin(); iter!=object.displayFormats.constEnd(); ++iter) {
xml.writeStartElement("column");
Expand Down Expand Up @@ -2872,6 +2908,20 @@ void MainWindow::updateFilter(int column, const QString& value)
applyBrowseTableSettings(settings, true);
}

void MainWindow::addCondFormat(int column, const QString& value)
{
CondFormat newCondFormat(value, m_condFormatPalette.nextSerialColor(Palette::appHasDarkTheme()), m_browseTableModel->encoding());
m_browseTableModel->addCondFormat(column, newCondFormat);
browseTableSettings[currentlyBrowsedTableName()].condFormats[column].append(newCondFormat);
}

void MainWindow::clearAllCondFormats(int column)
{
QVector<CondFormat> emptyCondFormatVector = QVector<CondFormat>();
m_browseTableModel->setCondFormats(column, emptyCondFormatVector);
browseTableSettings[currentlyBrowsedTableName()].condFormats[column].clear();
}

void MainWindow::editEncryption()
{
#ifdef ENABLE_SQLCIPHER
Expand Down
Loading