File: filtercontroller.cpp

package info (click to toggle)
kdepim 4%3A4.14.1-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 62,764 kB
  • sloc: cpp: 530,022; xml: 5,446; perl: 1,434; sh: 812; ansic: 433; php: 44; makefile: 22
file content (218 lines) | stat: -rw-r--r-- 6,037 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/*
  Copyright (C) 2010 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.net,
    Author: Tobias Koenig <tokoe@kdab.com>

  This library is free software; you can redistribute it and/or modify it
  under the terms of the GNU Library General Public License as published by
  the Free Software Foundation; either version 2 of the License, or (at your
  option) any later version.

  This library 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 Library General Public
  License for more details.

  You should have received a copy of the GNU Library General Public License
  along with this library; see the file COPYING.LIB.  If not, write to the
  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  02110-1301, USA.
*/

#include "filtercontroller.h"
#include "filtereditdialog_p.h"
#include "filtermodel_p.h"

#include <KLocale>
#include <KMessageBox>

#include <QAbstractItemModel>
#include <QAction>
#include <QItemSelectionModel>

using namespace MailCommon;

class FilterController::Private
{
public:

    void selectionChanged();
    void addFilter();
    void editFilter();
    void removeFilter();
    void moveUpFilter();
    void moveDownFilter();

    FilterModel *mModel;
    QItemSelectionModel *mSelectionModel;
    QAction *mAddAction;
    QAction *mEditAction;
    QAction *mRemoveAction;
    QAction *mMoveUpAction;
    QAction *mMoveDownAction;
};

void FilterController::Private::selectionChanged()
{
    const bool filterSelected = mSelectionModel->hasSelection();

    if ( filterSelected ) {
        mEditAction->setEnabled( true );
        mRemoveAction->setEnabled( true );

        const QModelIndex index = mSelectionModel->selectedRows().first();
        mMoveUpAction->setEnabled( index.row() != 0 );
        mMoveDownAction->setEnabled( index.row() != ( mModel->rowCount() - 1 ) );
    } else {
        mEditAction->setEnabled( false );
        mRemoveAction->setEnabled( false );
        mMoveUpAction->setEnabled( false );
        mMoveDownAction->setEnabled( false );
    }
}

void FilterController::Private::addFilter()
{
    mModel->insertRow( mModel->rowCount() );

    FilterEditDialog dlg;
    dlg.setCaption( i18n( "Add Filter" ) );
    dlg.load( mModel->rowCount() - 1 );

    if ( dlg.exec() ) {
        dlg.save();
    } else {
        mModel->removeRow( mModel->rowCount() - 1 );
    }
}

void FilterController::Private::editFilter()
{
    if ( !mSelectionModel->hasSelection() ) {
        return;
    }

    const QModelIndex index = mSelectionModel->selectedRows().first();

    FilterEditDialog dlg;
    dlg.setCaption( i18n( "Edit Filter" ) );
    dlg.load( index.row() );
    if ( dlg.exec() ) {
        dlg.save();
    }
}

void FilterController::Private::removeFilter()
{
    if ( !mSelectionModel->hasSelection() ) {
        return;
    }

    const QModelIndex index = mSelectionModel->selectedRows().first();

    const int result =
            KMessageBox::questionYesNo(
                0,
                i18n( "Do you really want to remove filter <b>%1</b>?",
                      index.data( Qt::DisplayRole ).toString() ),
                i18n( "Remove Filter" ) );

    if ( result == KMessageBox::No ) {
        return;
    }

    mModel->removeRow( index.row() );
}

void FilterController::Private::moveUpFilter()
{
    if ( !mSelectionModel->hasSelection() ) {
        return;
    }

    const QModelIndex index = mSelectionModel->selectedRows().first();
    mModel->moveRow( index.row(), index.row() - 1 );

    // moveRow will reset the model, so restore the selection
    mSelectionModel->select( mModel->index( index.row() - 1, 0 ),
                             QItemSelectionModel::ClearAndSelect );
}

void FilterController::Private::moveDownFilter()
{
    if ( !mSelectionModel->hasSelection() ) {
        return;
    }

    const QModelIndex index = mSelectionModel->selectedRows().first();
    mModel->moveRow( index.row(), index.row() + 1 );

    // moveRow will reset the model, so restore the selection
    mSelectionModel->select( mModel->index( index.row() + 1, 0 ),
                             QItemSelectionModel::ClearAndSelect );
}

FilterController::FilterController( QObject *parent )
    : QObject( parent ), d( new Private )
{
    d->mModel = new FilterModel( this );
    d->mSelectionModel = new QItemSelectionModel( d->mModel );

    d->mAddAction = new QAction( i18n( "Add" ), this );
    d->mEditAction = new QAction( i18n( "Edit" ), this );
    d->mRemoveAction = new QAction( i18n( "Remove" ), this );
    d->mMoveUpAction = new QAction( i18n( "Move Up" ), this );
    d->mMoveDownAction = new QAction( i18n( "Move Down" ), this );

    connect( d->mSelectionModel, SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
             this, SLOT(selectionChanged()) );

    connect( d->mAddAction, SIGNAL(triggered(bool)), SLOT(addFilter()) );
    connect( d->mEditAction, SIGNAL(triggered(bool)), SLOT(editFilter()) );
    connect( d->mRemoveAction, SIGNAL(triggered(bool)), SLOT(removeFilter()) );
    connect( d->mMoveUpAction, SIGNAL(triggered(bool)), SLOT(moveUpFilter()) );
    connect( d->mMoveDownAction, SIGNAL(triggered(bool)), SLOT(moveDownFilter()) );

    d->selectionChanged();
}

FilterController::~FilterController()
{
    delete d;
}

QAbstractItemModel *FilterController::model() const
{
    return d->mModel;
}

QItemSelectionModel *FilterController::selectionModel() const
{
    return d->mSelectionModel;
}

QAction *FilterController::addAction() const
{
    return d->mAddAction;
}

QAction *FilterController::editAction() const
{
    return d->mEditAction;
}

QAction *FilterController::removeAction() const
{
    return d->mRemoveAction;
}

QAction *FilterController::moveUpAction() const
{
    return d->mMoveUpAction;
}

QAction *FilterController::moveDownAction() const
{
    return d->mMoveDownAction;
}

#include "moc_filtercontroller.cpp"