Warning, file /utilities/okteta/kasten/gui/view/bytearrayview.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002     This file is part of the Okteta Kasten module, made within the KDE community.
0003 
0004     SPDX-FileCopyrightText: 2006-2012 Friedrich W. H. Kossebau <kossebau@kde.org>
0005 
0006     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0007 */
0008 
0009 #include "bytearrayview.hpp"
0010 
0011 // lib
0012 #include "bytearrayjanusview.hpp"
0013 #include <bytearrayviewprofilesynchronizer.hpp>
0014 // Okteta Kasten core
0015 #include <Kasten/Okteta/ByteArrayDocument>
0016 // Okteta gui
0017 #include <Okteta/AbstractByteArrayView>
0018 // Okteta core
0019 #include <Okteta/AbstractByteArrayModel>
0020 // Qt
0021 #include <QFontDatabase>
0022 
0023 namespace Kasten {
0024 
0025 // TODO: merge into ByteArrayView on next ABI break
0026 class SelectedDataCutExtension : public QObject, public If::SelectedDataCutable
0027 {
0028     Q_OBJECT
0029     Q_INTERFACES(
0030         Kasten::If::SelectedDataCutable
0031     )
0032 
0033 public:
0034     explicit SelectedDataCutExtension(ByteArrayView* view);
0035 
0036 public: // If::SelectedDataCutable API
0037     bool canCutSelectedData() const override;
0038 Q_SIGNALS:
0039     void canCutSelectedDataChanged(bool canCutSelectedData) override;
0040 
0041 private Q_SLOTS:
0042     void onOverwriteModeChanged(bool overwriteMode);
0043 };
0044 
0045 SelectedDataCutExtension::SelectedDataCutExtension(ByteArrayView* view)
0046     : QObject(view)
0047 {
0048     connect(view, &ByteArrayView::overwriteModeChanged, this, &SelectedDataCutExtension::onOverwriteModeChanged);
0049 }
0050 
0051 bool SelectedDataCutExtension::canCutSelectedData() const
0052 {
0053     return !static_cast<ByteArrayView*>(parent())->isOverwriteMode();
0054 }
0055 
0056 void SelectedDataCutExtension::onOverwriteModeChanged(bool overwriteMode)
0057 {
0058     emit canCutSelectedDataChanged(!overwriteMode);
0059 }
0060 
0061 
0062 ByteArrayView::ByteArrayView(ByteArrayDocument* document, ByteArrayViewProfileSynchronizer* synchronizer)
0063     : AbstractView(document)
0064     , mDocument(document)
0065     , mByteArrayViewProfileSynchronizer(synchronizer)
0066 {
0067     init();
0068     if (synchronizer) {
0069         synchronizer->setView(this);
0070     }
0071 }
0072 
0073 ByteArrayView::ByteArrayView(ByteArrayView* other, ByteArrayViewProfileSynchronizer* synchronizer,
0074                              Qt::Alignment alignment)
0075     : AbstractView(static_cast<ByteArrayDocument*>(other->baseModel()))
0076     , mDocument(static_cast<ByteArrayDocument*>(other->baseModel()))
0077     , mByteArrayViewProfileSynchronizer(synchronizer)
0078 {
0079     init();
0080 
0081     mWidget->setStartOffset(other->startOffset());
0082     mWidget->setFirstLineOffset(other->firstLineOffset());
0083 
0084     setViewModus(other->viewModus());
0085     setVisibleByteArrayCodings(other->visibleByteArrayCodings());
0086     toggleOffsetColumn(other->offsetColumnVisible());
0087     setOffsetCoding(other->offsetCoding());
0088 
0089     setCharCoding(other->charCodingName());
0090     setShowsNonprinting(other->showsNonprinting());
0091     setSubstituteChar(other->substituteChar());
0092     setUndefinedChar(other->undefinedChar());
0093 
0094     setValueCoding(other->valueCoding());
0095 
0096     setNoOfGroupedBytes(other->noOfGroupedBytes());
0097     setNoOfBytesPerLine(other->noOfBytesPerLine());
0098     // TODO: this can lead to different layouts due to possible one-pixel difference in width!
0099     setLayoutStyle(other->layoutStyle());
0100 
0101     const Okteta::AddressRange selection = other->selection();
0102     setSelection(selection.start(), selection.end());
0103     setZoomLevel(other->zoomLevel());
0104     setCursorPosition(other->cursorPosition());
0105 
0106     setOverwriteMode(other->isOverwriteMode());
0107     setReadOnly(other->isReadOnly());
0108     // TODO: all width
0109 
0110     const QRect otherViewRect = other->mWidget->viewRect();
0111 
0112     QPoint viewPos = otherViewRect.topLeft();
0113     if (alignment == Qt::AlignBottom) {
0114         viewPos.setY(otherViewRect.bottom() + 1);
0115     }
0116     // TODO: care for resize style
0117     else if (alignment == Qt::AlignRight) {
0118         viewPos.setX(otherViewRect.right() + 1);
0119     }
0120     // TODO: doesn't really work at this stage, because the widget will get resized when inserted
0121     // and then ensureCursorVisible destroys the fun
0122     mWidget->setViewPos(viewPos);
0123 
0124     if (synchronizer) {
0125         synchronizer->setView(this);
0126     }
0127 }
0128 
0129 ByteArrayView::~ByteArrayView()
0130 {
0131     delete mByteArrayViewProfileSynchronizer;
0132     delete mWidget;
0133 }
0134 
0135 void ByteArrayView::init()
0136 {
0137     Okteta::AbstractByteArrayModel* content = mDocument->content();
0138     mWidget = new Okteta::ByteArrayJanusView();
0139     mWidget->setByteArrayModel(content);
0140 
0141     // TODO: find a signal/event emitted when fixedfont changes
0142 //     connect( KGlobalSettings::self(), &KGlobalSettings::kdisplayFontChanged,
0143 //              this, &ByteArrayView::setFontByGlobalSettings );
0144     setFontByGlobalSettings();
0145 
0146     mWidget->setNoOfBytesPerLine(16);
0147 
0148     const bool useOverwriteAsDefault = (content->size() > 0);
0149     mWidget->setOverwriteMode(useOverwriteAsDefault);
0150 
0151     // propagate signals
0152     using Okteta::ByteArrayJanusView;
0153     connect(mDocument, &ByteArrayDocument::titleChanged, this, &ByteArrayView::titleChanged);
0154     connect(mWidget, &ByteArrayJanusView::hasSelectedDataChanged, this, &ByteArrayView::hasSelectedDataChanged);
0155     connect(mWidget, &ByteArrayJanusView::readOnlyChanged, this, &ByteArrayView::readOnlyChanged);
0156     connect(mWidget, &ByteArrayJanusView::overwriteModeChanged, this, &ByteArrayView::overwriteModeChanged);
0157     connect(mWidget, &ByteArrayJanusView::selectionChanged, this, &ByteArrayView::onSelectionChanged);
0158     connect(mWidget, &ByteArrayJanusView::cursorPositionChanged, this, &ByteArrayView::cursorPositionChanged);
0159     connect(mWidget, &ByteArrayJanusView::valueCodingChanged, this, &ByteArrayView::valueCodingChanged);
0160     connect(mWidget, &ByteArrayJanusView::charCodecChanged, this, &ByteArrayView::charCodecChanged);
0161     connect(mWidget, &ByteArrayJanusView::focusChanged, this, &ByteArrayView::focusChanged);
0162 
0163     connect(mWidget, &ByteArrayJanusView::offsetColumnVisibleChanged, this, &ByteArrayView::offsetColumnVisibleChanged);
0164     connect(mWidget, &ByteArrayJanusView::offsetCodingChanged, this, &ByteArrayView::offsetCodingChanged);
0165     connect(mWidget, &ByteArrayJanusView::visibleByteArrayCodingsChanged, this, &ByteArrayView::visibleByteArrayCodingsChanged);
0166     connect(mWidget, &ByteArrayJanusView::layoutStyleChanged, this, &ByteArrayView::layoutStyleChanged);
0167     connect(mWidget, &ByteArrayJanusView::noOfBytesPerLineChanged, this, &ByteArrayView::noOfBytesPerLineChanged);
0168     connect(mWidget, &ByteArrayJanusView::showsNonprintingChanged, this, &ByteArrayView::showsNonprintingChanged);
0169     connect(mWidget, &ByteArrayJanusView::substituteCharChanged, this, &ByteArrayView::substituteCharChanged);
0170     connect(mWidget, &ByteArrayJanusView::undefinedCharChanged, this, &ByteArrayView::undefinedCharChanged);
0171     connect(mWidget, &ByteArrayJanusView::noOfGroupedBytesChanged, this, &ByteArrayView::noOfGroupedBytesChanged);
0172     connect(mWidget, &ByteArrayJanusView::zoomLevelChanged, this, &ByteArrayView::zoomLevelChanged);
0173     connect(mWidget, &ByteArrayJanusView::viewModusChanged, this, &ByteArrayView::viewModusChanged);
0174 
0175     connect(mWidget, &ByteArrayJanusView::viewContextMenuRequested, this, &ByteArrayView::viewContextMenuRequested);
0176 
0177     new SelectedDataCutExtension(this);
0178 }
0179 
0180 ByteArrayViewProfileSynchronizer* ByteArrayView::synchronizer() const { return mByteArrayViewProfileSynchronizer; }
0181 
0182 const AbstractModelSelection* ByteArrayView::modelSelection() const { return &mSelection; }
0183 
0184 QWidget* ByteArrayView::widget()             const { return mWidget; }
0185 bool ByteArrayView::hasFocus()               const { return mWidget->focusWidget()->hasFocus(); } // TODO: does this work?
0186 
0187 QString ByteArrayView::title()               const { return mDocument->title(); }
0188 bool ByteArrayView::isModifiable()           const { return true; }
0189 bool ByteArrayView::isReadOnly()             const { return mWidget->isReadOnly(); }
0190 
0191 void ByteArrayView::setFocus()
0192 {
0193     mWidget->setFocus();
0194 }
0195 
0196 void ByteArrayView::setReadOnly(bool isReadOnly) { mWidget->setReadOnly(isReadOnly); }
0197 
0198 void ByteArrayView::setZoomLevel(double Level)
0199 {
0200     mWidget->setZoomLevel(Level);
0201 }
0202 
0203 double ByteArrayView::zoomLevel() const
0204 {
0205     return mWidget->zoomLevel();
0206 }
0207 
0208 void ByteArrayView::selectAllData(bool selectAll)
0209 {
0210     mWidget->selectAll(selectAll);
0211 }
0212 
0213 bool ByteArrayView::hasSelectedData() const
0214 {
0215     return mWidget->hasSelectedData();
0216 }
0217 
0218 QMimeData* ByteArrayView::copySelectedData() const
0219 {
0220     return mWidget->selectionAsMimeData();
0221 }
0222 
0223 void ByteArrayView::insertData(const QMimeData* data)
0224 {
0225     mWidget->pasteData(data);
0226 }
0227 
0228 QMimeData* ByteArrayView::cutSelectedData()
0229 {
0230     QMimeData* result = mWidget->selectionAsMimeData();
0231     mWidget->removeSelectedData();
0232     return result;
0233 }
0234 
0235 void ByteArrayView::deleteSelectedData()
0236 {
0237     mWidget->removeSelectedData();
0238 }
0239 
0240 bool ByteArrayView::canReadData(const QMimeData* data) const
0241 {
0242     return mWidget->canReadData(data);
0243 }
0244 
0245 void ByteArrayView::onSelectionChanged(const Okteta::AddressRange& selection)
0246 {
0247     // TODO: how to make sure the signal hasSelectedDataChanged() is not emitted before?
0248     mSelection.setRange(selection);
0249     emit selectedDataChanged(&mSelection);
0250 }
0251 
0252 void ByteArrayView::setCursorPosition(Okteta::Address cursorPosition)
0253 {
0254     mWidget->setCursorPosition(cursorPosition);
0255 }
0256 
0257 void ByteArrayView::setSelectionCursorPosition(Okteta::Address index)
0258 {
0259     mWidget->setSelectionCursorPosition(index);
0260 }
0261 
0262 Okteta::Address ByteArrayView::cursorPosition() const
0263 {
0264     return mWidget->cursorPosition();
0265 }
0266 QRect ByteArrayView::cursorRect() const
0267 {
0268     return mWidget->cursorRect();
0269 }
0270 
0271 Okteta::Address ByteArrayView::startOffset() const
0272 {
0273     return mWidget->startOffset();
0274 }
0275 Okteta::Address ByteArrayView::firstLineOffset() const
0276 {
0277     return mWidget->firstLineOffset();
0278 }
0279 int ByteArrayView::noOfBytesPerLine() const
0280 {
0281     return mWidget->noOfBytesPerLine();
0282 }
0283 
0284 int ByteArrayView::valueCoding() const
0285 {
0286     return mWidget->valueCoding();
0287 }
0288 
0289 QString ByteArrayView::charCodingName() const
0290 {
0291     return mWidget->charCodingName();
0292 }
0293 
0294 void ByteArrayView::setValueCoding(int valueCoding)
0295 {
0296     mWidget->setValueCoding((Okteta::AbstractByteArrayView::ValueCoding)valueCoding);
0297 }
0298 
0299 void ByteArrayView::setCharCoding(const QString& charCodingName)
0300 {
0301     mWidget->setCharCoding(charCodingName);
0302 }
0303 
0304 Okteta::AddressRange ByteArrayView::selection() const
0305 {
0306     return mWidget->selection();
0307 }
0308 
0309 void ByteArrayView::setSelection(Okteta::Address start, Okteta::Address end)
0310 {
0311     mWidget->setSelection(start, end);
0312 }
0313 
0314 void ByteArrayView::insert(const QByteArray& byteArray)
0315 {
0316     mWidget->insert(byteArray);
0317 }
0318 
0319 bool ByteArrayView::showsNonprinting() const
0320 {
0321     return mWidget->showsNonprinting();
0322 }
0323 
0324 bool ByteArrayView::offsetColumnVisible() const
0325 {
0326     return mWidget->offsetColumnVisible();
0327 }
0328 
0329 int ByteArrayView::offsetCoding() const
0330 {
0331     return mWidget->offsetCoding();
0332 }
0333 
0334 int ByteArrayView::layoutStyle() const
0335 {
0336     return (int)mWidget->layoutStyle();
0337 }
0338 
0339 int ByteArrayView::visibleByteArrayCodings() const
0340 {
0341     return (int)mWidget->visibleCodings();
0342 }
0343 
0344 bool ByteArrayView::isOverwriteMode() const
0345 {
0346     return mWidget->isOverwriteMode();
0347 }
0348 
0349 void ByteArrayView::setShowsNonprinting(bool on)
0350 {
0351     mWidget->setShowsNonprinting(on);
0352 }
0353 
0354 void ByteArrayView::setNoOfGroupedBytes(int noOfGroupedBytes)
0355 {
0356     mWidget->setNoOfGroupedBytes(noOfGroupedBytes);
0357 }
0358 
0359 void ByteArrayView::toggleOffsetColumn(bool on)
0360 {
0361     mWidget->toggleOffsetColumn(on);
0362 }
0363 
0364 void ByteArrayView::setOffsetCoding(int offsetCoding)
0365 {
0366     mWidget->setOffsetCoding(offsetCoding);
0367 }
0368 
0369 void ByteArrayView::setLayoutStyle(int layoutStyle)
0370 {
0371     mWidget->setLayoutStyle((Okteta::AbstractByteArrayView::LayoutStyle)layoutStyle);
0372 }
0373 
0374 void ByteArrayView::setNoOfBytesPerLine(int noOfBytesPerLine)
0375 {
0376     mWidget->setNoOfBytesPerLine(noOfBytesPerLine);
0377 }
0378 
0379 void ByteArrayView::setVisibleByteArrayCodings(int visibleColumns)
0380 {
0381     mWidget->setVisibleCodings(visibleColumns);
0382 }
0383 
0384 void ByteArrayView::setMarking(const Okteta::AddressRange& range, bool ensureVisible)
0385 {
0386     mWidget->setMarking(range);
0387     if (ensureVisible) {
0388         mWidget->ensureVisible(range);
0389     }
0390 }
0391 
0392 void ByteArrayView::setSubstituteChar(QChar substituteChar)
0393 {
0394     mWidget->setSubstituteChar(substituteChar);
0395 }
0396 
0397 void ByteArrayView::setUndefinedChar(QChar undefinedChar)
0398 {
0399     mWidget->setUndefinedChar(undefinedChar);
0400 }
0401 
0402 QChar ByteArrayView::substituteChar() const
0403 {
0404     return mWidget->substituteChar();
0405 }
0406 QChar ByteArrayView::undefinedChar() const
0407 {
0408     return mWidget->undefinedChar();
0409 }
0410 
0411 int ByteArrayView::byteSpacingWidth() const
0412 {
0413     return mWidget->byteSpacingWidth();
0414 }
0415 int ByteArrayView::noOfGroupedBytes() const
0416 {
0417     return mWidget->noOfGroupedBytes();
0418 }
0419 int ByteArrayView::groupSpacingWidth() const
0420 {
0421     return mWidget->groupSpacingWidth();
0422 }
0423 int ByteArrayView::binaryGapWidth() const
0424 {
0425     return mWidget->binaryGapWidth();
0426 }
0427 
0428 bool ByteArrayView::isOverwriteOnly() const
0429 {
0430     return mWidget->isOverwriteOnly();
0431 }
0432 
0433 void ByteArrayView::setOverwriteMode(bool overwriteMode)
0434 {
0435     mWidget->setOverwriteMode(overwriteMode);
0436 }
0437 
0438 void ByteArrayView::setViewModus(int viewModus)
0439 {
0440     mWidget->setViewModus(viewModus);
0441 }
0442 int ByteArrayView::viewModus() const
0443 {
0444     return mWidget->viewModus();
0445 }
0446 
0447 void ByteArrayView::setFontByGlobalSettings()
0448 {
0449     mWidget->propagateFont(QFontDatabase::systemFont(QFontDatabase::FixedFont));
0450 }
0451 
0452 }
0453 
0454 // needed for SelectedDataCutExtension
0455 #include "bytearrayview.moc"
0456 #include "moc_bytearrayview.cpp"