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 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768
|
/***************************************************************************
* Copyright (C) 2007 by Pino Toscano <pino@kde.org> *
* Copyright (C) 2009 by Eike Hein <hein@kde.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. *
***************************************************************************/
#include "sidebar.h"
#include <qabstractitemdelegate.h>
#include <qaction.h>
#include <qapplication.h>
#include <qevent.h>
#include <qfont.h>
#include <qfontmetrics.h>
#include <qlabel.h>
#include <qlayout.h>
#include <qlist.h>
#include <qlistwidget.h>
#include <qpainter.h>
#include <qscrollbar.h>
#include <qsplitter.h>
#include <qstackedwidget.h>
#include <kglobalsettings.h>
#include <kiconloader.h>
#include <klocale.h>
#include <kmenu.h>
#include <kcolorscheme.h>
#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 );
setText( text );
setToolTip( text );
}
QWidget* widget() const
{
return m_widget;
}
private:
QWidget *m_widget;
};
/* A simple delegate to paint the icon of each item */
#define ITEM_MARGIN_LEFT 5
#define ITEM_MARGIN_TOP 5
#define ITEM_MARGIN_RIGHT 5
#define ITEM_MARGIN_BOTTOM 5
#define ITEM_PADDING 5
class SidebarDelegate : public QAbstractItemDelegate
{
public:
SidebarDelegate( QObject *parent = 0 );
~SidebarDelegate();
void setShowText( bool show );
bool isTextShown() const;
void updateBrushCache();
// from QAbstractItemDelegate
void paint( QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index ) const;
QSize sizeHint( const QStyleOptionViewItem &option, const QModelIndex &index ) const;
private:
bool m_showText;
QScopedPointer<KStatefulBrush> m_windowBackground;
QScopedPointer<KStatefulBrush> m_windowForeground;
QScopedPointer<KStatefulBrush> m_selectionBackground;
QScopedPointer<KStatefulBrush> m_selectionForeground;
};
SidebarDelegate::SidebarDelegate( QObject *parent )
: QAbstractItemDelegate( parent ), m_showText( true ),
m_windowBackground( 0 ), m_windowForeground( 0 ),
m_selectionBackground( 0 ), m_selectionForeground( 0 )
{
updateBrushCache();
}
SidebarDelegate::~SidebarDelegate()
{
}
void SidebarDelegate::setShowText( bool show )
{
m_showText = show;
}
bool SidebarDelegate::isTextShown() const
{
return m_showText;
}
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));
}
void SidebarDelegate::paint( QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index ) const
{
QBrush backBrush;
QColor foreColor;
bool disabled = false;
bool hover = false;
if ( !( option.state & QStyle::State_Enabled ) )
{
backBrush = m_windowBackground->brush(QPalette::Disabled);
foreColor = m_windowForeground->brush(QPalette::Disabled).color();
disabled = true;
}
else if ( option.state & ( QStyle::State_HasFocus | QStyle::State_Selected ) )
{
backBrush = m_selectionBackground->brush(option.palette);
foreColor = m_selectionForeground->brush(option.palette).color();
}
else if ( option.state & QStyle::State_MouseOver )
{
backBrush = m_selectionBackground->brush(option.palette).color().light( 115 );
foreColor = m_selectionForeground->brush(option.palette).color();
hover = true;
}
else /*if ( option.state & QStyle::State_Enabled )*/
{
backBrush = m_windowBackground->brush(option.palette);
foreColor = m_windowForeground->brush(option.palette).color();
}
QStyle *style = QApplication::style();
QStyleOptionViewItemV4 opt( option );
// 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();
QIcon icon = index.data( Qt::DecorationRole ).value< QIcon >();
if ( !icon.isNull() )
{
QPoint iconpos(
( option.rect.width() - option.decorationSize.width() ) / 2,
ITEM_MARGIN_TOP
);
iconpos += option.rect.topLeft();
QIcon::Mode iconmode = disabled ? QIcon::Disabled : QIcon::Normal;
painter->drawPixmap( iconpos, icon.pixmap( option.decorationSize, iconmode ) );
}
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() );
painter->setPen( foreColor );
painter->drawText( fontBoundaries, Qt::AlignCenter, text );
}
}
QSize SidebarDelegate::sizeHint( const QStyleOptionViewItem &option, const QModelIndex &index ) const
{
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 );
}
/* A custom list widget that ignores the events for disabled items */
class SidebarListWidget : public QListWidget
{
public:
SidebarListWidget( QWidget *parent = 0 );
~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 );
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;
};
SidebarListWidget::SidebarListWidget( QWidget *parent )
: QListWidget( parent )
{
mousePressedRow = -1;
rowUnderMouse = -1;
}
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() );
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;
}
QListWidget::mouseMoveEvent( event );
}
void SidebarListWidget::mousePressEvent( QMouseEvent *event )
{
QModelIndex index = indexAt( event->pos() );
if ( index.isValid() )
{
if ( index.flags() & Qt::ItemIsSelectable )
{
if ( event->buttons() & Qt::LeftButton )
mousePressedRow = index.row();
}
else
return;
}
QListWidget::mousePressEvent( event );
}
void SidebarListWidget::mouseReleaseEvent( QMouseEvent *event )
{
QModelIndex index = indexAt( event->pos() );
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;
QListWidget::mouseReleaseEvent( event );
}
QModelIndex SidebarListWidget::moveCursor( QAbstractItemView::CursorAction cursorAction, Qt::KeyboardModifiers modifiers )
{
Q_UNUSED( modifiers )
QModelIndex oldindex = currentIndex();
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;
}
// 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()
: sideWidget( 0 ), bottomWidget( 0 ), splitterSizesSet( false ),
itemsHeight( 0 )
{
}
void adjustListSize( bool recalc, bool expand = true );
SidebarListWidget *list;
QSplitter *splitter;
QStackedWidget *stack;
QWidget *sideContainer;
QLabel *sideTitle;
QVBoxLayout *vlay;
QWidget *sideWidget;
QWidget *bottomWidget;
QList< SidebarItem* > pages;
bool splitterSizesSet;
int itemsHeight;
SidebarDelegate *sideDelegate;
};
void Sidebar::Private::adjustListSize( bool recalc, bool expand )
{
QRect bottomElemRect(
QPoint( 0, 0 ),
list->sizeHintForIndex( list->model()->index( list->count() - 1, 0 ) )
);
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 );
}
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 );
}
Sidebar::Sidebar( QWidget *parent )
: QWidget( parent ), d( new Private )
{
QHBoxLayout *mainlay = new QHBoxLayout( this );
mainlay->setMargin( 0 );
mainlay->setSpacing( 0 );
setAutoFillBackground( true );
setAcceptDrops( true );
d->list = new SidebarListWidget( this );
mainlay->addWidget( d->list );
d->list->setMouseTracking( true );
d->list->viewport()->setAttribute( Qt::WA_Hover );
d->sideDelegate = new SidebarDelegate( d->list );
d->sideDelegate->setShowText( Okular::Settings::sidebarShowText() );
d->list->setItemDelegate( d->sideDelegate );
d->list->setUniformItemSizes( true );
d->list->setSelectionMode( QAbstractItemView::SingleSelection );
int iconsize = Okular::Settings::sidebarIconSize();
d->list->setIconSize( QSize( iconsize, iconsize ) );
d->list->setHorizontalScrollBarPolicy( Qt::ScrollBarAlwaysOff );
d->list->setVerticalScrollBarPolicy( Qt::ScrollBarAlwaysOff );
d->list->setContextMenuPolicy( Qt::CustomContextMenu );
d->list->viewport()->setAutoFillBackground( false );
d->list->setFont( KGlobalSettings::toolBarFont() );
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 );
d->sideContainer->setMaximumWidth( 600 );
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 );
connect( d->list, SIGNAL(customContextMenuRequested(QPoint)),
this, SLOT(listContextMenu(QPoint)) );
connect( d->splitter, SIGNAL(splitterMoved(int,int)), this, SLOT(splitterMoved(int,int)) );
connect( KGlobalSettings::self(), SIGNAL(appearanceChanged()), this, SLOT(appearanceChanged()) );
setCollapsed( true );
setFocusProxy( d->list );
}
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
d->adjustListSize( false, true );
return d->pages.count() - 1;
}
void Sidebar::setMainWidget( QWidget *widget )
{
delete d->sideWidget;
d->sideWidget = widget;
if ( d->sideWidget )
{
// setting the splitter as parent for the widget automatically plugs it
// into the splitter, neat!
d->sideWidget->setParent( d->splitter );
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;
if ( d->bottomWidget )
{
d->bottomWidget->setParent( this );
d->vlay->addWidget( d->bottomWidget );
}
}
void Sidebar::setItemEnabled( int index, bool enabled )
{
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 );
if ( !enabled && index == currentIndex() && isSidebarVisible() )
// 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;
}
}
bool Sidebar::isItemEnabled( int index ) const
{
if ( index < 0 || index >= d->pages.count() )
return false;
Qt::ItemFlags f = d->pages.at( index )->flags();
return ( f & Qt::ItemIsEnabled ) == Qt::ItemIsEnabled;
}
void Sidebar::setCurrentIndex( int index, SetCurrentIndexBehaviour b )
{
if ( index < 0 || index >= d->pages.count() || !isItemEnabled( index ) )
return;
itemClicked( d->pages.at( index ), b );
QModelIndex modelindex = d->list->model()->index( index, 0 );
d->list->setCurrentIndex( modelindex );
d->list->selectionModel()->select( modelindex, QItemSelectionModel::ClearAndSelect );
}
int Sidebar::currentIndex() const
{
return d->list->currentRow();
}
void Sidebar::setSidebarVisibility( bool visible )
{
if ( visible != d->list->isHidden() )
return;
static bool wasCollapsed = isCollapsed();
d->list->setHidden( !visible );
if ( visible )
{
setCollapsed( wasCollapsed );
wasCollapsed = false;
}
else
{
wasCollapsed = isCollapsed();
setCollapsed( true );
}
}
bool Sidebar::isSidebarVisible() const
{
return !d->list->isHidden();
}
void Sidebar::setCollapsed( bool collapsed )
{
d->sideContainer->setHidden( collapsed );
}
bool Sidebar::isCollapsed() const
{
return d->sideContainer->isHidden();
}
void Sidebar::itemClicked( QListWidgetItem *item )
{
itemClicked( item, UncollapseIfCollapsed );
}
void Sidebar::itemClicked( QListWidgetItem *item, SetCurrentIndexBehaviour b )
{
if ( !item )
return;
SidebarItem* sbItem = dynamic_cast< SidebarItem* >( item );
if ( !sbItem )
return;
if ( sbItem->widget() == d->stack->currentWidget() )
{
if ( !isCollapsed() )
{
d->list->selectionModel()->clear();
setCollapsed( true );
}
else
{
if ( b == UncollapseIfCollapsed )
{
setCollapsed( false );
d->list->show();
}
}
}
else
{
if ( isCollapsed() && b == UncollapseIfCollapsed )
{
setCollapsed( false );
d->list->show();
}
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() );
Okular::Settings::self()->writeConfig();
}
void Sidebar::listContextMenu( const QPoint &pos )
{
KMenu menu( this );
menu.addTitle( i18n( "Okular" ) );
QAction *showTextAct = menu.addAction( i18n( "Show Text" ) );
showTextAct->setCheckable( true );
showTextAct->setChecked( d->sideDelegate->isTextShown() );
connect( showTextAct, SIGNAL(toggled(bool)), this, SLOT(showTextToggled(bool)) );
menu.addSeparator();
QActionGroup *sizeGroup = new QActionGroup( &menu );
int curSize = d->list->iconSize().width();
#define ADD_SIZE_ACTION( text, _itssize ) \
{ \
const int itssize = static_cast< int >( _itssize ); \
QAction *sizeAct = menu.addAction( text ); \
sizeAct->setCheckable( true ); \
sizeAct->setData( qVariantFromValue( itssize ) ); \
sizeAct->setChecked( itssize == curSize ); \
sizeGroup->addAction( sizeAct ); \
}
ADD_SIZE_ACTION( i18n( "Small Icons" ), KIconLoader::SizeSmallMedium )
ADD_SIZE_ACTION( i18n( "Normal Icons" ), KIconLoader::SizeMedium )
ADD_SIZE_ACTION( i18n( "Large Icons" ), KIconLoader::SizeLarge )
#undef ADD_SIZE_ACTION
connect( sizeGroup, SIGNAL(triggered(QAction*)), this, SLOT(iconSizeChanged(QAction*)) );
menu.exec( mapToGlobal( pos ) );
}
void Sidebar::showTextToggled( bool on )
{
d->sideDelegate->setShowText( on );
d->adjustListSize( true, on );
d->list->reset();
d->list->update();
Okular::Settings::setSidebarShowText( on );
Okular::Settings::self()->writeConfig();
}
void Sidebar::iconSizeChanged( QAction *action )
{
int size = action->data().toInt();
int oldSize = d->list->iconSize().width();
d->list->setIconSize( QSize( size, size ) );
d->adjustListSize( true, size > oldSize );
d->list->reset();
d->list->update();
Okular::Settings::setSidebarIconSize( size );
Okular::Settings::self()->writeConfig();
}
void Sidebar::appearanceChanged()
{
d->sideDelegate->updateBrushCache();
}
void Sidebar::dragEnterEvent( QDragEnterEvent* event )
{
event->setAccepted( KUrl::List::canDecode(event->mimeData()) );
}
void Sidebar::dropEvent( QDropEvent* event )
{
const KUrl::List list = KUrl::List::fromMimeData( event->mimeData() );
emit urlsDropped( list );
}
#include "sidebar.moc"
|