summaryrefslogtreecommitdiffstats
path: root/tdeio/tdefile/kcombiview.cpp
blob: ae408c8e2253858d470f98104619cecfa804a2c5 (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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/* This file is part of the KDE libraries
    Copyright (C) 1998 Stephan Kulow <coolo@kde.org>
                  1998 Daniel Grana <grana@ie.iwi.unibe.ch>

    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.
*/

// $Id$

#include <assert.h>

#include "tdefileitem.h"
#include "kcombiview.h"
#include "tdefileiconview.h"
#include "tdefiledetailview.h"
#include "config-tdefile.h"

#include <tqevent.h>

#include <tqdir.h>

#include <kapplication.h>
#include <tdeconfig.h>
#include <kdebug.h>
#include <kglobal.h>

#include <tqvaluelist.h>

KCombiView::KCombiView( TQWidget *parent, const char *name)
  : TQSplitter( parent, name),
    KFileView(),
    right(0),
    m_lastViewForNextItem(0),
    m_lastViewForPrevItem(0)
{
    left = new KFileIconView( this, "left" );
    left->setAcceptDrops(false);
    left->viewport()->setAcceptDrops(false);
    left->setGridX( 160 );
    left->KFileView::setViewMode( Directories );
    left->setArrangement( TQIconView::LeftToRight );
    left->setParentView( this );
    left->setAcceptDrops(false);
    left->installEventFilter( this );
    
    connect( sig, TQT_SIGNAL( sortingChanged( TQDir::SortSpec ) ),
             TQT_SLOT( slotSortingChanged( TQDir::SortSpec ) ));
}

KCombiView::~KCombiView()
{
    delete right;
}

void KCombiView::setRight(KFileView *view)
{
    delete right;
    right = view;
    right->KFileView::setViewMode( Files );
    setViewName( right->viewName() );

    TQValueList<int> lst;
    lst << left->gridX() + 2 * left->spacing();
    setSizes( lst );
    setResizeMode( left, TQSplitter::KeepSize );

    right->setParentView( this );
    right->widget()->setAcceptDrops(acceptDrops());
    right->setDropOptions(dropOptions());
    right->widget()->installEventFilter( this );
}


void KCombiView::insertItem( KFileItem *item )
{
    KFileView::insertItem( item );

    if ( item->isDir() ) {
        left->updateNumbers( item );
        left->insertItem( item );
    }
    else {
        right->updateNumbers( item );
        right->insertItem( item );
    }
}

void KCombiView::setSorting( TQDir::SortSpec sort )
{
    if ( !right )
        kdFatal() << "You need to call setRight( someview ) before!" << endl;
    right->setSorting( sort );
    left->setSorting( sort );

    KFileView::setSorting( right->sorting() );
}

void KCombiView::clearView()
{
    left->clearView();
    if ( right )
        right->clearView();
}

void KCombiView::updateView( bool b )
{
    left->updateView( b );
    if ( right )
        right->updateView( b );
}

void KCombiView::updateView( const KFileItem *i )
{
    left->updateView( i );
    if ( right )
        right->updateView( i );
}

void KCombiView::removeItem( const KFileItem *i )
{
    left->removeItem( i );
    if ( right )
        right->removeItem( i );
    KFileView::removeItem( i );
}

void KCombiView::listingCompleted()
{
    left->listingCompleted();
    if ( right )
        right->listingCompleted();
}

void KCombiView::clear()
{
    KFileView::clear();
    left->KFileView::clear();
    if ( right )
        right->clear();
}

void KCombiView::clearSelection()
{
    left->clearSelection();
    if ( right )
        right->clearSelection();
}

void KCombiView::selectAll()
{
    left->selectAll();
    if ( right )
        right->selectAll();
}

void KCombiView::invertSelection()
{
    left->invertSelection();
    if ( right )
        right->invertSelection();
}

bool KCombiView::isSelected( const KFileItem *item ) const
{
    assert( right ); // for performance reasons no if ( right ) check.
    return (right->isSelected( item ) || left->isSelected( item ));
}

void KCombiView::setSelectionMode( KFile::SelectionMode sm )
{
    // I think the left view (directories should always be in
    // Single-Mode, right?
    // left->setSelectionMode( sm );
    if ( !right )
        kdFatal() << "You need to call setRight( someview ) before!" << endl;
    right->setSelectionMode( sm );
}

void KCombiView::setSelected( const KFileItem *item, bool enable )
{
    left->setSelected( item, enable );
    if ( right )
        right->setSelected( item, enable );
}

void KCombiView::setCurrentItem( const KFileItem *item )
{
    left->setCurrentItem( item );
    if ( right )
        right->setCurrentItem( item );
}

KFileItem * KCombiView::currentFileItem() const
{
    // we can actually have two current items, one in each view. So we simply
    // prefer the fileview's item over the directory's.
    // Smarter: if the right view has focus, prefer that over the left.
    if ( !right )
        return left->currentFileItem();

    KFileView *preferredView = focusView( right );
    KFileItem *item = preferredView->currentFileItem();
    if ( !item && preferredView != left )
        item = left->currentFileItem();

    return item;
}

void KCombiView::ensureItemVisible(const KFileItem *item)
{
    left->ensureItemVisible( item );
    if ( right )
        right->ensureItemVisible( item );
}

KFileItem * KCombiView::firstFileItem() const
{
    if ( !right )
        return left->firstFileItem();

    KFileView *preferredView = focusView( left );
    KFileView *otherView = (preferredView == left) ? right : left;
    KFileItem *item = preferredView->firstFileItem();
    if ( !item )
        item = otherView->firstFileItem();

    return item;
}

KFileItem * KCombiView::nextItem( const KFileItem *fileItem ) const
{
    if ( !right )
        return left->nextItem( fileItem );

    KFileView *preferredView = focusView( left );
    KFileView *otherView = (preferredView == left) ? right : left;
    KFileItem *item = preferredView->nextItem( fileItem );

    if ( item )
        m_lastViewForNextItem = preferredView;
    else { // no item, check other view
        // when changing from one to another view, we need to continue
        // with the next view's first item!
        if ( m_lastViewForNextItem != otherView ) {
            m_lastViewForNextItem = otherView;
            return otherView->firstFileItem();
        }

        item = otherView->nextItem( fileItem );
        m_lastViewForNextItem = otherView;
    }

    return item;
}

KFileItem * KCombiView::prevItem( const KFileItem *fileItem ) const
{
    if ( !right )
        return left->nextItem( fileItem );

    KFileView *preferredView = focusView( left );
    KFileView *otherView = (preferredView == left) ? right : left;
    KFileItem *item = preferredView->prevItem( fileItem );
    if ( item )
        m_lastViewForPrevItem = preferredView;

    else { // no item, check other view
        // when changing from one to another view, we need to continue
        // with the next view's last item!
        if ( m_lastViewForPrevItem != otherView ) {
            fileItem = otherView->firstFileItem();
            while ( otherView->nextItem( fileItem ) ) // find the last item
                fileItem = otherView->nextItem( fileItem );
        }

        item = otherView->prevItem( fileItem );
        m_lastViewForPrevItem = otherView;
    }

    return item;
}

void KCombiView::slotSortingChanged( TQDir::SortSpec sorting )
{
    KFileView::setSorting( sorting );
}

KFileView *KCombiView::focusView( KFileView *preferred ) const
{
    TQWidget *w = focusWidget();
    KFileView *other = (right == preferred) ? left : right;
    return (preferred && w == preferred->widget()) ? preferred : other;
}

void KCombiView::readConfig( TDEConfig *config, const TQString& group )
{
    left->readConfig( config, group );
    if ( right )
        right->readConfig( config, group );
}

void KCombiView::writeConfig( TDEConfig *config, const TQString& group )
{
    left->writeConfig( config, group );
    if ( right )
        right->writeConfig( config, group );
}

TDEActionCollection * KCombiView::actionCollection() const
{
    return focusView( right )->actionCollection();
}

void KCombiView::setAcceptDrops(bool b)
{
    left->setAcceptDrops(b);
    if (right)
       right->widget()->setAcceptDrops(b);
    TQSplitter::setAcceptDrops(b);
}

void KCombiView::setDropOptions_impl(int options)
{
    KFileView::setDropOptions_impl(options);
    left->setDropOptions(options);
    if (right)
       right->setDropOptions(options);
}

void KCombiView::virtual_hook( int id, void* data )
{
    switch(id) {
      case VIRTUAL_SET_DROP_OPTIONS:
         setDropOptions_impl(*(int *)data);
         break;
      default:
         KFileView::virtual_hook( id, data );
    }
}

bool KCombiView::eventFilter( TQObject *o, TQEvent *e )
{
    int type = e->type();
    
    // only the focused view may have a selection
    if ( type == TQEvent::FocusIn )
    {
        if ( TQT_BASE_OBJECT(o) == TQT_BASE_OBJECT(left) )
            right->clearSelection();
        else if ( TQT_BASE_OBJECT(o) == TQT_BASE_OBJECT(right->widget()) )
            left->clearSelection();
    }
    
    return TQSplitter::eventFilter( o, e );
}

#include "kcombiview.moc"