Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More options in conditional formatting #2013

Merged
merged 6 commits into from
Sep 29, 2019
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
25 changes: 23 additions & 2 deletions src/CondFormat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,17 @@
#include "Settings.h"
#include "Data.h"

CondFormat::CondFormat(const QString& filter, const QColor& foreground, const QColor& background, const QString& encoding)
CondFormat::CondFormat(const QString& filter,
const QColor& foreground,
const QColor& background,
const QFont& font,
const Alignment alignment,
const QString& encoding)
: m_filter(filter),
m_bgColor(background),
m_fgColor(foreground)
m_fgColor(foreground),
m_font(font),
m_align(alignment)
{
if (!filter.isEmpty())
m_sqlCondition = filterToSqlCondition(filter, encoding);
Expand Down Expand Up @@ -115,3 +122,17 @@ QString CondFormat::filterToSqlCondition(const QString& value, const QString& en
return whereClause;
}
}

Qt::AlignmentFlag CondFormat::alignmentFlag() const
{
switch (m_align) {
case AlignLeft:
return Qt::AlignLeft;
case AlignCenter:
return Qt::AlignCenter;
case AlignRight:
return Qt::AlignRight;
case AlignJustify:
return Qt::AlignJustify;
}
}
40 changes: 39 additions & 1 deletion src/CondFormat.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,32 @@

#include <QString>
#include <QColor>
#include <QFont>

// Conditional formatting for given format to table cells based on a specified condition.
class CondFormat
{
public:

enum Alignment {
AlignLeft = 0,
AlignRight,
AlignCenter,
AlignJustify
};

// List of alignment texts. Order must be as Alignment definition above.
static QStringList alignmentTexts() {
return {QObject::tr("Left"), QObject::tr("Right"), QObject::tr("Center"), QObject::tr("Justify")};
};

CondFormat() {}
explicit CondFormat(const QString& filter, const QColor& foreground, const QColor& background, const QString& encoding = QString());
explicit CondFormat(const QString& filter,
const QColor& foreground,
const QColor& background,
const QFont& font,
const Alignment alignment = AlignLeft,
const QString& encoding = QString());

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

Expand All @@ -18,13 +37,32 @@ class CondFormat
QString m_filter;
QColor m_bgColor;
QColor m_fgColor;
QFont m_font;
Alignment m_align;

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;
};

#endif // CONDFORMAT_H
106 changes: 86 additions & 20 deletions src/CondFormatManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
#include <QUrl>
#include <QPushButton>
#include <QMessageBox>
#include <QFontComboBox>
#include <QSpinBox>
#include <QComboBox>

CondFormatManager::CondFormatManager(const std::vector<CondFormat>& condFormats, const QString& encoding, QWidget *parent) :
QDialog(parent),
Expand All @@ -20,6 +23,10 @@ CondFormatManager::CondFormatManager(const std::vector<CondFormat>& condFormats,
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);

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

connect(ui->buttonAdd, SIGNAL(clicked(bool)), this, SLOT(addNewItem()));
Expand All @@ -38,22 +45,51 @@ CondFormatManager::~CondFormatManager()

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

CondFormat newCondFormat("", QColor(Settings::getValue("databrowser", "reg_fg_colour").toString()),
m_condFormatPalette.nextSerialColor(Palette::appHasDarkTheme()),
font,
CondFormat::AlignLeft,
m_encoding);
addItem(newCondFormat);

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

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);

QComboBox* alignCombo = new QComboBox(ui->tableCondFormats);
alignCombo->addItems(CondFormat::alignmentTexts());
alignCombo->setCurrentIndex(aCondFormat.alignment());
ui->tableCondFormats->setItemWidget(newItem, ColumnAlignment, alignCombo);

newItem->setText(ColumnFilter, aCondFormat.filter());
ui->tableCondFormats->insertTopLevelItem(i, newItem);
ui->tableCondFormats->openPersistentEditor(newItem, ColumnFilter);
}
Expand All @@ -64,48 +100,78 @@ 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));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The font combobox is too wide on my system and overlaps the other controls. Either we need to make sure it's not as wide or somehow fix the automatic column resizing. Or maybe this problem even depends on the Qt version?
Screenshot_20190928_153111

Copy link
Member Author

@mgrojo mgrojo Sep 28, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's weird! This is how it looks in my version:
imagen

Setting a maximum width could make the trick, but it looks like a Qt bug. I have another problem when a row is moved to the top:

imagen

A simple resize of the window fixes the layout.

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());

QComboBox* alignCombo = qobject_cast<QComboBox*>(ui->tableCondFormats->itemWidget(item, ColumnAlignment));
QComboBox* alignCombo2 = new QComboBox(ui->tableCondFormats);
alignCombo2->addItems(CondFormat::alignmentTexts());
alignCombo2->setCurrentIndex(alignCombo->currentIndex());

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->setItemWidget(item, ColumnAlignment, alignCombo2);
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;

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);
QComboBox* alignCombo = qobject_cast<QComboBox*>(ui->tableCondFormats->itemWidget(item, ColumnAlignment));

result.emplace_back(item->text(ColumnFilter),
item->background(ColumnForeground).color(),
item->background(ColumnBackground).color(), m_encoding);
item->background(ColumnBackground).color(),
font,
static_cast<CondFormat::Alignment>(alignCombo->currentIndex()),
m_encoding);
}
return result;
}
Expand All @@ -124,7 +190,7 @@ void CondFormatManager::itemClicked(QTreeWidgetItem* item, int column)
}
break;
}
case ColumnFilter:
default:
// Nothing to do
break;
}
Expand Down
13 changes: 10 additions & 3 deletions src/CondFormatManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,25 @@ class CondFormatManager : public QDialog
private:
enum Columns {
ColumnForeground = 0,
ColumnBackground = 1,
ColumnFilter = 2
ColumnBackground,
ColumnFont,
ColumnSize,
ColumnBold,
ColumnItalic,
ColumnUnderline,
ColumnAlignment,
ColumnFilter
};
Ui::CondFormatManager *ui;
std::vector<CondFormat> m_condFormats;
Palette m_condFormatPalette;
QString m_encoding;

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
Loading