Automatic switching of the editor mode according to data type #1537

A new checkable button and associated setting is added for automatically
switching the editor mode in the Edit Database Cell dock in accordance to
the loaded data. The switch is done after loading data from cell, after
importing new data to the cell and when checking the button.

If the button is unchecked the behaviour is as formerly.

Since XML is not currently detected, the mode only changes to XML when SVG
is supplied.

Default value is true, since the new behaviour is considered more useful
for the general case.
This commit is contained in:
mgr
2018-09-21 20:48:16 +02:00
parent 18f0ef60ba
commit dd3417e1c4
4 changed files with 84 additions and 6 deletions

View File

@@ -51,12 +51,15 @@ EditDialog::EditDialog(QWidget* parent)
mustIndentAndCompact = Settings::getValue("databrowser", "indent_compact").toBool();
ui->buttonIndent->setChecked(mustIndentAndCompact);
ui->buttonAutoSwitchMode->setChecked(Settings::getValue("databrowser", "auto_switch_mode").toBool());
reloadSettings();
}
EditDialog::~EditDialog()
{
Settings::setValue("databrowser", "indent_compact", mustIndentAndCompact);
Settings::setValue("databrowser", "auto_switch_mode", ui->buttonAutoSwitchMode->isChecked());
delete ui;
}
@@ -66,7 +69,7 @@ void EditDialog::setCurrentIndex(const QModelIndex& idx)
QByteArray data = idx.data(Qt::EditRole).toByteArray();
loadData(data);
updateCellInfo(data);
updateCellInfoAndMode(data);
ui->buttonApply->setDisabled(true);
}
@@ -340,7 +343,8 @@ void EditDialog::importData()
file.close();
// Update the cell data info in the bottom left of the Edit Cell
updateCellInfo(d);
// and update mode (if required) to the just imported data type.
updateCellInfoAndMode(d);
}
}
}
@@ -430,7 +434,7 @@ void EditDialog::setNull()
sciEdit->setEnabled(true);
// Update the cell data info in the bottom left of the Edit Cell
updateCellInfo(hexEdit->data());
updateCellInfoAndMode(hexEdit->data());
ui->editorText->setFocus();
}
@@ -719,7 +723,7 @@ void EditDialog::editTextChanged()
ui->labelType->setText(tr("Type of data currently in cell: Text / Numeric"));
}
// Update the cell info in the bottom left manually. This is because
// updateCellInfo() only works with QByteArray's (for now)
// updateCellInfoAndMode() only works with QByteArray's (for now)
int dataLength;
switch (dataSource) {
case TextBuffer:
@@ -850,11 +854,39 @@ void EditDialog::setReadOnly(bool ro)
}
}
void EditDialog::switchEditorMode(bool autoSwitchForType)
{
if (autoSwitchForType) {
// Switch automatically the editing mode according to the detected data.
switch (dataType) {
case Image:
ui->comboMode->setCurrentIndex(ImageViewer);
break;
case Binary:
ui->comboMode->setCurrentIndex(HexEditor);
break;
case Null:
case Text:
ui->comboMode->setCurrentIndex(TextEditor);
break;
case JSON:
ui->comboMode->setCurrentIndex(JsonEditor);
break;
case SVG:
ui->comboMode->setCurrentIndex(XmlEditor);
break;
}
}
}
// Update the information labels in the bottom left corner of the dialog
void EditDialog::updateCellInfo(const QByteArray& data)
// and switches the editor mode, if required, according to the detected data type.
void EditDialog::updateCellInfoAndMode(const QByteArray& data)
{
QByteArray cellData = data;
switchEditorMode(ui->buttonAutoSwitchMode->isChecked());
// Image data needs special treatment
if (dataType == Image || dataType == SVG) {
QBuffer imageBuffer(&cellData);

View File

@@ -40,7 +40,8 @@ private slots:
void toggleOverwriteMode();
void editModeChanged(int newMode);
void editTextChanged();
void updateCellInfo(const QByteArray& data);
void switchEditorMode(bool autoSwitchForType);
void updateCellInfoAndMode(const QByteArray& data);
void setMustIndentAndCompact(bool enable);
signals:

View File

@@ -67,6 +67,33 @@
</item>
</widget>
</item>
<item>
<widget class="QToolButton" name="buttonAutoSwitchMode">
<property name="toolTip">
<string>Automatically adjust the editor mode to the loaded data type</string>
</property>
<property name="statusTip">
<string>Automatically adjust the editor mode to the loaded data type</string>
</property>
<property name="whatsThis">
<string>This checkable button enables or disables the automatic switching of the editor mode. When a new cell is selected or new data is imported and the automatic switching is enabled, the mode adjusts to the detected data type. You can then change the editor mode manually. If you want to keep this manually switched mode while moving through the cells, switch the button off.</string>
</property>
<property name="text">
<string>Auto-switch</string>
</property>
<property name="icon">
<iconset resource="icons/icons.qrc">
<normaloff>:/icons/keyword</normaloff>
<normalon>:/icons/cog_go.png</normalon>:/icons/keyword</iconset>
</property>
<property name="checkable">
<bool>true</bool>
</property>
<property name="checked">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
@@ -396,6 +423,22 @@ Errors are indicated with a red squiggle underline.</string>
</hint>
</hints>
</connection>
<connection>
<sender>buttonAutoSwitchMode</sender>
<signal>toggled(bool)</signal>
<receiver>EditDialog</receiver>
<slot>switchEditorMode(bool)</slot>
<hints>
<hint type="sourcelabel">
<x>135</x>
<y>22</y>
</hint>
<hint type="destinationlabel">
<x>308</x>
<y>190</y>
</hint>
</hints>
</connection>
</connections>
<slots>
<slot>importData()</slot>

View File

@@ -178,6 +178,8 @@ QVariant Settings::getDefaultValue(const QString& group, const QString& name)
return 5000;
if(name == "indent_compact")
return false;
if(name == "auto_switch_mode")
return true;
if(name == "null_text")
return "NULL";
if(name == "blob_text")