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 // Hide the pointless list header
45 list_->header()->hide();
46 // QStringList HeaderLabels;
47 // HeaderLabels << QString("Category");
48 // list_->setHeaderLabels(HeaderLabels);
50 connect(list_, SIGNAL(currentItemChanged (QTreeWidgetItem*, QTreeWidgetItem*)),
51 this, SLOT(switchPanel(QTreeWidgetItem *, QTreeWidgetItem*)));
53 QHBoxLayout * layout = new QHBoxLayout(this);
54 layout->addWidget(list_, 0);
55 layout->addWidget(stack_, 1);
59 void PanelStack::addCategory(docstring const & n, docstring const & parent)
61 QTreeWidgetItem * item = 0;
62 QString const name = toqstr(n);
64 LYXERR(Debug::GUI) << "addCategory n= " << to_utf8(n) << " parent= " << endl;
69 item = new QTreeWidgetItem(list_);
70 item->setText(0, name);
73 PanelMap::iterator it = panel_map_.find(parent);
74 //BOOST_ASSERT(it != panel_map_.end());
75 if (it == panel_map_.end()) {
77 it = panel_map_.find(parent);
79 BOOST_ASSERT(it != panel_map_.end());
81 item = new QTreeWidgetItem(it->second);
82 item->setText(0, name);
88 QFontMetrics fm(list_->font());
89 // calculate the real size the current item needs in the listview
90 int itemsize = fm.width(name) + 10
91 + list_->indentation() * depth;
92 // adjust the listview width to the max. itemsize
93 if (itemsize > list_->minimumWidth())
94 list_->setMinimumWidth(itemsize);
98 void PanelStack::addPanel(QWidget * panel, docstring const & name, docstring const & parent)
100 addCategory(name, parent);
101 QTreeWidgetItem * item = panel_map_.find(name)->second;
103 widget_map_[item] = panel;
104 stack_->addWidget(panel);
105 stack_->setMinimumSize(panel->minimumSize());
109 void PanelStack::setCurrentPanel(docstring const & name)
111 PanelMap::const_iterator cit = panel_map_.find(name);
112 BOOST_ASSERT(cit != panel_map_.end());
114 // force on first set
115 if (list_->currentItem() == cit->second)
116 switchPanel(cit->second);
118 list_->setCurrentItem(cit->second);
122 void PanelStack::switchPanel(QTreeWidgetItem * item,
123 QTreeWidgetItem * /*previous*/)
125 WidgetMap::const_iterator cit = widget_map_.find(item);
126 if (cit == widget_map_.end())
129 stack_->setCurrentWidget(cit->second);
133 QSize PanelStack::sizeHint() const
135 return QSize(list_->width() + stack_->width(),
136 qMax(list_->height(), stack_->height()));
139 } // namespace frontend
142 #include "PanelStack_moc.cpp"