@@ -31,11 +31,17 @@ EditDialog::EditDialog(QWidget* parent)
3131 hexLayout->addWidget (hexEdit);
3232 hexEdit->setOverwriteMode (false );
3333
34+ QHBoxLayout* jsonLayout = new QHBoxLayout (ui->editorJSON );
35+ jsonEdit = new JsonTextEdit (this );
36+ jsonLayout->addWidget (jsonEdit);
37+
3438 QShortcut* ins = new QShortcut (QKeySequence (Qt::Key_Insert), this );
3539 connect (ins, SIGNAL (activated ()), this , SLOT (toggleOverwriteMode ()));
3640
3741 connect (ui->editorText , SIGNAL (textChanged ()), this , SLOT (updateApplyButton ()));
3842 connect (hexEdit, SIGNAL (dataChanged ()), this , SLOT (updateApplyButton ()));
43+ connect (jsonEdit, SIGNAL (textChanged ()), this , SLOT (updateApplyButton ()));
44+ connect (jsonEdit, SIGNAL (textChanged ()), this , SLOT (editTextChanged ()));
3945
4046 reloadSettings ();
4147}
@@ -89,17 +95,29 @@ void EditDialog::loadData(const QByteArray& data)
8995 // Data type specific handling
9096 switch (dataType) {
9197 case Null:
98+ // Set enabled any of the text widgets
99+ ui->editorText ->setEnabled (true );
100+ jsonEdit->setEnabled (true );
92101 switch (editMode) {
93102 case TextEditor:
94103 // The text widget buffer is now the main data source
95104 dataSource = TextBuffer;
96105
97106 // Empty the text editor contents, then enable text editing
98107 ui->editorText ->clear ();
99- ui->editorText ->setEnabled (true );
100108
101109 break ;
102110
111+ case JsonEditor:
112+ // The JSON widget buffer is now the main data source
113+ dataSource = JsonBuffer;
114+
115+ // Empty the text editor contents, then enable text editing
116+ jsonEdit->clear ();
117+
118+ break ;
119+
120+
103121 case HexEditor:
104122 // The hex widget buffer is now the main data source
105123 dataSource = HexBuffer;
@@ -124,6 +142,10 @@ void EditDialog::loadData(const QByteArray& data)
124142 break ;
125143
126144 case Text:
145+ // Set enabled any of the text widgets
146+ ui->editorText ->setEnabled (true );
147+ jsonEdit->setEnabled (true );
148+
127149 switch (editMode) {
128150 case TextEditor:
129151 // The text widget buffer is now the main data source
@@ -133,14 +155,24 @@ void EditDialog::loadData(const QByteArray& data)
133155 textData = QString::fromUtf8 (data.constData (), data.size ());
134156 ui->editorText ->setPlainText (textData);
135157
136- // Enable text editing
137- ui->editorText ->setEnabled (true );
138-
139158 // Select all of the text by default
140159 ui->editorText ->selectAll ();
141160
142161 break ;
143162
163+ case JsonEditor:
164+ // The JSON widget buffer is now the main data source
165+ dataSource = JsonBuffer;
166+
167+ // Load the text into the text editor
168+ textData = QString::fromUtf8 (data.constData (), data.size ());
169+ jsonEdit->setText (textData);
170+
171+ // Select all of the text by default
172+ jsonEdit->selectAll ();
173+
174+ break ;
175+
144176 case HexEditor:
145177 // The hex widget buffer is now the main data source
146178 dataSource = HexBuffer;
@@ -191,6 +223,12 @@ void EditDialog::loadData(const QByteArray& data)
191223 ui->editorText ->setEnabled (false );
192224 break ;
193225
226+ case JsonEditor:
227+ // Disable text editing, and use a warning message as the contents
228+ jsonEdit->setText (tr (" Image data can't be viewed with the JSON editor" ));
229+ jsonEdit->setEnabled (false );
230+ break ;
231+
194232 case ImageViewer:
195233 // Load the image into the image viewing widget
196234 if (img.loadFromData (data)) {
@@ -219,6 +257,12 @@ void EditDialog::loadData(const QByteArray& data)
219257 ui->editorText ->setEnabled (false );
220258 break ;
221259
260+ case JsonEditor:
261+ // Disable text editing, and use a warning message as the contents
262+ jsonEdit->setText (QString (tr (" Binary data can't be viewed with the JSON editor" )));
263+ jsonEdit->setEnabled (false );
264+ break ;
265+
222266 case ImageViewer:
223267 // Clear any image from the image viewing widget
224268 ui->editorImage ->setPixmap (QPixmap (0 ,0 ));
@@ -281,12 +325,20 @@ void EditDialog::exportData()
281325 QFile file (fileName);
282326 if (file.open (QIODevice::WriteOnly))
283327 {
284- if (dataSource == HexBuffer) {
328+ switch (dataSource) {
329+ case HexBuffer:
285330 // Data source is the hex buffer
286331 file.write (hexEdit->data ());
287- } else {
332+ break ;
333+ case TextBuffer:
288334 // Data source is the text buffer
289335 file.write (ui->editorText ->toPlainText ().toUtf8 ());
336+ break ;
337+ case JsonBuffer:
338+ // Data source is the JSON buffer
339+ file.write (jsonEdit->text ().toUtf8 ());
340+ break ;
341+
290342 }
291343 file.close ();
292344 }
@@ -298,16 +350,19 @@ void EditDialog::setNull()
298350 ui->editorText ->clear ();
299351 ui->editorImage ->clear ();
300352 hexEdit->setData (QByteArray ());
353+ jsonEdit->clear ();
301354 dataType = Null;
302355
303356 // Check if in text editor mode
304357 int editMode = ui->editorStack ->currentIndex ();
305- if (editMode == TextEditor) {
358+ if (editMode == TextEditor || editMode == JsonEditor ) {
306359 // Setting NULL in the text editor switches the data source to it
307360 dataSource = TextBuffer;
308361
309362 // Ensure the text editor is enabled
310363 ui->editorText ->setEnabled (true );
364+ // Ensure the JSON editor is enabled
365+ jsonEdit->setEnabled (true );
311366
312367 // The text editor doesn't know the difference between an empty string
313368 // and a NULL, so we need to record NULL outside of that
@@ -331,7 +386,8 @@ void EditDialog::accept()
331386 if (!currentIndex.isValid ())
332387 return ;
333388
334- if (dataSource == TextBuffer) {
389+ switch (dataSource) {
390+ case TextBuffer:
335391 // Check if a NULL is set in the text editor
336392 if (textNullSet) {
337393 emit recordTextUpdated (currentIndex, hexEdit->data (), true );
@@ -343,25 +399,50 @@ void EditDialog::accept()
343399 // The data is different, so commit it back to the database
344400 emit recordTextUpdated (currentIndex, newData.toUtf8 (), false );
345401 }
346- } else {
402+ break ;
403+ case JsonBuffer:
404+ // Check if a NULL is set in the text editor
405+ if (textNullSet) {
406+ emit recordTextUpdated (currentIndex, hexEdit->data (), true );
407+ } else {
408+ // It's not NULL, so proceed with normal text string checking
409+ QString oldData = currentIndex.data (Qt::EditRole).toString ();
410+ QString newData = jsonEdit->text ();
411+ if (oldData != newData)
412+ // The data is different, so commit it back to the database
413+ emit recordTextUpdated (currentIndex, newData.toUtf8 (), false );
414+ }
415+ break ;
416+
417+ case HexBuffer:
347418 // The data source is the hex widget buffer, thus binary data
348419 QByteArray oldData = currentIndex.data (Qt::EditRole).toByteArray ();
349420 QByteArray newData = hexEdit->data ();
350421 if (newData != oldData)
351422 emit recordTextUpdated (currentIndex, newData, true );
423+ break ;
352424 }
353425}
354426
355427// Called when the user manually changes the "Mode" drop down combobox
356428void EditDialog::editModeChanged (int newMode)
357429{
358430 // * If the dataSource is the text buffer, the data is always text *
359- if (dataSource == TextBuffer) {
431+ switch (dataSource) {
432+ case TextBuffer:
360433 switch (newMode) {
361434 case TextEditor: // Switching to the text editor
362435 // Nothing to do, as the text is already in the text buffer
363436 break ;
364437
438+ case JsonEditor: // Switching to the JSON editor
439+ // Convert the text widget buffer for the JSON widget
440+ jsonEdit->setText (ui->editorText ->toPlainText ().toUtf8 ());
441+
442+ // The JSON widget buffer is now the main data source
443+ dataSource = JsonBuffer;
444+ break ;
445+
365446 case HexEditor: // Switching to the hex editor
366447 // Convert the text widget buffer for the hex widget
367448 hexEdit->setData (ui->editorText ->toPlainText ().toUtf8 ());
@@ -379,24 +460,56 @@ void EditDialog::editModeChanged(int newMode)
379460 // Switch to the selected editor
380461 ui->editorStack ->setCurrentIndex (newMode);
381462 return ;
382- }
463+ break ;
464+ case HexBuffer:
383465
384- // * If the dataSource is the hex buffer, the contents could be anything
385- // so we just pass it to our loadData() function to handle *
386- if (dataSource == HexBuffer) {
466+ // * If the dataSource is the hex buffer, the contents could be anything
467+ // so we just pass it to our loadData() function to handle *
387468 // Switch to the selected editor first, as loadData() relies on it
388469 // being current
389470 ui->editorStack ->setCurrentIndex (newMode);
390471
391472 // Load the data into the appropriate widget, as done by loadData()
392473 loadData (hexEdit->data ());
393- }
474+ break ;
475+ case JsonBuffer:
476+ switch (newMode) {
477+ case TextEditor: // Switching to the text editor
478+ // Convert the text widget buffer for the JSON widget
479+ ui->editorText ->setText (jsonEdit->text ());
480+
481+ // The Text widget buffer is now the main data source
482+ dataSource = TextBuffer;
483+ break ;
484+
485+ case JsonEditor: // Switching to the JSON editor
486+ // Nothing to do, as the text is already in the JSON buffer
487+ break ;
488+
489+
490+ case HexEditor: // Switching to the hex editor
491+ // Convert the text widget buffer for the hex widget
492+ hexEdit->setData (jsonEdit->text ().toUtf8 ());
493+
494+ // The hex widget buffer is now the main data source
495+ dataSource = HexBuffer;
496+ break ;
497+
498+ case ImageViewer:
499+ // Clear any image from the image viewing widget
500+ ui->editorImage ->setPixmap (QPixmap (0 ,0 ));
501+ break ;
502+ }
503+
504+ // Switch to the selected editor
505+ ui->editorStack ->setCurrentIndex (newMode);
506+ }
394507}
395508
396509// Called for every keystroke in the text editor (only)
397510void EditDialog::editTextChanged ()
398511{
399- if (dataSource == TextBuffer) {
512+ if (dataSource == TextBuffer || dataSource == JsonBuffer ) {
400513 // Data has been changed in the text editor, so it can't be a NULL
401514 // any more
402515 textNullSet = false ;
@@ -442,6 +555,7 @@ void EditDialog::toggleOverwriteMode()
442555
443556 hexEdit->setOverwriteMode (currentMode);
444557 ui->editorText ->setOverwriteMode (currentMode);
558+ jsonEdit->setOverwriteMode (currentMode);
445559}
446560
447561void EditDialog::setFocus ()
@@ -467,6 +581,7 @@ void EditDialog::setReadOnly(bool ro)
467581 ui->buttonImport ->setEnabled (!ro);
468582 ui->editorText ->setReadOnly (ro);
469583 ui->editorBinary ->setEnabled (!ro); // We disable the entire hex editor here instead of setting it to read only because it doesn't have a setReadOnly() method
584+ jsonEdit->setReadOnly (ro);
470585}
471586
472587// Update the information labels in the bottom left corner of the dialog
@@ -544,9 +659,16 @@ QString EditDialog::humanReadableSize(double byteCount) const
544659
545660void EditDialog::reloadSettings ()
546661{
547- // Set the font for the text and hex editors
548- QFont editorFont (Settings::getValue (" databrowser" , " font" ).toString ());
549- editorFont.setPointSize (Settings::getValue (" databrowser" , " fontsize" ).toInt ());
550- ui->editorText ->setFont (editorFont);
551- hexEdit->setFont (editorFont);
662+ // Set the databrowser font for the text editor but the (SQL) editor
663+ // font for hex editor, since it needs a Monospace font and the
664+ // databrowser font would be usually of variable width.
665+ QFont textFont (Settings::getValue (" databrowser" , " font" ).toString ());
666+ textFont.setPointSize (Settings::getValue (" databrowser" , " fontsize" ).toInt ());
667+ ui->editorText ->setFont (textFont);
668+
669+ QFont hexFont (Settings::getValue (" editor" , " font" ).toString ());
670+ hexFont.setPointSize (Settings::getValue (" databrowser" , " fontsize" ).toInt ());
671+ hexEdit->setFont (hexFont);
672+
673+ jsonEdit->reloadSettings ();
552674}
0 commit comments