hydrogen 1.2.6
SoundLibraryExportDialog.cpp
Go to the documentation of this file.
1/*
2 * Hydrogen
3 * Copyright(c) 2002-2008 by Alex >Comix< Cominu [comix@users.sourceforge.net]
4 * Copyright(c) 2008-2025 The hydrogen development team [hydrogen-devel@lists.sourceforge.net]
5 *
6 * http://www.hydrogen-music.org
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY, without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see https://www.gnu.org/licenses
20 *
21 */
22
24#include "../HydrogenApp.h"
25#include "../CommonStrings.h"
27
31
32#ifndef H2CORE_HAVE_QT6
33 #include <QTextCodec>
34#endif
35
36using namespace H2Core;
37
39 std::shared_ptr<Drumkit> pDrumkit )
40 : QDialog( pParent ),
41 m_pDrumkit( pDrumkit )
42{
43 auto pCommonStrings = HydrogenApp::get_instance()->getCommonStrings();
44
45 setupUi( this );
46
47 exportBtn->setText( tr( "&Export" ) );
48 cancelBtn->setText( pCommonStrings->getButtonCancel() );
49
50 setWindowTitle( QString( "%1 [%2]" )
51 .arg( tr( "Export Drumkit" ) )
52 .arg( pDrumkit != nullptr ? pDrumkit->get_name() : tr( "invalid drumkit" ) ) );
53 adjustSize();
54 setFixedSize( width(), height() );
55 drumkitPathTxt->setText( Preferences::get_instance()->getLastExportDrumkitDirectory() );
56
57 if ( pDrumkit != nullptr ) {
58 for ( const auto& pComponent : *pDrumkit->get_components() ) {
59 m_components.append( pComponent->get_name() );
60 }
61
63 }
64}
65
69
70
71
73{
74 if ( m_pDrumkit == nullptr ) {
75 ERRORLOG( "Invalid drumkit" );
76 return;
77 }
78
79 auto pCommonStrings = HydrogenApp::get_instance()->getCommonStrings();
80
81 if ( ! Filesystem::dir_writable( drumkitPathTxt->text(), false ) ) {
82 QMessageBox::warning( this, "Hydrogen",
83 pCommonStrings->getFileDialogMissingWritePermissions(),
84 QMessageBox::Ok );
85 return;
86 }
87
88
90 ERRORLOG( "User cancelled dialog due to licensing issues." );
91 return;
92 }
93
94 bool bRecentVersion = versionList->currentIndex() == 1 ? false : true;
95
96 QString sTargetComponent;
97 if ( componentList->currentIndex() == 0 && bRecentVersion ) {
98 // Exporting all components
99 sTargetComponent = "";
100 } else {
101 sTargetComponent = componentList->currentText();
102 }
103
104 // Check whether the resulting file does already exist and ask the
105 // user if it should be overwritten.
106 QString sTargetName = drumkitPathTxt->text() + "/" +
107 m_pDrumkit->getExportName( sTargetComponent, bRecentVersion ) +
109
110 if ( Filesystem::file_exists( sTargetName, true ) ) {
111 QMessageBox msgBox;
112 msgBox.setWindowTitle("Hydrogen");
113 msgBox.setIcon( QMessageBox::Warning );
114 msgBox.setText( tr( "The file [%1] does already exist and will be overwritten.")
115 .arg( sTargetName ) );
116
117 msgBox.setStandardButtons( QMessageBox::Ok | QMessageBox::Cancel );
118 msgBox.setDefaultButton(QMessageBox::Ok);
119
120 if ( msgBox.exec() == QMessageBox::Cancel ) {
121 return;
122 }
123 }
124
125 QApplication::setOverrideCursor(Qt::WaitCursor);
126
127 if ( ! m_pDrumkit->exportTo( drumkitPathTxt->text(), // Target folder
128 sTargetComponent, // Selected component
129 bRecentVersion ) ) {
130 QApplication::restoreOverrideCursor();
131 QString sError = tr( "Unable to export drumkit" );
132
133#ifndef H2CORE_HAVE_QT6
134 // Check whether encoding might be the problem in here.
135 auto pCodec = QTextCodec::codecForLocale();
136 if ( ! pCodec->canEncode( drumkitPathTxt->text() ) ) {
137 QMessageBox::critical(
138 this, "Hydrogen", QString( "%1\n\n%2\n\n%3: [%4]" )
139 .arg( sError ).arg( drumkitPathTxt->text() )
140 .arg( pCommonStrings->getEncodingError() )
141 .arg( QString( pCodec->name() ) ) );
142 }
143 else {
144 QMessageBox::critical( this, "Hydrogen", sError );
145 }
146#else
147 QMessageBox::critical( this, "Hydrogen", sError );
148#endif
149
150 return;
151 }
152
153 QApplication::restoreOverrideCursor();
154 QMessageBox::information( this, "Hydrogen",
155 tr("Drumkit exported to") + "\n" +
156 sTargetName );
157}
158
160{
161 QString path = drumkitPathTxt->text();
162 if (path.isEmpty()) {
163 exportBtn->setEnabled( false );
164 }
165 else {
166 exportBtn->setEnabled( true );
167 }
168}
169
171{
173 if ( ! Filesystem::dir_writable( sPath, false ) ){
174 sPath = QDir::homePath();
175 }
176
177 FileDialog fd(this);
178 fd.setFileMode( QFileDialog::Directory );
179 fd.setAcceptMode( QFileDialog::AcceptOpen );
180 fd.setDirectory( sPath );
181 fd.setWindowTitle( tr("Directory") );
182
183 if ( fd.exec() == QDialog::Accepted ) {
184 QString sFilename = fd.selectedFiles().first();
185 if ( sFilename.isEmpty() ) {
186 drumkitPathTxt->setText( sPath );
187 } else {
188 drumkitPathTxt->setText( sFilename );
190 }
191 }
192}
193
198
203
205{
206 componentList->clear();
207
208 if ( versionList->currentIndex() == 0 ) {
209 // Only kit version 0.9.7 or newer support components. For
210 // them we can support to export all or individual ones. For
211 // older versions one component must pretend to be the whole
212 // kit.
213 componentList->addItem( tr( "All" ) );
214 componentList->insertSeparator( 1 );
215 }
216
217 for ( const auto& sComponentName : m_components ) {
218 componentList->addItem( sComponentName );
219 }
220}
#define ERRORLOG(x)
Definition Object.h:242
Custom file dialog checking whether the user has write access to the selected folder before allowing ...
Definition FileDialog.h:34
static bool dir_writable(const QString &path, bool silent=false)
returns true if the given path is a writable regular directory
static bool file_exists(const QString &path, bool silent=false)
returns true if the given path is an existing regular file
static const QString drumkit_ext
Definition Filesystem.h:121
static Preferences * get_instance()
Returns a pointer to the current Preferences singleton stored in __instance.
void setLastExportDrumkitDirectory(QString sPath)
QString getLastExportDrumkitDirectory() const
static HydrogenApp * get_instance()
Returns the instance of HydrogenApp class.
std::shared_ptr< CommonStrings > getCommonStrings()
static bool checkDrumkitLicense(std::shared_ptr< H2Core::Drumkit > pDrumkit)
void on_drumkitPathTxt_textChanged(QString str)
SoundLibraryExportDialog(QWidget *pParent, std::shared_ptr< H2Core::Drumkit > pDrumkit)
std::shared_ptr< H2Core::Drumkit > m_pDrumkit