Support text in different encoding in Browse Data tab · skyformat99/sqlitebrowser@eaf6ff1 · GitHub
Skip to content

Commit eaf6ff1

Browse files
committed
Support text in different encoding in Browse Data tab
When browsing and editing a table in the Browse Data tab allow the user to change the assumed encoding of the text in the table. We'll then try to convert from this encoding to Unicode for display purposes and to convert from Unicode to this encoding for editing purposes. This way the UI and all tools will keep using Unicode while the database format is still respected. I hope the amount conversions generated this way won't affect the performance too much. See issue sqlitebrowser#414.
1 parent 1ebecbb commit eaf6ff1

5 files changed

Lines changed: 94 additions & 9 deletions

File tree

‎src/MainWindow.cpp‎

Lines changed: 32 additions & 0 deletions

‎src/MainWindow.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class MainWindow : public QMainWindow
4040
QMap<int, QString> filterValues;
4141
QMap<int, QString> displayFormats;
4242
bool showRowid;
43+
QString encoding;
4344

4445
friend QDataStream& operator<<(QDataStream& stream, const MainWindow::BrowseDataTableSettings& object)
4546
{
@@ -49,6 +50,7 @@ class MainWindow : public QMainWindow
4950
stream << object.filterValues;
5051
stream << object.displayFormats;
5152
stream << object.showRowid;
53+
stream << object.encoding;
5254

5355
return stream;
5456
}
@@ -62,6 +64,7 @@ class MainWindow : public QMainWindow
6264
stream >> object.filterValues;
6365
stream >> object.displayFormats;
6466
stream >> object.showRowid;
67+
stream >> object.encoding;
6568

6669
return stream;
6770
}
@@ -218,6 +221,7 @@ private slots:
218221
void showDataColumnPopupMenu(const QPoint& pos);
219222
void editDataColumnDisplayFormat();
220223
void showRowidColumn(bool show);
224+
void browseDataSetTableEncoding();
221225
};
222226

223227
#endif

‎src/MainWindow.ui‎

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -324,8 +324,8 @@
324324
<rect>
325325
<x>0</x>
326326
<y>0</y>
327-
<width>514</width>
328-
<height>579</height>
327+
<width>506</width>
328+
<height>558</height>
329329
</rect>
330330
</property>
331331
<layout class="QFormLayout" name="formLayout">
@@ -1779,6 +1779,14 @@
17791779
<string>Toggle the visibility of the rowid column</string>
17801780
</property>
17811781
</action>
1782+
<action name="actionSetTableEncoding">
1783+
<property name="text">
1784+
<string>Set encoding</string>
1785+
</property>
1786+
<property name="toolTip">
1787+
<string>Change the encoding of the text in the table cells</string>
1788+
</property>
1789+
</action>
17821790
</widget>
17831791
<customwidgets>
17841792
<customwidget>
@@ -2734,6 +2742,22 @@
27342742
</hint>
27352743
</hints>
27362744
</connection>
2745+
<connection>
2746+
<sender>actionSetTableEncoding</sender>
2747+
<signal>triggered()</signal>
2748+
<receiver>MainWindow</receiver>
2749+
<slot>browseDataSetTableEncoding()</slot>
2750+
<hints>
2751+
<hint type="sourcelabel">
2752+
<x>-1</x>
2753+
<y>-1</y>
2754+
</hint>
2755+
<hint type="destinationlabel">
2756+
<x>518</x>
2757+
<y>314</y>
2758+
</hint>
2759+
</hints>
2760+
</connection>
27372761
</connections>
27382762
<slots>
27392763
<slot>fileOpen()</slot>
@@ -2792,5 +2816,6 @@
27922816
<slot>jumpToRow(QString,QString,QByteArray)</slot>
27932817
<slot>editDataColumnDisplayFormat()</slot>
27942818
<slot>showRowidColumn(bool)</slot>
2819+
<slot>browseDataSetTableEncoding()</slot>
27952820
</slots>
27962821
</ui>

‎src/sqlitetablemodel.cpp‎

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@
66
#include <QDebug>
77
#include <QMessageBox>
88
#include <QApplication>
9+
#include <QTextCodec>
910

10-
SqliteTableModel::SqliteTableModel(QObject* parent, DBBrowserDB* db, size_t chunkSize)
11+
SqliteTableModel::SqliteTableModel(QObject* parent, DBBrowserDB* db, size_t chunkSize, const QString& encoding)
1112
: QAbstractTableModel(parent)
1213
, m_db(db)
1314
, m_rowCount(0)
1415
, m_chunkSize(chunkSize)
1516
, m_valid(false)
17+
, m_encoding(encoding)
1618
{
1719
reset();
1820
}
@@ -251,7 +253,7 @@ QVariant SqliteTableModel::data(const QModelIndex &index, int role) const
251253
else if(role == Qt::DisplayRole && m_data.at(index.row()).at(index.column()).isNull())
252254
return PreferencesDialog::getSettingsValue("databrowser", "null_text").toString();
253255
else
254-
return m_data.at(index.row()).at(index.column());
256+
return decode(m_data.at(index.row()).at(index.column()));
255257
} else if(role == Qt::FontRole) {
256258
QFont font;
257259
if(m_data.at(index.row()).at(index.column()).isNull() || isBinary(index))
@@ -293,7 +295,7 @@ bool SqliteTableModel::setData(const QModelIndex& index, const QVariant& value,
293295
{
294296
if(index.isValid() && role == Qt::EditRole)
295297
{
296-
QByteArray newValue = value.toByteArray();
298+
QByteArray newValue = encode(value.toByteArray());
297299
QByteArray oldValue = m_data.at(index.row()).at(index.column());
298300

299301
// Don't do anything if the data hasn't changed
@@ -570,7 +572,7 @@ void SqliteTableModel::updateFilter(int column, const QString& value)
570572
if(val == "''")
571573
m_mWhere.remove(column);
572574
else
573-
m_mWhere.insert(column, QString("%1 %2").arg(op).arg(val));
575+
m_mWhere.insert(column, QString("%1 %2").arg(op).arg(QString(encode(val.toUtf8()))));
574576

575577
// Build the new query
576578
buildQuery();
@@ -587,3 +589,19 @@ bool SqliteTableModel::isBinary(const QModelIndex& index) const
587589
{
588590
return m_vDataTypes.at(index.column()) == SQLITE_BLOB;
589591
}
592+
593+
QByteArray SqliteTableModel::encode(const QByteArray& str) const
594+
{
595+
if(m_encoding.isEmpty())
596+
return str;
597+
else
598+
return QTextCodec::codecForName(m_encoding.toUtf8())->fromUnicode(str);
599+
}
600+
601+
QByteArray SqliteTableModel::decode(const QByteArray& str) const
602+
{
603+
if(m_encoding.isEmpty())
604+
return str;
605+
else
606+
return QTextCodec::codecForName(m_encoding.toUtf8())->toUnicode(str).toUtf8();
607+
}

‎src/sqlitetablemodel.h‎

Lines changed: 9 additions & 3 deletions

0 commit comments

Comments
 (0)