File: filterselectiondialog.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 (146 lines) | stat: -rw-r--r-- 4,471 bytes parent folder | download | duplicates (2)
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
/*
  Copyright (c) 2013, 2014 Montel Laurent <montel@kde.org>

  This program is free software; you can redistribute it and/or modify it
  under the terms of the GNU General Public License, version 2, as
  published by the Free Software Foundation.

  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
*/


#include "filterselectiondialog.h"
#include "mailfilter.h"

#include <KDebug>
#include <KListWidgetSearchLine>
#include <KPushButton>

#include <QListWidget>
#include <QVBoxLayout>


using namespace MailCommon;

FilterSelectionDialog::FilterSelectionDialog( QWidget *parent )
    : KDialog( parent )
{
    setObjectName( QLatin1String("filterselection") );
    setModal( true );
    setCaption( i18n( "Select Filters" ) );
    setButtons( Ok | Cancel );
    setDefaultButton( Ok );
    showButtonSeparator( true );

    QVBoxLayout *const top = new QVBoxLayout( mainWidget() );

    filtersListWidget = new QListWidget();
    KListWidgetSearchLine *searchLine = new KListWidgetSearchLine( this, filtersListWidget );
    searchLine->setClickMessage(
                i18nc( "@info/plain Displayed grayed-out inside the textbox, verb to search",
                       "Search" ) );

    top->addWidget( searchLine );
    top->addWidget( filtersListWidget );
    filtersListWidget->setAlternatingRowColors( true );
    filtersListWidget->setSortingEnabled( false );
    filtersListWidget->setSelectionMode( QAbstractItemView::NoSelection );

    QHBoxLayout *const buttonLayout = new QHBoxLayout();
    top->addLayout( buttonLayout );
    selectAllButton = new KPushButton( i18n( "Select All" ) );
    buttonLayout->addWidget( selectAllButton );
    unselectAllButton = new KPushButton( i18n( "Unselect All" ) );
    buttonLayout->addWidget( unselectAllButton );

    connect( selectAllButton, SIGNAL(clicked()), this, SLOT(slotSelectAllButton()) );
    connect( unselectAllButton, SIGNAL(clicked()), this, SLOT(slotUnselectAllButton()) );

    readConfig();
}

FilterSelectionDialog::~FilterSelectionDialog()
{
    writeConfig();
}

void FilterSelectionDialog::reject()
{
    qDeleteAll(originalFilters);
    QDialog::reject();
}

void FilterSelectionDialog::writeConfig()
{
    KConfigGroup group( KGlobal::config(), "FilterSelectionDialog" );
    group.writeEntry( "Size", size() );
}

void FilterSelectionDialog::readConfig()
{
    KConfigGroup group( KGlobal::config(), "FilterSelectionDialog" );
    const QSize sizeDialog = group.readEntry( "Size", QSize(300, 350) );
    if ( sizeDialog.isValid() ) {
        resize( sizeDialog );
    }
}

void FilterSelectionDialog::setFilters( const QList<MailFilter *> &filters )
{
    if ( filters.isEmpty() ) {
        enableButtonOk( false );
        return;
    }

    originalFilters = filters;
    filtersListWidget->clear();

    foreach ( const MailFilter *filter, filters ) {
        QListWidgetItem *item = new QListWidgetItem( filter->name(), filtersListWidget );
        item->setFlags( Qt::ItemIsUserCheckable | Qt::ItemIsEnabled );
        item->setCheckState( Qt::Checked );
    }
}

QList<MailFilter*> FilterSelectionDialog::selectedFilters() const
{
    QList<MailFilter*> filters;

    const int filterCount = filtersListWidget->count();
    for ( int i = 0; i < filterCount; ++i ) {
        const QListWidgetItem *item = filtersListWidget->item( i );
        if ( item->checkState() == Qt::Checked ) {
            filters << originalFilters[ i ];
        } else {
            delete originalFilters[ i ];
        }
    }

    return filters;
}

void FilterSelectionDialog::slotUnselectAllButton()
{
    const int filterCount = filtersListWidget->count();
    for ( int i = 0; i < filterCount; ++i ) {
        QListWidgetItem * const item = filtersListWidget->item( i );
        item->setCheckState( Qt::Unchecked );
    }
}

void FilterSelectionDialog::slotSelectAllButton()
{
    const int filterCount = filtersListWidget->count();
    for ( int i = 0; i < filterCount; ++i ) {
        QListWidgetItem * const item = filtersListWidget->item( i );
        item->setCheckState( Qt::Checked );
    }
}