Sophie

Sophie

distrib > Mandriva > 8.2 > i586 > media > contrib > by-pkgid > 112b0974ad288f6cd55bf971ee6026a9 > files > 1185

libqt3-devel-3.0.2-2mdk.i586.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- /tmp/qt-3.0-reggie-28534/qt-x11-free-3.0.2/examples/qdir/qdir.doc:4 -->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>QDir</title>
<style type="text/css"><!--
h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm; }
a:link { color: #004faf; text-decoration: none }
a:visited { color: #672967; text-decoration: none }
body { background: #ffffff; color: black; }
--></style>
</head>
<body>

<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr bgcolor="#E5E5E5">
<td valign=center>
 <a href="index.html">
<font color="#004faf">Home</font></a>
 | <a href="classes.html">
<font color="#004faf">All&nbsp;Classes</font></a>
 | <a href="mainclasses.html">
<font color="#004faf">Main&nbsp;Classes</font></a>
 | <a href="annotated.html">
<font color="#004faf">Annotated</font></a>
 | <a href="groups.html">
<font color="#004faf">Grouped&nbsp;Classes</font></a>
 | <a href="functions.html">
<font color="#004faf">Functions</font></a>
</td>
<td align="right" valign="center"><img src="logo32.png" align="right" width="64" height="32" border="0"></td></tr></table><h1 align=center>QDir</h1>

   
<p> 
<hr>
<p> Main:
<p> <pre>/****************************************************************************
** $Id:  qt/qdir.cpp   3.0.2   edited Oct 12 12:18 $
**
** Copyright (C) 1992-2000 Trolltech AS.  All rights reserved.
**
** This file is part of an example program for Qt.  This example
** program may be used, distributed and modified without limitation.
**
*****************************************************************************/

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

#include &lt;<a href="qapplication-h.html">qapplication.h</a>&gt;
#include &lt;<a href="qtextview-h.html">qtextview.h</a>&gt;
#include &lt;<a href="qfileinfo-h.html">qfileinfo.h</a>&gt;
#include &lt;<a href="qfile-h.html">qfile.h</a>&gt;
#include &lt;<a href="qtextstream-h.html">qtextstream.h</a>&gt;
#include &lt;<a href="qhbox-h.html">qhbox.h</a>&gt;
#include &lt;<a href="qspinbox-h.html">qspinbox.h</a>&gt;
#include &lt;<a href="qlabel-h.html">qlabel.h</a>&gt;
#include &lt;<a href="qmultilineedit-h.html">qmultilineedit.h</a>&gt;
#include &lt;<a href="qheader-h.html">qheader.h</a>&gt;
#include &lt;<a href="qevent-h.html">qevent.h</a>&gt;
#include &lt;<a href="qpainter-h.html">qpainter.h</a>&gt;
#include &lt;<a href="qpopupmenu-h.html">qpopupmenu.h</a>&gt;
#include &lt;<a href="qpushbutton-h.html">qpushbutton.h</a>&gt;
#include &lt;<a href="qtoolbutton-h.html">qtoolbutton.h</a>&gt;
#include &lt;<a href="qfile-h.html">qfile.h</a>&gt;
#include &lt;<a href="qtextstream-h.html">qtextstream.h</a>&gt;
#include &lt;<a href="qtooltip-h.html">qtooltip.h</a>&gt;

#include &lt;stdlib.h&gt;

/* 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#..",
    "..#####.######.."
};

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

<a name="f278"></a>PixmapView::PixmapView( <a href="qwidget.html">QWidget</a> *parent )
    : <a href="qscrollview.html">QScrollView</a>( parent )
{
    <a href="qscrollview.html#viewport">viewport</a>()-&gt;setBackgroundMode( PaletteBase );
}

void <a name="f279"></a>PixmapView::setPixmap( const <a href="qpixmap.html">QPixmap</a> &amp;pix )
{
    pixmap = pix;
<a name="x812"></a>    <a href="qscrollview.html#resizeContents">resizeContents</a>( pixmap.<a href="qwidget.html#size">size</a>().width(), pixmap.<a href="qwidget.html#size">size</a>().height() );
    <a href="qscrollview.html#viewport">viewport</a>()-&gt;repaint( FALSE );
}

<a name="x795"></a>void PixmapView::<a href="qscrollview.html#drawContents">drawContents</a>( <a href="qpainter.html">QPainter</a> *p, int cx, int cy, int cw, int ch )
{
<a name="x791"></a>    p-&gt;<a href="qpainter.html#fillRect">fillRect</a>( cx, cy, cw, ch, colorGroup().brush( QColorGroup::Base ) );
<a name="x790"></a>    p-&gt;<a href="qpainter.html#drawPixmap">drawPixmap</a>( 0, 0, pixmap );
}

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

<a name="f280"></a>Preview::Preview( <a href="qwidget.html">QWidget</a> *parent )
    : <a href="qwidgetstack.html">QWidgetStack</a>( parent )
{
    normalText = new <a href="qmultilineedit.html">QMultiLineEdit</a>( this );
<a name="x802"></a>    normalText-&gt;<a href="qtextedit.html#setReadOnly">setReadOnly</a>( TRUE );
    html = new <a href="qtextview.html">QTextView</a>( this );
    pixmap = new PixmapView( this );
    <a href="qwidgetstack.html#raiseWidget">raiseWidget</a>( normalText );
}

void <a name="f281"></a>Preview::showPreview( const <a href="qurl.html">QUrl</a> &amp;u, int size )
{
<a name="x806"></a>    if ( u.<a href="qurl.html#isLocalFile">isLocalFile</a>() ) {
<a name="x807"></a>        <a href="qstring.html">QString</a> path = u.<a href="qurl.html#path">path</a>();
        <a href="qfileinfo.html">QFileInfo</a> fi( path );
<a name="x783"></a><a name="x782"></a>        if ( fi.<a href="qfileinfo.html#isFile">isFile</a>() &amp;&amp; (int)fi.<a href="qfileinfo.html#size">size</a>() &gt; size * 1000 ) {
<a name="x803"></a>            normalText-&gt;<a href="qtextedit.html#setText">setText</a>( <a href="qobject.html#tr">tr</a>( "The File\n%1\nis too large, so I don't show it!" ).arg( path ) );
            <a href="qwidgetstack.html#raiseWidget">raiseWidget</a>( normalText );
            return;
        }

        <a href="qpixmap.html">QPixmap</a> pix( path );
<a name="x792"></a>        if ( pix.<a href="qpixmap.html#isNull">isNull</a>() ) {
            if ( fi.<a href="qfileinfo.html#isFile">isFile</a>() ) {
                <a href="qfile.html">QFile</a> f( path );
<a name="x773"></a>                if ( f.<a href="qfile.html#open">open</a>( <a href="qfile.html#open">IO_ReadOnly</a> ) ) {
                    <a href="qtextstream.html">QTextStream</a> ts( &amp;f );
<a name="x804"></a>                    <a href="qstring.html">QString</a> text = ts.<a href="qtextstream.html#read">read</a>();
<a name="x772"></a>                    f.<a href="qfile.html#close">close</a>();
<a name="x781"></a>                    if ( fi.<a href="qfileinfo.html#extension">extension</a>().lower().contains( "htm" ) ) {
<a name="x801"></a><a name="x800"></a>                        <a href="qstring.html">QString</a> url = html-&gt;<a href="qtextedit.html#mimeSourceFactory">mimeSourceFactory</a>()-&gt;makeAbsolute( path, html-&gt;<a href="qtextedit.html#context">context</a>() );
                        html-&gt;<a href="qtextedit.html#setText">setText</a>( text, url );
                        raiseWidget( html );
                        return;
                    } else {
                        normalText-&gt;<a href="qtextedit.html#setText">setText</a>( text );
                        raiseWidget( normalText );
                        return;
                    }
                }
            }
            normalText-&gt;<a href="qtextedit.html#setText">setText</a>( <a href="qstring.html#QString-null">QString::null</a> );
            raiseWidget( normalText );
        } else {
            pixmap-&gt;setPixmap( pix );
            raiseWidget( pixmap );
        }
    } else {
        normalText-&gt;<a href="qtextedit.html#setText">setText</a>( "I only show local files!" );
        raiseWidget( normalText );
    }
}

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

<a name="f282"></a>PreviewWidget::PreviewWidget( <a href="qwidget.html">QWidget</a> *parent )
    : <a href="qvbox.html">QVBox</a>( parent ), QFilePreview()
{
    setSpacing( 5 );
    setMargin( 5 );
    <a href="qhbox.html">QHBox</a> *row = new <a href="qhbox.html">QHBox</a>( this );
<a name="x784"></a>    row-&gt;<a href="qhbox.html#setSpacing">setSpacing</a>( 5 );
    (void)new <a href="qlabel.html">QLabel</a>( <a href="qobject.html#tr">tr</a>( "Only show files smaller than: " ), row );
    sizeSpinBox = new <a href="qspinbox.html">QSpinBox</a>( 1, 10000, 1, row );
<a name="x796"></a>    sizeSpinBox-&gt;<a href="qspinbox.html#setSuffix">setSuffix</a>( " KB" );
<a name="x797"></a>    sizeSpinBox-&gt;<a href="qspinbox.html#setValue">setValue</a>( 64 );
<a name="x809"></a>    row-&gt;<a href="qwidget.html#setFixedHeight">setFixedHeight</a>( 10 + sizeSpinBox-&gt;<a href="qwidget.html#sizeHint">sizeHint</a>().height() );
    preview = new Preview( this );
}

void <a name="f283"></a>PreviewWidget::previewUrl( const <a href="qurl.html">QUrl</a> &amp;u )
{
<a name="x798"></a>    preview-&gt;showPreview( u, sizeSpinBox-&gt;<a href="qspinbox.html#value">value</a>() );
}

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

<a name="f274"></a>CustomFileDialog::CustomFileDialog()
    :  <a href="qfiledialog.html">QFileDialog</a>( 0, 0, TRUE )
{
    <a href="qfiledialog.html#setDir">setDir</a>( "/" );

    dirView = new DirectoryView( this, 0, TRUE );
<a name="x785"></a>    dirView-&gt;<a href="qlistview.html#addColumn">addColumn</a>( "" );
<a name="x786"></a>    dirView-&gt;<a href="qlistview.html#header">header</a>()-&gt;hide();
    ::Directory *root = new ::Directory( dirView, "/" );
    root-&gt;setOpen( TRUE );
<a name="x810"></a>    dirView-&gt;<a href="qwidget.html#setFixedWidth">setFixedWidth</a>( 150 );

    <a href="qfiledialog.html#addLeftWidget">addLeftWidget</a>( dirView );

    <a href="qpushbutton.html">QPushButton</a> *p = new <a href="qpushbutton.html">QPushButton</a>( this );
<a name="x769"></a>    p-&gt;<a href="qbutton.html#setPixmap">setPixmap</a>( QPixmap( bookmarks ) );
<a name="x805"></a>    QToolTip::<a href="qtooltip.html#add">add</a>( p, tr( "Bookmarks" ) );

    bookmarkMenu = new <a href="qpopupmenu.html">QPopupMenu</a>( this );
<a name="x793"></a>    <a href="qobject.html#connect">connect</a>( bookmarkMenu, SIGNAL( <a href="qpopupmenu.html#activated">activated</a>( int ) ),
             this, SLOT( bookmarkChosen( int ) ) );
    addId = bookmarkMenu-&gt;<a href="qmenudata.html#insertItem">insertItem</a>( <a href="qobject.html#tr">tr</a>( "Add bookmark" ) );
    bookmarkMenu-&gt;<a href="qmenudata.html#insertSeparator">insertSeparator</a>();

    <a href="qfile.html">QFile</a> f( ".bookmarks" );
    if ( f.<a href="qfile.html#open">open</a>( IO_ReadOnly ) ) {
        <a href="qdatastream.html">QDataStream</a> ds( &amp;f );
        ds &gt;&gt; bookmarkList;
        f.<a href="qfile.html#close">close</a>();

        QStringList::Iterator it = bookmarkList.begin();
        for ( ; it != bookmarkList.end(); ++it ) {
            bookmarkMenu-&gt;<a href="qmenudata.html#insertItem">insertItem</a>( *it );
        }
    }

<a name="x794"></a>    p-&gt;<a href="qpushbutton.html#setPopup">setPopup</a>( bookmarkMenu );

    <a href="qfiledialog.html#addToolButton">addToolButton</a>( p, TRUE );

    <a href="qobject.html#connect">connect</a>( dirView, SIGNAL( folderSelected( const <a href="qstring.html">QString</a> &amp; ) ),
             this, SLOT( setDir2( const <a href="qstring.html">QString</a> &amp; ) ) );
    <a href="qobject.html#connect">connect</a>( this, SIGNAL( <a href="qfiledialog.html#dirEntered">dirEntered</a>( const <a href="qstring.html">QString</a> &amp; ) ),
             dirView, SLOT( <a href="qfiledialog.html#setDir">setDir</a>( const <a href="qstring.html">QString</a> &amp; ) ) );

    <a href="qtoolbutton.html">QToolButton</a> *b = new <a href="qtoolbutton.html">QToolButton</a>( this );
    QToolTip::<a href="qtooltip.html#add">add</a>( b, tr( "Go Home!" ) );
    b-&gt;<a href="qbutton.html#setPixmap">setPixmap</a>( QPixmap( home ) );
    <a href="qobject.html#connect">connect</a>( b, SIGNAL( <a href="qbutton.html#clicked">clicked</a>() ),
             this, SLOT( goHome() ) );

    <a href="qfiledialog.html#addToolButton">addToolButton</a>( b );

    <a href="qwidget.html#resize">resize</a>( <a href="qwidget.html#width">width</a>() + width() / 3, height() );
}

CustomFileDialog::~CustomFileDialog()
{
    if ( !bookmarkList.isEmpty() ) {
        <a href="qfile.html">QFile</a> f( ".bookmarks" );
        if ( f.<a href="qfile.html#open">open</a>( <a href="qfile.html#open">IO_WriteOnly</a> ) ) {
            <a href="qdatastream.html">QDataStream</a> ds( &amp;f );
            ds &lt;&lt; bookmarkList;
            f.<a href="qfile.html#close">close</a>();
        }
    }
}

void <a name="f275"></a>CustomFileDialog::setDir2( const <a href="qstring.html">QString</a> &amp;s )
{
    <a href="qobject.html#blockSignals">blockSignals</a>( TRUE );
    <a href="qfiledialog.html#setDir">setDir</a>( s );
    <a href="qobject.html#blockSignals">blockSignals</a>( FALSE );
}

<a name="x811"></a>void CustomFileDialog::<a href="qwidget.html#showEvent">showEvent</a>( <a href="qshowevent.html">QShowEvent</a> *e )
{
    QFileDialog::<a href="qwidget.html#showEvent">showEvent</a>( e );
    dirView-&gt;setDir( <a href="qfiledialog.html#dirPath">dirPath</a>() );
}

void <a name="f276"></a>CustomFileDialog::bookmarkChosen( int i )
{
    if ( i == addId ) {
        bookmarkList &lt;&lt; dirPath();
        bookmarkMenu-&gt;<a href="qmenudata.html#insertItem">insertItem</a>( <a href="qfiledialog.html#dirPath">dirPath</a>() );
    } else {
<a name="x789"></a>        <a href="qfiledialog.html#setDir">setDir</a>( bookmarkMenu-&gt;<a href="qmenudata.html#text">text</a>( i ) );
    }
}

void <a name="f277"></a>CustomFileDialog::goHome()
{
    if ( getenv( "HOME" ) )
        <a href="qfiledialog.html#setDir">setDir</a>( getenv( "HOME" ) );
    else
        <a href="qfiledialog.html#setDir">setDir</a>( "/" );
}

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

int main( int argc, char ** argv )
{
    QFileDialog::Mode mode = QFileDialog::ExistingFile;
    <a href="qstring.html">QString</a> start;
    <a href="qstring.html">QString</a> filter;
    <a href="qstring.html">QString</a> caption;
    bool preview = FALSE;
    bool custom = FALSE;
    <a href="qapplication.html">QApplication</a> a( argc, argv );
    for (int i=1; i&lt;argc; i++) {
        <a href="qstring.html">QString</a> arg = argv[i];
        if ( arg == "-any" )
            mode = QFileDialog::AnyFile;
        else if ( arg == "-dir" )
            mode = QFileDialog::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] == '-' ) {
            <a href="qapplication.html#qDebug">qDebug</a>("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 QFileDialog 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 {
<a name="x799"></a>            if ( !caption.<a href="qstring.html#isNull">isNull</a>() )
                caption += ' ';
            caption += arg;
        }
    }

    if ( !start )
<a name="x771"></a>        start = QDir::<a href="qdir.html#currentDirPath">currentDirPath</a>();

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

    if ( !custom ) {
        <a href="qfiledialog.html">QFileDialog</a> fd( QString::null, filter, 0, 0, TRUE );
<a name="x777"></a>        fd.<a href="qfiledialog.html#setMode">setMode</a>( mode );
        if ( preview ) {
<a name="x776"></a>            fd.<a href="qfiledialog.html#setContentsPreviewEnabled">setContentsPreviewEnabled</a>( TRUE );
            PreviewWidget *pw = new PreviewWidget( &amp;fd );
<a name="x775"></a>            fd.<a href="qfiledialog.html#setContentsPreview">setContentsPreview</a>( pw, pw );
<a name="x780"></a>            fd.<a href="qfiledialog.html#setViewMode">setViewMode</a>( QFileDialog::List );
<a name="x778"></a>            fd.<a href="qfiledialog.html#setPreviewMode">setPreviewMode</a>( QFileDialog::Contents );
        }
        fd.<a href="qwidget.html#setCaption">setCaption</a>( caption );
<a name="x779"></a>        fd.<a href="qfiledialog.html#setSelection">setSelection</a>( start );
<a name="x770"></a>        if ( fd.<a href="qdialog.html#exec">exec</a>() == QDialog::Accepted ) {
<a name="x774"></a>            <a href="qstring.html">QString</a> result = fd.<a href="qfiledialog.html#selectedFile">selectedFile</a>();
            printf("%s\n", (const char*)result);
            return 0;
        } else {
            return 1;
        }
    } else {
        CustomFileDialog fd;
        fd.<a href="qdialog.html#exec">exec</a>();
        return 1;
    }
}
</pre>

<p>See also <a href="examples.html">Examples</a>.

<!-- eof -->
<p><address><hr><div align=center>
<table width=100% cellspacing=0 border=0><tr>
<td>Copyright &copy; 2001 
<a href="http://www.trolltech.com">Trolltech</a><td><a href="http://www.trolltech.com/trademarks.html">Trademarks</a>
<td align=right><div align=right>Qt version 3.0.2</div>
</table></div></address></body>
</html>