Commit ce7f81fc authored by Albert Astals Cid's avatar Albert Astals Cid

Make the sidebar work on widget pointers and not ints

This way we don't need to worry if we add more widgets in the middle
parent 1cd101b4
......@@ -373,17 +373,16 @@ m_cliPresentation(false), m_cliPrint(false), m_embedMode(detectEmbedMode(parentW
// sLabel->setBuddy( m_searchWidget );
// m_searchToolBar->setStretchableWidget( m_searchWidget );
int tbIndex;
// [left toolbox: Table of Contents] | []
m_toc = new TOC( 0, m_document );
connect( m_toc, SIGNAL(hasTOC(bool)), this, SLOT(enableTOC(bool)) );
tbIndex = m_sidebar->addItem( m_toc, KIcon(QApplication::isLeftToRight() ? "format-justify-left" : "format-justify-right"), i18n("Contents") );
m_sidebar->addItem( m_toc, KIcon(QApplication::isLeftToRight() ? "format-justify-left" : "format-justify-right"), i18n("Contents") );
enableTOC( false );
// [left toolbox: Layers] | []
m_layers = new Layers( 0, m_document );
connect( m_layers, SIGNAL(hasLayers(bool)), this, SLOT(enableLayers(bool)) );
tbIndex = m_sidebar->addItem( m_layers, KIcon( "draw-freehand" ), i18n( "Layers" ) );
m_sidebar->addItem( m_layers, KIcon( "draw-freehand" ), i18n( "Layers" ) );
enableLayers( false );
// [left toolbox: Thumbnails and Bookmarks] | []
......@@ -393,18 +392,19 @@ m_cliPresentation(false), m_cliPrint(false), m_embedMode(detectEmbedMode(parentW
m_thumbnailList = new ThumbnailList( thumbsBox, m_document );
// ThumbnailController * m_tc = new ThumbnailController( thumbsBox, m_thumbnailList );
connect( m_thumbnailList, SIGNAL(rightClick(const Okular::Page*,QPoint)), this, SLOT(slotShowMenu(const Okular::Page*,QPoint)) );
tbIndex = m_sidebar->addItem( thumbsBox, KIcon( "view-preview" ), i18n("Thumbnails") );
m_sidebar->setCurrentIndex( tbIndex );
m_sidebar->addItem( thumbsBox, KIcon( "view-preview" ), i18n("Thumbnails") );
m_sidebar->setCurrentItem( thumbsBox );
// [left toolbox: Reviews] | []
m_reviewsWidget = new Reviews( 0, m_document );
m_sidebar->addItem( m_reviewsWidget, KIcon("draw-freehand"), i18n("Reviews") );
m_sidebar->setItemEnabled( 2, false );
m_sidebar->setItemEnabled( m_reviewsWidget, false );
// [left toolbox: Bookmarks] | []
m_bookmarkList = new BookmarkList( m_document, 0 );
m_sidebar->addItem( m_bookmarkList, KIcon("bookmarks"), i18n("Bookmarks") );
m_sidebar->setItemEnabled( 3, false );
m_sidebar->setItemEnabled( m_bookmarkList, false );
// widgets: [../miniBarContainer] | []
#ifdef OKULAR_ENABLE_MINIBAR
......@@ -1461,9 +1461,9 @@ bool Part::openFile()
}
// if the 'OpenTOC' flag is set, open the TOC
if ( m_document->metaData( "OpenTOC" ).toBool() && m_sidebar->isItemEnabled( 0 ) && !m_sidebar->isCollapsed() && m_sidebar->currentIndex() != 0 )
if ( m_document->metaData( "OpenTOC" ).toBool() && m_sidebar->isItemEnabled( m_toc ) && !m_sidebar->isCollapsed() && m_sidebar->currentItem() != m_toc )
{
m_sidebar->setCurrentIndex( 0, Sidebar::DoNotUncollapseIfCollapsed );
m_sidebar->setCurrentItem( m_toc, Sidebar::DoNotUncollapseIfCollapsed );
}
// if the 'StartFullScreen' flag is set, or the command line flag was
// specified, start presentation
......@@ -1743,7 +1743,7 @@ void Part::slotDoFileDirty()
m_viewportDirty = m_document->viewport();
// store the current toolbox pane
m_dirtyToolboxIndex = m_sidebar->currentIndex();
m_dirtyToolboxItem = m_sidebar->currentItem();
m_wasSidebarVisible = m_sidebar->isSidebarVisible();
m_wasSidebarCollapsed = m_sidebar->isCollapsed();
......@@ -1789,10 +1789,10 @@ void Part::slotDoFileDirty()
m_oldUrl = KUrl();
m_viewportDirty.pageNumber = -1;
m_document->setRotation( m_dirtyPageRotation );
if ( m_sidebar->currentIndex() != m_dirtyToolboxIndex && m_sidebar->isItemEnabled( m_dirtyToolboxIndex )
if ( m_sidebar->currentItem() != m_dirtyToolboxItem && m_sidebar->isItemEnabled( m_dirtyToolboxItem )
&& !m_sidebar->isCollapsed() )
{
m_sidebar->setCurrentIndex( m_dirtyToolboxIndex );
m_sidebar->setCurrentItem( m_dirtyToolboxItem );
}
if ( m_sidebar->isSidebarVisible() != m_wasSidebarVisible )
{
......@@ -1930,12 +1930,12 @@ void Part::updateBookmarksActions()
void Part::enableTOC(bool enable)
{
m_sidebar->setItemEnabled(0, enable);
m_sidebar->setItemEnabled(m_toc, enable);
// If present, show the TOC when a document is opened
if ( enable && m_sidebar->currentIndex() != 0 )
if ( enable && m_sidebar->currentItem() != m_toc )
{
m_sidebar->setCurrentIndex( 0, Sidebar::DoNotUncollapseIfCollapsed );
m_sidebar->setCurrentItem( m_toc, Sidebar::DoNotUncollapseIfCollapsed );
}
}
......@@ -1946,7 +1946,7 @@ void Part::slotRebuildBookmarkMenu()
void Part::enableLayers(bool enable)
{
m_sidebar->setItemVisible( 1, enable );
m_sidebar->setItemVisible( m_layers, enable );
}
void Part::slotShowFindBar()
......@@ -2376,7 +2376,7 @@ void Part::slotNewConfig()
m_document->reparseConfig();
// update TOC settings
if ( m_sidebar->isItemEnabled(0) )
if ( m_sidebar->isItemEnabled(m_toc) )
m_toc->reparseConfig();
// update ThumbnailList contents
......@@ -2384,7 +2384,7 @@ void Part::slotNewConfig()
m_thumbnailList->updateWidgets();
// update Reviews settings
if ( m_sidebar->isItemEnabled(2) )
if ( m_sidebar->isItemEnabled(m_reviewsWidget) )
m_reviewsWidget->reparseConfig();
setWindowTitleFromDocument ();
......@@ -2832,8 +2832,8 @@ void Part::unsetDummyMode()
if ( m_embedMode == PrintPreviewMode )
return;
m_sidebar->setItemEnabled( 2, true );
m_sidebar->setItemEnabled( 3, true );
m_sidebar->setItemEnabled( m_reviewsWidget, true );
m_sidebar->setItemEnabled( m_bookmarkList, true );
m_sidebar->setSidebarVisibility( Okular::Settings::showLeftPanel() );
// add back and next in history
......
......@@ -288,7 +288,7 @@ class OKULAR_PART_EXPORT Part : public KParts::ReadWritePart, public Okular::Doc
KUrl m_oldUrl;
Okular::DocumentViewport m_viewportDirty;
bool m_wasPresentationOpen;
int m_dirtyToolboxIndex;
QWidget *m_dirtyToolboxItem;
bool m_wasSidebarVisible;
bool m_wasSidebarCollapsed;
bool m_fileWasRemoved;
......
......@@ -397,6 +397,14 @@ public:
{
}
int indexOf(QWidget *w) const
{
for (int i = 0; i < pages.count(); ++i) {
if (pages[i]->widget() == w) return i;
}
return -1;
}
void adjustListSize( bool recalc, bool expand = true );
SidebarListWidget *list;
......@@ -556,7 +564,13 @@ void Sidebar::setBottomWidget( QWidget *widget )
}
}
void Sidebar::setItemEnabled( int index, bool enabled )
void Sidebar::setItemEnabled( QWidget *widget, bool enabled )
{
const int index = d->indexOf( widget );
setIndexEnabled( index, enabled );
}
void Sidebar::setIndexEnabled( int index, bool enabled )
{
if ( index < 0 || index >= d->pages.count() )
return;
......@@ -574,7 +588,7 @@ void Sidebar::setItemEnabled( int index, bool enabled )
}
d->pages.at( index )->setFlags( f );
if ( !enabled && index == currentIndex() && isSidebarVisible() )
if ( !enabled && index == d->list->currentRow() && 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 )
......@@ -584,18 +598,30 @@ void Sidebar::setItemEnabled( int index, bool enabled )
}
}
bool Sidebar::isItemEnabled( int index ) const
bool Sidebar::isItemEnabled( QWidget *widget ) const
{
if ( index < 0 || index >= d->pages.count() )
const int index = d->indexOf( widget );
return isIndexEnabled( index );
}
bool Sidebar::isIndexEnabled( int index ) const
{
if ( index < 0 )
return false;
Qt::ItemFlags f = d->pages.at( index )->flags();
return ( f & Qt::ItemIsEnabled ) == Qt::ItemIsEnabled;
}
void Sidebar::setCurrentIndex( int index, SetCurrentIndexBehaviour b )
void Sidebar::setCurrentItem( QWidget *widget, SetCurrentItemBehaviour b )
{
if ( index < 0 || index >= d->pages.count() || !isItemEnabled( index ) )
const int index = d->indexOf( widget );
setCurrentIndex( index, b );
}
void Sidebar::setCurrentIndex( int index, SetCurrentItemBehaviour b )
{
if ( index < 0 || !isIndexEnabled( index ) )
return;
itemClicked( d->pages.at( index ), b );
......@@ -604,9 +630,13 @@ void Sidebar::setCurrentIndex( int index, SetCurrentIndexBehaviour b )
d->list->selectionModel()->select( modelindex, QItemSelectionModel::ClearAndSelect );
}
int Sidebar::currentIndex() const
QWidget *Sidebar::currentItem() const
{
return d->list->currentRow();
const int row = d->list->currentRow();
if (row < 0 || row >= d->pages.count())
return 0;
return d->pages[row]->widget();
}
void Sidebar::setSidebarVisibility( bool visible )
......@@ -653,10 +683,14 @@ void Sidebar::moveSplitter(int sideWidgetSize)
d->splitter->setSizes( splitterSizeList );
}
void Sidebar::setItemVisible( int index, bool visible )
void Sidebar::setItemVisible( QWidget *widget, bool visible )
{
const int index = d->indexOf( widget );
if ( index < 0 )
return;
d->list->setRowHidden( index, !visible );
setItemEnabled( index, visible );
setIndexEnabled( index, visible );
}
void Sidebar::itemClicked( QListWidgetItem *item )
......@@ -664,7 +698,7 @@ void Sidebar::itemClicked( QListWidgetItem *item )
itemClicked( item, UncollapseIfCollapsed );
}
void Sidebar::itemClicked( QListWidgetItem *item, SetCurrentIndexBehaviour b )
void Sidebar::itemClicked( QListWidgetItem *item, SetCurrentItemBehaviour b )
{
if ( !item )
return;
......
......@@ -28,15 +28,15 @@ class Sidebar : public QWidget
void setMainWidget( QWidget *widget );
void setBottomWidget( QWidget *widget );
void setItemEnabled( int index, bool enabled );
bool isItemEnabled( int index ) const;
void setItemEnabled( QWidget *widget, bool enabled );
bool isItemEnabled( QWidget *widget ) const;
void setItemVisible( int index, bool visible );
void setItemVisible( QWidget *widget, bool visible );
enum SetCurrentIndexBehaviour { UncollapseIfCollapsed, DoNotUncollapseIfCollapsed };
enum SetCurrentItemBehaviour { UncollapseIfCollapsed, DoNotUncollapseIfCollapsed };
void setCurrentIndex( int index, SetCurrentIndexBehaviour b = UncollapseIfCollapsed );
int currentIndex() const;
void setCurrentItem( QWidget *widget, SetCurrentItemBehaviour b = UncollapseIfCollapsed );
QWidget *currentItem() const;
void setSidebarVisibility( bool visible );
bool isSidebarVisible() const;
......@@ -62,7 +62,10 @@ class Sidebar : public QWidget
void appearanceChanged();
private:
void itemClicked( QListWidgetItem *item, SetCurrentIndexBehaviour b );
void setIndexEnabled( int index, bool enabled );
void setCurrentIndex( int index, SetCurrentItemBehaviour b = UncollapseIfCollapsed );
bool isIndexEnabled( int index ) const;
void itemClicked( QListWidgetItem *item, SetCurrentItemBehaviour b );
void saveSplitterSize() const;
// private storage
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment