Skip to content

Commit

Permalink
Added toolbar for direct formatting of columns in Data Browser
Browse files Browse the repository at this point in the history
The new toolbar is hidden by default and can be toggled using a button in
the main Data Browser toolbar. Margins and spacing have been updated to
improve appearance of the two toolbars.

These tool buttons apply the format to the columns which are contained in
the selection. This is done by updating or adding a new condition-less
format to the list of conditional formats of those columns.

New style icons from the Silk icon set. Changed existent ones for
coherence.

See issue #1976.
  • Loading branch information
mgrojo committed Sep 29, 2019
1 parent 7541a82 commit 63aabb9
Show file tree
Hide file tree
Showing 20 changed files with 455 additions and 6 deletions.
12 changes: 12 additions & 0 deletions src/CondFormat.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,25 @@ class CondFormat
public:
QString sqlCondition() const { return m_sqlCondition; }
QString filter() const { return m_filter; }

QColor backgroundColor() const { return m_bgColor; }
QColor foregroundColor() const { return m_fgColor; }
void setBackgroundColor(QColor color) { m_bgColor = color; }
void setForegroundColor(QColor color) { m_fgColor = color; }

bool isBold() const { return m_font.bold(); }
bool isItalic() const { return m_font.italic(); }
bool isUnderline() const { return m_font.underline(); }
void setBold(bool value) { m_font.setBold(value); }
void setItalic(bool value) { m_font.setItalic(value); }
void setUnderline(bool value) { m_font.setUnderline(value); }

QFont font() const { return m_font; }
void setFontFamily(const QString &family) { m_font.setFamily(family); }
void setFontPointSize(int pointSize) { m_font.setPointSize(pointSize); }

Alignment alignment() const { return m_align; }
void setAlignment(Alignment value) { m_align = value; }
Qt::AlignmentFlag alignmentFlag() const;
};

Expand Down
2 changes: 2 additions & 0 deletions src/ExtendedTableWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ public slots:
void openFileFromDropEvent(QString);
void selectedRowsToBeDeleted();
void editCondFormats(int column);
// Make the inherited protected signal public
void currentChanged(const QModelIndex &current, const QModelIndex &previous);

private:
void copyMimeData(const QModelIndexList& fromIndices, QMimeData* mimeData, const bool withHeaders, const bool inSQL);
Expand Down
2 changes: 1 addition & 1 deletion src/FilterLineEdit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ void FilterLineEdit::showContextMenu(const QPoint &pos)
emit clearAllCondFormats();
});
} else {
conditionalFormatAction = new QAction(QIcon(":/icons/cond_formats"), tr("Use for Conditional Format"), editContextMenu);
conditionalFormatAction = new QAction(QIcon(":/icons/add_cond_format"), tr("Use for Conditional Format"), editContextMenu);
connect(conditionalFormatAction, &QAction::triggered, [&]() {
emit addFilterAsCondFormat(text());
});
Expand Down
15 changes: 15 additions & 0 deletions src/MainWindow.ui
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,21 @@ You can drag SQL statements from an object row and drop them into other applicat
<string>Browse Data</string>
</attribute>
<layout class="QVBoxLayout" name="verticalLayout_2">
<property name="spacing">
<number>3</number>
</property>
<property name="leftMargin">
<number>3</number>
</property>
<property name="topMargin">
<number>3</number>
</property>
<property name="rightMargin">
<number>3</number>
</property>
<property name="bottomMargin">
<number>3</number>
</property>
<item>
<widget class="TableBrowser" name="tableBrowser" native="true"/>
</item>
Expand Down
138 changes: 138 additions & 0 deletions src/TableBrowser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <QScrollBar>
#include <QShortcut>
#include <QTextCodec>
#include <QColorDialog>

QMap<sqlb::ObjectIdentifier, BrowseDataTableSettings> TableBrowser::browseTableSettings;
QString TableBrowser::defaultBrowseTableEncoding;
Expand Down Expand Up @@ -127,6 +128,114 @@ TableBrowser::TableBrowser(QWidget* parent) :
connect(ui->dataTable->verticalHeader(), &QHeaderView::customContextMenuRequested, this, &TableBrowser::showRecordPopupMenu);
connect(ui->dataTable, &ExtendedTableWidget::openFileFromDropEvent, this, &TableBrowser::requestFileOpen);
connect(ui->dataTable, &ExtendedTableWidget::selectedRowsToBeDeleted, this, &TableBrowser::deleteRecord);

connect(ui->fontComboBox, &QFontComboBox::currentFontChanged, this, [this](const QFont &font) {
modifyColumnFormat(ui->dataTable->selectedCols(), [font](CondFormat& format) { format.setFontFamily(font.family()); });
});
connect(ui->fontSizeBox, QOverload<int>::of(&QSpinBox::valueChanged), this, [this](int pointSize) {
modifyColumnFormat(ui->dataTable->selectedCols(), [pointSize](CondFormat& format) { format.setFontPointSize(pointSize); });
});

connect(ui->actionFontColor, &QAction::triggered, this, [this]() {
QColor color = QColorDialog::getColor(QColor(m_browseTableModel->data(currentIndex(), Qt::ForegroundRole).toString()));
if(color.isValid())
modifyColumnFormat(ui->dataTable->selectedCols(), [color](CondFormat& format) { format.setForegroundColor(color); });
});
connect(ui->actionBackgroundColor, &QAction::triggered, this, [this]() {
QColor color = QColorDialog::getColor(QColor(m_browseTableModel->data(currentIndex(), Qt::BackgroundRole).toString()));
if(color.isValid())
modifyColumnFormat(ui->dataTable->selectedCols(), [color](CondFormat& format) { format.setBackgroundColor(color); });
});

connect(ui->actionBold, &QAction::toggled, this, [this](bool checked) {
modifyColumnFormat(ui->dataTable->selectedCols(), [checked](CondFormat& format) { format.setBold(checked); });
});
connect(ui->actionItalic, &QAction::toggled, this, [this](bool checked) {
modifyColumnFormat(ui->dataTable->selectedCols(), [checked](CondFormat& format) { format.setItalic(checked); });
});
connect(ui->actionUnderline, &QAction::toggled, this, [this](bool checked) {
modifyColumnFormat(ui->dataTable->selectedCols(), [checked](CondFormat& format) { format.setUnderline(checked); });
});

connect(ui->actionLeftAlign, &QAction::triggered, this, [this]() {
ui->actionLeftAlign->setChecked(true);
ui->actionRightAlign->setChecked(false);
ui->actionCenter->setChecked(false);
ui->actionJustify->setChecked(false);
modifyColumnFormat(ui->dataTable->selectedCols(), [](CondFormat& format) { format.setAlignment(CondFormat::AlignLeft); });
});
connect(ui->actionRightAlign, &QAction::triggered, this, [this]() {
ui->actionLeftAlign->setChecked(false);
ui->actionRightAlign->setChecked(true);
ui->actionCenter->setChecked(false);
ui->actionJustify->setChecked(false);
modifyColumnFormat(ui->dataTable->selectedCols(), [](CondFormat& format) { format.setAlignment(CondFormat::AlignRight); });
});
connect(ui->actionCenter, &QAction::triggered, this, [this]() {
ui->actionLeftAlign->setChecked(false);
ui->actionRightAlign->setChecked(false);
ui->actionCenter->setChecked(true);
ui->actionJustify->setChecked(false);
modifyColumnFormat(ui->dataTable->selectedCols(), [](CondFormat& format) { format.setAlignment(CondFormat::AlignCenter); });
});
connect(ui->actionJustify, &QAction::triggered, this, [this]() {
ui->actionLeftAlign->setChecked(false);
ui->actionRightAlign->setChecked(false);
ui->actionCenter->setChecked(false);
ui->actionJustify->setChecked(true);
modifyColumnFormat(ui->dataTable->selectedCols(), [](CondFormat& format) { format.setAlignment(CondFormat::AlignJustify); });
});

connect(ui->actionEditCondFormats, &QAction::triggered, this, [this]() { editCondFormats(currentIndex().column()); });
connect(ui->actionClearFormat, &QAction::triggered, this, [this]() {
for (int column : ui->dataTable->selectedCols())
clearAllCondFormats(column);
});

connect(ui->dataTable, &ExtendedTableWidget::currentChanged, this, [this](const QModelIndex &current, const QModelIndex &) {
// Get cell current format for updating the format toolbar values. Block signals, so the format change is not reapplied.
QFont font = QFont(m_browseTableModel->data(current, Qt::FontRole).toString());
ui->fontComboBox->blockSignals(true);
ui->fontComboBox->setCurrentFont(font);
ui->fontComboBox->blockSignals(false);

ui->fontSizeBox->blockSignals(true);
ui->fontSizeBox->setValue(font.pointSize());
ui->fontSizeBox->blockSignals(false);

ui->actionBold->blockSignals(true);
ui->actionBold->setChecked(font.bold());
ui->actionBold->blockSignals(false);

ui->actionItalic->blockSignals(true);
ui->actionItalic->setChecked(font.italic());
ui->actionItalic->blockSignals(false);

ui->actionUnderline->blockSignals(true);
ui->actionUnderline->setChecked(font.underline());
ui->actionUnderline->blockSignals(false);

Qt::Alignment align = Qt::Alignment(m_browseTableModel->data(current, Qt::TextAlignmentRole).toInt());
ui->actionLeftAlign->blockSignals(true);
ui->actionLeftAlign->setChecked(align.testFlag(Qt::AlignLeft));
ui->actionLeftAlign->blockSignals(false);

ui->actionRightAlign->blockSignals(true);
ui->actionRightAlign->setChecked(align.testFlag(Qt::AlignRight));
ui->actionRightAlign->blockSignals(false);

ui->actionCenter->blockSignals(true);
ui->actionCenter->setChecked(align.testFlag(Qt::AlignCenter));
ui->actionCenter->blockSignals(false);

ui->actionJustify->blockSignals(true);
ui->actionJustify->setChecked(align.testFlag(Qt::AlignJustify));
ui->actionJustify->blockSignals(false);
});

connect(ui->actionToggleFormatToolbar, &QAction::toggled, ui->formatFrame, &QFrame::setVisible);
ui->actionToggleFormatToolbar->setChecked(false);
ui->formatFrame->setVisible(false);
}

TableBrowser::~TableBrowser()
Expand Down Expand Up @@ -473,6 +582,35 @@ void TableBrowser::editCondFormats(int column)
}
}

void TableBrowser::modifyColumnFormat(std::unordered_set<int> columns, std::function<void(CondFormat&)> changeFunction)
{
for (int column : columns) {
std::vector<CondFormat>& columnFormats = browseTableSettings[currentlyBrowsedTableName()].condFormats[column];

auto it = std::find_if(columnFormats.begin(), columnFormats.end(), [](const CondFormat& format) {
return format.sqlCondition().isEmpty();
});
if(it != columnFormats.end()) {
changeFunction(*it);
m_browseTableModel->addCondFormat(column, *it);
} else {

QFont font = QFont(Settings::getValue("databrowser", "font").toString());
font.setPointSize(Settings::getValue("databrowser", "fontsize").toInt());

CondFormat newCondFormat(QString(""), QColor(Settings::getValue("databrowser", "reg_fg_colour").toString()),
QColor(Settings::getValue("databrowser", "reg_bg_colour").toString()),
font,
CondFormat::AlignLeft,
m_browseTableModel->encoding());
changeFunction(newCondFormat);
m_browseTableModel->addCondFormat(column, newCondFormat);
browseTableSettings[currentlyBrowsedTableName()].condFormats[column].push_back(newCondFormat);
}
}
emit projectModified();
}

void TableBrowser::updateRecordsetLabel()
{
// Get all the numbers, i.e. the number of the first row and the last row as well as the total number of rows
Expand Down
4 changes: 4 additions & 0 deletions src/TableBrowser.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#include <QModelIndex>
#include <QWidget>

#include <unordered_set>

class DBBrowserDB;
class ExtendedTableWidget;
class SqliteTableModel;
Expand Down Expand Up @@ -169,6 +171,8 @@ private slots:
static QString defaultBrowseTableEncoding;

Palette m_condFormatPalette;

void modifyColumnFormat(std::unordered_set<int> columns, std::function<void(CondFormat&)> changeFunction);
};

#endif
Loading

0 comments on commit 63aabb9

Please sign in to comment.