File indexing completed on 2025-01-05 05:23:30

0001 /*
0002     This file is part of the Okteta Kasten module, made within the KDE community.
0003 
0004     SPDX-FileCopyrightText: 2008 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 "infotool.hpp"
0010 
0011 // controller
0012 #include "statistictablemodel.hpp"
0013 #include "createstatisticjob.hpp"
0014 // Okteta Kasten gui
0015 #include <Kasten/Okteta/ByteArrayView>
0016 // Okteta Kasten core
0017 #include <Kasten/Okteta/ByteArrayDocument>
0018 // Okteta core
0019 #include <Okteta/AbstractByteArrayModel>
0020 #include <Okteta/ArrayChangeMetricsList>
0021 // KF
0022 #include <KLocalizedString>
0023 // Qt
0024 #include <QApplication>
0025 
0026 namespace Kasten {
0027 
0028 InfoTool::InfoTool()
0029     : mStatisticTableModel(new StatisticTableModel(mByteCount, this))
0030 {
0031     setObjectName(QStringLiteral("Info"));
0032     updateStatistic();
0033 }
0034 
0035 InfoTool::~InfoTool() = default;
0036 
0037 QString InfoTool::title() const { return i18nc("@title:window", "Statistics"); }
0038 StatisticTableModel* InfoTool::statisticTableModel() const { return mStatisticTableModel; }
0039 int InfoTool::size() const { return (mByteArrayModel != nullptr) ? mByteArrayModel->size() : -1; }
0040 bool InfoTool::isApplyable() const
0041 {
0042     return (mByteArrayModel && mByteArrayView && mByteArrayView->hasSelectedData() && !isStatisticUptodate());
0043 }
0044 bool InfoTool::isStatisticUptodate() const
0045 {
0046     return (mSourceByteArrayModelUptodate
0047             && mSourceByteArrayModel == mByteArrayModel
0048             && mByteArrayView && mSourceSelection == mByteArrayView->selection());
0049 }
0050 
0051 void InfoTool::setTargetModel(AbstractModel* model)
0052 {
0053     if (mByteArrayView) {
0054         mByteArrayView->disconnect(mStatisticTableModel);
0055         mByteArrayView->disconnect(this);
0056     }
0057 
0058     mByteArrayView = model ? model->findBaseModel<ByteArrayView*>() : nullptr;
0059 
0060     ByteArrayDocument* document =
0061         mByteArrayView ? qobject_cast<ByteArrayDocument*>(mByteArrayView->baseModel()) : nullptr;
0062     mByteArrayModel = document ? document->content() : nullptr;
0063 
0064     if (mByteArrayView && mByteArrayModel) {
0065         mStatisticTableModel->setCharCodec(mByteArrayView->charCodingName());
0066         mStatisticTableModel->setValueCoding(mByteArrayView->valueCoding());
0067         mStatisticTableModel->setSubstituteChar(mByteArrayView->substituteChar());
0068         mStatisticTableModel->setUndefinedChar(mByteArrayView->undefinedChar());
0069         connect(mByteArrayView,  &ByteArrayView::charCodecChanged,
0070                 mStatisticTableModel, &StatisticTableModel::setCharCodec);
0071         connect(mByteArrayView,  &ByteArrayView::valueCodingChanged,
0072                 mStatisticTableModel, &StatisticTableModel::setValueCoding);
0073         connect(mByteArrayView,  &ByteArrayView::substituteCharChanged,
0074                 mStatisticTableModel, &StatisticTableModel::setSubstituteChar);
0075         connect(mByteArrayView,  &ByteArrayView::undefinedCharChanged,
0076                 mStatisticTableModel, &StatisticTableModel::setUndefinedChar);
0077 
0078         connect(mByteArrayView,  &ByteArrayView::selectedDataChanged,
0079                 this, &InfoTool::onSelectionChanged);
0080     } else {
0081         // TODO: set based on default view profile, also char codec
0082         mStatisticTableModel->setSubstituteChar(QChar());
0083         mStatisticTableModel->setUndefinedChar(QChar());
0084     }
0085 
0086     Q_EMIT statisticDirty(!isStatisticUptodate());
0087     Q_EMIT isApplyableChanged(isApplyable());
0088 }
0089 
0090 void InfoTool::onSelectionChanged()
0091 {
0092 // TODO: could be quicker using the selection data
0093     Q_EMIT statisticDirty(!isStatisticUptodate());
0094     Q_EMIT isApplyableChanged(isApplyable());
0095 }
0096 
0097 void InfoTool::onSourceChanged()
0098 {
0099     mSourceByteArrayModelUptodate = false;
0100     Q_EMIT statisticDirty(true);
0101     Q_EMIT isApplyableChanged(isApplyable());
0102 }
0103 
0104 void InfoTool::onSourceDestroyed()
0105 {
0106     mSourceByteArrayModel = nullptr;
0107     onSourceChanged();
0108 }
0109 
0110 void InfoTool::updateStatistic()
0111 {
0112     // forget old string source
0113     if (mSourceByteArrayModel) {
0114         mSourceByteArrayModel->disconnect(this);
0115     }
0116 
0117     QApplication::setOverrideCursor(Qt::WaitCursor);
0118 
0119     const Okteta::AddressRange selection = (mByteArrayView ? mByteArrayView->selection() : Okteta::AddressRange());
0120     auto* createStatisticJob = new CreateStatisticJob(mByteArrayModel, selection, mByteCount);
0121     const int selectionSize = createStatisticJob->exec();
0122 
0123     QApplication::restoreOverrideCursor();
0124 
0125     mStatisticTableModel->update(selectionSize);
0126 
0127     // remember new string source
0128     mSourceByteArrayModel = mByteArrayModel;
0129     mSourceSelection = selection;
0130     if (mSourceByteArrayModel) {
0131         connect(mSourceByteArrayModel,  &Okteta::AbstractByteArrayModel::contentsChanged,
0132                 this, &InfoTool::onSourceChanged);
0133         connect(mSourceByteArrayModel,  &Okteta::AbstractByteArrayModel::destroyed,
0134                 this, &InfoTool::onSourceDestroyed);
0135     }
0136 
0137     mSourceByteArrayModelUptodate = true;
0138     Q_EMIT statisticDirty(false);
0139     Q_EMIT isApplyableChanged(false);
0140 
0141     if (mByteArrayView) {
0142         mByteArrayView->setFocus();
0143     }
0144 }
0145 
0146 }
0147 
0148 #include "moc_infotool.cpp"