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 "support/debug.h"
19 #include <QFontMetrics>
20 #include <QStackedWidget>
21 #include <QTreeWidget>
22 #include <QHBoxLayout>
23 #include <QHeaderView>
25 #include <boost/assert.hpp>
35 PanelStack::PanelStack(QWidget * parent)
38 list_ = new QTreeWidget(this);
39 stack_ = new QStackedWidget(this);
41 list_->setColumnCount(1);
42 list_->setRootIsDecorated(false);
43 // Hide the pointless list header
44 list_->header()->hide();
45 // QStringList HeaderLabels;
46 // HeaderLabels << QString("Category");
47 // list_->setHeaderLabels(HeaderLabels);
49 connect(list_, SIGNAL(currentItemChanged (QTreeWidgetItem*, QTreeWidgetItem*)),
50 this, SLOT(switchPanel(QTreeWidgetItem *, QTreeWidgetItem*)));
52 QHBoxLayout * layout = new QHBoxLayout(this);
53 layout->addWidget(list_, 0);
54 layout->addWidget(stack_, 1);
58 void PanelStack::addCategory(docstring const & n, docstring const & parent)
60 QTreeWidgetItem * item = 0;
61 QString const name = toqstr(n);
63 LYXERR(Debug::GUI, "addCategory n= " << to_utf8(n) << " parent= ");
68 item = new QTreeWidgetItem(list_);
69 item->setText(0, name);
72 PanelMap::iterator it = panel_map_.find(parent);
73 //BOOST_ASSERT(it != panel_map_.end());
74 if (it == panel_map_.end()) {
76 it = panel_map_.find(parent);
78 BOOST_ASSERT(it != panel_map_.end());
80 item = new QTreeWidgetItem(it->second);
81 item->setText(0, name);
87 QFontMetrics fm(list_->font());
88 // calculate the real size the current item needs in the listview
89 int itemsize = fm.width(name) + 10
90 + list_->indentation() * depth;
91 // adjust the listview width to the max. itemsize
92 if (itemsize > list_->minimumWidth())
93 list_->setMinimumWidth(itemsize);
97 void PanelStack::addPanel(QWidget * panel, docstring const & name, docstring const & parent)
99 addCategory(name, parent);
100 QTreeWidgetItem * item = panel_map_.find(name)->second;
102 widget_map_[item] = panel;
103 stack_->addWidget(panel);
104 stack_->setMinimumSize(panel->minimumSize());
108 void PanelStack::setCurrentPanel(docstring const & name)
110 PanelMap::const_iterator cit = panel_map_.find(name);
111 BOOST_ASSERT(cit != panel_map_.end());
113 // force on first set
114 if (list_->currentItem() == cit->second)
115 switchPanel(cit->second);
117 list_->setCurrentItem(cit->second);
121 void PanelStack::switchPanel(QTreeWidgetItem * item,
122 QTreeWidgetItem * /*previous*/)
124 WidgetMap::const_iterator cit = widget_map_.find(item);
125 if (cit == widget_map_.end())
128 stack_->setCurrentWidget(cit->second);
132 QSize PanelStack::sizeHint() const
134 return QSize(list_->width() + stack_->width(),
135 qMax(list_->height(), stack_->height()));
138 } // namespace frontend
141 #include "PanelStack_moc.cpp"