00001
00002 #include "engine_editor.h"
00003
00004 EngineEditor::EngineEditor( QWidget *parent ) : QWidget( parent )
00005 {
00006 setObjectName( "Editor Engine" ) ;
00007 setWindowTitle( "Editor Engine[*]" ) ;
00008
00009 _engine = 0 ;
00010 _imagePath = "" ;
00011
00012 setMaximumSize( QSize(190, 400) ) ;
00013
00014 _nameEdit = new QLineEdit( "" ) ;
00015 connect( _nameEdit, SIGNAL( textChanged( const QString ) ), this, SLOT( setDirty() ) ) ;
00016
00017 QLabel *name = new QLabel( trUtf8("Name") ) ;
00018 QLabel *protocol = new QLabel( trUtf8("Protocol") ) ;
00019 QLabel *address = new QLabel( trUtf8("Address") ) ;
00020
00021 _addressBox = new QSpinBox();
00022 _addressBox->setRange( 0, 80) ;
00023 _addressBox->setValue( 0 ) ;
00024 connect( _addressBox, SIGNAL( valueChanged( int ) ), this, SLOT( setDirty() ) ) ;
00025
00026 _decoderBox = new QComboBox();
00027 _decoderBox->addItem( trUtf8("Märklin 1"), QVariant("M1") ) ;
00028 _decoderBox->addItem( trUtf8("Märklin 2"), QVariant("M2") ) ;
00029 _decoderBox->addItem( trUtf8("Märklin 3"), QVariant("M3") ) ;
00030 _decoderBox->addItem( trUtf8("Märklin 4"), QVariant("M4") ) ;
00031 _decoderBox->addItem( trUtf8("Märklin 5"), QVariant("M5") ) ;
00032
00033
00034 _decoderBox->setCurrentIndex( 1 ) ;
00035 connect( _decoderBox, SIGNAL( currentIndexChanged( int ) ), this, SLOT( setDirty() ) ) ;
00036
00037 QGridLayout *optionLayout = new QGridLayout() ;
00038 optionLayout->addWidget( name, 0, 0) ;
00039 optionLayout->addWidget( _nameEdit, 0, 1) ;
00040 optionLayout->addWidget( protocol, 1, 0) ;
00041 optionLayout->addWidget( _decoderBox, 1, 1) ;
00042 optionLayout->addWidget( address, 2, 0 ) ;
00043 optionLayout->addWidget( _addressBox, 2, 1) ;
00044
00045 QGroupBox *optionBox = new QGroupBox( trUtf8("Settings"), this ) ;
00046 optionBox->setLayout( optionLayout ) ;
00047
00048 _image = new QLabel( trUtf8("No image available") );
00049 _image->setMaximumSize( QSize(180,130) ) ;
00050 _image->setScaledContents ( TRUE ) ;
00051
00052 QPushButton *changeImageBtn = new QPushButton( trUtf8("Change image") );
00053 connect(changeImageBtn, SIGNAL( clicked() ), this, SLOT( changeImage() ) );
00054
00055 QVBoxLayout *imageLayout = new QVBoxLayout();
00056 imageLayout->addWidget( _image, 0, Qt::AlignHCenter );
00057 imageLayout->addWidget( changeImageBtn );
00058
00059 QGroupBox *imageBox = new QGroupBox( trUtf8("Image"), this ) ;
00060 imageBox->setLayout( imageLayout );
00061
00062 _apply = new QPushButton( QIcon( ":img/apply.svg" ), trUtf8("Apply"), this );
00063 connect( _apply, SIGNAL( clicked() ), this, SLOT( applyChanges() ) ) ;
00064
00065 _delete = new QPushButton( QIcon( ":img/remove.svg" ), trUtf8("Delete"), this );
00066 connect( _delete, SIGNAL( clicked() ), this, SLOT( deleteEngine() ) ) ;
00067
00068 QHBoxLayout *btnLayout = new QHBoxLayout() ;
00069 btnLayout->addWidget( _delete ) ;
00070 btnLayout->addWidget( _apply ) ;
00071
00072
00073 QVBoxLayout *mainLayout = new QVBoxLayout( this ) ;
00074 mainLayout->addWidget( optionBox ) ;
00075 mainLayout->addWidget( imageBox ) ;
00076 mainLayout->addLayout( btnLayout ) ;
00077
00078 setEnabled( FALSE ) ;
00079 setDirty( FALSE ) ;
00080 }
00081
00082 void EngineEditor::changeImage()
00083 {
00084 QString filename = QFileDialog::getOpenFileName( this, tr( "Choose image" ), QDir::homePath (), tr( "Images (*.png *.xpm *.jpg);;All Files (*)" ) ) ;
00085
00086 if ( filename.isEmpty () )
00087 return ;
00088
00089 setImage( filename ) ;
00090 }
00091
00092 void EngineEditor::setImage( const QString &imagePath )
00093 {
00094 _imagePath = imagePath ;
00095
00096 QPixmap picture = QPixmap() ;
00097 if ( picture.load( imagePath ) )
00098 _image->setPixmap( picture ) ;
00099 else
00100 _image->setText( trUtf8("No image available") ) ;
00101
00102 setDirty() ;
00103 }
00104
00105 void EngineEditor::applyChanges()
00106 {
00107 _engine->setName( _nameEdit->text() ) ;
00108 _engine->setAddress( _addressBox->value() ) ;
00109 _engine->setImage( _imagePath ) ;
00110 _engine->setDecoder( _decoderBox->itemData( _decoderBox->currentIndex() ).toString() ) ;
00111
00112 emit engineChanged() ;
00113 setDirty( FALSE ) ;
00114 }
00115
00116 void EngineEditor::deleteEngine()
00117 {
00118 switch( QMessageBox::warning( this, trUtf8( "OrK" ), trUtf8( "Do you really want to delete the engine %1?" ).arg( _engine->name() ) ,QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes) )
00119 {
00120 case QMessageBox::Yes :
00121
00122 qDebug() << "Delete engine" ;
00123 setEnabled( FALSE ) ;
00124 break ;
00125
00126 case QMessageBox::No :
00127 return ;
00128 break ;
00129
00130 default :
00131 return ;
00132 break ;
00133 }
00134
00135
00136 }
00137
00138 void EngineEditor::setDirty( bool dirty )
00139 {
00140 setWindowModified( dirty ) ;
00141 _apply->setEnabled( dirty ) ;
00142 }
00143
00144 bool EngineEditor::maybeApplyChanges()
00145 {
00146 if ( isWindowModified() )
00147 {
00148 switch( QMessageBox::warning( this, trUtf8( "OrK" ), trUtf8( "The current settings were not saved.\nDo you want to apply your changes?" ),QMessageBox::Apply | QMessageBox::Discard | QMessageBox::Cancel, QMessageBox::Apply) )
00149 {
00150 case QMessageBox::Apply :
00151 applyChanges() ;
00152 return TRUE ;
00153 break ;
00154
00155 case QMessageBox::Discard:
00156 setDirty( FALSE ) ;
00157 return TRUE ;
00158 break ;
00159
00160 case QMessageBox::Cancel :
00161 return FALSE;
00162 break ;
00163
00164 default :
00165 return TRUE ;
00166 break ;
00167 }
00168 }
00169 else
00170 return TRUE ;
00171 }
00172
00173 void EngineEditor::changeEngine()
00174 {
00175 if ( sender() == 0 )
00176 return ;
00177
00178 if ( ! maybeApplyChanges() )
00179 return ;
00180
00181 setEngine( dynamic_cast<Engine*>( sender() ) ) ;
00182 setEnabled( TRUE ) ;
00183 _delete->setEnabled( TRUE ) ;
00184 }
00185
00186 void EngineEditor::setEngine( Engine *engine )
00187 {
00188 if ( ! maybeApplyChanges() )
00189 return ;
00190
00191 _engine = engine ;
00192
00193 _nameEdit->setText( _engine->name() ) ;
00194 _addressBox->setValue( _engine->address() ) ;
00195 setImage( _engine->imagePath() ) ;
00196 _decoderBox->setCurrentIndex(
00197 _decoderBox->findData( QVariant( _engine->decoder() ) ) ) ;
00198
00199
00200 setDirty( FALSE ) ;
00201 }