Skip to content

Commit b88fd93

Browse files
committed
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.
1 parent 6c8712d commit b88fd93

File tree

2 files changed

+44
-16
lines changed

2 files changed

+44
-16
lines changed

src/EditDialog.cpp

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ EditDialog::EditDialog(QWidget* parent)
1212
{
1313
ui->setupUi(this);
1414

15+
QHBoxLayout* hexLayout = new QHBoxLayout;
1516
hexEdit = new QHexEdit(this);
16-
hexEdit->setVisible(false);
17+
hexLayout->addWidget(hexEdit);
18+
ui->editorBinary->setLayout(hexLayout);
1719

1820
reset();
1921
}
@@ -27,8 +29,9 @@ void EditDialog::reset()
2729
{
2830
curRow = -1;
2931
curCol = -1;
30-
ui->editData->setPlainText("");
31-
ui->editData->setFocus();
32+
ui->editorText->clear();
33+
ui->editorText->setFocus();
34+
ui->editorImage->clear();
3235
hexEdit->setData(QByteArray());
3336
setDataType(kSQLiteMediaType_Void, 0);
3437
}
@@ -47,7 +50,7 @@ void EditDialog::setDataType(int type, int size)
4750
{
4851
case kSQLiteMediaType_String:
4952
ui->labelType->setText(tr("Type of data currently in cell: Text / Numeric"));
50-
ui->labelSize->setText(tr("%n char(s)", "", ui->editData->toPlainText().length()));
53+
ui->labelSize->setText(tr("%n char(s)", "", hexEdit->data().length()));
5154
enableExport(true);
5255
break;
5356
case kSQLiteMediaType_Void:
@@ -65,9 +68,27 @@ void EditDialog::closeEvent(QCloseEvent*)
6568

6669
void EditDialog::loadText(const QByteArray& data, int row, int col)
6770
{
68-
ui->editData->setPlainText(data);
69-
ui->editData->setFocus();
70-
ui->editData->selectAll();
71+
// Check if data is text only
72+
if(QString(data).toAscii() == data) // Any proper way??
73+
{
74+
ui->editorStack->setCurrentIndex(0);
75+
} else {
76+
// It's not. So it might be an image.
77+
QImage img;
78+
if(img.loadFromData(data))
79+
{
80+
// It is.
81+
ui->editorImage->setPixmap(QPixmap::fromImage(img));
82+
ui->editorStack->setCurrentIndex(1);
83+
} else {
84+
// It's not. So it's probably some random binary data.
85+
ui->editorStack->setCurrentIndex(2);
86+
}
87+
}
88+
89+
ui->editorText->setPlainText(data);
90+
ui->editorText->setFocus();
91+
ui->editorText->selectAll();
7192
hexEdit->setData(data);
7293
curRow = row;
7394
curCol = col;
@@ -93,7 +114,7 @@ void EditDialog::importData()
93114
{
94115
QByteArray d = file.readAll();
95116
hexEdit->setData(d);
96-
ui->editData->setPlainText(d);
117+
ui->editorText->setPlainText(d);
97118
file.close();
98119
}
99120
setDataType(type, hexEdit->data().length());
@@ -140,7 +161,8 @@ void EditDialog::exportData()
140161

141162
void EditDialog::clearData()
142163
{
143-
ui->editData->setPlainText("");
164+
ui->editorText->clear();
165+
ui->editorImage->clear();
144166
hexEdit->setData(QByteArray());
145167
setDataType(kSQLiteMediaType_Void, 0);
146168
}
@@ -158,8 +180,8 @@ void EditDialog::accept()
158180

159181
void EditDialog::editTextChanged()
160182
{
161-
if(ui->editData->hasFocus())
162-
hexEdit->setData(ui->editData->toPlainText().toUtf8());
183+
if(ui->editorText->hasFocus())
184+
hexEdit->setData(ui->editorText->toPlainText().toUtf8());
163185

164186
int newtype = kSQLiteMediaType_String;
165187
if(hexEdit->data().length() == 0)

src/EditDialog.ui

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,17 @@
7171
</layout>
7272
</item>
7373
<item>
74-
<widget class="QTextEdit" name="editData">
75-
<property name="whatsThis">
76-
<string>This area displays information about the data present in this database cell</string>
74+
<widget class="QStackedWidget" name="editorStack">
75+
<property name="currentIndex">
76+
<number>0</number>
7777
</property>
78+
<widget class="QTextEdit" name="editorText">
79+
<property name="whatsThis">
80+
<string>This area displays information about the data present in this database cell</string>
81+
</property>
82+
</widget>
83+
<widget class="QLabel" name="editorImage"/>
84+
<widget class="QWidget" name="editorBinary"/>
7885
</widget>
7986
</item>
8087
<item>
@@ -107,7 +114,6 @@
107114
<tabstop>buttomImport</tabstop>
108115
<tabstop>buttonExport</tabstop>
109116
<tabstop>buttonClear</tabstop>
110-
<tabstop>editData</tabstop>
111117
<tabstop>buttonBox</tabstop>
112118
</tabstops>
113119
<resources/>
@@ -193,7 +199,7 @@
193199
</hints>
194200
</connection>
195201
<connection>
196-
<sender>editData</sender>
202+
<sender>editorText</sender>
197203
<signal>textChanged()</signal>
198204
<receiver>EditDialog</receiver>
199205
<slot>editTextChanged()</slot>

0 commit comments

Comments
 (0)