PreferenceWidget.cpp 4.63 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 <QtDebug>
39
#include <QWidget>
40
#include <QLabel>
41 42 43
#include <QHash>
#include <QHashIterator>
#include <QEvent>
44

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

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

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

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

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

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

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

127
const char*
128
PreferenceWidget::category() const
129
{
130
    return m_category;
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 156

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() ) );
    }
157 158 159 160 161 162 163 164

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