~ubuntu-branches/ubuntu/quantal/kdepimlibs/quantal-proposed

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
/*
    Copyright 2008 Ingo Klöcker <kloecker@kde.org>

    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 "collectionrequester.h"
#include "collectiondialog.h"

#include <klineedit.h>
#include <klocale.h>
#include <kpushbutton.h>
#include <kstandardshortcut.h>

#include <QtCore/QEvent>
#include <QtGui/QAction>
#include <QtGui/QApplication>

using namespace Akonadi;

class CollectionRequester::Private
{
  public:
    Private( CollectionRequester *parent )
      : q( parent ),
        edit( 0 ),
        button( 0 ),
        collectionDialog( 0 )
    {
    }

    ~Private()
    {
    }

    void init();

    // slots
    void _k_slotOpenDialog();

    CollectionRequester *q;
    Collection collection;
    KLineEdit *edit;
    KPushButton *button;
    CollectionDialog *collectionDialog;
};


void CollectionRequester::Private::init()
{
  q->setMargin( 0 );

  edit = new KLineEdit( q );
  edit->setReadOnly( true );
  edit->setClickMessage( i18n( "No Folder" ) );
  edit->setClearButtonShown( false );
  edit->setFocusPolicy( Qt::NoFocus );

  button = new KPushButton( q );
  button->setIcon( KIcon( QLatin1String( "document-open" ) ) );
  const int buttonSize = edit->sizeHint().height();
  button->setFixedSize( buttonSize, buttonSize );
  button->setToolTip( i18n( "Open collection dialog" ) );

  q->setSpacing( -1 );

  edit->installEventFilter( q );
  q->setFocusProxy( button );
  q->setFocusPolicy( Qt::StrongFocus );

  q->connect( button, SIGNAL(clicked()), q, SLOT(_k_slotOpenDialog()) );

  QAction *openAction = new QAction( q );
  openAction->setShortcut( KStandardShortcut::Open );
  q->connect( openAction, SIGNAL(triggered(bool)), q, SLOT(_k_slotOpenDialog()) );

  collectionDialog = new CollectionDialog( q );
  collectionDialog->setCaption( i18n( "Select a collection" ) );
  collectionDialog->setSelectionMode( QAbstractItemView::SingleSelection );
}

void CollectionRequester::Private::_k_slotOpenDialog()
{
  CollectionDialog *dlg = collectionDialog;

  if ( dlg->exec() != QDialog::Accepted )
    return;

  const Akonadi::Collection collection = dlg->selectedCollection();
  q->setCollection( collection );
  emit q->collectionChanged( collection );
}

CollectionRequester::CollectionRequester( QWidget *parent )
  : KHBox( parent ),
    d( new Private( this ) )
{
  d->init();
}


CollectionRequester::CollectionRequester( const Akonadi::Collection &collection, QWidget *parent )
  : KHBox( parent ),
    d( new Private( this ) )
{
  d->init();
  setCollection( collection );
}


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


Collection CollectionRequester::collection() const
{
  return d->collection;
}


void CollectionRequester::setCollection( const Collection& collection )
{
  d->collection = collection;
  d->edit->setText( collection.isValid() ? collection.name() : QString() );
  emit collectionChanged( collection );
}

void CollectionRequester::setMimeTypeFilter( const QStringList &mimeTypes )
{
  if ( d->collectionDialog )
    d->collectionDialog->setMimeTypeFilter( mimeTypes );
}

QStringList CollectionRequester::mimeTypeFilter() const
{
  if ( d->collectionDialog )
    return d->collectionDialog->mimeTypeFilter();
  else
    return QStringList();
}

void CollectionRequester::setAccessRightsFilter( Collection::Rights rights )
{
  if ( d->collectionDialog )
    d->collectionDialog->setAccessRightsFilter( rights );
}

Collection::Rights CollectionRequester::accessRightsFilter() const
{
  if ( d->collectionDialog )
    return d->collectionDialog->accessRightsFilter();
  else
    return Akonadi::Collection::ReadOnly;
}

void CollectionRequester::changeCollectionDialogOptions( CollectionDialog::CollectionDialogOptions options )
{
  if ( d->collectionDialog )
    d->collectionDialog->changeCollectionDialogOptions( options );
}

#include "collectionrequester.moc"