File indexing completed on 2024-12-22 04:40:08

0001 /*
0002     SPDX-FileCopyrightText: 2007-2009 Sergio Pistone <sergio_pistone@yahoo.com.ar>
0003     SPDX-FileCopyrightText: 2010-2022 Mladen Milinkovic <max@smoothware.net>
0004 
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 #include "finderrorsdialog.h"
0009 
0010 #include <QCheckBox>
0011 #include <QGridLayout>
0012 #include <QGroupBox>
0013 
0014 using namespace SubtitleComposer;
0015 
0016 FindErrorsDialog::FindErrorsDialog(QWidget *parent)
0017     : ActionWithTargetDialog(i18n("Find Error"), parent),
0018       m_errorsGroupBox(nullptr),
0019       m_errorsCheckBox(nullptr),
0020       m_errorsLayout(nullptr)
0021 {
0022     createErrorsGroupBox(i18nc("@title:group", "Errors to Find"));
0023     createErrorsButtons(true, translationMode());
0024 
0025 
0026 
0027 
0028     QGroupBox *miscGroupBox = createGroupBox(i18nc("@title:group Miscellaneous settings", "Miscellaneous"));
0029 
0030     m_clearOtherErrorsCheckBox = new QCheckBox(miscGroupBox);
0031     m_clearOtherErrorsCheckBox->setText(i18n("Clear other errors"));
0032     m_clearOtherErrorsCheckBox->setChecked(true);
0033 
0034     m_clearMarksCheckBox = new QCheckBox(miscGroupBox);
0035     m_clearMarksCheckBox->setText(i18n("Clear user marks"));
0036     m_clearMarksCheckBox->setChecked(false);
0037 
0038     QGridLayout *miscLayout = createLayout(miscGroupBox);
0039     miscLayout->addWidget(m_clearOtherErrorsCheckBox, 0, 0);
0040     miscLayout->addWidget(m_clearMarksCheckBox, 1, 0);
0041 
0042     createTargetsGroupBox(i18n("Search in"));
0043     createLineTargetsButtonGroup();
0044     createTextTargetsButtonGroup();
0045 }
0046 
0047 FindErrorsDialog::~FindErrorsDialog()
0048 {
0049     delete[] m_errorsCheckBox;
0050 }
0051 
0052 void
0053 FindErrorsDialog::setTranslationMode(bool value)
0054 {
0055     ActionWithTargetDialog::setTranslationMode(value);
0056     createErrorsButtons(true, value);
0057 }
0058 QGroupBox *
0059 FindErrorsDialog::createErrorsGroupBox(const QString &title)
0060 {
0061     m_errorsGroupBox = createGroupBox(title);
0062     m_errorsLayout = createLayout(m_errorsGroupBox);
0063     return m_errorsGroupBox;
0064 }
0065 
0066 void
0067 FindErrorsDialog::createErrorsButtons(bool showUserMarks, bool showMissingTranslation)
0068 {
0069     if(m_errorsCheckBox) {
0070         // no need to recreate everything if the configuration to show has not changed
0071         if((m_errorsCheckBox[SubtitleLine::UserMarkID] != nullptr) == showUserMarks
0072         && (m_errorsCheckBox[SubtitleLine::UntranslatedTextID] != nullptr) == showMissingTranslation)
0073             return;
0074     } else {
0075         m_errorsCheckBox = new QCheckBox *[SubtitleLine::ErrorSIZE];
0076     }
0077 
0078     if(m_errorsGroupBox) {
0079         for(QLayoutItem *child = m_errorsLayout->takeAt(0); child != 0; child = m_errorsLayout->takeAt(0))
0080             delete child;
0081 
0082         QList<QWidget *> children = m_errorsGroupBox->findChildren<QWidget *>();
0083         for(QList<QWidget *>::ConstIterator it = children.constBegin(), end = children.constEnd(); it != end; ++it)
0084             delete *it;
0085     } else {
0086         createErrorsGroupBox(i18n("Available errors"));
0087     }
0088 
0089     int excludedErrorFlags = SubtitleLine::SecondaryOnlyErrors;
0090 
0091     if(showUserMarks)
0092         excludedErrorFlags &= ~SubtitleLine::UserMark;
0093     else
0094         excludedErrorFlags |= SubtitleLine::UserMark;
0095 
0096     if(showMissingTranslation)
0097         excludedErrorFlags &= ~SubtitleLine::UntranslatedText;
0098     else
0099         excludedErrorFlags |= SubtitleLine::UntranslatedText;
0100 
0101     int errorCount = 0;
0102     for(int errorId = 0; errorId < SubtitleLine::ErrorSIZE; ++errorId) {
0103         if((0x1 << errorId) & excludedErrorFlags)
0104             m_errorsCheckBox[errorId] = 0;
0105         else {
0106             m_errorsCheckBox[errorId] = new QCheckBox(m_errorsGroupBox);
0107             m_errorsCheckBox[errorId]->setText(SubtitleLine::simpleErrorText((SubtitleLine::ErrorID)errorId));
0108             m_errorsCheckBox[errorId]->setChecked(true);
0109             errorCount++;
0110         }
0111     }
0112 
0113     QPushButton *selectAllButton = new QPushButton(m_errorsGroupBox);
0114     selectAllButton->setText(i18n("Select All"));
0115     QPushButton *selectNoneButton = new QPushButton(m_errorsGroupBox);
0116     selectNoneButton->setText(i18n("Select None"));
0117 
0118     connect(selectAllButton, &QAbstractButton::clicked, this, &FindErrorsDialog::selectAllErrorFlags);
0119     connect(selectNoneButton, &QAbstractButton::clicked, this, &FindErrorsDialog::deselectAllErrorFlags);
0120 
0121     int row = 0, col = 0;
0122     for(int errorId = 0; errorId < SubtitleLine::ErrorSIZE; ++errorId) {
0123         if(m_errorsCheckBox[errorId]) {
0124             m_errorsLayout->addWidget(m_errorsCheckBox[errorId], row++, col);
0125             if(row > (errorCount - 1) / 2) {
0126                 row = 0;
0127                 col = 1;
0128             }
0129         }
0130     }
0131 
0132     QHBoxLayout *buttonsLayout = new QHBoxLayout();
0133     buttonsLayout->addStretch();
0134     buttonsLayout->addWidget(selectAllButton);
0135     buttonsLayout->addWidget(selectNoneButton);
0136 
0137     m_errorsLayout->addLayout(buttonsLayout, errorCount / 2 + 1, 0, 1, 2);
0138 }
0139 
0140 
0141 void
0142 FindErrorsDialog::selectAllErrorFlags()
0143 {
0144     for(int errorId = 0; errorId < SubtitleLine::ErrorSIZE; ++errorId)
0145         if(m_errorsCheckBox[errorId])
0146             m_errorsCheckBox[errorId]->setChecked(true);
0147 }
0148 
0149 void
0150 FindErrorsDialog::deselectAllErrorFlags()
0151 {
0152     for(int errorId = 0; errorId < SubtitleLine::ErrorSIZE; ++errorId)
0153         if(m_errorsCheckBox[errorId])
0154             m_errorsCheckBox[errorId]->setChecked(false);
0155 }
0156 
0157 int
0158 FindErrorsDialog::selectedErrorFlags() const
0159 {
0160     int errorFlags = 0;
0161     for(int errorId = 0; errorId < SubtitleLine::ErrorSIZE; ++errorId)
0162         if(m_errorsCheckBox[errorId] && m_errorsCheckBox[errorId]->isChecked())
0163             errorFlags |= SubtitleLine::errorFlag((SubtitleLine::ErrorID)errorId);
0164 
0165     switch(selectedTextsTarget()) {
0166     case Primary:
0167         return errorFlags;
0168     case Secondary: {
0169         const int secondaryErrorFlags = (errorFlags &SubtitleLine::PrimaryOnlyErrors) << 1;
0170         errorFlags = errorFlags & ~SubtitleLine::PrimaryOnlyErrors;
0171         return errorFlags | secondaryErrorFlags;
0172     }
0173     case Both:
0174     default: {
0175         const int secondaryErrorFlags = (errorFlags &SubtitleLine::PrimaryOnlyErrors) << 1;
0176         return errorFlags | secondaryErrorFlags;
0177     }
0178     }
0179 }