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 "support/lassert.h"
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*)));
51 connect(list_, SIGNAL(itemClicked (QTreeWidgetItem*, int)),
52 this, SLOT(itemSelected(QTreeWidgetItem *, int)));
54 QHBoxLayout * layout = new QHBoxLayout(this);
55 layout->addWidget(list_, 0);
56 layout->addWidget(stack_, 1);
60 void PanelStack::addCategory(QString const & name, QString const & parent)
62 QTreeWidgetItem * item = 0;
64 LYXERR(Debug::GUI, "addCategory n= " << fromqstr(name) << " parent= ");
68 if (parent.isEmpty()) {
69 item = new QTreeWidgetItem(list_);
70 item->setText(0, name);
73 if (!panel_map_.contains(parent))
75 item = new QTreeWidgetItem(panel_map_.value(parent));
76 item->setText(0, name);
78 list_->setRootIsDecorated(true);
81 panel_map_[name] = item;
83 QFontMetrics fm(list_->font());
85 // calculate the real size the current item needs in the listview
86 int itemsize = fm.width(name) + 10
87 + list_->indentation() * depth;
88 // adjust the listview width to the max. itemsize
89 if (itemsize > list_->minimumWidth())
90 list_->setMinimumWidth(itemsize);
94 void PanelStack::addPanel(QWidget * panel, QString const & name, QString const & parent)
96 addCategory(name, parent);
97 QTreeWidgetItem * item = panel_map_.value(name);
98 widget_map_[item] = panel;
99 stack_->addWidget(panel);
100 stack_->setMinimumSize(panel->minimumSize());
104 void PanelStack::setCurrentPanel(QString const & name)
106 QTreeWidgetItem * item = panel_map_.value(name, 0);
109 // force on first set
110 if (list_->currentItem() == item)
113 list_->setCurrentItem(item);
117 void PanelStack::switchPanel(QTreeWidgetItem * item,
118 QTreeWidgetItem * previous)
120 // if we have a category, expand the tree and go to the
122 if (item->childCount() > 0) {
123 item->setExpanded(true);
124 if (previous != item->child(0))
125 list_->setCurrentItem(item->child(0));
127 if (QWidget * w = widget_map_.value(item, 0))
128 stack_->setCurrentWidget(w);
132 void PanelStack::itemSelected(QTreeWidgetItem * item, int)
134 // de-select the category if a child is selected
135 if (item->childCount() > 0 && item->child(0)->isSelected())
136 item->setSelected(false);
140 QSize PanelStack::sizeHint() const
142 return QSize(list_->width() + stack_->width(),
143 qMax(list_->height(), stack_->height()));
146 } // namespace frontend
149 #include "PanelStack_moc.cpp"