-rw-r--r-- | library/fileselector.cpp | 340 |
1 files changed, 340 insertions, 0 deletions
diff --git a/library/fileselector.cpp b/library/fileselector.cpp new file mode 100644 index 0000000..365f383 --- a/dev/null +++ b/library/fileselector.cpp | |||
@@ -0,0 +1,340 @@ | |||
1 | /********************************************************************** | ||
2 | ** Copyright (C) 2000 Trolltech AS. All rights reserved. | ||
3 | ** | ||
4 | ** This file is part of Qtopia Environment. | ||
5 | ** | ||
6 | ** This file may be distributed and/or modified under the terms of the | ||
7 | ** GNU General Public License version 2 as published by the Free Software | ||
8 | ** Foundation and appearing in the file LICENSE.GPL included in the | ||
9 | ** packaging of this file. | ||
10 | ** | ||
11 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE | ||
12 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. | ||
13 | ** | ||
14 | ** See http://www.trolltech.com/gpl/ for GPL licensing information. | ||
15 | ** | ||
16 | ** Contact info@trolltech.com if any conditions of this licensing are | ||
17 | ** not clear to you. | ||
18 | ** | ||
19 | **********************************************************************/ | ||
20 | #include "fileselector.h" | ||
21 | #include "global.h" | ||
22 | #include "resource.h" | ||
23 | #include "config.h" | ||
24 | #include "applnk.h" | ||
25 | #include "storage.h" | ||
26 | #include "qpemenubar.h" | ||
27 | #include "qcopchannel_qws.h" | ||
28 | #include "lnkproperties.h" | ||
29 | #include "applnk.h" | ||
30 | #include "qpeapplication.h" | ||
31 | #include "categorymenu.h" | ||
32 | |||
33 | #include <stdlib.h> | ||
34 | |||
35 | #include <qdir.h> | ||
36 | #include <qwidget.h> | ||
37 | #include <qpopupmenu.h> | ||
38 | #include <qtoolbutton.h> | ||
39 | #include <qpushbutton.h> | ||
40 | #include <qheader.h> | ||
41 | #include <qtooltip.h> | ||
42 | |||
43 | |||
44 | FileSelectorItem::FileSelectorItem( QListView *parent, const DocLnk &f ) | ||
45 | : QListViewItem( parent ), fl( f ) | ||
46 | { | ||
47 | setText( 0, f.name() ); | ||
48 | setPixmap( 0, f.pixmap() ); | ||
49 | } | ||
50 | |||
51 | FileSelectorItem::~FileSelectorItem() | ||
52 | { | ||
53 | } | ||
54 | |||
55 | class FileSelectorViewPrivate | ||
56 | { | ||
57 | public: | ||
58 | CategoryMenu *cm; | ||
59 | }; | ||
60 | |||
61 | FileSelectorView::FileSelectorView( const QString &f, QWidget *parent, const char *name ) | ||
62 | : QListView( parent, name ), filter( f ), count( 0 ) | ||
63 | { | ||
64 | d = new FileSelectorViewPrivate(); | ||
65 | d->cm = 0; | ||
66 | setAllColumnsShowFocus( TRUE ); | ||
67 | addColumn( tr( "Name" ) ); | ||
68 | header()->hide(); | ||
69 | |||
70 | fileManager = new FileManager; | ||
71 | reread(); | ||
72 | QCopChannel *channel = new QCopChannel( "QPE/Card", this ); | ||
73 | connect( channel, SIGNAL(received(const QCString &, const QByteArray &)), | ||
74 | this, SLOT(cardMessage( const QCString &, const QByteArray &)) ); | ||
75 | } | ||
76 | |||
77 | FileSelectorView::~FileSelectorView() | ||
78 | { | ||
79 | } | ||
80 | |||
81 | void FileSelectorView::reread() | ||
82 | { | ||
83 | FileSelectorItem *item = (FileSelectorItem *)selectedItem(); | ||
84 | QString oldFile; | ||
85 | if ( item ) | ||
86 | oldFile = item->file().file(); | ||
87 | clear(); | ||
88 | DocLnkSet files; | ||
89 | Global::findDocuments(&files, filter); | ||
90 | count = files.children().count(); | ||
91 | QListIterator<DocLnk> dit( files.children() ); | ||
92 | for ( ; dit.current(); ++dit ) { | ||
93 | if (d->cm) | ||
94 | if (!d->cm->isSelected((**dit).categories())) | ||
95 | continue; | ||
96 | item = new FileSelectorItem( this, **dit ); | ||
97 | if ( item->file().file() == oldFile ) | ||
98 | setCurrentItem( item ); | ||
99 | } | ||
100 | if ( !selectedItem() ) | ||
101 | setCurrentItem( firstChild() ); | ||
102 | } | ||
103 | |||
104 | void FileSelectorView::setCategoryFilter(CategoryMenu *cm) | ||
105 | { | ||
106 | d->cm = cm; | ||
107 | connect(cm, SIGNAL(categoryChange()), this, SLOT(categoryChanged()) ); | ||
108 | } | ||
109 | |||
110 | void FileSelectorView::categoryChanged() { reread(); } | ||
111 | |||
112 | void FileSelectorView::cardMessage( const QCString &msg, const QByteArray &) | ||
113 | { | ||
114 | if ( msg == "mtabChanged()" ) | ||
115 | reread(); | ||
116 | } | ||
117 | |||
118 | void FileSelectorView::keyPressEvent( QKeyEvent *e ) | ||
119 | { | ||
120 | QString txt = e->text(); | ||
121 | if (e->key() == Key_Space) | ||
122 | emit returnPressed( currentItem() ); | ||
123 | else if ( !txt.isNull() && txt[0] > ' ' && e->key() < 0x1000 ) | ||
124 | e->ignore(); | ||
125 | else | ||
126 | QListView::keyPressEvent(e); | ||
127 | } | ||
128 | |||
129 | class FileSelectorPrivate | ||
130 | { | ||
131 | public: | ||
132 | CategoryMenu *cm; | ||
133 | QMenuBar *mb; | ||
134 | }; | ||
135 | |||
136 | /*! | ||
137 | \class FileSelector fileselector.h | ||
138 | \brief The FileSelector widget allows the user to select DocLnk objects. | ||
139 | */ | ||
140 | |||
141 | /*! | ||
142 | Constructs a FileSelector with mime filter \a f. | ||
143 | The standard Qt \a parent and \a name parameters are passed to the | ||
144 | parent. | ||
145 | |||
146 | If \a newVisible is TRUE, the widget has an button allowing the user | ||
147 | the create "new" documents - editor applications will have this while | ||
148 | viewer applications will not. | ||
149 | |||
150 | If \a closeVisible is TRUE, the widget has an button allowinf the user | ||
151 | to select "no document". | ||
152 | |||
153 | \sa DocLnkSet::DocLnkSet() | ||
154 | */ | ||
155 | FileSelector::FileSelector( const QString &f, QWidget *parent, const char *name, bool newVisible, bool closeVisible ) | ||
156 | : QVBox( parent, name ), filter( f ) | ||
157 | { | ||
158 | setMargin( 0 ); | ||
159 | setSpacing( 0 ); | ||
160 | QHBox *top = new QHBox( this ); | ||
161 | top->setBackgroundMode( PaletteButton );// same colour as toolbars | ||
162 | top->setSpacing( 0 ); | ||
163 | |||
164 | QWidget *spacer = new QWidget( top ); | ||
165 | spacer->setBackgroundMode( PaletteButton ); | ||
166 | |||
167 | d = new FileSelectorPrivate(); | ||
168 | d->mb = new QMenuBar(spacer); | ||
169 | d->cm = new CategoryMenu("Document View", this); | ||
170 | QPEMenuToolFocusManager::manager()->addWidget( d->mb ); | ||
171 | d->mb->insertItem(tr("View"), d->cm); | ||
172 | |||
173 | |||
174 | QToolButton *tb = new QToolButton( top ); | ||
175 | tb->setPixmap( Resource::loadPixmap( "new" ) ); | ||
176 | connect( tb, SIGNAL( clicked() ), this, SLOT( createNew() ) ); | ||
177 | buttonNew = tb; | ||
178 | tb->setFixedSize( 18, 20 ); // tb->sizeHint() ); | ||
179 | tb->setAutoRaise( TRUE ); | ||
180 | QToolTip::add( tb, tr( "Create a new Document" ) ); | ||
181 | QPEMenuToolFocusManager::manager()->addWidget( tb ); | ||
182 | |||
183 | tb = new QToolButton( top ); | ||
184 | tb->setPixmap( Resource::loadPixmap( "close" ) ); | ||
185 | connect( tb, SIGNAL( clicked() ), this, SIGNAL( closeMe() ) ); | ||
186 | buttonClose = tb; | ||
187 | tb->setFixedSize( 18, 20 ); // tb->sizeHint() ); | ||
188 | tb->setAutoRaise( TRUE ); | ||
189 | QToolTip::add( tb, tr( "Close the File Selector" ) ); | ||
190 | QPEMenuToolFocusManager::manager()->addWidget( tb ); | ||
191 | |||
192 | view = new FileSelectorView( filter, this, "fileview" ); | ||
193 | view->setCategoryFilter(d->cm); | ||
194 | QPEApplication::setStylusOperation( view->viewport(), QPEApplication::RightOnHold ); | ||
195 | connect( view, SIGNAL( mouseButtonClicked( int, QListViewItem *, const QPoint &, int ) ), | ||
196 | this, SLOT( fileClicked( int, QListViewItem *, const QPoint &, int ) ) ); | ||
197 | connect( view, SIGNAL( mouseButtonPressed( int, QListViewItem *, const QPoint &, int ) ), | ||
198 | this, SLOT( filePressed( int, QListViewItem *, const QPoint &, int ) ) ); | ||
199 | connect( view, SIGNAL( returnPressed( QListViewItem * ) ), | ||
200 | this, SLOT( fileClicked( QListViewItem * ) ) ); | ||
201 | |||
202 | setNewVisible( newVisible ); | ||
203 | setCloseVisible( closeVisible ); | ||
204 | } | ||
205 | |||
206 | /*! | ||
207 | Destroys the widget. | ||
208 | */ | ||
209 | FileSelector::~FileSelector() | ||
210 | { | ||
211 | |||
212 | } | ||
213 | |||
214 | /*! | ||
215 | Returns the number of files in the view. If this is zero, and editor | ||
216 | application might avoid using the selector and immediately start with | ||
217 | a "new" document. | ||
218 | */ | ||
219 | int FileSelector::fileCount() | ||
220 | { | ||
221 | return view->fileCount(); | ||
222 | } | ||
223 | |||
224 | /*! | ||
225 | Causes the file selector to act as if the "new" button was chosen. | ||
226 | |||
227 | \sa newSelected(), closeMe() | ||
228 | */ | ||
229 | void FileSelector::createNew() | ||
230 | { | ||
231 | DocLnk f; | ||
232 | emit newSelected( f ); | ||
233 | emit closeMe(); | ||
234 | } | ||
235 | |||
236 | void FileSelector::fileClicked( int button, QListViewItem *i, const QPoint &, int ) | ||
237 | { | ||
238 | if ( !i ) | ||
239 | return; | ||
240 | if ( button == Qt::LeftButton ) { | ||
241 | fileClicked( i ); | ||
242 | } | ||
243 | } | ||
244 | |||
245 | void FileSelector::filePressed( int button, QListViewItem *i, const QPoint &, int ) | ||
246 | { | ||
247 | if ( !i ) | ||
248 | return; | ||
249 | if ( button == Qt::RightButton ) { | ||
250 | DocLnk l = ((FileSelectorItem *)i)->file(); | ||
251 | LnkProperties prop( &l ); | ||
252 | prop.showMaximized(); | ||
253 | prop.exec(); | ||
254 | d->cm->reload(); | ||
255 | reread(); | ||
256 | } | ||
257 | } | ||
258 | |||
259 | void FileSelector::fileClicked( QListViewItem *i ) | ||
260 | { | ||
261 | if ( !i ) | ||
262 | return; | ||
263 | emit fileSelected( ( (FileSelectorItem*)i )->file() ); | ||
264 | emit closeMe(); | ||
265 | } | ||
266 | |||
267 | /*! | ||
268 | Returns the selected DocLnk. The caller is responsible for deleting | ||
269 | the returned value. | ||
270 | */ | ||
271 | const DocLnk *FileSelector::selected() | ||
272 | { | ||
273 | FileSelectorItem *item = (FileSelectorItem *)view->selectedItem(); | ||
274 | if ( item ) | ||
275 | return new DocLnk( item->file() ); | ||
276 | return NULL; | ||
277 | } | ||
278 | |||
279 | /*! | ||
280 | \fn void FileSelector::fileSelected( const DocLnk &f ) | ||
281 | |||
282 | This signal is emitted when the user selects a file. | ||
283 | \a f is the file. | ||
284 | */ | ||
285 | |||
286 | /*! | ||
287 | \fn void FileSelector::newSelected( const DocLnk &f ) | ||
288 | |||
289 | This signal is emitted when the user selects "new" file. | ||
290 | \a f is a DocLnk for the file. You will need to set the type | ||
291 | of the value after copying it. | ||
292 | */ | ||
293 | |||
294 | /*! | ||
295 | \fn void FileSelector::closeMe() | ||
296 | |||
297 | This signal is emitted when the user no longer needs to view the widget. | ||
298 | */ | ||
299 | |||
300 | |||
301 | /*! | ||
302 | Sets whether a "new document" button is visible, according to \a b. | ||
303 | */ | ||
304 | void FileSelector::setNewVisible( bool b ) | ||
305 | { | ||
306 | if ( b ) | ||
307 | buttonNew->show(); | ||
308 | else | ||
309 | buttonNew->hide(); | ||
310 | } | ||
311 | |||
312 | /*! | ||
313 | Sets whether a "no document" button is visible, according to \a b. | ||
314 | */ | ||
315 | void FileSelector::setCloseVisible( bool b ) | ||
316 | { | ||
317 | if ( b ) | ||
318 | buttonClose->show(); | ||
319 | else | ||
320 | buttonClose->hide(); | ||
321 | } | ||
322 | |||
323 | /*! | ||
324 | Sets whether a categories menu is visible, according to \a b. | ||
325 | */ | ||
326 | void FileSelector::setCategoriesVisible( bool b ) | ||
327 | { | ||
328 | if ( b ) | ||
329 | d->mb->show(); | ||
330 | else | ||
331 | d->mb->hide(); | ||
332 | } | ||
333 | |||
334 | /*! | ||
335 | Rereads the list of files. | ||
336 | */ | ||
337 | void FileSelector::reread() | ||
338 | { | ||
339 | view->reread(); | ||
340 | } | ||