1 | /****************************************************************************
|
---|
2 | ** $Id: qdir.cpp 160 2006-12-11 20:15:57Z dmik $
|
---|
3 | **
|
---|
4 | ** Copyright (C) 1992-2000 Trolltech AS. All rights reserved.
|
---|
5 | **
|
---|
6 | ** This file is part of an example program for Qt. This example
|
---|
7 | ** program may be used, distributed and modified without limitation.
|
---|
8 | **
|
---|
9 | *****************************************************************************/
|
---|
10 |
|
---|
11 | #include "../dirview/dirview.h"
|
---|
12 | #include "qdir.h"
|
---|
13 |
|
---|
14 | #include <qapplication.h>
|
---|
15 | #include <qtextview.h>
|
---|
16 | #include <qfileinfo.h>
|
---|
17 | #include <qfile.h>
|
---|
18 | #include <qtextstream.h>
|
---|
19 | #include <qhbox.h>
|
---|
20 | #include <qspinbox.h>
|
---|
21 | #include <qlabel.h>
|
---|
22 | #include <qmultilineedit.h>
|
---|
23 | #include <qheader.h>
|
---|
24 | #include <qevent.h>
|
---|
25 | #include <qpainter.h>
|
---|
26 | #include <qpopupmenu.h>
|
---|
27 | #include <qpushbutton.h>
|
---|
28 | #include <qtoolbutton.h>
|
---|
29 | #include <qfile.h>
|
---|
30 | #include <qtextstream.h>
|
---|
31 | #include <qtooltip.h>
|
---|
32 |
|
---|
33 | #include <stdlib.h>
|
---|
34 |
|
---|
35 | /* XPM */
|
---|
36 | static const char *bookmarks[]={
|
---|
37 | "22 14 8 1",
|
---|
38 | "# c #000080",
|
---|
39 | "a c #585858",
|
---|
40 | "b c #000000",
|
---|
41 | "c c #ffffff",
|
---|
42 | "d c #ffffff",
|
---|
43 | "e c #ffffff",
|
---|
44 | "f c #000000",
|
---|
45 | ". c None",
|
---|
46 | "...bb.................",
|
---|
47 | "..bacb....bbb.........",
|
---|
48 | "..badcb.bbccbab.......",
|
---|
49 | "..bacccbadccbab.......",
|
---|
50 | "..baecdbcccdbab.......",
|
---|
51 | "..bacccbacccbab.......",
|
---|
52 | "..badcdbcecdfab.......",
|
---|
53 | "..bacecbacccbab.......",
|
---|
54 | "..baccdbcccdbab.......",
|
---|
55 | "...badcbacdbbab.......",
|
---|
56 | "....bacbcbbccab.......",
|
---|
57 | ".....babbaaaaab.......",
|
---|
58 | ".....bbabbbbbbb.......",
|
---|
59 | "......bb.............."
|
---|
60 | };
|
---|
61 |
|
---|
62 | /* XPM */
|
---|
63 | static const char *home[]={
|
---|
64 | "16 15 4 1",
|
---|
65 | "# c #000000",
|
---|
66 | "a c #ffffff",
|
---|
67 | "b c #c0c0c0",
|
---|
68 | ". c None",
|
---|
69 | ".......##.......",
|
---|
70 | "..#...####......",
|
---|
71 | "..#..#aabb#.....",
|
---|
72 | "..#.#aaaabb#....",
|
---|
73 | "..##aaaaaabb#...",
|
---|
74 | "..#aaaaaaaabb#..",
|
---|
75 | ".#aaaaaaaaabbb#.",
|
---|
76 | "###aaaaaaaabb###",
|
---|
77 | "..#aaaaaaaabb#..",
|
---|
78 | "..#aaa###aabb#..",
|
---|
79 | "..#aaa#.#aabb#..",
|
---|
80 | "..#aaa#.#aabb#..",
|
---|
81 | "..#aaa#.#aabb#..",
|
---|
82 | "..#aaa#.#aabb#..",
|
---|
83 | "..#####.######.."
|
---|
84 | };
|
---|
85 |
|
---|
86 | // ****************************************************************************************************
|
---|
87 |
|
---|
88 | PixmapView::PixmapView( QWidget *parent )
|
---|
89 | : QScrollView( parent )
|
---|
90 | {
|
---|
91 | viewport()->setBackgroundMode( PaletteBase );
|
---|
92 | }
|
---|
93 |
|
---|
94 | void PixmapView::setPixmap( const QPixmap &pix )
|
---|
95 | {
|
---|
96 | pixmap = pix;
|
---|
97 | resizeContents( pixmap.size().width(), pixmap.size().height() );
|
---|
98 | viewport()->repaint( FALSE );
|
---|
99 | }
|
---|
100 |
|
---|
101 | void PixmapView::drawContents( QPainter *p, int cx, int cy, int cw, int ch )
|
---|
102 | {
|
---|
103 | p->fillRect( cx, cy, cw, ch, colorGroup().brush( QColorGroup::Base ) );
|
---|
104 | p->drawPixmap( 0, 0, pixmap );
|
---|
105 | }
|
---|
106 |
|
---|
107 | // ****************************************************************************************************
|
---|
108 |
|
---|
109 | Preview::Preview( QWidget *parent )
|
---|
110 | : QWidgetStack( parent )
|
---|
111 | {
|
---|
112 | normalText = new QMultiLineEdit( this );
|
---|
113 | normalText->setReadOnly( TRUE );
|
---|
114 | html = new QTextView( this );
|
---|
115 | pixmap = new PixmapView( this );
|
---|
116 | raiseWidget( normalText );
|
---|
117 | }
|
---|
118 |
|
---|
119 | void Preview::showPreview( const QUrl &u, int size )
|
---|
120 | {
|
---|
121 | if ( u.isLocalFile() ) {
|
---|
122 | QString path = u.path();
|
---|
123 | QFileInfo fi( path );
|
---|
124 | if ( fi.isFile() && (int)fi.size() > size * 1000 ) {
|
---|
125 | normalText->setText( tr( "The File\n%1\nis too large, so I don't show it!" ).arg( path ) );
|
---|
126 | raiseWidget( normalText );
|
---|
127 | return;
|
---|
128 | }
|
---|
129 |
|
---|
130 | QPixmap pix( path );
|
---|
131 | if ( pix.isNull() ) {
|
---|
132 | if ( fi.isFile() ) {
|
---|
133 | QFile f( path );
|
---|
134 | if ( f.open( IO_ReadOnly ) ) {
|
---|
135 | QTextStream ts( &f );
|
---|
136 | QString text = ts.read();
|
---|
137 | f.close();
|
---|
138 | if ( fi.extension().lower().contains( "htm" ) ) {
|
---|
139 | QString url = html->mimeSourceFactory()->makeAbsolute( path, html->context() );
|
---|
140 | html->setText( text, url );
|
---|
141 | raiseWidget( html );
|
---|
142 | return;
|
---|
143 | } else {
|
---|
144 | normalText->setText( text );
|
---|
145 | raiseWidget( normalText );
|
---|
146 | return;
|
---|
147 | }
|
---|
148 | }
|
---|
149 | }
|
---|
150 | normalText->setText( QString::null );
|
---|
151 | raiseWidget( normalText );
|
---|
152 | } else {
|
---|
153 | pixmap->setPixmap( pix );
|
---|
154 | raiseWidget( pixmap );
|
---|
155 | }
|
---|
156 | } else {
|
---|
157 | normalText->setText( "I only show local files!" );
|
---|
158 | raiseWidget( normalText );
|
---|
159 | }
|
---|
160 | }
|
---|
161 |
|
---|
162 | // ****************************************************************************************************
|
---|
163 |
|
---|
164 | PreviewWidget::PreviewWidget( QWidget *parent )
|
---|
165 | : QVBox( parent ), QFilePreview()
|
---|
166 | {
|
---|
167 | setSpacing( 5 );
|
---|
168 | setMargin( 5 );
|
---|
169 | QHBox *row = new QHBox( this );
|
---|
170 | row->setSpacing( 5 );
|
---|
171 | (void)new QLabel( tr( "Only show files smaller than: " ), row );
|
---|
172 | sizeSpinBox = new QSpinBox( 1, 10000, 1, row );
|
---|
173 | sizeSpinBox->setSuffix( " KB" );
|
---|
174 | sizeSpinBox->setValue( 64 );
|
---|
175 | row->setFixedHeight( 10 + sizeSpinBox->sizeHint().height() );
|
---|
176 | preview = new Preview( this );
|
---|
177 | }
|
---|
178 |
|
---|
179 | void PreviewWidget::previewUrl( const QUrl &u )
|
---|
180 | {
|
---|
181 | preview->showPreview( u, sizeSpinBox->value() );
|
---|
182 | }
|
---|
183 |
|
---|
184 | // ****************************************************************************************************
|
---|
185 |
|
---|
186 | CustomFileDialog::CustomFileDialog()
|
---|
187 | : QFileDialog( 0, 0, TRUE )
|
---|
188 | {
|
---|
189 | setDir( "/" );
|
---|
190 |
|
---|
191 | dirView = new DirectoryView( this, 0, TRUE );
|
---|
192 | dirView->addColumn( "" );
|
---|
193 | dirView->header()->hide();
|
---|
194 | ::Directory *root = new ::Directory( dirView, "/" );
|
---|
195 | root->setOpen( TRUE );
|
---|
196 | dirView->setFixedWidth( 150 );
|
---|
197 |
|
---|
198 | addLeftWidget( dirView );
|
---|
199 |
|
---|
200 | QPushButton *p = new QPushButton( this );
|
---|
201 | p->setPixmap( QPixmap( bookmarks ) );
|
---|
202 | QToolTip::add( p, tr( "Bookmarks" ) );
|
---|
203 |
|
---|
204 | bookmarkMenu = new QPopupMenu( this );
|
---|
205 | connect( bookmarkMenu, SIGNAL( activated( int ) ),
|
---|
206 | this, SLOT( bookmarkChosen( int ) ) );
|
---|
207 | addId = bookmarkMenu->insertItem( tr( "Add bookmark" ) );
|
---|
208 | bookmarkMenu->insertSeparator();
|
---|
209 |
|
---|
210 | QFile f( ".bookmarks" );
|
---|
211 | if ( f.open( IO_ReadOnly ) ) {
|
---|
212 | QDataStream ds( &f );
|
---|
213 | ds >> bookmarkList;
|
---|
214 | f.close();
|
---|
215 |
|
---|
216 | QStringList::Iterator it = bookmarkList.begin();
|
---|
217 | for ( ; it != bookmarkList.end(); ++it ) {
|
---|
218 | bookmarkMenu->insertItem( *it );
|
---|
219 | }
|
---|
220 | }
|
---|
221 |
|
---|
222 | p->setPopup( bookmarkMenu );
|
---|
223 |
|
---|
224 | addToolButton( p, TRUE );
|
---|
225 |
|
---|
226 | connect( dirView, SIGNAL( folderSelected( const QString & ) ),
|
---|
227 | this, SLOT( setDir2( const QString & ) ) );
|
---|
228 | connect( this, SIGNAL( dirEntered( const QString & ) ),
|
---|
229 | dirView, SLOT( setDir( const QString & ) ) );
|
---|
230 |
|
---|
231 | QToolButton *b = new QToolButton( this );
|
---|
232 | QToolTip::add( b, tr( "Go Home!" ) );
|
---|
233 | b->setPixmap( QPixmap( home ) );
|
---|
234 | connect( b, SIGNAL( clicked() ),
|
---|
235 | this, SLOT( goHome() ) );
|
---|
236 |
|
---|
237 | addToolButton( b );
|
---|
238 |
|
---|
239 | resize( width() + width() / 3, height() );
|
---|
240 | }
|
---|
241 |
|
---|
242 | CustomFileDialog::~CustomFileDialog()
|
---|
243 | {
|
---|
244 | if ( !bookmarkList.isEmpty() ) {
|
---|
245 | QFile f( ".bookmarks" );
|
---|
246 | if ( f.open( IO_WriteOnly ) ) {
|
---|
247 | QDataStream ds( &f );
|
---|
248 | ds << bookmarkList;
|
---|
249 | f.close();
|
---|
250 | }
|
---|
251 | }
|
---|
252 | }
|
---|
253 |
|
---|
254 | void CustomFileDialog::setDir2( const QString &s )
|
---|
255 | {
|
---|
256 | blockSignals( TRUE );
|
---|
257 | setDir( s );
|
---|
258 | blockSignals( FALSE );
|
---|
259 | }
|
---|
260 |
|
---|
261 | void CustomFileDialog::showEvent( QShowEvent *e )
|
---|
262 | {
|
---|
263 | QFileDialog::showEvent( e );
|
---|
264 | dirView->setDir( dirPath() );
|
---|
265 | }
|
---|
266 |
|
---|
267 | void CustomFileDialog::bookmarkChosen( int i )
|
---|
268 | {
|
---|
269 | if ( i == addId ) {
|
---|
270 | bookmarkList << dirPath();
|
---|
271 | bookmarkMenu->insertItem( dirPath() );
|
---|
272 | } else {
|
---|
273 | setDir( bookmarkMenu->text( i ) );
|
---|
274 | }
|
---|
275 | }
|
---|
276 |
|
---|
277 | void CustomFileDialog::goHome()
|
---|
278 | {
|
---|
279 | if ( getenv( "HOME" ) )
|
---|
280 | setDir( getenv( "HOME" ) );
|
---|
281 | else
|
---|
282 | setDir( "/" );
|
---|
283 | }
|
---|
284 |
|
---|
285 | // ****************************************************************************************************
|
---|
286 |
|
---|
287 | int main( int argc, char ** argv )
|
---|
288 | {
|
---|
289 | QFileDialog::Mode mode = QFileDialog::ExistingFile;
|
---|
290 | QString start;
|
---|
291 | QString filter;
|
---|
292 | QString caption;
|
---|
293 | bool preview = FALSE;
|
---|
294 | bool custom = FALSE;
|
---|
295 | QApplication a( argc, argv );
|
---|
296 | for (int i=1; i<argc; i++) {
|
---|
297 | QString arg = argv[i];
|
---|
298 | if ( arg == "-any" )
|
---|
299 | mode = QFileDialog::AnyFile;
|
---|
300 | else if ( arg == "-dir" )
|
---|
301 | mode = QFileDialog::Directory;
|
---|
302 | else if ( arg == "-default" )
|
---|
303 | start = argv[++i];
|
---|
304 | else if ( arg == "-filter" )
|
---|
305 | filter = argv[++i];
|
---|
306 | else if ( arg == "-preview" )
|
---|
307 | preview = TRUE;
|
---|
308 | else if ( arg == "-custom" )
|
---|
309 | custom = TRUE;
|
---|
310 | else if ( arg[0] == '-' ) {
|
---|
311 | qDebug("Usage: qdir [-any | -dir | -custom] [-preview] [-default f] {-filter f} [caption ...]\n"
|
---|
312 | " -any Get any filename, need not exist.\n"
|
---|
313 | " -dir Return a directory rather than a file.\n"
|
---|
314 | " -custom Opens a customized QFileDialog with \n"
|
---|
315 | " dir browser, bookmark menu, etc.\n"
|
---|
316 | " -preview Show a preview widget.\n"
|
---|
317 | " -default f Start from directory/file f.\n"
|
---|
318 | " -filter f eg. '*.gif' '*.bmp'\n"
|
---|
319 | " caption ... Caption for dialog.\n"
|
---|
320 | );
|
---|
321 | return 1;
|
---|
322 | } else {
|
---|
323 | if ( !caption.isNull() )
|
---|
324 | caption += ' ';
|
---|
325 | caption += arg;
|
---|
326 | }
|
---|
327 | }
|
---|
328 |
|
---|
329 | if ( !start )
|
---|
330 | start = QDir::currentDirPath();
|
---|
331 |
|
---|
332 | if ( !caption )
|
---|
333 | caption = mode == QFileDialog::Directory
|
---|
334 | ? "Choose directory..." : "Choose file...";
|
---|
335 |
|
---|
336 | if ( !custom ) {
|
---|
337 | QFileDialog fd( QString::null, filter, 0, 0, TRUE );
|
---|
338 | fd.setMode( mode );
|
---|
339 | if ( preview ) {
|
---|
340 | fd.setContentsPreviewEnabled( TRUE );
|
---|
341 | PreviewWidget *pw = new PreviewWidget( &fd );
|
---|
342 | fd.setContentsPreview( pw, pw );
|
---|
343 | fd.setViewMode( QFileDialog::List );
|
---|
344 | fd.setPreviewMode( QFileDialog::Contents );
|
---|
345 | }
|
---|
346 | fd.setCaption( caption );
|
---|
347 | fd.setSelection( start );
|
---|
348 | if ( fd.exec() == QDialog::Accepted ) {
|
---|
349 | QString result = fd.selectedFile();
|
---|
350 | printf("%s\n", (const char*)result);
|
---|
351 | return 0;
|
---|
352 | } else {
|
---|
353 | return 1;
|
---|
354 | }
|
---|
355 | } else {
|
---|
356 | CustomFileDialog fd;
|
---|
357 | fd.exec();
|
---|
358 | return 1;
|
---|
359 | }
|
---|
360 | }
|
---|