mirror of
https://github.com/sqlitebrowser/sqlitebrowser.git
synced 2026-01-20 19:11:39 -06:00
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 #414.
This commit is contained in:
@@ -6,13 +6,15 @@
|
||||
#include <QDebug>
|
||||
#include <QMessageBox>
|
||||
#include <QApplication>
|
||||
#include <QTextCodec>
|
||||
|
||||
SqliteTableModel::SqliteTableModel(QObject* parent, DBBrowserDB* db, size_t chunkSize)
|
||||
SqliteTableModel::SqliteTableModel(QObject* parent, DBBrowserDB* db, size_t chunkSize, const QString& encoding)
|
||||
: QAbstractTableModel(parent)
|
||||
, m_db(db)
|
||||
, m_rowCount(0)
|
||||
, m_chunkSize(chunkSize)
|
||||
, m_valid(false)
|
||||
, m_encoding(encoding)
|
||||
{
|
||||
reset();
|
||||
}
|
||||
@@ -251,7 +253,7 @@ QVariant SqliteTableModel::data(const QModelIndex &index, int role) const
|
||||
else if(role == Qt::DisplayRole && m_data.at(index.row()).at(index.column()).isNull())
|
||||
return PreferencesDialog::getSettingsValue("databrowser", "null_text").toString();
|
||||
else
|
||||
return m_data.at(index.row()).at(index.column());
|
||||
return decode(m_data.at(index.row()).at(index.column()));
|
||||
} else if(role == Qt::FontRole) {
|
||||
QFont font;
|
||||
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,
|
||||
{
|
||||
if(index.isValid() && role == Qt::EditRole)
|
||||
{
|
||||
QByteArray newValue = value.toByteArray();
|
||||
QByteArray newValue = encode(value.toByteArray());
|
||||
QByteArray oldValue = m_data.at(index.row()).at(index.column());
|
||||
|
||||
// Don't do anything if the data hasn't changed
|
||||
@@ -570,7 +572,7 @@ void SqliteTableModel::updateFilter(int column, const QString& value)
|
||||
if(val == "''")
|
||||
m_mWhere.remove(column);
|
||||
else
|
||||
m_mWhere.insert(column, QString("%1 %2").arg(op).arg(val));
|
||||
m_mWhere.insert(column, QString("%1 %2").arg(op).arg(QString(encode(val.toUtf8()))));
|
||||
|
||||
// Build the new query
|
||||
buildQuery();
|
||||
@@ -587,3 +589,19 @@ bool SqliteTableModel::isBinary(const QModelIndex& index) const
|
||||
{
|
||||
return m_vDataTypes.at(index.column()) == SQLITE_BLOB;
|
||||
}
|
||||
|
||||
QByteArray SqliteTableModel::encode(const QByteArray& str) const
|
||||
{
|
||||
if(m_encoding.isEmpty())
|
||||
return str;
|
||||
else
|
||||
return QTextCodec::codecForName(m_encoding.toUtf8())->fromUnicode(str);
|
||||
}
|
||||
|
||||
QByteArray SqliteTableModel::decode(const QByteArray& str) const
|
||||
{
|
||||
if(m_encoding.isEmpty())
|
||||
return str;
|
||||
else
|
||||
return QTextCodec::codecForName(m_encoding.toUtf8())->toUnicode(str).toUtf8();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user