File indexing completed on 2024-10-06 04:26:02

0001 /*
0002     SPDX-FileCopyrightText: 2003-2008 Sebastian Trueg <trueg@k3b.org>
0003     SPDX-FileCopyrightText: 2009-2010 Michal Malek <michalm@jabster.pl>
0004     SPDX-FileCopyrightText: 1998-2008 Sebastian Trueg <trueg@k3b.org>
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 
0009 
0010 #include "k3bprojecttabwidget.h"
0011 #include "k3bapplication.h"
0012 #include "k3bprojectmanager.h"
0013 
0014 #include "k3bview.h"
0015 #include "k3bdoc.h"
0016 
0017 #include <KLocalizedString>
0018 #include <KIconLoader>
0019 #include <KActionMenu>
0020 
0021 #include <QDebug>
0022 #include <QMimeData>
0023 #include <QUrl>
0024 #include <QDragMoveEvent>
0025 #include <QIcon>
0026 #include <QDropEvent>
0027 #include <QDragEnterEvent>
0028 #include <QMouseEvent>
0029 #include <QAction>
0030 #include <QMenu>
0031 #include <QTabBar>
0032 
0033 namespace {
0034     
0035     class ProjectData
0036     {
0037     public:
0038         ProjectData()
0039             : doc(0),
0040             modified(false) {
0041         }
0042 
0043         ProjectData( K3b::Doc* doc_ )
0044             : doc(doc_),
0045             modified(false) {
0046         }
0047 
0048         // the project
0049         K3b::Doc* doc;
0050 
0051         // is the project marked modified here
0052         bool modified;
0053     };
0054     
0055 } // namespace
0056 
0057 class K3b::ProjectTabWidget::Private
0058 {
0059 public:
0060     KActionMenu* projectActionMenu;
0061     QMap<Doc*, ProjectData> projectDataMap;
0062 };
0063 
0064 
0065 K3b::ProjectTabWidget::ProjectTabWidget( QWidget *parent )
0066     : QTabWidget( parent ),
0067       d( new Private )
0068 {
0069     setDocumentMode( true );
0070     setTabsClosable( true );
0071     setMovable( true );
0072     tabBar()->setAcceptDrops(true);
0073     tabBar()->installEventFilter( this );
0074 
0075     d->projectActionMenu = new KActionMenu( i18n("Project"), this );
0076     
0077     connect( this, SIGNAL(tabCloseRequested(int)), SLOT(slotTabCloseRequested(int)));
0078 }
0079 
0080 
0081 K3b::ProjectTabWidget::~ProjectTabWidget()
0082 {
0083     delete d;
0084 }
0085 
0086 
0087 void K3b::ProjectTabWidget::addTab( Doc* doc )
0088 {
0089     QTabWidget::addTab( doc->view(), doc->URL().fileName() );
0090     connect( k3bappcore->projectManager(), SIGNAL(projectSaved(K3b::Doc*)), this, SLOT(slotDocSaved(K3b::Doc*)) );
0091     connect( doc, SIGNAL(changed(K3b::Doc*)), this, SLOT(slotDocChanged(K3b::Doc*)) );
0092 
0093     d->projectDataMap[doc] = ProjectData( doc );
0094 
0095     if( doc->isModified() )
0096         slotDocChanged( doc );
0097     else
0098         slotDocSaved( doc );
0099 }
0100 
0101 
0102 void K3b::ProjectTabWidget::removeTab( Doc* doc )
0103 {
0104     if( doc != 0 ) {
0105         QTabWidget::removeTab( indexOf( doc->view() ) );
0106     }
0107 }
0108 
0109 
0110 void K3b::ProjectTabWidget::setCurrentTab( Doc* doc )
0111 {
0112     if( doc != 0 ) {
0113         setCurrentWidget( doc->view() );
0114         doc->view()->setFocus();
0115     }
0116 }
0117 
0118 
0119 K3b::Doc* K3b::ProjectTabWidget::currentTab() const
0120 {
0121     QWidget* widget = currentWidget();
0122     if( K3b::View* view = qobject_cast<K3b::View*>(widget) )
0123         return view->doc();
0124     else
0125         return 0;
0126 }
0127 
0128 
0129 void K3b::ProjectTabWidget::addAction( QAction* action )
0130 {
0131     d->projectActionMenu->addAction( action );
0132 }
0133 
0134 
0135 K3b::Doc* K3b::ProjectTabWidget::projectAt( const QPoint& pos ) const
0136 {
0137     int tabPos = tabBar()->tabAt(pos);
0138     if( tabPos != -1 )
0139     {
0140         QWidget *w = widget(tabPos);
0141         if(View* view = qobject_cast<View*>(w) )
0142             return view->doc();
0143     }
0144     return 0;
0145 }
0146 
0147 
0148 bool K3b::ProjectTabWidget::eventFilter( QObject* o, QEvent* e )
0149 {
0150     if( o == tabBar() ) {
0151         if( e->type() == QEvent::MouseButtonPress ) {
0152             QMouseEvent* me = static_cast<QMouseEvent*>(e);
0153             if( me->button() == Qt::RightButton ) {
0154                 if( projectAt( me->pos() ) ) {
0155                     int tabPos = tabBar()->tabAt(me->pos());
0156                     if(tabPos!=-1){
0157                         setCurrentIndex(tabPos);
0158                         // show the popup menu
0159                         d->projectActionMenu->menu()->exec( me->globalPos() );
0160                     }
0161                 }
0162                 return true;
0163             }
0164         }
0165 
0166         else if( e->type() == QEvent::DragEnter ) {
0167             QDragEnterEvent* de = static_cast<QDragEnterEvent*>(e);
0168             de->setAccepted( de->mimeData()->hasUrls() );
0169             return true;
0170         }
0171 
0172         else if( e->type() == QEvent::DragMove ) {
0173             QDragMoveEvent* de = static_cast<QDragMoveEvent*>(e);
0174             de->setAccepted( projectAt(de->pos()) != 0 );
0175             return true;
0176         }
0177 
0178         else if( e->type() == QEvent::Drop ) {
0179             QDropEvent* de = static_cast<QDropEvent*>(e);
0180             if( de->mimeData()->hasUrls() ) {
0181                 if( Doc* doc = projectAt( de->pos() ) ) {
0182                     QList<QUrl> urls;
0183                     Q_FOREACH( const QUrl& url, de->mimeData()->urls() ) {
0184                         urls.append( url );
0185                     }
0186                     qobject_cast<View*>(doc->view())->addUrls( urls );
0187                 }
0188             }
0189             return true;
0190         }
0191     }
0192 
0193     return QTabWidget::eventFilter( o, e );
0194 }
0195 
0196 
0197 void K3b::ProjectTabWidget::slotDocChanged( K3b::Doc* doc )
0198 {
0199     // we need to cache the icon changes since the changed() signal will be emitted very often
0200     if( !d->projectDataMap[doc].modified ) {
0201         setTabIcon( indexOf( doc->view() ), QIcon::fromTheme( "document-save" ) );
0202         d->projectDataMap[doc].modified = true;
0203 
0204         // we need this one for the session management
0205         setTabText( indexOf( doc->view() ), doc->URL().fileName() );
0206     }
0207 }
0208 
0209 
0210 void K3b::ProjectTabWidget::slotDocSaved( K3b::Doc* doc )
0211 {
0212     setTabIcon( indexOf( doc->view() ), QIcon() );
0213     setTabText( indexOf( doc->view() ), doc->URL().fileName() );
0214 }
0215 
0216 
0217 void K3b::ProjectTabWidget::slotTabCloseRequested( int index )
0218 {
0219     QWidget* w = widget( index );
0220     if( View* view = qobject_cast<View*>(w) ) {  
0221         emit tabCloseRequested( view->doc() );
0222     }
0223 }
0224 
0225 #include "moc_k3bprojecttabwidget.cpp"