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_->setRootIsDecorated(false);
42 list_->setColumnCount(1);
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(QString const & name, QString const & parent)
60 QTreeWidgetItem * item = 0;
62 LYXERR(Debug::GUI, "addCategory n= " << fromqstr(name) << " parent= ");
66 if (parent.isEmpty()) {
67 item = new QTreeWidgetItem(list_);
68 item->setText(0, name);
71 if (!panel_map_.contains(parent))
73 item = new QTreeWidgetItem(panel_map_.value(parent));
74 item->setText(0, name);
76 list_->setRootIsDecorated(true);
79 panel_map_[name] = item;
81 QFontMetrics fm(list_->font());
83 // calculate the real size the current item needs in the listview
84 int itemsize = fm.width(name) + 10
85 + list_->indentation() * depth;
86 // adjust the listview width to the max. itemsize
87 if (itemsize > list_->minimumWidth())
88 list_->setMinimumWidth(itemsize);
92 void PanelStack::addPanel(QWidget * panel, QString const & name, QString const & parent)
94 addCategory(name, parent);
95 QTreeWidgetItem * item = panel_map_.value(name);
96 widget_map_[item] = panel;
97 stack_->addWidget(panel);
98 stack_->setMinimumSize(panel->minimumSize());
102 void PanelStack::setCurrentPanel(QString const & name)
104 QTreeWidgetItem * item = panel_map_.value(name, 0);
107 // force on first set
108 if (list_->currentItem() == item)
111 list_->setCurrentItem(item);
115 void PanelStack::switchPanel(QTreeWidgetItem * item,
116 QTreeWidgetItem * /*previous*/)
118 // if we have a category, expand the tree and go to the
120 if (item->childCount() > 0) {
121 item->setExpanded(true);
122 list_->setCurrentItem(item->child(0));
124 if (QWidget * w = widget_map_.value(item, 0))
125 stack_->setCurrentWidget(w);
129 QSize PanelStack::sizeHint() const
131 return QSize(list_->width() + stack_->width(),
132 qMax(list_->height(), stack_->height()));
135 } // namespace frontend
138 #include "PanelStack_moc.cpp"