summaryrefslogtreecommitdiffstats
path: root/src/k3bprojecttabwidget.cpp
blob: f5ae1b88e931431c4b8daebf4944dda60aa9ca8a (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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/* 
 *
 * $Id: k3bprojecttabwidget.cpp 619556 2007-01-03 17:38:12Z trueg $
 * Copyright (C) 2003 Sebastian Trueg <trueg@k3b.org>
 *
 * This file is part of the K3b project.
 * Copyright (C) 1998-2007 Sebastian Trueg <trueg@k3b.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * See the file "COPYING" for the exact licensing terms.
 */



#include "k3bprojecttabwidget.h"
#include "k3bapplication.h"
#include "k3bprojectmanager.h"

#include <k3bview.h>
#include <k3bdoc.h>

#include <tdeaction.h>
#include <kiconloader.h>
#include <kdebug.h>
#include <kurldrag.h>
#include <tdelocale.h>

#include <tqevent.h>
#include <tqtabbar.h>


class K3bProjectTabWidget::ProjectData
{
public:
  ProjectData()
    : doc(0),
      modified(false) {
  }

  ProjectData( K3bDoc* doc_ )
    : doc(doc_),
      modified(false) {
  }

  // the project
  K3bDoc* doc;

  // is the project marked modified here
  bool modified;
};



K3bProjectTabWidget::K3bProjectTabWidget( TQWidget *parent, const char *name, WFlags f )
  : TQTabWidget( parent, name, f )
{
  tabBar()->setAcceptDrops(true);
  tabBar()->installEventFilter( this );

  m_projectActionMenu = new TDEActionMenu( i18n("Project"), this );
}


K3bProjectTabWidget::~K3bProjectTabWidget()
{
}


void K3bProjectTabWidget::addTab( TQWidget* child, const TQString& label )
{
  TQTabWidget::addTab( child, label );
  tabBar()->setShown( count() != 1 );
}


void K3bProjectTabWidget::addTab( TQWidget* child, const TQIconSet& iconset, const TQString& label )
{
  TQTabWidget::addTab( child, iconset, label );
  tabBar()->setShown( count() != 1 );
}


void K3bProjectTabWidget::addTab( TQWidget* child, TQTab* tab )
{
  TQTabWidget::addTab( child, tab );
  tabBar()->setShown( count() != 1 );
}


void K3bProjectTabWidget::insertTab( TQWidget* child, const TQString& label, int index )
{
  TQTabWidget::insertTab( child, label, index );
  tabBar()->setShown( count() != 1 );
}


void K3bProjectTabWidget::insertTab( TQWidget* child, const TQIconSet& iconset, const TQString& label, int index )
{
  TQTabWidget::insertTab( child, iconset, label, index );
  tabBar()->setShown( count() != 1 );
}


void K3bProjectTabWidget::insertTab( TQWidget* child, TQTab* tab, int index )
{
  TQTabWidget::insertTab( child, tab, index );
  tabBar()->setShown( count() != 1 );
}


void K3bProjectTabWidget::removePage( TQWidget* w )
{
  TQTabWidget::removePage( w );
  tabBar()->setShown( count() != 1 );
}


void K3bProjectTabWidget::insertTab( K3bDoc* doc )
{
  TQTabWidget::insertTab( doc->view(), doc->URL().fileName() );
  connect( k3bappcore->projectManager(), TQT_SIGNAL(projectSaved(K3bDoc*)), this, TQT_SLOT(slotDocSaved(K3bDoc*)) );
  connect( doc, TQT_SIGNAL(changed(K3bDoc*)), this, TQT_SLOT(slotDocChanged(K3bDoc*)) );

  m_projectDataMap[doc] = ProjectData( doc );

  if( doc->isModified() )
    slotDocChanged( doc );
  else
    slotDocSaved( doc );
}


void K3bProjectTabWidget::insertAction( TDEAction* action )
{
  m_projectActionMenu->insert( action );
}


void K3bProjectTabWidget::slotDocChanged( K3bDoc* doc )
{
  // we need to cache the icon changes since the changed() signal will be emitted very often
  if( !m_projectDataMap[doc].modified ) {
    setTabIconSet( doc->view(), SmallIconSet( "document-save" ) );
    m_projectDataMap[doc].modified = true;

    // we need this one for the session management
    changeTab( doc->view(), doc->URL().fileName() );
  }
}


void K3bProjectTabWidget::slotDocSaved( K3bDoc* doc )
{
  setTabIconSet( doc->view(), TQIconSet() );
  changeTab( doc->view(), doc->URL().fileName() );
}


K3bDoc* K3bProjectTabWidget::projectAt( const TQPoint& pos ) const
{
  TQTab* tab = tabBar()->selectTab( pos );
  if( tab ) {
    TQWidget* w = page( tabBar()->indexOf( tab->identifier() ) );
    if( K3bView* view = dynamic_cast<K3bView*>(w) )
      return view->doc();
  }

  return 0;
}


bool K3bProjectTabWidget::eventFilter( TQObject* o, TQEvent* e )
{
  if( o == tabBar() ) {
    if( e->type() == TQEvent::MouseButtonPress ) {
      TQMouseEvent* me = static_cast<TQMouseEvent*>(e);
      if( me->button() == TQt::RightButton ) {
	if( projectAt( me->pos() ) ) {
	  // we need change the tab because the actions work on the current tab
	  TQTab* clickedTab = tabBar()->selectTab( me->pos() );
	  if( clickedTab ) {
	    tabBar()->setCurrentTab( clickedTab );
	    
	    // show the popup menu
	    m_projectActionMenu->popup( me->globalPos() );
	  }
	}
	return true;
      }
    }

    else if( e->type() == TQEvent::DragMove ) {
      TQDragMoveEvent* de = static_cast<TQDragMoveEvent*>(e);
      de->accept( KURLDrag::canDecode(de) && projectAt(de->pos()) );
      return true;
    }

    else if( e->type() == TQEvent::Drop ) {
      TQDropEvent* de = static_cast<TQDropEvent*>(e);
      KURL::List l;
      if( KURLDrag::decode( de, l ) ) {
	if( K3bDoc* doc = projectAt( de->pos() ) )
	  dynamic_cast<K3bView*>(doc->view())->addUrls( l );
      }
      return true;
    }
  }

  return TQTabWidget::eventFilter( o, e );
}

#include "k3bprojecttabwidget.moc"