Skip to content

Commit

Permalink
Added text alignment to conditional formats
Browse files Browse the repository at this point in the history
Combo box for selecting the desired text alignment for a conditional
format.

The default alignment in the browser has been adjusted for numbers, which
are now aligned to the right as in spreadsheets.

Project file format updated.

See issues sqlitebrowser#1976 and sqlitebrowser#1815.
  • Loading branch information
mgrojo committed Sep 21, 2019
1 parent 1ec502a commit c27002c
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 8 deletions.
24 changes: 22 additions & 2 deletions src/CondFormat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@
#include "Settings.h"
#include "Data.h"

CondFormat::CondFormat(const QString& filter, const QColor& foreground, const QColor& background, const QFont& font, 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_font(font)
m_font(font),
m_align(alignment)
{
if (!filter.isEmpty())
m_sqlCondition = filterToSqlCondition(filter, encoding);
Expand Down Expand Up @@ -116,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;
}
}
23 changes: 22 additions & 1 deletion src/CondFormat.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,26 @@
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 QFont& font, 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 @@ -20,6 +38,7 @@ class CondFormat
QColor m_bgColor;
QColor m_fgColor;
QFont m_font;
Alignment m_align;

public:
QString sqlCondition() const { return m_sqlCondition; }
Expand All @@ -30,6 +49,8 @@ class CondFormat
bool isItalic() const { return m_font.italic(); }
bool isUnderline() const { return m_font.underline(); }
QFont font() const { return m_font; }
Alignment alignment() const { return m_align; }
Qt::AlignmentFlag alignmentFlag() const;
};

#endif // CONDFORMAT_H
19 changes: 18 additions & 1 deletion src/CondFormatManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <QMessageBox>
#include <QFontComboBox>
#include <QSpinBox>
#include <QComboBox>

CondFormatManager::CondFormatManager(const std::vector<CondFormat>& condFormats, const QString& encoding, QWidget *parent) :
QDialog(parent),
Expand Down Expand Up @@ -47,6 +48,7 @@ void CondFormatManager::addNewItem()
CondFormat newCondFormat("", QColor(Settings::getValue("databrowser", "reg_fg_colour").toString()),
m_condFormatPalette.nextSerialColor(Palette::appHasDarkTheme()),
QFont(Settings::getValue("databrowser", "font").toString()),
CondFormat::AlignLeft,
m_encoding);
addItem(newCondFormat);
}
Expand Down Expand Up @@ -74,6 +76,12 @@ void CondFormatManager::addItem(const CondFormat& aCondFormat)
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 Down Expand Up @@ -107,12 +115,18 @@ void CondFormatManager::moveItem(int offset)
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(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(newRow,
ui->tableCondFormats->currentIndex().column()));
Expand Down Expand Up @@ -143,11 +157,14 @@ std::vector<CondFormat> CondFormatManager::getCondFormats()
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(),
font, m_encoding);
font,
static_cast<CondFormat::Alignment>(alignCombo->currentIndex()),
m_encoding);
}
return result;
}
Expand Down
3 changes: 2 additions & 1 deletion src/CondFormatManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,14 @@ class CondFormatManager : public QDialog
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);
Expand Down
9 changes: 7 additions & 2 deletions src/CondFormatManager.ui
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>640</width>
<height>463</height>
<width>700</width>
<height>400</height>
</rect>
</property>
<property name="windowTitle">
Expand Down Expand Up @@ -215,6 +215,11 @@
<normaloff>:/icons/text_underline.png</normaloff>:/icons/text_underline.png</iconset>
</property>
</column>
<column>
<property name="text">
<string>Alignment</string>
</property>
</column>
<column>
<property name="text">
<string>Condition</string>
Expand Down
9 changes: 8 additions & 1 deletion src/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2252,10 +2252,16 @@ static void loadBrowseDataTableSettings(BrowseDataTableSettings& settings, QXmlS
else
Settings::getValue("databrowser", "font").toString();

CondFormat::Alignment align;
if (xml.attributes().hasAttribute("align"))
align = static_cast<CondFormat::Alignment>(xml.attributes().value("align").toInt());
else
align = CondFormat::AlignLeft;

settings.condFormats[index].emplace_back(xml.attributes().value("condition").toString(),
QColor(xml.attributes().value("foreground").toString()),
QColor(xml.attributes().value("background").toString()),
font, settings.encoding);
font, align, settings.encoding);
xml.skipCurrentElement();
}
}
Expand Down Expand Up @@ -2594,6 +2600,7 @@ static void saveBrowseDataTableSettings(const BrowseDataTableSettings& object, Q
xml.writeAttribute("background", format.backgroundColor().name());
xml.writeAttribute("foreground", format.foregroundColor().name());
xml.writeAttribute("font", format.font().toString());
xml.writeAttribute("align", QString().setNum(format.alignment()));
xml.writeEndElement();
}
xml.writeEndElement();
Expand Down
1 change: 1 addition & 0 deletions src/TableBrowser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,7 @@ void TableBrowser::addCondFormat(int column, const QString& value)
CondFormat newCondFormat(value, QColor(Settings::getValue("databrowser", "reg_fg_colour").toString()),
m_condFormatPalette.nextSerialColor(Palette::appHasDarkTheme()),
QFont(Settings::getValue("databrowser", "font").toString()),
CondFormat::AlignLeft,
m_browseTableModel->encoding());
m_browseTableModel->addCondFormat(column, newCondFormat);
browseTableSettings[currentlyBrowsedTableName()].condFormats[column].push_back(newCondFormat);
Expand Down
12 changes: 12 additions & 0 deletions src/sqlitetablemodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,8 @@ QVariant SqliteTableModel::getMatchingCondFormat(int column, const QString& valu
return eachCondFormat.backgroundColor();
case Qt::FontRole:
return eachCondFormat.font();
case Qt::TextAlignmentRole:
return eachCondFormat.alignmentFlag();
}
}
return QVariant();
Expand Down Expand Up @@ -409,8 +411,18 @@ QVariant SqliteTableModel::data(const QModelIndex &index, int role) const
.arg(QKeySequence(Qt::CTRL).toString(QKeySequence::NativeText));
else
return QString();
} else if (role == Qt::TextAlignmentRole) {
QString value = cached_row->at(column);
lock.unlock();
QVariant condFormat = getMatchingCondFormat(index.column(), value, role);
if (condFormat.isValid())
return condFormat;
bool isNumber;
value.toDouble(&isNumber);
return isNumber ? Qt::AlignRight : Qt::AlignLeft;
}


return QVariant();
}

Expand Down

0 comments on commit c27002c

Please sign in to comment.