Use different editors in EditDialog depending on the data type

Only show the old text edit widget when there is actually a text to be
edited. If the data is a image file show the image instead of some
random garbage. If it is some other binary data show the hex editor.
This commit is contained in:
Martin Kleusberg
2013-03-17 02:11:12 +01:00
parent 6c8712d804
commit b88fd9316e
2 changed files with 44 additions and 16 deletions

View File

@@ -12,8 +12,10 @@ EditDialog::EditDialog(QWidget* parent)
{
ui->setupUi(this);
QHBoxLayout* hexLayout = new QHBoxLayout;
hexEdit = new QHexEdit(this);
hexEdit->setVisible(false);
hexLayout->addWidget(hexEdit);
ui->editorBinary->setLayout(hexLayout);
reset();
}
@@ -27,8 +29,9 @@ void EditDialog::reset()
{
curRow = -1;
curCol = -1;
ui->editData->setPlainText("");
ui->editData->setFocus();
ui->editorText->clear();
ui->editorText->setFocus();
ui->editorImage->clear();
hexEdit->setData(QByteArray());
setDataType(kSQLiteMediaType_Void, 0);
}
@@ -47,7 +50,7 @@ void EditDialog::setDataType(int type, int size)
{
case kSQLiteMediaType_String:
ui->labelType->setText(tr("Type of data currently in cell: Text / Numeric"));
ui->labelSize->setText(tr("%n char(s)", "", ui->editData->toPlainText().length()));
ui->labelSize->setText(tr("%n char(s)", "", hexEdit->data().length()));
enableExport(true);
break;
case kSQLiteMediaType_Void:
@@ -65,9 +68,27 @@ void EditDialog::closeEvent(QCloseEvent*)
void EditDialog::loadText(const QByteArray& data, int row, int col)
{
ui->editData->setPlainText(data);
ui->editData->setFocus();
ui->editData->selectAll();
// Check if data is text only
if(QString(data).toAscii() == data) // Any proper way??
{
ui->editorStack->setCurrentIndex(0);
} else {
// It's not. So it might be an image.
QImage img;
if(img.loadFromData(data))
{
// It is.
ui->editorImage->setPixmap(QPixmap::fromImage(img));
ui->editorStack->setCurrentIndex(1);
} else {
// It's not. So it's probably some random binary data.
ui->editorStack->setCurrentIndex(2);
}
}
ui->editorText->setPlainText(data);
ui->editorText->setFocus();
ui->editorText->selectAll();
hexEdit->setData(data);
curRow = row;
curCol = col;
@@ -93,7 +114,7 @@ void EditDialog::importData()
{
QByteArray d = file.readAll();
hexEdit->setData(d);
ui->editData->setPlainText(d);
ui->editorText->setPlainText(d);
file.close();
}
setDataType(type, hexEdit->data().length());
@@ -140,7 +161,8 @@ void EditDialog::exportData()
void EditDialog::clearData()
{
ui->editData->setPlainText("");
ui->editorText->clear();
ui->editorImage->clear();
hexEdit->setData(QByteArray());
setDataType(kSQLiteMediaType_Void, 0);
}
@@ -158,8 +180,8 @@ void EditDialog::accept()
void EditDialog::editTextChanged()
{
if(ui->editData->hasFocus())
hexEdit->setData(ui->editData->toPlainText().toUtf8());
if(ui->editorText->hasFocus())
hexEdit->setData(ui->editorText->toPlainText().toUtf8());
int newtype = kSQLiteMediaType_String;
if(hexEdit->data().length() == 0)

View File

@@ -71,10 +71,17 @@
</layout>
</item>
<item>
<widget class="QTextEdit" name="editData">
<property name="whatsThis">
<string>This area displays information about the data present in this database cell</string>
<widget class="QStackedWidget" name="editorStack">
<property name="currentIndex">
<number>0</number>
</property>
<widget class="QTextEdit" name="editorText">
<property name="whatsThis">
<string>This area displays information about the data present in this database cell</string>
</property>
</widget>
<widget class="QLabel" name="editorImage"/>
<widget class="QWidget" name="editorBinary"/>
</widget>
</item>
<item>
@@ -107,7 +114,6 @@
<tabstop>buttomImport</tabstop>
<tabstop>buttonExport</tabstop>
<tabstop>buttonClear</tabstop>
<tabstop>editData</tabstop>
<tabstop>buttonBox</tabstop>
</tabstops>
<resources/>
@@ -193,7 +199,7 @@
</hints>
</connection>
<connection>
<sender>editData</sender>
<sender>editorText</sender>
<signal>textChanged()</signal>
<receiver>EditDialog</receiver>
<slot>editTextChanged()</slot>