summaryrefslogtreecommitdiffstats
path: root/examples/qdir/qdir.cpp
blob: f936a31f0b24f6fc0e689b510e3050dfe9501b65 (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
354
355
356
357
358
359
/****************************************************************************
**
** Copyright (C) 1992-2008 Trolltech ASA.  All rights reserved.
**
** This file is part of an example program for TQt.  This example
** program may be used, distributed and modified without limitation.
**
*****************************************************************************/

#include "../dirview/dirview.h"
#include "qdir.h"

#include <qapplication.h>
#include <qtextview.h>
#include <qfileinfo.h>
#include <qfile.h>
#include <qtextstream.h>
#include <qhbox.h>
#include <qspinbox.h>
#include <qlabel.h>
#include <qmultilineedit.h>
#include <qheader.h>
#include <qevent.h>
#include <qpainter.h>
#include <qpopupmenu.h>
#include <qpushbutton.h>
#include <qtoolbutton.h>
#include <qfile.h>
#include <qtextstream.h>
#include <qtooltip.h>

#include <stdlib.h>

/* XPM */
static const char *bookmarks[]={
    "22 14 8 1",
    "# c #000080",
    "a c #585858",
    "b c #000000",
    "c c #ffffff",
    "d c #ffffff",
    "e c #ffffff",
    "f c #000000",
    ". c None",
    "...bb.................",
    "..bacb....bbb.........",
    "..badcb.bbccbab.......",
    "..bacccbadccbab.......",
    "..baecdbcccdbab.......",
    "..bacccbacccbab.......",
    "..badcdbcecdfab.......",
    "..bacecbacccbab.......",
    "..baccdbcccdbab.......",
    "...badcbacdbbab.......",
    "....bacbcbbccab.......",
    ".....babbaaaaab.......",
    ".....bbabbbbbbb.......",
    "......bb.............."
};

/* XPM */
static const char *home[]={
    "16 15 4 1",
    "# c #000000",
    "a c #ffffff",
    "b c #c0c0c0",
    ". c None",
    ".......##.......",
    "..#...####......",
    "..#..#aabb#.....",
    "..#.#aaaabb#....",
    "..##aaaaaabb#...",
    "..#aaaaaaaabb#..",
    ".#aaaaaaaaabbb#.",
    "###aaaaaaaabb###",
    "..#aaaaaaaabb#..",
    "..#aaa###aabb#..",
    "..#aaa#.#aabb#..",
    "..#aaa#.#aabb#..",
    "..#aaa#.#aabb#..",
    "..#aaa#.#aabb#..",
    "..#####.######.."
};

// ****************************************************************************************************

PixmapView::PixmapView( TQWidget *parent )
    : TQScrollView( parent )
{
    viewport()->setBackgroundMode( PaletteBase );
}

void PixmapView::setPixmap( const TQPixmap &pix )
{
    pixmap = pix;
    resizeContents( pixmap.size().width(), pixmap.size().height() );
    viewport()->repaint( FALSE );
}

void PixmapView::drawContents( TQPainter *p, int cx, int cy, int cw, int ch )
{
    p->fillRect( cx, cy, cw, ch, colorGroup().brush( TQColorGroup::Base ) );
    p->drawPixmap( 0, 0, pixmap );
}

// ****************************************************************************************************

Preview::Preview( TQWidget *parent )
    : TQWidgetStack( parent )
{
    normalText = new TQMultiLineEdit( this );
    normalText->setReadOnly( TRUE );
    html = new TQTextView( this );
    pixmap = new PixmapView( this );
    raiseWidget( normalText );
}

void Preview::showPreview( const TQUrl &u, int size )
{
    if ( u.isLocalFile() ) {
	TQString path = u.path();
	TQFileInfo fi( path );
	if ( fi.isFile() && (int)fi.size() > size * 1000 ) {
	    normalText->setText( tr( "The File\n%1\nis too large, so I don't show it!" ).arg( path ) );
	    raiseWidget( normalText );
	    return;
	}
	
	TQPixmap pix( path );
	if ( pix.isNull() ) {
	    if ( fi.isFile() ) {
		TQFile f( path );
		if ( f.open( IO_ReadOnly ) ) {
		    TQTextStream ts( &f );
		    TQString text = ts.read();
		    f.close();
		    if ( fi.extension().lower().contains( "htm" ) ) {
			TQString url = html->mimeSourceFactory()->makeAbsolute( path, html->context() );
			html->setText( text, url ); 	
			raiseWidget( html );
			return;
		    } else {
			normalText->setText( text ); 	
			raiseWidget( normalText );
			return;
		    }
		}
	    }
	    normalText->setText( TQString::null );
	    raiseWidget( normalText );
	} else {
	    pixmap->setPixmap( pix );
	    raiseWidget( pixmap );
	}
    } else {
	normalText->setText( "I only show local files!" );
	raiseWidget( normalText );
    }
}

// ****************************************************************************************************

PreviewWidget::PreviewWidget( TQWidget *parent )
    : TQVBox( parent ), TQFilePreview()
{
    setSpacing( 5 );
    setMargin( 5 );
    TQHBox *row = new TQHBox( this );
    row->setSpacing( 5 );
    (void)new TQLabel( tr( "Only show files smaller than: " ), row );
    sizeSpinBox = new TQSpinBox( 1, 10000, 1, row );
    sizeSpinBox->setSuffix( " KB" );
    sizeSpinBox->setValue( 64 );
    row->setFixedHeight( 10 + sizeSpinBox->sizeHint().height() );
    preview = new Preview( this );
}

void PreviewWidget::previewUrl( const TQUrl &u )
{
    preview->showPreview( u, sizeSpinBox->value() );
}

// ****************************************************************************************************

CustomFileDialog::CustomFileDialog()
    :  TQFileDialog( 0, 0, TRUE )
{
    setDir( "/" );

    dirView = new DirectoryView( this, 0, TRUE );
    dirView->addColumn( "" );
    dirView->header()->hide();
    ::Directory *root = new ::Directory( dirView, "/" );
    root->setOpen( TRUE );
    dirView->setFixedWidth( 150 );

    addLeftWidget( dirView );

    TQPushButton *p = new TQPushButton( this );
    p->setPixmap( TQPixmap( bookmarks ) );
    TQToolTip::add( p, tr( "Bookmarks" ) );

    bookmarkMenu = new TQPopupMenu( this );
    connect( bookmarkMenu, SIGNAL( activated( int ) ),
	     this, SLOT( bookmarkChosen( int ) ) );
    addId = bookmarkMenu->insertItem( tr( "Add bookmark" ) );
    bookmarkMenu->insertSeparator();

    TQFile f( ".bookmarks" );
    if ( f.open( IO_ReadOnly ) ) {
	TQDataStream ds( &f );
	ds >> bookmarkList;
	f.close();
	
	TQStringList::Iterator it = bookmarkList.begin();
	for ( ; it != bookmarkList.end(); ++it ) {
	    bookmarkMenu->insertItem( *it );
	}
    }
	
    p->setPopup( bookmarkMenu );

    addToolButton( p, TRUE );

    connect( dirView, SIGNAL( folderSelected( const TQString & ) ),
	     this, SLOT( setDir2( const TQString & ) ) );
    connect( this, SIGNAL( dirEntered( const TQString & ) ),
	     dirView, SLOT( setDir( const TQString & ) ) );

    TQToolButton *b = new TQToolButton( this );
    TQToolTip::add( b, tr( "Go Home!" ) );
    b->setPixmap( TQPixmap( home ) );
    connect( b, SIGNAL( clicked() ),
	     this, SLOT( goHome() ) );

    addToolButton( b );

    resize( width() + width() / 3, height() );
}

CustomFileDialog::~CustomFileDialog()
{
    if ( !bookmarkList.isEmpty() ) {
	TQFile f( ".bookmarks" );
	if ( f.open( IO_WriteOnly ) ) {
	    TQDataStream ds( &f );
	    ds << bookmarkList;
	    f.close();
	}
    }
}

void CustomFileDialog::setDir2( const TQString &s )
{
    blockSignals( TRUE );
    setDir( s );
    blockSignals( FALSE );
}

void CustomFileDialog::showEvent( TQShowEvent *e )
{
    TQFileDialog::showEvent( e );
    dirView->setDir( dirPath() );
}

void CustomFileDialog::bookmarkChosen( int i )
{
    if ( i == addId ) {
	bookmarkList << dirPath();
	bookmarkMenu->insertItem( dirPath() );
    } else {
	setDir( bookmarkMenu->text( i ) );
    }
}

void CustomFileDialog::goHome()
{
    if ( getenv( "HOME" ) )
	setDir( getenv( "HOME" ) );
    else
	setDir( "/" );
}

// ****************************************************************************************************

int main( int argc, char ** argv )
{
    TQFileDialog::Mode mode = TQFileDialog::ExistingFile;
    TQString start;
    TQString filter;
    TQString caption;
    bool preview = FALSE;
    bool custom = FALSE;
    TQApplication a( argc, argv );
    for (int i=1; i<argc; i++) {
	TQString arg = argv[i];
	if ( arg == "-any" )
	    mode = TQFileDialog::AnyFile;
	else if ( arg == "-dir" )
	    mode = TQFileDialog::Directory;
	else if ( arg == "-default" )
	    start = argv[++i];
	else if ( arg == "-filter" )
	    filter = argv[++i];
	else if ( arg == "-preview" )
	    preview = TRUE;
	else if ( arg == "-custom" )
	    custom = TRUE;
	else if ( arg[0] == '-' ) {
	    qDebug("Usage: qdir [-any | -dir | -custom] [-preview] [-default f] {-filter f} [caption ...]\n"
		   "      -any         Get any filename, need not exist.\n"
		   "      -dir         Return a directory rather than a file.\n"
		   "      -custom      Opens a customized TQFileDialog with \n"
		   "                   dir browser, bookmark menu, etc.\n"
		   "      -preview     Show a preview widget.\n"
		   "      -default f   Start from directory/file f.\n"
		   "      -filter f    eg. '*.gif' '*.bmp'\n"
		   "      caption ...  Caption for dialog.\n"
		   );
	    return 1;
	} else {
	    if ( !caption.isNull() )
		caption += ' ';
	    caption += arg;
	}
    }

    if ( !start )
	start = TQDir::currentDirPath();

    if ( !caption )
	caption = mode == TQFileDialog::Directory
		    ? "Choose directory..." : "Choose file...";

    if ( !custom ) {
	TQFileDialog fd( TQString::null, filter, 0, 0, TRUE );
	fd.setMode( mode );
	if ( preview ) {
	    fd.setContentsPreviewEnabled( TRUE );
	    PreviewWidget *pw = new PreviewWidget( &fd );
	    fd.setContentsPreview( pw, pw );
	    fd.setViewMode( TQFileDialog::List );
	    fd.setPreviewMode( TQFileDialog::Contents );
	}
	fd.setCaption( caption );
	fd.setSelection( start );
	if ( fd.exec() == TQDialog::Accepted ) {
	    TQString result = fd.selectedFile();
	    printf("%s\n", (const char*)result);
	    return 0;
	} else {
	    return 1;
	}
    } else {
	CustomFileDialog fd;
	fd.exec();
	return 1;
    }
}