mirror of
https://github.com/sqlitebrowser/sqlitebrowser.git
synced 2026-01-20 11:00:44 -06:00
Add option for changing encoding for all tables in Browse Data tab
Add a new menu option to the Browse Data tab for changing the encoding of all tables instead of just the one table. Also check if the encoding the user typed in exists before trying to use it to prevent a crash when an invalid encoding is used. See issue #414.
This commit is contained in:
@@ -125,6 +125,8 @@ void MainWindow::init()
|
||||
popupBrowseDataHeaderMenu->addAction(ui->actionShowRowidColumn);
|
||||
popupBrowseDataHeaderMenu->addAction(ui->actionBrowseTableEditDisplayFormat);
|
||||
popupBrowseDataHeaderMenu->addAction(ui->actionSetTableEncoding);
|
||||
popupBrowseDataHeaderMenu->addSeparator();
|
||||
popupBrowseDataHeaderMenu->addAction(ui->actionSetAllTablesEncoding);
|
||||
|
||||
// Add menu item for log dock
|
||||
ui->viewMenu->insertAction(ui->viewDBToolbarAction, ui->dockLog->toggleViewAction());
|
||||
@@ -408,7 +410,7 @@ void MainWindow::populateTable(const QString& tablename)
|
||||
ui->dataTable->filterHeader()->setSortIndicator(0, Qt::AscendingOrder);
|
||||
|
||||
// Encoding
|
||||
m_browseTableModel->setEncoding(QString());
|
||||
m_browseTableModel->setEncoding(defaultBrowseTableEncoding);
|
||||
|
||||
// The filters can be left empty as they are
|
||||
}
|
||||
@@ -481,6 +483,7 @@ void MainWindow::fileClose()
|
||||
|
||||
// Remove all stored table information browse data tab
|
||||
browseTableSettings.clear();
|
||||
defaultBrowseTableEncoding = QString();
|
||||
|
||||
// Manually update the recordset label inside the Browse tab now
|
||||
setRecordsetLabel();
|
||||
@@ -1960,6 +1963,10 @@ bool MainWindow::loadProject(QString filename)
|
||||
// Currently selected table
|
||||
ui->comboBrowseTable->setCurrentIndex(ui->comboBrowseTable->findText(xml.attributes().value("name").toString()));
|
||||
xml.skipCurrentElement();
|
||||
} else if(xml.name() == "default_encoding") {
|
||||
// Default text encoding
|
||||
defaultBrowseTableEncoding = xml.attributes().value("codec").toString();
|
||||
xml.skipCurrentElement();
|
||||
} else if(xml.name() == "browsetable_info") {
|
||||
QString attrData = xml.attributes().value("data").toString();
|
||||
QByteArray temp = QByteArray::fromBase64(attrData.toUtf8());
|
||||
@@ -2068,6 +2075,9 @@ void MainWindow::saveProject()
|
||||
xml.writeStartElement("current_table"); // Currently selected table
|
||||
xml.writeAttribute("name", ui->comboBrowseTable->currentText());
|
||||
xml.writeEndElement();
|
||||
xml.writeStartElement("default_encoding"); // Default encoding for text stored in tables
|
||||
xml.writeAttribute("codec", defaultBrowseTableEncoding);
|
||||
xml.writeEndElement();
|
||||
{ // Table browser information
|
||||
QByteArray temp;
|
||||
QDataStream stream(&temp, QIODevice::WriteOnly);
|
||||
@@ -2320,16 +2330,21 @@ void MainWindow::showRowidColumn(bool show)
|
||||
qobject_cast<FilterTableHeader*>(ui->dataTable->horizontalHeader())->generateFilters(m_browseTableModel->columnCount(), show);
|
||||
}
|
||||
|
||||
void MainWindow::browseDataSetTableEncoding()
|
||||
void MainWindow::browseDataSetTableEncoding(bool forAllTables)
|
||||
{
|
||||
// Get the old encoding
|
||||
QString encoding = m_browseTableModel->encoding();
|
||||
|
||||
// Ask the user for a new encoding
|
||||
bool ok;
|
||||
QString question;
|
||||
if(forAllTables)
|
||||
question = tr("Please choose a new encoding for this table.");
|
||||
else
|
||||
question = tr("Please choose a new encoding for all table.");
|
||||
encoding = QInputDialog::getText(this,
|
||||
tr("Set encoding"),
|
||||
tr("Please choose a new encoding for this table. Leave the field empty for using the database encoding."),
|
||||
tr("%1\nLeave the field empty for using the database encoding.").arg(question),
|
||||
QLineEdit::Normal,
|
||||
encoding,
|
||||
&ok);
|
||||
@@ -2337,10 +2352,31 @@ void MainWindow::browseDataSetTableEncoding()
|
||||
// Only set the new encoding if the user clicked the OK button
|
||||
if(ok)
|
||||
{
|
||||
// Set encoding
|
||||
// Check if encoding is valid
|
||||
if(!QTextCodec::codecForName(encoding.toUtf8()))
|
||||
{
|
||||
QMessageBox::warning(this, qApp->applicationName(), tr("This encoding is either not valid or not supported."));
|
||||
return;
|
||||
}
|
||||
|
||||
// Set encoding for current table
|
||||
m_browseTableModel->setEncoding(encoding);
|
||||
|
||||
// Save encoding for this table
|
||||
browseTableSettings[ui->comboBrowseTable->currentText()].encoding = encoding;
|
||||
|
||||
// Set default encoding if requested to and change all stored table encodings
|
||||
if(forAllTables)
|
||||
{
|
||||
defaultBrowseTableEncoding = encoding;
|
||||
|
||||
for(QMap<QString, BrowseDataTableSettings>::Iterator it=browseTableSettings.begin();it!=browseTableSettings.end();++it)
|
||||
it.value().encoding = encoding;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void MainWindow::browseDataSetDefaultTableEncoding()
|
||||
{
|
||||
browseDataSetTableEncoding(true);
|
||||
}
|
||||
|
||||
@@ -125,6 +125,7 @@ private:
|
||||
QIntValidator* gotoValidator;
|
||||
|
||||
DBBrowserDB db;
|
||||
QString defaultBrowseTableEncoding;
|
||||
|
||||
QNetworkAccessManager* m_NetworkManager;
|
||||
|
||||
@@ -221,7 +222,8 @@ private slots:
|
||||
void showDataColumnPopupMenu(const QPoint& pos);
|
||||
void editDataColumnDisplayFormat();
|
||||
void showRowidColumn(bool show);
|
||||
void browseDataSetTableEncoding();
|
||||
void browseDataSetTableEncoding(bool forAllTables = false);
|
||||
void browseDataSetDefaultTableEncoding();
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@@ -1787,6 +1787,14 @@
|
||||
<string>Change the encoding of the text in the table cells</string>
|
||||
</property>
|
||||
</action>
|
||||
<action name="actionSetAllTablesEncoding">
|
||||
<property name="text">
|
||||
<string>Set encoding for all tables</string>
|
||||
</property>
|
||||
<property name="toolTip">
|
||||
<string>Change the default encoding assumed for all tables in the database</string>
|
||||
</property>
|
||||
</action>
|
||||
</widget>
|
||||
<customwidgets>
|
||||
<customwidget>
|
||||
@@ -2758,6 +2766,22 @@
|
||||
</hint>
|
||||
</hints>
|
||||
</connection>
|
||||
<connection>
|
||||
<sender>actionSetAllTablesEncoding</sender>
|
||||
<signal>triggered()</signal>
|
||||
<receiver>MainWindow</receiver>
|
||||
<slot>browseDataSetDefaultTableEncoding()</slot>
|
||||
<hints>
|
||||
<hint type="sourcelabel">
|
||||
<x>-1</x>
|
||||
<y>-1</y>
|
||||
</hint>
|
||||
<hint type="destinationlabel">
|
||||
<x>518</x>
|
||||
<y>314</y>
|
||||
</hint>
|
||||
</hints>
|
||||
</connection>
|
||||
</connections>
|
||||
<slots>
|
||||
<slot>fileOpen()</slot>
|
||||
@@ -2817,5 +2841,6 @@
|
||||
<slot>editDataColumnDisplayFormat()</slot>
|
||||
<slot>showRowidColumn(bool)</slot>
|
||||
<slot>browseDataSetTableEncoding()</slot>
|
||||
<slot>browseDataSetDefaultTableEncoding()</slot>
|
||||
</slots>
|
||||
</ui>
|
||||
|
||||
Reference in New Issue
Block a user