1 /**
2  * \file numbertracksdialog.cpp
3  * Number tracks dialog.
4  *
5  * \b Project: Kid3
6  * \author Urs Fleisch
7  * \date 25 May 2006
8  *
9  * Copyright (C) 2006-2018  Urs Fleisch
10  *
11  * This file is part of Kid3.
12  *
13  * Kid3 is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2 of the License, or
16  * (at your option) any later version.
17  *
18  * Kid3 is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25  */
26 
27 #include "numbertracksdialog.h"
28 #include <QLayout>
29 #include <QPushButton>
30 #include <QLabel>
31 #include <QSpinBox>
32 #include <QString>
33 #include <QComboBox>
34 #include <QCheckBox>
35 #include <QVBoxLayout>
36 #include <QHBoxLayout>
37 #include "numbertracksconfig.h"
38 #include "contexthelp.h"
39 
40 /**
41  * Constructor.
42  *
43  * @param parent parent widget
44  */
NumberTracksDialog(QWidget * parent)45 NumberTracksDialog::NumberTracksDialog(QWidget* parent)
46   : QDialog(parent)
47 {
48   setObjectName(QLatin1String("NumberTracksDialog"));
49   setModal(true);
50   setWindowTitle(tr("Number Tracks"));
51 
52   const NumberTracksConfig& cfg = NumberTracksConfig::instance();
53   auto vlayout = new QVBoxLayout(this);
54   auto trackLayout = new QHBoxLayout;
55   m_numberTracksCheckBox = new QCheckBox(tr("Start &number:"), this);
56   m_numberTracksCheckBox->setChecked(cfg.isTrackNumberingEnabled());
57   m_trackSpinBox = new QSpinBox(this);
58   m_trackSpinBox->setMaximum(9999);
59   m_trackSpinBox->setValue(cfg.numberTracksStart());
60   trackLayout->addWidget(m_numberTracksCheckBox);
61   trackLayout->addWidget(m_trackSpinBox);
62 
63   auto trackSpacer = new QSpacerItem(16, 0, QSizePolicy::Expanding,
64                                              QSizePolicy::Minimum);
65   trackLayout->addItem(trackSpacer);
66 
67   QLabel* destLabel = new QLabel(tr("&Destination:"), this);
68   m_destComboBox = new QComboBox(this);
69   m_destComboBox->setEditable(false);
70   const QList<QPair<Frame::TagVersion, QString> > tagVersions =
71       Frame::availableTagVersions();
72   for (auto it = tagVersions.constBegin(); it != tagVersions.constEnd(); ++it) {
73     m_destComboBox->addItem(it->second, it->first);
74   }
75   m_destComboBox->setCurrentIndex(
76       m_destComboBox->findData(cfg.numberTracksDestination()));
77   trackLayout->addWidget(destLabel);
78   trackLayout->addWidget(m_destComboBox);
79   destLabel->setBuddy(m_destComboBox);
80 
81   vlayout->addLayout(trackLayout);
82 
83   m_resetCounterCheckBox = new QCheckBox(tr("&Reset counter for each folder"),
84                                          this);
85   m_resetCounterCheckBox->setChecked(cfg.isDirectoryCounterResetEnabled());
86   vlayout->addWidget(m_resetCounterCheckBox);
87 
88   auto totalLayout = new QHBoxLayout;
89   m_totalNumTracksCheckBox = new QCheckBox(tr("&Total number of tracks:"),
90                                            this);
91   m_totalNumTrackSpinBox = new QSpinBox(this);
92   if (m_totalNumTracksCheckBox && m_totalNumTrackSpinBox) {
93     m_totalNumTrackSpinBox->setMaximum(999);
94     totalLayout->addWidget(m_totalNumTracksCheckBox);
95     totalLayout->addWidget(m_totalNumTrackSpinBox);
96   }
97   auto totalSpacer = new QSpacerItem(16, 0, QSizePolicy::Expanding,
98                                              QSizePolicy::Minimum);
99   totalLayout->addItem(totalSpacer);
100   vlayout->addLayout(totalLayout);
101 
102   auto hlayout = new QHBoxLayout;
103   QPushButton* helpButton = new QPushButton(tr("&Help"), this);
104   helpButton->setAutoDefault(false);
105   hlayout->addWidget(helpButton);
106   connect(helpButton, &QAbstractButton::clicked, this, &NumberTracksDialog::showHelp);
107 
108   QPushButton* saveButton = new QPushButton(tr("&Save Settings"), this);
109   saveButton->setAutoDefault(false);
110   hlayout->addWidget(saveButton);
111   connect(saveButton, &QAbstractButton::clicked, this, &NumberTracksDialog::saveConfig);
112 
113   auto hspacer = new QSpacerItem(16, 0, QSizePolicy::Expanding,
114                                          QSizePolicy::Minimum);
115   hlayout->addItem(hspacer);
116 
117   QPushButton* okButton = new QPushButton(tr("&OK"), this);
118   okButton->setAutoDefault(false);
119   okButton->setDefault(true);
120   hlayout->addWidget(okButton);
121   connect(okButton, &QAbstractButton::clicked, this, &QDialog::accept);
122 
123   QPushButton* cancelButton = new QPushButton(tr("&Cancel"), this);
124   cancelButton->setAutoDefault(false);
125   hlayout->addWidget(cancelButton);
126   connect(cancelButton, &QAbstractButton::clicked, this, &QDialog::reject);
127 
128   vlayout->addLayout(hlayout);
129 
130   QByteArray geometry = cfg.windowGeometry();
131   if (!geometry.isEmpty()) {
132     restoreGeometry(geometry);
133   }
134 }
135 
136 /**
137  * Get start number.
138  */
getStartNumber() const139 int NumberTracksDialog::getStartNumber() const
140 {
141   return m_trackSpinBox->value();
142 }
143 
144 /**
145  * Get destination.
146  *
147   * @return TagV1, TagV2 or TagV2V1 if ID3v1, ID2v2 or both are destination
148   */
getDestination() const149 Frame::TagVersion NumberTracksDialog::getDestination() const
150 {
151   return Frame::tagVersionCast(
152         m_destComboBox->itemData(m_destComboBox->currentIndex()).toInt());
153 }
154 
155 /**
156  * Save the local settings to the configuration.
157  */
saveConfig()158 void NumberTracksDialog::saveConfig()
159 {
160   NumberTracksConfig& cfg = NumberTracksConfig::instance();
161   cfg.setNumberTracksDestination(getDestination());
162   cfg.setNumberTracksStart(m_trackSpinBox->value());
163   cfg.setTrackNumberingEnabled(isTrackNumberingEnabled());
164   cfg.setDirectoryCounterResetEnabled(isDirectoryCounterResetEnabled());
165   QByteArray geometry = saveGeometry();
166   cfg.setWindowGeometry(geometry);
167 }
168 
169 /**
170  * Show help.
171  */
showHelp()172 void NumberTracksDialog::showHelp()
173 {
174   ContextHelp::displayHelp(QLatin1String("number-tracks"));
175 }
176 
177 /**
178  * Set the total number of tracks.
179  *
180  * @param numTracks number of tracks
181  * @param enable    true to enable setting of total
182  */
setTotalNumberOfTracks(int numTracks,bool enable)183 void NumberTracksDialog::setTotalNumberOfTracks(int numTracks, bool enable)
184 {
185   m_totalNumTrackSpinBox->setValue(numTracks);
186   m_totalNumTracksCheckBox->setChecked(enable);
187 }
188 
189 /**
190  * Get the total number of tracks.
191  *
192  * @param enable true is returned here if total number of tracks is checked
193  *
194  * @return number of tracks entered
195  */
getTotalNumberOfTracks(bool * enable) const196 int NumberTracksDialog::getTotalNumberOfTracks(bool* enable) const
197 {
198   *enable = m_totalNumTracksCheckBox->isChecked();
199   return m_totalNumTrackSpinBox->value();
200 }
201 
202 /**
203  * Check if track numbering is enabled.
204  * @return true if enabled.
205  */
isTrackNumberingEnabled() const206 bool NumberTracksDialog::isTrackNumberingEnabled() const
207 {
208   return m_numberTracksCheckBox->isChecked();
209 }
210 
211 /**
212  * Check if counter has to be reset for each directory.
213  * @return true if enabled.
214  */
isDirectoryCounterResetEnabled() const215 bool NumberTracksDialog::isDirectoryCounterResetEnabled() const
216 {
217   return m_resetCounterCheckBox->isChecked();
218 }
219