Improvements for text alignment

The requested text alignment is always combined with AlignVCenter, which is
the value used before conditional formatting was implemented and gives
better display result.

When creating a new conditional formatting while applying a format from the
toolbar to a column which does not already have one, the alignment flag
of the format is taken from the current cell. In this way, the default text
alignment for numbers is preserved.

See issue #1976 and PR #2013
This commit is contained in:
mgrojo
2019-09-29 13:54:18 +02:00
parent 63aabb9601
commit 6affb875cf
4 changed files with 25 additions and 4 deletions

View File

@@ -2,6 +2,18 @@
#include "Settings.h"
#include "Data.h"
CondFormat::Alignment CondFormat::fromCombinedAlignment(Qt::Alignment align)
{
if (align.testFlag(Qt::AlignLeft))
return AlignLeft;
if (align.testFlag(Qt::AlignRight))
return AlignRight;
if (align.testFlag(Qt::AlignCenter))
return AlignCenter;
if (align.testFlag(Qt::AlignJustify))
return AlignJustify;
}
CondFormat::CondFormat(const QString& filter,
const QColor& foreground,
const QColor& background,

View File

@@ -22,6 +22,10 @@ public:
return {QObject::tr("Left"), QObject::tr("Right"), QObject::tr("Center"), QObject::tr("Justify")};
};
// Get alignment from combined Qt alignment (note that this will lose any combination of our Alignment enum
// with other values present in the flag (e.g. vertical alignment).
static Alignment fromCombinedAlignment(Qt::Alignment align);
CondFormat() {}
explicit CondFormat(const QString& filter,
const QColor& foreground,

View File

@@ -594,14 +594,17 @@ void TableBrowser::modifyColumnFormat(std::unordered_set<int> columns, std::func
changeFunction(*it);
m_browseTableModel->addCondFormat(column, *it);
} else {
// Create a new conditional format based on defaults and then modify it as requested using the passed function.
// Alignment is get from the current column since the default is different from text and numbers.
QFont font = QFont(Settings::getValue("databrowser", "font").toString());
font.setPointSize(Settings::getValue("databrowser", "fontsize").toInt());
Qt::Alignment align = Qt::Alignment(m_browseTableModel->data(currentIndex().sibling(currentIndex().row(), column),
Qt::TextAlignmentRole).toInt());
CondFormat newCondFormat(QString(""), QColor(Settings::getValue("databrowser", "reg_fg_colour").toString()),
QColor(Settings::getValue("databrowser", "reg_bg_colour").toString()),
font,
CondFormat::AlignLeft,
CondFormat::fromCombinedAlignment(align),
m_browseTableModel->encoding());
changeFunction(newCondFormat);
m_browseTableModel->addCondFormat(column, newCondFormat);

View File

@@ -302,7 +302,7 @@ QVariant SqliteTableModel::getMatchingCondFormat(int column, const QString& valu
case Qt::FontRole:
return eachCondFormat.font();
case Qt::TextAlignmentRole:
return eachCondFormat.alignmentFlag();
return static_cast<int>(eachCondFormat.alignmentFlag() | Qt::AlignVCenter);
}
}
return QVariant();
@@ -412,6 +412,8 @@ QVariant SqliteTableModel::data(const QModelIndex &index, int role) const
else
return QString();
} else if (role == Qt::TextAlignmentRole) {
// Align horizontally according to conditional format or default (left for text and right for numbers)
// Align vertically to the center, which displays better.
QString value = cached_row->at(column);
lock.unlock();
QVariant condFormat = getMatchingCondFormat(index.column(), value, role);
@@ -419,7 +421,7 @@ QVariant SqliteTableModel::data(const QModelIndex &index, int role) const
return condFormat;
bool isNumber;
value.toDouble(&isNumber);
return isNumber ? Qt::AlignRight : Qt::AlignLeft;
return static_cast<int>((isNumber ? Qt::AlignRight : Qt::AlignLeft) | Qt::AlignVCenter);
}