Skip to content

Commit

Permalink
Add option for changing encoding for all tables in Browse Data tab
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
MKleusberg committed Sep 15, 2015
1 parent eaf6ff1 commit c78c466
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 5 deletions.
44 changes: 40 additions & 4 deletions src/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -2320,27 +2330,53 @@ 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);

// 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);
}
4 changes: 3 additions & 1 deletion src/MainWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ class MainWindow : public QMainWindow
QIntValidator* gotoValidator;

DBBrowserDB db;
QString defaultBrowseTableEncoding;

QNetworkAccessManager* m_NetworkManager;

Expand Down Expand Up @@ -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
25 changes: 25 additions & 0 deletions src/MainWindow.ui
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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>
Expand Down Expand Up @@ -2817,5 +2841,6 @@
<slot>editDataColumnDisplayFormat()</slot>
<slot>showRowidColumn(bool)</slot>
<slot>browseDataSetTableEncoding()</slot>
<slot>browseDataSetDefaultTableEncoding()</slot>
</slots>
</ui>

0 comments on commit c78c466

Please sign in to comment.