PageRenderTime 143ms CodeModel.GetById 60ms app.highlight 9ms RepoModel.GetById 32ms app.codeStats 0ms

/src/delegateconfigwrapper.h

http://github.com/tomahawk-player/tomahawk
C++ Header | 105 lines | 71 code | 14 blank | 20 comment | 3 complexity | b5bddd1455ebe7bfbba608b6b7945fed MD5 | raw file
  1/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
  2 *
  3 *   Copyright 2010-2011, Leo Franchi <lfranchi@kde.org>
  4 *
  5 *   Tomahawk is free software: you can redistribute it and/or modify
  6 *   it under the terms of the GNU General Public License as published by
  7 *   the Free Software Foundation, either version 3 of the License, or
  8 *   (at your option) any later version.
  9 *
 10 *   Tomahawk is distributed in the hope that it will be useful,
 11 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 12 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 13 *   GNU General Public License for more details.
 14 *
 15 *   You should have received a copy of the GNU General Public License
 16 *   along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
 17 */
 18#ifndef RESOLVER_CONFIG_WRAPPER
 19#define RESOLVER_CONFIG_WRAPPER
 20
 21#include <QDialog>
 22#include <QDialogButtonBox>
 23#include <QVBoxLayout>
 24#include <QPushButton>
 25
 26class DelegateConfigWrapper : public QDialog
 27{
 28    Q_OBJECT
 29public:
 30    DelegateConfigWrapper( QWidget* conf, const QString& title, QWidget* parent, Qt::WindowFlags flags = 0 ) : QDialog( parent, flags ), m_widget( conf )
 31    {
 32        m_widget->setWindowFlags( Qt::Sheet );
 33#ifdef Q_WS_MAC
 34        m_widget->setVisible( true );
 35#endif
 36
 37        setWindowTitle( title );
 38        QVBoxLayout* v = new QVBoxLayout( this );
 39        v->setContentsMargins( 0, 0, 0, 0 );
 40        v->addWidget( m_widget );
 41
 42        QDialogButtonBox* buttons = new QDialogButtonBox( QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal, this );
 43        m_okButton = buttons->button( QDialogButtonBox::Ok );
 44        connect( buttons, SIGNAL( clicked( QAbstractButton*)  ), this, SLOT( closed( QAbstractButton* ) ) );
 45        connect( this, SIGNAL( rejected() ), this, SLOT( rejected() ) );
 46        v->addWidget( buttons );
 47
 48        setLayout( v );
 49
 50#ifdef Q_WS_MAC
 51        setSizeGripEnabled( false );
 52        setMinimumSize( sizeHint() );
 53        setMaximumSize( sizeHint() ); // to remove the resize grip on osx this is the only way
 54
 55        if( conf->metaObject()->indexOfSignal( "sizeHintChanged()" ) > -1 )
 56            connect( conf, SIGNAL( sizeHintChanged() ), this, SLOT( updateSizeHint() ) );
 57#else
 58        m_widget->setVisible( true );
 59#endif
 60
 61    }
 62public slots:
 63    void toggleOkButton( bool dataError )
 64    {
 65        // if dataError is True we want to set the button enabled to false
 66        m_okButton->setEnabled( !dataError );
 67    }
 68    void closed( QAbstractButton* b )
 69    {
 70        // let the config widget live to see another day
 71        layout()->removeWidget( m_widget );
 72        m_widget->setParent( 0 );
 73        m_widget->setVisible( false );
 74
 75        QDialogButtonBox* buttons = qobject_cast< QDialogButtonBox* >( sender() );
 76        if( buttons->standardButton( b ) == QDialogButtonBox::Ok )
 77            done( QDialog::Accepted );
 78        else
 79            done( QDialog::Rejected );
 80    }
 81
 82    // we get a rejected() signal emitted if the user presses escape (and no clicked() signal )
 83    void rejected()
 84    {
 85        layout()->removeWidget( m_widget );
 86        m_widget->setParent( 0 );
 87        m_widget->setVisible( false );
 88    }
 89
 90    void updateSizeHint()
 91    {
 92        hide();
 93        setSizeGripEnabled( false );
 94        setMinimumSize( sizeHint() );
 95        setMaximumSize( sizeHint() );
 96
 97        show();
 98    }
 99
100private:
101    QWidget* m_widget;
102    QPushButton* m_okButton;
103};
104
105#endif