forked from sqlitebrowser/sqlitebrowser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEditDialog.cpp
188 lines (161 loc) · 5.37 KB
/
EditDialog.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#include "EditDialog.h"
#include "ui_EditDialog.h"
#include "sqlitedb.h"
#include "PreferencesDialog.h"
#include "src/qhexedit.h"
#include <QFileDialog>
#include <QKeySequence>
#include <QShortcut>
EditDialog::EditDialog(QWidget* parent)
: QDialog(parent),
ui(new Ui::EditDialog)
{
ui->setupUi(this);
ui->buttonBox->button(QDialogButtonBox::Ok)->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Return));
QHBoxLayout* hexLayout = new QHBoxLayout(ui->editorBinary);
hexEdit = new QHexEdit(this);
hexLayout->addWidget(hexEdit);
hexEdit->setOverwriteMode(false);
connect(hexEdit, SIGNAL(dataChanged()), this, SLOT(hexDataChanged()));
QShortcut* ins = new QShortcut(QKeySequence(Qt::Key_Insert), this);
connect(ins, SIGNAL(activated()), this, SLOT(toggleOverwriteMode()));
reset();
}
EditDialog::~EditDialog()
{
delete ui;
}
void EditDialog::reset()
{
curRow = -1;
curCol = -1;
ui->editorText->clear();
ui->editorText->setFocus();
ui->editorImage->clear();
hexEdit->setData(QByteArray());
oldData = "";
checkDataType();
}
void EditDialog::closeEvent(QCloseEvent*)
{
emit goingAway();
}
void EditDialog::loadText(const QByteArray& data, int row, int col)
{
curRow = row;
curCol = col;
oldData = data;
QString textData = QString::fromUtf8(data.constData(), data.size());
ui->editorText->setPlainText(textData);
ui->editorText->setFocus();
ui->editorText->selectAll();
hexEdit->setData(data);
// Assume it's binary data and only call checkDatyType afterwards. This means the correct input widget is selected here in all cases
// but once the user changed it to text input it will stay there.
ui->editorStack->setCurrentIndex(1);
checkDataType();
}
void EditDialog::importData()
{
// Get list of supported image file formats to include them in the file dialog filter
QString image_formats;
QList<QByteArray> image_formats_list = QImageReader::supportedImageFormats();
for(int i=0;i<image_formats_list.size();++i)
image_formats.append(QString("*.%1 ").arg(QString::fromUtf8(image_formats_list.at(i))));
QString fileName = QFileDialog::getOpenFileName(
this,
tr("Choose a file"),
PreferencesDialog::getSettingsValue("db", "defaultlocation").toString(),
tr("Text files(*.txt);;Image files(%1);;All files(*)").arg(image_formats));
if(QFile::exists(fileName))
{
QFile file(fileName);
if(file.open(QIODevice::ReadOnly))
{
QByteArray d = file.readAll();
hexEdit->setData(d);
ui->editorText->setPlainText(d);
checkDataType();
file.close();
}
}
}
void EditDialog::exportData()
{
QString fileName = QFileDialog::getSaveFileName(
this,
tr("Choose a filename to export data"),
PreferencesDialog::getSettingsValue("db", "defaultlocation").toString(),
tr("Text files(*.txt);;All files(*)"));
if(fileName.size() > 0)
{
QFile file(fileName);
if(file.open(QIODevice::WriteOnly))
{
file.write(hexEdit->data());
file.close();
}
}
}
void EditDialog::clearData()
{
ui->editorText->clear();
ui->editorImage->clear();
hexEdit->setData(QByteArray());
checkDataType();
}
void EditDialog::accept()
{
if(hexEdit->data() != oldData)
emit updateRecordText(curRow, curCol, hexEdit->data());
emit goingAway();
}
void EditDialog::editTextChanged()
{
if(ui->editorText->hasFocus())
{
hexEdit->setData(ui->editorText->toPlainText().toUtf8());
checkDataType();
}
}
void EditDialog::hexDataChanged()
{
// Update the text editor accordingly
ui->editorText->setPlainText(QString::fromUtf8(hexEdit->data().constData(), hexEdit->data().size()));
}
void EditDialog::checkDataType()
{
ui->comboEditor->setVisible(true);
// Check if data is text only
if(QString(hexEdit->data()).toUtf8() == hexEdit->data()) // Any proper way??
{
ui->editorStack->setCurrentIndex(0);
ui->labelBinayWarning->setVisible(false);
ui->labelType->setText(tr("Type of data currently in cell: Text / Numeric"));
ui->labelSize->setText(tr("%n char(s)", "", hexEdit->data().length()));
} else {
// It's not. So it might be an image.
QImage img;
if(img.loadFromData(hexEdit->data()))
{
// It is.
ui->editorImage->setPixmap(QPixmap::fromImage(img));
ui->editorStack->setCurrentIndex(2);
ui->labelType->setText(tr("Type of data currently in cell: Image"));
ui->labelSize->setText(tr("%1x%2 pixel").arg(ui->editorImage->pixmap()->size().width()).arg(ui->editorImage->pixmap()->size().height()));
ui->comboEditor->setVisible(false);
} else {
// It's not. So it's probably some random binary data.
ui->labelBinayWarning->setVisible(true);
ui->labelType->setText(tr("Type of data currently in cell: Binary"));
ui->labelSize->setText(tr("%n byte(s)", "", hexEdit->data().length()));
}
}
}
void EditDialog::toggleOverwriteMode()
{
static bool currentMode = false;
currentMode = !currentMode;
hexEdit->setOverwriteMode(currentMode);
ui->editorText->setOverwriteMode(currentMode);
}