File indexing completed on 2024-06-16 05:24:49

0001 /*
0002     This file is part of the Okteta Kasten module, made within the KDE community.
0003 
0004     SPDX-FileCopyrightText: 2009, 2022 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 "checksumview.hpp"
0010 
0011 // tool
0012 #include "checksumtool.hpp"
0013 // lib
0014 #include <bytearraychecksumparameterseteditfactory.hpp>
0015 #include <abstractbytearraychecksumparametersetedit.hpp>
0016 #include <abstractbytearraychecksumparameterset.hpp>
0017 #include <abstractbytearraychecksumalgorithm.hpp>
0018 // utils
0019 #include <labelledtoolbarwidget.hpp>
0020 // KF
0021 #include <KComboBox>
0022 #include <KLocalizedString>
0023 // Qt
0024 #include <QToolBar>
0025 #include <QLabel>
0026 #include <QGroupBox>
0027 #include <QStackedWidget>
0028 #include <QLineEdit>
0029 #include <QAction>
0030 #include <QVBoxLayout>
0031 #include <QClipboard>
0032 #include <QApplication>
0033 #include <QAbstractItemView>
0034 #include <QIcon>
0035 
0036 namespace Kasten {
0037 
0038 ChecksumView::ChecksumView(ChecksumTool* tool, QWidget* parent)
0039     : AbstractToolWidget(parent)
0040     , mTool(tool)
0041 {
0042     auto* baseLayout = new QVBoxLayout(this);
0043     baseLayout->setContentsMargins(0, 0, 0, 0);
0044     baseLayout->setSpacing(0);
0045 
0046     // algorithm
0047     auto* algorithmToolBar = new QToolBar(this);
0048     auto* label = new QLabel(i18nc("@label:listbox algorithm to use for the checksum", "Algorithm:"), this);
0049     mAlgorithmComboBox = new KComboBox(this);
0050     connect(mAlgorithmComboBox, qOverload<int>(&KComboBox::activated),
0051             mTool, &ChecksumTool::setAlgorithm);
0052 
0053     auto* labelledAlgorithmComboBox = new LabelledToolBarWidget(label, mAlgorithmComboBox, this);
0054     const QString algorithmWhatsThis =
0055         i18nc("@info:whatsthis", "Select the algorithm to use for the checksum.");
0056     label->setWhatsThis(algorithmWhatsThis);
0057     mAlgorithmComboBox->setWhatsThis(algorithmWhatsThis);
0058     mAlgorithmComboBox->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
0059 
0060     algorithmToolBar->addWidget(labelledAlgorithmComboBox);
0061     baseLayout->addWidget(algorithmToolBar);
0062 
0063     // parameter
0064     auto* parameterSetBox = new QGroupBox(i18nc("@title:group", "Parameters"), this);
0065     baseLayout->addWidget(parameterSetBox);
0066 
0067     auto* parameterSetLayout = new QVBoxLayout(parameterSetBox);
0068 
0069     mParameterSetEditStack = new QStackedWidget(parameterSetBox);
0070     parameterSetLayout->addWidget(mParameterSetEditStack);
0071 
0072     // calculate
0073     auto* actionToolBar = new QToolBar(this);
0074     actionToolBar->setToolButtonStyle(Qt::ToolButtonFollowStyle);
0075 
0076     auto* stretcher = new QWidget(this);
0077     stretcher->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
0078     actionToolBar->addWidget(stretcher);
0079 
0080     mCalculateAction =
0081         actionToolBar->addAction(QIcon::fromTheme(QStringLiteral("run-build")),
0082                                  i18nc("@action:button calculate the checksum", "&Calculate"),
0083                                  mTool, &ChecksumTool::calculateChecksum);
0084     mCalculateAction->setToolTip(i18nc("@info:tooltip",
0085                                        "Calculate the checksum for the bytes in the selected range."));
0086     mCalculateAction->setWhatsThis(xi18nc("@info:whatsthis",
0087                                           "If you press the <interface>Calculate</interface> button, the list will be updated "
0088                                           "to all strings which are contained in the selected range and have the set minimum length."));
0089     mCalculateAction->setEnabled(mTool->isApplyable());
0090 
0091     baseLayout->addWidget(actionToolBar);
0092 
0093     mChecksumLabel = new QLineEdit(this);
0094     mChecksumLabel->setReadOnly(true);
0095     mChecksumLabel->setText(mTool->checkSum());
0096     connect(mTool, &ChecksumTool::checksumChanged, mChecksumLabel, &QLineEdit::setText);
0097     baseLayout->addWidget(mChecksumLabel, 10);
0098 
0099     baseLayout->addStretch(10);
0100 
0101     connect(mTool, &ChecksumTool::algorithmChanged, this, &ChecksumView::onAlgorithmChanged);
0102     connect(mTool, &ChecksumTool::uptodateChanged, this, &ChecksumView::onChecksumUptodateChanged);
0103     connect(mTool, &ChecksumTool::isApplyableChanged, this, &ChecksumView::onApplyableChanged);
0104 
0105     // automatically set focus to the parameters if a operation has been selected
0106     QAbstractItemView* algorithmComboBoxListView = mAlgorithmComboBox->view();
0107     QObject::connect(algorithmComboBoxListView, &QAbstractItemView::activated,
0108                      mParameterSetEditStack, qOverload<>(&QStackedWidget::setFocus));
0109     // TODO: is a workaround for Qt 4.5.1 which doesn't emit activated() for mouse clicks
0110     QObject::connect(algorithmComboBoxListView, &QAbstractItemView::pressed,
0111                      mParameterSetEditStack, qOverload<>(&QStackedWidget::setFocus));
0112     // TODO: goto filter button if there are no parameters
0113 
0114     addAlgorithms();
0115 }
0116 
0117 ChecksumView::~ChecksumView() = default;
0118 
0119 void ChecksumView::addAlgorithms()
0120 {
0121     //
0122     const QVector<AbstractByteArrayChecksumAlgorithm*> algorithmList = mTool->algorithmList();
0123     for (AbstractByteArrayChecksumAlgorithm* algorithm : algorithmList) {
0124         mAlgorithmComboBox->addItem(algorithm->name());
0125 
0126         const char* const parameterSetId = algorithm->parameterSet()->id();
0127         AbstractByteArrayChecksumParameterSetEdit* parameterEdit =
0128             ByteArrayChecksumParameterSetEditFactory::createEdit(parameterSetId);
0129 
0130         mParameterSetEditStack->addWidget(parameterEdit);
0131     }
0132 
0133     onAlgorithmChanged(mTool->algorithmId());
0134 }
0135 
0136 void ChecksumView::getParameterSet(AbstractByteArrayChecksumParameterSet* parameterSet) const
0137 {
0138     auto* parametersetEdit =
0139         qobject_cast<AbstractByteArrayChecksumParameterSetEdit*>(mParameterSetEditStack->currentWidget());
0140     if (parametersetEdit) {
0141         parametersetEdit->getParameterSet(parameterSet);
0142     }
0143 }
0144 
0145 void ChecksumView::onOperationChange(int index)
0146 {
0147     QWidget* oldWidget = mParameterSetEditStack->currentWidget();
0148     if (oldWidget) {
0149         oldWidget->disconnect(this);
0150         oldWidget->disconnect(mTool);
0151     }
0152 
0153     mTool->setAlgorithm(index);
0154     mParameterSetEditStack->setCurrentIndex(index);
0155 
0156     auto* parametersetEdit =
0157         qobject_cast<AbstractByteArrayChecksumParameterSetEdit*>(mParameterSetEditStack->currentWidget());
0158     if (parametersetEdit) {
0159         AbstractByteArrayChecksumParameterSet* parameterSet = mTool->parameterSet();
0160         if (parameterSet) {
0161             parametersetEdit->setParameterSet(parameterSet);
0162         }
0163         connect(parametersetEdit, &AbstractByteArrayChecksumParameterSetEdit::validityChanged,
0164                 this, &ChecksumView::onValidityChanged);
0165         connect(parametersetEdit, &AbstractByteArrayChecksumParameterSetEdit::valuesChanged,
0166                 this, &ChecksumView::onValuesChanged);
0167         onValidityChanged(parametersetEdit->isValid());
0168     }
0169 }
0170 
0171 void ChecksumView::onAlgorithmChanged(int algorithmId)
0172 {
0173     mAlgorithmComboBox->setCurrentIndex(algorithmId);
0174     onOperationChange(algorithmId);
0175 }
0176 
0177 void ChecksumView::onChecksumUptodateChanged(bool checksumUptodate)
0178 {
0179     const bool isApplyable = mTool->isApplyable();
0180     mCalculateAction->setEnabled(!checksumUptodate && isApplyable);
0181 }
0182 
0183 void ChecksumView::onApplyableChanged(bool isApplyable)
0184 {
0185     mCalculateAction->setEnabled(!mTool->isUptodate() && isApplyable);
0186 }
0187 
0188 void ChecksumView::onValuesChanged()
0189 {
0190     AbstractByteArrayChecksumParameterSet* parameterSet = mTool->parameterSet();
0191     if (parameterSet) {
0192         getParameterSet(parameterSet);
0193     }
0194     // TODO: hack, see checksum source
0195     mTool->resetSourceTool();
0196 }
0197 
0198 void ChecksumView::onValidityChanged(bool isValid)
0199 {
0200     mCalculateAction->setEnabled(mTool->isApplyable() && isValid);
0201 }
0202 
0203 }
0204 
0205 #include "moc_checksumview.cpp"