1 /*
2     This file is part of the Okteta Kasten module, made within the KDE community.
3 
4     SPDX-FileCopyrightText: 2008 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 "infotool.hpp"
10 
11 // controller
12 #include "statistictablemodel.hpp"
13 #include "createstatisticjob.hpp"
14 // Okteta Kasten gui
15 #include <Kasten/Okteta/ByteArrayView>
16 // Okteta Kasten core
17 #include <Kasten/Okteta/ByteArrayDocument>
18 // Okteta core
19 #include <Okteta/AbstractByteArrayModel>
20 #include <Okteta/ArrayChangeMetricsList>
21 // KF
22 #include <KLocalizedString>
23 // Qt
24 #include <QApplication>
25 
26 namespace Kasten {
27 
InfoTool()28 InfoTool::InfoTool()
29     : mStatisticTableModel(new StatisticTableModel(mByteCount, this))
30 {
31     setObjectName(QStringLiteral("Info"));
32     updateStatistic();
33 }
34 
35 InfoTool::~InfoTool() = default;
36 
title() const37 QString InfoTool::title() const { return i18nc("@title:window", "Statistics"); }
statisticTableModel() const38 StatisticTableModel* InfoTool::statisticTableModel() const { return mStatisticTableModel; }
size() const39 int InfoTool::size() const { return (mByteArrayModel != nullptr) ? mByteArrayModel->size() : -1; }
isApplyable() const40 bool InfoTool::isApplyable() const
41 {
42     return (mByteArrayModel && mByteArrayView && mByteArrayView->hasSelectedData() && !isStatisticUptodate());
43 }
isStatisticUptodate() const44 bool InfoTool::isStatisticUptodate() const
45 {
46     return (mSourceByteArrayModelUptodate
47             && mSourceByteArrayModel == mByteArrayModel
48             && mByteArrayView && mSourceSelection == mByteArrayView->selection());
49 }
50 
setTargetModel(AbstractModel * model)51 void InfoTool::setTargetModel(AbstractModel* model)
52 {
53     if (mByteArrayView) {
54         mByteArrayView->disconnect(mStatisticTableModel);
55         mByteArrayView->disconnect(this);
56     }
57 
58     mByteArrayView = model ? model->findBaseModel<ByteArrayView*>() : nullptr;
59 
60     ByteArrayDocument* document =
61         mByteArrayView ? qobject_cast<ByteArrayDocument*>(mByteArrayView->baseModel()) : nullptr;
62     mByteArrayModel = document ? document->content() : nullptr;
63 
64     if (mByteArrayView && mByteArrayModel) {
65         mStatisticTableModel->setCharCodec(mByteArrayView->charCodingName());
66         mStatisticTableModel->setValueCoding(mByteArrayView->valueCoding());
67         mStatisticTableModel->setSubstituteChar(mByteArrayView->substituteChar());
68         mStatisticTableModel->setUndefinedChar(mByteArrayView->undefinedChar());
69         connect(mByteArrayView,  &ByteArrayView::charCodecChanged,
70                 mStatisticTableModel, &StatisticTableModel::setCharCodec);
71         connect(mByteArrayView,  &ByteArrayView::valueCodingChanged,
72                 mStatisticTableModel, &StatisticTableModel::setValueCoding);
73         connect(mByteArrayView,  &ByteArrayView::substituteCharChanged,
74                 mStatisticTableModel, &StatisticTableModel::setSubstituteChar);
75         connect(mByteArrayView,  &ByteArrayView::undefinedCharChanged,
76                 mStatisticTableModel, &StatisticTableModel::setUndefinedChar);
77 
78         connect(mByteArrayView,  &ByteArrayView::selectedDataChanged,
79                 this, &InfoTool::onSelectionChanged);
80     } else {
81         // TODO: set based on default view profile, also char codec
82         mStatisticTableModel->setSubstituteChar(QChar());
83         mStatisticTableModel->setUndefinedChar(QChar());
84     }
85 
86     emit statisticDirty(!isStatisticUptodate());
87     emit isApplyableChanged(isApplyable());
88 }
89 
onSelectionChanged()90 void InfoTool::onSelectionChanged()
91 {
92 // TODO: could be quicker using the selection data
93     emit statisticDirty(!isStatisticUptodate());
94     emit isApplyableChanged(isApplyable());
95 }
96 
onSourceChanged()97 void InfoTool::onSourceChanged()
98 {
99     mSourceByteArrayModelUptodate = false;
100     emit statisticDirty(true);
101     emit isApplyableChanged(isApplyable());
102 }
103 
onSourceDestroyed()104 void InfoTool::onSourceDestroyed()
105 {
106     mSourceByteArrayModel = nullptr;
107     onSourceChanged();
108 }
109 
updateStatistic()110 void InfoTool::updateStatistic()
111 {
112     // forget old string source
113     if (mSourceByteArrayModel) {
114         mSourceByteArrayModel->disconnect(this);
115     }
116 
117     QApplication::setOverrideCursor(Qt::WaitCursor);
118 
119     const Okteta::AddressRange selection = (mByteArrayView ? mByteArrayView->selection() : Okteta::AddressRange());
120     auto* createStatisticJob = new CreateStatisticJob(mByteArrayModel, selection, mByteCount);
121     const int selectionSize = createStatisticJob->exec();
122 
123     QApplication::restoreOverrideCursor();
124 
125     mStatisticTableModel->update(selectionSize);
126 
127     // remember new string source
128     mSourceByteArrayModel = mByteArrayModel;
129     mSourceSelection = selection;
130     if (mSourceByteArrayModel) {
131         connect(mSourceByteArrayModel,  &Okteta::AbstractByteArrayModel::contentsChanged,
132                 this, &InfoTool::onSourceChanged);
133         connect(mSourceByteArrayModel,  &Okteta::AbstractByteArrayModel::destroyed,
134                 this, &InfoTool::onSourceDestroyed);
135     }
136 
137     mSourceByteArrayModelUptodate = true;
138     emit statisticDirty(false);
139     emit isApplyableChanged(false);
140 
141     if (mByteArrayView) {
142         mByteArrayView->setFocus();
143     }
144 }
145 
146 }
147