sidebar.cpp 24.4 KB
Newer Older
Pino Toscano's avatar
Pino Toscano committed
1
2
/***************************************************************************
 *   Copyright (C) 2007 by Pino Toscano <pino@kde.org>                     *
3
 *   Copyright (C) 2009 by Eike Hein <hein@kde.org>                        *
Pino Toscano's avatar
Pino Toscano committed
4
5
6
7
8
9
10
11
12
13
 *                                                                         *
 *   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.                                   *
 ***************************************************************************/

#include "sidebar.h"

#include <qabstractitemdelegate.h>
14
#include <qaction.h>
15
#include <qapplication.h>
Pino Toscano's avatar
Pino Toscano committed
16
17
#include <qevent.h>
#include <qfont.h>
18
#include <qfontmetrics.h>
Pino Toscano's avatar
Pino Toscano committed
19
20
21
22
23
24
25
26
#include <qlabel.h>
#include <qlayout.h>
#include <qlist.h>
#include <qlistwidget.h>
#include <qpainter.h>
#include <qscrollbar.h>
#include <qsplitter.h>
#include <qstackedwidget.h>
Vishesh Handa's avatar
Vishesh Handa committed
27
#include <qmimedata.h>
Pino Toscano's avatar
Pino Toscano committed
28

29
#include <kglobalsettings.h>
30
#include <kiconloader.h>
Laurent Montel's avatar
Laurent Montel committed
31
#include <KLocalizedString>
Laurent Montel's avatar
Laurent Montel committed
32
#include <qmenu.h>
33
#include <kcolorscheme.h>
Vishesh Handa's avatar
Vishesh Handa committed
34
#include <kurlmimedata.h>
35

Pino Toscano's avatar
Pino Toscano committed
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include "settings.h"

static const int SidebarItemType = QListWidgetItem::UserType + 1;

/* List item representing a sidebar entry. */
class SidebarItem : public QListWidgetItem
{
    public:
        SidebarItem( QWidget* w, const QIcon &icon, const QString &text )
            : QListWidgetItem( 0, SidebarItemType ),
              m_widget( w )
        {
            setFlags( Qt::ItemIsSelectable | Qt::ItemIsEnabled );
            setIcon( icon );
50
            setText( text );
Pino Toscano's avatar
Pino Toscano committed
51
52
53
54
55
56
57
58
59
60
61
62
63
64
            setToolTip( text );
        }

        QWidget* widget() const
        {
            return m_widget;
        }

    private:
        QWidget *m_widget;
};


/* A simple delegate to paint the icon of each item */
65
66
67
68
69
70
#define ITEM_MARGIN_LEFT 5
#define ITEM_MARGIN_TOP 5
#define ITEM_MARGIN_RIGHT 5
#define ITEM_MARGIN_BOTTOM 5
#define ITEM_PADDING 5

Pino Toscano's avatar
Pino Toscano committed
71
72
73
class SidebarDelegate : public QAbstractItemDelegate
{
    public:
Laurent Montel's avatar
Laurent Montel committed
74
        SidebarDelegate( QObject *parent = Q_NULLPTR );
Pino Toscano's avatar
Pino Toscano committed
75
76
        ~SidebarDelegate();

77
78
79
        void setShowText( bool show );
        bool isTextShown() const;

80
81
        void updateBrushCache();

Pino Toscano's avatar
Pino Toscano committed
82
83
84
        // from QAbstractItemDelegate
        void paint( QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index ) const;
        QSize sizeHint( const QStyleOptionViewItem &option, const QModelIndex &index ) const;
85
86
87

    private:
        bool m_showText;
88
89
90
91
        QScopedPointer<KStatefulBrush> m_windowBackground;
        QScopedPointer<KStatefulBrush> m_windowForeground;
        QScopedPointer<KStatefulBrush> m_selectionBackground;
        QScopedPointer<KStatefulBrush> m_selectionForeground;
Pino Toscano's avatar
Pino Toscano committed
92
93
94
};

SidebarDelegate::SidebarDelegate( QObject *parent )
95
96
97
    : QAbstractItemDelegate( parent ), m_showText( true ),
    m_windowBackground( 0 ), m_windowForeground( 0 ),
    m_selectionBackground( 0 ), m_selectionForeground( 0 )
Pino Toscano's avatar
Pino Toscano committed
98
{
99
    updateBrushCache();
Pino Toscano's avatar
Pino Toscano committed
100
101
102
103
104
105
}

SidebarDelegate::~SidebarDelegate()
{
}

106
107
108
109
110
111
112
113
114
115
void SidebarDelegate::setShowText( bool show )
{
    m_showText = show;
}

bool SidebarDelegate::isTextShown() const
{
    return m_showText;
}

116
117
118
119
120
121
122
123
void SidebarDelegate::updateBrushCache()
{
    m_windowBackground.reset(new KStatefulBrush(KColorScheme::Window, KColorScheme::NormalBackground));
    m_windowForeground.reset(new KStatefulBrush(KColorScheme::Window, KColorScheme::NormalText));
    m_selectionBackground.reset(new KStatefulBrush(KColorScheme::Selection, KColorScheme::NormalBackground));
    m_selectionForeground.reset(new KStatefulBrush(KColorScheme::Selection, KColorScheme::NormalText));
}

Pino Toscano's avatar
Pino Toscano committed
124
125
126
void SidebarDelegate::paint( QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index ) const
{
    QBrush backBrush;
127
    QColor foreColor;
Pino Toscano's avatar
Pino Toscano committed
128
    bool disabled = false;
129
    bool hover = false;
130

Pino Toscano's avatar
Pino Toscano committed
131
132
    if ( !( option.state & QStyle::State_Enabled ) )
    {
133
134
        backBrush = m_windowBackground->brush(QPalette::Disabled);
        foreColor = m_windowForeground->brush(QPalette::Disabled).color();
Pino Toscano's avatar
Pino Toscano committed
135
136
137
138
        disabled = true;
    }
    else if ( option.state & ( QStyle::State_HasFocus | QStyle::State_Selected ) )
    {
139
140
        backBrush = m_selectionBackground->brush(option.palette);
        foreColor = m_selectionForeground->brush(option.palette).color();
Pino Toscano's avatar
Pino Toscano committed
141
142
143
    }
    else if ( option.state & QStyle::State_MouseOver )
    {
144
145
        backBrush = m_selectionBackground->brush(option.palette).color().light( 115 );
        foreColor = m_selectionForeground->brush(option.palette).color();
146
        hover = true;
Pino Toscano's avatar
Pino Toscano committed
147
148
149
    }
    else /*if ( option.state & QStyle::State_Enabled )*/
    {
150
151
        backBrush = m_windowBackground->brush(option.palette);
        foreColor = m_windowForeground->brush(option.palette).color();
Pino Toscano's avatar
Pino Toscano committed
152
    }
153
    QStyle *style = QApplication::style();
154
    QStyleOptionViewItem opt( option );
155
156
157
158
159
160
161
162
163
164
165
166
167
    // KStyle provides an "hover highlight" effect for free;
    // but we want that for non-KStyle-based styles too
    if ( !style->inherits( "KStyle" ) && hover )
    {
        Qt::BrushStyle bs = opt.backgroundBrush.style();
        if ( bs > Qt::NoBrush && bs < Qt::TexturePattern )
            opt.backgroundBrush = opt.backgroundBrush.color().light( 115 );
        else
            opt.backgroundBrush = backBrush;
    }
    painter->save();
    style->drawPrimitive( QStyle::PE_PanelItemViewItem, &opt, painter, 0 );
    painter->restore();
Pino Toscano's avatar
Pino Toscano committed
168
169
170
171
172
    QIcon icon = index.data( Qt::DecorationRole ).value< QIcon >();
    if ( !icon.isNull() )
    {
        QPoint iconpos(
            ( option.rect.width() - option.decorationSize.width() ) / 2,
173
            ITEM_MARGIN_TOP
Pino Toscano's avatar
Pino Toscano committed
174
175
176
177
178
        );
        iconpos += option.rect.topLeft();
        QIcon::Mode iconmode = disabled ? QIcon::Disabled : QIcon::Normal;
        painter->drawPixmap( iconpos, icon.pixmap( option.decorationSize, iconmode ) );
    }
179
180
181
182
183
184
185
186
187
188
189
190

    if ( m_showText )
    {
        QString text = index.data( Qt::DisplayRole ).toString();
        QRect fontBoundaries = QFontMetrics( option.font ).boundingRect( text );
        QPoint textPos(
            ITEM_MARGIN_LEFT + ( option.rect.width() - ITEM_MARGIN_LEFT - ITEM_MARGIN_RIGHT - fontBoundaries.width() ) / 2,
            ITEM_MARGIN_TOP + option.decorationSize.height() + ITEM_PADDING
        );
        fontBoundaries.translate( -fontBoundaries.topLeft() );
        fontBoundaries.translate( textPos );
        fontBoundaries.translate( option.rect.topLeft() );
191
        painter->setPen( foreColor );
192
193
        painter->drawText( fontBoundaries, Qt::AlignCenter, text );
    }
Pino Toscano's avatar
Pino Toscano committed
194
195
196
197
}

QSize SidebarDelegate::sizeHint( const QStyleOptionViewItem &option, const QModelIndex &index ) const
{
198
199
200
201
202
203
204
205
    QSize baseSize( option.decorationSize.width(), option.decorationSize.height() );
    if ( m_showText )
    {
        QRect fontBoundaries = QFontMetrics( option.font ).boundingRect( index.data( Qt::DisplayRole ).toString() );
        baseSize.setWidth( qMax( fontBoundaries.width(), baseSize.width() ) );
        baseSize.setHeight( baseSize.height() + fontBoundaries.height() + ITEM_PADDING );
    }
    return baseSize + QSize( ITEM_MARGIN_LEFT + ITEM_MARGIN_RIGHT, ITEM_MARGIN_TOP + ITEM_MARGIN_BOTTOM );
Pino Toscano's avatar
Pino Toscano committed
206
207
208
209
210
211
212
}


/* A custom list widget that ignores the events for disabled items */
class SidebarListWidget : public QListWidget
{
    public:
Laurent Montel's avatar
Laurent Montel committed
213
        SidebarListWidget( QWidget *parent = Q_NULLPTR );
Pino Toscano's avatar
Pino Toscano committed
214
215
216
217
218
219
220
221
222
223
        ~SidebarListWidget();

    protected:
        // from QListWidget
        void mouseDoubleClickEvent( QMouseEvent *event );
        void mouseMoveEvent( QMouseEvent *event );
        void mousePressEvent( QMouseEvent *event );
        void mouseReleaseEvent( QMouseEvent *event );

        QModelIndex moveCursor( QAbstractItemView::CursorAction cursorAction, Qt::KeyboardModifiers modifiers );
224
225
226
227
228
229
230
231
232
233
234
235
236

    private:
        // These two are used to keep track of the row an initial mousePress-
        // Event() occurs on and the row the cursor moves over while the left
        // mouse button is pressed, respectively, as well as for event compre-
        // ssion, to avoid calling SideBar::itemClicked() multiple times for
        // the same item in a row on mouseMoveEvent()'s. This code is written
        // under the assumption that the number and positions of items in the
        // list won't change while the user interacts with it using the mouse.
        // Future work here must see to that this assumption continues to hold
        // up, or achieve calling SideBar::itemClicked() differently.
        int mousePressedRow;
        int rowUnderMouse;
Pino Toscano's avatar
Pino Toscano committed
237
238
239
240
241
};

SidebarListWidget::SidebarListWidget( QWidget *parent )
    : QListWidget( parent )
{
242
243
    mousePressedRow = -1;
    rowUnderMouse = -1;
Pino Toscano's avatar
Pino Toscano committed
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
}

SidebarListWidget::~SidebarListWidget()
{
}

void SidebarListWidget::mouseDoubleClickEvent( QMouseEvent *event )
{
    QModelIndex index = indexAt( event->pos() );
    if ( index.isValid() && !( index.flags() & Qt::ItemIsSelectable ) )
        return;

    QListWidget::mouseDoubleClickEvent( event );
}

void SidebarListWidget::mouseMoveEvent( QMouseEvent *event )
{
    QModelIndex index = indexAt( event->pos() );
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280

    if ( index.isValid() )
    {
        if ( index.flags() & Qt::ItemIsSelectable )
        {
            if ( event->buttons() & Qt::LeftButton
                 && index.row() != mousePressedRow
                 && index.row() != rowUnderMouse )
            {
                mousePressedRow = -1;
                rowUnderMouse = index.row();

                QMetaObject::invokeMethod(parent(), "itemClicked", Qt::DirectConnection,
                    Q_ARG(QListWidgetItem*, item(index.row())));
            }
        }
        else
            return;
    }
Pino Toscano's avatar
Pino Toscano committed
281
282
283
284
285
286
287

    QListWidget::mouseMoveEvent( event );
}

void SidebarListWidget::mousePressEvent( QMouseEvent *event )
{
    QModelIndex index = indexAt( event->pos() );
288
289
290
291
292
293
294
295
296
297
298

    if ( index.isValid() )
    {
        if ( index.flags() & Qt::ItemIsSelectable )
        {
            if ( event->buttons() & Qt::LeftButton )
                mousePressedRow = index.row();
        }
        else
            return;
    }
Pino Toscano's avatar
Pino Toscano committed
299
300
301
302
303
304
305

    QListWidget::mousePressEvent( event );
}

void SidebarListWidget::mouseReleaseEvent( QMouseEvent *event )
{
    QModelIndex index = indexAt( event->pos() );
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328

    if ( index.isValid() )
    {
        if ( index.flags() & Qt::ItemIsSelectable )
        {
            if ( event->button() == Qt::LeftButton
                 && index.row() != rowUnderMouse )
            {
                QMetaObject::invokeMethod(parent(), "itemClicked", Qt::DirectConnection,
                    Q_ARG(QListWidgetItem*, item(index.row())));
            }
        }
        else
        {
            mousePressedRow = -1;
            rowUnderMouse = -1;

            return;
        }
    }

    mousePressedRow = -1;
    rowUnderMouse = -1;
Pino Toscano's avatar
Pino Toscano committed
329
330
331
332
333
334

    QListWidget::mouseReleaseEvent( event );
}

QModelIndex SidebarListWidget::moveCursor( QAbstractItemView::CursorAction cursorAction, Qt::KeyboardModifiers modifiers )
{
335
    Q_UNUSED( modifiers )
Pino Toscano's avatar
Pino Toscano committed
336
    QModelIndex oldindex = currentIndex();
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
372
373
374
375
376
377
378
379
380
381
382
    QModelIndex newindex = oldindex;
    switch ( cursorAction )
    {
        case MoveUp:
        case MovePrevious:
        {
            int row = oldindex.row() - 1;
            while ( row > -1 && !( model()->index( row, 0 ).flags() & Qt::ItemIsSelectable ) ) --row;
            if ( row > -1 )
                newindex = model()->index( row, 0 );
            break;
        }
        case MoveDown:
        case MoveNext:
        {
            int row = oldindex.row() + 1;
            int max = model()->rowCount();
            while ( row < max && !( model()->index( row, 0 ).flags() & Qt::ItemIsSelectable ) ) ++row;
            if ( row < max )
                newindex = model()->index( row, 0 );
            break;
        }
        case MoveHome:
        case MovePageUp:
        {
            int row = 0;
            while ( row < oldindex.row() && !( model()->index( row, 0 ).flags() & Qt::ItemIsSelectable ) ) ++row;
            if ( row < oldindex.row() )
                newindex = model()->index( row, 0 );
            break;
        }
        case MoveEnd:
        case MovePageDown:
        {
            int row = model()->rowCount() - 1;
            while ( row > oldindex.row() && !( model()->index( row, 0 ).flags() & Qt::ItemIsSelectable ) ) --row;
            if ( row > oldindex.row() )
                newindex = model()->index( row, 0 );
            break;
        }
        // no navigation possible for these
        case MoveLeft:
        case MoveRight:
            break;
    }

Pino Toscano's avatar
Pino Toscano committed
383
384
385
386
387
388
389
390
391
392
393
394
395
396
    // dirty hack to change item when the key cursor changes item
    if ( oldindex != newindex )
    {
        emit itemClicked( itemFromIndex( newindex ) );
    }
    return newindex;
}


/* Private storage. */
class Sidebar::Private
{
public:
    Private()
397
398
        : sideWidget( 0 ), bottomWidget( 0 ), splitterSizesSet( false ),
          itemsHeight( 0 )
Pino Toscano's avatar
Pino Toscano committed
399
400
401
    {
    }

402
403
404
405
406
407
408
409
    int indexOf(QWidget *w) const
    {
        for (int i = 0; i < pages.count(); ++i) {
            if (pages[i]->widget() == w) return i;
        }
        return -1;
    }

410
    void adjustListSize( bool recalc, bool expand = true );
411
412

    SidebarListWidget *list;
Pino Toscano's avatar
Pino Toscano committed
413
414
415
416
417
418
419
420
421
    QSplitter *splitter;
    QStackedWidget *stack;
    QWidget *sideContainer;
    QLabel *sideTitle;
    QVBoxLayout *vlay;
    QWidget *sideWidget;
    QWidget *bottomWidget;
    QList< SidebarItem* > pages;
    bool splitterSizesSet;
422
423
    int itemsHeight;
    SidebarDelegate *sideDelegate;
Pino Toscano's avatar
Pino Toscano committed
424
425
};

426
void Sidebar::Private::adjustListSize( bool recalc, bool expand )
427
428
429
430
431
{
    QRect bottomElemRect(
        QPoint( 0, 0 ),
        list->sizeHintForIndex( list->model()->index( list->count() - 1, 0 ) )
    );
432
433
434
435
436
437
438
439
440
441
442
    if ( recalc )
    {
        int w = 0;
        for ( int i = 0; i < list->count(); ++i )
        {
            QSize s = list->sizeHintForIndex( list->model()->index( i, 0 ) );
            if ( s.width() > w )
                w = s.width();
        }
        bottomElemRect.setWidth( w );
    }
443
444
445
446
447
448
449
450
451
452
    bottomElemRect.translate( 0, bottomElemRect.height() * ( list->count() - 1 ) );
    itemsHeight = bottomElemRect.height() * list->count();
    list->setMinimumHeight( itemsHeight + list->frameWidth() * 2 );
    int curWidth = list->minimumWidth();
    int newWidth = expand
                   ? qMax( bottomElemRect.width() + list->frameWidth() * 2, curWidth )
                   : qMin( bottomElemRect.width() + list->frameWidth() * 2, curWidth );
    list->setFixedWidth( newWidth );
}

Pino Toscano's avatar
Pino Toscano committed
453
454
455
456
457
458

Sidebar::Sidebar( QWidget *parent )
    : QWidget( parent ), d( new Private )
{
    QHBoxLayout *mainlay = new QHBoxLayout( this );
    mainlay->setMargin( 0 );
459
    mainlay->setSpacing( 0 );
Pino Toscano's avatar
Pino Toscano committed
460

461
    setAutoFillBackground( true );
462
    setAcceptDrops( true );
463

Pino Toscano's avatar
Pino Toscano committed
464
465
466
467
    d->list = new SidebarListWidget( this );
    mainlay->addWidget( d->list );
    d->list->setMouseTracking( true );
    d->list->viewport()->setAttribute( Qt::WA_Hover );
468
    d->sideDelegate = new SidebarDelegate( d->list );
469
    d->sideDelegate->setShowText( Okular::Settings::sidebarShowText() );
470
    d->list->setItemDelegate( d->sideDelegate );
Pino Toscano's avatar
Pino Toscano committed
471
472
    d->list->setUniformItemSizes( true );
    d->list->setSelectionMode( QAbstractItemView::SingleSelection );
473
474
    int iconsize = Okular::Settings::sidebarIconSize();
    d->list->setIconSize( QSize( iconsize, iconsize ) );
Pino Toscano's avatar
Pino Toscano committed
475
    d->list->setHorizontalScrollBarPolicy( Qt::ScrollBarAlwaysOff );
476
477
    d->list->setVerticalScrollBarPolicy( Qt::ScrollBarAlwaysOff );
    d->list->setContextMenuPolicy( Qt::CustomContextMenu );
478
    d->list->viewport()->setAutoFillBackground( false );
Pino Toscano's avatar
Pino Toscano committed
479
480
481
482
483
484
485
486

    d->splitter = new QSplitter( this );
    mainlay->addWidget( d->splitter );
    d->splitter->setOpaqueResize( true );
    d->splitter->setChildrenCollapsible( false );

    d->sideContainer = new QWidget( d->splitter );
    d->sideContainer->setMinimumWidth( 90 );
487
    d->sideContainer->setMaximumWidth( 600 );
Pino Toscano's avatar
Pino Toscano committed
488
489
490
491
492
493
494
495
496
497
498
499
500
501
    d->vlay = new QVBoxLayout( d->sideContainer );
    d->vlay->setMargin( 0 );

    d->sideTitle = new QLabel( d->sideContainer );
    d->vlay->addWidget( d->sideTitle );
    QFont tf = d->sideTitle->font();
    tf.setBold( true );
    d->sideTitle->setFont( tf );
    d->sideTitle->setMargin( 3 );
    d->sideTitle->setIndent( 3 );

    d->stack = new QStackedWidget( d->sideContainer );
    d->vlay->addWidget( d->stack );

Laurent Montel's avatar
Laurent Montel committed
502
503
    connect(d->list, &SidebarListWidget::customContextMenuRequested, this, &Sidebar::listContextMenu);
    connect(d->splitter, &QSplitter::splitterMoved, this, &Sidebar::splitterMoved);
504

Laurent Montel's avatar
Laurent Montel committed
505
    connect(KGlobalSettings::self(), &KGlobalSettings::appearanceChanged, this, &Sidebar::appearanceChanged);
506

507
    setCollapsed( true );
508
    setFocusProxy( d->list );
Pino Toscano's avatar
Pino Toscano committed
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
}

Sidebar::~Sidebar()
{
    delete d;
}

int Sidebar::addItem( QWidget *widget, const QIcon &icon, const QString &text )
{
    if ( !widget )
        return -1;

    SidebarItem *newitem = new SidebarItem( widget, icon, text );
    d->list->addItem( newitem );
    d->pages.append( newitem );
    widget->setParent( d->stack );
    d->stack->addWidget( widget );
    // updating the minimum height of the icon view, so all are visible with no scrolling
527
    d->adjustListSize( false, true );
Pino Toscano's avatar
Pino Toscano committed
528
529
530
531
532
533
534
    return d->pages.count() - 1;
}

void Sidebar::setMainWidget( QWidget *widget )
{
    delete d->sideWidget;
    d->sideWidget = widget;
535
    if ( d->sideWidget )
Pino Toscano's avatar
Pino Toscano committed
536
537
538
    {
        // setting the splitter as parent for the widget automatically plugs it
        // into the splitter, neat!
539
        d->sideWidget->setParent( d->splitter );
Pino Toscano's avatar
Pino Toscano committed
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559

        if ( !d->splitterSizesSet )
        {
            QList<int> splitterSizes = Okular::Settings::splitterSizes();
            if ( !splitterSizes.count() )
            {
                // the first time use 1/10 for the panel and 9/10 for the pageView
                splitterSizes.push_back( 50 );
                splitterSizes.push_back( 500 );
            }
            d->splitter->setSizes( splitterSizes );
            d->splitterSizesSet = true;
        }
    }
}

void Sidebar::setBottomWidget( QWidget *widget )
{
    delete d->bottomWidget;
    d->bottomWidget = widget;
560
    if ( d->bottomWidget )
Pino Toscano's avatar
Pino Toscano committed
561
    {
562
        d->bottomWidget->setParent( this );
Pino Toscano's avatar
Pino Toscano committed
563
564
565
566
        d->vlay->addWidget( d->bottomWidget );
    }
}

567
568
569
570
571
572
573
void Sidebar::setItemEnabled( QWidget *widget, bool enabled )
{
    const int index = d->indexOf( widget );
    setIndexEnabled( index, enabled );
}

void Sidebar::setIndexEnabled( int index, bool enabled )
Pino Toscano's avatar
Pino Toscano committed
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
{
    if ( index < 0 || index >= d->pages.count() )
        return;

    Qt::ItemFlags f = d->pages.at( index )->flags();
    if ( enabled )
    {
        f |= Qt::ItemIsEnabled;
        f |= Qt::ItemIsSelectable;
    }
    else
    {
        f &= ~Qt::ItemIsEnabled;
        f &= ~Qt::ItemIsSelectable;
    }
    d->pages.at( index )->setFlags( f );

591
    if ( !enabled && index == d->list->currentRow() && isSidebarVisible() )
Pino Toscano's avatar
Pino Toscano committed
592
593
594
595
596
597
598
599
600
        // find an enabled item, and select that one
        for ( int i = 0; i < d->pages.count(); ++i )
            if ( d->pages.at(i)->flags() & Qt::ItemIsEnabled )
            {
                setCurrentIndex( i );
                break;
            }
}

601
bool Sidebar::isItemEnabled( QWidget *widget ) const
Pino Toscano's avatar
Pino Toscano committed
602
{
603
604
605
606
607
608
609
    const int index = d->indexOf( widget );
    return isIndexEnabled( index );
}

bool Sidebar::isIndexEnabled( int index ) const
{
    if ( index < 0 )
Pino Toscano's avatar
Pino Toscano committed
610
611
612
613
614
615
        return false;

    Qt::ItemFlags f = d->pages.at( index )->flags();
    return ( f & Qt::ItemIsEnabled ) == Qt::ItemIsEnabled;
}

616
void Sidebar::setCurrentItem( QWidget *widget, SetCurrentItemBehaviour b )
Pino Toscano's avatar
Pino Toscano committed
617
{
618
619
620
621
622
623
624
    const int index = d->indexOf( widget );
    setCurrentIndex( index, b );
}

void Sidebar::setCurrentIndex( int index, SetCurrentItemBehaviour b )
{
    if ( index < 0 || !isIndexEnabled( index ) )
Pino Toscano's avatar
Pino Toscano committed
625
626
        return;

627
    itemClicked( d->pages.at( index ), b );
628
629
630
    QModelIndex modelindex = d->list->model()->index( index, 0 );
    d->list->setCurrentIndex( modelindex );
    d->list->selectionModel()->select( modelindex, QItemSelectionModel::ClearAndSelect );
Pino Toscano's avatar
Pino Toscano committed
631
632
}

633
QWidget *Sidebar::currentItem() const
Pino Toscano's avatar
Pino Toscano committed
634
{
635
636
637
638
639
    const int row = d->list->currentRow();
    if (row < 0 || row >= d->pages.count())
        return 0;

    return d->pages[row]->widget();
Pino Toscano's avatar
Pino Toscano committed
640
641
642
643
}

void Sidebar::setSidebarVisibility( bool visible )
{
644
    if ( visible != d->list->isHidden() )
645
646
        return;

647
    static bool wasCollapsed = isCollapsed();
648

649
    d->list->setHidden( !visible );
650
651
    if ( visible )
    {
652
653
        setCollapsed( wasCollapsed );
        wasCollapsed = false;
654
655
656
    }
    else
    {
657
658
        wasCollapsed = isCollapsed();
        setCollapsed( true );
659
    }
Pino Toscano's avatar
Pino Toscano committed
660
661
}

662
663
bool Sidebar::isSidebarVisible() const
{
664
    return !d->list->isHidden();
665
666
}

667
668
669
670
671
672
673
674
675
676
void Sidebar::setCollapsed( bool collapsed )
{
    d->sideContainer->setHidden( collapsed );
}

bool Sidebar::isCollapsed() const
{
    return d->sideContainer->isHidden();
}

677
678
679
680
681
682
683
684
685
void Sidebar::moveSplitter(int sideWidgetSize)
{
    QList<int> splitterSizeList = d->splitter->sizes();
    const int total = splitterSizeList.at( 0 ) + splitterSizeList.at( 1 );
    splitterSizeList.replace( 0, total - sideWidgetSize );
    splitterSizeList.replace( 1, sideWidgetSize );
    d->splitter->setSizes( splitterSizeList );
}

686
void Sidebar::setItemVisible( QWidget *widget, bool visible )
687
{
688
689
690
691
    const int index = d->indexOf( widget );
    if ( index < 0 )
        return;

692
    d->list->setRowHidden( index, !visible );
693
    setIndexEnabled( index, visible );
694
695
}

Pino Toscano's avatar
Pino Toscano committed
696
void Sidebar::itemClicked( QListWidgetItem *item )
697
698
699
700
{
    itemClicked( item, UncollapseIfCollapsed );
}

701
void Sidebar::itemClicked( QListWidgetItem *item, SetCurrentItemBehaviour b )
Pino Toscano's avatar
Pino Toscano committed
702
703
704
705
706
707
708
709
{
    if ( !item )
        return;

    SidebarItem* sbItem = dynamic_cast< SidebarItem* >( item );
    if ( !sbItem )
        return;

710
    if ( sbItem->widget() == d->stack->currentWidget() )
Pino Toscano's avatar
Pino Toscano committed
711
    {
712
        if ( !isCollapsed() )
713
714
        {
            d->list->selectionModel()->clear();
715
            setCollapsed( true );
716
717
718
        }
        else
        {
719
720
721
722
723
            if ( b == UncollapseIfCollapsed )
            {
                setCollapsed( false );
                d->list->show();
            }
724
        }
Pino Toscano's avatar
Pino Toscano committed
725
726
727
    }
    else
    {
728
        if ( isCollapsed() && b == UncollapseIfCollapsed )
729
        {
730
            setCollapsed( false );
731
732
            d->list->show();
        }
Pino Toscano's avatar
Pino Toscano committed
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
        d->stack->setCurrentWidget( sbItem->widget() );
        d->sideTitle->setText( sbItem->toolTip() );
    }
}

void Sidebar::splitterMoved( int /*pos*/, int index )
{
    // if the side panel has been resized, save splitter sizes
    if ( index == 1 )
        saveSplitterSize();
}

void Sidebar::saveSplitterSize() const
{
    Okular::Settings::setSplitterSizes( d->splitter->sizes() );
748
    Okular::Settings::self()->save();
Pino Toscano's avatar
Pino Toscano committed
749
750
}

751
752
void Sidebar::listContextMenu( const QPoint &pos )
{
Vishesh Handa's avatar
Vishesh Handa committed
753
754
    QMenu menu( this );
    menu.setTitle( i18n( "Okular" ) );
755
756
757
    QAction *showTextAct = menu.addAction( i18n( "Show Text" ) );
    showTextAct->setCheckable( true );
    showTextAct->setChecked( d->sideDelegate->isTextShown() );
Laurent Montel's avatar
Laurent Montel committed
758
    connect(showTextAct, &QAction::toggled, this, &Sidebar::showTextToggled);
759
760
761
    menu.addSeparator();
    QActionGroup *sizeGroup = new QActionGroup( &menu );
    int curSize = d->list->iconSize().width();
762
#define ADD_SIZE_ACTION( text, _itssize ) \
763
{ \
764
    const int itssize = static_cast< int >( _itssize ); \
765
766
767
768
769
770
    QAction *sizeAct = menu.addAction( text ); \
    sizeAct->setCheckable( true ); \
    sizeAct->setData( qVariantFromValue( itssize ) ); \
    sizeAct->setChecked( itssize == curSize ); \
    sizeGroup->addAction( sizeAct ); \
}
771
772
773
    ADD_SIZE_ACTION( i18n( "Small Icons" ), KIconLoader::SizeSmallMedium )
    ADD_SIZE_ACTION( i18n( "Normal Icons" ), KIconLoader::SizeMedium )
    ADD_SIZE_ACTION( i18n( "Large Icons" ), KIconLoader::SizeLarge )
774
#undef ADD_SIZE_ACTION
Laurent Montel's avatar
Laurent Montel committed
775
    connect(sizeGroup, &QActionGroup::triggered, this, &Sidebar::iconSizeChanged);
776
777
778
779
780
781
    menu.exec( mapToGlobal( pos ) );
}

void Sidebar::showTextToggled( bool on )
{
    d->sideDelegate->setShowText( on );
782
    d->adjustListSize( true, on );
783
784
    d->list->reset();
    d->list->update();
785
    Okular::Settings::setSidebarShowText( on );
786
    Okular::Settings::self()->save();
787
}
Pino Toscano's avatar
Pino Toscano committed
788

789
790
791
792
793
void Sidebar::iconSizeChanged( QAction *action )
{
    int size = action->data().toInt();
    int oldSize = d->list->iconSize().width();
    d->list->setIconSize( QSize( size, size ) );
794
    d->adjustListSize( true, size > oldSize );
795
796
797
    d->list->reset();
    d->list->update();
    Okular::Settings::setSidebarIconSize( size );
798
    Okular::Settings::self()->save();
799
800
}

801
802
803
804
805
void Sidebar::appearanceChanged()
{
    d->sideDelegate->updateBrushCache();
}

806
807
void Sidebar::dragEnterEvent( QDragEnterEvent* event )
{
Vishesh Handa's avatar
Vishesh Handa committed
808
    event->setAccepted( event->mimeData()->hasUrls() );
809
810
811
812
}

void Sidebar::dropEvent( QDropEvent* event )
{
Vishesh Handa's avatar
Vishesh Handa committed
813
    const QList<QUrl> list = KUrlMimeData::urlsFromMimeData( event->mimeData() );
Albert Astals Cid's avatar
Albert Astals Cid committed
814
    emit urlsDropped( list );
815
816
}

817
#include "moc_sidebar.cpp"