Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove temp sequence list read / write methods #1269

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
119 changes: 0 additions & 119 deletions src/core/Basics/Song.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -309,125 +309,6 @@ void Song::clearMissingSamples() {
}
}

void Song::readTempPatternList( const QString& sFilename )
{
XMLDoc doc;
if( !doc.read( sFilename ) ) {
return;
}
XMLNode root = doc.firstChildElement( "sequence" );
if ( root.isNull() ) {
ERRORLOG( "sequence node not found" );
return;
}

XMLNode virtualsNode = root.firstChildElement( "virtuals" );
if ( !virtualsNode.isNull() ) {
XMLNode virtualNode = virtualsNode.firstChildElement( "virtual" );
while ( !virtualNode.isNull() ) {
QString patternName = virtualNode.read_attribute( "pattern", nullptr, false, false );
XMLNode patternNode = virtualNode.firstChildElement( "pattern" );
Pattern* pPattern = nullptr;
while ( !patternName.isEmpty() && !patternNode.isNull() ) {
QString virtualName = patternNode.read_text( false );
if ( !virtualName.isEmpty() ) {
Pattern* pVirtualPattern = nullptr;
for ( unsigned i = 0; i < getPatternList()->size(); i++ ) {
Pattern* pat = getPatternList()->get( i );
if ( pPattern == nullptr && pat->get_name() == patternName ) {
pPattern = pat;
}
if ( pVirtualPattern == nullptr && pat->get_name() == virtualName ) {
pVirtualPattern = pat;
}
if ( pPattern != nullptr && pVirtualPattern != nullptr) {
break;
}
}
if ( pPattern == nullptr ) {
ERRORLOG( QString( "Invalid pattern name %1" ).arg( patternName ) );
}
if ( pVirtualPattern == nullptr ) {
ERRORLOG( QString( "Invalid virtual pattern name %1" ).arg( virtualName ) );
}
if ( pPattern != nullptr && pVirtualPattern != nullptr ) {
pPattern->virtual_patterns_add( pVirtualPattern );
}
}
patternNode = patternNode.nextSiblingElement( "pattern" );
}
virtualNode = virtualNode.nextSiblingElement( "virtual" );
}
} else {
WARNINGLOG( "no virtuals node not found" );
}

getPatternList()->flattened_virtual_patterns_compute();
getPatternGroupVector()->clear();

XMLNode sequenceNode = root.firstChildElement( "groups" );
if ( !sequenceNode.isNull() ) {
XMLNode groupNode = sequenceNode.firstChildElement( "group" );
while ( !groupNode.isNull() ) {
PatternList* patternSequence = new PatternList();
XMLNode patternNode = groupNode.firstChildElement( "pattern" );
while ( !patternNode.isNull() ) {
QString patternName = patternNode.read_text( false );
if( !patternName.isEmpty() ) {
Pattern* p = nullptr;
for ( unsigned i = 0; i < getPatternList()->size(); i++ ) {
Pattern* pat = getPatternList()->get( i );
if ( pat->get_name() == patternName ) {
p = pat;
break;
}
}
if ( p == nullptr ) {
ERRORLOG( QString( "Invalid pattern name %1" ).arg( patternName ) );
} else {
patternSequence->add( p );
}
}
patternNode = patternNode.nextSiblingElement( "pattern" );
}
getPatternGroupVector()->push_back( patternSequence );
groupNode = groupNode.nextSiblingElement( "group" );
}
} else {
WARNINGLOG( "no sequence node not found" );
}
}

bool Song::writeTempPatternList( const QString& sFilename )
{
XMLDoc doc;
XMLNode root = doc.set_root( "sequence" );

XMLNode virtualPatternListNode = root.createNode( "virtuals" );
for ( unsigned i = 0; i < getPatternList()->size(); i++ ) {
Pattern *pPattern = getPatternList()->get( i );
if ( !pPattern->get_virtual_patterns()->empty() ) {
XMLNode node = virtualPatternListNode.createNode( "virtual" );
node.write_attribute( "pattern", pPattern->get_name() );
for ( Pattern::virtual_patterns_it_t virtIter = pPattern->get_virtual_patterns()->begin(); virtIter != pPattern->get_virtual_patterns()->end(); ++virtIter ) {
node.write_string( "pattern", (*virtIter)->get_name() );
}
}
}

XMLNode patternSequenceNode = root.createNode( "groups" );
for ( unsigned i = 0; i < getPatternGroupVector()->size(); i++ ) {
XMLNode node = patternSequenceNode.createNode( "group" );
PatternList *pList = ( *getPatternGroupVector() )[i];
for ( unsigned j = 0; j < pList->size(); j++ ) {
Pattern *pPattern = pList->get( j );
node.write_string( "pattern", pPattern->get_name() );
}
}

return doc.write( sFilename );
}

QString Song::copyInstrumentLineToString( int nSelectedPattern, int nSelectedInstrument )
{
Instrument *pInstr = getInstrumentList()->get( nSelectedInstrument );
Expand Down
3 changes: 0 additions & 3 deletions src/core/Basics/Song.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,6 @@ class Song : public H2Core::Object

DrumkitComponent* getComponent( int nID ) const;

void readTempPatternList( const QString& sFilename );
bool writeTempPatternList( const QString& sFilename );

QString copyInstrumentLineToString( int nSelectedPattern, int selectedInstrument );
bool pasteInstrumentLineFromString( const QString& sSerialized, int nSelectedPattern, int nSelectedInstrument, std::list<Pattern *>& pPatterns );

Expand Down
91 changes: 41 additions & 50 deletions src/gui/src/SongEditor/SongEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1126,31 +1126,6 @@ QRect SongEditor::getKeyboardCursorRect() {
QSize( m_nGridWidth, m_nGridHeight -1 ) );
}

void SongEditor::clearThePatternSequenceVector( QString filename )
{
Hydrogen *engine = Hydrogen::get_instance();

m_pAudioEngine->lock( RIGHT_HERE );

Song *song = engine->getSong();

//before deleting the sequence, write a temp sequence file to disk
song->writeTempPatternList( filename );

std::vector<PatternList*> *pPatternGroupsVect = song->getPatternGroupVector();
for (uint i = 0; i < pPatternGroupsVect->size(); i++) {
PatternList *pPatternList = (*pPatternGroupsVect)[i];
pPatternList->clear();
delete pPatternList;
}
pPatternGroupsVect->clear();

song->setIsModified( true );
m_pAudioEngine->unlock();
m_bSequenceChanged = true;
update();
}

void SongEditor::updateEditorandSetTrue()
{
Hydrogen::get_instance()->getSong()->setIsModified( true );
Expand Down Expand Up @@ -1542,8 +1517,8 @@ void SongEditorPatternList::patternPopup_load()
{
Hydrogen *engine = Hydrogen::get_instance();
int nSelectedPattern = engine->getSelectedPatternNumber();
Song *song = engine->getSong();
Pattern *pattern = song->getPatternList()->get( nSelectedPattern );
Song *pSong = engine->getSong();
Pattern *pPattern = pSong->getPatternList()->get( nSelectedPattern );

QFileDialog fd(this);
fd.setFileMode( QFileDialog::ExistingFile );
Expand All @@ -1556,19 +1531,22 @@ void SongEditorPatternList::patternPopup_load()
}
QString patternPath = fd.selectedFiles().first();

QString prevPatternPath = Files::savePatternTmp( pattern->get_name(), pattern, song, engine->getCurrentDrumkitName() );
QString prevPatternPath = Files::savePatternTmp( pPattern->get_name(), pPattern, pSong, engine->getCurrentDrumkitName() );
if ( prevPatternPath.isEmpty() ) {
QMessageBox::warning( this, "Hydrogen", tr("Could not save pattern to temporary directory.") );
return;
}
LocalFileMng fileMng;
QString sequencePath = Filesystem::tmp_file_path( "SEQ.xml" );
if ( !song->writeTempPatternList( sequencePath ) ) {
QMessageBox::warning( this, "Hydrogen", tr("Could not export sequence.") );
return;

std::vector< QPoint > deleteCells;
auto pColumns = pSong->getPatternGroupVector();
for ( int nColumn = 0; nColumn < pColumns->size(); nColumn++ ) {
PatternList *pColumn = ( *pColumns )[ nColumn ];
if ( pColumn->index( pPattern ) != -1 ) {
deleteCells.push_back( QPoint( nColumn, nSelectedPattern ) );
}
}

SE_loadPatternAction *action = new SE_loadPatternAction( patternPath, prevPatternPath, sequencePath, nSelectedPattern, false );
SE_loadPatternAction *action = new SE_loadPatternAction( patternPath, prevPatternPath, deleteCells, nSelectedPattern, false );
HydrogenApp *hydrogenApp = HydrogenApp::get_instance();
hydrogenApp->m_pUndoStack->push( action );
}
Expand Down Expand Up @@ -1689,29 +1667,30 @@ void SongEditorPatternList::revertPatternPropertiesDialogSettings(QString oldPat
void SongEditorPatternList::patternPopup_delete()
{
Song *pSong = m_pHydrogen->getSong();
int patternPosition = m_pHydrogen->getSelectedPatternNumber();
Pattern *pattern = pSong->getPatternList()->get( patternPosition );
int nPatternPosition = m_pHydrogen->getSelectedPatternNumber();
Pattern *pPattern = pSong->getPatternList()->get( nPatternPosition );

QString patternPath = Files::savePatternTmp( pattern->get_name(), pattern, pSong, m_pHydrogen->getCurrentDrumkitName() );
if ( patternPath.isEmpty() ) {
QString sPatternPath = Files::savePatternTmp( pPattern->get_name(), pPattern, pSong, m_pHydrogen->getCurrentDrumkitName() );
if ( sPatternPath.isEmpty() ) {
QMessageBox::warning( this, "Hydrogen", tr("Could not save pattern to temporary directory.") );
return;
}
LocalFileMng fileMng;
QString sequencePath = Filesystem::tmp_file_path( "SEQ.xml" );
if ( !pSong->writeTempPatternList( sequencePath ) ) {
QMessageBox::warning( this, "Hydrogen", tr("Could not export sequence.") );
return;
}

SE_deletePatternFromListAction *action = new SE_deletePatternFromListAction( patternPath , sequencePath, patternPosition );
HydrogenApp *hydrogenApp = HydrogenApp::get_instance();
hydrogenApp->m_pUndoStack->push( action );
std::vector< QPoint > deleteCells;
auto pColumns = pSong->getPatternGroupVector();
for ( int nColumn = 0; nColumn < pColumns->size(); nColumn++ ) {
PatternList *pColumn = ( *pColumns )[ nColumn ];
if ( pColumn->index( pPattern ) != -1 ) {
deleteCells.push_back( QPoint( nColumn, nPatternPosition ) );
}
}
HydrogenApp::get_instance()->m_pUndoStack
->push( new SE_deletePatternFromListAction( sPatternPath, deleteCells, nPatternPosition ) );

}


void SongEditorPatternList::deletePatternFromList( QString patternFilename, QString sequenceFileName, int patternPosition )
void SongEditorPatternList::deletePatternFromList( QString patternFilename, int patternPosition )
{
if ( m_pHydrogen->getSong()->getMode() == Song::PATTERN_MODE ) {
m_pHydrogen->sequencer_setNextPattern( -1 );
Expand Down Expand Up @@ -1781,7 +1760,7 @@ void SongEditorPatternList::deletePatternFromList( QString patternFilename, QStr

}

void SongEditorPatternList::restoreDeletedPatternsFromList( QString patternFilename, QString sequenceFileName, int patternPosition )
void SongEditorPatternList::restoreDeletedPatternsFromList( QString patternFilename, int patternPosition )
{
Hydrogen *engine = Hydrogen::get_instance();
Song *pSong = engine->getSong();
Expand Down Expand Up @@ -2027,7 +2006,19 @@ void SongEditorPatternList::dropEvent(QDropEvent *event)
QString sequenceFilename = Filesystem::tmp_file_path( "SEQ.xml" );
bool drag = false;
if( QString( tokens.at(0) ).contains( "drag pattern" )) drag = true;
SE_loadPatternAction *pAction = new SE_loadPatternAction( sPatternName, oldPatternName, sequenceFilename, nTargetPattern, drag );

std::vector< QPoint > deleteCells;
if ( ! drag ) {
auto pColumns = pSong->getPatternGroupVector();
for ( int nColumn = 0; nColumn < pColumns->size(); nColumn++ ) {
PatternList *pColumn = ( *pColumns )[ nColumn ];
if ( pColumn->index( pPattern ) != -1 ) {
deleteCells.push_back( QPoint( nColumn, nTargetPattern ) );
}
}
}

SE_loadPatternAction *pAction = new SE_loadPatternAction( sPatternName, oldPatternName, deleteCells, nTargetPattern, drag );

pHydrogenApp->m_pUndoStack->push( pAction );
}
Expand Down
4 changes: 2 additions & 2 deletions src/gui/src/SongEditor/SongEditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -246,8 +246,8 @@ class SongEditorPatternList : public QWidget, public H2Core::Object, public Even
void updateEditor();
void createBackground();
void movePatternLine( int, int );
void deletePatternFromList( QString patternFilename, QString sequenceFileName, int patternPosition );
void restoreDeletedPatternsFromList( QString patternFilename, QString sequenceFileName, int patternPosition );
void deletePatternFromList( QString patternFilename, int patternPosition );
void restoreDeletedPatternsFromList( QString patternFilename, int patternPosition );
void acceptPatternPropertiesDialogSettings( QString newPatternName, QString newPatternInfo, QString newPatternCategory, int patternNr );
void revertPatternPropertiesDialogSettings(QString oldPatternName, QString oldPatternInfo, QString oldPatternCategory, int patternNr);
void loadPatternAction( QString filename, int position);
Expand Down
35 changes: 14 additions & 21 deletions src/gui/src/SongEditor/SongEditorPanel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -662,32 +662,25 @@ void SongEditorPanel::clearSequence( Button* btn)
if ( res == 1 ) {
return;
}

QString filename = Filesystem::tmp_file_path( "SEQ.xml" );
SE_deletePatternSequenceAction *pAction = new SE_deletePatternSequenceAction( filename );
HydrogenApp *pH2App = HydrogenApp::get_instance();

pH2App->m_pUndoStack->push( pAction );
}


void SongEditorPanel::restoreGroupVector( QString filename )
{
//clear the old sequese
std::vector<PatternList*> *pPatternGroupsVect = Hydrogen::get_instance()->getSong()->getPatternGroupVector();
for (uint i = 0; i < pPatternGroupsVect->size(); i++) {
PatternList *pPatternList = (*pPatternGroupsVect)[i];
pPatternList->clear();
delete pPatternList;
// Find and delete all pattern cells.
std::vector< QPoint > deleteCells, mergeCells, addCells;
Song *pSong = Hydrogen::get_instance()->getSong();
std::vector< PatternList* > *pColumns = pSong->getPatternGroupVector();
PatternList *pPatternList = pSong->getPatternList();
for ( int nColumn = 0 ; nColumn < pColumns->size(); nColumn++ ) {
PatternList *pColumn = pColumns->at( nColumn );
for ( int n = 0; n < pColumn->size(); n++ ) {
deleteCells.push_back( QPoint( nColumn, pPatternList->index( pColumn->get( n ) ) ) );
}
}
pPatternGroupsVect->clear();

Hydrogen::get_instance()->getSong()->readTempPatternList( filename );
m_pSongEditor->updateEditorandSetTrue();
updateAll();
HydrogenApp *pH2App = HydrogenApp::get_instance();
pH2App->m_pUndoStack->push( new SE_modifyPatternCellsAction( addCells, deleteCells, mergeCells,
tr( "Delete %1 pattern cells" )
.arg( deleteCells.size() ) ) );
}


void SongEditorPanel::resyncExternalScrollBar()
{
int nGridHeight = m_pPatternList->getGridHeight();
Expand Down
1 change: 0 additions & 1 deletion src/gui/src/SongEditor/SongEditorPanel.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ class SongEditorPanel : public QWidget, public EventListener, public H2Core::Obj

// Implements EventListener interface
virtual void selectedPatternChangedEvent() override;
void restoreGroupVector( QString filename );
//~ Implements EventListener interface
///< an empty new pattern will be added to pattern list at idx
void insertPattern( int idx, H2Core::Pattern* pPattern );
Expand Down
Loading