![]() |
Mixxx
|
00001 #include <QFileDialog> 00002 #include <QMessageBox> 00003 00004 #include "sampler.h" 00005 #include "samplerbank.h" 00006 #include "trackinfoobject.h" 00007 #include "controlpushbutton.h" 00008 #include "playermanager.h" 00009 #include "playerinfo.h" 00010 00011 SamplerBank::SamplerBank(PlayerManager* pPlayerManager) 00012 : QObject(pPlayerManager), 00013 m_pPlayerManager(pPlayerManager) { 00014 Q_ASSERT(pPlayerManager); 00015 m_pLoadControl = new ControlPushButton(ConfigKey("[Sampler]", "LoadSamplerBank")); 00016 connect(m_pLoadControl, SIGNAL(valueChanged(double)), this, SLOT(slotLoadSamplerBank(double))); 00017 m_pSaveControl = new ControlPushButton(ConfigKey("[Sampler]", "SaveSamplerBank")); 00018 connect(m_pSaveControl, SIGNAL(valueChanged(double)), this, SLOT(slotSaveSamplerBank(double))); 00019 } 00020 00021 SamplerBank::~SamplerBank() { 00022 delete m_pLoadControl; 00023 delete m_pSaveControl; 00024 } 00025 00026 void SamplerBank::slotSaveSamplerBank(double v) { 00027 if (v == 0.0f) 00028 return; 00029 00030 QString s = QFileDialog::getSaveFileName(NULL, tr("Save Sampler Bank")); 00031 00032 QFile file(s); 00033 if (!file.open(QIODevice::WriteOnly)) { 00034 QMessageBox::warning(NULL, 00035 tr("Error Saving Sampler Bank"), 00036 tr("Could not write the sampler bank to '%1'.").arg(s)); 00037 return; 00038 } 00039 00040 QDomDocument doc("SamplerBank"); 00041 00042 QDomElement root = doc.createElement("samplerbank"); 00043 doc.appendChild(root); 00044 00045 for (unsigned int i = 0; i < m_pPlayerManager->numSamplers(); ++i) { 00046 Sampler* pSampler = m_pPlayerManager->getSampler(i); 00047 QDomElement samplerNode = doc.createElement(QString("sampler")); 00048 00049 samplerNode.setAttribute("group", pSampler->getGroup()); 00050 00051 TrackPointer pTrack = PlayerInfo::Instance().getTrackInfo(pSampler->getGroup()); 00052 if (pTrack) { 00053 QString samplerLocation = pTrack->getLocation(); 00054 samplerNode.setAttribute("location", samplerLocation); 00055 } 00056 root.appendChild(samplerNode); 00057 } 00058 00059 QString docStr = doc.toString(); 00060 00061 file.write(docStr.toUtf8().constData()); 00062 file.close(); 00063 } 00064 00065 void SamplerBank::slotLoadSamplerBank(double v) { 00066 if (v == 0.0f) 00067 return; 00068 00069 QString s = QFileDialog::getOpenFileName(NULL, tr("Load Sampler Bank")); 00070 00071 QFile file(s); 00072 if (!file.open(QIODevice::WriteOnly)) { 00073 QMessageBox::warning(NULL, 00074 tr("Error Reading Sampler Bank"), 00075 tr("Could not open the sampler bank file '%1'.").arg(s)); 00076 return; 00077 } 00078 00079 QDomDocument doc; 00080 00081 if (!doc.setContent(file.readAll())) { 00082 QMessageBox::warning(NULL, 00083 tr("Error Reading Sampler Bank"), 00084 tr("Could not read the sampler bank file '%1'.").arg(s)); 00085 return; 00086 } 00087 00088 QDomElement root = doc.documentElement(); 00089 if(root.tagName() != "samplerbank") { 00090 QMessageBox::warning(NULL, 00091 tr("Error Reading Sampler Bank"), 00092 tr("Could not read the sampler bank file '%1'.").arg(s)); 00093 return; 00094 } 00095 00096 QDomNode n = root.firstChild(); 00097 00098 while (!n.isNull()) { 00099 QDomElement e = n.toElement(); 00100 00101 if (!e.isNull()) { 00102 if (e.tagName() == "sampler") { 00103 QString group = e.attribute("group", ""); 00104 QString location = e.attribute("location", ""); 00105 m_pPlayerManager->slotLoadToPlayer(location, group); 00106 } 00107 } 00108 n = n.nextSibling(); 00109 } 00110 00111 file.close(); 00112 }