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 <QHBoxLayout>
21 #include <QHeaderView>
22 #include <QStackedWidget>
23 #include <QTreeWidget>
25 #include "support/lassert.h"
33 PanelStack::PanelStack(QWidget * parent)
36 list_ = new QTreeWidget(this);
37 stack_ = new QStackedWidget(this);
39 list_->setRootIsDecorated(false);
40 list_->setColumnCount(1);
41 list_->header()->hide();
42 list_->header()->setResizeMode(QHeaderView::ResizeToContents);
43 list_->header()->setStretchLastSection(false);
44 list_->setMinimumSize(list_->viewport()->size());
46 connect(list_, SIGNAL(currentItemChanged(QTreeWidgetItem*, QTreeWidgetItem*)),
47 this, SLOT(switchPanel(QTreeWidgetItem *, QTreeWidgetItem*)));
48 connect(list_, SIGNAL(itemClicked (QTreeWidgetItem*, int)),
49 this, SLOT(itemSelected(QTreeWidgetItem *, int)));
51 QHBoxLayout * layout = new QHBoxLayout(this);
52 layout->addWidget(list_, 0);
53 layout->addWidget(stack_, 1);
57 void PanelStack::addCategory(QString const & name, QString const & parent)
59 QTreeWidgetItem * item = 0;
61 LYXERR(Debug::GUI, "addCategory n= " << name << " parent= ");
65 if (parent.isEmpty()) {
66 item = new QTreeWidgetItem(list_);
67 item->setText(0, name);
70 if (!panel_map_.contains(parent))
72 item = new QTreeWidgetItem(panel_map_.value(parent));
73 item->setText(0, name);
75 list_->setRootIsDecorated(true);
78 panel_map_[name] = item;
80 QFontMetrics fm(list_->font());
82 // calculate the real size the current item needs in the listview
83 int itemsize = fm.width(name) + 10 + 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::showPanel(QString const & name, bool show)
102 QTreeWidgetItem * item = panel_map_.value(name, 0);
103 LASSERT(item, return);
105 item->setHidden(!show);
109 void PanelStack::setCurrentPanel(QString const & name)
111 QTreeWidgetItem * item = panel_map_.value(name, 0);
112 LASSERT(item, return);
114 // force on first set
115 if (list_->currentItem() == item)
118 list_->setCurrentItem(item);
122 bool PanelStack::isCurrentPanel(QString const & name) const
124 QTreeWidgetItem * item = panel_map_.value(name, 0);
125 LASSERT(item, return false);
127 return (list_->currentItem() == item);
131 void PanelStack::switchPanel(QTreeWidgetItem * item,
132 QTreeWidgetItem * previous)
134 // do nothing when clicked on whitespace (item=NULL)
138 // if we have a category, expand the tree and go to the
140 if (item->childCount() > 0) {
141 item->setExpanded(true);
142 if (previous && previous->parent() != item)
143 switchPanel( item->child(0), previous );
145 else if (QWidget * w = widget_map_.value(item, 0)) {
146 stack_->setCurrentWidget(w);
151 void PanelStack::itemSelected(QTreeWidgetItem * item, int)
153 // de-select the category if a child is selected
154 if (item->childCount() > 0 && item->child(0)->isSelected())
155 item->setSelected(false);
159 QSize PanelStack::sizeHint() const
161 return QSize(list_->width() + stack_->width(),
162 qMax(list_->height(), stack_->height()));
165 } // namespace frontend
168 #include "moc_PanelStack.cpp"