1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2017 LoRd_MuldeR <MuldeR2@GMX.de>
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version, but always including the *additional*
9 // restrictions defined in the "License.txt" file.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with this program; if not, write to the Free Software Foundation, Inc.,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 // http://www.gnu.org/licenses/gpl-2.0.txt
21 ///////////////////////////////////////////////////////////////////////////////
23 #include "Model_FileExts.h"
27 #include "Registry_Encoder.h"
30 #include <MUtils/Global.h>
31 #include <MUtils/Sound.h>
38 #include <QTextStream>
39 #include <QInputDialog>
41 static inline int LOG10(int x
)
51 static inline QString
EXTENSION(const QString
&string
)
53 QRegExp
regExp("^\\*\\.([A-Za-z0-9]+)$");
54 if(regExp
.indexIn(string
) >= 0)
56 return regExp
.cap(1).trimmed().toLower();
61 static inline bool VALIDATE(const QString
&string
)
63 QRegExp
regExp("^[A-Za-z0-9]+$");
64 return (regExp
.indexIn(string
) >= 0);
67 ////////////////////////////////////////////////////////////
68 // Constructor & Destructor
69 ////////////////////////////////////////////////////////////
71 FileExtsModel::FileExtsModel(QObject
*const parent
)
73 QAbstractItemModel(parent
),
74 m_label_1(":/icons/tag_blue.png"),
75 m_label_2(":/icons/tag_red.png")
77 //m_fileExts.append("mp4");
78 //m_replace.insert(m_fileExts.first(), "m4a");
81 FileExtsModel::~FileExtsModel(void)
85 ////////////////////////////////////////////////////////////
87 ////////////////////////////////////////////////////////////
89 int FileExtsModel::columnCount(const QModelIndex
&parent
) const
94 int FileExtsModel::rowCount(const QModelIndex
&parent
) const
96 return m_fileExt
.count();
99 QVariant
FileExtsModel::data(const QModelIndex
&index
, int role
) const
101 if(((role
== Qt::DisplayRole
) || (role
== Qt::ToolTipRole
)) && (index
.row() < m_fileExt
.count()) && (index
.row() >= 0))
103 switch(index
.column())
106 return QString("*.%0").arg(m_fileExt
.at(index
.row()));
108 return QString("*.%0").arg(m_replace
.value(m_fileExt
.at(index
.row())));
113 else if((role
== Qt::DecorationRole
))
115 switch(index
.column())
131 QVariant
FileExtsModel::headerData(int section
, Qt::Orientation orientation
, int role
) const
133 if(role
== Qt::DisplayRole
)
135 if(orientation
== Qt::Horizontal
)
140 return QVariant(tr("File Extension"));
142 return QVariant(tr("Replace With"));
149 return int2str(section
+ 1);
158 QModelIndex
FileExtsModel::index(int row
, int column
, const QModelIndex
& parent
) const
160 return createIndex(row
, column
, qHash((qint64(row
) << 32)| qint64(column
)));
163 QModelIndex
FileExtsModel::parent(const QModelIndex
& index
) const
165 return QModelIndex();
168 ////////////////////////////////////////////////////////////
170 ////////////////////////////////////////////////////////////
172 bool FileExtsModel::addOverwrite(QWidget
*const parent
)
174 const QStringList allExts
= EncoderRegistry::getOutputFileExtensions();
175 QStringList extensions
;
176 for(QStringList::ConstIterator iter
= allExts
.constBegin(); iter
!= allExts
.constEnd(); iter
++)
178 if(!m_fileExt
.contains((*iter
), Qt::CaseInsensitive
))
180 extensions
<< QString("*.%0").arg(*iter
);
183 if(extensions
.isEmpty())
188 QInputDialog
dialog(parent
);
189 dialog
.setLabelText(tr("Select file extensions to overwrite:"));
190 dialog
.setInputMode(QInputDialog::TextInput
);
191 dialog
.setTextEchoMode(QLineEdit::Normal
);
192 dialog
.setComboBoxEditable(false);
193 dialog
.setComboBoxItems(extensions
);
195 if(dialog
.exec() == 0)
200 const QString selectedExt
= EXTENSION(dialog
.textValue());
201 if(selectedExt
.isEmpty())
206 dialog
.setComboBoxEditable(true);
207 dialog
.setComboBoxItems(QStringList());
208 dialog
.setLabelText(tr("Enter the new file extension:"));
211 while(replacement
.isEmpty())
213 dialog
.setTextValue(QString("*.%0").arg(selectedExt
));
214 if(dialog
.exec() == 0)
218 replacement
= EXTENSION(dialog
.textValue());
219 if(!replacement
.compare(selectedExt
, Qt::CaseInsensitive
))
223 if(replacement
.isEmpty())
225 MUtils::Sound::beep(MUtils::Sound::BEEP_ERR
);
230 m_fileExt
.append(selectedExt
);
232 m_replace
.insert(selectedExt
, replacement
);
237 bool FileExtsModel::removeOverwrite(const QModelIndex
&index
)
239 if((index
.row() < m_fileExt
.count()) && (index
.row() >= 0))
242 m_replace
.remove(m_fileExt
.at(index
.row()));
243 m_fileExt
.removeAt(index
.row());
250 ////////////////////////////////////////////////////////////
252 ////////////////////////////////////////////////////////////
254 QString
FileExtsModel::exportItems(void) const
257 for(QStringList::ConstIterator iter
= m_fileExt
.constBegin(); iter
!= m_fileExt
.constEnd(); iter
++)
259 if(m_replace
.contains(*iter
))
261 if(!exported
.isEmpty()) exported
.append('|');
262 exported
.append(QString("%0>%1").arg(iter
->trimmed(), m_replace
.value(*iter
).trimmed()));
268 void FileExtsModel::importItems(const QString
&data
)
274 const QStringList list
= data
.split('|', QString::SkipEmptyParts
);
275 for(QStringList::ConstIterator iter
= list
.constBegin(); iter
!= list
.constEnd(); iter
++)
277 const QStringList item
= iter
->trimmed().split('>');
278 if(item
.count() >= 2)
280 const QString fileExt
= item
.at(0).simplified().toLower();
281 const QString replace
= item
.at(1).simplified().toLower();
282 if(VALIDATE(fileExt
) && VALIDATE(replace
) && (!m_fileExt
.contains(fileExt
)))
284 m_fileExt
.append(fileExt
);
285 m_replace
.insert(fileExt
, replace
);
294 ////////////////////////////////////////////////////////////
296 ////////////////////////////////////////////////////////////
298 QString
FileExtsModel::apply(const QString
&originalExtension
) const
300 if((!m_replace
.isEmpty()) && m_replace
.contains(originalExtension
.toLower()))
302 return m_replace
.value(originalExtension
);
304 return originalExtension
;
308 ////////////////////////////////////////////////////////////
310 ////////////////////////////////////////////////////////////
312 QString
FileExtsModel::int2str(const int &value
) const
314 if(m_fileExt
.count() < 10)
316 return QString().sprintf("%d", value
);
320 const QString format
= QString().sprintf("%%0%dd", LOG10(m_fileExt
.count()));
321 return QString().sprintf(format
.toLatin1().constData(), value
);