| 948 | } |
| 949 | |
| 950 | EltID TreeView::createElement(IdList ids, QStringList &existingNames, Duplicator * duplicator) |
| 951 | { |
| 952 | SoundfontManager * sm = SoundfontManager::getInstance(); |
| 953 | bool isSmpl = (ids[0].typeElement == elementSmpl); |
| 954 | |
| 955 | // Default name |
| 956 | QStringList currentNames; |
| 957 | foreach (EltID id, ids) |
| 958 | currentNames << SoundfontManager::getInstance()->getQstr(id, champ_name); |
| 959 | QString elementName = Utils::commonPart(currentNames); |
| 960 | if (elementName.isEmpty()) |
| 961 | elementName = isSmpl ? tr("instrument") : tr("preset"); |
| 962 | |
| 963 | // Possibly add a suffix |
| 964 | if (existingNames.contains(elementName)) |
| 965 | { |
| 966 | int suffix = 1; |
| 967 | while (existingNames.contains(elementName + "-" + QString::number(suffix))) |
| 968 | suffix++; |
| 969 | elementName += "-" + QString::number(suffix); |
| 970 | } |
| 971 | existingNames << elementName; |
| 972 | |
| 973 | // Choose a free bank / preset for a preset |
| 974 | EltID newElement(isSmpl ? elementInst : elementPrst, ids[0].indexSf2); |
| 975 | int nBank = -1, nPreset = -1; |
| 976 | if (!isSmpl) |
| 977 | { |
| 978 | sm->firstAvailablePresetBank(newElement, nBank, nPreset); |
| 979 | if (nBank < 0 || nPreset < 0) |
| 980 | { |
| 981 | // Cannot create more presets |
| 982 | QMessageBox::warning(this, tr("Warning"), tr("Cannot create more presets.")); |
| 983 | return EltID(elementUnknown); |
| 984 | } |
| 985 | } |
| 986 | |
| 987 | // Create an element |
| 988 | newElement.indexElt = sm->add(newElement); |
| 989 | sm->set(newElement, champ_name, elementName); |
| 990 | |
| 991 | if (isSmpl) |
| 992 | { |
| 993 | bool sampleLooped = true; |
| 994 | bool samePitch = false; |
| 995 | QMap<unsigned char, QPair<bool, bool> > pitches; |
| 996 | |
| 997 | // Link all dragged samples |
| 998 | foreach (EltID id, ids) |
| 999 | { |
| 1000 | // Check the sample is looped |
| 1001 | if (sm->get(id, champ_dwStartLoop).dwValue == sm->get(id, champ_dwEndLoop).dwValue) |
| 1002 | sampleLooped = false; |
| 1003 | |
| 1004 | // Store the pitch |
| 1005 | int bPitch = sm->get(id, champ_byOriginalPitch).bValue; |
| 1006 | SFSampleLink sampleType = sm->get(id, champ_sfSampleType).sfLinkValue; |
| 1007 | if (!pitches.contains(bPitch)) |