/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. #include <QDialog>
  21. #include <QDialogButtonBox>
  22. #include <QVBoxLayout>
  23. #include <QPushButton>
  24. class DelegateConfigWrapper : public QDialog
  25. {
  26. Q_OBJECT
  27. public:
  28. DelegateConfigWrapper( QWidget* conf, const QString& title, QWidget* parent, Qt::WindowFlags flags = 0 ) : QDialog( parent, flags ), m_widget( conf )
  29. {
  30. m_widget->setWindowFlags( Qt::Sheet );
  31. #ifdef Q_WS_MAC
  32. m_widget->setVisible( true );
  33. #endif
  34. setWindowTitle( title );
  35. QVBoxLayout* v = new QVBoxLayout( this );
  36. v->setContentsMargins( 0, 0, 0, 0 );
  37. v->addWidget( m_widget );
  38. QDialogButtonBox* buttons = new QDialogButtonBox( QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal, this );
  39. m_okButton = buttons->button( QDialogButtonBox::Ok );
  40. connect( buttons, SIGNAL( clicked( QAbstractButton*) ), this, SLOT( closed( QAbstractButton* ) ) );
  41. connect( this, SIGNAL( rejected() ), this, SLOT( rejected() ) );
  42. v->addWidget( buttons );
  43. setLayout( v );
  44. #ifdef Q_WS_MAC
  45. setSizeGripEnabled( false );
  46. setMinimumSize( sizeHint() );
  47. setMaximumSize( sizeHint() ); // to remove the resize grip on osx this is the only way
  48. if( conf->metaObject()->indexOfSignal( "sizeHintChanged()" ) > -1 )
  49. connect( conf, SIGNAL( sizeHintChanged() ), this, SLOT( updateSizeHint() ) );
  50. #else
  51. m_widget->setVisible( true );
  52. #endif
  53. }
  54. public slots:
  55. void toggleOkButton( bool dataError )
  56. {
  57. // if dataError is True we want to set the button enabled to false
  58. m_okButton->setEnabled( !dataError );
  59. }
  60. void closed( QAbstractButton* b )
  61. {
  62. // let the config widget live to see another day
  63. layout()->removeWidget( m_widget );
  64. m_widget->setParent( 0 );
  65. m_widget->setVisible( false );
  66. QDialogButtonBox* buttons = qobject_cast< QDialogButtonBox* >( sender() );
  67. if( buttons->standardButton( b ) == QDialogButtonBox::Ok )
  68. done( QDialog::Accepted );
  69. else
  70. done( QDialog::Rejected );
  71. }
  72. // we get a rejected() signal emitted if the user presses escape (and no clicked() signal )
  73. void rejected()
  74. {
  75. layout()->removeWidget( m_widget );
  76. m_widget->setParent( 0 );
  77. m_widget->setVisible( false );
  78. }
  79. void updateSizeHint()
  80. {
  81. hide();
  82. setSizeGripEnabled( false );
  83. setMinimumSize( sizeHint() );
  84. setMaximumSize( sizeHint() );
  85. show();
  86. }
  87. private:
  88. QWidget* m_widget;
  89. QPushButton* m_okButton;
  90. };
  91. #endif