PreferenceWidget.cpp 4.61 KB
Newer Older
1 2 3 4
/*****************************************************************************
 * PreferenceWidget.cpp: Abstract class that will be used to save load / preferences
 * values.
 *****************************************************************************
Ludovic Fauvet's avatar
Ludovic Fauvet committed
5
 * Copyright (C) 2008-2010 VideoLAN
6 7
 *
 * Authors: Clement CHAVANCE <kinder@vlmc.org>
8
 *          Hugo Beauzée-Luyssen <hugo@beauzee.fr>
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 *
 * 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.
 *****************************************************************************/

25
#include "ISettingsCategoryWidget.h"
26 27
#include "PreferenceWidget.h"
#include "SettingsManager.h"
28

29 30 31
#include "BoolWidget.h"
#include "DoubleWidget.h"
#include "IntWidget.h"
32
#include "KeyboardShortcut.h"
33
#include "LanguageWidget.h"
34
#include "PathWidget.h"
35
#include "StringWidget.h"
36 37

#include <QFormLayout>
38
#include <QWidget>
39
#include <QLabel>
40 41 42
#include <QHash>
#include <QHashIterator>
#include <QEvent>
43

44
PreferenceWidget::PreferenceWidget( const QString &name, const char *label, SettingsManager::Type type,
45
                                    QWidget *parent ) :
46
    QScrollArea( parent ),
47
    m_category( label )
48
{
49
    QWidget     *container = new QWidget( this );
50
    SettingsManager::SettingList    settings =
51
            SettingsManager::getInstance()->group( name, type );
52 53
    QFormLayout *layout = new QFormLayout( container );
    layout->setFieldGrowthPolicy( QFormLayout::AllNonFixedFieldsGrow );
54

55
    foreach ( const SettingsManager::Pair p, settings )
56
    {
57
        SettingValue    *s = p.value;
58 59 60
        //Do not display private variables
        if ( ( s->flags() & SettingValue::Private ) != 0 )
            continue ;
61
        ISettingsCategoryWidget    *widget = widgetFactory( s );
62 63 64
        QLabel                      *label = new QLabel( tr( s->name() ), this );
        label->setToolTip( tr( s->description() ) );
        m_labels.insert( s, label );
65 66
        widget->setToolTip( s->description() );
        layout->addRow( label, widget );
67
        m_settings.push_back( widget );
68
    }
69

70 71 72
    setWidget( container );
    setWidgetResizable( true );
    setFrameStyle( QFrame::NoFrame );
73 74
}

75
ISettingsCategoryWidget*
76
PreferenceWidget::widgetFactory( SettingValue *s )
77
{
78 79 80 81
    switch ( s->type() )
    {
    case SettingValue::KeyboardShortcut:
        return new KeyboardShortcut( s, this );
82 83
    case SettingValue::Language:
        return new LanguageWidget( s, this );
84 85
    case SettingValue::String:
        return new StringWidget( s, this );
86 87
    case SettingValue::Int:
        return new IntWidget( s, this );
88 89
    case SettingValue::Double:
        return new DoubleWidget( s, this );
90 91
    case SettingValue::Bool:
        return new BoolWidget( s, this );
92 93
    case SettingValue::Path:
        return new PathWidget( s, this );
94 95
    default:
        return NULL;
96
    }
97
}
98

99
bool
100
PreferenceWidget::save()
101
{
102
    foreach ( ISettingsCategoryWidget* w, m_settings )
103 104 105 106 107
    {
        if ( w->save() == false )
            return false;
    }
    return true;
108
}
109

110 111 112 113 114
void
PreferenceWidget::reset()
{
    foreach ( ISettingsCategoryWidget* w, m_settings )
        w->setting()->restoreDefault();
115
    discard();
116 117 118
    save();
}

119 120 121 122 123 124 125
void
PreferenceWidget::discard()
{
    foreach ( ISettingsCategoryWidget* w, m_settings )
        w->changed( w->setting()->get() );
}

126
const char*
127
PreferenceWidget::category() const
128
{
129
    return m_category;
130
}
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155

void
PreferenceWidget::changeEvent( QEvent *e )
{
    switch ( e->type() )
    {
    case QEvent::LanguageChange:
        retranslateUi();
        break;
    default:
        break;
    }
}

void
PreferenceWidget::retranslateUi()
{
    QHashIterator<SettingValue*, QLabel*> i( m_labels );

    while ( i.hasNext() )
    {
        i.next();
        i.value()->setText( tr( i.key()->name() ) );
        i.value()->setToolTip( tr( i.key()->description() ) );
    }
156 157 158 159 160 161 162 163

    SettingsList::iterator      it = m_settings.begin();
    SettingsList::iterator      ite = m_settings.end();
    while ( it != ite )
    {
        (*it)->setToolTip( tr( (*it)->setting()->description() ) );
        ++it;
    }
164
}