summaryrefslogtreecommitdiff
path: root/library/fileselector.cpp
blob: 013f43afaf4f2b3d5d74d542e50c16d4248e525f (plain)
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/**********************************************************************
** Copyright (C) 2000 Trolltech AS.  All rights reserved.
**
** This file is part of Qtopia Environment.
**
** This file may be distributed and/or modified under the terms of the
** GNU General Public License version 2 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
** See http://www.trolltech.com/gpl/ for GPL licensing information.
**
** Contact info@trolltech.com if any conditions of this licensing are
** not clear to you.
**
**********************************************************************/

// WARNING: Do *NOT* define this yourself. The SL5xxx from SHARP does NOT
//      have this class.
#define QTOPIA_INTERNAL_FSLP

#include "fileselector.h"
#include "fileselector_p.h"
#include "global.h"
#include "resource.h"
#include "config.h"
#include "applnk.h"
#include "storage.h"
#include "qpemenubar.h"
#include "qcopchannel_qws.h"
#include "lnkproperties.h"
#include "applnk.h"
#include "qpeapplication.h"
#include "categorymenu.h"

#include <stdlib.h>

#include <qdir.h>
#include <qwidget.h>
#include <qpopupmenu.h>
#include <qtoolbutton.h>
#include <qpushbutton.h>
#include <qheader.h>
#include <qtooltip.h>


FileSelectorItem::FileSelectorItem( QListView *parent, const DocLnk &f )
    : QListViewItem( parent ), fl( f )
{
    setText( 0, f.name() );
    setPixmap( 0, f.pixmap() );
}

FileSelectorItem::~FileSelectorItem()
{
}

class FileSelectorViewPrivate
{
public:
    CategoryMenu *cm;
    bool m_noItems:1;
};

FileSelectorView::FileSelectorView( const QString &f, QWidget *parent, const char *name )
    : QListView( parent, name ), filter( f ), count( 0 )
{
    d = new FileSelectorViewPrivate();
    d->cm = 0;
    d->m_noItems = false;
    setAllColumnsShowFocus( TRUE );
    addColumn( tr( "Name" ) );
    header()->hide();

    fileManager = new FileManager;
    reread();
    QCopChannel *channel = new QCopChannel( "QPE/Card", this );
    connect( channel, SIGNAL(received(const QCString &, const QByteArray &)),
	     this, SLOT(cardMessage( const QCString &, const QByteArray &)) );
}

FileSelectorView::~FileSelectorView()
{
}

void FileSelectorView::reread()
{
    QString oldFile;
    FileSelectorItem *item;
    if( !d->m_noItems ) { // there are items
      item = (FileSelectorItem *)selectedItem();
      if ( item )
	  oldFile = item->file().file();
    }
    clear();
    DocLnkSet files;
    Global::findDocuments(&files, filter);
    count = files.children().count();
    if(count == 0 ){ // No Documents
      d->m_noItems = true;
      QListViewItem *it = new QListViewItem(this, tr("There are no files in this directory." ), "empty" );
      it->setSelectable(FALSE );
      return;
    }
    QListIterator<DocLnk> dit( files.children() );
    for ( ; dit.current(); ++dit ) {
	if (d->cm)
	    if (!d->cm->isSelected((**dit).categories()))
		continue;
        item = new FileSelectorItem( this, **dit );
	if ( item->file().file() == oldFile )
	    setCurrentItem( item );
    }
    if ( !selectedItem() )
	setCurrentItem( firstChild() );
}

void FileSelectorView::setCategoryFilter(CategoryMenu *cm)
{
    d->cm = cm;
    connect(cm, SIGNAL(categoryChange()), this, SLOT(categoryChanged()) );
}

void FileSelectorView::categoryChanged() { reread(); }

void FileSelectorView::cardMessage( const QCString &msg, const QByteArray &)
{
    if ( msg == "mtabChanged()" )
	reread();
}

void FileSelectorView::keyPressEvent( QKeyEvent *e )
{
    QString txt = e->text();
    if (e->key() == Key_Space)
	emit returnPressed( currentItem() );
    else if ( !txt.isNull() && txt[0] > ' ' && e->key() < 0x1000 )
	e->ignore();
    else
	QListView::keyPressEvent(e);
}

class FileSelectorPrivate
{
public:
    CategoryMenu *cm;
    QMenuBar *mb;
};

/*!
  \class FileSelector fileselector.h
  \brief The FileSelector widget allows the user to select DocLnk objects.
*/

/*!
  Constructs a FileSelector with mime filter \a f.
  The standard Qt \a parent and \a name parameters are passed to the
  parent.

  If \a newVisible is TRUE, the widget has an button allowing the user
  the create "new" documents - editor applications will have this while
  viewer applications will not.

  If \a closeVisible is TRUE, the widget has an button allowinf the user
  to select "no document".

  \sa DocLnkSet::DocLnkSet()
*/
FileSelector::FileSelector( const QString &f, QWidget *parent, const char *name, bool newVisible, bool closeVisible )
    : QVBox( parent, name ), filter( f )
{
    setMargin( 0 );
    setSpacing( 0 );
    QHBox *top = new QHBox( this );
    top->setBackgroundMode( PaletteButton );	// same colour as toolbars
    top->setSpacing( 0 );

    QWidget *spacer = new QWidget( top );
    spacer->setBackgroundMode( PaletteButton );

    d = new FileSelectorPrivate();
    d->mb = new QMenuBar(spacer);
    d->cm = new CategoryMenu("Document View", this);
    QPEMenuToolFocusManager::manager()->addWidget( d->mb );
    d->mb->insertItem(tr("View"), d->cm);


    QToolButton *tb = new QToolButton( top );
    tb->setPixmap( Resource::loadPixmap( "new" ) );
    connect( tb, SIGNAL( clicked() ), this, SLOT( createNew() ) );
    buttonNew = tb;
    tb->setFixedSize( 18, 20 ); // tb->sizeHint() );
    tb->setAutoRaise( TRUE );
    QToolTip::add( tb, tr( "Create a new Document" ) );
    QPEMenuToolFocusManager::manager()->addWidget( tb );

    tb = new QToolButton( top );
    tb->setPixmap( Resource::loadPixmap( "close" ) );
    connect( tb, SIGNAL( clicked() ), this, SIGNAL( closeMe() ) );
    buttonClose = tb;
    tb->setFixedSize( 18, 20 ); // tb->sizeHint() );
    tb->setAutoRaise( TRUE );
    QToolTip::add( tb, tr( "Close the File Selector" ) );
    QPEMenuToolFocusManager::manager()->addWidget( tb );

    view = new FileSelectorView( filter, this, "fileview" );
    view->setCategoryFilter(d->cm);
    QPEApplication::setStylusOperation( view->viewport(), QPEApplication::RightOnHold );
    connect( view, SIGNAL( mouseButtonClicked( int, QListViewItem *, const QPoint &, int ) ),
	     this, SLOT( fileClicked( int, QListViewItem *, const QPoint &, int ) ) );
    connect( view, SIGNAL( mouseButtonPressed( int, QListViewItem *, const QPoint &, int ) ),
	     this, SLOT( filePressed( int, QListViewItem *, const QPoint &, int ) ) );
    connect( view, SIGNAL( returnPressed( QListViewItem * ) ),
	     this, SLOT( fileClicked( QListViewItem * ) ) );

    setNewVisible( newVisible );
    setCloseVisible( closeVisible );
}

/*!
  Destroys the widget.
*/
FileSelector::~FileSelector()
{

}

/*!
  Returns the number of files in the view. If this is zero, and editor
  application might avoid using the selector and immediately start with
  a "new" document.
*/
int FileSelector::fileCount()
{
    return view->fileCount();
}

/*!
  Causes the file selector to act as if the "new" button was chosen.

  \sa newSelected(), closeMe()
*/
void FileSelector::createNew()
{
    DocLnk f;
    emit newSelected( f );
    emit closeMe();
}

void FileSelector::fileClicked( int button, QListViewItem *i, const QPoint &, int )
{
    if ( !i )
	return;
    if(i->text(1) == QString::fromLatin1("empty" ) )
      return; 

    if ( button == Qt::LeftButton ) {
	fileClicked( i );
    }
}

void FileSelector::filePressed( int button, QListViewItem *i, const QPoint &, int )
{
    if ( !i )
	return;
    if(i->text(1) == QString::fromLatin1("empty" ) )
      return; 

    if ( button == Qt::RightButton ) {
	DocLnk l = ((FileSelectorItem *)i)->file();
	LnkProperties prop( &l );
	prop.showMaximized();
	prop.exec();
	d->cm->reload();
	reread();
    }
}

void FileSelector::fileClicked( QListViewItem *i )
{
    if ( !i )
	return;
    emit fileSelected( ( (FileSelectorItem*)i )->file() );
    emit closeMe();
}

/*!
  Returns the selected DocLnk. The caller is responsible for deleting
  the returned value.
*/
const DocLnk *FileSelector::selected()
{
    FileSelectorItem *item = (FileSelectorItem *)view->selectedItem();
    if ( item )
	return new DocLnk( item->file() );
    return NULL;
}

/*!
  \fn void FileSelector::fileSelected( const DocLnk &f )

  This signal is emitted when the user selects a file.
  \a f is the file.
*/

/*!
  \fn void FileSelector::newSelected( const DocLnk &f )

  This signal is emitted when the user selects "new" file.
  \a f is a DocLnk for the file. You will need to set the type
  of the value after copying it.
*/

/*!
  \fn void FileSelector::closeMe()

  This signal is emitted when the user no longer needs to view the widget.
*/


/*!
  Sets whether a "new document" button is visible, according to \a b.
*/
void FileSelector::setNewVisible( bool b )
{
    if ( b )
	buttonNew->show();
    else
	buttonNew->hide();
}

/*!
  Sets whether a "no document" button is visible, according to \a b.
*/
void FileSelector::setCloseVisible( bool b )
{
    if ( b )
	buttonClose->show();
    else
	buttonClose->hide();
}

/*!
  Rereads the list of files.
*/
void FileSelector::reread()
{
    view->reread();
}