1 /*
2     This file is part of the Okteta Kasten module, made within the KDE community.
3 
4     SPDX-FileCopyrightText: 2008-2012 Friedrich W. H. Kossebau <kossebau@kde.org>
5 
6     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
7 */
8 
9 #include "bytearrayjanusview.hpp"
10 
11 // Okteta gui
12 #include <Okteta/ByteArrayColumnView>
13 #include <Okteta/ByteArrayRowView>
14 // Qt
15 #include <QLayout>
16 #include <QScrollBar>
17 
18 namespace Okteta {
19 
ByteArrayJanusView(QWidget * parent)20 ByteArrayJanusView::ByteArrayJanusView(QWidget* parent)
21     : QWidget(parent)
22 {
23     mLayout = new QHBoxLayout(this);
24     mLayout->setContentsMargins(0, 0, 0, 0);
25     setViewModus(ColumnViewId);
26 }
27 
28 ByteArrayJanusView::~ByteArrayJanusView() = default;
29 
setByteArrayModel(AbstractByteArrayModel * byteArrayModel)30 void ByteArrayJanusView::setByteArrayModel(AbstractByteArrayModel* byteArrayModel)
31 {
32     mView->setByteArrayModel(byteArrayModel);
33 }
34 
setViewModus(int viewModus)35 void ByteArrayJanusView::setViewModus(int viewModus)
36 {
37     if (viewModus == mViewModus) {
38         return;
39     }
40 
41     AbstractByteArrayView* newView = (viewModus == ColumnViewId) ?
42                                      (AbstractByteArrayView*)new ByteArrayColumnView(this) :
43                                      (AbstractByteArrayView*)new ByteArrayRowView(this);
44 
45     const bool hasFocus = mView ? mView->hasFocus() : false;
46     if (mView) {
47         newView->setFont(mView->font());
48 
49         newView->setByteArrayModel(mView->byteArrayModel());
50         newView->setReadOnly(mView->isReadOnly());
51         newView->setOverwriteMode(mView->isOverwriteMode());
52         newView->setZoomLevel(mView->zoomLevel());
53         newView->setShowsNonprinting(mView->showsNonprinting());
54         newView->setValueCoding(mView->valueCoding());
55         newView->setCharCoding(mView->charCodingName());
56         newView->setVisibleCodings(mView->visibleCodings());
57         newView->setActiveCoding(mView->activeCoding());
58         newView->toggleOffsetColumn(mView->offsetColumnVisible());
59         newView->setOffsetCoding(mView->offsetCoding());
60         newView->setStartOffset(mView->startOffset());
61         newView->setFirstLineOffset(mView->firstLineOffset());
62         newView->setNoOfBytesPerLine(mView->noOfBytesPerLine());
63         newView->setNoOfGroupedBytes(mView->noOfGroupedBytes());
64         newView->setLayoutStyle(mView->layoutStyle());
65         newView->setSubstituteChar(mView->substituteChar());
66         newView->setUndefinedChar(mView->undefinedChar());
67         newView->setCursorPosition(mView->cursorPosition());
68         newView->setSelection(mView->selection());
69         newView->setMarking(mView->marking());
70 
71         mLayout->removeWidget(mView);
72         delete mView;
73     }
74 
75     mView = newView;
76 
77     mLayout->addWidget(mView);
78     setFocusProxy(mView);
79     if (hasFocus) {
80         mView->setFocus();
81     }
82     mViewModus = viewModus;
83 
84     mView->setContextMenuPolicy(Qt::CustomContextMenu);
85     connect(mView, &QWidget::customContextMenuRequested, this, &ByteArrayJanusView::viewContextMenuRequested);
86 
87     connect(mView, &AbstractByteArrayView::hasSelectedDataChanged, this, &ByteArrayJanusView::hasSelectedDataChanged);
88     connect(mView, &AbstractByteArrayView::selectionChanged, this, &ByteArrayJanusView::selectionChanged);
89     connect(mView, &AbstractByteArrayView::readOnlyChanged, this, &ByteArrayJanusView::readOnlyChanged);
90     connect(mView, &AbstractByteArrayView::overwriteModeChanged, this, &ByteArrayJanusView::overwriteModeChanged);
91     connect(mView, &AbstractByteArrayView::cursorPositionChanged, this, &ByteArrayJanusView::cursorPositionChanged);
92     connect(mView, &AbstractByteArrayView::valueCodingChanged, this, &ByteArrayJanusView::valueCodingChanged);
93     connect(mView, &AbstractByteArrayView::charCodecChanged, this, &ByteArrayJanusView::charCodecChanged);
94     connect(mView, &AbstractByteArrayView::focusChanged, this, &ByteArrayJanusView::focusChanged);
95 
96     connect(mView, &AbstractByteArrayView::offsetColumnVisibleChanged, this, &ByteArrayJanusView::offsetColumnVisibleChanged);
97     connect(mView, &AbstractByteArrayView::offsetCodingChanged, this, &ByteArrayJanusView::offsetCodingChanged);
98     connect(mView, &AbstractByteArrayView::visibleByteArrayCodingsChanged, this, &ByteArrayJanusView::visibleByteArrayCodingsChanged);
99     connect(mView, &AbstractByteArrayView::layoutStyleChanged, this, &ByteArrayJanusView::layoutStyleChanged);
100     connect(mView, &AbstractByteArrayView::noOfBytesPerLineChanged, this, &ByteArrayJanusView::noOfBytesPerLineChanged);
101     connect(mView, &AbstractByteArrayView::showsNonprintingChanged, this, &ByteArrayJanusView::showsNonprintingChanged);
102     connect(mView, &AbstractByteArrayView::substituteCharChanged, this, &ByteArrayJanusView::substituteCharChanged);
103     connect(mView, &AbstractByteArrayView::undefinedCharChanged, this, &ByteArrayJanusView::undefinedCharChanged);
104     connect(mView, &AbstractByteArrayView::noOfGroupedBytesChanged, this, &ByteArrayJanusView::noOfGroupedBytesChanged);
105     connect(mView, &AbstractByteArrayView::zoomLevelChanged, this, &ByteArrayJanusView::zoomLevelChanged);
106 
107     emit viewModusChanged(mViewModus);
108 }
109 
isReadOnly() const110 bool ByteArrayJanusView::isReadOnly()             const { return mView->isReadOnly(); }
setReadOnly(bool isReadOnly)111 void ByteArrayJanusView::setReadOnly(bool isReadOnly) { mView->setReadOnly(isReadOnly); }
112 
setZoomLevel(double Level)113 void ByteArrayJanusView::setZoomLevel(double Level)
114 {
115     mView->setZoomLevel(Level);
116 }
117 
zoomLevel() const118 double ByteArrayJanusView::zoomLevel() const
119 {
120     return mView->zoomLevel();
121 }
122 
selectAll(bool selectAll)123 void ByteArrayJanusView::selectAll(bool selectAll)
124 {
125     mView->selectAll(selectAll);
126 }
127 
hasSelectedData() const128 bool ByteArrayJanusView::hasSelectedData() const
129 {
130     return mView->hasSelectedData();
131 }
132 
selectionAsMimeData() const133 QMimeData* ByteArrayJanusView::selectionAsMimeData() const
134 {
135     return mView->selectionAsMimeData();
136 }
137 
pasteData(const QMimeData * data)138 void ByteArrayJanusView::pasteData(const QMimeData* data)
139 {
140     mView->pasteData(data);
141 }
142 
removeSelectedData()143 void ByteArrayJanusView::removeSelectedData()
144 {
145     mView->removeSelectedData();
146 }
147 
canReadData(const QMimeData * data) const148 bool ByteArrayJanusView::canReadData(const QMimeData* data) const
149 {
150     return mView->canReadData(data);
151 }
152 
setCursorPosition(Address cursorPosition)153 void ByteArrayJanusView::setCursorPosition(Address cursorPosition)
154 {
155     mView->setCursorPosition(cursorPosition);
156 }
setSelectionCursorPosition(Address index)157 void ByteArrayJanusView::setSelectionCursorPosition(Address index)
158 {
159     mView->setSelectionCursorPosition(index);
160 }
cursorPosition() const161 Address ByteArrayJanusView::cursorPosition() const
162 {
163     return mView->cursorPosition();
164 }
cursorRect() const165 QRect ByteArrayJanusView::cursorRect() const
166 {
167     // Okteta Gui uses viewport coordinates like QTextEdit,
168     // but here view coordinates are used, so map the rect as needed
169     QRect cursorRect = mView->cursorRect();
170     const QPoint viewTopLeft = mView->viewport()->mapToParent(cursorRect.topLeft());
171     cursorRect.moveTopLeft(viewTopLeft);
172     return cursorRect;
173 }
174 
setStartOffset(Address startOffset)175 void ByteArrayJanusView::setStartOffset(Address startOffset)
176 {
177     mView->setStartOffset(startOffset);
178 }
setFirstLineOffset(Address firstLineOffset)179 void ByteArrayJanusView::setFirstLineOffset(Address firstLineOffset)
180 {
181     mView->setFirstLineOffset(firstLineOffset);
182 }
setNoOfBytesPerLine(int noOfBytesPerLine)183 void ByteArrayJanusView::setNoOfBytesPerLine(int noOfBytesPerLine)
184 {
185     mView->setNoOfBytesPerLine(noOfBytesPerLine);
186 }
startOffset() const187 Address ByteArrayJanusView::startOffset() const
188 {
189     return mView->startOffset();
190 }
firstLineOffset() const191 Address ByteArrayJanusView::firstLineOffset() const
192 {
193     return mView->firstLineOffset();
194 }
noOfBytesPerLine() const195 int ByteArrayJanusView::noOfBytesPerLine() const
196 {
197     return mView->noOfBytesPerLine();
198 }
199 
valueCoding() const200 int ByteArrayJanusView::valueCoding() const
201 {
202     return mView->valueCoding();
203 }
204 
charCodingName() const205 QString ByteArrayJanusView::charCodingName() const
206 {
207     return mView->charCodingName();
208 }
209 
setValueCoding(int valueCoding)210 void ByteArrayJanusView::setValueCoding(int valueCoding)
211 {
212     mView->setValueCoding((AbstractByteArrayView::ValueCoding)valueCoding);
213 }
214 
setCharCoding(const QString & charCodingName)215 void ByteArrayJanusView::setCharCoding(const QString& charCodingName)
216 {
217     mView->setCharCoding(charCodingName);
218 }
219 
selection() const220 AddressRange ByteArrayJanusView::selection() const
221 {
222     return mView->selection();
223 }
224 
setSelection(Address start,Address end)225 void ByteArrayJanusView::setSelection(Address start, Address end)
226 {
227     mView->setSelection(start, end);
228 }
229 
setMarking(const AddressRange & marking)230 void ByteArrayJanusView::setMarking(const AddressRange& marking)
231 {
232     mView->setMarking(marking);
233 }
234 
ensureVisible(const AddressRange & range)235 void ByteArrayJanusView::ensureVisible(const AddressRange& range)
236 {
237     mView->ensureVisible(range);
238 }
239 
insert(const QByteArray & byteArray)240 void ByteArrayJanusView::insert(const QByteArray& byteArray)
241 {
242     mView->insert(byteArray);
243 }
244 
showsNonprinting() const245 bool ByteArrayJanusView::showsNonprinting() const
246 {
247     return false; // TODOSHOWNONPRINTING pin to false for now return mView->showsNonprinting();
248 }
249 
offsetColumnVisible() const250 bool ByteArrayJanusView::offsetColumnVisible() const
251 {
252     return mView->offsetColumnVisible();
253 }
254 
offsetCoding() const255 int ByteArrayJanusView::offsetCoding() const
256 {
257     return mView->offsetCoding();
258 }
259 
layoutStyle() const260 int ByteArrayJanusView::layoutStyle() const
261 {
262     return (int)mView->layoutStyle();
263 }
264 
visibleCodings() const265 int ByteArrayJanusView::visibleCodings() const
266 {
267     return (int)mView->visibleCodings();
268 }
269 
isOverwriteMode() const270 bool ByteArrayJanusView::isOverwriteMode() const
271 {
272     return mView->isOverwriteMode();
273 }
274 
setShowsNonprinting(bool on)275 void ByteArrayJanusView::setShowsNonprinting(bool on)
276 {
277     Q_UNUSED(on);
278     // TODOSHOWNONPRINTING mView->setShowsNonprinting(on);
279 }
280 
setNoOfGroupedBytes(int noOfGroupedBytes)281 void ByteArrayJanusView::setNoOfGroupedBytes(int noOfGroupedBytes)
282 {
283     mView->setNoOfGroupedBytes(noOfGroupedBytes);
284 }
285 
setSubstituteChar(QChar substituteChar)286 void ByteArrayJanusView::setSubstituteChar(QChar substituteChar)
287 {
288     mView->setSubstituteChar(substituteChar);
289 }
290 
setUndefinedChar(QChar undefinedChar)291 void ByteArrayJanusView::setUndefinedChar(QChar undefinedChar)
292 {
293     mView->setUndefinedChar(undefinedChar);
294 }
295 
toggleOffsetColumn(bool on)296 void ByteArrayJanusView::toggleOffsetColumn(bool on)
297 {
298     mView->toggleOffsetColumn(on);
299 }
300 
setOffsetCoding(int offsetCoding)301 void ByteArrayJanusView::setOffsetCoding(int offsetCoding)
302 {
303     mView->setOffsetCoding((AbstractByteArrayView::OffsetCoding)offsetCoding);
304 }
305 
setLayoutStyle(int layoutStyle)306 void ByteArrayJanusView::setLayoutStyle(int layoutStyle)
307 {
308     mView->setLayoutStyle((AbstractByteArrayView::LayoutStyle)layoutStyle);
309 }
310 
setVisibleCodings(int visibleColumns)311 void ByteArrayJanusView::setVisibleCodings(int visibleColumns)
312 {
313     mView->setVisibleCodings(visibleColumns);
314 }
315 
substituteChar() const316 QChar ByteArrayJanusView::substituteChar() const
317 {
318     return mView->substituteChar();
319 }
undefinedChar() const320 QChar ByteArrayJanusView::undefinedChar() const
321 {
322     return mView->undefinedChar();
323 }
324 
byteSpacingWidth() const325 int ByteArrayJanusView::byteSpacingWidth() const
326 {
327     return mView->byteSpacingWidth();
328 }
noOfGroupedBytes() const329 int ByteArrayJanusView::noOfGroupedBytes() const
330 {
331     return mView->noOfGroupedBytes();
332 }
groupSpacingWidth() const333 int ByteArrayJanusView::groupSpacingWidth() const
334 {
335     return mView->groupSpacingWidth();
336 }
binaryGapWidth() const337 int ByteArrayJanusView::binaryGapWidth() const
338 {
339     return mView->binaryGapWidth();
340 }
341 
isOverwriteOnly() const342 bool ByteArrayJanusView::isOverwriteOnly() const
343 {
344     return mView->isOverwriteOnly();
345 }
346 
setOverwriteMode(bool overwriteMode)347 void ByteArrayJanusView::setOverwriteMode(bool overwriteMode)
348 {
349     mView->setOverwriteMode(overwriteMode);
350 }
351 
setViewPos(QPoint pos)352 void ByteArrayJanusView::setViewPos(QPoint pos)
353 {
354     mView->horizontalScrollBar()->setValue(pos.x());
355     mView->verticalScrollBar()->setValue(pos.y());
356 }
viewRect() const357 QRect ByteArrayJanusView::viewRect() const
358 {
359     // TODO: find why mView->viewport()->rect() doesn't work but is always at pos (0.0)
360     const QRect result(
361         QPoint(mView->horizontalScrollBar()->value(), mView->verticalScrollBar()->value()),
362         mView->viewport()->size());
363     return result;
364 }
365 
propagateFont(const QFont & font)366 void ByteArrayJanusView::propagateFont(const QFont& font)
367 {
368     setFont(font);
369     mView->setFont(font);
370 }
371 
372 }
373