Settings.cpp 5.96 KB
Newer Older
1
/*****************************************************************************
2
 * Settings.cpp: Generic preferences interface
3
 *****************************************************************************
4
 * Copyright (C) 2008-2014 VideoLAN
5
 *
6
 * Authors: Ludovic Fauvet <etix@l0cal.com>
7
 *          Hugo Beauzée-Luyssen <hugo@beauzee.fr>
8
 *
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 *
 * This program 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 2
 * of the License, or (at your option) any later version.
 *
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/
24

25 26
#include "Settings.h"

27
#include "PreferenceWidget.h"
28
#include "Settings/SettingsManager.h"
29 30
#include "Panel.h"

31
#include <QAbstractButton>
32
#include <QApplication>
33
#include <QMessageBox>
34
#include <QDialogButtonBox>
35
#include <QHBoxLayout>
36
#include <QLabel>
37
#include <QScrollArea>
38
#include <QStackedLayout>
39

40 41
Settings::Settings( SettingsManager::Type type, QWidget *parent ) :
    QDialog( parent ),
42
    m_type( type )
43
{
44 45 46 47 48 49
    setMinimumHeight( 400 );
    setMinimumWidth( 600 );

    m_buttons = new QDialogButtonBox( Qt::Horizontal, this );
    m_buttons->setStandardButtons( QDialogButtonBox::Ok |
                                   QDialogButtonBox::Cancel |
50 51
                                   QDialogButtonBox::Apply |
                                   QDialogButtonBox::Reset );
52 53

    // Create the layout
54
    buildLayout();
55

56 57
    connect( m_panel, SIGNAL( changePanel( int ) ),
             this, SLOT( switchWidget( int ) ) );
58 59 60 61
    connect( m_buttons, SIGNAL( clicked( QAbstractButton* ) ),
             this, SLOT( buttonClicked( QAbstractButton* ) ) );
    connect( m_buttons, SIGNAL( accepted() ), this, SLOT( accept() ) );
    connect( m_buttons, SIGNAL( rejected() ), this, SLOT( reject() ) );
62 63
}

64
void
65
Settings::addCategory( const QString &name, const char *label,
66
                       SettingsManager::Type type,
67
                       const QIcon &icon )
68
{
69
    PreferenceWidget    *pWidget = new PreferenceWidget( name, label, type, this );
70

71
    m_stackedLayout->addWidget( pWidget );
72

73
    // Create a button linked to the widget using its index
74
    m_panel->addButton( label, icon, m_stackedLayout->count() - 1 );
75 76

    switchWidget( 0 );
77 78
}

79 80
void
Settings::buildLayout()
81
{
82 83 84
    // Create the left panel
    m_panel = new Panel( this );
    m_panel->setMaximumWidth( 130 );
85

86
    // Create the master layout
87
    QGridLayout     *mLayout = new QGridLayout( this );
88
    mLayout->addWidget( m_panel, 0, 0, 2, 1 );
89 90

    m_title = new QLabel( this );
91
    m_stackedLayout = new QStackedLayout;
92

93
    // Set the font and text of the panel title
94 95 96 97 98 99 100 101
    QFont titleFont = QApplication::font();
    titleFont.setPointSize( titleFont.pointSize() + 6 );
    titleFont.setFamily( "Verdana" );
    m_title->setFont( titleFont );

    mLayout->addWidget( m_title, 0, 1, 1, 2 );
    mLayout->addLayout( m_stackedLayout, 1, 2, 1, 2 );
    mLayout->addWidget( m_buttons, 2, 2 );
Ludovic Fauvet's avatar
Ludovic Fauvet committed
102
}
103

104 105
void
Settings::buttonClicked( QAbstractButton *button )
106 107 108
{
    switch ( m_buttons->standardButton( button ) )
    {
109 110 111 112 113 114 115 116 117 118 119
    case QDialogButtonBox::Reset:
        {
            if ( QMessageBox::question( NULL, tr( "Restore default?" ),
                                        tr( "This will restore all settings default value.\nAre you sure you want to continue?" ),
                                        QMessageBox::Ok | QMessageBox::Cancel, QMessageBox::Cancel ) == QMessageBox::Ok )
            {
                for ( int i = 0; i < m_stackedLayout->count(); ++i )
                    qobject_cast<PreferenceWidget*>( m_stackedLayout->widget( i ) )->reset();
            }
            break ;
        }
120 121 122 123
    case QDialogButtonBox::Ok:
    case QDialogButtonBox::Apply:
        {
            for ( int i = 0; i < m_stackedLayout->count(); ++i )
124 125 126 127 128 129 130 131
            {
                if ( qobject_cast<PreferenceWidget*>( m_stackedLayout->widget( i ) )->save() == false )
                {
                    QMessageBox::warning( NULL, tr( "Invalid value" ),
                                          tr( "Can't save settings due to an invalid value" ) );
                    return ;
                }
            }
132 133
            //If we're handling vlmc preferences, save the value in the QSettings
            if ( m_type == SettingsManager::Vlmc )
134
                Core::getInstance()->settings()->save();
135
        }
136 137 138 139 140
    case QDialogButtonBox::Cancel:
        {
            for ( int i = 0; i < m_stackedLayout->count(); ++i )
                qobject_cast<PreferenceWidget*>( m_stackedLayout->widget( i ) )->discard();
        }
141 142
    default:
        break ;
143
    }
144 145
}

146 147
void
Settings::switchWidget( int index )
148
{
149
    m_stackedLayout->setCurrentIndex( index );
150 151 152

    // Reload the translated title
    retranslateUi();
153
}
154

155 156
void
Settings::changeEvent( QEvent *e )
157 158 159 160 161 162 163 164 165 166 167
{
    switch ( e->type() )
    {
    case QEvent::LanguageChange:
        retranslateUi();
        break;
    default:
        break;
    }
}

168 169
void
Settings::retranslateUi()
170
{
171 172
    PreferenceWidget *pWidget = qobject_cast<PreferenceWidget*>(
            m_stackedLayout->widget( m_stackedLayout->currentIndex() ) );
173 174 175 176 177 178 179 180
    Q_ASSERT( pWidget != NULL );

    // Translate the category name using the current locale
    QString text = tr( pWidget->category() );

    if ( text.length() >= 1 )
        text[0] = text[0].toUpper();
    m_title->setText( text );
181 182 183 184
    if ( m_type == SettingsManager::Project )
        setWindowTitle( tr( "Project preferences" ) );
    else
        setWindowTitle( tr( "VLMC Settings" ) );
185
    m_panel->retranslate();
186
}