24 #include "kpageview_p.h"
32 #include <QAbstractItemView>
33 #include <QGridLayout>
36 #include <QStackedWidget>
39 void KPageViewPrivate::_k_rebuildGui()
44 QModelIndex currentLastIndex;
45 if ( view && view->selectionModel() ) {
46 QObject::disconnect(view->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
47 q, SLOT(_k_pageSelected(QItemSelection,QItemSelection)));
48 currentLastIndex = view->selectionModel()->currentIndex();
52 view = q->createView();
56 view->setSelectionBehavior( QAbstractItemView::SelectItems );
57 view->setSelectionMode( QAbstractItemView::SingleSelection );
60 view->setModel( model );
64 if ( view->selectionModel() ) {
65 QObject::connect(view->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)), q, SLOT(_k_pageSelected(QItemSelection,QItemSelection)));
67 if ( currentLastIndex.isValid() )
68 view->selectionModel()->setCurrentIndex( currentLastIndex, QItemSelectionModel::Select );
70 view->selectionModel()->setCurrentIndex( model->index( 0, 0 ), QItemSelectionModel::Select );
74 stack->setVisible(
false);
75 layout->removeWidget( stack );
77 layout->addWidget( stack, 2, 1 );
78 stack->setVisible(
true);
81 titleWidget->setVisible(q->showPageHeader());
83 Qt::Alignment alignment = q->viewPosition();
84 if ( alignment & Qt::AlignTop )
85 layout->addWidget( view, 2, 1 );
86 else if ( alignment & Qt::AlignRight )
87 layout->addWidget( view, 1, 2, 2, 1 );
88 else if ( alignment & Qt::AlignBottom )
89 layout->addWidget( view, 4, 1 );
90 else if ( alignment & Qt::AlignLeft )
91 layout->addWidget( view, 1, 0, 2, 1 );
94 void KPageViewPrivate::updateSelection()
103 if ( !view || !view->selectionModel() )
106 const QModelIndex index = view->selectionModel()->currentIndex();
107 if ( !index.isValid() )
108 view->selectionModel()->setCurrentIndex( model->index( 0, 0 ), QItemSelectionModel::Select );
111 void KPageViewPrivate::cleanupPages()
119 for (
int i = 0; i < stack->count(); ++i ) {
120 QWidget *page = stack->widget( i );
123 for (
int j = 0; j < widgets.count(); ++j ) {
124 if ( widgets[ j ] == page )
129 stack->removeWidget( page );
133 QList<QWidget *> KPageViewPrivate::collectPages(
const QModelIndex &parentIndex)
141 int rows = model->rowCount( parentIndex );
142 for (
int j = 0; j < rows; ++j ) {
143 const QModelIndex index = model->index( j, 0, parentIndex );
146 if ( model->rowCount( index ) > 0 ) {
147 retval += collectPages( index );
162 bool hasSubPages =
false;
163 const int count = model->rowCount();
164 for (
int i = 0; i < count; ++i ) {
165 if ( model->rowCount( model->index( i, 0 ) ) > 0 ) {
174 if ( model->rowCount() > 1 )
180 void KPageViewPrivate::_k_modelChanged()
198 QSize size = stack->size();
200 for (
int i = 0; i < widgets.count(); ++i ) {
201 const QWidget *widget = widgets[ i ];
203 size = size.expandedTo( widget->minimumSizeHint() );
205 stack->setMinimumSize( size );
210 void KPageViewPrivate::_k_pageSelected(
const QItemSelection &index,
const QItemSelection &previous)
216 if ( index.indexes().size() != 1 ) {
219 QModelIndex currentIndex = index.indexes().first();
221 QModelIndex previousIndex;
223 if ( previous.indexes().size() == 1 ) {
224 previousIndex = previous.indexes().first();
231 if ( stack->indexOf( widget ) == -1 ) {
232 stack->addWidget( widget );
235 stack->setCurrentWidget( widget );
237 stack->setCurrentWidget( defaultWidget );
240 updateTitleWidget(currentIndex);
244 emit q->currentPageChanged(currentIndex, previousIndex);
247 void KPageViewPrivate::updateTitleWidget(
const QModelIndex& index)
252 if ( header.isNull() ) {
253 header = model->data( index, Qt::DisplayRole ).toString();
256 const QIcon icon = model->data( index, Qt::DecorationRole ).value<QIcon>();
257 titleWidget->setPixmap( icon.pixmap( 22, 22 ) );
258 titleWidget->setText( header );
260 titleWidget->setVisible(q->showPageHeader());
263 void KPageViewPrivate::_k_dataChanged(
const QModelIndex &,
const QModelIndex &)
272 QModelIndex index = view->selectionModel()->currentIndex();
273 if ( !index.isValid() )
276 updateTitleWidget( index );
279 KPageViewPrivate::KPageViewPrivate(
KPageView *_parent)
280 : q_ptr(_parent), model(0), faceType(
KPageView::Auto),
281 layout(0), stack(0), titleWidget(0), view(0)
285 void KPageViewPrivate::init()
288 layout =
new QGridLayout(q);
289 stack =
new KPageStackedWidget(q);
291 QPixmap emptyPixmap(22, 22);
292 emptyPixmap.fill(Qt::transparent);
293 titleWidget->setPixmap(emptyPixmap);
294 layout->addWidget(titleWidget, 1, 1);
295 layout->addWidget(stack, 2, 1);
297 defaultWidget =
new QWidget(q);
298 stack->addWidget(defaultWidget);
301 layout->setColumnStretch(1, 1);
302 layout->setRowStretch(2, 1);
309 :
QWidget(parent), d_ptr(new KPageViewPrivate(this))
330 disconnect(d->model, SIGNAL(layoutChanged()),
this, SLOT(_k_modelChanged()));
331 disconnect(d->model, SIGNAL(dataChanged(QModelIndex,QModelIndex)),
332 this, SLOT(_k_dataChanged(QModelIndex,QModelIndex)));
338 connect(d->model, SIGNAL(layoutChanged()),
this, SLOT(_k_modelChanged()));
339 connect(d->model, SIGNAL(dataChanged(QModelIndex,QModelIndex)),
340 this, SLOT(_k_dataChanged(QModelIndex,QModelIndex)));
344 d->view->setModel( model );
352 return d_func()->model;
365 return d_func()->faceType;
371 if ( !d->view || !d->view->selectionModel() )
374 d->view->selectionModel()->setCurrentIndex( index, QItemSelectionModel::SelectCurrent );
380 if ( !d->view || !d->view->selectionModel() )
381 return QModelIndex();
383 return d->view->selectionModel()->currentIndex();
390 d->view->setItemDelegate( delegate );
397 return d->view->itemDelegate();
408 bool isCurrent = (d->stack->currentIndex() == d->stack->indexOf( d->defaultWidget ));
411 d->stack->removeWidget( d->defaultWidget );
412 delete d->defaultWidget;
415 d->defaultWidget = widget;
416 d->stack->addWidget(d->defaultWidget);
419 d->stack->setCurrentWidget(d->defaultWidget);
425 if ( d->faceType ==
Auto ) {
428 if ( faceType ==
Plain )
429 return new KDEPrivate::KPagePlainView(
this );
430 else if ( faceType ==
List )
431 return new KDEPrivate::KPageListView(
this );
432 else if ( faceType ==
Tree )
433 return new KDEPrivate::KPageTreeView(
this );
436 }
else if ( d->faceType ==
Plain )
437 return new KDEPrivate::KPagePlainView(
this );
438 else if ( d->faceType ==
List )
439 return new KDEPrivate::KPageListView(
this );
440 else if ( d->faceType ==
Tree )
441 return new KDEPrivate::KPageTreeView(
this );
442 else if ( d->faceType ==
Tabbed )
443 return new KDEPrivate::KPageTabbedView(
this );
453 if ( faceType ==
Auto )
454 faceType = d->detectAutoFace();
456 if ( faceType ==
Tabbed ) {
459 return !d->titleWidget->text().isEmpty();
468 if ( faceType ==
Auto )
469 faceType = d->detectAutoFace();
474 return Qt::AlignLeft;
477 #include "kpageview.moc"
KPageView(QWidget *parent=0)
Creates a page view with given parent.
virtual ~KPageView()
Destroys the page view.
FaceType
This enum is used to decide which type of navigation view shall be used in the page view...
virtual QAbstractItemView * createView()
Returns the navigation view, depending on the current face type.
void setFaceType(FaceType faceType)
Sets the face type of the page view.
A base class which can handle multiple pages.
void setModel(QAbstractItemModel *model)
Sets the model of the page view.
QModelIndex currentPage() const
Returns the index for the current page or an invalid index if no current page exists.
A string to be rendered as page header.
QAbstractItemModel * model() const
Returns the model of the page view.
FaceType faceType() const
Returns the face type of the page view.
A pointer to the page widget.
void setItemDelegate(QAbstractItemDelegate *delegate)
Sets the item.
virtual bool showPageHeader() const
Returns whether the page header should be visible.
QAbstractItemDelegate * itemDelegate() const
Returns the item delegate of the page view.
void setCurrentPage(const QModelIndex &index)
Sets the page with.
KPageViewPrivate *const d_ptr
virtual Qt::Alignment viewPosition() const
Returns the position where the navigation view should be located according to the page stack...
void setDefaultWidget(QWidget *widget)
Sets the widget which will be shown when a page is selected that has no own widget set...