Skip to content

Commit

Permalink
Added font and point size selection to conditional formats
Browse files Browse the repository at this point in the history
Added font combo box and spin box for selecting font and font point size
of a conditional format. The default for a new conditional format is the
corresponding setting.

Minor dialog adjustments for better display.

See issues sqlitebrowser#1976 and sqlitebrowser#1815.
  • Loading branch information
mgrojo committed Sep 21, 2019
1 parent 412c239 commit 1ec502a
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 30 deletions.
72 changes: 49 additions & 23 deletions src/CondFormatManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#include <QUrl>
#include <QPushButton>
#include <QMessageBox>
#include <QFontComboBox>
#include <QSpinBox>

CondFormatManager::CondFormatManager(const std::vector<CondFormat>& condFormats, const QString& encoding, QWidget *parent) :
QDialog(parent),
Expand All @@ -17,13 +19,13 @@ CondFormatManager::CondFormatManager(const std::vector<CondFormat>& condFormats,
{
ui->setupUi(this);

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

// Resize columns to contents, except for the condition
for(int col = ColumnForeground; col < ColumnFilter; ++col)
ui->tableCondFormats->resizeColumnToContents(col);

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

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

connect(ui->buttonAdd, SIGNAL(clicked(bool)), this, SLOT(addNewItem()));
Expand Down Expand Up @@ -52,16 +54,27 @@ void CondFormatManager::addNewItem()
void CondFormatManager::addItem(const CondFormat& aCondFormat)
{
int i = ui->tableCondFormats->topLevelItemCount();
QTreeWidgetItem *newItem = new QTreeWidgetItem({"", "", "", "", "", aCondFormat.filter()});
QTreeWidgetItem *newItem = new QTreeWidgetItem(ui->tableCondFormats);
newItem->setForeground(ColumnForeground, aCondFormat.foregroundColor());
newItem->setBackground(ColumnForeground, aCondFormat.foregroundColor());
newItem->setForeground(ColumnBackground, aCondFormat.backgroundColor());
newItem->setBackground(ColumnBackground, aCondFormat.backgroundColor());
newItem->setToolTip(ColumnBackground, tr("Click to select color"));
newItem->setToolTip(ColumnForeground, tr("Click to select color"));

QFontComboBox* fontCombo = new QFontComboBox(ui->tableCondFormats);
fontCombo->setCurrentFont(aCondFormat.font());
ui->tableCondFormats->setItemWidget(newItem, ColumnFont, fontCombo);

QSpinBox* sizeBox = new QSpinBox(ui->tableCondFormats);
sizeBox->setMinimum(1);
sizeBox->setValue(aCondFormat.font().pointSize());
ui->tableCondFormats->setItemWidget(newItem, ColumnSize, sizeBox);

newItem->setCheckState(ColumnBold, aCondFormat.isBold() ? Qt::Checked : Qt::Unchecked);
newItem->setCheckState(ColumnItalic, aCondFormat.isItalic() ? Qt::Checked : Qt::Unchecked);
newItem->setCheckState(ColumnUnderline, aCondFormat.isUnderline() ? Qt::Checked : Qt::Unchecked);
newItem->setText(ColumnFilter, aCondFormat.filter());
ui->tableCondFormats->insertTopLevelItem(i, newItem);
ui->tableCondFormats->openPersistentEditor(newItem, ColumnFilter);
}
Expand All @@ -72,48 +85,61 @@ void CondFormatManager::removeItem()
delete item;
}

void CondFormatManager::upItem()
void CondFormatManager::moveItem(int offset)
{
if (!ui->tableCondFormats->currentIndex().isValid())
return;

int selectedRow = ui->tableCondFormats->currentIndex().row();
if(selectedRow == 0)
int newRow = selectedRow + offset;
if(newRow < 0 || newRow >= ui->tableCondFormats->topLevelItemCount())
return;

QTreeWidgetItem* item;
QTreeWidgetItem* item = ui->tableCondFormats->topLevelItem(selectedRow);

// Rescue widgets, since they will be deleted, and add them later.
QFontComboBox* fontCombo = qobject_cast<QFontComboBox*>(ui->tableCondFormats->itemWidget(item, ColumnFont));
QFontComboBox* fontCombo2 = new QFontComboBox(ui->tableCondFormats);
fontCombo2->setCurrentFont(fontCombo->currentFont());

QSpinBox* sizeBox = qobject_cast<QSpinBox*>(ui->tableCondFormats->itemWidget(item, ColumnSize));
QSpinBox* sizeBox2 = new QSpinBox(ui->tableCondFormats);
sizeBox2->setValue(sizeBox->value());
sizeBox2->setMinimum(sizeBox->minimum());

item = ui->tableCondFormats->takeTopLevelItem(selectedRow);
ui->tableCondFormats->insertTopLevelItem(selectedRow-1, item);
ui->tableCondFormats->insertTopLevelItem(newRow, item);

// Restore widgets and state
ui->tableCondFormats->setItemWidget(item, ColumnFont, fontCombo2);
ui->tableCondFormats->setItemWidget(item, ColumnSize, sizeBox2);
ui->tableCondFormats->openPersistentEditor(item, ColumnFilter);
ui->tableCondFormats->setCurrentIndex(ui->tableCondFormats->currentIndex().sibling(selectedRow-1,
ui->tableCondFormats->setCurrentIndex(ui->tableCondFormats->currentIndex().sibling(newRow,
ui->tableCondFormats->currentIndex().column()));
}

void CondFormatManager::downItem()
void CondFormatManager::upItem()
{
if (!ui->tableCondFormats->currentIndex().isValid()) return;

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

QTreeWidgetItem* item;
item = ui->tableCondFormats->takeTopLevelItem(selectedRow);
ui->tableCondFormats->insertTopLevelItem(selectedRow+1, item);
ui->tableCondFormats->openPersistentEditor(item, ColumnFilter);
ui->tableCondFormats->setCurrentIndex(ui->tableCondFormats->currentIndex().sibling(selectedRow+1,
ui->tableCondFormats->currentIndex().column()));
void CondFormatManager::downItem()
{
moveItem(+1);
}

std::vector<CondFormat> CondFormatManager::getCondFormats()
{
std::vector<CondFormat> result;
QFont font = Settings::getValue("databrowser", "font").toString();

for (int i = 0; i < ui->tableCondFormats->topLevelItemCount(); ++i)
{
QTreeWidgetItem* item = ui->tableCondFormats->topLevelItem(i);

QFontComboBox* fontCombo = qobject_cast<QFontComboBox*>(ui->tableCondFormats->itemWidget(item, ColumnFont));
QSpinBox* sizeBox = qobject_cast<QSpinBox*>(ui->tableCondFormats->itemWidget(item, ColumnSize));
QFont font = fontCombo->currentFont();
font.setPointSize(sizeBox->value());
font.setBold(item->checkState(ColumnBold) == Qt::Checked);
font.setItalic(item->checkState(ColumnItalic) == Qt::Checked);
font.setUnderline(item->checkState(ColumnUnderline) == Qt::Checked);
Expand All @@ -140,7 +166,7 @@ void CondFormatManager::itemClicked(QTreeWidgetItem* item, int column)
}
break;
}
case ColumnFilter:
default:
// Nothing to do
break;
}
Expand Down
13 changes: 8 additions & 5 deletions src/CondFormatManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@ class CondFormatManager : public QDialog
private:
enum Columns {
ColumnForeground = 0,
ColumnBackground = 1,
ColumnBold = 2,
ColumnItalic = 3,
ColumnUnderline = 4,
ColumnFilter = 5
ColumnBackground,
ColumnFont,
ColumnSize,
ColumnBold,
ColumnItalic,
ColumnUnderline,
ColumnFilter
};
Ui::CondFormatManager *ui;
std::vector<CondFormat> m_condFormats;
Expand All @@ -41,6 +43,7 @@ private slots:
void addNewItem();
void addItem(const CondFormat& aCondFormat);
void removeItem();
void moveItem(int offset);
void upItem();
void downItem();
void on_buttonBox_clicked(QAbstractButton* button);
Expand Down
20 changes: 18 additions & 2 deletions src/CondFormatManager.ui
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<rect>
<x>0</x>
<y>0</y>
<width>600</width>
<width>640</width>
<height>463</height>
</rect>
</property>
Expand All @@ -17,7 +17,7 @@
<item>
<widget class="QLabel" name="labelTitle">
<property name="text">
<string>This dialog allows creating and editing conditional formats, where the cell text and background will be colored based on one or more conditions. Conditional formats can be moved up and down, where those at higher rows take precedence over those at lower.</string>
<string>This dialog allows creating and editing conditional formats. Each cell style will be selected by the first accomplished condition for that cell data. Conditional formats can be moved up and down, where those at higher rows take precedence over those at lower. An empty condition applies to all values.</string>
</property>
<property name="wordWrap">
<bool>true</bool>
Expand Down Expand Up @@ -155,14 +155,30 @@
</attribute>
<column>
<property name="text">
<string>Foreground</string>
</property>
<property name="toolTip">
<string>Text color</string>
</property>
</column>
<column>
<property name="text">
<string>Background</string>
</property>
<property name="toolTip">
<string>Background color</string>
</property>
</column>
<column>
<property name="text">
<string>Font</string>
</property>
</column>
<column>
<property name="text">
<string>Size</string>
</property>
</column>
<column>
<property name="text">
<string/>
Expand Down

0 comments on commit 1ec502a

Please sign in to comment.