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(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);
78 panel_map_[name] = item;
80 QFontMetrics fm(list_->font());
81 // calculate the real size the current item needs in the listview
82 int itemsize = fm.width(name) + 10
83 + list_->indentation() * depth;
84 // adjust the listview width to the max. itemsize
85 if (itemsize > list_->minimumWidth())
86 list_->setMinimumWidth(itemsize);
90 void PanelStack::addPanel(QWidget * panel, QString const & name, QString const & parent)
92 addCategory(name, parent);
93 QTreeWidgetItem * item = panel_map_.value(name);
94 widget_map_[item] = panel;
95 stack_->addWidget(panel);
96 stack_->setMinimumSize(panel->minimumSize());
100 void PanelStack::setCurrentPanel(QString const & name)
102 QTreeWidgetItem * item = panel_map_.value(name, 0);
105 // force on first set
106 if (list_->currentItem() == item)
109 list_->setCurrentItem(item);
113 void PanelStack::switchPanel(QTreeWidgetItem * item,
114 QTreeWidgetItem * /*previous*/)
116 if (QWidget * w = widget_map_.value(item, 0))
117 stack_->setCurrentWidget(w);
121 QSize PanelStack::sizeHint() const
123 return QSize(list_->width() + stack_->width(),
124 qMax(list_->height(), stack_->height()));
127 } // namespace frontend
130 #include "PanelStack_moc.cpp"