hydrogen 1.2.3
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-2024 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
32using namespace H2Core;
33
35 std::shared_ptr<Drumkit> pDrumkit )
36 : QDialog( pParent ),
37 m_pDrumkit( pDrumkit )
38{
39 auto pCommonStrings = HydrogenApp::get_instance()->getCommonStrings();
40
41 setupUi( this );
42
43 exportBtn->setText( tr( "&Export" ) );
44 cancelBtn->setText( pCommonStrings->getButtonCancel() );
45
46 setWindowTitle( QString( "%1 [%2]" )
47 .arg( tr( "Export Drumkit" ) )
48 .arg( pDrumkit != nullptr ? pDrumkit->get_name() : tr( "invalid drumkit" ) ) );
49 adjustSize();
50 setFixedSize( width(), height() );
51 drumkitPathTxt->setText( Preferences::get_instance()->getLastExportDrumkitDirectory() );
52
53 if ( pDrumkit != nullptr ) {
54 for ( const auto& pComponent : *pDrumkit->get_components() ) {
55 m_components.append( pComponent->get_name() );
56 }
57
59 }
60}
61
65
66
67
69{
70 if ( m_pDrumkit == nullptr ) {
71 ERRORLOG( "Invalid drumkit" );
72 return;
73 }
74
75 auto pCommonStrings = HydrogenApp::get_instance()->getCommonStrings();
76
77 if ( ! Filesystem::dir_writable( drumkitPathTxt->text(), false ) ) {
78 QMessageBox::warning( this, "Hydrogen",
79 pCommonStrings->getFileDialogMissingWritePermissions(),
80 QMessageBox::Ok );
81 return;
82 }
83
84
86 ERRORLOG( "User cancelled dialog due to licensing issues." );
87 return;
88 }
89
90 bool bRecentVersion = versionList->currentIndex() == 1 ? false : true;
91
92 QString sTargetComponent;
93 if ( componentList->currentIndex() == 0 && bRecentVersion ) {
94 // Exporting all components
95 sTargetComponent = "";
96 } else {
97 sTargetComponent = componentList->currentText();
98 }
99
100 // Check whether the resulting file does already exist and ask the
101 // user if it should be overwritten.
102 QString sTargetName = drumkitPathTxt->text() + "/" +
103 m_pDrumkit->getExportName( sTargetComponent, bRecentVersion ) +
105
106 if ( Filesystem::file_exists( sTargetName, true ) ) {
107 QMessageBox msgBox;
108 msgBox.setWindowTitle("Hydrogen");
109 msgBox.setIcon( QMessageBox::Warning );
110 msgBox.setText( tr( "The file [%1] does already exist and will be overwritten.")
111 .arg( sTargetName ) );
112
113 msgBox.setStandardButtons( QMessageBox::Ok | QMessageBox::Cancel );
114 msgBox.setButtonText(QMessageBox::Ok,
115 pCommonStrings->getButtonOk() );
116 msgBox.setButtonText(QMessageBox::Cancel,
117 pCommonStrings->getButtonCancel());
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 QMessageBox::critical( this, "Hydrogen", tr("Unable to export drumkit") );
132 return;
133 }
134
135 QApplication::restoreOverrideCursor();
136 QMessageBox::information( this, "Hydrogen",
137 tr("Drumkit exported to") + "\n" +
138 sTargetName );
139}
140
142{
143 QString path = drumkitPathTxt->text();
144 if (path.isEmpty()) {
145 exportBtn->setEnabled( false );
146 }
147 else {
148 exportBtn->setEnabled( true );
149 }
150}
151
153{
155 if ( ! Filesystem::dir_writable( sPath, false ) ){
156 sPath = QDir::homePath();
157 }
158
159 FileDialog fd(this);
160 fd.setFileMode( QFileDialog::Directory );
161 fd.setAcceptMode( QFileDialog::AcceptOpen );
162 fd.setDirectory( sPath );
163 fd.setWindowTitle( tr("Directory") );
164
165 if ( fd.exec() == QDialog::Accepted ) {
166 QString sFilename = fd.selectedFiles().first();
167 if ( sFilename.isEmpty() ) {
168 drumkitPathTxt->setText( sPath );
169 } else {
170 drumkitPathTxt->setText( sFilename );
172 }
173 }
174}
175
180
185
187{
188 componentList->clear();
189
190 if ( versionList->currentIndex() == 0 ) {
191 // Only kit version 0.9.7 or newer support components. For
192 // them we can support to export all or individual ones. For
193 // older versions one component must pretend to be the whole
194 // kit.
195 componentList->addItem( tr( "All" ) );
196 componentList->insertSeparator( 1 );
197 }
198
199 for ( const auto& sComponentName : m_components ) {
200 componentList->addItem( sComponentName );
201 }
202}
#define ERRORLOG(x)
Definition Object.h:239
Custom file dialog checking whether the user has write access to the selected folder before allowing ...
Definition FileDialog.h:33
QString getExportName(const QString &sComponentName, bool bRecentVersion) const
Returns the base name used when exporting the drumkit.
Definition Drumkit.cpp:307
bool exportTo(const QString &sTargetDir, const QString &sComponentName="", bool bRecentVersion=true, bool bSilent=false)
Compresses the drumkit into a .h2drumkit file.
Definition Drumkit.cpp:718
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:91
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