1 |
/* |
/* |
2 |
Copyright (c) 2014 Christian Schoenebeck |
Copyright (c) 2014-2017 Christian Schoenebeck |
3 |
|
|
4 |
This file is part of "gigedit" and released under the terms of the |
This file is part of "gigedit" and released under the terms of the |
5 |
GNU General Public License version 2. |
GNU General Public License version 2. |
6 |
*/ |
*/ |
7 |
|
|
8 |
|
#include "global.h" |
9 |
#include "CombineInstrumentsDialog.h" |
#include "CombineInstrumentsDialog.h" |
10 |
|
|
11 |
// enable this for debug messages being printed while combining the instruments |
// enable this for debug messages being printed while combining the instruments |
12 |
#define DEBUG_COMBINE_INSTRUMENTS 0 |
#define DEBUG_COMBINE_INSTRUMENTS 0 |
13 |
|
|
14 |
#include "global.h" |
#include "compat.h" |
15 |
|
|
16 |
#include <set> |
#include <set> |
17 |
#include <iostream> |
#include <iostream> |
18 |
#include <assert.h> |
#include <assert.h> |
19 |
|
#include <stdarg.h> |
20 |
|
#include <string.h> |
21 |
|
|
22 |
#include <glibmm/ustring.h> |
#include <glibmm/ustring.h> |
23 |
#include <gtkmm/stock.h> |
#include <gtkmm/stock.h> |
24 |
#include <gtkmm/messagedialog.h> |
#include <gtkmm/messagedialog.h> |
25 |
|
#include <gtkmm/label.h> |
26 |
|
#include <gtk/gtkwidget.h> // for gtk_widget_modify_*() |
27 |
|
|
28 |
Glib::ustring gig_to_utf8(const gig::String& gig_string); |
Glib::ustring dimTypeAsString(gig::dimension_t d); |
29 |
|
|
30 |
|
typedef std::vector< std::pair<gig::Instrument*, gig::Region*> > OrderedRegionGroup; |
31 |
typedef std::map<gig::Instrument*, gig::Region*> RegionGroup; |
typedef std::map<gig::Instrument*, gig::Region*> RegionGroup; |
32 |
typedef std::map<DLS::range_t,RegionGroup> RegionGroups; |
typedef std::map<DLS::range_t,RegionGroup> RegionGroups; |
33 |
|
|
34 |
typedef std::vector<DLS::range_t> DimensionZones; |
typedef std::vector<DLS::range_t> DimensionZones; |
35 |
typedef std::map<gig::dimension_t,DimensionZones> Dimensions; |
typedef std::map<gig::dimension_t,DimensionZones> Dimensions; |
36 |
|
|
|
typedef std::map<gig::dimension_t,int> DimensionCase; |
|
|
|
|
37 |
typedef std::map<gig::dimension_t, int> DimensionRegionUpperLimits; |
typedef std::map<gig::dimension_t, int> DimensionRegionUpperLimits; |
38 |
|
|
39 |
|
typedef std::set<Glib::ustring> Warnings; |
40 |
|
|
41 |
|
/////////////////////////////////////////////////////////////////////////// |
42 |
|
// private static data |
43 |
|
|
44 |
|
static Warnings g_warnings; |
45 |
|
|
46 |
/////////////////////////////////////////////////////////////////////////// |
/////////////////////////////////////////////////////////////////////////// |
47 |
// private functions |
// private functions |
48 |
|
|
58 |
#endif |
#endif |
59 |
|
|
60 |
/** |
/** |
61 |
|
* Store a warning message that shall be stored and displayed to the user as a |
62 |
|
* list of warnings after the overall operation has finished. Duplicate warning |
63 |
|
* messages are automatically eliminated. |
64 |
|
*/ |
65 |
|
inline void addWarning(const char* fmt, ...) { |
66 |
|
va_list arg; |
67 |
|
va_start(arg, fmt); |
68 |
|
const int SZ = 255 + strlen(fmt); |
69 |
|
char* buf = new char[SZ]; |
70 |
|
vsnprintf(buf, SZ, fmt, arg); |
71 |
|
Glib::ustring s = buf; |
72 |
|
delete [] buf; |
73 |
|
va_end(arg); |
74 |
|
std::cerr << _("WARNING:") << " " << s << std::endl << std::flush; |
75 |
|
g_warnings.insert(s); |
76 |
|
} |
77 |
|
|
78 |
|
/** |
79 |
* If the two ranges overlap, then this function returns the smallest point |
* If the two ranges overlap, then this function returns the smallest point |
80 |
* within that overlapping zone. If the two ranges do not overlap, then this |
* within that overlapping zone. If the two ranges do not overlap, then this |
81 |
* function will return -1 instead. |
* function will return -1 instead. |
95 |
* found with a range member point >= iStart |
* found with a range member point >= iStart |
96 |
*/ |
*/ |
97 |
static int findLowestRegionPoint(std::vector<gig::Instrument*>& instruments, int iStart) { |
static int findLowestRegionPoint(std::vector<gig::Instrument*>& instruments, int iStart) { |
98 |
DLS::range_t searchRange = { iStart, 127 }; |
DLS::range_t searchRange = { uint16_t(iStart), 127 }; |
99 |
int result = -1; |
int result = -1; |
100 |
for (uint i = 0; i < instruments.size(); ++i) { |
for (uint i = 0; i < instruments.size(); ++i) { |
101 |
gig::Instrument* instr = instruments[i]; |
gig::Instrument* instr = instruments[i]; |
117 |
* with a range end >= iStart |
* with a range end >= iStart |
118 |
*/ |
*/ |
119 |
static int findFirstRegionEnd(std::vector<gig::Instrument*>& instruments, int iStart) { |
static int findFirstRegionEnd(std::vector<gig::Instrument*>& instruments, int iStart) { |
120 |
DLS::range_t searchRange = { iStart, 127 }; |
DLS::range_t searchRange = { uint16_t(iStart), 127 }; |
121 |
int result = -1; |
int result = -1; |
122 |
for (uint i = 0; i < instruments.size(); ++i) { |
for (uint i = 0; i < instruments.size(); ++i) { |
123 |
gig::Instrument* instr = instruments[i]; |
gig::Instrument* instr = instruments[i]; |
160 |
std::vector<gig::Region*> v = getAllRegionsWhichOverlapRange(instr, range); |
std::vector<gig::Region*> v = getAllRegionsWhichOverlapRange(instr, range); |
161 |
if (v.empty()) continue; |
if (v.empty()) continue; |
162 |
if (v.size() > 1) { |
if (v.size() > 1) { |
163 |
std::cerr << "WARNING: More than one region found!" << std::endl; |
addWarning("More than one region found!"); |
164 |
} |
} |
165 |
group[instr] = v[0]; |
group[instr] = v[0]; |
166 |
} |
} |
170 |
/** @brief Identify required regions. |
/** @brief Identify required regions. |
171 |
* |
* |
172 |
* Takes a list of @a instruments as argument (which are planned to be combined |
* Takes a list of @a instruments as argument (which are planned to be combined |
173 |
* as layers in one single new instrument) and fulfills the following tasks: |
* as separate dimension zones of a certain dimension into one single new |
174 |
|
* instrument) and fulfills the following tasks: |
175 |
* |
* |
176 |
* - 1. Identification of total amount of regions required to create a new |
* - 1. Identification of total amount of regions required to create a new |
177 |
* instrument to become a layered version of the given instruments. |
* instrument to become a combined version of the given instruments. |
178 |
* - 2. Precise key range of each of those identified required regions to be |
* - 2. Precise key range of each of those identified required regions to be |
179 |
* created in that new instrument. |
* created in that new instrument. |
180 |
* - 3. Grouping the original source regions of the given original instruments |
* - 3. Grouping the original source regions of the given original instruments |
193 |
iStart = findLowestRegionPoint(instruments, iStart); |
iStart = findLowestRegionPoint(instruments, iStart); |
194 |
if (iStart < 0) break; |
if (iStart < 0) break; |
195 |
const int iEnd = findFirstRegionEnd(instruments, iStart); |
const int iEnd = findFirstRegionEnd(instruments, iStart); |
196 |
DLS::range_t range = { iStart, iEnd }; |
DLS::range_t range = { uint16_t(iStart), uint16_t(iEnd) }; |
197 |
intersections.push_back(range); |
intersections.push_back(range); |
198 |
iStart = iEnd + 1; |
iStart = iEnd + 1; |
199 |
} |
} |
205 |
if (!group.empty()) |
if (!group.empty()) |
206 |
groups[range] = group; |
groups[range] = group; |
207 |
else |
else |
208 |
std::cerr << "WARNING: empty region group!" << std::endl; |
addWarning("Empty region group!"); |
209 |
} |
} |
210 |
|
|
211 |
return groups; |
return groups; |
264 |
itNums != it->second.end(); ++itNums) |
itNums != it->second.end(); ++itNums) |
265 |
{ |
{ |
266 |
const int iUpperLimit = *itNums; |
const int iUpperLimit = *itNums; |
267 |
DLS::range_t range = { iLow, iUpperLimit }; |
DLS::range_t range = { uint16_t(iLow), uint16_t(iUpperLimit) }; |
268 |
dims[type].push_back(range); |
dims[type].push_back(range); |
269 |
iLow = iUpperLimit + 1; |
iLow = iUpperLimit + 1; |
270 |
} |
} |
273 |
return dims; |
return dims; |
274 |
} |
} |
275 |
|
|
|
inline int getDimensionIndex(gig::dimension_t type, gig::Region* rgn) { |
|
|
for (uint i = 0; i < rgn->Dimensions; ++i) |
|
|
if (rgn->pDimensionDefinitions[i].dimension == type) |
|
|
return i; |
|
|
return -1; |
|
|
} |
|
|
|
|
276 |
static void fillDimValues(uint* values/*[8]*/, DimensionCase dimCase, gig::Region* rgn, bool bShouldHaveAllDimensionsPassed) { |
static void fillDimValues(uint* values/*[8]*/, DimensionCase dimCase, gig::Region* rgn, bool bShouldHaveAllDimensionsPassed) { |
277 |
#if DEBUG_COMBINE_INSTRUMENTS |
#if DEBUG_COMBINE_INSTRUMENTS |
278 |
printf("dimvalues = { "); |
printf("dimvalues = { "); |
289 |
#endif |
#endif |
290 |
} |
} |
291 |
#if DEBUG_COMBINE_INSTRUMENTS |
#if DEBUG_COMBINE_INSTRUMENTS |
292 |
printf("\n"); |
printf("}\n"); |
293 |
#endif |
#endif |
294 |
} |
} |
295 |
|
|
314 |
} |
} |
315 |
} |
} |
316 |
|
|
|
/** |
|
|
* Returns the sum of all bits of all dimensions defined before the given |
|
|
* dimensions (@a type). This allows to access cases of that particular |
|
|
* dimension directly. |
|
|
* |
|
|
* @param type - dimension that shall be used |
|
|
* @param rgn - parent region of that dimension |
|
|
*/ |
|
|
inline int baseBits(gig::dimension_t type, gig::Region* rgn) { |
|
|
int previousBits = 0; |
|
|
for (uint i = 0; i < rgn->Dimensions; ++i) { |
|
|
if (rgn->pDimensionDefinitions[i].dimension == type) break; |
|
|
previousBits += rgn->pDimensionDefinitions[i].bits; |
|
|
} |
|
|
return previousBits; |
|
|
} |
|
|
|
|
317 |
inline int dimensionRegionIndex(gig::DimensionRegion* dimRgn) { |
inline int dimensionRegionIndex(gig::DimensionRegion* dimRgn) { |
318 |
gig::Region* rgn = dimRgn->GetParent(); |
gig::Region* rgn = dimRgn->GetParent(); |
319 |
int sz = sizeof(rgn->pDimensionRegions) / sizeof(gig::DimensionRegion*); |
int sz = sizeof(rgn->pDimensionRegions) / sizeof(gig::DimensionRegion*); |
346 |
const gig::dimension_def_t& def = rgn->pDimensionDefinitions[iDimension]; |
const gig::dimension_def_t& def = rgn->pDimensionDefinitions[iDimension]; |
347 |
int iDimRgn = dimensionRegionIndex(dimRgn); |
int iDimRgn = dimensionRegionIndex(dimRgn); |
348 |
int iBaseBits = baseBits(type, rgn); |
int iBaseBits = baseBits(type, rgn); |
349 |
|
assert(iBaseBits >= 0); |
350 |
int mask = ~(((1 << def.bits) - 1) << iBaseBits); |
int mask = ~(((1 << def.bits) - 1) << iBaseBits); |
351 |
|
|
352 |
#if DEBUG_COMBINE_INSTRUMENTS |
#if DEBUG_COMBINE_INSTRUMENTS |
358 |
gig::DimensionRegion* dimRgn2 = |
gig::DimensionRegion* dimRgn2 = |
359 |
rgn->pDimensionRegions[ (iDimRgn & mask) | ( z << iBaseBits) ]; |
rgn->pDimensionRegions[ (iDimRgn & mask) | ( z << iBaseBits) ]; |
360 |
int iHigh = dimRgn2->DimensionUpperLimits[iDimension]; |
int iHigh = dimRgn2->DimensionUpperLimits[iDimension]; |
361 |
DLS::range_t range = { iLow, iHigh}; |
DLS::range_t range = { uint16_t(iLow), uint16_t(iHigh) }; |
362 |
#if DEBUG_COMBINE_INSTRUMENTS |
#if DEBUG_COMBINE_INSTRUMENTS |
363 |
printf("%d..%d, ", iLow, iHigh); |
printf("%d..%d, ", iLow, iHigh); |
364 |
fflush(stdout); |
fflush(stdout); |
380 |
}; |
}; |
381 |
typedef std::vector<CopyAssignSchedEntry> CopyAssignSchedule; |
typedef std::vector<CopyAssignSchedEntry> CopyAssignSchedule; |
382 |
|
|
383 |
/** @brief Copy all DimensionRegions from source Region to target Region. |
/** @brief Schedule copying DimensionRegions from source Region to target Region. |
384 |
* |
* |
385 |
* Copies the entire articulation informations (including sample reference of |
* Schedules copying the entire articulation informations (including sample |
386 |
* course) from all individual DimensionRegions of source Region @a inRgn to |
* reference) from all individual DimensionRegions of source Region @a inRgn to |
387 |
* target Region @a outRgn. There are no dimension regions created during this |
* target Region @a outRgn. It is expected that the required dimensions (thus |
388 |
* task. It is expected that the required dimensions (thus the required |
* the required dimension regions) were already created before calling this |
389 |
* dimension regions) were already created before calling this function. |
* function. |
390 |
* |
* |
391 |
* To be precise, it does the task above only for the layer selected by |
* To be precise, it does the task above only for the dimension zones defined by |
392 |
* @a iSrcLayer and @a iDstLayer. All dimensions regions of other layers that |
* the three arguments @a mainDim, @a iSrcMainBit, @a iDstMainBit, which reflect |
393 |
* may exist, will not be copied by one single call of this function. So if |
* a selection which dimension zones shall be copied. All other dimension zones |
394 |
* there is a layer dimension, this function needs to be called several times. |
* will not be scheduled to be copied by a single call of this function. So this |
395 |
|
* function needs to be called several time in case all dimension regions shall |
396 |
|
* be copied of the entire region (@a inRgn, @a outRgn). |
397 |
* |
* |
398 |
* @param outRgn - where the dimension regions shall be copied to |
* @param outRgn - where the dimension regions shall be copied to |
399 |
* @param inRgn - all dimension regions that shall be copied from |
* @param inRgn - all dimension regions that shall be copied from |
400 |
* @param dims - precise dimension definitions of target region |
* @param dims - precise dimension definitions of target region |
401 |
* @param iDstLayer - layer index of destination region where the dimension |
* @param mainDim - this dimension type, in combination with @a iSrcMainBit and |
402 |
* regions shall be copied to |
* @a iDstMainBit defines a selection which dimension region |
403 |
* @param iSrcLayer - layer index of the source region where the dimension |
* zones shall be copied by this call of this function |
404 |
* regions shall be copied from |
* @param iDstMainBit - destination bit of @a mainDim |
405 |
|
* @param iSrcMainBit - source bit of @a mainDim |
406 |
|
* @param schedule - list of all DimensionRegion copy operations which is filled |
407 |
|
* during the nested loops / recursions of this function call |
408 |
* @param dimCase - just for internal purpose (function recursion), don't pass |
* @param dimCase - just for internal purpose (function recursion), don't pass |
409 |
* anything here, this function will call itself recursively |
* anything here, this function will call itself recursively |
410 |
* will fill this container with concrete dimension values for |
* will fill this container with concrete dimension values for |
411 |
* selecting the precise dimension regions during its task |
* selecting the precise dimension regions during its task |
|
* @param schedule - just for internal purpose (function recursion), don't pass |
|
|
anything here: list of all DimensionRegion copy operations |
|
|
* which is filled during the nested loops / recursions of |
|
|
* this function call, they will be peformed after all |
|
|
* function recursions have been completed |
|
412 |
*/ |
*/ |
413 |
static void copyDimensionRegions(gig::Region* outRgn, gig::Region* inRgn, Dimensions dims, int iDstLayer, int iSrcLayer, DimensionCase dimCase = DimensionCase(), CopyAssignSchedule* schedule = NULL) { |
static void scheduleCopyDimensionRegions(gig::Region* outRgn, gig::Region* inRgn, |
414 |
const bool isHighestLevelOfRecursion = !schedule; |
Dimensions dims, gig::dimension_t mainDim, |
415 |
|
int iDstMainBit, int iSrcMainBit, |
416 |
if (isHighestLevelOfRecursion) |
CopyAssignSchedule* schedule, |
417 |
schedule = new CopyAssignSchedule; |
DimensionCase dimCase = DimensionCase()) |
418 |
|
{ |
419 |
if (dims.empty()) { // reached deepest level of function recursion ... |
if (dims.empty()) { // reached deepest level of function recursion ... |
420 |
CopyAssignSchedEntry e; |
CopyAssignSchedEntry e; |
421 |
|
|
424 |
uint dstDimValues[8] = {}; |
uint dstDimValues[8] = {}; |
425 |
DimensionCase srcDimCase = dimCase; |
DimensionCase srcDimCase = dimCase; |
426 |
DimensionCase dstDimCase = dimCase; |
DimensionCase dstDimCase = dimCase; |
427 |
srcDimCase[gig::dimension_layer] = iSrcLayer; |
srcDimCase[mainDim] = iSrcMainBit; |
428 |
dstDimCase[gig::dimension_layer] = iDstLayer; |
dstDimCase[mainDim] = iDstMainBit; |
429 |
|
|
430 |
#if DEBUG_COMBINE_INSTRUMENTS |
#if DEBUG_COMBINE_INSTRUMENTS |
431 |
printf("-------------------------------\n"); |
printf("-------------------------------\n"); |
432 |
|
printf("iDstMainBit=%d iSrcMainBit=%d\n", iDstMainBit, iSrcMainBit); |
433 |
#endif |
#endif |
434 |
|
|
435 |
// first select source & target dimension region with an arbitrary |
// first select source & target dimension region with an arbitrary |
444 |
#if DEBUG_COMBINE_INSTRUMENTS |
#if DEBUG_COMBINE_INSTRUMENTS |
445 |
printf("dst "); fflush(stdout); |
printf("dst "); fflush(stdout); |
446 |
#endif |
#endif |
447 |
fillDimValues(dstDimValues, dstDimCase, outRgn, true); |
fillDimValues(dstDimValues, dstDimCase, outRgn, false); |
448 |
gig::DimensionRegion* srcDimRgn = inRgn->GetDimensionRegionByValue(srcDimValues); |
gig::DimensionRegion* srcDimRgn = inRgn->GetDimensionRegionByValue(srcDimValues); |
449 |
gig::DimensionRegion* dstDimRgn = outRgn->GetDimensionRegionByValue(dstDimValues); |
gig::DimensionRegion* dstDimRgn = outRgn->GetDimensionRegionByValue(dstDimValues); |
450 |
#if DEBUG_COMBINE_INSTRUMENTS |
#if DEBUG_COMBINE_INSTRUMENTS |
451 |
printf("iDstLayer=%d iSrcLayer=%d\n", iDstLayer, iSrcLayer); |
printf("iDstMainBit=%d iSrcMainBit=%d\n", iDstMainBit, iSrcMainBit); |
452 |
printf("srcDimRgn=%lx dstDimRgn=%lx\n", (uint64_t)srcDimRgn, (uint64_t)dstDimRgn); |
printf("srcDimRgn=%lx dstDimRgn=%lx\n", (uint64_t)srcDimRgn, (uint64_t)dstDimRgn); |
453 |
printf("srcSample='%s' dstSample='%s'\n", |
printf("srcSample='%s' dstSample='%s'\n", |
454 |
(!srcDimRgn->pSample ? "NULL" : srcDimRgn->pSample->pInfo->Name.c_str()), |
(!srcDimRgn->pSample ? "NULL" : srcDimRgn->pSample->pInfo->Name.c_str()), |
467 |
// re-select target dimension region (with correct velocity zone) |
// re-select target dimension region (with correct velocity zone) |
468 |
DimensionZones dstZones = preciseDimensionZonesFor(gig::dimension_velocity, dstDimRgn); |
DimensionZones dstZones = preciseDimensionZonesFor(gig::dimension_velocity, dstDimRgn); |
469 |
assert(dstZones.size() > 1); |
assert(dstZones.size() > 1); |
470 |
int iZoneIndex = dstDimCase[gig::dimension_velocity]; |
const int iDstZoneIndex = |
471 |
e.velocityZone = iZoneIndex; |
(mainDim == gig::dimension_velocity) |
472 |
|
? iDstMainBit : dstDimCase[gig::dimension_velocity]; // (mainDim == gig::dimension_velocity) exception case probably unnecessary here |
473 |
|
e.velocityZone = iDstZoneIndex; |
474 |
#if DEBUG_COMBINE_INSTRUMENTS |
#if DEBUG_COMBINE_INSTRUMENTS |
475 |
printf("dst velocity zone: %d/%d\n", iZoneIndex, (int)dstZones.size()); |
printf("dst velocity zone: %d/%d\n", iDstZoneIndex, (int)dstZones.size()); |
476 |
#endif |
#endif |
477 |
assert(uint(iZoneIndex) < dstZones.size()); |
assert(uint(iDstZoneIndex) < dstZones.size()); |
478 |
dstDimCase[gig::dimension_velocity] = dstZones[iZoneIndex].low; // arbitrary value between low and high |
dstDimCase[gig::dimension_velocity] = dstZones[iDstZoneIndex].low; // arbitrary value between low and high |
479 |
#if DEBUG_COMBINE_INSTRUMENTS |
#if DEBUG_COMBINE_INSTRUMENTS |
480 |
printf("dst velocity value = %d\n", dstDimCase[gig::dimension_velocity]); |
printf("dst velocity value = %d\n", dstDimCase[gig::dimension_velocity]); |
481 |
printf("dst refilled "); fflush(stdout); |
printf("dst refilled "); fflush(stdout); |
482 |
#endif |
#endif |
483 |
fillDimValues(dstDimValues, dstDimCase, outRgn, true); |
fillDimValues(dstDimValues, dstDimCase, outRgn, false); |
484 |
dstDimRgn = outRgn->GetDimensionRegionByValue(dstDimValues); |
dstDimRgn = outRgn->GetDimensionRegionByValue(dstDimValues); |
485 |
#if DEBUG_COMBINE_INSTRUMENTS |
#if DEBUG_COMBINE_INSTRUMENTS |
486 |
printf("reselected dstDimRgn=%lx\n", (uint64_t)dstDimRgn); |
printf("reselected dstDimRgn=%lx\n", (uint64_t)dstDimRgn); |
487 |
printf("dstSample='%s'\n", |
printf("dstSample='%s'%s\n", |
488 |
(!dstDimRgn->pSample ? "NULL" : dstDimRgn->pSample->pInfo->Name.c_str()) |
(!dstDimRgn->pSample ? "NULL" : dstDimRgn->pSample->pInfo->Name.c_str()), |
489 |
|
(dstDimRgn->pSample ? " <--- ERROR ERROR ERROR !!!!!!!!! " : "") |
490 |
); |
); |
491 |
#endif |
#endif |
492 |
|
|
495 |
if (inRgn->GetDimensionDefinition(gig::dimension_velocity)) { |
if (inRgn->GetDimensionDefinition(gig::dimension_velocity)) { |
496 |
DimensionZones srcZones = preciseDimensionZonesFor(gig::dimension_velocity, srcDimRgn); |
DimensionZones srcZones = preciseDimensionZonesFor(gig::dimension_velocity, srcDimRgn); |
497 |
e.totalSrcVelocityZones = srcZones.size(); |
e.totalSrcVelocityZones = srcZones.size(); |
498 |
assert(srcZones.size() > 1); |
assert(srcZones.size() > 0); |
499 |
if (uint(iZoneIndex) >= srcZones.size()) |
if (srcZones.size() <= 1) { |
500 |
iZoneIndex = srcZones.size() - 1; |
addWarning("Input region has a velocity dimension with only ONE zone!"); |
501 |
srcDimCase[gig::dimension_velocity] = srcZones[iZoneIndex].low; // same zone as used above for target dimension region (no matter what the precise zone ranges are) |
} |
502 |
|
int iSrcZoneIndex = |
503 |
|
(mainDim == gig::dimension_velocity) |
504 |
|
? iSrcMainBit : iDstZoneIndex; |
505 |
|
if (uint(iSrcZoneIndex) >= srcZones.size()) |
506 |
|
iSrcZoneIndex = srcZones.size() - 1; |
507 |
|
srcDimCase[gig::dimension_velocity] = srcZones[iSrcZoneIndex].low; // same zone as used above for target dimension region (no matter what the precise zone ranges are) |
508 |
#if DEBUG_COMBINE_INSTRUMENTS |
#if DEBUG_COMBINE_INSTRUMENTS |
509 |
printf("src refilled "); fflush(stdout); |
printf("src refilled "); fflush(stdout); |
510 |
#endif |
#endif |
519 |
} |
} |
520 |
} |
} |
521 |
|
|
522 |
// Schedule copy opertion of source -> target DimensionRegion for the |
// Schedule copy operation of source -> target DimensionRegion for the |
523 |
// time after all nested loops have been traversed. We have to postpone |
// time after all nested loops have been traversed. We have to postpone |
524 |
// the actual copy operations this way, because otherwise it would |
// the actual copy operations this way, because otherwise it would |
525 |
// overwrite informations inside the destination DimensionRegion object |
// overwrite informations inside the destination DimensionRegion object |
550 |
dimCase[type] = (def->split_type == gig::split_type_bit) ? iZone : zoneRange.low; |
dimCase[type] = (def->split_type == gig::split_type_bit) ? iZone : zoneRange.low; |
551 |
|
|
552 |
// recurse until 'dims' is exhausted (and dimCase filled up with concrete value) |
// recurse until 'dims' is exhausted (and dimCase filled up with concrete value) |
553 |
copyDimensionRegions(outRgn, inRgn, dims, iDstLayer, iSrcLayer, dimCase, schedule); |
scheduleCopyDimensionRegions(outRgn, inRgn, dims, mainDim, iDstMainBit, iSrcMainBit, schedule, dimCase); |
554 |
} |
} |
555 |
|
} |
556 |
|
|
557 |
// if current function call is the (very first) entry point ... |
static OrderedRegionGroup sortRegionGroup(const RegionGroup& group, const std::vector<gig::Instrument*>& instruments) { |
558 |
if (isHighestLevelOfRecursion) { |
OrderedRegionGroup result; |
559 |
// ... then perform all scheduled DimensionRegion copy operations |
for (std::vector<gig::Instrument*>::const_iterator it = instruments.begin(); |
560 |
for (uint i = 0; i < schedule->size(); ++i) { |
it != instruments.end(); ++it) |
561 |
CopyAssignSchedEntry& e = (*schedule)[i]; |
{ |
562 |
|
RegionGroup::const_iterator itRgn = group.find(*it); |
563 |
// backup the target DimensionRegion's current dimension zones upper |
if (itRgn == group.end()) continue; |
564 |
// limits (because the target DimensionRegion's upper limits are |
result.push_back( |
565 |
// already defined correctly since calling AddDimension(), and the |
std::pair<gig::Instrument*, gig::Region*>( |
566 |
// CopyAssign() call next, will overwrite those upper limits |
itRgn->first, itRgn->second |
567 |
// unfortunately |
) |
568 |
DimensionRegionUpperLimits dstUpperLimits = getDimensionRegionUpperLimits(e.dst); |
); |
|
DimensionRegionUpperLimits srcUpperLimits = getDimensionRegionUpperLimits(e.src); |
|
|
|
|
|
// now actually copy over the current DimensionRegion |
|
|
const gig::Region* const origRgn = e.dst->GetParent(); // just for sanity check below |
|
|
e.dst->CopyAssign(e.src); |
|
|
assert(origRgn == e.dst->GetParent()); // if gigedit is crashing here, then you must update libgig (to at least SVN r2547, v3.3.0.svn10) |
|
|
|
|
|
// restore all original dimension zone upper limits except of the |
|
|
// velocity dimension, because the velocity dimension zone sizes are |
|
|
// allowed to differ for individual DimensionRegions in gig v3 |
|
|
// format |
|
|
if (srcUpperLimits.count(gig::dimension_velocity)) { |
|
|
assert(dstUpperLimits.count(gig::dimension_velocity)); |
|
|
dstUpperLimits[gig::dimension_velocity] = |
|
|
(e.velocityZone >= e.totalSrcVelocityZones) |
|
|
? 127 : srcUpperLimits[gig::dimension_velocity]; |
|
|
} |
|
|
restoreDimensionRegionUpperLimits(e.dst, dstUpperLimits); |
|
|
} |
|
|
delete schedule; |
|
569 |
} |
} |
570 |
|
return result; |
571 |
} |
} |
572 |
|
|
573 |
/** @brief Combine given list of instruments to one instrument. |
/** @brief Combine given list of instruments to one instrument. |
574 |
* |
* |
575 |
* Takes a list of @a instruments as argument and combines them to one single |
* Takes a list of @a instruments as argument and combines them to one single |
576 |
* new @a output instrument. For this task, it will create a 'layer' dimension |
* new @a output instrument. For this task, it will create a dimension of type |
577 |
* in the new instrument and copies the source instruments to those layers. |
* given by @a mainDimension in the new instrument and copies the source |
578 |
|
* instruments to those dimension zones. |
579 |
* |
* |
580 |
* @param instruments - (input) list of instruments that shall be combined, |
* @param instruments - (input) list of instruments that shall be combined, |
581 |
* they will only be read, so they will be left untouched |
* they will only be read, so they will be left untouched |
583 |
* be created |
* be created |
584 |
* @param output - (output) on success this pointer will be set to the new |
* @param output - (output) on success this pointer will be set to the new |
585 |
* instrument being created |
* instrument being created |
586 |
|
* @param mainDimension - the dimension that shall be used to combine the |
587 |
|
* instruments |
588 |
* @throw RIFF::Exception on any kinds of errors |
* @throw RIFF::Exception on any kinds of errors |
589 |
*/ |
*/ |
590 |
static void combineInstruments(std::vector<gig::Instrument*>& instruments, gig::File* gig, gig::Instrument*& output) { |
static void combineInstruments(std::vector<gig::Instrument*>& instruments, gig::File* gig, gig::Instrument*& output, gig::dimension_t mainDimension) { |
591 |
output = NULL; |
output = NULL; |
592 |
|
|
593 |
// divide the individual regions to (probably even smaller) groups of |
// divide the individual regions to (probably even smaller) groups of |
620 |
{ |
{ |
621 |
gig::Region* outRgn = outInstr->AddRegion(); |
gig::Region* outRgn = outInstr->AddRegion(); |
622 |
outRgn->SetKeyRange(itGroup->first.low, itGroup->first.high); |
outRgn->SetKeyRange(itGroup->first.low, itGroup->first.high); |
623 |
|
#if DEBUG_COMBINE_INSTRUMENTS |
624 |
|
printf("---> Start target region %d..%d\n", itGroup->first.low, itGroup->first.high); |
625 |
|
#endif |
626 |
|
|
627 |
// detect the total amount of layers required to build up this combi |
// detect the total amount of zones required for the given main |
628 |
// for current key range |
// dimension to build up this combi for current key range |
629 |
int iTotalLayers = 0; |
int iTotalZones = 0; |
630 |
for (RegionGroup::iterator itRgn = itGroup->second.begin(); |
for (RegionGroup::iterator itRgn = itGroup->second.begin(); |
631 |
itRgn != itGroup->second.end(); ++itRgn) |
itRgn != itGroup->second.end(); ++itRgn) |
632 |
{ |
{ |
633 |
gig::Region* inRgn = itRgn->second; |
gig::Region* inRgn = itRgn->second; |
634 |
iTotalLayers += inRgn->Layers; |
gig::dimension_def_t* def = inRgn->GetDimensionDefinition(mainDimension); |
635 |
|
iTotalZones += (def) ? def->zones : 1; |
636 |
} |
} |
637 |
|
#if DEBUG_COMBINE_INSTRUMENTS |
638 |
|
printf("Required total zones: %d, vertical regions: %d\n", iTotalZones, itGroup->second.size()); |
639 |
|
#endif |
640 |
|
|
641 |
// create all required dimensions for this output region |
// create all required dimensions for this output region |
642 |
// (except the layer dimension, which we create as next step) |
// (except the main dimension used for separating the individual |
643 |
|
// instruments, we create that particular dimension as next step) |
644 |
Dimensions dims = getDimensionsForRegionGroup(itGroup->second); |
Dimensions dims = getDimensionsForRegionGroup(itGroup->second); |
645 |
for (Dimensions::iterator itDim = dims.begin(); |
// the given main dimension which is used to combine the instruments is |
646 |
itDim != dims.end(); ++itDim) |
// created separately after the next code block, and the main dimension |
647 |
|
// should not be part of dims here, because it also used for iterating |
648 |
|
// all dimensions zones, which would lead to this dimensions being |
649 |
|
// iterated twice |
650 |
|
dims.erase(mainDimension); |
651 |
{ |
{ |
652 |
if (itDim->first == gig::dimension_layer) continue; |
std::vector<gig::dimension_t> skipTheseDimensions; // used to prevent a misbehavior (i.e. crash) of the combine algorithm in case one of the source instruments has a dimension with only one zone, which is not standard conform |
653 |
|
|
654 |
gig::dimension_def_t def; |
for (Dimensions::iterator itDim = dims.begin(); |
655 |
def.dimension = itDim->first; // dimension type |
itDim != dims.end(); ++itDim) |
656 |
def.zones = itDim->second.size(); |
{ |
657 |
def.bits = zoneCountToBits(def.zones); |
gig::dimension_def_t def; |
658 |
#if DEBUG_COMBINE_INSTRUMENTS |
def.dimension = itDim->first; // dimension type |
659 |
std::cout << "Adding new regular dimension type=" << std::hex << (int)def.dimension << std::dec << ", zones=" << (int)def.zones << ", bits=" << (int)def.bits << " ... " << std::flush; |
def.zones = itDim->second.size(); |
660 |
#endif |
def.bits = zoneCountToBits(def.zones); |
661 |
outRgn->AddDimension(&def); |
if (def.zones < 2) { |
662 |
#if DEBUG_COMBINE_INSTRUMENTS |
addWarning( |
663 |
std::cout << "OK" << std::endl << std::flush; |
"Attempt to create dimension with type=0x%x with only " |
664 |
#endif |
"ONE zone (because at least one of the source " |
665 |
|
"instruments seems to have such a velocity dimension " |
666 |
|
"with only ONE zone, which is odd)! Skipping this " |
667 |
|
"dimension for now.", |
668 |
|
(int)itDim->first |
669 |
|
); |
670 |
|
skipTheseDimensions.push_back(itDim->first); |
671 |
|
continue; |
672 |
|
} |
673 |
|
#if DEBUG_COMBINE_INSTRUMENTS |
674 |
|
std::cout << "Adding new regular dimension type=" << std::hex << (int)def.dimension << std::dec << ", zones=" << (int)def.zones << ", bits=" << (int)def.bits << " ... " << std::flush; |
675 |
|
#endif |
676 |
|
outRgn->AddDimension(&def); |
677 |
|
#if DEBUG_COMBINE_INSTRUMENTS |
678 |
|
std::cout << "OK" << std::endl << std::flush; |
679 |
|
#endif |
680 |
|
} |
681 |
|
// prevent the following dimensions to be processed further below |
682 |
|
// (since the respective dimension was not created above) |
683 |
|
for (int i = 0; i < skipTheseDimensions.size(); ++i) |
684 |
|
dims.erase(skipTheseDimensions[i]); |
685 |
} |
} |
686 |
|
|
687 |
// create the layer dimension (if necessary for current key range) |
// create the main dimension (if necessary for current key range) |
688 |
if (iTotalLayers > 1) { |
if (iTotalZones > 1) { |
689 |
gig::dimension_def_t def; |
gig::dimension_def_t def; |
690 |
def.dimension = gig::dimension_layer; // dimension type |
def.dimension = mainDimension; // dimension type |
691 |
def.zones = iTotalLayers; |
def.zones = iTotalZones; |
692 |
def.bits = zoneCountToBits(def.zones); |
def.bits = zoneCountToBits(def.zones); |
693 |
#if DEBUG_COMBINE_INSTRUMENTS |
#if DEBUG_COMBINE_INSTRUMENTS |
694 |
std::cout << "Adding new (layer) dimension type=" << std::hex << (int)def.dimension << std::dec << ", zones=" << (int)def.zones << ", bits=" << (int)def.bits << " ... " << std::flush; |
std::cout << "Adding new main combi dimension type=" << std::hex << (int)def.dimension << std::dec << ", zones=" << (int)def.zones << ", bits=" << (int)def.bits << " ... " << std::flush; |
695 |
#endif |
#endif |
696 |
outRgn->AddDimension(&def); |
outRgn->AddDimension(&def); |
697 |
#if DEBUG_COMBINE_INSTRUMENTS |
#if DEBUG_COMBINE_INSTRUMENTS |
698 |
std::cout << "OK" << std::endl << std::flush; |
std::cout << "OK" << std::endl << std::flush; |
699 |
#endif |
#endif |
700 |
|
} else { |
701 |
|
dims.erase(mainDimension); |
702 |
} |
} |
703 |
|
|
704 |
// now copy the source dimension regions to the target dimension regions |
// for the next task we need to have the current RegionGroup to be |
705 |
int iDstLayer = 0; |
// sorted by instrument in the same sequence as the 'instruments' vector |
706 |
for (RegionGroup::iterator itRgn = itGroup->second.begin(); |
// argument passed to this function (because the std::map behind the |
707 |
itRgn != itGroup->second.end(); ++itRgn) // iterate over 'vertical' / source regions ... |
// 'RegionGroup' type sorts by memory address instead, and that would |
708 |
|
// sometimes lead to the source instruments' region to be sorted into |
709 |
|
// the wrong target layer) |
710 |
|
OrderedRegionGroup currentGroup = sortRegionGroup(itGroup->second, instruments); |
711 |
|
|
712 |
|
// schedule copying the source dimension regions to the target dimension |
713 |
|
// regions |
714 |
|
CopyAssignSchedule schedule; |
715 |
|
int iDstMainBit = 0; |
716 |
|
for (OrderedRegionGroup::iterator itRgn = currentGroup.begin(); |
717 |
|
itRgn != currentGroup.end(); ++itRgn) // iterate over 'vertical' / source regions ... |
718 |
{ |
{ |
719 |
gig::Region* inRgn = itRgn->second; |
gig::Region* inRgn = itRgn->second; |
720 |
for (uint iSrcLayer = 0; iSrcLayer < inRgn->Layers; ++iSrcLayer, ++iDstLayer) { |
#if DEBUG_COMBINE_INSTRUMENTS |
721 |
copyDimensionRegions(outRgn, inRgn, dims, iDstLayer, iSrcLayer); |
printf("[source region of '%s']\n", inRgn->GetParent()->pInfo->Name.c_str()); |
722 |
|
#endif |
723 |
|
|
724 |
|
// determine how many main dimension zones this input region requires |
725 |
|
gig::dimension_def_t* def = inRgn->GetDimensionDefinition(mainDimension); |
726 |
|
const int inRgnMainZones = (def) ? def->zones : 1; |
727 |
|
|
728 |
|
for (uint iSrcMainBit = 0; iSrcMainBit < inRgnMainZones; ++iSrcMainBit, ++iDstMainBit) { |
729 |
|
scheduleCopyDimensionRegions( |
730 |
|
outRgn, inRgn, dims, mainDimension, |
731 |
|
iDstMainBit, iSrcMainBit, &schedule |
732 |
|
); |
733 |
} |
} |
734 |
} |
} |
735 |
|
|
736 |
|
// finally copy the scheduled source -> target dimension regions |
737 |
|
for (uint i = 0; i < schedule.size(); ++i) { |
738 |
|
CopyAssignSchedEntry& e = schedule[i]; |
739 |
|
|
740 |
|
// backup the target DimensionRegion's current dimension zones upper |
741 |
|
// limits (because the target DimensionRegion's upper limits are |
742 |
|
// already defined correctly since calling AddDimension(), and the |
743 |
|
// CopyAssign() call next, will overwrite those upper limits |
744 |
|
// unfortunately |
745 |
|
DimensionRegionUpperLimits dstUpperLimits = getDimensionRegionUpperLimits(e.dst); |
746 |
|
DimensionRegionUpperLimits srcUpperLimits = getDimensionRegionUpperLimits(e.src); |
747 |
|
|
748 |
|
// now actually copy over the current DimensionRegion |
749 |
|
const gig::Region* const origRgn = e.dst->GetParent(); // just for sanity check below |
750 |
|
e.dst->CopyAssign(e.src); |
751 |
|
assert(origRgn == e.dst->GetParent()); // if gigedit is crashing here, then you must update libgig (to at least SVN r2547, v3.3.0.svn10) |
752 |
|
|
753 |
|
// restore all original dimension zone upper limits except of the |
754 |
|
// velocity dimension, because the velocity dimension zone sizes are |
755 |
|
// allowed to differ for individual DimensionRegions in gig v3 |
756 |
|
// format |
757 |
|
// |
758 |
|
// if the main dinension is the 'velocity' dimension, then skip |
759 |
|
// restoring the source's original velocity zone limits, because |
760 |
|
// dealing with merging that is not implemented yet |
761 |
|
// TODO: merge custom velocity splits if main dimension is the velocity dimension (for now equal sized velocity zones are used if mainDim is 'velocity') |
762 |
|
if (srcUpperLimits.count(gig::dimension_velocity) && mainDimension != gig::dimension_velocity) { |
763 |
|
if (!dstUpperLimits.count(gig::dimension_velocity)) { |
764 |
|
addWarning("Source instrument seems to have a velocity dimension whereas new target instrument doesn't!"); |
765 |
|
} else { |
766 |
|
dstUpperLimits[gig::dimension_velocity] = |
767 |
|
(e.velocityZone >= e.totalSrcVelocityZones) |
768 |
|
? 127 : srcUpperLimits[gig::dimension_velocity]; |
769 |
|
} |
770 |
|
} |
771 |
|
restoreDimensionRegionUpperLimits(e.dst, dstUpperLimits); |
772 |
|
} |
773 |
} |
} |
774 |
|
|
775 |
// success |
// success |
780 |
// class 'CombineInstrumentsDialog' |
// class 'CombineInstrumentsDialog' |
781 |
|
|
782 |
CombineInstrumentsDialog::CombineInstrumentsDialog(Gtk::Window& parent, gig::File* gig) |
CombineInstrumentsDialog::CombineInstrumentsDialog(Gtk::Window& parent, gig::File* gig) |
783 |
: Gtk::Dialog(_("Combine Instruments"), parent, true), |
: ManagedDialog(_("Combine Instruments"), parent, true), |
784 |
m_gig(gig), m_fileWasChanged(false), m_newCombinedInstrument(NULL), |
m_gig(gig), m_fileWasChanged(false), m_newCombinedInstrument(NULL), |
785 |
m_cancelButton(Gtk::Stock::CANCEL), m_OKButton(Gtk::Stock::OK), |
m_cancelButton(Gtk::Stock::CANCEL), m_OKButton(Gtk::Stock::OK), |
786 |
m_descriptionLabel() |
m_descriptionLabel(), m_tableDimCombo(2, 2), m_comboDimType(), |
787 |
|
m_labelDimType(Glib::ustring(_("Combine by Dimension:")) + " ", Gtk::ALIGN_END) |
788 |
{ |
{ |
789 |
|
if (!Settings::singleton()->autoRestoreWindowDimension) { |
790 |
|
set_default_size(500, 600); |
791 |
|
set_position(Gtk::WIN_POS_MOUSE); |
792 |
|
} |
793 |
|
|
794 |
|
m_scrolledWindow.add(m_treeView); |
795 |
|
m_scrolledWindow.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC); |
796 |
|
|
797 |
get_vbox()->pack_start(m_descriptionLabel, Gtk::PACK_SHRINK); |
get_vbox()->pack_start(m_descriptionLabel, Gtk::PACK_SHRINK); |
798 |
get_vbox()->pack_start(m_treeView); |
get_vbox()->pack_start(m_tableDimCombo, Gtk::PACK_SHRINK); |
799 |
|
get_vbox()->pack_start(m_scrolledWindow); |
800 |
|
get_vbox()->pack_start(m_labelOrder, Gtk::PACK_SHRINK); |
801 |
|
get_vbox()->pack_start(m_iconView, Gtk::PACK_SHRINK); |
802 |
get_vbox()->pack_start(m_buttonBox, Gtk::PACK_SHRINK); |
get_vbox()->pack_start(m_buttonBox, Gtk::PACK_SHRINK); |
803 |
|
|
804 |
#if GTKMM_MAJOR_VERSION >= 3 |
#if GTKMM_MAJOR_VERSION >= 3 |
805 |
description.set_line_wrap(); |
m_descriptionLabel.set_line_wrap(); |
806 |
#endif |
#endif |
807 |
m_descriptionLabel.set_text(_( |
m_descriptionLabel.set_text(_( |
808 |
"Select at least two instruments below that shall be combined " |
"Select at least two instruments below that shall be combined (as " |
809 |
"as layers (using a \"Layer\" dimension) to a new instrument. The " |
"separate dimension zones of the selected dimension type) as a new " |
810 |
"original instruments remain untouched.") |
"instrument. The original instruments remain untouched.\n\n" |
811 |
); |
"You may use this tool for example to combine solo instruments into " |
812 |
|
"a combi sound arrangement by selecting the 'layer' dimension, or you " |
813 |
|
"might combine similar sounding solo sounds into separate velocity " |
814 |
|
"split layers by using the 'velocity' dimension, and so on." |
815 |
|
)); |
816 |
|
|
817 |
|
// add dimension type combo box |
818 |
|
{ |
819 |
|
int iLayerDimIndex = -1; |
820 |
|
Glib::RefPtr<Gtk::ListStore> refComboModel = Gtk::ListStore::create(m_comboDimsModel); |
821 |
|
for (int i = 0x01, iRow = 0; i < 0xff; i++) { |
822 |
|
Glib::ustring sType = |
823 |
|
dimTypeAsString(static_cast<gig::dimension_t>(i)); |
824 |
|
if (sType.find("Unknown") != 0) { |
825 |
|
Gtk::TreeModel::Row row = *(refComboModel->append()); |
826 |
|
row[m_comboDimsModel.m_type_id] = i; |
827 |
|
row[m_comboDimsModel.m_type_name] = sType; |
828 |
|
if (i == gig::dimension_layer) iLayerDimIndex = iRow; |
829 |
|
iRow++; |
830 |
|
} |
831 |
|
} |
832 |
|
m_comboDimType.set_model(refComboModel); |
833 |
|
m_comboDimType.pack_start(m_comboDimsModel.m_type_id); |
834 |
|
m_comboDimType.pack_start(m_comboDimsModel.m_type_name); |
835 |
|
m_tableDimCombo.attach(m_labelDimType, 0, 1, 0, 1); |
836 |
|
m_tableDimCombo.attach(m_comboDimType, 1, 2, 0, 1); |
837 |
|
m_comboDimType.set_active(iLayerDimIndex); // preselect "layer" dimension |
838 |
|
} |
839 |
|
|
840 |
m_refTreeModel = Gtk::ListStore::create(m_columns); |
m_refTreeModel = Gtk::ListStore::create(m_columns); |
841 |
m_treeView.set_model(m_refTreeModel); |
m_treeView.set_model(m_refTreeModel); |
843 |
"Use SHIFT + left click or CTRL + left click to select the instruments " |
"Use SHIFT + left click or CTRL + left click to select the instruments " |
844 |
"you want to combine." |
"you want to combine." |
845 |
)); |
)); |
846 |
m_treeView.append_column("Instrument", m_columns.m_col_name); |
m_treeView.append_column(_("Nr"), m_columns.m_col_index); |
847 |
m_treeView.set_headers_visible(false); |
m_treeView.append_column(_("Instrument"), m_columns.m_col_name); |
848 |
|
m_treeView.set_headers_visible(true); |
849 |
m_treeView.get_selection()->set_mode(Gtk::SELECTION_MULTIPLE); |
m_treeView.get_selection()->set_mode(Gtk::SELECTION_MULTIPLE); |
850 |
m_treeView.get_selection()->signal_changed().connect( |
m_treeView.get_selection()->signal_changed().connect( |
851 |
sigc::mem_fun(*this, &CombineInstrumentsDialog::onSelectionChanged) |
sigc::mem_fun(*this, &CombineInstrumentsDialog::onSelectionChanged) |
870 |
Glib::ustring name(gig_to_utf8(instr->pInfo->Name)); |
Glib::ustring name(gig_to_utf8(instr->pInfo->Name)); |
871 |
Gtk::TreeModel::iterator iter = m_refTreeModel->append(); |
Gtk::TreeModel::iterator iter = m_refTreeModel->append(); |
872 |
Gtk::TreeModel::Row row = *iter; |
Gtk::TreeModel::Row row = *iter; |
873 |
|
row[m_columns.m_col_index] = i; |
874 |
row[m_columns.m_col_name] = name; |
row[m_columns.m_col_name] = name; |
875 |
row[m_columns.m_col_instr] = instr; |
row[m_columns.m_col_instr] = instr; |
876 |
} |
} |
877 |
|
|
878 |
|
m_refOrderModel = Gtk::ListStore::create(m_orderColumns); |
879 |
|
m_iconView.set_model(m_refOrderModel); |
880 |
|
m_iconView.set_tooltip_text(_("Use drag & drop to change the order.")); |
881 |
|
m_iconView.set_markup_column(1); |
882 |
|
m_iconView.set_selection_mode(Gtk::SELECTION_SINGLE); |
883 |
|
// force background to retain white also on selections |
884 |
|
// (this also fixes a bug with GTK 2 which often causes visibility issue |
885 |
|
// with the text of the selected item) |
886 |
|
{ |
887 |
|
Gdk::Color white; |
888 |
|
white.set("#ffffff"); |
889 |
|
GtkWidget* widget = (GtkWidget*) m_iconView.gobj(); |
890 |
|
gtk_widget_modify_base(widget, GTK_STATE_SELECTED, white.gobj()); |
891 |
|
gtk_widget_modify_base(widget, GTK_STATE_ACTIVE, white.gobj()); |
892 |
|
gtk_widget_modify_bg(widget, GTK_STATE_SELECTED, white.gobj()); |
893 |
|
gtk_widget_modify_bg(widget, GTK_STATE_ACTIVE, white.gobj()); |
894 |
|
} |
895 |
|
|
896 |
|
m_labelOrder.set_text(_("Order of the instruments to be combined:")); |
897 |
|
|
898 |
|
// establish drag&drop within the instrument tree view, allowing to reorder |
899 |
|
// the sequence of instruments within the gig file |
900 |
|
{ |
901 |
|
std::vector<Gtk::TargetEntry> drag_target_instrument; |
902 |
|
drag_target_instrument.push_back(Gtk::TargetEntry("gig::Instrument")); |
903 |
|
m_iconView.drag_source_set(drag_target_instrument); |
904 |
|
m_iconView.drag_dest_set(drag_target_instrument); |
905 |
|
m_iconView.signal_drag_begin().connect( |
906 |
|
sigc::mem_fun(*this, &CombineInstrumentsDialog::on_order_drag_begin) |
907 |
|
); |
908 |
|
m_iconView.signal_drag_data_get().connect( |
909 |
|
sigc::mem_fun(*this, &CombineInstrumentsDialog::on_order_drag_data_get) |
910 |
|
); |
911 |
|
m_iconView.signal_drag_data_received().connect( |
912 |
|
sigc::mem_fun(*this, &CombineInstrumentsDialog::on_order_drop_drag_data_received) |
913 |
|
); |
914 |
|
} |
915 |
|
|
916 |
m_buttonBox.set_layout(Gtk::BUTTONBOX_END); |
m_buttonBox.set_layout(Gtk::BUTTONBOX_END); |
917 |
m_buttonBox.set_border_width(5); |
m_buttonBox.set_border_width(5); |
918 |
m_buttonBox.pack_start(m_cancelButton, Gtk::PACK_SHRINK); |
m_buttonBox.pack_start(m_cancelButton, Gtk::PACK_SHRINK); |
944 |
Gtk::MessageDialog msg(*this, txt, false, Gtk::MESSAGE_WARNING); |
Gtk::MessageDialog msg(*this, txt, false, Gtk::MESSAGE_WARNING); |
945 |
msg.run(); |
msg.run(); |
946 |
} |
} |
947 |
|
|
948 |
|
// OK button should have focus by default for quick combining with Return key |
949 |
|
m_OKButton.grab_focus(); |
950 |
|
} |
951 |
|
|
952 |
|
void CombineInstrumentsDialog::on_order_drag_begin(const Glib::RefPtr<Gdk::DragContext>& context) |
953 |
|
{ |
954 |
|
printf("Drag begin\n"); |
955 |
|
first_call_to_drag_data_get = true; |
956 |
|
} |
957 |
|
|
958 |
|
void CombineInstrumentsDialog::on_order_drag_data_get(const Glib::RefPtr<Gdk::DragContext>& context, |
959 |
|
Gtk::SelectionData& selection_data, guint, guint) |
960 |
|
{ |
961 |
|
printf("Drag data get\n"); |
962 |
|
if (!first_call_to_drag_data_get) return; |
963 |
|
first_call_to_drag_data_get = false; |
964 |
|
|
965 |
|
// get selected source instrument |
966 |
|
gig::Instrument* src = NULL; |
967 |
|
{ |
968 |
|
std::vector<Gtk::TreeModel::Path> rows = m_iconView.get_selected_items(); |
969 |
|
if (!rows.empty()) { |
970 |
|
Gtk::TreeModel::iterator it = m_refOrderModel->get_iter(rows[0]); |
971 |
|
if (it) { |
972 |
|
Gtk::TreeModel::Row row = *it; |
973 |
|
src = row[m_orderColumns.m_col_instr]; |
974 |
|
} |
975 |
|
} |
976 |
|
} |
977 |
|
if (!src) { |
978 |
|
printf("Drag data get: !src\n"); |
979 |
|
return; |
980 |
|
} |
981 |
|
printf("src=%ld\n", (size_t)src); |
982 |
|
|
983 |
|
// pass the source gig::Instrument as pointer |
984 |
|
selection_data.set(selection_data.get_target(), 0/*unused*/, (const guchar*)&src, |
985 |
|
sizeof(src)/*length of data in bytes*/); |
986 |
|
} |
987 |
|
|
988 |
|
void CombineInstrumentsDialog::on_order_drop_drag_data_received( |
989 |
|
const Glib::RefPtr<Gdk::DragContext>& context, int x, int y, |
990 |
|
const Gtk::SelectionData& selection_data, guint, guint time) |
991 |
|
{ |
992 |
|
printf("Drag data received\n"); |
993 |
|
if (&selection_data == NULL) { |
994 |
|
printf("!selection_data\n"); |
995 |
|
return; |
996 |
|
} |
997 |
|
if (!selection_data.get_data()) { |
998 |
|
printf("selection_data.get_data() == NULL\n"); |
999 |
|
return; |
1000 |
|
} |
1001 |
|
|
1002 |
|
gig::Instrument* src = *((gig::Instrument**) selection_data.get_data()); |
1003 |
|
if (!src || selection_data.get_length() != sizeof(gig::Instrument*)) { |
1004 |
|
printf("!src\n"); |
1005 |
|
return; |
1006 |
|
} |
1007 |
|
printf("src=%d\n", src); |
1008 |
|
|
1009 |
|
gig::Instrument* dst = NULL; |
1010 |
|
{ |
1011 |
|
Gtk::TreeModel::Path path = m_iconView.get_path_at_pos(x, y); |
1012 |
|
if (!path) return; |
1013 |
|
|
1014 |
|
Gtk::TreeModel::iterator iter = m_refOrderModel->get_iter(path); |
1015 |
|
if (!iter) return; |
1016 |
|
Gtk::TreeModel::Row row = *iter; |
1017 |
|
dst = row[m_orderColumns.m_col_instr]; |
1018 |
|
} |
1019 |
|
if (!dst) { |
1020 |
|
printf("!dst\n"); |
1021 |
|
return; |
1022 |
|
} |
1023 |
|
|
1024 |
|
printf("dragdrop received src='%s' dst='%s'\n", src->pInfo->Name.c_str(), dst->pInfo->Name.c_str()); |
1025 |
|
|
1026 |
|
// swap the two items |
1027 |
|
typedef Gtk::TreeModel::Children Children; |
1028 |
|
Children children = m_refOrderModel->children(); |
1029 |
|
Children::iterator itSrc, itDst; |
1030 |
|
int i = 0, iSrc = -1, iDst = -1; |
1031 |
|
for (Children::iterator iter = children.begin(); |
1032 |
|
iter != children.end(); ++iter, ++i) |
1033 |
|
{ |
1034 |
|
Gtk::TreeModel::Row row = *iter; |
1035 |
|
if (row[m_orderColumns.m_col_instr] == src) { |
1036 |
|
itSrc = iter; |
1037 |
|
iSrc = i; |
1038 |
|
} else if (row[m_orderColumns.m_col_instr] == dst) { |
1039 |
|
itDst = iter; |
1040 |
|
iDst = i; |
1041 |
|
} |
1042 |
|
} |
1043 |
|
if (itSrc && itDst) { |
1044 |
|
// swap elements |
1045 |
|
m_refOrderModel->iter_swap(itSrc, itDst); |
1046 |
|
// update markup |
1047 |
|
Gtk::TreeModel::Row rowSrc = *itSrc; |
1048 |
|
Gtk::TreeModel::Row rowDst = *itDst; |
1049 |
|
{ |
1050 |
|
Glib::ustring name = rowSrc[m_orderColumns.m_col_name]; |
1051 |
|
Glib::ustring markup = |
1052 |
|
"<span foreground='black' background='white'>" + ToString(iDst+1) + ".</span>\n<span foreground='green' background='white'>" + name + "</span>"; |
1053 |
|
rowSrc[m_orderColumns.m_col_markup] = markup; |
1054 |
|
} |
1055 |
|
{ |
1056 |
|
Glib::ustring name = rowDst[m_orderColumns.m_col_name]; |
1057 |
|
Glib::ustring markup = |
1058 |
|
"<span foreground='black' background='white'>" + ToString(iSrc+1) + ".</span>\n<span foreground='green' background='white'>" + name + "</span>"; |
1059 |
|
rowDst[m_orderColumns.m_col_markup] = markup; |
1060 |
|
} |
1061 |
|
} |
1062 |
|
} |
1063 |
|
|
1064 |
|
void CombineInstrumentsDialog::setSelectedInstruments(const std::set<int>& instrumentIndeces) { |
1065 |
|
typedef Gtk::TreeModel::Children Children; |
1066 |
|
Children children = m_refTreeModel->children(); |
1067 |
|
for (Children::iterator iter = children.begin(); |
1068 |
|
iter != children.end(); ++iter) |
1069 |
|
{ |
1070 |
|
Gtk::TreeModel::Row row = *iter; |
1071 |
|
int index = row[m_columns.m_col_index]; |
1072 |
|
if (instrumentIndeces.count(index)) |
1073 |
|
m_treeView.get_selection()->select(iter); |
1074 |
|
} |
1075 |
|
// hack: OK button lost focus after doing the above, it should have focus by default for quick combining with Return key |
1076 |
|
m_OKButton.grab_focus(); |
1077 |
} |
} |
1078 |
|
|
1079 |
void CombineInstrumentsDialog::combineSelectedInstruments() { |
void CombineInstrumentsDialog::combineSelectedInstruments() { |
1080 |
std::vector<gig::Instrument*> instruments; |
std::vector<gig::Instrument*> instruments; |
1081 |
std::vector<Gtk::TreeModel::Path> v = m_treeView.get_selection()->get_selected_rows(); |
{ |
1082 |
for (uint i = 0; i < v.size(); ++i) { |
typedef Gtk::TreeModel::Children Children; |
1083 |
Gtk::TreeModel::iterator it = m_refTreeModel->get_iter(v[i]); |
int i = 0; |
1084 |
Gtk::TreeModel::Row row = *it; |
Children selection = m_refOrderModel->children(); |
1085 |
Glib::ustring name = row[m_columns.m_col_name]; |
for (Children::iterator it = selection.begin(); |
1086 |
gig::Instrument* instrument = row[m_columns.m_col_instr]; |
it != selection.end(); ++it, ++i) |
1087 |
#if DEBUG_COMBINE_INSTRUMENTS |
{ |
1088 |
printf("Selection '%s' 0x%lx\n\n", name.c_str(), int64_t((void*)instrument)); |
Gtk::TreeModel::Row row = *it; |
1089 |
#endif |
Glib::ustring name = row[m_orderColumns.m_col_name]; |
1090 |
instruments.push_back(instrument); |
gig::Instrument* instrument = row[m_orderColumns.m_col_instr]; |
1091 |
|
#if DEBUG_COMBINE_INSTRUMENTS |
1092 |
|
printf("Selection %d. '%s' %p\n\n", (i+1), name.c_str(), instrument)); |
1093 |
|
#endif |
1094 |
|
instruments.push_back(instrument); |
1095 |
|
} |
1096 |
} |
} |
1097 |
|
|
1098 |
|
g_warnings.clear(); |
1099 |
|
|
1100 |
try { |
try { |
1101 |
combineInstruments(instruments, m_gig, m_newCombinedInstrument); |
// which main dimension was selected in the combo box? |
1102 |
|
gig::dimension_t mainDimension; |
1103 |
|
{ |
1104 |
|
Gtk::TreeModel::iterator iterType = m_comboDimType.get_active(); |
1105 |
|
if (!iterType) throw gig::Exception("No dimension selected"); |
1106 |
|
Gtk::TreeModel::Row rowType = *iterType; |
1107 |
|
if (!rowType) throw gig::Exception("Something is wrong regarding dimension selection"); |
1108 |
|
int iTypeID = rowType[m_comboDimsModel.m_type_id]; |
1109 |
|
mainDimension = static_cast<gig::dimension_t>(iTypeID); |
1110 |
|
} |
1111 |
|
|
1112 |
|
// now start the actual combination task ... |
1113 |
|
combineInstruments(instruments, m_gig, m_newCombinedInstrument, mainDimension); |
1114 |
} catch (RIFF::Exception e) {; |
} catch (RIFF::Exception e) {; |
1115 |
Gtk::MessageDialog msg(*this, e.Message, false, Gtk::MESSAGE_ERROR); |
Gtk::MessageDialog msg(*this, e.Message, false, Gtk::MESSAGE_ERROR); |
1116 |
msg.run(); |
msg.run(); |
1117 |
return; |
return; |
1118 |
|
} catch (...) { |
1119 |
|
Glib::ustring txt = _("An unknown exception occurred!"); |
1120 |
|
Gtk::MessageDialog msg(*this, txt, false, Gtk::MESSAGE_ERROR); |
1121 |
|
msg.run(); |
1122 |
|
return; |
1123 |
|
} |
1124 |
|
|
1125 |
|
if (!g_warnings.empty()) { |
1126 |
|
Glib::ustring txt = _( |
1127 |
|
"Combined instrument was created successfully, but there were warnings:" |
1128 |
|
); |
1129 |
|
txt += "\n\n"; |
1130 |
|
for (Warnings::const_iterator itWarn = g_warnings.begin(); |
1131 |
|
itWarn != g_warnings.end(); ++itWarn) |
1132 |
|
{ |
1133 |
|
txt += "-> " + *itWarn + "\n"; |
1134 |
|
} |
1135 |
|
txt += "\n"; |
1136 |
|
txt += _( |
1137 |
|
"You might also want to check the console for further warnings and " |
1138 |
|
"error messages." |
1139 |
|
); |
1140 |
|
Gtk::MessageDialog msg(*this, txt, false, Gtk::MESSAGE_WARNING); |
1141 |
|
msg.run(); |
1142 |
} |
} |
1143 |
|
|
1144 |
// no error occurred |
// no error occurred |
1149 |
void CombineInstrumentsDialog::onSelectionChanged() { |
void CombineInstrumentsDialog::onSelectionChanged() { |
1150 |
std::vector<Gtk::TreeModel::Path> v = m_treeView.get_selection()->get_selected_rows(); |
std::vector<Gtk::TreeModel::Path> v = m_treeView.get_selection()->get_selected_rows(); |
1151 |
m_OKButton.set_sensitive(v.size() >= 2); |
m_OKButton.set_sensitive(v.size() >= 2); |
1152 |
|
|
1153 |
|
typedef Gtk::TreeModel::Children Children; |
1154 |
|
|
1155 |
|
// update horizontal selection list (icon view) ... |
1156 |
|
|
1157 |
|
// remove items which are not part of the new selection anymore |
1158 |
|
{ |
1159 |
|
Children allOrdered = m_refOrderModel->children(); |
1160 |
|
for (Children::iterator itOrder = allOrdered.begin(); |
1161 |
|
itOrder != allOrdered.end(); ++itOrder) |
1162 |
|
{ |
1163 |
|
Gtk::TreeModel::Row rowOrder = *itOrder; |
1164 |
|
gig::Instrument* instr = rowOrder[m_orderColumns.m_col_instr]; |
1165 |
|
for (uint i = 0; i < v.size(); ++i) { |
1166 |
|
Gtk::TreeModel::iterator itSel = m_refTreeModel->get_iter(v[i]); |
1167 |
|
Gtk::TreeModel::Row rowSel = *itSel; |
1168 |
|
if (rowSel[m_columns.m_col_instr] == instr) |
1169 |
|
goto nextOrderedItem; |
1170 |
|
} |
1171 |
|
goto removeOrderedItem; |
1172 |
|
nextOrderedItem: |
1173 |
|
continue; |
1174 |
|
removeOrderedItem: |
1175 |
|
m_refOrderModel->erase(itOrder); |
1176 |
|
} |
1177 |
|
} |
1178 |
|
|
1179 |
|
// add items newly added to the selection |
1180 |
|
for (uint i = 0; i < v.size(); ++i) { |
1181 |
|
Gtk::TreeModel::iterator itSel = m_refTreeModel->get_iter(v[i]); |
1182 |
|
Gtk::TreeModel::Row rowSel = *itSel; |
1183 |
|
gig::Instrument* instr = rowSel[m_columns.m_col_instr]; |
1184 |
|
Children allOrdered = m_refOrderModel->children(); |
1185 |
|
for (Children::iterator itOrder = allOrdered.begin(); |
1186 |
|
itOrder != allOrdered.end(); ++itOrder) |
1187 |
|
{ |
1188 |
|
Gtk::TreeModel::Row rowOrder = *itOrder; |
1189 |
|
if (rowOrder[m_orderColumns.m_col_instr] == instr) |
1190 |
|
goto nextSelectionItem; |
1191 |
|
} |
1192 |
|
goto addNewSelectionItem; |
1193 |
|
nextSelectionItem: |
1194 |
|
continue; |
1195 |
|
addNewSelectionItem: |
1196 |
|
Glib::ustring name = gig_to_utf8(instr->pInfo->Name); |
1197 |
|
Gtk::TreeModel::iterator iterOrder = m_refOrderModel->append(); |
1198 |
|
Gtk::TreeModel::Row rowOrder = *iterOrder; |
1199 |
|
rowOrder[m_orderColumns.m_col_name] = name; |
1200 |
|
rowOrder[m_orderColumns.m_col_instr] = instr; |
1201 |
|
} |
1202 |
|
|
1203 |
|
// update markup |
1204 |
|
{ |
1205 |
|
int i = 0; |
1206 |
|
Children allOrdered = m_refOrderModel->children(); |
1207 |
|
for (Children::iterator itOrder = allOrdered.begin(); |
1208 |
|
itOrder != allOrdered.end(); ++itOrder, ++i) |
1209 |
|
{ |
1210 |
|
Gtk::TreeModel::Row rowOrder = *itOrder; |
1211 |
|
Glib::ustring name = rowOrder[m_orderColumns.m_col_name]; |
1212 |
|
Glib::ustring markup = |
1213 |
|
"<span foreground='black' background='white'>" + ToString(i+1) + ".</span>\n<span foreground='green' background='white'>" + name + "</span>"; |
1214 |
|
rowOrder[m_orderColumns.m_col_markup] = markup; |
1215 |
|
} |
1216 |
|
} |
1217 |
} |
} |
1218 |
|
|
1219 |
bool CombineInstrumentsDialog::fileWasChanged() const { |
bool CombineInstrumentsDialog::fileWasChanged() const { |