diff --git a/plugins/documentview/kdevdocumentmodel.cpp b/plugins/documentview/kdevdocumentmodel.cpp index b0044b9c52..7b33dec977 100644 --- a/plugins/documentview/kdevdocumentmodel.cpp +++ b/plugins/documentview/kdevdocumentmodel.cpp @@ -1,123 +1,119 @@ /* This file is part of KDevelop Copyright 2005 Adam Treat + Copyright 2013 Sebastian Kügler 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 "kdevdocumentmodel.h" #include #include KDevDocumentItem::KDevDocumentItem( const QString &name ) : QStandardItem( name ), m_documentState( KDevelop::IDocument::Clean ) { - setIcon(icon()); + setIcon( icon() ); } KDevDocumentItem::~KDevDocumentItem() {} -// KDevDocumentItem *KDevDocumentItem::itemAt( int index ) const -// { -// return static_cast( QStandardItem::itemFromIndex( index ) ); -// } - KDevCategoryItem::KDevCategoryItem( const QString &name ) : KDevDocumentItem( name ) { - setIcon(KIcon("inode-directory")); - setToolTip(name); + setIcon( KIcon( "inode-directory" ) ); + setToolTip( name ); } KDevCategoryItem::~KDevCategoryItem() {} QList KDevCategoryItem::fileList() const { QList lst; for ( int i = 0; i < rowCount(); ++i ) { if ( KDevFileItem * item = dynamic_cast( child( i ) ) ->fileItem() ) lst.append( item ); } return lst; } KDevFileItem* KDevCategoryItem::file( const KUrl &url ) const { foreach( KDevFileItem * item, fileList() ) { if ( item->url() == url ) return item; } return 0; } KDevFileItem::KDevFileItem( const KUrl &url ) : KDevDocumentItem( url.fileName() ), m_url( url ) { KFileItem fi = KFileItem( url, QString(), 0 ); m_fileIcon = fi.iconName(); setIcon( KIcon( m_fileIcon ) ); } KDevFileItem::~KDevFileItem() {} KDevDocumentModel::KDevDocumentModel( QObject *parent ) : QStandardItemModel( parent ) { setRowCount(0); setColumnCount(1); } KDevDocumentModel::~KDevDocumentModel() {} QList KDevDocumentModel::categoryList() const { QList lst; for ( int i = 0; i < rowCount() ; ++i ) { if ( KDevCategoryItem * categoryitem = dynamic_cast( item( i ) ) ->categoryItem() ) { lst.append( categoryitem ); } } return lst; } KDevCategoryItem* KDevDocumentModel::category( const QString& category ) const { foreach( KDevCategoryItem * item, categoryList() ) { if ( item->toolTip() == category ) return item; } return 0; } #include "kdevdocumentmodel.moc" diff --git a/plugins/documentview/kdevdocumentmodel.h b/plugins/documentview/kdevdocumentmodel.h index 259a91f7b1..57e24619ec 100644 --- a/plugins/documentview/kdevdocumentmodel.h +++ b/plugins/documentview/kdevdocumentmodel.h @@ -1,139 +1,139 @@ /* This file is part of KDevelop Copyright 2005 Adam Treat + Copyright 2013 Sebastian Kügler 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. */ #ifndef KDEVDOCUMENTMODEL_H #define KDEVDOCUMENTMODEL_H #include #include #include #include #include #include class KUrl; class KDevDocumentItem; class KDevCategoryItem; class KDevFileItem; class KDevDocumentItem: public QStandardItem { public: explicit KDevDocumentItem( const QString &name ); virtual ~KDevDocumentItem(); -// virtual KDevDocumentItem *itemAt( int index ) const; virtual KDevCategoryItem *categoryItem() const { return 0; } virtual KDevFileItem *fileItem() const { return 0; } QIcon icon() const { switch ( m_documentState ) { case KDevelop::IDocument::Clean: return KIcon ( m_fileIcon ); case KDevelop::IDocument::Modified: return KIcon( "document-save" ); case KDevelop::IDocument::Dirty: return KIcon( "document-revert" ); case KDevelop::IDocument::DirtyAndModified: return KIcon( "edit-delete" ); default: - return KIcon("inode-directory"); + return KIcon( "inode-directory" ); } } KDevelop::IDocument::DocumentState documentState() const { return m_documentState; } void setDocumentState( KDevelop::IDocument::DocumentState state ) { m_documentState = state; setIcon(icon()); } protected: QString m_fileIcon; private: KDevelop::IDocument::DocumentState m_documentState; }; class KDevCategoryItem: public KDevDocumentItem { public: explicit KDevCategoryItem( const QString &name ); virtual ~KDevCategoryItem(); virtual KDevCategoryItem *categoryItem() const { return const_cast( this ); } QList fileList() const; KDevFileItem* file( const KUrl &url ) const; }; class KDevFileItem: public KDevDocumentItem { public: explicit KDevFileItem( const KUrl &url ); virtual ~KDevFileItem(); virtual KDevFileItem *fileItem() const { return const_cast( this ); } const KUrl &url() const { return m_url; } void setUrl( const KUrl &url ) { m_url = url; } private: KUrl m_url; }; class KDevDocumentModel: public QStandardItemModel { Q_OBJECT public: KDevDocumentModel( QObject *parent = 0 ); virtual ~KDevDocumentModel(); QList categoryList() const; - KDevCategoryItem* category( const QString& mimeType ) const; + KDevCategoryItem* category( const QString& category ) const; }; #endif // KDEVDOCUMENTMODEL_H diff --git a/plugins/documentview/kdevdocumentview.cpp b/plugins/documentview/kdevdocumentview.cpp index 68b5d7b4d2..47607213e1 100644 --- a/plugins/documentview/kdevdocumentview.cpp +++ b/plugins/documentview/kdevdocumentview.cpp @@ -1,375 +1,367 @@ /* This file is part of KDevelop Copyright 2005 Adam Treat +Copyright 2013 Sebastian Kügler 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 "kdevdocumentview.h" #include "kdevdocumentviewplugin.h" #include "kdevdocumentmodel.h" #include #include #include #include #include #include #include //#include #include #include #include "kdevdocumentselection.h" #include "kdevdocumentviewdelegate.h" #include #include #include #include #include #include #include #include KDevDocumentView::KDevDocumentView( KDevDocumentViewPlugin *plugin, QWidget *parent ) : QTreeView( parent ), m_plugin( plugin ) { - QList projects = KDevelop::ICore::self()->projectController()->projects(); connect(KDevelop::ICore::self()->projectController(), SIGNAL(projectOpened(KDevelop::IProject*)), SLOT(updateProjectPaths())); connect(KDevelop::ICore::self()->projectController(), SIGNAL(projectClosed(KDevelop::IProject*)), SLOT(updateProjectPaths())); m_documentModel = new KDevDocumentModel(); m_delegate = new KDevDocumentViewDelegate( this, this ); m_proxy = new QSortFilterProxyModel( this ); m_proxy->setSourceModel( m_documentModel ); m_proxy->setDynamicSortFilter( true ); m_proxy->setSortCaseSensitivity( Qt::CaseInsensitive ); m_proxy->sort(0); m_selectionModel = new KDevDocumentSelection( m_proxy ); setModel( m_proxy ); setSelectionModel( m_selectionModel ); setItemDelegate( m_delegate ); setObjectName( i18n( "Documents" ) ); setWindowIcon( SmallIcon( "document-multiple" ) ); setWindowTitle( i18n( "Documents" ) ); setFocusPolicy( Qt::NoFocus ); setRootIsDecorated( false ); header()->hide(); setSelectionBehavior( QAbstractItemView::SelectRows ); setSelectionMode( QAbstractItemView::ExtendedSelection ); } KDevDocumentView::~KDevDocumentView() {} KDevDocumentViewPlugin *KDevDocumentView::plugin() const { return m_plugin; } -// void KDevDocumentView::projectOpened(KDevelop::IProject *project) -// { -// //foreach ( const KDevelop::IProject *p, projects) { -// kDebug() << "XXXXX FOLDER: " << project->folder(); -// m_projectFolders << project->folder().path(); -// //m_projectFolders = p->folder().pathOrUrl(); -// //} -// } - void KDevDocumentView::mousePressEvent( QMouseEvent * event ) { QModelIndex proxyIndex = indexAt( event->pos() ); QModelIndex index = m_proxy->mapToSource( proxyIndex ); if ( event->button() == Qt::LeftButton && proxyIndex.parent().isValid() && event->modifiers() == Qt::NoModifier ) { KDevelop::IDocumentController* dc = m_plugin->core()->documentController(); KUrl documentUrl = static_cast( m_documentModel->itemFromIndex( index ) )->fileItem()->url(); if (dc->documentForUrl(documentUrl) != dc->activeDocument()) { dc->openDocument(documentUrl); return; } } if ( !proxyIndex.parent().isValid() ) { setExpanded( proxyIndex, !isExpanded( proxyIndex ) ); return; } QTreeView::mousePressEvent( event ); } template void KDevDocumentView::visitItems(F f, bool selectedItems) { KDevelop::IDocumentController* dc = m_plugin->core()->documentController(); QList docs = selectedItems ? m_selectedDocs : m_unselectedDocs; foreach(const KUrl& url, docs) { KDevelop::IDocument* doc = dc->documentForUrl(url); if (doc) f(doc); } } namespace { class DocSaver { public: void operator()(KDevelop::IDocument* doc) { doc->save(); } }; class DocCloser { public: void operator()(KDevelop::IDocument* doc) { doc->close(); } }; class DocReloader { public: void operator()(KDevelop::IDocument* doc) { doc->reload(); } }; } void KDevDocumentView::saveSelected() { visitItems(DocSaver(), true); } void KDevDocumentView::closeSelected() { visitItems(DocCloser(), true); } void KDevDocumentView::closeUnselected() { visitItems(DocCloser(), false); } void KDevDocumentView::reloadSelected() { visitItems(DocReloader(), true); } void KDevDocumentView::contextMenuEvent( QContextMenuEvent * event ) { updateSelectedDocs(); if (!m_selectedDocs.isEmpty()) { KMenu* ctxMenu = new KMenu(this); KDevelop::FileContext context(m_selectedDocs); QList extensions = m_plugin->core()->pluginController()->queryPluginsForContextMenuExtensions( &context ); QList vcsActions; QList fileActions; QList editActions; QList extensionActions; foreach( const KDevelop::ContextMenuExtension& ext, extensions ) { fileActions += ext.actions(KDevelop::ContextMenuExtension::FileGroup); vcsActions += ext.actions(KDevelop::ContextMenuExtension::VcsGroup); editActions += ext.actions(KDevelop::ContextMenuExtension::EditGroup); extensionActions += ext.actions(KDevelop::ContextMenuExtension::ExtensionGroup); } appendActions(ctxMenu, fileActions); KAction* save = KStandardAction::save(this, SLOT(saveSelected()), ctxMenu); save->setEnabled(selectedDocHasChanges()); ctxMenu->addAction(save); ctxMenu->addAction(KIcon("view-refresh"), i18n( "Reload" ), this, SLOT(reloadSelected())); appendActions(ctxMenu, editActions); ctxMenu->addAction(KStandardAction::close(this, SLOT(closeSelected()), ctxMenu)); QAction* closeUnselected = ctxMenu->addAction(KIcon("document-close"), i18n( "Close Other Files" ), this, SLOT(closeUnselected())); closeUnselected->setEnabled(!m_unselectedDocs.isEmpty()); appendActions(ctxMenu, vcsActions); appendActions(ctxMenu, extensionActions); connect(ctxMenu, SIGNAL(aboutToHide()), ctxMenu, SLOT(deleteLater())); ctxMenu->popup( event->globalPos() ); } } void KDevDocumentView::appendActions(QMenu* menu, const QList& actions) { foreach( QAction* act, actions ) { menu->addAction(act); } menu->addSeparator(); } bool KDevDocumentView::selectedDocHasChanges() { KDevelop::IDocumentController* dc = m_plugin->core()->documentController(); foreach(const KUrl& url, m_selectedDocs) { KDevelop::IDocument* doc = dc->documentForUrl(url); if (!doc) continue; if (doc->state() != KDevelop::IDocument::Clean) { return true; } } return false; } void KDevDocumentView::updateSelectedDocs() { m_selectedDocs.clear(); m_unselectedDocs.clear(); QList allItems = m_documentModel->findItems("*", Qt::MatchWildcard | Qt::MatchRecursive); foreach (QStandardItem* item, allItems) { if (KDevFileItem * fileItem = dynamic_cast(item)->fileItem()) { if (m_selectionModel->isSelected(m_proxy->mapFromSource(m_documentModel->indexFromItem(fileItem)))) m_selectedDocs << fileItem->url(); else m_unselectedDocs << fileItem->url(); } } } void KDevDocumentView::activated( KDevelop::IDocument* document ) { setCurrentIndex( m_proxy->mapFromSource( m_documentModel->indexFromItem( m_doc2index[ document ] ) ) ); } void KDevDocumentView::saved( KDevelop::IDocument* ) { } void KDevDocumentView::opened( KDevelop::IDocument* document ) { QString label = document->url().path(); - QStringList ps = label.split( '/' ); + QStringList ps = label.split( QDir::separator() ); ps.removeLast(); - label = ps.join( "/" ) + '/'; + label = ps.join( QDir::separator() ) + QDir::separator(); KDevCategoryItem *mimeItem = m_documentModel->category( label ); if ( !mimeItem ) { mimeItem = new KDevCategoryItem( label ); m_documentModel->insertRow( m_documentModel->rowCount(), mimeItem ); setExpanded( m_proxy->mapFromSource( m_documentModel->indexFromItem( mimeItem ) ), false); updateCategoryItem(mimeItem); } if ( !mimeItem->file( document->url() ) ) { KDevFileItem * fileItem = new KDevFileItem( document->url() ); mimeItem->setChild( mimeItem->rowCount(), fileItem ); setCurrentIndex( m_proxy->mapFromSource( m_documentModel->indexFromItem( fileItem ) ) ); m_doc2index[ document ] = fileItem; } } void KDevDocumentView::closed( KDevelop::IDocument* document ) { KDevFileItem* file = m_doc2index[ document ]; if ( !file ) return; QStandardItem* mimeItem = file->parent(); qDeleteAll(mimeItem->takeRow(m_documentModel->indexFromItem(file).row())); m_doc2index.remove(document); if ( mimeItem->hasChildren() ) return; qDeleteAll(m_documentModel->takeRow(m_documentModel->indexFromItem(mimeItem).row())); doItemsLayout(); } void KDevDocumentView::updateCategoryItem( KDevCategoryItem *item ) { - const QString homePath = QDir::homePath(); QString label = item->toolTip(); - foreach (const QString &projectPath, m_projectFolders) { - label.replace( projectPath, "" ); - } - label.replace( homePath, "~" ); - QStringList ps = label.split( '/' ); + foreach ( const QString &projectPath, m_projectFolders ) + label.replace( projectPath, QString() ); + + label.replace( QDir::homePath(), "~" ); + + QStringList ps = label.split( QDir::separator() ); ps.removeLast(); - label = ps.join( "/" ) + '/'; - item->setText(label); + label = ps.join( QDir::separator() ) + QDir::separator(); + + item->setText( label ); } bool longerThan( const QString &s1, const QString &s2 ) { // compare path depth of two directories - return s1.split('/').count() > s2.split('/').count(); + return s1.split( QDir::separator() ).count() > s2.split( QDir::separator() ).count(); } void KDevDocumentView::updateProjectPaths() { QList projects = KDevelop::ICore::self()->projectController()->projects(); m_projectFolders.clear(); - foreach ( KDevelop::IProject *p, projects ) { + + foreach ( KDevelop::IProject *p, projects ) m_projectFolders << p->folder().pathOrUrl(); - } + // sort folders, longest first, so replacing them one by one is save - qSort(m_projectFolders.begin(), m_projectFolders.end(), longerThan); + qSort( m_projectFolders.begin(), m_projectFolders.end(), longerThan ); - foreach ( KDevCategoryItem *it, m_documentModel->categoryList() ) { + foreach ( KDevCategoryItem *it, m_documentModel->categoryList() ) updateCategoryItem( it ); - } } void KDevDocumentView::contentChanged( KDevelop::IDocument* ) { } void KDevDocumentView::stateChanged( KDevelop::IDocument* document ) { KDevDocumentItem * documentItem = m_doc2index[ document ]; if ( documentItem && documentItem->documentState() != document->state() ) documentItem->setDocumentState( document->state() ); doItemsLayout(); } void KDevDocumentView::documentUrlChanged( KDevelop::IDocument* document ) { closed(document); opened(document); } #include "kdevdocumentview.moc" diff --git a/plugins/documentview/kdevdocumentview.h b/plugins/documentview/kdevdocumentview.h index 2ac54c19aa..b05c4d7cac 100644 --- a/plugins/documentview/kdevdocumentview.h +++ b/plugins/documentview/kdevdocumentview.h @@ -1,97 +1,98 @@ /* This file is part of KDevelop Copyright 2005 Adam Treat + Copyright 2013 Sebastian Kügler 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. */ #ifndef KDEVDOCUMENTVIEW_H #define KDEVDOCUMENTVIEW_H #include class QSortFilterProxyModel; class KAction; class KUrl; class KDevDocumentViewPlugin; class KDevDocumentModel; class KDevDocumentViewDelegate; class KDevDocumentSelection; class KDevFileItem; class KMenu; namespace KDevelop { class IDocument; class IProject; } class KDevCategoryItem; class KDevDocumentModel; class KDevDocumentItem; class KDevDocumentView: public QTreeView { Q_OBJECT public: explicit KDevDocumentView( KDevDocumentViewPlugin *plugin, QWidget *parent ); virtual ~KDevDocumentView(); KDevDocumentViewPlugin *plugin() const; signals: void activateURL( const KUrl &url ); public slots: void opened( KDevelop::IDocument* document ); private slots: void activated( KDevelop::IDocument* document ); void saved( KDevelop::IDocument* document ); void closed( KDevelop::IDocument* document ); void contentChanged( KDevelop::IDocument* document ); void stateChanged( KDevelop::IDocument* document ); void documentUrlChanged( KDevelop::IDocument* document ); void updateCategoryItem( KDevCategoryItem *item ); void updateProjectPaths(); void saveSelected(); void reloadSelected(); void closeSelected(); void closeUnselected(); protected: virtual void mousePressEvent( QMouseEvent * event ); virtual void contextMenuEvent( QContextMenuEvent * event ); private: template void visitItems(F, bool selectedItems); bool selectedDocHasChanges(); void updateSelectedDocs(); void appendActions(QMenu* menu, const QList< QAction* >& actions); private: KDevDocumentViewPlugin *m_plugin; KDevDocumentModel *m_documentModel; KDevDocumentSelection* m_selectionModel; QSortFilterProxyModel* m_proxy; KDevDocumentViewDelegate* m_delegate; QHash< KDevelop::IDocument*, KDevFileItem* > m_doc2index; QList m_selectedDocs; // used for ctx menu QList m_unselectedDocs; // used for ctx menu QStringList m_projectFolders; }; #endif // KDEVDOCUMENTVIEW_H diff --git a/plugins/documentview/kdevdocumentviewdelegate.cpp b/plugins/documentview/kdevdocumentviewdelegate.cpp index fd12063651..79e04322b5 100644 --- a/plugins/documentview/kdevdocumentviewdelegate.cpp +++ b/plugins/documentview/kdevdocumentviewdelegate.cpp @@ -1,92 +1,93 @@ /* This file is part of KDevelop Copyright 2005 Adam Treat +Copyright 2013 Sebastian Kügler 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 "kdevdocumentviewdelegate.h" #include #include KDevDocumentViewDelegate::KDevDocumentViewDelegate( QTreeView *view, QObject *parent ) : QItemDelegate( parent ), m_view( view ) {} KDevDocumentViewDelegate::~KDevDocumentViewDelegate() {} void KDevDocumentViewDelegate::paint( QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index ) const { const QAbstractItemModel * model = index.model(); Q_ASSERT( model ); // easy way to disable custom painting for top level items - const bool useExpandIndicator = true; + const bool useExpandIndicator = true; if ( !model->parent( index ).isValid() && useExpandIndicator) { // this is a top-level item. QStyleOptionButton buttonOption; buttonOption.state = option.state; #ifdef Q_WS_MAC buttonOption.state |= QStyle::State_Raised; #endif buttonOption.state &= ~QStyle::State_HasFocus; buttonOption.rect = option.rect; buttonOption.palette = option.palette; buttonOption.features = QStyleOptionButton::None; QStyleOption branchOption; static const int i = 9; // ### hardcoded in qcommonstyle.cpp QRect r = option.rect; branchOption.rect = QRect( r.left() + i / 2, r.top() + ( r.height() - i ) / 2, i, i ); branchOption.palette = option.palette; branchOption.state = QStyle::State_Children; if ( m_view->isExpanded( index ) ) branchOption.state |= QStyle::State_Open; m_view->style() ->drawPrimitive( QStyle::PE_IndicatorBranch, &branchOption, painter, m_view ); // draw text QRect textrect = QRect( r.left() + i * 2, r.top(), r.width() - ( ( 5 * i ) / 2 ), r.height() ); QString text = elidedText( option.fontMetrics, textrect.width(), Qt::ElideRight, model->data( index, Qt::DisplayRole ).toString() ); m_view->style() ->drawItemText( painter, textrect, Qt::AlignLeft | Qt::AlignVCenter, option.palette, m_view->isEnabled(), text ); } else { QItemDelegate::paint( painter, option, index ); } } QSize KDevDocumentViewDelegate::sizeHint( const QStyleOptionViewItem &opt, const QModelIndex &index ) const { QStyleOptionViewItem option = opt; QSize sz = QItemDelegate::sizeHint( opt, index ) + QSize( 2, 4 ); return sz; } #include "kdevdocumentviewdelegate.moc"