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

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 "autodurationsdialog.h"
0009 
0010 #include <QLabel>
0011 #include <QCheckBox>
0012 #include <QGroupBox>
0013 #include <QButtonGroup>
0014 #include <QRadioButton>
0015 #include <QGridLayout>
0016 #include <QSpinBox>
0017 
0018 using namespace SubtitleComposer;
0019 
0020 AutoDurationsDialog::AutoDurationsDialog(unsigned charMillis, unsigned wordMillis, unsigned lineMillis, QWidget *parent)
0021     : ActionWithTargetDialog(i18n("Set Automatic Durations"), parent),
0022     m_translationMode(false)
0023 {
0024     QGroupBox *settingsGroupBox = createGroupBox(i18nc("@title:group", "Settings"));
0025 
0026     m_lineMillisSpinBox = new QSpinBox(settingsGroupBox);
0027     m_lineMillisSpinBox->setMinimum(0);
0028     m_lineMillisSpinBox->setMaximum(1000);
0029     m_lineMillisSpinBox->setValue(lineMillis);
0030 
0031     QLabel *lineMillisLabel = new QLabel(settingsGroupBox);
0032     lineMillisLabel->setText(i18n("Milliseconds per line:"));
0033     lineMillisLabel->setBuddy(m_lineMillisSpinBox);
0034 
0035     m_wordMillisSpinBox = new QSpinBox(settingsGroupBox);
0036     m_wordMillisSpinBox->setMinimum(0);
0037     m_wordMillisSpinBox->setMaximum(1000);
0038     m_wordMillisSpinBox->setValue(wordMillis);
0039 
0040     QLabel *wordMillisLabel = new QLabel(settingsGroupBox);
0041     wordMillisLabel->setText(i18n("Milliseconds per word:"));
0042     wordMillisLabel->setBuddy(m_wordMillisSpinBox);
0043 
0044     m_charMillisSpinBox = new QSpinBox(settingsGroupBox);
0045     m_charMillisSpinBox->setMinimum(0);
0046     m_charMillisSpinBox->setMaximum(1000);
0047     m_charMillisSpinBox->setValue(charMillis);
0048 
0049     QLabel *charMillisLabel = new QLabel(settingsGroupBox);
0050     charMillisLabel->setText(i18n("Milliseconds per character:"));
0051     charMillisLabel->setBuddy(m_charMillisSpinBox);
0052 
0053     m_preventOverlapCheckBox = new QCheckBox(settingsGroupBox);
0054     m_preventOverlapCheckBox->setText(i18n("Prevent overlapping"));
0055     m_preventOverlapCheckBox->setChecked(true);
0056 
0057     QGroupBox *calculationGroupBox = createGroupBox(i18nc("@title:group", "Duration Calculation"));
0058 
0059     m_calculationButtonGroup = new QButtonGroup(this);
0060     for(int index = 0; index < SubtitleTargetSize; ++index)
0061         m_calculationButtonGroup->addButton(new QRadioButton(calculationGroupBox), index);
0062 
0063     m_calculationButtonGroup->button(Primary)->setText(i18n("Use primary text"));
0064     m_calculationButtonGroup->button(Secondary)->setText(i18n("Use translation text"));
0065     m_calculationButtonGroup->button(Both)->setText(i18n("Calculate both and use maximum"));
0066 
0067     createLineTargetsButtonGroup();
0068 
0069     QGridLayout *settingsGroupBoxLayout = createLayout(settingsGroupBox);
0070     settingsGroupBoxLayout->addWidget(lineMillisLabel, 0, 0, Qt::AlignRight | Qt::AlignVCenter);
0071     settingsGroupBoxLayout->addWidget(m_lineMillisSpinBox, 0, 1);
0072     settingsGroupBoxLayout->addWidget(wordMillisLabel, 1, 0, Qt::AlignRight | Qt::AlignVCenter);
0073     settingsGroupBoxLayout->addWidget(m_wordMillisSpinBox, 1, 1);
0074     settingsGroupBoxLayout->addWidget(charMillisLabel, 2, 0, Qt::AlignRight | Qt::AlignVCenter);
0075     settingsGroupBoxLayout->addWidget(m_charMillisSpinBox, 2, 1);
0076     settingsGroupBoxLayout->addWidget(m_preventOverlapCheckBox, 3, 0, 1, 2);
0077 
0078     QGridLayout *calculationLayout = createLayout(calculationGroupBox);
0079     for(int index = 0; index < SubtitleTargetSize; ++index)
0080         calculationLayout->addWidget(m_calculationButtonGroup->button(index), index, 0);
0081 }
0082 
0083 unsigned
0084 AutoDurationsDialog::charMillis() const
0085 {
0086     return m_charMillisSpinBox->value();
0087 }
0088 
0089 unsigned
0090 AutoDurationsDialog::wordMillis() const
0091 {
0092     return m_wordMillisSpinBox->value();
0093 }
0094 
0095 unsigned
0096 AutoDurationsDialog::lineMillis() const
0097 {
0098     return m_lineMillisSpinBox->value();
0099 }
0100 
0101 bool
0102 AutoDurationsDialog::preventOverlap() const
0103 {
0104     return m_preventOverlapCheckBox->isChecked();
0105 }
0106 
0107 bool
0108 AutoDurationsDialog::translationMode() const
0109 {
0110     return m_translationMode;
0111 }
0112 
0113 void
0114 AutoDurationsDialog::setTranslationMode(bool enabled)
0115 {
0116     if(m_translationMode != enabled) {
0117         m_translationMode = enabled;
0118 
0119         if(m_translationMode) {
0120             QAbstractButton *radioButton = m_calculationButtonGroup->button(Both);
0121             radioButton->setEnabled(true);
0122             radioButton->setChecked(true);
0123 
0124             radioButton->parentWidget()->show();
0125         } else {
0126             QAbstractButton *radioButton = m_calculationButtonGroup->button(Primary);
0127 
0128             radioButton->setEnabled(true);
0129             radioButton->setChecked(true);
0130 
0131             radioButton->parentWidget()->hide();
0132         }
0133     }
0134 }
0135 
0136 SubtitleTarget
0137 AutoDurationsDialog::calculationMode() const
0138 {
0139     int checkedId = m_calculationButtonGroup->checkedId();
0140     return checkedId == -1 ? SubtitleTargetSize : SubtitleTarget(checkedId);
0141 }