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 <QStackedWidget>
18 #include <QFontMetrics>
19 #include <QTreeWidget>
20 #include <QHBoxLayout>
23 #include <boost/assert.hpp>
32 PanelStack::PanelStack(QWidget * parent)
35 list_ = new QTreeWidget(this);
36 stack_ = new QStackedWidget(this);
38 list_->setColumnCount(1);
39 list_->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding));
40 stack_->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding));
42 list_->setSortingEnabled(false);
43 // list_->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
44 // list_->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
45 // list_->addColumn("");
46 // list_->setColumnWidthMode(0, QTreeWidget::Maximum);
48 // list_->setResizeMode(QTreeWidget::AllColumns);
50 QStringList HeaderLabels; HeaderLabels << QString("Category");
51 list_->setHeaderLabels(HeaderLabels);
53 connect(list_, SIGNAL(currentItemChanged (QTreeWidgetItem*, QTreeWidgetItem*)),
54 this, SLOT(switchPanel(QTreeWidgetItem *, QTreeWidgetItem*)));
56 QHBoxLayout * layout = new QHBoxLayout(this);
57 layout->addWidget(list_, 0);
58 layout->addWidget(stack_, 1);
62 void PanelStack::addCategory(string const & n, string const & parent)
64 QTreeWidgetItem * item;
66 QString name = toqstr(n);
68 cout << "addCategory n= " << n << " parent= " << endl;
71 item = new QTreeWidgetItem(list_);
72 item->setText(0, name);
73 //list_->addTopLevelItem(item);
76 PanelMap::iterator it = panel_map_.find(parent);
77 //BOOST_ASSERT(it != panel_map_.end());
78 if (it == panel_map_.end()) {
80 it = panel_map_.find(parent);
82 BOOST_ASSERT(it != panel_map_.end());
84 item = new QTreeWidgetItem(it->second);
85 item->setText(0, name);
86 //it->second->addChild(item);
91 list_->setFixedWidth(list_->sizeHint().width());
93 item->setFlags(false);
96 // calculate the real size the current item needs in the listview
97 int itemsize = item->width(list_->fontMetrics(), list_, 0) + 10
98 + list_->treeStepSize() * (item->depth() + 1) + list_->itemMargin();
99 // adjust the listview width to the max. itemsize
100 if (itemsize > list_->minimumWidth())
101 list_->setMinimumWidth(itemsize);
106 void PanelStack::addPanel(QWidget * panel, string const & name, string const & parent)
108 addCategory(name, parent);
109 QTreeWidgetItem * item = panel_map_.find(name)->second;
111 // reset the selectability set by addCategory
112 // item->setSelectable(true);
114 widget_map_[item] = panel;
115 stack_->addWidget(panel);
116 stack_->setMinimumSize(panel->minimumSize());
120 void PanelStack::setCurrentPanel(string const & name)
122 PanelMap::const_iterator cit = panel_map_.find(name);
123 BOOST_ASSERT(cit != panel_map_.end());
125 // force on first set
126 if (list_->currentItem() == cit->second)
127 switchPanel(cit->second);
129 list_->setCurrentItem(cit->second);
133 void PanelStack::switchPanel(QTreeWidgetItem * item,
134 QTreeWidgetItem * /*previous*/)
136 WidgetMap::const_iterator cit = widget_map_.find(item);
137 if (cit == widget_map_.end())
140 stack_->setCurrentWidget(cit->second);
143 #include "panelstack_moc.cpp"