1
//////////////////////////////////////////////////////////////////////////////
2
// oxygenexceptionlistwidget.cpp
5
// Copyright (c) 2009 Hugo Pereira Da Costa <hugo.pereira@free.fr>
7
// Permission is hereby granted, free of charge, to any person obtaining a copy
8
// of this software and associated documentation files (the "Software"), to
9
// deal in the Software without restriction, including without limitation the
10
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
11
// sell copies of the Software, and to permit persons to whom the Software is
12
// furnished to do so, subject to the following conditions:
14
// The above copyright notice and this permission notice shall be included in
15
// all copies or substantial portions of the Software.
17
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24
//////////////////////////////////////////////////////////////////////////////
26
#include "oxygenexceptionlistwidget.h"
27
#include "oxygenexceptionlistwidget.moc"
28
#include "oxygenexceptiondialog.h"
30
#include <QtCore/QSharedPointer>
32
#include <KMessageBox>
34
//__________________________________________________________
38
//__________________________________________________________
39
ExceptionListWidget::ExceptionListWidget( QWidget* parent, Configuration defaultConfiguration ):
41
_defaultConfiguration( defaultConfiguration )
48
ui.exceptionListView->setAllColumnsShowFocus( true );
49
ui.exceptionListView->setRootIsDecorated( false );
50
ui.exceptionListView->setSortingEnabled( false );
51
ui.exceptionListView->setModel( &model() );
52
ui.exceptionListView->sortByColumn( ExceptionModel::TYPE );
53
ui.exceptionListView->setSizePolicy( QSizePolicy( QSizePolicy::MinimumExpanding, QSizePolicy::Ignored ) );
55
KIconLoader* icon_loader = KIconLoader::global();
56
ui.moveUpButton->setIcon( KIcon( "arrow-up", icon_loader ) );
57
ui.moveDownButton->setIcon( KIcon( "arrow-down", icon_loader ) );
58
ui.addButton->setIcon( KIcon( "list-add", icon_loader ) );
59
ui.removeButton->setIcon( KIcon( "list-remove", icon_loader ) );
60
ui.editButton->setIcon( KIcon( "edit-rename", icon_loader ) );
62
connect( ui.addButton, SIGNAL( clicked() ), SLOT( add() ) );
63
connect( ui.editButton, SIGNAL( clicked() ), SLOT( edit() ) );
64
connect( ui.removeButton, SIGNAL( clicked() ), SLOT( remove() ) );
65
connect( ui.moveUpButton, SIGNAL( clicked() ), SLOT( up() ) );
66
connect( ui.moveDownButton, SIGNAL( clicked() ), SLOT( down() ) );
68
connect( ui.exceptionListView, SIGNAL( activated( const QModelIndex& ) ), SLOT( edit() ) );
69
connect( ui.exceptionListView, SIGNAL( clicked( const QModelIndex& ) ), SLOT( toggle( const QModelIndex& ) ) );
70
connect( ui.exceptionListView->selectionModel(), SIGNAL( selectionChanged(const QItemSelection &, const QItemSelection &) ), SLOT( updateButtons() ) );
78
//__________________________________________________________
79
void ExceptionListWidget::setExceptions( const ExceptionList& exceptions )
81
model().set( ExceptionModel::List( exceptions.begin(), exceptions.end() ) );
85
//__________________________________________________________
86
ExceptionList ExceptionListWidget::exceptions( void ) const
89
ExceptionModel::List exceptions( model().get() );
91
for( ExceptionModel::List::const_iterator iter = exceptions.begin(); iter != exceptions.end(); ++iter )
92
{ out.push_back( *iter ); }
97
//__________________________________________________________
98
void ExceptionListWidget::updateButtons( void )
101
bool hasSelection( !ui.exceptionListView->selectionModel()->selectedRows().empty() );
102
ui.removeButton->setEnabled( hasSelection );
103
ui.editButton->setEnabled( hasSelection );
105
ui.moveUpButton->setEnabled( hasSelection && !ui.exceptionListView->selectionModel()->isRowSelected( 0, QModelIndex() ) );
106
ui.moveDownButton->setEnabled( hasSelection && !ui.exceptionListView->selectionModel()->isRowSelected( model().rowCount()-1, QModelIndex() ) );
111
//_______________________________________________________
112
void ExceptionListWidget::add( void )
116
QSharedPointer<ExceptionDialog> dialog( new ExceptionDialog( this ) );
117
dialog->setException( _defaultConfiguration );
118
if( dialog->exec() == QDialog::Rejected ) return;
120
// retrieve exception and check
121
Exception exception( dialog->exception() );
122
if( !checkException( exception ) ) return;
125
model().add( exception );
127
// make sure item is selected
128
QModelIndex index( model().index( exception ) );
129
if( index != ui.exceptionListView->selectionModel()->currentIndex() )
131
ui.exceptionListView->selectionModel()->select( index, QItemSelectionModel::Clear|QItemSelectionModel::Select|QItemSelectionModel::Rows );
132
ui.exceptionListView->selectionModel()->setCurrentIndex( index, QItemSelectionModel::Current|QItemSelectionModel::Rows );
141
//_______________________________________________________
142
void ExceptionListWidget::edit( void )
145
// retrieve selection
146
QModelIndex current( ui.exceptionListView->selectionModel()->currentIndex() );
147
if( !current.isValid() ) return;
149
Exception& exception( model().get( current ) );
152
QSharedPointer<ExceptionDialog> dialog( new ExceptionDialog( this ) );
153
dialog->setException( exception );
156
if( dialog->exec() == QDialog::Rejected ) return;
157
Exception newException = dialog->exception();
159
// check if exception was changed
160
if( exception == newException ) return;
162
// check new exception validity
163
if( !checkException( newException ) ) return;
165
// asign new exception
166
*&exception = newException;
173
//_______________________________________________________
174
void ExceptionListWidget::remove( void )
177
// should use a konfirmation dialog
178
if( KMessageBox::questionYesNo( this, i18n("Remove selected exception?") ) == KMessageBox::No ) return;
181
model().remove( model().get( ui.exceptionListView->selectionModel()->selectedRows() ) );
188
//_______________________________________________________
189
void ExceptionListWidget::toggle( const QModelIndex& index )
192
if( !index.isValid() ) return;
193
if( index.column() != ExceptionModel::ENABLED ) return;
195
// get matching exception
196
Exception& exception( model().get( index ) );
197
exception.setEnabled( !exception.enabled() );
198
model().add( exception );
205
//_______________________________________________________
206
void ExceptionListWidget::up( void )
209
ExceptionModel::List selection( model().get( ui.exceptionListView->selectionModel()->selectedRows() ) );
210
if( selection.empty() ) { return; }
212
// retrieve selected indexes in list and store in model
213
QModelIndexList selectedIndices( ui.exceptionListView->selectionModel()->selectedRows() );
214
ExceptionModel::List selectedExceptions( model().get( selectedIndices ) );
216
ExceptionModel::List currentException( model().get() );
217
ExceptionModel::List newExceptions;
219
for( ExceptionModel::List::const_iterator iter = currentException.begin(); iter != currentException.end(); ++iter )
222
// check if new list is not empty, current index is selected and last index is not.
225
!( newExceptions.empty() ||
226
selectedIndices.indexOf( model().index( *iter ) ) == -1 ||
227
selectedIndices.indexOf( model().index( newExceptions.back() ) ) != -1
230
Exception last( newExceptions.back() );
231
newExceptions.pop_back();
232
newExceptions.push_back( *iter );
233
newExceptions.push_back( last );
234
} else newExceptions.push_back( *iter );
238
model().set( newExceptions );
241
ui.exceptionListView->selectionModel()->select( model().index( selectedExceptions.front() ), QItemSelectionModel::Clear|QItemSelectionModel::Select|QItemSelectionModel::Rows );
242
for( ExceptionModel::List::const_iterator iter = selectedExceptions.begin(); iter != selectedExceptions.end(); ++iter )
243
{ ui.exceptionListView->selectionModel()->select( model().index( *iter ), QItemSelectionModel::Select|QItemSelectionModel::Rows ); }
250
//_______________________________________________________
251
void ExceptionListWidget::down( void )
254
ExceptionModel::List selection( model().get( ui.exceptionListView->selectionModel()->selectedRows() ) );
255
if( selection.empty() )
258
// retrieve selected indexes in list and store in model
259
QModelIndexList selectedIndices( ui.exceptionListView->selectionModel()->selectedIndexes() );
260
ExceptionModel::List selectedExceptions( model().get( selectedIndices ) );
262
ExceptionModel::List currentException( model().get() );
263
ExceptionModel::List newExceptions;
265
for( ExceptionModel::List::reverse_iterator iter = currentException.rbegin(); iter != currentException.rend(); ++iter )
268
// check if new list is not empty, current index is selected and last index is not.
271
!( newExceptions.empty() ||
272
selectedIndices.indexOf( model().index( *iter ) ) == -1 ||
273
selectedIndices.indexOf( model().index( newExceptions.back() ) ) != -1
277
Exception last( newExceptions.back() );
278
newExceptions.pop_back();
279
newExceptions.push_back( *iter );
280
newExceptions.push_back( last );
282
} else newExceptions.push_back( *iter );
285
model().set( ExceptionModel::List( newExceptions.rbegin(), newExceptions.rend() ) );
288
ui.exceptionListView->selectionModel()->select( model().index( selectedExceptions.front() ), QItemSelectionModel::Clear|QItemSelectionModel::Select|QItemSelectionModel::Rows );
289
for( ExceptionModel::List::const_iterator iter = selectedExceptions.begin(); iter != selectedExceptions.end(); ++iter )
290
{ ui.exceptionListView->selectionModel()->select( model().index( *iter ), QItemSelectionModel::Select|QItemSelectionModel::Rows ); }
297
//_______________________________________________________
298
void ExceptionListWidget::resizeColumns( void ) const
300
ui.exceptionListView->resizeColumnToContents( ExceptionModel::ENABLED );
301
ui.exceptionListView->resizeColumnToContents( ExceptionModel::TYPE );
302
ui.exceptionListView->resizeColumnToContents( ExceptionModel::REGEXP );
305
//_______________________________________________________
306
bool ExceptionListWidget::checkException( Exception& exception )
309
while( !exception.regExp().isValid() )
312
KMessageBox::error( this, i18n("Regular Expression syntax is incorrect") );
313
QSharedPointer<ExceptionDialog> dialog( new ExceptionDialog( this ) );
314
dialog->setException( exception );
315
if( dialog->exec() == QDialog::Rejected ) return false;
316
exception = dialog->exception();