summaryrefslogtreecommitdiffstats
path: root/kio/kfile/tests/kfiletreeviewtest.cpp
blob: 6a4ea719fb2b5e82c975170b77a2cda1858fba41 (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
/* This file is part of the KDE libraries
    Copyright (C) 2001 Klaas Freitag <[email protected]>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.
*/

#include <qdir.h>

#include <kglobal.h>
#include <kiconloader.h>
#include <kmainwindow.h>
#include <kapplication.h>
#include <kurl.h>
#include <kdebug.h>
#include <kstatusbar.h>

#include <kfiletreeview.h>
#include "kfiletreeviewtest.h"


#include "kfiletreeviewtest.moc"

testFrame::testFrame():KMainWindow(0,"Test FileTreeView"),
		       dirOnlyMode(false)

{
   treeView = new KFileTreeView( this );
   treeView->setDragEnabled( true );
   treeView->setAcceptDrops( true );
   treeView->setDropVisualizer( true );


   /* Connect to see the status bar */
   KStatusBar* sta = statusBar();
   connect( treeView, SIGNAL( onItem( const QString& )),
	    sta, SLOT( message( const QString& )));

   connect( treeView, SIGNAL( dropped( QWidget*, QDropEvent*, KURL::List& )),
	    this, SLOT( urlsDropped( QWidget*, QDropEvent*, KURL::List& )));

   connect( treeView, SIGNAL( dropped( KURL::List&, KURL& )), this,
	    SLOT( copyURLs( KURL::List&, KURL& )));

   treeView->addColumn( "File" );
   treeView->addColumn( "ChildCount" );
   setCentralWidget( treeView );
   resize( 600, 400 );

   showPath( KURL::fromPathOrURL( QDir::homeDirPath() ));
}

void testFrame::showPath( const KURL &url )
{
   QString fname = "TestBranch"; // url.fileName ();
   /* try a user icon */
   KIconLoader *loader = KGlobal::iconLoader();
   QPixmap pix = loader->loadIcon( "contents2", KIcon::Small );
   QPixmap pixOpen = loader->loadIcon( "contents", KIcon::Small );

   KFileTreeBranch *nb = treeView->addBranch( url, fname, pix );

   if( nb )
   {
      if( dirOnlyMode ) treeView->setDirOnlyMode( nb, true );
      nb->setOpenPixmap( pixOpen );

      connect( nb, SIGNAL(populateFinished(KFileTreeViewItem*)),
	       this, SLOT(slotPopulateFinished(KFileTreeViewItem*)));
      connect( nb, SIGNAL( directoryChildCount( KFileTreeViewItem *, int )),
	       this, SLOT( slotSetChildCount( KFileTreeViewItem*, int )));
      // nb->setChildRecurse(false );

      nb->setOpen(true);
   }


}

void testFrame::urlsDropped( QWidget* , QDropEvent* , KURL::List& list )
{
   KURL::List::ConstIterator it = list.begin();
   for ( ; it != list.end(); ++it ) {
      kdDebug() << "Url dropped: " << (*it).prettyURL() << endl;
   }
}

void testFrame::copyURLs( KURL::List& list, KURL& to )
{
   KURL::List::ConstIterator it = list.begin();
   kdDebug() << "Copy to " << to.prettyURL() << endl;
   for ( ; it != list.end(); ++it ) {
      kdDebug() << "Url: " << (*it).prettyURL() << endl;
   }

}


void testFrame::slotPopulateFinished(KFileTreeViewItem *item )
{
   if( item )
   {
#if 0
      int cc = item->childCount();

      kdDebug() << "setting column 2 of treeview with count " << cc << endl;

      item->setText( 1, QString::number( cc ));
#endif
   }
   else
   {
      kdDebug() << "slotPopFinished for uninitalised item" << endl;
   }
}

void testFrame::slotSetChildCount( KFileTreeViewItem *item, int c )
{
   if( item )
      item->setText(1, QString::number( c ));
}

int main(int argc, char **argv)
{
    KApplication a(argc, argv, "kfiletreeviewtest");
    QString name1;
    QStringList names;

    QString argv1;
    testFrame *tf;

    tf =  new testFrame();
    a.setMainWidget( tf );

    if (argc > 1)
    {
       for( int i = 1; i < argc; i++ )
       {
	  argv1 = QString::fromLatin1(argv[i]);
	  kdDebug() << "Opening " << argv1 << endl;
	  if( argv1 == "-d" )
	     tf->setDirOnly();
	  else
	  {
	  KURL u( argv1 );
	  tf->showPath( u );
       }
    }
    }
    tf->show();
    int ret = a.exec();
    return( ret );
}