add mp3 and ogg torrent url info to JamendoAlbum
[amarok.git] / src / equalizerpresetmanager.cpp
blobbc0c3bbbb858ae86fac76664e7e2b88ac1a85f70
1 /***************************************************************************
2 * Copyright (C) 2005 by Markus Brueffer <markus@brueffer.de> *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
18 ***************************************************************************/
20 #include "equalizerpresetmanager.h"
22 #include <k3listview.h>
23 #include <KApplication>
24 #include <KInputDialog>
25 #include <KLocale>
26 #include <KMessageBox>
27 #include <KStandardDirs> //locate()
28 #include <KVBox>
30 #include <QDomDocument>
31 #include <QDomElement>
32 #include <QDomNode>
33 #include <QFile>
34 #include <QLayout>
35 #include <QPushButton>
36 #include <QTextStream>
38 EqualizerPresetManager::EqualizerPresetManager( QWidget *parent, const char *name )
39 : KDialog( parent )
41 setCaption( i18n("Presets") );
42 setModal( true );
43 setButtons( Ok | Cancel | Default );
44 setDefaultButton( Ok );
45 showButtonSeparator( true );
47 QWidget *mainWidget = new QWidget( this );
48 setMainWidget( mainWidget );
49 QHBoxLayout *mainLayout = new QHBoxLayout( mainWidget, 0, spacingHint() );
51 m_presetsView = new K3ListView( mainWidget );
52 m_presetsView->setObjectName( "presetListView" );
53 m_presetsView->addColumn( i18n( "Presets" ) );
54 m_presetsView->setFullWidth( true );
55 connect(m_presetsView, SIGNAL( selectionChanged() ), SLOT( updateButtonState() ));
56 connect(m_presetsView, SIGNAL( doubleClicked ( Q3ListViewItem*, const QPoint&, int ) ), SLOT( slotRename() ));
57 mainLayout->addWidget( m_presetsView );
59 QVBoxLayout* buttonsLayout = new QVBoxLayout( mainLayout );
61 m_renameBtn = new QPushButton( i18n("&Rename"), mainWidget, "renameBtn" );
62 m_deleteBtn = new QPushButton( i18n("&Delete"), mainWidget, "deleteBtn" );
64 buttonsLayout->addWidget( m_renameBtn );
65 buttonsLayout->addWidget( m_deleteBtn );
67 connect(m_renameBtn, SIGNAL( clicked() ), SLOT( slotRename() ));
68 connect(m_deleteBtn, SIGNAL( clicked() ), SLOT( slotDelete() ));
69 connect(this, SIGNAL( defaultClicked() ), SLOT( slotDefault() ));
71 QSpacerItem* spacer = new QSpacerItem( 20, 40, QSizePolicy::Minimum, QSizePolicy::Expanding );
72 buttonsLayout->addItem( spacer );
74 updateButtonState();
76 resize( QSize(300, 250).expandedTo(minimumSizeHint()) );
80 EqualizerPresetManager::~EqualizerPresetManager()
84 void
85 EqualizerPresetManager::setPresets(QMap< QString, QList<int> > presets)
87 if ( presets.empty() )
88 return;
90 m_presets = presets;
91 m_presetsView->clear();
93 QMap< QString, QList<int> >::Iterator end = presets.end();
94 for ( QMap< QString, QList<int> >::Iterator it = presets.begin(); it != end; ++it )
95 if ( it.key() != i18n( "Zero" ) && it.key() != i18n( "Manual" ) ) // Don't add 'Manual' and 'Zero'
96 new K3ListViewItem( m_presetsView, it.key() );
99 QMap< QString, QList<int> >
100 EqualizerPresetManager::presets()
102 return m_presets;
105 void
106 EqualizerPresetManager::slotRename()
108 bool ok;
109 Q3ListViewItem* item = m_presetsView->selectedItem();
110 const QString title = KInputDialog::getText( i18n("Rename Equalizer Preset"),
111 i18n("Enter new preset name:"), item->text(0), &ok, this);
113 if ( ok && item->text(0) != title ) {
114 // Check if the new preset title exists
115 if ( m_presets.find( title ) != m_presets.end() ) {
116 int button = KMessageBox::warningYesNo( this, i18n( "A preset with the name %1 already exists. Overwrite?", title ) );
118 if ( button != KMessageBox::Yes )
119 return;
122 m_presets[ title ] = m_presets[ item->text(0)];
123 m_presets.remove( item->text(0) );
124 item->setText(0, title);
128 void
129 EqualizerPresetManager::slotDefault()
131 int button = KMessageBox::warningYesNo( this, i18n( "All presets will be deleted and defaults will be restored. Are you sure?" ) );
133 if ( button != KMessageBox::Yes )
134 return;
136 // Preserve the 'Manual' preset
137 QList<int> manualGains = m_presets[ i18n("Manual") ];
139 // Delete all presets
140 m_presets.clear();
142 // Create predefined presets 'Zero' and 'Manual'
143 QList<int> zeroGains;
144 zeroGains << 0 << 0 << 0 << 0 << 0 << 0 << 0 << 0 << 0 << 0;
145 m_presets[ i18n("Zero") ] = zeroGains;
146 m_presets[ i18n("Manual") ] = manualGains;
148 // Load the default presets
149 QFile file( KStandardDirs::locate( "data", "amarok/data/equalizer_presets.xml" ) );
151 QTextStream stream( &file );
152 stream.setCodec( "UTF8" );
154 QDomDocument d;
156 if( !file.open( QIODevice::ReadOnly ) || !d.setContent( stream.read() ) )
157 return;
159 QDomNode n = d.namedItem( "equalizerpresets" ).namedItem("preset");
161 for( ; !n.isNull(); n = n.nextSibling() )
163 QDomElement e = n.toElement();
164 QString title = e.attribute( "name" );
166 QList<int> gains;
167 gains << e.namedItem( "b0" ).toElement().text().toInt();
168 gains << e.namedItem( "b1" ).toElement().text().toInt();
169 gains << e.namedItem( "b2" ).toElement().text().toInt();
170 gains << e.namedItem( "b3" ).toElement().text().toInt();
171 gains << e.namedItem( "b4" ).toElement().text().toInt();
172 gains << e.namedItem( "b5" ).toElement().text().toInt();
173 gains << e.namedItem( "b6" ).toElement().text().toInt();
174 gains << e.namedItem( "b7" ).toElement().text().toInt();
175 gains << e.namedItem( "b8" ).toElement().text().toInt();
176 gains << e.namedItem( "b9" ).toElement().text().toInt();
178 m_presets[ title ] = gains;
181 file.close();
183 // Update listview
184 setPresets( m_presets );
187 void
188 EqualizerPresetManager::slotDelete()
190 Q3ListViewItem* item = m_presetsView->selectedItem();
192 m_presets.remove( item->text(0) );
194 delete item;
197 void
198 EqualizerPresetManager::updateButtonState()
200 bool selected = ( m_presetsView->selectedItem() != 0 );
202 m_deleteBtn->setEnabled( selected );
203 m_renameBtn->setEnabled( selected );
206 #include "equalizerpresetmanager.moc"