From 6cbc8a95395c973138b37c702f9aa7ff3b181629 Mon Sep 17 00:00:00 2001 From: Leo Franchi Date: Thu, 8 Mar 2012 18:53:59 -0500 Subject: [PATCH] Refactor account dialogs into utils, and port tomahawk to automoc too --- src/AccountFactoryWrapper.cpp | 35 +---- src/AccountFactoryWrapper.h | 1 - src/CMakeLists.txt | 64 +------- src/delegateconfigwrapper.cpp | 114 ++++++++++++++ src/delegateconfigwrapper.h | 80 +--------- src/libtomahawk/utils/tomahawkutils.cpp | 1 + src/libtomahawk/utils/tomahawkutils.h | 1 + src/libtomahawk/utils/tomahawkutilsgui.cpp | 10 +- src/libtomahawk/utils/tomahawkutilsgui.h | 4 +- src/settingsdialog.cpp | 137 +--------------- src/settingsdialog.h | 5 - src/utils/guihelpers.cpp | 172 +++++++++++++++++++++ src/utils/guihelpers.h | 38 +++++ 13 files changed, 357 insertions(+), 305 deletions(-) create mode 100644 src/delegateconfigwrapper.cpp create mode 100644 src/utils/guihelpers.cpp create mode 100644 src/utils/guihelpers.h diff --git a/src/AccountFactoryWrapper.cpp b/src/AccountFactoryWrapper.cpp index f5f270b06..0065508c6 100644 --- a/src/AccountFactoryWrapper.cpp +++ b/src/AccountFactoryWrapper.cpp @@ -19,7 +19,8 @@ #include "AccountFactoryWrapper.h" #include "accounts/Account.h" -#include +#include "accounts/AccountManager.h" +#include "guihelpers.h" #include "AccountFactoryWrapperDelegate.h" #include "delegateconfigwrapper.h" #include "ui_AccountFactoryWrapper.h" @@ -92,40 +93,10 @@ AccountFactoryWrapper::load() void AccountFactoryWrapper::openAccountConfig( Account* account ) { - if( account->configurationWidget() ) - { -#ifndef Q_WS_MAC - DelegateConfigWrapper dialog( account->configurationWidget(), QString("%1 Configuration" ).arg( account->accountFriendlyName() ), this ); - QWeakPointer< DelegateConfigWrapper > watcher( &dialog ); - int ret = dialog.exec(); - if( !watcher.isNull() && ret == QDialog::Accepted ) - { - // send changed config to resolver - account->saveConfig(); - } -#else - // on osx a sheet needs to be non-modal - DelegateConfigWrapper* dialog = new DelegateConfigWrapper( account->configurationWidget(), QString("%1 Configuration" ).arg( account->accountFriendlyName() ), this, Qt::Sheet ); - dialog->setProperty( "accountplugin", QVariant::fromValue< QObject* >( account ) ); - connect( dialog, SIGNAL( finished( int ) ), this, SLOT( accountConfigClosed( int ) ) ); - - dialog->show(); -#endif - } + TomahawkUtils::openAccountConfig( account, this, false ); } -void -AccountFactoryWrapper::accountConfigClosed( int value ) -{ - if( value == QDialog::Accepted ) - { - DelegateConfigWrapper* dialog = qobject_cast< DelegateConfigWrapper* >( sender() ); - Account* account = qobject_cast< Account* >( dialog->property( "accountplugin" ).value< QObject* >() ); - account->saveConfig(); - } -} - void AccountFactoryWrapper::removeAccount( Tomahawk::Accounts::Account* acct ) { diff --git a/src/AccountFactoryWrapper.h b/src/AccountFactoryWrapper.h index 02c8ec335..10223e0bc 100644 --- a/src/AccountFactoryWrapper.h +++ b/src/AccountFactoryWrapper.h @@ -49,7 +49,6 @@ signals: public slots: void openAccountConfig( Tomahawk::Accounts::Account* ); - void accountConfigClosed( int value ); void removeAccount( Tomahawk::Accounts::Account* ); private slots: diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index ee8ac9b0b..1bfd80e61 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -70,63 +70,21 @@ SET( tomahawkSourcesGui ${tomahawkSourcesGui} breakpad/BreakPad.cpp + utils/guihelpers.cpp + tomahawktrayicon.cpp audiocontrols.cpp settingsdialog.cpp diagnosticsdialog.cpp AccountDelegate.cpp settingslistdelegate.cpp + delegateconfigwrapper.cpp tomahawkwindow.cpp LoadXSPFDialog.cpp AccountFactoryWrapper.cpp AccountFactoryWrapperDelegate.cpp ) -SET( tomahawkHeaders ${tomahawkHeaders} - tomahawkapp.h - - web/api_v1.h - - musicscanner.h - scanmanager.h - ubuntuunityhack.h - shortcuthandler.h -) - -IF(LIBLASTFM_FOUND) - SET(tomahawkHeaders ${tomahawkHeaders} - scrobbler.h - ) -ENDIF(LIBLASTFM_FOUND) - -SET( tomahawkHeadersGui ${tomahawkHeadersGui} - sourcetree/sourcesmodel.h - sourcetree/sourcesproxymodel.h - sourcetree/sourcetreeview.h - sourcetree/sourcedelegate.h - sourcetree/animationhelper.h - sourcetree/items/sourcetreeitem.h - sourcetree/items/sourceitem.h - sourcetree/items/playlistitems.h - sourcetree/items/categoryitems.h - sourcetree/items/genericpageitems.h - sourcetree/items/temporarypageitem.h - sourcetree/items/groupitem.h - sourcetree/items/historyitem.h - - tomahawktrayicon.h - audiocontrols.h - settingsdialog.h - diagnosticsdialog.h - AccountDelegate.h - settingslistdelegate.h - delegateconfigwrapper.h - tomahawkwindow.h - LoadXSPFDialog.h - AccountFactoryWrapper.h - AccountFactoryWrapperDelegate.h -) - SET( tomahawkUI ${tomahawkUI} tomahawkwindow.ui diagnosticsdialog.ui @@ -180,17 +138,11 @@ ENDIF( UNIX ) IF( APPLE ) INCLUDE_DIRECTORIES( ${CMAKE_SOURCE_DIR}/thirdparty/SPMediaKeyTap ) - SET( tomahawkHeaders ${tomahawkHeaders} mac/tomahawkapp_mac.h mac/macshortcuthandler.h ) SET( tomahawkSources ${tomahawkSources} mac/tomahawkapp_mac.mm mac/macshortcuthandler.cpp ) - - IF(HAVE_SPARKLE) - SET( tomahawkHeaders ${tomahawkHeaders} ${SPARKLE}/Headers ) - ENDIF(HAVE_SPARKLE) ENDIF( APPLE ) IF(GLOOX_FOUND) INCLUDE_DIRECTORIES( ${GLOOX_INCLUDE_DIR} ) - SET( tomahawkHeaders ${tomahawkHeaders} xmppbot/xmppbot.h ) SET( tomahawkSources ${tomahawkSources} xmppbot/xmppbot.cpp ) ENDIF(GLOOX_FOUND) @@ -206,26 +158,26 @@ configure_file(${CMAKE_CURRENT_SOURCE_DIR}/config.h.in include( ${CMAKE_SOURCE_DIR}/lang/translations.cmake ) -SET( final_src ${final_src} ${tomahawkMoc} ${tomahawkSources} ${tomahawkHeaders} ${trans_outfile}) +SET( final_src ${final_src} ${tomahawkMoc} ${tomahawkSources} ${trans_outfile}) IF( BUILD_GUI ) - LIST(APPEND tomahawkHeaders ${tomahawkHeadersGui}) LIST(APPEND tomahawkSources ${tomahawkSourcesGui}) qt4_wrap_ui( tomahawkUI_H ${tomahawkUI} ) ENDIF() kde4_add_app_icon( tomahawkSources "${CMAKE_SOURCE_DIR}/data/icons/tomahawk-icon-*.png" ) qt4_add_resources( RC_SRCS "../resources.qrc" ) -qt4_wrap_cpp( tomahawkMoc ${tomahawkHeaders} ) + SET( final_src ${final_src} ${tomahawkUI_H} ${tomahawkMoc} ${tomahawkSources} ${RC_SRCS} ) IF( UNIX AND NOT APPLE ) ADD_EXECUTABLE( tomahawk ${final_src} ) + SET_TARGET_PROPERTIES(tomahawk PROPERTIES AUTOMOC TRUE) ENDIF( UNIX AND NOT APPLE ) IF( APPLE ) ADD_EXECUTABLE( tomahawk MACOSX_BUNDLE ${final_src} ) - SET_TARGET_PROPERTIES(tomahawk PROPERTIES MACOSX_BUNDLE_INFO_PLIST "${CMAKE_BINARY_DIR}/Info.plist" -) + SET_TARGET_PROPERTIES(tomahawk PROPERTIES MACOSX_BUNDLE_INFO_PLIST "${CMAKE_BINARY_DIR}/Info.plist") + SET_TARGET_PROPERTIES(tomahawk PROPERTIES AUTOMOC TRUE) ENDIF( APPLE ) IF( WIN32 ) ADD_EXECUTABLE( tomahawk WIN32 ${final_src} ) diff --git a/src/delegateconfigwrapper.cpp b/src/delegateconfigwrapper.cpp new file mode 100644 index 000000000..a7d8cb6a9 --- /dev/null +++ b/src/delegateconfigwrapper.cpp @@ -0,0 +1,114 @@ +/* === This file is part of Tomahawk Player - === + * + * Copyright 2010-2011, Leo Franchi + * + * Tomahawk 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 3 of the License, or + * (at your option) any later version. + * + * Tomahawk 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 Tomahawk. If not, see . + */ +#include "delegateconfigwrapper.h" + + +DelegateConfigWrapper::DelegateConfigWrapper( QWidget* conf, const QString& title, QWidget* parent, Qt::WindowFlags flags ) + : QDialog( parent, flags ) + , m_widget( conf ) + , m_deleted( false ) +{ + m_widget->setWindowFlags( Qt::Sheet ); +#ifdef Q_WS_MAC + m_widget->setVisible( true ); +#endif + setWindowTitle( title ); + QVBoxLayout* v = new QVBoxLayout( this ); + v->setContentsMargins( 0, 0, 0, 0 ); + v->addWidget( m_widget ); + + m_buttons = new QDialogButtonBox( QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal, this ); + m_okButton = m_buttons->button( QDialogButtonBox::Ok ); + connect( m_buttons, SIGNAL( clicked( QAbstractButton*) ), this, SLOT( closed( QAbstractButton* ) ) ); + connect( this, SIGNAL( rejected() ), this, SLOT( rejected() ) ); + v->addWidget( m_buttons ); + + setLayout( v ); + +#ifdef Q_WS_MAC + setSizeGripEnabled( false ); + setMinimumSize( sizeHint() ); + setMaximumSize( sizeHint() ); // to remove the resize grip on osx this is the only way + + if( conf->metaObject()->indexOfSignal( "sizeHintChanged()" ) > -1 ) + connect( conf, SIGNAL( sizeHintChanged() ), this, SLOT( updateSizeHint() ) ); +#else + m_widget->setVisible( true ); +#endif + +} + + +void +DelegateConfigWrapper::setShowDelete( bool del ) +{ + if ( del ) + m_deleteButton = m_buttons->addButton( tr( "Delete Account" ), QDialogButtonBox::DestructiveRole ); +} + + +void +DelegateConfigWrapper::toggleOkButton( bool dataError ) +{ + // if dataError is True we want to set the button enabled to false + m_okButton->setEnabled( !dataError ); +} + + +void +DelegateConfigWrapper::closed( QAbstractButton* b ) +{ + // let the config widget live to see another day + layout()->removeWidget( m_widget ); + m_widget->setParent( 0 ); + m_widget->setVisible( false ); + + QDialogButtonBox* buttons = qobject_cast< QDialogButtonBox* >( sender() ); + if ( buttons->standardButton( b ) == QDialogButtonBox::Ok ) + done( QDialog::Accepted ); + else if ( b == m_deleteButton ) + { + m_deleted = true; + emit closedWithDelete(); + reject(); + } + else + done( QDialog::Rejected ); +} + + +void +DelegateConfigWrapper::rejected() +{ + layout()->removeWidget( m_widget ); + m_widget->setParent( 0 ); + m_widget->setVisible( false ); +} + + +void +DelegateConfigWrapper::updateSizeHint() +{ + hide(); + setSizeGripEnabled( false ); + setMinimumSize( sizeHint() ); + setMaximumSize( sizeHint() ); + + show(); +} + diff --git a/src/delegateconfigwrapper.h b/src/delegateconfigwrapper.h index 4a2d4bfd1..d934c481e 100644 --- a/src/delegateconfigwrapper.h +++ b/src/delegateconfigwrapper.h @@ -28,91 +28,23 @@ class DelegateConfigWrapper : public QDialog { Q_OBJECT public: - DelegateConfigWrapper( QWidget* conf, const QString& title, QWidget* parent, Qt::WindowFlags flags = 0 ) : QDialog( parent, flags ), m_widget( conf ), m_deleted( false ) - { - m_widget->setWindowFlags( Qt::Sheet ); -#ifdef Q_WS_MAC - m_widget->setVisible( true ); -#endif - setWindowTitle( title ); - QVBoxLayout* v = new QVBoxLayout( this ); - v->setContentsMargins( 0, 0, 0, 0 ); - v->addWidget( m_widget ); - - m_buttons = new QDialogButtonBox( QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal, this ); - m_okButton = m_buttons->button( QDialogButtonBox::Ok ); - connect( m_buttons, SIGNAL( clicked( QAbstractButton*) ), this, SLOT( closed( QAbstractButton* ) ) ); - connect( this, SIGNAL( rejected() ), this, SLOT( rejected() ) ); - v->addWidget( m_buttons ); - - setLayout( v ); - -#ifdef Q_WS_MAC - setSizeGripEnabled( false ); - setMinimumSize( sizeHint() ); - setMaximumSize( sizeHint() ); // to remove the resize grip on osx this is the only way - - if( conf->metaObject()->indexOfSignal( "sizeHintChanged()" ) > -1 ) - connect( conf, SIGNAL( sizeHintChanged() ), this, SLOT( updateSizeHint() ) ); -#else - m_widget->setVisible( true ); -#endif - - } + DelegateConfigWrapper( QWidget* conf, const QString& title, QWidget* parent, Qt::WindowFlags flags = 0 ); ~DelegateConfigWrapper() {} - void setShowDelete( bool del ) - { - if ( del ) - m_deleteButton = m_buttons->addButton( tr( "Delete Account" ), QDialogButtonBox::DestructiveRole ); - } + void setShowDelete( bool del ); bool deleted() const { return m_deleted; } public slots: void toggleOkButton( bool dataError ) - { - // if dataError is True we want to set the button enabled to false - m_okButton->setEnabled( !dataError ); - } - void closed( QAbstractButton* b ) - { - // let the config widget live to see another day - layout()->removeWidget( m_widget ); - m_widget->setParent( 0 ); - m_widget->setVisible( false ); - - QDialogButtonBox* buttons = qobject_cast< QDialogButtonBox* >( sender() ); - if( buttons->standardButton( b ) == QDialogButtonBox::Ok ) - done( QDialog::Accepted ); - else if ( b == m_deleteButton ) - { - m_deleted = true; - emit closedWithDelete(); - reject(); - } - else - done( QDialog::Rejected ); - } + ; + void closed( QAbstractButton* b ); // we get a rejected() signal emitted if the user presses escape (and no clicked() signal ) - void rejected() - { - layout()->removeWidget( m_widget ); - m_widget->setParent( 0 ); - m_widget->setVisible( false ); - } + void rejected(); - void updateSizeHint() - { - hide(); - setSizeGripEnabled( false ); - setMinimumSize( sizeHint() ); - setMaximumSize( sizeHint() ); - - show(); - } + void updateSizeHint(); signals: void closedWithDelete(); diff --git a/src/libtomahawk/utils/tomahawkutils.cpp b/src/libtomahawk/utils/tomahawkutils.cpp index d706c3232..640b075a5 100644 --- a/src/libtomahawk/utils/tomahawkutils.cpp +++ b/src/libtomahawk/utils/tomahawkutils.cpp @@ -1,6 +1,7 @@ /* === This file is part of Tomahawk Player - === * * Copyright 2010-2011, Christian Muehlhaeuser + * Copyright 2010-2011, Leo Franchi * * Tomahawk is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by diff --git a/src/libtomahawk/utils/tomahawkutils.h b/src/libtomahawk/utils/tomahawkutils.h index f3133e112..8b3252a98 100644 --- a/src/libtomahawk/utils/tomahawkutils.h +++ b/src/libtomahawk/utils/tomahawkutils.h @@ -1,6 +1,7 @@ /* === This file is part of Tomahawk Player - === * * Copyright 2010-2011, Christian Muehlhaeuser + * Copyright 2010-2011, Leo Franchi * * Tomahawk is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by diff --git a/src/libtomahawk/utils/tomahawkutilsgui.cpp b/src/libtomahawk/utils/tomahawkutilsgui.cpp index 637f73cc4..168e5915c 100644 --- a/src/libtomahawk/utils/tomahawkutilsgui.cpp +++ b/src/libtomahawk/utils/tomahawkutilsgui.cpp @@ -1,6 +1,7 @@ /* === This file is part of Tomahawk Player - === * * Copyright 2010-2011, Christian Muehlhaeuser + * Copyright 2010-2011, Leo Franchi * * Tomahawk is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -19,6 +20,7 @@ #include "config.h" #include "tomahawkutilsgui.h" +#include "logger.h" #include #include #include @@ -36,7 +38,6 @@ #include #endif -#include "logger.h" namespace TomahawkUtils { @@ -321,7 +322,7 @@ defaultPixmap( ImageType type, ImageMode mode, const QSize& size ) if ( subcache.contains( mode ) ) { subsubcache = subcache.value( mode ); - + if ( subsubcache.contains( size.width() ) ) return subsubcache.value( size.width() ); } @@ -339,7 +340,7 @@ defaultPixmap( ImageType type, ImageMode mode, const QSize& size ) case DefaultArtistImage: pixmap = QPixmap( RESPATH "images/no-artist-image-placeholder.png" ); break; - + default: break; } @@ -352,7 +353,7 @@ defaultPixmap( ImageType type, ImageMode mode, const QSize& size ) if ( !size.isNull() ) pixmap = pixmap.scaled( size, Qt::IgnoreAspectRatio, Qt::SmoothTransformation ); - + subsubcache.insert( size.width(), pixmap ); subcache.insert( mode, subsubcache ); cache.insert( type, subcache ); @@ -360,4 +361,5 @@ defaultPixmap( ImageType type, ImageMode mode, const QSize& size ) return pixmap; } + } // ns diff --git a/src/libtomahawk/utils/tomahawkutilsgui.h b/src/libtomahawk/utils/tomahawkutilsgui.h index f353608de..1790aeb9b 100644 --- a/src/libtomahawk/utils/tomahawkutilsgui.h +++ b/src/libtomahawk/utils/tomahawkutilsgui.h @@ -1,6 +1,7 @@ /* === This file is part of Tomahawk Player - === * * Copyright 2010-2011, Christian Muehlhaeuser + * Copyright 2010-2011, Leo Franchi * * Tomahawk is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -48,8 +49,9 @@ namespace TomahawkUtils DLLEXPORT int headerHeight(); DLLEXPORT void setHeaderHeight( int height ); - + DLLEXPORT QPixmap defaultPixmap( ImageType type, ImageMode mode, const QSize& size = QSize( 0, 0 ) ); + } #endif // TOMAHAWKUTILSGUI_H diff --git a/src/settingsdialog.cpp b/src/settingsdialog.cpp index 25b6b42b0..c1bce31b8 100644 --- a/src/settingsdialog.cpp +++ b/src/settingsdialog.cpp @@ -20,8 +20,6 @@ #include "settingsdialog.h" #include "config.h" -#include "utils/tomahawkutilsgui.h" - #include #include #include @@ -38,6 +36,8 @@ #include "pipeline.h" #include "resolver.h" #include "ExternalResolverGui.h" +#include "utils/tomahawkutilsgui.h" +#include "guihelpers.h" #include "scanmanager.h" #include "settingslistdelegate.h" #include "AccountDelegate.h" @@ -343,61 +343,6 @@ SettingsDialog::accountsFilterChanged( int ) } -void -SettingsDialog::openAccountConfig( Account* account, bool showDelete ) -{ - if( account->configurationWidget() ) - { -#ifndef Q_OS_MAC - DelegateConfigWrapper dialog( account->configurationWidget(), QString("%1 Configuration" ).arg( account->accountFriendlyName() ), this ); - dialog.setShowDelete( showDelete ); - QWeakPointer< DelegateConfigWrapper > watcher( &dialog ); - int ret = dialog.exec(); - if ( !watcher.isNull() && dialog.deleted() ) - { - AccountManager::instance()->removeAccount( account ); - } - else if( !watcher.isNull() && ret == QDialog::Accepted ) - { - // send changed config to resolver - account->saveConfig(); - } -#else - // on osx a sheet needs to be non-modal - DelegateConfigWrapper* dialog = new DelegateConfigWrapper( account->configurationWidget(), QString("%1 Configuration" ).arg( account->accountFriendlyName() ), this, Qt::Sheet ); - dialog->setShowDelete( showDelete ); - dialog->setProperty( "accountplugin", QVariant::fromValue< QObject* >( account ) ); - connect( dialog, SIGNAL( finished( int ) ), this, SLOT( accountConfigClosed( int ) ) ); - connect( dialog, SIGNAL( closedWithDelete() ), this, SLOT( accountConfigDelete() ) ); - - dialog->show(); -#endif - } -} - - -void -SettingsDialog::accountConfigClosed( int value ) -{ - if( value == QDialog::Accepted ) - { - DelegateConfigWrapper* dialog = qobject_cast< DelegateConfigWrapper* >( sender() ); - Account* account = qobject_cast< Account* >( dialog->property( "accountplugin" ).value< QObject* >() ); - account->saveConfig(); - } -} - - -void -SettingsDialog::accountConfigDelete() -{ - DelegateConfigWrapper* dialog = qobject_cast< DelegateConfigWrapper* >( sender() ); - Account* account = qobject_cast< Account* >( dialog->property( "accountplugin" ).value< QObject* >() ); - Q_ASSERT( account ); - AccountManager::instance()->removeAccount( account ); -} - - void SettingsDialog::openAccountFactoryConfig( AccountFactory* factory ) { @@ -437,89 +382,17 @@ SettingsDialog::openAccountFactoryConfig( AccountFactory* factory ) void SettingsDialog::createAccountFromFactory( AccountFactory* factory ) { -#ifdef Q_WS_MAC - // On mac we need to close the dialog we came from before showing another dialog, if we are b eing called from a dialog - if ( sender() ) - { - Q_ASSERT( qobject_cast< AccountFactoryWrapper* >( sender() ) ); - AccountFactoryWrapper* dialog = qobject_cast< AccountFactoryWrapper* >( sender() ); - dialog->accept(); - } -#endif - - //if exited with OK, create it, if not, delete it immediately! - Account* account = factory->createAccount(); - bool added = false; - if( account->configurationWidget() ) - { -#ifdef Q_WS_MAC - // on osx a sheet needs to be non-modal - DelegateConfigWrapper* dialog = new DelegateConfigWrapper( account->configurationWidget(), QString("%1 Config" ).arg( account->accountFriendlyName() ), this, Qt::Sheet ); - dialog->setProperty( "accountplugin", QVariant::fromValue< QObject* >( account ) ); - connect( dialog, SIGNAL( finished( int ) ), this, SLOT( accountCreateConfigClosed( int ) ) ); - - if( account->configurationWidget()->metaObject()->indexOfSignal( "dataError(bool)" ) > -1 ) - connect( account->configurationWidget(), SIGNAL( dataError( bool ) ), dialog, SLOT( toggleOkButton( bool ) ), Qt::UniqueConnection ); - - dialog->show(); -#else - DelegateConfigWrapper dialog( account->configurationWidget(), QString("%1 Config" ).arg( account->accountFriendlyName() ), this ); - QWeakPointer< DelegateConfigWrapper > watcher( &dialog ); - - if( account->configurationWidget()->metaObject()->indexOfSignal( "dataError(bool)" ) > -1 ) - connect( account->configurationWidget(), SIGNAL( dataError( bool ) ), &dialog, SLOT( toggleOkButton( bool ) ), Qt::UniqueConnection ); - - int ret = dialog.exec(); - if( !watcher.isNull() && ret == QDialog::Accepted ) // send changed config to account - added = true; - else // canceled, delete it - added = false; - - handleAccountAdded( account, added ); -#endif - } - else - { - // no config, so just add it - added = true; - handleAccountAdded( account, added ); - } + TomahawkUtils::createAccountFromFactory( factory, this ); } void -SettingsDialog::accountCreateConfigClosed( int finished ) +SettingsDialog::openAccountConfig( Account* account, bool showDelete ) { - DelegateConfigWrapper* dialog = qobject_cast< DelegateConfigWrapper* >( sender() ); - Account* account = qobject_cast< Account* >( dialog->property( "accountplugin" ).value< QObject* >() ); - Q_ASSERT( account ); - - bool added = ( finished == QDialog::Accepted ); - - handleAccountAdded( account, added ); + TomahawkUtils::openAccountConfig( account, this, showDelete ); } -void -SettingsDialog::handleAccountAdded( Account* account, bool added ) -{ - if ( added ) - { - account->setEnabled( true ); - account->setAutoConnect( true ); - account->saveConfig(); - - TomahawkSettings::instance()->addAccount( account->accountId() ); - AccountManager::instance()->addAccount( account ); - AccountManager::instance()->hookupAndEnable( account ); - } - else - { - // user pressed cancel - delete account; - } -} - void SettingsDialog::installFromFile() diff --git a/src/settingsdialog.h b/src/settingsdialog.h index ca146efb3..cb02666bd 100644 --- a/src/settingsdialog.h +++ b/src/settingsdialog.h @@ -89,12 +89,8 @@ private slots: void accountsFilterChanged( int ); void createAccountFromFactory( Tomahawk::Accounts::AccountFactory* ); - void openAccountConfig( Tomahawk::Accounts::Account*, bool showDelete = false ); void openAccountFactoryConfig( Tomahawk::Accounts::AccountFactory* ); - void accountConfigClosed( int value ); - void accountConfigDelete(); - void accountCreateConfigClosed( int value ); void installFromFile(); void scrollTo( const QModelIndex& ); @@ -108,7 +104,6 @@ private slots: private: void createIcons(); - void handleAccountAdded( Tomahawk::Accounts::Account* p, bool added ); Ui_StackedSettingsDialog* ui; diff --git a/src/utils/guihelpers.cpp b/src/utils/guihelpers.cpp new file mode 100644 index 000000000..e7345e4f2 --- /dev/null +++ b/src/utils/guihelpers.cpp @@ -0,0 +1,172 @@ +/* === This file is part of Tomahawk Player - === + * + * Copyright 2010-2011, Leo Franchi + * + * Tomahawk 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 3 of the License, or + * (at your option) any later version. + * + * Tomahawk 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 Tomahawk. If not, see . + */ + +#include "guihelpers.h" + +#include + +#include "accounts/Account.h" +#include "accounts/AccountManager.h" +#include "delegateconfigwrapper.h" + +namespace TomahawkUtils +{ + +void +handleAccountAdded( Tomahawk::Accounts::Account* account, bool added ) +{ + if ( added ) + { + account->setEnabled( true ); + account->setAutoConnect( true ); + account->saveConfig(); + + TomahawkSettings::instance()->addAccount( account->accountId() ); + Tomahawk::Accounts::AccountManager::instance()->addAccount( account ); + Tomahawk::Accounts::AccountManager::instance()->hookupAndEnable( account ); + } + else + { + // user pressed cancel + delete account; + } +} + + +class UtilsObject : public QObject +{ + Q_OBJECT +public: + UtilsObject( DelegateConfigWrapper* w ) : QObject( w ), m_w( w ) {} + +public slots: + void + accountCreateConfigClosed( int ret ) + { + Tomahawk::Accounts::Account* account = qobject_cast< Tomahawk::Accounts::Account* >( m_w->property( "accountplugin" ).value< QObject* >() ); + Q_ASSERT( account ); + + bool added = ( ret == QDialog::Accepted ); + + handleAccountAdded( account, added ); + } + + void + accountConfigClosed( int ret ) + { + if( ret == QDialog::Accepted ) + { + Tomahawk::Accounts::Account* account = qobject_cast< Tomahawk::Accounts::Account* >( m_w->property( "accountplugin" ).value< QObject* >() ); + account->saveConfig(); + } + } + + void + accountConfigDelete() + { + Tomahawk::Accounts::Account* account = qobject_cast< Tomahawk::Accounts::Account* >( m_w->property( "accountplugin" ).value< QObject* >() ); + Q_ASSERT( account ); + Tomahawk::Accounts::AccountManager::instance()->removeAccount( account ); + } +private: + DelegateConfigWrapper* m_w; + +}; + + +void +createAccountFromFactory( Tomahawk::Accounts::AccountFactory* factory, QWidget* parent ) +{ + //if exited with OK, create it, if not, delete it immediately! + Tomahawk::Accounts::Account* account = factory->createAccount(); + bool added = false; + if( account->configurationWidget() ) + { + #ifdef Q_WS_MAC + // on osx a sheet needs to be non-modal + DelegateConfigWrapper* dialog = new DelegateConfigWrapper( account->configurationWidget(), QString("%1 Config" ).arg( account->accountFriendlyName() ), parent, Qt::Sheet ); + dialog->setProperty( "accountplugin", QVariant::fromValue< QObject* >( account ) ); + + UtilsObject* obj = new UtilsObject( dialog ); + QObject::connect( dialog, SIGNAL( finished( int ) ), obj, SLOT( accountCreateConfigClosed( int ) ) ); + + if( account->configurationWidget()->metaObject()->indexOfSignal( "dataError(bool)" ) > -1 ) + QObject::connect( account->configurationWidget(), SIGNAL( dataError( bool ) ), dialog, SLOT( toggleOkButton( bool ) ), Qt::UniqueConnection ); + + dialog->show(); + #else + DelegateConfigWrapper dialog( account->configurationWidget(), QString("%1 Config" ).arg( account->accountFriendlyName() ), parent ); + QWeakPointer< DelegateConfigWrapper > watcher( &dialog ); + + if( account->configurationWidget()->metaObject()->indexOfSignal( "dataError(bool)" ) > -1 ) + QObject::connect( account->configurationWidget(), SIGNAL( dataError( bool ) ), &dialog, SLOT( toggleOkButton( bool ) ), Qt::UniqueConnection ); + + int ret = dialog.exec(); + if( !watcher.isNull() && ret == QDialog::Accepted ) // send changed config to account + added = true; + else // canceled, delete it + added = false; + + handleAccountAdded( account, added ); + #endif + } + else + { + // no config, so just add it + added = true; + handleAccountAdded( account, added ); + } +} + +void +openAccountConfig( Tomahawk::Accounts::Account* account, QWidget* parent, bool showDelete ) +{ + if( account->configurationWidget() ) + { + #ifndef Q_OS_MAC + DelegateConfigWrapper dialog( account->configurationWidget(), QString("%1 Configuration" ).arg( account->accountFriendlyName() ), parent ); + dialog.setShowDelete( showDelete ); + QWeakPointer< DelegateConfigWrapper > watcher( &dialog ); + int ret = dialog.exec(); + if ( !watcher.isNull() && dialog.deleted() ) + { + Tomahawk::Accounts::AccountManager::instance()->removeAccount( account ); + } + else if( !watcher.isNull() && ret == QDialog::Accepted ) + { + // send changed config to resolver + account->saveConfig(); + } + #else + // on osx a sheet needs to be non-modal + DelegateConfigWrapper* dialog = new DelegateConfigWrapper( account->configurationWidget(), QString("%1 Configuration" ).arg( account->accountFriendlyName() ), parent, Qt::Sheet ); + dialog->setShowDelete( showDelete ); + dialog->setProperty( "accountplugin", QVariant::fromValue< QObject* >( account ) ); + UtilsObject* obj = new UtilsObject( dialog ); + + QObject::connect( dialog, SIGNAL( finished( int ) ), obj, SLOT( accountConfigClosed( int ) ) ); + QObject::connect( dialog, SIGNAL( closedWithDelete() ), obj, SLOT( accountConfigDelete() ) ); + + dialog->show(); + #endif + } +} + +} // namespace TomahawkUtils + +#include "guihelpers.moc" \ No newline at end of file diff --git a/src/utils/guihelpers.h b/src/utils/guihelpers.h new file mode 100644 index 000000000..8d1f7ac49 --- /dev/null +++ b/src/utils/guihelpers.h @@ -0,0 +1,38 @@ +/* === This file is part of Tomahawk Player - === + * + * Copyright 2010-2011, Leo Franchi + * + * Tomahawk 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 3 of the License, or + * (at your option) any later version. + * + * Tomahawk 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 Tomahawk. If not, see . + */ + +#ifndef TOMAHAWK_GUI_HELPERS_H +#define TOMAHAWK_GUI_HELPERS_H + +#include "dllmacro.h" + +class QWidget; +namespace Tomahawk { + namespace Accounts { + class AccountFactory; + class Account; + } +} + +namespace TomahawkUtils +{ + DLLEXPORT void createAccountFromFactory( Tomahawk::Accounts::AccountFactory*, QWidget* parent ); + DLLEXPORT void openAccountConfig( Tomahawk::Accounts::Account*, QWidget* parent, bool showDelete = false ); +} + +#endif \ No newline at end of file