summaryrefslogtreecommitdiffstats
path: root/src/libgui/hex_editor.cpp
blob: 438996c63c0c9600eb0c5183470632633739c0be (plain)
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
189
190
191
192
/***************************************************************************
 *   Copyright (C) 2005-2007 Nicolas Hadacek <hadacek@kde.org>             *
 *   Copyright (C) 2003 Alain Gibaud <alain.gibaud@free.fr>                *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 ***************************************************************************/
#include "hex_editor.h"

#include <tqgroupbox.h>
#include <tqhgroupbox.h>
#include <tqregexp.h>
#include <tqlayout.h>
#include <tqscrollview.h>
#include <tqstringlist.h>
#include <tqlabel.h>
#include <tqtimer.h>

#include <klocale.h>
#include <ktempfile.h>

#include "devices/base/device_group.h"
#include "devices/gui/device_group_ui.h"
#include "devices/gui/hex_view.h"
#include "toplevel.h"
#include "common/global/pfile.h"
#include "main_global.h"

//-----------------------------------------------------------------------------
HexEditorPart::HexEditorPart(HexEditor *editor)
 : KParts::ReadWritePart(TQT_TQOBJECT(editor), "hex_editor_part")
{
  setXMLFile("hexeditorpartui.rc");

  (void)KStdAction::save(TQT_TQOBJECT(editor), TQT_SLOT(save()), actionCollection());
  (void)KStdAction::saveAs(TQT_TQOBJECT(editor), TQT_SLOT(saveAs()), actionCollection());
  (void)new KToggleAction(i18n("Read Only Mode"), 0, 0, TQT_TQOBJECT(editor), TQT_SLOT(toggleReadOnly()), actionCollection(), "tools_toggle_write_lock");
}

void HexEditorPart::setReadWrite(bool rw)
{
  KParts::ReadWritePart::setReadWrite(rw);
  static_cast<KToggleAction *>(action("tools_toggle_write_lock"))->setChecked(!rw);
}

//-----------------------------------------------------------------------------
HexEditor::HexEditor(const TQString &name, TQWidget *parent)
  : DeviceEditor(name, TQString(), parent, "hex_editor")
{
  init();
}

HexEditor::HexEditor(TQWidget *parent)
  : DeviceEditor(parent, "hex_editor")
{
  init();
}

void HexEditor::init()
{
  _modified = false;
  _memory = 0;
  _originalMemory = 0;
  _top->addStretch(1);
  _part = new HexEditorPart(this);
  setReadOnly(false);
}

void HexEditor::clear()
{
  delete _memory;
  _memory = 0;
  delete _originalMemory;
  _originalMemory = 0;
}

TQWidget *HexEditor::createView(const Device::Data *data, TQWidget *parent)
{
  clear();
  if (data) {
    _originalMemory = data->group().createMemory(*data);
    _memory = data->group().createMemory(*data);
  }
  if ( data==0 ) return new TQWidget(parent);
  Device::HexView *hv = Device::groupui(*data).createHexView(*this, parent);
  connect(hv, TQT_SIGNAL(modified()), TQT_SLOT(slotModified()));
  _dirty = true;
  TQTimer::singleShot(0, this, TQT_SLOT(simpleLoad()));
  return hv;
}

bool HexEditor::simpleLoad()
{
  if ( !_dirty ) return true;
  _dirty = false;
  if (_memory) {
    TQStringList warnings;
    if ( _memory->fromHexBuffer(_hexBuffer, warnings)!=Device::Memory::NoWarning ) {
      _labelWarning->setText(i18n("<b>Warning:</b> hex file seems to be incompatible with the selected device %1:<br>%2")
                             .arg(_memory->device().name()).arg(warnings.join("<br>")));
      _labelWarning->show();
    } else _labelWarning->hide();
    display();
  }
  return true;
}

void HexEditor::setReadOnlyInternal(bool readOnly)
{
  _part->setReadWrite(!readOnly);
  if (_memory) static_cast<Device::HexView *>(_view)->setReadOnly(readOnly);
}

void HexEditor::addGui()
{
  Main::toplevel().guiFactory()->addClient(_part);
}

void HexEditor::removeGui()
{
  Main::toplevel().guiFactory()->removeClient(_part);
}

bool HexEditor::open(const PURL::Url &url)
{
  _url = url;
  PURL::File file(url, Main::compileLog());
  if ( !file.openForRead() ) return false;
  TQStringList errors;
  if ( !_hexBuffer.load(file.stream(), errors) ) {
    MessageBox::detailedSorry(i18n("Error(s) reading hex file."), errors.join("\n"), Log::Show);
    return false;
  }
  _dirty = true;
  return simpleLoad();
}

bool HexEditor::save(const PURL::Url &url)
{
  return save(url, i18n("File URL: \"%1\".").arg(url.pretty()));
}

bool HexEditor::save(const PURL::Url &url, const TQString &fileErrorString)
{
  PURL::File file(url, Main::compileLog());
  if ( !file.openForWrite() ) return false;
  if ( !_memory->save(file.stream(), HexBuffer::IHX32) ) {
    MessageBox::detailedSorry(i18n("Error while writing file \"%1\".").arg(url.pretty()), fileErrorString, Log::Show);
    return false;
  }
  _originalMemory->copyFrom(*_memory);
  _url = url;
  return file.close();
}

void HexEditor::display()
{
  _modified = false;
  if (_memory) {
    _originalMemory->copyFrom(*_memory);
    static_cast<Device::HexView *>(_view)->display(_memory);
    static_cast<Device::HexView *>(_view)->setReadOnly(isReadOnly());
    static_cast<Device::HexView *>(_view)->updateDisplay();
  }
  statusChanged();
}

void HexEditor::memoryRead()
{
  display();
  emit guiChanged();
}

void HexEditor::slotModified()
{
  static_cast<Device::HexView *>(_view)->updateDisplay();
  _modified = true;
  statusChanged();
  emit guiChanged();
}

void HexEditor::statusChanged()
{
  TQString s;
  if (_memory) {
    BitValue cs = static_cast<Device::HexView *>(_view)->checksum();
    s = i18n("Checksum: %1").arg(toHexLabel(cs, 4));
  }
  emit statusTextChanged(s);
}