3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
13 #include "panelstack.h"
15 #include "qt_helpers.h"
17 #include <qwidgetstack.h>
18 #include <qfontmetrics.h>
20 #include <qlistview.h>
22 #include <boost/assert.hpp>
27 PanelStack::PanelStack(QWidget * parent, const char * name)
28 : QWidget(parent, name)
30 list_ = new QListView(this);
31 stack_ = new QWidgetStack(this);
32 list_->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding));
33 stack_->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding));
35 list_->setSorting(-1);
36 list_->setHScrollBarMode(QScrollView::AlwaysOff);
37 list_->setVScrollBarMode(QScrollView::AlwaysOff);
39 list_->setColumnWidthMode(0, QListView::Maximum);
40 list_->setResizeMode(QListView::AllColumns);
41 list_->setRootIsDecorated(true);
42 QWidget * w = static_cast<QWidget*>(list_->child("list view header"));
46 connect(list_, SIGNAL(currentChanged(QListViewItem*)),
47 this, SLOT(switchPanel(QListViewItem *)));
49 QHBoxLayout * layout = new QHBoxLayout(this);
50 layout->addWidget(list_, 0);
51 layout->addWidget(stack_, 1);
55 void PanelStack::addCategory(string const & n, string const & parent)
59 QString name = toqstr(n);
61 if (!parent.empty()) {
62 PanelMap::iterator it = panel_map_.find(parent);
63 BOOST_ASSERT(it != panel_map_.end());
65 QListViewItem * before = it->second->firstChild();
67 while (before->nextSibling())
68 before = before->nextSibling();
70 item = new QListViewItem(it->second, before, name);
72 item = new QListViewItem(it->second, name);
75 QListViewItem * before = list_->firstChild();
77 while (before->nextSibling())
78 before = before->nextSibling();
79 item = new QListViewItem(list_, before, name);
81 item = new QListViewItem(list_, name);
85 item->setSelectable(false);
89 // calculate the real size the current item needs in the listview
90 QFontMetrics fm(list_->font());
91 int itemsize = item->width(fm, list_, 0) + 10
92 + list_->treeStepSize() * (item->depth() + 1) + list_->itemMargin();
93 // adjust the listview width to the max. itemsize
94 if (itemsize > list_->minimumWidth())
95 list_->setMinimumWidth(itemsize);
99 void PanelStack::addPanel(QWidget * panel, string const & name, string const & parent)
101 addCategory(name, parent);
102 QListViewItem * item = panel_map_.find(name)->second;
104 // reset the selectability set by addCategory
105 item->setSelectable(true);
107 widget_map_[item] = panel;
108 stack_->addWidget(panel, -1);
109 stack_->setMinimumSize(panel->minimumSize());
114 void PanelStack::setCurrentPanel(string const & name)
116 PanelMap::const_iterator cit = panel_map_.find(name);
117 BOOST_ASSERT(cit != panel_map_.end());
119 // force on first set
120 if (list_->currentItem() == cit->second)
121 switchPanel(cit->second);
123 list_->setCurrentItem(cit->second);
127 void PanelStack::switchPanel(QListViewItem * item)
129 WidgetMap::const_iterator cit = widget_map_.find(item);
130 if (cit == widget_map_.end())
133 stack_->raiseWidget(cit->second);