~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

Viewing changes to kwin/clients/oxygen/config/oxygenexceptionlistwidget.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//////////////////////////////////////////////////////////////////////////////
 
2
// oxygenexceptionlistwidget.cpp
 
3
// -------------------
 
4
//
 
5
// Copyright (c) 2009 Hugo Pereira Da Costa <hugo.pereira@free.fr>
 
6
//
 
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:
 
13
//
 
14
// The above copyright notice and this permission notice shall be included in
 
15
// all copies or substantial portions of the Software.
 
16
//
 
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
 
23
// IN THE SOFTWARE.
 
24
//////////////////////////////////////////////////////////////////////////////
 
25
 
 
26
#include "oxygenexceptionlistwidget.h"
 
27
#include "oxygenexceptionlistwidget.moc"
 
28
#include "oxygenexceptiondialog.h"
 
29
 
 
30
#include <QtCore/QSharedPointer>
 
31
#include <KLocale>
 
32
#include <KMessageBox>
 
33
 
 
34
//__________________________________________________________
 
35
namespace Oxygen
 
36
{
 
37
 
 
38
    //__________________________________________________________
 
39
    ExceptionListWidget::ExceptionListWidget( QWidget* parent, Configuration defaultConfiguration ):
 
40
        QWidget( parent ),
 
41
        _defaultConfiguration( defaultConfiguration )
 
42
    {
 
43
 
 
44
        //! ui
 
45
        ui.setupUi( this );
 
46
 
 
47
        // list
 
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 ) );
 
54
 
 
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 ) );
 
61
 
 
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() ) );
 
67
 
 
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() ) );
 
71
 
 
72
        updateButtons();
 
73
        resizeColumns();
 
74
 
 
75
 
 
76
    }
 
77
 
 
78
    //__________________________________________________________
 
79
    void ExceptionListWidget::setExceptions( const ExceptionList& exceptions )
 
80
    {
 
81
        model().set( ExceptionModel::List( exceptions.begin(), exceptions.end() ) );
 
82
        resizeColumns();
 
83
    }
 
84
 
 
85
    //__________________________________________________________
 
86
    ExceptionList ExceptionListWidget::exceptions( void ) const
 
87
    {
 
88
 
 
89
        ExceptionModel::List exceptions( model().get() );
 
90
        ExceptionList out;
 
91
        for( ExceptionModel::List::const_iterator iter = exceptions.begin(); iter != exceptions.end(); ++iter )
 
92
        { out.push_back( *iter ); }
 
93
        return out;
 
94
 
 
95
    }
 
96
 
 
97
    //__________________________________________________________
 
98
    void ExceptionListWidget::updateButtons( void )
 
99
    {
 
100
 
 
101
        bool hasSelection( !ui.exceptionListView->selectionModel()->selectedRows().empty() );
 
102
        ui.removeButton->setEnabled( hasSelection );
 
103
        ui.editButton->setEnabled( hasSelection );
 
104
 
 
105
        ui.moveUpButton->setEnabled( hasSelection && !ui.exceptionListView->selectionModel()->isRowSelected( 0, QModelIndex() ) );
 
106
        ui.moveDownButton->setEnabled( hasSelection && !ui.exceptionListView->selectionModel()->isRowSelected( model().rowCount()-1, QModelIndex() ) );
 
107
 
 
108
    }
 
109
 
 
110
 
 
111
    //_______________________________________________________
 
112
    void ExceptionListWidget::add( void )
 
113
    {
 
114
 
 
115
        // map dialog
 
116
        QSharedPointer<ExceptionDialog> dialog( new ExceptionDialog( this ) );
 
117
        dialog->setException( _defaultConfiguration );
 
118
        if( dialog->exec() == QDialog::Rejected ) return;
 
119
 
 
120
        // retrieve exception and check
 
121
        Exception exception( dialog->exception() );
 
122
        if( !checkException( exception ) ) return;
 
123
 
 
124
        // create new item
 
125
        model().add( exception );
 
126
 
 
127
        // make sure item is selected
 
128
        QModelIndex index( model().index( exception ) );
 
129
        if( index != ui.exceptionListView->selectionModel()->currentIndex() )
 
130
        {
 
131
            ui.exceptionListView->selectionModel()->select( index,  QItemSelectionModel::Clear|QItemSelectionModel::Select|QItemSelectionModel::Rows );
 
132
            ui.exceptionListView->selectionModel()->setCurrentIndex( index,  QItemSelectionModel::Current|QItemSelectionModel::Rows );
 
133
        }
 
134
 
 
135
        resizeColumns();
 
136
        emit changed();
 
137
        return;
 
138
 
 
139
    }
 
140
 
 
141
    //_______________________________________________________
 
142
    void ExceptionListWidget::edit( void )
 
143
    {
 
144
 
 
145
        // retrieve selection
 
146
        QModelIndex current( ui.exceptionListView->selectionModel()->currentIndex() );
 
147
        if( !current.isValid() ) return;
 
148
 
 
149
        Exception& exception( model().get( current ) );
 
150
 
 
151
        // create dialog
 
152
        QSharedPointer<ExceptionDialog> dialog( new ExceptionDialog( this ) );
 
153
        dialog->setException( exception );
 
154
 
 
155
        // map dialog
 
156
        if( dialog->exec() == QDialog::Rejected ) return;
 
157
        Exception newException = dialog->exception();
 
158
 
 
159
        // check if exception was changed
 
160
        if( exception == newException ) return;
 
161
 
 
162
        // check new exception validity
 
163
        if( !checkException( newException ) ) return;
 
164
 
 
165
        // asign new exception
 
166
        *&exception = newException;
 
167
        resizeColumns();
 
168
        emit changed();
 
169
        return;
 
170
 
 
171
    }
 
172
 
 
173
    //_______________________________________________________
 
174
    void ExceptionListWidget::remove( void )
 
175
    {
 
176
 
 
177
        // should use a konfirmation dialog
 
178
        if( KMessageBox::questionYesNo( this, i18n("Remove selected exception?") ) == KMessageBox::No ) return;
 
179
 
 
180
        // remove
 
181
        model().remove( model().get( ui.exceptionListView->selectionModel()->selectedRows() ) );
 
182
        resizeColumns();
 
183
        emit changed();
 
184
        return;
 
185
 
 
186
    }
 
187
 
 
188
    //_______________________________________________________
 
189
    void ExceptionListWidget::toggle( const QModelIndex& index )
 
190
    {
 
191
 
 
192
        if( !index.isValid() ) return;
 
193
        if( index.column() != ExceptionModel::ENABLED ) return;
 
194
 
 
195
        // get matching exception
 
196
        Exception& exception( model().get( index ) );
 
197
        exception.setEnabled( !exception.enabled() );
 
198
        model().add( exception );
 
199
 
 
200
        emit changed();
 
201
        return;
 
202
 
 
203
    }
 
204
 
 
205
    //_______________________________________________________
 
206
    void ExceptionListWidget::up( void )
 
207
    {
 
208
 
 
209
        ExceptionModel::List selection( model().get( ui.exceptionListView->selectionModel()->selectedRows() ) );
 
210
        if( selection.empty() ) { return; }
 
211
 
 
212
        // retrieve selected indexes in list and store in model
 
213
        QModelIndexList selectedIndices( ui.exceptionListView->selectionModel()->selectedRows() );
 
214
        ExceptionModel::List selectedExceptions( model().get( selectedIndices ) );
 
215
 
 
216
        ExceptionModel::List currentException( model().get() );
 
217
        ExceptionModel::List newExceptions;
 
218
 
 
219
        for( ExceptionModel::List::const_iterator iter = currentException.begin(); iter != currentException.end(); ++iter )
 
220
        {
 
221
 
 
222
            // check if new list is not empty, current index is selected and last index is not.
 
223
            // if yes, move.
 
224
            if(
 
225
                !( newExceptions.empty() ||
 
226
                selectedIndices.indexOf( model().index( *iter ) ) == -1 ||
 
227
                selectedIndices.indexOf( model().index( newExceptions.back() ) ) != -1
 
228
                ) )
 
229
            {
 
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 );
 
235
 
 
236
        }
 
237
 
 
238
        model().set( newExceptions );
 
239
 
 
240
        // restore selection
 
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 ); }
 
244
 
 
245
        emit changed();
 
246
        return;
 
247
 
 
248
    }
 
249
 
 
250
    //_______________________________________________________
 
251
    void ExceptionListWidget::down( void )
 
252
    {
 
253
 
 
254
        ExceptionModel::List selection( model().get( ui.exceptionListView->selectionModel()->selectedRows() ) );
 
255
        if( selection.empty() )
 
256
        { return; }
 
257
 
 
258
        // retrieve selected indexes in list and store in model
 
259
        QModelIndexList selectedIndices( ui.exceptionListView->selectionModel()->selectedIndexes() );
 
260
        ExceptionModel::List selectedExceptions( model().get( selectedIndices ) );
 
261
 
 
262
        ExceptionModel::List currentException( model().get() );
 
263
        ExceptionModel::List newExceptions;
 
264
 
 
265
        for( ExceptionModel::List::reverse_iterator iter = currentException.rbegin(); iter != currentException.rend(); ++iter )
 
266
        {
 
267
 
 
268
            // check if new list is not empty, current index is selected and last index is not.
 
269
            // if yes, move.
 
270
            if(
 
271
                !( newExceptions.empty() ||
 
272
                selectedIndices.indexOf( model().index( *iter ) ) == -1 ||
 
273
                selectedIndices.indexOf( model().index( newExceptions.back() ) ) != -1
 
274
                ) )
 
275
            {
 
276
 
 
277
                Exception last( newExceptions.back() );
 
278
                newExceptions.pop_back();
 
279
                newExceptions.push_back( *iter );
 
280
                newExceptions.push_back( last );
 
281
 
 
282
            } else newExceptions.push_back( *iter );
 
283
        }
 
284
 
 
285
        model().set( ExceptionModel::List( newExceptions.rbegin(), newExceptions.rend() ) );
 
286
 
 
287
        // restore selection
 
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 ); }
 
291
 
 
292
        emit changed();
 
293
        return;
 
294
 
 
295
    }
 
296
 
 
297
    //_______________________________________________________
 
298
    void ExceptionListWidget::resizeColumns( void ) const
 
299
    {
 
300
        ui.exceptionListView->resizeColumnToContents( ExceptionModel::ENABLED );
 
301
        ui.exceptionListView->resizeColumnToContents( ExceptionModel::TYPE );
 
302
        ui.exceptionListView->resizeColumnToContents( ExceptionModel::REGEXP );
 
303
    }
 
304
 
 
305
    //_______________________________________________________
 
306
    bool ExceptionListWidget::checkException( Exception& exception )
 
307
    {
 
308
 
 
309
        while( !exception.regExp().isValid() )
 
310
        {
 
311
 
 
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();
 
317
 
 
318
        }
 
319
 
 
320
        return true;
 
321
    }
 
322
 
 
323
}