GeezerVison is showing github.com — Added text alignment to conditional formats · sqlitebrowser/sqlitebrowser@c27002c
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: src/CondFormat.cpp +22-2Lines changed: 22 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 2 | 2 | #include "Settings.h" | 3 | 3 | #include "Data.h" | 4 | 4 |
| 5 | | -CondFormat::CondFormat(const QString& filter, const QColor& foreground, const QColor& background, const QFont& font, const QString& encoding) | | 5 | +CondFormat::CondFormat(const QString& filter, | | 6 | + const QColor& foreground, | | 7 | + const QColor& background, | | 8 | + const QFont& font, | | 9 | + const Alignment alignment, | | 10 | + const QString& encoding) | 6 | 11 | : m_filter(filter), | 7 | 12 | m_bgColor(background), | 8 | 13 | m_fgColor(foreground), | 9 | | - m_font(font) | | 14 | + m_font(font), | | 15 | + m_align(alignment) | 10 | 16 | { | 11 | 17 | if (!filter.isEmpty()) | 12 | 18 | m_sqlCondition = filterToSqlCondition(filter, encoding); | @@ -116,3 +122,17 @@ QString CondFormat::filterToSqlCondition(const QString& value, const QString& en | 116 | 122 | return whereClause; | 117 | 123 | } | 118 | 124 | } | | 125 | + | | 126 | +Qt::AlignmentFlag CondFormat::alignmentFlag() const | | 127 | +{ | | 128 | + switch (m_align) { | | 129 | + case AlignLeft: | | 130 | + return Qt::AlignLeft; | | 131 | + case AlignCenter: | | 132 | + return Qt::AlignCenter; | | 133 | + case AlignRight: | | 134 | + return Qt::AlignRight; | | 135 | + case AlignJustify: | | 136 | + return Qt::AlignJustify; | | 137 | + } | | 138 | +} |
Collapse file Copy file name to clipboardExpand all lines: src/CondFormat.h +22-1Lines changed: 22 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 9 | 9 | class CondFormat | 10 | 10 | { | 11 | 11 | public: | | 12 | + | | 13 | + enum Alignment { | | 14 | + AlignLeft = 0, | | 15 | + AlignRight, | | 16 | + AlignCenter, | | 17 | + AlignJustify | | 18 | + }; | | 19 | + | | 20 | + // List of alignment texts. Order must be as Alignment definition above. | | 21 | + static QStringList alignmentTexts() { | | 22 | + return {QObject::tr("Left"), QObject::tr("Right"), QObject::tr("Center"), QObject::tr("Justify")}; | | 23 | + }; | | 24 | + | 12 | 25 | CondFormat() {} | 13 | | - explicit CondFormat(const QString& filter, const QColor& foreground, const QColor& background, const QFont& font, const QString& encoding = QString()); | | 26 | + explicit CondFormat(const QString& filter, | | 27 | + const QColor& foreground, | | 28 | + const QColor& background, | | 29 | + const QFont& font, | | 30 | + const Alignment alignment = AlignLeft, | | 31 | + const QString& encoding = QString()); | 14 | 32 |
| 15 | 33 | static QString filterToSqlCondition(const QString& value, const QString& encoding = QString()); | 16 | 34 |
| @@ -20,6 +38,7 @@ class CondFormat | 20 | 38 | QColor m_bgColor; | 21 | 39 | QColor m_fgColor; | 22 | 40 | QFont m_font; | | 41 | + Alignment m_align; | 23 | 42 |
| 24 | 43 | public: | 25 | 44 | QString sqlCondition() const { return m_sqlCondition; } | @@ -30,6 +49,8 @@ class CondFormat | 30 | 49 | bool isItalic() const { return m_font.italic(); } | 31 | 50 | bool isUnderline() const { return m_font.underline(); } | 32 | 51 | QFont font() const { return m_font; } | | 52 | + Alignment alignment() const { return m_align; } | | 53 | + Qt::AlignmentFlag alignmentFlag() const; | 33 | 54 | }; | 34 | 55 |
| 35 | 56 | #endif // CONDFORMAT_H |
Collapse file Copy file name to clipboardExpand all lines: src/CondFormatManager.cpp +18-1Lines changed: 18 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 10 | 10 | #include <QMessageBox> | 11 | 11 | #include <QFontComboBox> | 12 | 12 | #include <QSpinBox> | | 13 | +#include <QComboBox> | 13 | 14 |
| 14 | 15 | CondFormatManager::CondFormatManager(const std::vector<CondFormat>& condFormats, const QString& encoding, QWidget *parent) : | 15 | 16 | QDialog(parent), | @@ -47,6 +48,7 @@ void CondFormatManager::addNewItem() | 47 | 48 | CondFormat newCondFormat("", QColor(Settings::getValue("databrowser", "reg_fg_colour").toString()), | 48 | 49 | m_condFormatPalette.nextSerialColor(Palette::appHasDarkTheme()), | 49 | 50 | QFont(Settings::getValue("databrowser", "font").toString()), | | 51 | + CondFormat::AlignLeft, | 50 | 52 | m_encoding); | 51 | 53 | addItem(newCondFormat); | 52 | 54 | } | @@ -74,6 +76,12 @@ void CondFormatManager::addItem(const CondFormat& aCondFormat) | 74 | 76 | newItem->setCheckState(ColumnBold, aCondFormat.isBold() ? Qt::Checked : Qt::Unchecked); | 75 | 77 | newItem->setCheckState(ColumnItalic, aCondFormat.isItalic() ? Qt::Checked : Qt::Unchecked); | 76 | 78 | newItem->setCheckState(ColumnUnderline, aCondFormat.isUnderline() ? Qt::Checked : Qt::Unchecked); | | 79 | + | | 80 | + QComboBox* alignCombo = new QComboBox(ui->tableCondFormats); | | 81 | + alignCombo->addItems(CondFormat::alignmentTexts()); | | 82 | + alignCombo->setCurrentIndex(aCondFormat.alignment()); | | 83 | + ui->tableCondFormats->setItemWidget(newItem, ColumnAlignment, alignCombo); | | 84 | + | 77 | 85 | newItem->setText(ColumnFilter, aCondFormat.filter()); | 78 | 86 | ui->tableCondFormats->insertTopLevelItem(i, newItem); | 79 | 87 | ui->tableCondFormats->openPersistentEditor(newItem, ColumnFilter); | @@ -107,12 +115,18 @@ void CondFormatManager::moveItem(int offset) | 107 | 115 | sizeBox2->setValue(sizeBox->value()); | 108 | 116 | sizeBox2->setMinimum(sizeBox->minimum()); | 109 | 117 |
| | 118 | + QComboBox* alignCombo = qobject_cast<QComboBox*>(ui->tableCondFormats->itemWidget(item, ColumnAlignment)); | | 119 | + QComboBox* alignCombo2 = new QComboBox(ui->tableCondFormats); | | 120 | + alignCombo2->addItems(CondFormat::alignmentTexts()); | | 121 | + alignCombo2->setCurrentIndex(alignCombo->currentIndex()); | | 122 | + | 110 | 123 | item = ui->tableCondFormats->takeTopLevelItem(selectedRow); | 111 | 124 | ui->tableCondFormats->insertTopLevelItem(newRow, item); | 112 | 125 |
| 113 | 126 | // Restore widgets and state | 114 | 127 | ui->tableCondFormats->setItemWidget(item, ColumnFont, fontCombo2); | 115 | 128 | ui->tableCondFormats->setItemWidget(item, ColumnSize, sizeBox2); | | 129 | + ui->tableCondFormats->setItemWidget(item, ColumnAlignment, alignCombo2); | 116 | 130 | ui->tableCondFormats->openPersistentEditor(item, ColumnFilter); | 117 | 131 | ui->tableCondFormats->setCurrentIndex(ui->tableCondFormats->currentIndex().sibling(newRow, | 118 | 132 | ui->tableCondFormats->currentIndex().column())); | @@ -143,11 +157,14 @@ std::vector<CondFormat> CondFormatManager::getCondFormats() | 143 | 157 | font.setBold(item->checkState(ColumnBold) == Qt::Checked); | 144 | 158 | font.setItalic(item->checkState(ColumnItalic) == Qt::Checked); | 145 | 159 | font.setUnderline(item->checkState(ColumnUnderline) == Qt::Checked); | | 160 | + QComboBox* alignCombo = qobject_cast<QComboBox*>(ui->tableCondFormats->itemWidget(item, ColumnAlignment)); | 146 | 161 |
| 147 | 162 | result.emplace_back(item->text(ColumnFilter), | 148 | 163 | item->background(ColumnForeground).color(), | 149 | 164 | item->background(ColumnBackground).color(), | 150 | | - font, m_encoding); | | 165 | + font, | | 166 | + static_cast<CondFormat::Alignment>(alignCombo->currentIndex()), | | 167 | + m_encoding); | 151 | 168 | } | 152 | 169 | return result; | 153 | 170 | } | |
Collapse file Copy file name to clipboardExpand all lines: src/CondFormatManager.h +2-1Lines changed: 2 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -32,13 +32,14 @@ class CondFormatManager : public QDialog | 32 | 32 | ColumnBold, | 33 | 33 | ColumnItalic, | 34 | 34 | ColumnUnderline, | | 35 | + ColumnAlignment, | 35 | 36 | ColumnFilter | 36 | 37 | }; | 37 | 38 | Ui::CondFormatManager *ui; | 38 | 39 | std::vector<CondFormat> m_condFormats; | 39 | 40 | Palette m_condFormatPalette; | 40 | 41 | QString m_encoding; | 41 | | - | | 42 | + | 42 | 43 | private slots: | 43 | 44 | void addNewItem(); | 44 | 45 | void addItem(const CondFormat& aCondFormat); | |
Collapse file Copy file name to clipboardExpand all lines: src/CondFormatManager.ui +7-2Lines changed: 7 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 6 | 6 | <rect> | 7 | 7 | <x>0</x> | 8 | 8 | <y>0</y> | 9 | | - <width>640</width> | 10 | | - <height>463</height> | | 9 | + <width>700</width> | | 10 | + <height>400</height> | 11 | 11 | </rect> | 12 | 12 | </property> | 13 | 13 | <property name="windowTitle"> | | 215 | 215 | <normaloff>:/icons/text_underline.png</normaloff>:/icons/text_underline.png</iconset> | 216 | 216 | </property> | 217 | 217 | </column> | | 218 | + <column> | | 219 | + <property name="text"> | | 220 | + <string>Alignment</string> | | 221 | + </property> | | 222 | + </column> | 218 | 223 | <column> | 219 | 224 | <property name="text"> | 220 | 225 | <string>Condition</string> | |
Collapse file Copy file name to clipboardExpand all lines: src/MainWindow.cpp +8-1Lines changed: 8 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -2252,10 +2252,16 @@ static void loadBrowseDataTableSettings(BrowseDataTableSettings& settings, QXmlS | 2252 | 2252 | else | 2253 | 2253 | Settings::getValue("databrowser", "font").toString(); | 2254 | 2254 |
| | 2255 | + CondFormat::Alignment align; | | 2256 | + if (xml.attributes().hasAttribute("align")) | | 2257 | + align = static_cast<CondFormat::Alignment>(xml.attributes().value("align").toInt()); | | 2258 | + else | | 2259 | + align = CondFormat::AlignLeft; | | 2260 | + | 2255 | 2261 | settings.condFormats[index].emplace_back(xml.attributes().value("condition").toString(), | 2256 | 2262 | QColor(xml.attributes().value("foreground").toString()), | 2257 | 2263 | QColor(xml.attributes().value("background").toString()), | 2258 | | - font, settings.encoding); | | 2264 | + font, align, settings.encoding); | 2259 | 2265 | xml.skipCurrentElement(); | 2260 | 2266 | } | 2261 | 2267 | } | @@ -2594,6 +2600,7 @@ static void saveBrowseDataTableSettings(const BrowseDataTableSettings& object, Q | 2594 | 2600 | xml.writeAttribute("background", format.backgroundColor().name()); | 2595 | 2601 | xml.writeAttribute("foreground", format.foregroundColor().name()); | 2596 | 2602 | xml.writeAttribute("font", format.font().toString()); | | 2603 | + xml.writeAttribute("align", QString().setNum(format.alignment())); | 2597 | 2604 | xml.writeEndElement(); | 2598 | 2605 | } | 2599 | 2606 | xml.writeEndElement(); | |
Collapse file Copy file name to clipboardExpand all lines: src/TableBrowser.cpp +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -442,6 +442,7 @@ void TableBrowser::addCondFormat(int column, const QString& value) | 442 | 442 | CondFormat newCondFormat(value, QColor(Settings::getValue("databrowser", "reg_fg_colour").toString()), | 443 | 443 | m_condFormatPalette.nextSerialColor(Palette::appHasDarkTheme()), | 444 | 444 | QFont(Settings::getValue("databrowser", "font").toString()), | | 445 | + CondFormat::AlignLeft, | 445 | 446 | m_browseTableModel->encoding()); | 446 | 447 | m_browseTableModel->addCondFormat(column, newCondFormat); | 447 | 448 | browseTableSettings[currentlyBrowsedTableName()].condFormats[column].push_back(newCondFormat); | |
Collapse file Copy file name to clipboardExpand all lines: src/sqlitetablemodel.cpp +12Lines changed: 12 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -301,6 +301,8 @@ QVariant SqliteTableModel::getMatchingCondFormat(int column, const QString& valu | 301 | 301 | return eachCondFormat.backgroundColor(); | 302 | 302 | case Qt::FontRole: | 303 | 303 | return eachCondFormat.font(); | | 304 | + case Qt::TextAlignmentRole: | | 305 | + return eachCondFormat.alignmentFlag(); | 304 | 306 | } | 305 | 307 | } | 306 | 308 | return QVariant(); | @@ -409,8 +411,18 @@ QVariant SqliteTableModel::data(const QModelIndex &index, int role) const | 409 | 411 | .arg(QKeySequence(Qt::CTRL).toString(QKeySequence::NativeText)); | 410 | 412 | else | 411 | 413 | return QString(); | | 414 | + } else if (role == Qt::TextAlignmentRole) { | | 415 | + QString value = cached_row->at(column); | | 416 | + lock.unlock(); | | 417 | + QVariant condFormat = getMatchingCondFormat(index.column(), value, role); | | 418 | + if (condFormat.isValid()) | | 419 | + return condFormat; | | 420 | + bool isNumber; | | 421 | + value.toDouble(&isNumber); | | 422 | + return isNumber ? Qt::AlignRight : Qt::AlignLeft; | 412 | 423 | } | 413 | 424 |
| | 425 | + | 414 | 426 | return QVariant(); | 415 | 427 | } | 416 | 428 |
| |
|