File indexing completed on 2024-10-13 04:22:10

0001 /*
0002     SPDX-FileCopyrightText: 2003-2007 Sebastian Trueg <trueg@k3b.org>
0003     SPDX-FileCopyrightText: 2009 Gustavo Pichorim Boiko <gustavo.boiko@kdemail.net>
0004     SPDX-FileCopyrightText: 1998-2009 Sebastian Trueg <trueg@k3b.org>
0005 
0006     SPDX-License-Identifier: GPL-2.0-or-later
0007 */
0008 
0009 
0010 #include "k3bfiletreeview.h"
0011 #include "k3bappdevicemanager.h"
0012 #include "k3bapplication.h"
0013 #include "k3bdevice.h"
0014 #include "k3bglobals.h"
0015 #include "k3bplacesmodel.h"
0016 #include "k3bdevicedelegate.h"
0017 #include "k3bdevicemenu.h"
0018 #include "k3baction.h"
0019 #include "k3b.h"
0020 
0021 #include <KLocalizedString>
0022 #include <KFileItem>
0023 #include <KActionMenu>
0024 
0025 #include <QUrl>
0026 #include <QAction>
0027 
0028 
0029 
0030 class K3b::FileTreeView::Private
0031 {
0032 public:
0033     Private()
0034         : deviceManager(0) {
0035     }
0036 
0037     K3b::Device::DeviceManager* deviceManager;
0038 
0039     K3b::PlacesModel* model;
0040 
0041     KActionCollection* actionCollection;
0042     K3b::DeviceMenu* devicePopupMenu;
0043     KActionMenu* urlPopupMenu;
0044     bool menuEnabled;
0045 };
0046 
0047 
0048 K3b::FileTreeView::FileTreeView( QWidget *parent )
0049     : QTreeView( parent ),
0050       d( new Private() )
0051 {
0052     setHeaderHidden( true );
0053 
0054     setContextMenuPolicy( Qt::CustomContextMenu );
0055     setEditTriggers( QAbstractItemView::NoEditTriggers );
0056     setSelectionMode(QAbstractItemView::SingleSelection);
0057 //    setSortingEnabled( true );
0058 //    setRootIsDecorated( false );
0059     setDragEnabled( true );
0060     setVerticalScrollMode( QAbstractItemView::ScrollPerPixel );
0061 
0062     K3b::DeviceDelegate* delegate = new K3b::DeviceDelegate(this);
0063     setItemDelegate(delegate);
0064 
0065     d->model = new K3b::PlacesModel( this );
0066     setModel( d->model );
0067 
0068     d->actionCollection = new KActionCollection( this );
0069     d->devicePopupMenu = new K3b::DeviceMenu( this );
0070     d->urlPopupMenu = new KActionMenu(this);
0071     initActions();
0072 
0073     // react on K3b::PlacesModel::expandToUrl calls
0074     connect( d->model, SIGNAL(expand(QModelIndex)),
0075              this, SLOT(slotExpandUrl(QModelIndex)) );
0076 
0077     connect( this, SIGNAL(clicked(QModelIndex)), SLOT(slotClicked(QModelIndex)) );
0078     connect( this, SIGNAL(customContextMenuRequested(QPoint)), SLOT(slotContextMenu(QPoint)) );
0079 }
0080 
0081 
0082 K3b::FileTreeView::~FileTreeView()
0083 {
0084     delete d;
0085 }
0086 
0087 void K3b::FileTreeView::initActions()
0088 {
0089     // those actions are supposed to be used with url items
0090     d->urlPopupMenu->addAction( K3b::createAction(this,i18n("&Add to Project"), 0, Qt::SHIFT|Qt::Key_Return,
0091                                                   this, SLOT(slotAddFilesToProject()),
0092                                                   d->actionCollection, "add_files_to_project") );
0093 }
0094 
0095 K3b::Device::Device* K3b::FileTreeView::selectedDevice() const
0096 {
0097     return d->model->deviceForIndex( currentIndex() );
0098 }
0099 
0100 
0101 QUrl K3b::FileTreeView::selectedUrl() const
0102 {
0103     KFileItem fileItem = d->model->itemForIndex( currentIndex() );
0104     if( fileItem.isNull() )
0105         return QUrl();
0106     else
0107         return fileItem.url();
0108 }
0109 
0110 
0111 void K3b::FileTreeView::slotExpandUrl( const QModelIndex& index )
0112 {
0113     qDebug();
0114     setCurrentIndex( index );
0115     scrollTo( index );
0116 }
0117 
0118 void K3b::FileTreeView::slotAddFilesToProject()
0119 {
0120     QModelIndexList indexes = selectedIndexes();
0121     QList<QUrl> files;
0122     foreach(const QModelIndex &index, indexes)
0123     {
0124         KFileItem item = d->model->itemForIndex(index);
0125         if (item.isNull())
0126             continue;
0127 
0128         files.append(item.url());
0129     }
0130 
0131     if (!files.isEmpty())
0132         k3bappcore->k3bMainWindow()->addUrls(files);
0133 }
0134 
0135 
0136 void K3b::FileTreeView::setSelectedUrl( const QUrl& url )
0137 {
0138     qDebug();
0139     KFileItem fileItem = d->model->itemForIndex( currentIndex() );
0140     if( fileItem.isNull() || !fileItem.url().matches( url, QUrl::StripTrailingSlash ) ) {
0141         d->model->expandToUrl( url );
0142     }
0143 }
0144 
0145 
0146 void K3b::FileTreeView::setSelectedDevice( K3b::Device::Device* dev )
0147 {
0148     Device::Device* currentDev = d->model->deviceForIndex( currentIndex() );
0149     if( currentDev != dev ) {
0150         setCurrentIndex( d->model->indexForDevice( dev ) );
0151     }
0152 }
0153 
0154 
0155 void K3b::FileTreeView::slotClicked( const QModelIndex& index )
0156 {
0157     if ( K3b::Device::Device* dev = d->model->deviceForIndex( index ) ) {
0158         k3bappcore->appDeviceManager()->setCurrentDevice( dev );
0159         emit activated( dev );
0160     }
0161     else if ( index.isValid() ) {
0162         KFileItem fileItem = d->model->itemForIndex( index );
0163         if ( !fileItem.isNull() ) {
0164             emit activated( fileItem.url() );
0165         }
0166     }
0167 }
0168 
0169 
0170 void K3b::FileTreeView::slotContextMenu( const QPoint& pos )
0171 {
0172     // check if the context menu is for a device item
0173     QModelIndex index = indexAt( pos );
0174     if ( K3b::Device::Device* dev = d->model->deviceForIndex( index ) ) {
0175         k3bappcore->appDeviceManager()->setCurrentDevice( dev );
0176         d->devicePopupMenu->exec( mapToGlobal( pos ) );
0177     }
0178 
0179     // ... or if it is for an url item
0180     KFileItem item = d->model->itemForIndex( index );
0181     if ( !item.isNull() )
0182     {
0183         // enable/disable the "add to project" action
0184         d->actionCollection->action("add_files_to_project")->setEnabled(k3bappcore->k3bMainWindow()->activeView() != 0);
0185 
0186         // and shows the menu
0187         d->urlPopupMenu->menu()->exec( mapToGlobal( pos ) );
0188     }
0189 }
0190 
0191 #include "moc_k3bfiletreeview.cpp"