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>
33 PanelStack::PanelStack(QWidget * parent)
36 list_ = new QTreeWidget(this);
37 stack_ = new QStackedWidget(this);
39 list_->setColumnCount(1);
40 list_->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding));
41 stack_->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding));
43 list_->setSortingEnabled(false);
44 // list_->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
45 // list_->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
46 // list_->addColumn("");
47 // list_->setColumnWidthMode(0, QTreeWidget::Maximum);
49 // list_->setResizeMode(QTreeWidget::AllColumns);
51 QStringList HeaderLabels; HeaderLabels << QString("Category");
52 list_->setHeaderLabels(HeaderLabels);
54 connect(list_, SIGNAL(currentItemChanged (QTreeWidgetItem*, QTreeWidgetItem*)),
55 this, SLOT(switchPanel(QTreeWidgetItem *, QTreeWidgetItem*)));
57 QHBoxLayout * layout = new QHBoxLayout(this);
58 layout->addWidget(list_, 0);
59 layout->addWidget(stack_, 1);
63 void PanelStack::addCategory(docstring const & n, docstring const & parent)
65 QTreeWidgetItem * item;
68 lyx::ucs4_to_qstring(n, name);
70 cout << "addCategory n= " << lyx::to_utf8(n) << " parent= " << endl;
73 item = new QTreeWidgetItem(list_);
74 item->setText(0, name);
75 //list_->addTopLevelItem(item);
78 PanelMap::iterator it = panel_map_.find(parent);
79 //BOOST_ASSERT(it != panel_map_.end());
80 if (it == panel_map_.end()) {
82 it = panel_map_.find(parent);
84 BOOST_ASSERT(it != panel_map_.end());
86 item = new QTreeWidgetItem(it->second);
87 item->setText(0, name);
88 //it->second->addChild(item);
93 list_->setFixedWidth(list_->sizeHint().width());
95 item->setFlags(false);
98 // calculate the real size the current item needs in the listview
99 int itemsize = item->width(list_->fontMetrics(), list_, 0) + 10
100 + list_->treeStepSize() * (item->depth() + 1) + list_->itemMargin();
101 // adjust the listview width to the max. itemsize
102 if (itemsize > list_->minimumWidth())
103 list_->setMinimumWidth(itemsize);
108 void PanelStack::addPanel(QWidget * panel, docstring const & name, docstring const & parent)
110 addCategory(name, parent);
111 QTreeWidgetItem * item = panel_map_.find(name)->second;
113 // reset the selectability set by addCategory
114 // item->setSelectable(true);
116 widget_map_[item] = panel;
117 stack_->addWidget(panel);
118 stack_->setMinimumSize(panel->minimumSize());
122 void PanelStack::setCurrentPanel(docstring const & name)
124 PanelMap::const_iterator cit = panel_map_.find(name);
125 BOOST_ASSERT(cit != panel_map_.end());
127 // force on first set
128 if (list_->currentItem() == cit->second)
129 switchPanel(cit->second);
131 list_->setCurrentItem(cit->second);
135 void PanelStack::switchPanel(QTreeWidgetItem * item,
136 QTreeWidgetItem * /*previous*/)
138 WidgetMap::const_iterator cit = widget_map_.find(item);
139 if (cit == widget_map_.end())
142 stack_->setCurrentWidget(cit->second);
145 #include "panelstack_moc.cpp"