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"
19 #include <QFontMetrics>
20 #include <QStackedWidget>
21 #include <QTreeWidget>
22 #include <QHBoxLayout>
23 #include <QHeaderView>
25 #include <boost/assert.hpp>
37 PanelStack::PanelStack(QWidget * parent)
40 list_ = new QTreeWidget(this);
41 stack_ = new QStackedWidget(this);
43 list_->setColumnCount(1);
44 list_->setRootIsDecorated(false);
45 // Hide the pointless list header
46 list_->header()->hide();
47 // QStringList HeaderLabels;
48 // HeaderLabels << QString("Category");
49 // list_->setHeaderLabels(HeaderLabels);
51 connect(list_, SIGNAL(currentItemChanged (QTreeWidgetItem*, QTreeWidgetItem*)),
52 this, SLOT(switchPanel(QTreeWidgetItem *, QTreeWidgetItem*)));
54 QHBoxLayout * layout = new QHBoxLayout(this);
55 layout->addWidget(list_, 0);
56 layout->addWidget(stack_, 1);
60 void PanelStack::addCategory(docstring const & n, docstring const & parent)
62 QTreeWidgetItem * item = 0;
63 QString const name = toqstr(n);
65 LYXERR(Debug::GUI) << "addCategory n= " << to_utf8(n) << " parent= " << endl;
70 item = new QTreeWidgetItem(list_);
71 item->setText(0, name);
74 PanelMap::iterator it = panel_map_.find(parent);
75 //BOOST_ASSERT(it != panel_map_.end());
76 if (it == panel_map_.end()) {
78 it = panel_map_.find(parent);
80 BOOST_ASSERT(it != panel_map_.end());
82 item = new QTreeWidgetItem(it->second);
83 item->setText(0, name);
89 QFontMetrics fm(list_->font());
90 // calculate the real size the current item needs in the listview
91 int itemsize = fm.width(name) + 10
92 + list_->indentation() * depth;
93 // adjust the listview width to the max. itemsize
94 if (itemsize > list_->minimumWidth())
95 list_->setMinimumWidth(itemsize);
99 void PanelStack::addPanel(QWidget * panel, docstring const & name, docstring const & parent)
101 addCategory(name, parent);
102 QTreeWidgetItem * item = panel_map_.find(name)->second;
104 widget_map_[item] = panel;
105 stack_->addWidget(panel);
106 stack_->setMinimumSize(panel->minimumSize());
110 void PanelStack::setCurrentPanel(docstring const & name)
112 PanelMap::const_iterator cit = panel_map_.find(name);
113 BOOST_ASSERT(cit != panel_map_.end());
115 // force on first set
116 if (list_->currentItem() == cit->second)
117 switchPanel(cit->second);
119 list_->setCurrentItem(cit->second);
123 void PanelStack::switchPanel(QTreeWidgetItem * item,
124 QTreeWidgetItem * /*previous*/)
126 WidgetMap::const_iterator cit = widget_map_.find(item);
127 if (cit == widget_map_.end())
130 stack_->setCurrentWidget(cit->second);
134 QSize PanelStack::sizeHint() const
136 return QSize(list_->width() + stack_->width(),
137 qMax(list_->height(), stack_->height()));
140 } // namespace frontend
143 #include "PanelStack_moc.cpp"