Settings.cpp 4.77 KB
Newer Older
1
/*****************************************************************************
2
 * Settings.cpp: generic preferences interface
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 *****************************************************************************
 * Copyright (C) 2008-2009 the VLMC team
 *
 * Authors: Clement CHAVANCE <kinder@vlmc.org>
 *
 * 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.
 *****************************************************************************/
22
#include <QPushButton>
23
#include <QDialogButtonBox>
24
#include <QAbstractButton>
25
#include <QApplication>
26
#include <QSizePolicy>
27
28
#include <QLabel>
#include <QHash>
29
#include <QIcon>
30
31
#include <QLabel>

32
33
#include <QtDebug>

34

35
#include "PreferenceWidget.h"
36
#include "Settings.h"
37
#include "Panel.h"
38
39


Ludovic Fauvet's avatar
Ludovic Fauvet committed
40
41
Settings::Settings( QWidget* parent, Qt::WindowFlags f )
    : QDialog( parent, f ),
42
    m_currentWidget( NULL ),
43
44
    m_panel( NULL ),
    m_title( NULL ),
45
    m_stackedWidgets( 0 )
46
{
47
    m_panel = new Panel( this );
48
    m_stackedWidgets = new QStackedWidget( this );
49
50
    connect( m_panel, SIGNAL( changePanel( int ) ),
             SLOT( switchWidget( int ) ) );
51
52
    QObject::connect( this, SIGNAL( widgetSwitched( int ) ),
                      m_stackedWidgets, SLOT( setCurrentIndex( int ) ));
53
54
}

55
Settings::~Settings()
56
{
57
58
    delete m_panel;
    delete m_title;
59
    delete m_stackedWidgets;
60
61
62
63
64
}


//TODO : see if the widget MUST have a fixed size, or if the window can dynamicaly
//adjust to the size of the biggest Widget.
65
void        Settings::addWidget( const QString& name,
66
                                          PreferenceWidget* pWidget,
67
68
69
                                          const QString& icon,
                                          const QString& label)
{
70
    m_stackedWidgets->addWidget( pWidget->widget() );
71

72
    int idx = m_stackedWidgets->indexOf( pWidget->widget() );
73
74
    m_widgets.insert( idx, name );
    m_panel->addButton( label, icon, idx );
75
    if (m_currentWidget == 0)
76
    {
77
        m_currentWidget = pWidget->widget();
78
    }
79
80
}

81
void        Settings::build()
82
83
{
    if (m_currentWidget == 0)
84
        qFatal(  "Can't build the preference panel without an added widget"  );
85
    QHBoxLayout*        hLayout = new QHBoxLayout( this );
86
    setLayout( hLayout );
87
    //TODO : change the size of the widgets to make it look cleaner
88
    hLayout->addWidget( m_panel );
89
    hLayout->insertLayout( 1, buildRightHLayout() );
90
91
}

92

93
QVBoxLayout*    Settings::buildRightHLayout()
94
{
95
    QVBoxLayout*    layout = new QVBoxLayout;
96
    QFrame*         titleLine = new QFrame;
97
98
99
100
    m_buttons = new QDialogButtonBox( this );

    QObject::connect( m_buttons, SIGNAL( clicked( QAbstractButton* ) ),
                      this, SLOT( buttonClicked( QAbstractButton* ) ) );
101
102
103
104
105
106
107
108
109
110
111

    m_title = new QLabel( this );
    titleLine->setFrameShape( QFrame::HLine );
    titleLine->setFrameShadow( QFrame::Sunken );

    QFont   labelFont = QApplication::font(this);

    labelFont.setPointSize( labelFont.pointSize() + 6 );
    labelFont.setFamily( "Verdana" );
    m_title->setFont( labelFont );

112
    m_buttons->setStandardButtons( QDialogButtonBox::Ok |
113
114
115
                                 QDialogButtonBox::Cancel |
                                 QDialogButtonBox::Apply );

116
117
    QString title( m_widgets.value( m_stackedWidgets->indexOf( m_currentWidget ) ) );
    m_title->setText( title );
118
    layout->addWidget( m_title );
119
    layout->addWidget( titleLine );
120
    layout->addWidget( m_stackedWidgets );
121
    layout->addWidget( m_buttons );
122
    return ( layout );
Ludovic Fauvet's avatar
Ludovic Fauvet committed
123
}
124

125
void    Settings::save( void )
126
127
128
{
}

129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
void    Settings::buttonClicked( QAbstractButton* button )
{
    switch ( m_buttons->standardButton( button ) )
    {
    case QDialogButtonBox::Ok :
        qDebug() << "MOK";
        break;
    case QDialogButtonBox::Cancel :
        qDebug() << "Oh NOES";
        break;
    case QDialogButtonBox::Apply :
        qDebug() << "Apply";
        break;
    default :
        break;
    }
}

147

148
void    Settings::switchWidget( int widget )
149
{
150
    //TODO : Change the title of the preferences shown
151
    //Hide the current widget and show the new one.
Ludovic Fauvet's avatar
Ludovic Fauvet committed
152

153
    if ( !m_widgets.contains( widget ) )
154
        return ;
155
    m_title->setText( m_widgets.value( widget ) );
156
    emit widgetSwitched( widget );
157
}
158