1 #include "kdockwidgetdemo.h"
3 #include <Qt3Support/Q3Header>
4 #include <QtGui/QToolButton>
6 #include <Qt3Support/Q3TextView>
7 #include <QtCore/QFileInfo>
8 #include <QtCore/QFile>
9 #include <QtCore/QTextStream>
10 #include <QtGui/QLabel>
11 #include <Qt3Support/Q3MultiLineEdit>
12 #include <QtGui/QKeyEvent>
13 #include <Qt3Support/Q3PopupMenu>
14 #include <QtGui/QPushButton>
15 #include <QtCore/QPoint>
16 #include <QtGui/QMessageBox>
17 #include <QtGui/QMacMime>
18 #include <Qt3Support/Q3StrList>
19 #include <QtGui/QPainter>
21 #include <QtGui/QPixmap>
23 #include <kconfiggroup.h>
24 #include <kaboutdata.h>
25 #include <kcmdlineargs.h>
26 #include <kapplication.h>
30 static const char *dir_tree_xpm
[] = {
58 static const char *preview_xpm
[] = {
84 SFileDialog::SFileDialog( QString initially
, const QStringList
& filter
, const char* name
)
89 KConfigGroup
config( KGlobal::config(), QLatin1String("SFileDialogData:") + name
);
90 if ( initially
.isNull() ){
91 initially
= config
.readPathEntry( "InitiallyDir", QDir::currentPath() );
95 bookmark
= config
.readEntry( "Bookmarks",QStringList() );
97 dockManager
= new K3DockManager(this);
99 d_dirView
= new K3DockWidget( dockManager
, "Dock_DirView", QPixmap(dir_tree_xpm
) );
100 d_dirView
->setWindowTitle("Tree");
102 dirView
= new DirectoryView( d_dirView
, 0 );
103 dirView
->addColumn( "" );
104 dirView
->header()->hide();
105 d_dirView
->setWidget( dirView
);
107 Directory
* root
= new Directory( dirView
, "/" );
110 d_preview
= new K3DockWidget( dockManager
, "Dock_Preview", QPixmap(preview_xpm
) );
111 d_preview
->setWindowTitle("Preview");
112 preview
= new Preview( d_preview
);
113 d_preview
->setWidget( preview
);
115 d_fd
= new K3DockWidget( dockManager
, "Dock_QFileDialog", QPixmap(), this );
116 fd
= new CustomFileDialog( d_fd
);
117 fd
->setDir( initially
);
118 fd
->setFilters( filter
);
119 fd
->setBookmark( bookmark
);
120 fd
->setParent( d_fd
);
121 d_fd
->setWidget( fd
);
123 connect( dirView
, SIGNAL( folderSelected( const QString
& ) ), fd
, SLOT( setDir2( const QString
& ) ) );
124 connect( fd
, SIGNAL( dirEntered( const QString
& ) ), dirView
, SLOT( setDir( const QString
& ) ) );
126 d_fd
->setDockSite( K3DockWidget::DockTop
|K3DockWidget::DockLeft
|K3DockWidget::DockRight
|K3DockWidget::DockBottom
);
127 d_fd
->setEnableDocking(K3DockWidget::DockNone
);
129 d_dirView
->setEnableDocking(K3DockWidget::DockTop
|K3DockWidget::DockLeft
|K3DockWidget::DockRight
|K3DockWidget::DockBottom
|K3DockWidget::DockCenter
);
130 d_preview
->setEnableDocking(K3DockWidget::DockTop
|K3DockWidget::DockLeft
|K3DockWidget::DockRight
|K3DockWidget::DockBottom
|K3DockWidget::DockCenter
);
132 d_dirView
->manualDock( d_fd
, K3DockWidget::DockLeft
, 20 );
133 d_preview
->manualDock( d_fd
, K3DockWidget::DockBottom
, 70 );
135 connect(fd
, SIGNAL(fileHighlighted(const QString
&)), preview
, SLOT(showPreview(const QString
&)));
136 connect(fd
, SIGNAL(signalDone(int)), this, SLOT(done(int)));
138 connect(fd
, SIGNAL(dirEntered(const QString
&)), this, SLOT(changeDir(const QString
&)));
139 connect(dirView
, SIGNAL(folderSelected(const QString
&)), this, SLOT(changeDir(const QString
&)));
141 b_tree
= new QToolButton( fd
);
142 b_tree
->setToolTip( "Show/Hide Tree" );
143 b_tree
->setIcon( QIcon( QPixmap( dir_tree_xpm
) ) );
144 connect( b_tree
, SIGNAL(clicked()), d_dirView
, SLOT(changeHideShowState()) );
145 b_tree
->setCheckable(true);
146 b_tree
->setChecked(true);
147 fd
->addToolButton( b_tree
, true );
149 b_preview
= new QToolButton( fd
);
150 b_preview
->setToolTip( "Show/Hide Preview" );
151 b_preview
->setIcon( QIcon( QPixmap( preview_xpm
) ) );
152 connect( b_preview
, SIGNAL(clicked()), d_preview
, SLOT(changeHideShowState()) );
153 b_preview
->setCheckable(true);
154 b_preview
->setChecked(true);
155 fd
->addToolButton( b_preview
);
157 connect( dockManager
, SIGNAL(change()), this, SLOT(dockChange()));
158 connect( dockManager
, SIGNAL(setDockDefaultPos(K3DockWidget
*)), this, SLOT(setDockDefaultPos(K3DockWidget
*)));
159 setWindowTitle("Open File");
161 qDebug("read config");
162 dockManager
->readConfig( 0L , name
);
165 void SFileDialog::dockChange()
167 b_preview
->setChecked( d_preview
->isVisible() );
168 b_tree
->setChecked( d_dirView
->isVisible() );
171 SFileDialog::~SFileDialog()
173 KConfigGroup
config(KGlobal::config(), QString("SFileDialogData:") + objectName() );
174 config
.writeEntry( "Bookmarks", fd
->getBookmark() );
176 qDebug("write config");
177 dockManager
->writeConfig( 0L , objectName() );
180 void SFileDialog::setDockDefaultPos( K3DockWidget
* d
)
182 if ( d
== d_dirView
){
183 d_dirView
->manualDock( d_fd
, K3DockWidget::DockLeft
, 20 );
186 if ( d
== d_preview
){
187 d_preview
->manualDock( d_fd
, K3DockWidget::DockBottom
, 70 );
191 void SFileDialog::changeDir( const QString
& f
)
194 KConfigGroup
config( KGlobal::config(), QString("SFileDialogData:") + objectName() );
195 config
.writePathEntry( "InitiallyDir", f
);
199 QString
SFileDialog::getOpenFileName( QString initially
,
200 const QStringList
& filter
,
201 const QString caption
, const char* name
)
203 SFileDialog
* fd
= new SFileDialog( initially
, filter
, name
);
204 if ( !caption
.isNull() ) fd
->setWindowTitle( caption
);
205 QString result
= ( fd
->exec() == QDialog::Accepted
) ? fd
->fd
->selectedFile():QString();
211 QStringList
SFileDialog::getOpenFileNames( QString initially
,
212 const QStringList
& filter
,
213 const QString caption
, const char* name
)
215 SFileDialog
* fd
= new SFileDialog( initially
, filter
, name
);
216 if ( !caption
.isNull() ) fd
->setWindowTitle( caption
);
218 fd
->fd
->setMode( Q3FileDialog::ExistingFiles
);
219 fd
->d_preview
->undock();
220 fd
->b_preview
->hide();
223 QStringList result
= fd
->fd
->selectedFiles();
229 void SFileDialog::showEvent( QShowEvent
*e
)
231 QDialog::showEvent( e
);
232 dirView
->setDir( fd
->dirPath() );
235 /******************************************************************************************************/
236 PixmapView::PixmapView( QWidget
*parent
)
237 :Q3ScrollView( parent
)
242 void PixmapView::setPixmap( const QPixmap
&pix
)
245 resizeContents( pixmap
.size().width(), pixmap
.size().height() );
246 viewport()->repaint();
249 void PixmapView::drawContents( QPainter
*p
, int, int, int, int )
251 p
->drawPixmap( 0, 0, pixmap
);
254 Preview::Preview( QWidget
*parent
)
255 :Q3WidgetStack( parent
)
257 normalText
= new Q3MultiLineEdit( this );
258 normalText
->setReadOnly( true );
259 html
= new Q3TextView( this );
260 pixmap
= new PixmapView( this );
261 raiseWidget( normalText
);
264 void Preview::showPreview( const QString
&str
)
267 if ( u
.isLocalFile() ){
268 QString path
= u
.path();
269 QFileInfo
fi( path
);
270 if ( fi
.isFile() && (int)fi
.size() > 400 * 1024 ) {
271 normalText
->setText( tr( "The File\n%1\nis too large, so I don't show it!" ).arg( path
) );
272 raiseWidget( normalText
);
277 if ( pix
.isNull() ) {
280 if ( f
.open( QIODevice::ReadOnly
) ) {
281 QTextStream
ts( &f
);
282 QString text
= ts
.readAll();
284 if ( fi
.suffix().toLower().contains( "htm" ) ) {
285 QString url
= html
->mimeSourceFactory()->makeAbsolute( path
, html
->context() );
286 html
->setText( text
, url
);
290 normalText
->setText( text
);
291 raiseWidget( normalText
);
296 normalText
->setText( QString() );
297 raiseWidget( normalText
);
299 pixmap
->setPixmap( pix
);
300 raiseWidget( pixmap
);
303 normalText
->setText( "I only show local files!" );
304 raiseWidget( normalText
);
308 // ****************************************************************************************************
309 static const char* homepage_xpm
[] = {
320 "$$$$$$$$$$$$$$$$$$$$$$$$",
321 "$$$$$$$$$$$$$$$$$$$$$$$$",
322 "$$$$$$$$$$$$$$$$o.X$$$$$",
323 "$$$$$$$$$$oOOOO$O.X$$$$$",
324 "$$$$$$$$$$oXXXX+O.X$$$$$",
325 "$$$$$$$$$X#XXXXXO.X$$$$$",
326 "$$$$$$$$X #XXXXO.X$$$$$",
327 "$$$$$$$X XO #XXXO.X$$$$$",
328 "$$$$$$X XOOO #XXX.XX$$$$",
329 "$$$$$X XOOOOO #XXXXXX$$$",
330 "$$$$X XOOOOOOO #XXXXXX$$",
331 "$$$X XOOOOOOOOO #XXXXXX$",
332 "$$X XOOOOOOOOOOO #.....$",
333 "$$$$$OOOOXXXXOOOOX...$$$",
334 "$$$$$OOOOXXXXOOOOX...$$$",
335 "$$$$$OOOOXXXXOOOOX..o$$$",
336 "$$$$$OOOOXXXXOOOOX.oo$$$",
337 "$$$$$OOOOXXXXOOOOXXoo$$$",
338 "$$$$$OOOOXXXXOOOOXooo$$$",
339 "$$$ooOOOOXXXXOOOOXoooo$$",
340 "$ooooXXXXXXXXXXXXXooooo$",
341 "$ooooooooooooooooooooooo",
342 "$$$$$$$$$$$$$$$$$$$$$$$$",
343 "$$$$$$$$$$$$$$$$$$$$$$$$"
346 static const char* folder_trash
[] = {
376 static const char* globalbookmark_xpm
[]={
398 CustomFileDialog::CustomFileDialog( QWidget
* parent
)
399 : Q3FileDialog( parent
, 0, false )
401 QToolButton
*p
= new QToolButton( this );
403 p
->setIcon( QIcon( QPixmap( globalbookmark_xpm
) ) );
404 p
->setToolTip( tr("Bookmarks") );
406 bookmarkMenu
= new Q3PopupMenu( this );
407 connect( bookmarkMenu
, SIGNAL( activated( int ) ), this, SLOT( bookmarkChosen( int ) ) );
408 addId
= bookmarkMenu
->insertItem( "Add bookmark" );
409 clearId
= bookmarkMenu
->insertItem( QPixmap(folder_trash
), "Clear bookmarks" );
410 bookmarkMenu
->addSeparator();
412 p
->setMenu( bookmarkMenu
);
414 addToolButton( p
, true );
416 QToolButton
*b
= new QToolButton( this );
417 b
->setToolTip( tr("Go Home!") );
419 b
->setIcon( QIcon( QPixmap( homepage_xpm
) ) );
420 connect( b
, SIGNAL( clicked() ), this, SLOT( goHome() ) );
426 void CustomFileDialog::setBookmark( QStringList
&s
)
428 QStringList::Iterator it
= s
.begin();
429 for ( ; it
!= s
.end(); ++it
){
430 bookmarkList
<< (*it
);
432 const char* book_pix
[]={
453 bookmarkMenu
->insertItem( QIcon( book_pix
), (*it
) );
457 CustomFileDialog::~CustomFileDialog()
461 void CustomFileDialog::setDir2( const QString
&s
)
463 blockSignals( true );
465 blockSignals( false );
468 void CustomFileDialog::bookmarkChosen( int i
)
471 bookmarkList
.clear();
472 bookmarkMenu
->clear();
473 addId
= bookmarkMenu
->insertItem( "Add bookmark" );
474 clearId
= bookmarkMenu
->insertItem( "Clear bookmarks" );
475 bookmarkMenu
->addSeparator();
480 bookmarkList
<< dirPath();
482 const char* book_pix
[]={
503 bookmarkMenu
->insertItem( QIcon( book_pix
), dirPath() );
507 setDir( bookmarkMenu
->text( i
) );
510 void CustomFileDialog::goHome()
512 const QByteArray home
= qgetenv( "HOME" );
513 setDir( home
.isEmpty() ? QLatin1String( "/" ) : QString(home
) );
516 void CustomFileDialog::done( int i
)
520 /******************************************************************************************************/
521 static const char* folder_closed_xpm
[] = {
550 static const char* folder_open_xpm
[] = {
580 static const char* folder_locked_xpm
[] = {
608 Directory::Directory( Directory
* parent
, const QString
& filename
)
609 :Q3ListViewItem( parent
), f(filename
)
612 readable
= QDir( fullName() ).isReadable();
615 setPixmap( 0, QPixmap( folder_locked_xpm
) );
617 setPixmap( 0, QPixmap( folder_closed_xpm
) );
621 Directory::Directory( Q3ListView
* parent
, const QString
& filename
)
622 :Q3ListViewItem( parent
), f(filename
)
625 readable
= QDir( fullName() ).isReadable();
629 void Directory::setOpen( bool o
)
632 setPixmap( 0, QPixmap( folder_open_xpm
) );
634 setPixmap( 0, QPixmap( folder_closed_xpm
) );
636 if ( o
&& !childCount() ){
637 QString
s( fullName() );
639 if ( !thisDir
.isReadable() ) {
641 setExpandable( false );
645 listView()->setUpdatesEnabled( false );
646 QFileInfoList files
= thisDir
.entryInfoList();
647 foreach ( const QFileInfo
&f
, files
){
648 if ( f
.fileName() != "." && f
.fileName() != ".." && f
.isDir() )
649 (void)new Directory( this, f
.fileName() );
651 listView()->setUpdatesEnabled( true );
653 Q3ListViewItem::setOpen( o
);
657 void Directory::setup()
659 setExpandable( true );
660 Q3ListViewItem::setup();
664 QString
Directory::fullName()
669 s
.append( f
.fileName() );
678 QString
Directory::text( int column
) const
686 return "Unreadable Directory";
689 DirectoryView::DirectoryView( QWidget
*parent
, const char *name
)
690 :Q3ListView( parent
, name
)
692 connect( this, SIGNAL( clicked( Q3ListViewItem
* ) ),
693 this, SLOT( slotFolderSelected( Q3ListViewItem
* ) ) );
694 connect( this, SIGNAL( doubleClicked( Q3ListViewItem
* ) ),
695 this, SLOT( slotFolderSelected( Q3ListViewItem
* ) ) );
696 connect( this, SIGNAL( returnPressed( Q3ListViewItem
* ) ),
697 this, SLOT( slotFolderSelected( Q3ListViewItem
* ) ) );
699 setAcceptDrops( true );
700 viewport()->setAcceptDrops( true );
703 void DirectoryView::setOpen( Q3ListViewItem
* i
, bool b
)
705 Q3ListView::setOpen(i
,b
);
707 slotFolderSelected(i
);
710 void DirectoryView::slotFolderSelected( Q3ListViewItem
*i
)
714 Directory
*dir
= (Directory
*)i
;
715 emit
folderSelected( dir
->fullName() );
718 QString
DirectoryView::fullPath(Q3ListViewItem
* item
)
720 QString fullpath
= item
->text(0);
721 while ( (item
=item
->parent()) ) {
722 if ( item
->parent() )
723 fullpath
= item
->text(0) + "/" + fullpath
;
725 fullpath
= item
->text(0) + fullpath
;
730 void DirectoryView::setDir( const QString
&s
)
732 Q3ListViewItemIterator
it( this );
734 for ( ; it
.current(); ++it
) {
735 it
.current()->setOpen( false );
738 QStringList
lst( s
.split( "/", QString::SkipEmptyParts
) );
739 Q3ListViewItem
*item
= firstChild();
740 QStringList::Iterator it2
= lst
.begin();
741 for ( ; it2
!= lst
.end(); ++it2
) {
743 if ( item
->text( 0 ) == *it2
) {
744 item
->setOpen( true );
747 item
= item
->itemBelow();
752 setSelected( item
, true );
753 setCurrentItem( item
);
757 QString
DirectoryView::selectedDir()
759 Directory
*dir
= (Directory
*)currentItem();
760 return dir
->fullName();
762 /**********************************************************************************************/
764 int main(int argc
, char* argv
[]) {
765 KAboutData
about("kdockwidgetdemo", 0, ki18n("kdockwidgetdemo"), "version");
766 KCmdLineArgs::init(argc
, argv
, &about
);
771 SFileDialog
* openfile
= new SFileDialog();
773 qDebug( openfile
->fileName() );
777 qDebug ( SFileDialog::getOpenFileName( QString(), QLatin1String("All (*)"),
778 QLatin1String("DockWidget Demo"), "dialog1" ) );
782 QStringList s
= SFileDialog::getOpenFileNames( QString(),
783 QStringList(QLatin1String("All (*)")),
784 QLatin1String("DockWidget Demo"), "dialog1" );
785 QStringList::Iterator it
= s
.begin();
786 for ( ; it
!= s
.end(); ++it
){
787 qDebug( "%s", (*it
).toLocal8Bit().data() );
793 #include "kdockwidgetdemo.moc"