๐Ÿ“ฆ motiz88 / GR-55Floorboard

๐Ÿ“„ initPatchListMenu.cpp ยท 193 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193/****************************************************************************
**
** Copyright (C) 2007~2016 Colin Willcocks.
** Copyright (C) 2005~2007 Uco Mesdag. 
** All rights reserved.
** This file is part of "GR-55 FloorBoard".
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License along
** with this program; if not, write to the Free Software Foundation, Inc.,
** 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
**
****************************************************************************/

#include <QFileInfo>
#include <QFile>
#include <QDir>
#include <QRegExp>
#include "SysxIO.h"
#include "sysxWriter.h"
#include "Preferences.h"
#include "initPatchListMenu.h"

initPatchListMenu::initPatchListMenu(QRect geometry, QWidget *parent)
    : QWidget(parent)
{
	this->available = false;
	setInitPatchComboBox(geometry);
}

QDir initPatchListMenu::getInitPatchDir()
{
	Preferences *preferences = Preferences::Instance();
	QDir preferencesDir = QDir(preferences->getPreferences("General", "Files", "dir").remove(QRegExp("$(/)")));
    QString initPatchesDirName = "init_patches";
	QString symlinkExstention;

	#ifdef Q_OS_UNIX
		symlinkExstention = "";
	#endif

	#ifdef Q_OS_WIN
		symlinkExstention = ".lnk";
	#endif
	
    QDir initPatchesDir; /* The "init_patches" directory. */
	if ( QFileInfo( preferencesDir, initPatchesDirName + symlinkExstention ).exists() &&
		 QFileInfo( preferencesDir, initPatchesDirName + symlinkExstention ).canonicalPath() == 
		 QFileInfo( preferencesDir, initPatchesDirName + symlinkExstention ).symLinkTarget() )
	{	/* If the "Init Pathces" directory is a symlink and lives in the user sellected patch directory. */
		initPatchesDir.setPath(QFileInfo( preferencesDir, initPatchesDirName + symlinkExstention ).symLinkTarget());
	}
	else if ( QFileInfo( preferencesDir, initPatchesDirName ).exists() )
	{	/* If the "Init Pathces" directory lives in the user sellected patch directory. */
		initPatchesDir.setPath(QFileInfo( preferencesDir, initPatchesDirName ).absoluteFilePath() );
	}
	else if ( QFileInfo( initPatchesDirName ).exists() )
	{	/* If the "Init Pathces" directory lives in the application directory. */
		initPatchesDir.setPath(QFileInfo( initPatchesDirName ).absoluteFilePath());
		if( QFileInfo( preferencesDir, initPatchesDirName ).absolutePath() != initPatchesDir.absolutePath() && 
			preferencesDir.exists() )
		{	/* Add a symlink to the user selected patch directory (if it is set and exists). */
			QString symlinkPath = QFileInfo( preferencesDir, initPatchesDirName + symlinkExstention ).absoluteFilePath();
			QFile::link(initPatchesDir.absolutePath(), symlinkPath);
		};
	};

	return initPatchesDir;
}

void initPatchListMenu::setInitPatchComboBox(QRect geometry)
{
    Preferences *preferences = Preferences::Instance();
    bool ok;
    const double ratio = preferences->getPreferences("Window", "Scale", "ratio").toDouble(&ok);

	QDir initPatchesDir = getInitPatchDir();
	if(initPatchesDir.exists())
	{	/* If the "Init Pathces" directory exists. */
		QStringList filters;
                filters << "*.g5l" << "*.syx";
		QStringList initPatchesList = initPatchesDir.entryList(filters);

		if (initPatchesList.size() != 0)
		{	/* If it has "Init Pathces" in it. */
			this->initPatchComboBox = new customComboBox(this);
			this->available = true;
			this->initPatchComboBox->setObjectName("smallcombo");
            QFont Sfont( "Arial", 8*ratio, QFont::Bold);
            this->initPatchComboBox->setFont(Sfont);
            initPatchComboBox->addItem(tr("[ init_patches ]"));
			
			int itemcount;
			for(itemcount=0; itemcount<initPatchesList.size(); itemcount++)
			{	/* Filling the combobox with the patches. */
				QString path = initPatchesDir.absolutePath().append("/").append(initPatchesList.at(itemcount));
				this->initPatches.append(path);
				QString item = initPatchesList.at(itemcount);	// Start formatting the item name.
				item.remove(QRegExp("^[0-9_]+"));
                                item.remove(QRegExp(".{1}(g5l|syx)"));
				if(!item.contains("INIT_"))
				{
					item.prepend(tr("(My INIT) "));
				};
				item.remove("INIT_");
				item.replace("_", " ");
				item.replace("-!-", "/");
				initPatchComboBox->addItem(item);				// Finished formatting the item name.
			};	

			initPatchComboBox->setGeometry(geometry);
			initPatchComboBox->setEditable(false);
			initPatchComboBox->setFrame(false);
            initPatchComboBox->setMaxVisibleItems(itemcount + 1); // +1 for "[ init_patches ]" entry.

			QObject::connect(initPatchComboBox, SIGNAL(currentIndexChanged(int)),
					this, SLOT(loadInitPatch(int)));

            QObject::connect(initPatchComboBox, SIGNAL(highlighted(int)),
                    this, SLOT(highLightInitPatch(int)));

			QObject::connect(this, SIGNAL(updateSignal()),
				this->parent()->parent(), SIGNAL(updateSignal()));
		};
	};
}


void initPatchListMenu::setIndex(int index)
{
	if(this->available)
	{
		this->initPatchComboBox->setCurrentIndex(index);
	};
}

void initPatchListMenu::loadInitPatch(int index)
{
	if(index > 0)
	{
		QString fileName = this->initPatches.at(index - 1 );
		if (!fileName.isEmpty())	
		{
			sysxWriter file;
			file.setFile(fileName);  
			if(file.readFile())
			{	
				// DO SOMETHING AFTER READING THE FILE (UPDATE THE GUI)
				SysxIO *sysxIO = SysxIO::Instance();
                sysxIO->setFileSource("Structure", file.getFileSource());
				sysxIO->setFileName(tr("init patch"));
				sysxIO->setSyncStatus(false);
				sysxIO->setDevice(false);
                if(sysxIO->isConnected()) { sysxIO->writeToBuffer(); };
                emit updateSignal();
			};
		};
	};
}

void initPatchListMenu::highLightInitPatch(int index)
{
  /*  if(index > 0)
    {
        QString fileName = this->initPatches.at(index - 1 );
        if (!fileName.isEmpty())
        {
            sysxWriter file;
            file.setFile(fileName);
            if(file.readFile())
            {
                // DO SOMETHING AFTER READING THE FILE (UPDATE THE GUI)
                SysxIO *sysxIO = SysxIO::Instance();
                QString area = "Structure";
                sysxIO->setFileSource(area, file.getFileSource());
                sysxIO->setFileName(tr("init patch"));
                sysxIO->setSyncStatus(false);
                sysxIO->setDevice(false);
                emit updateSignal();
            };
        };
    };  */
}