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();
43 connect(list_
, SIGNAL(currentItemChanged(QTreeWidgetItem
*, QTreeWidgetItem
*)),
44 this, SLOT(switchPanel(QTreeWidgetItem
*, QTreeWidgetItem
*)));
45 connect(list_
, SIGNAL(itemClicked (QTreeWidgetItem
*, int)),
46 this, SLOT(itemSelected(QTreeWidgetItem
*, int)));
48 QHBoxLayout
* layout
= new QHBoxLayout(this);
49 layout
->addWidget(list_
, 0);
50 layout
->addWidget(stack_
, 1);
54 void PanelStack::addCategory(QString
const & name
, QString
const & parent
)
56 QTreeWidgetItem
* item
= 0;
58 LYXERR(Debug::GUI
, "addCategory n= " << name
<< " parent= ");
62 if (parent
.isEmpty()) {
63 item
= new QTreeWidgetItem(list_
);
64 item
->setText(0, name
);
67 if (!panel_map_
.contains(parent
))
69 item
= new QTreeWidgetItem(panel_map_
.value(parent
));
70 item
->setText(0, name
);
72 list_
->setRootIsDecorated(true);
75 panel_map_
[name
] = item
;
77 QFontMetrics
fm(list_
->font());
79 // calculate the real size the current item needs in the listview
80 int itemsize
= fm
.width(name
) + 10 + list_
->indentation() * depth
;
81 // adjust the listview width to the max. itemsize
82 if (itemsize
> list_
->minimumWidth())
83 list_
->setMinimumWidth(itemsize
);
87 void PanelStack::addPanel(QWidget
* panel
, QString
const & name
, QString
const & parent
)
89 addCategory(name
, parent
);
90 QTreeWidgetItem
* item
= panel_map_
.value(name
);
91 widget_map_
[item
] = panel
;
92 stack_
->addWidget(panel
);
93 stack_
->setMinimumSize(panel
->minimumSize());
97 void PanelStack::setCurrentPanel(QString
const & name
)
99 QTreeWidgetItem
* item
= panel_map_
.value(name
, 0);
100 LASSERT(item
, return);
102 // force on first set
103 if (list_
->currentItem() == item
)
106 list_
->setCurrentItem(item
);
110 void PanelStack::switchPanel(QTreeWidgetItem
* item
,
111 QTreeWidgetItem
* previous
)
113 // do nothing when clicked on whitespace (item=NULL)
117 // if we have a category, expand the tree and go to the
119 if (item
->childCount() > 0) {
120 item
->setExpanded(true);
121 if (previous
&& previous
->parent() != item
)
122 switchPanel( item
->child(0), previous
);
124 else if (QWidget
* w
= widget_map_
.value(item
, 0)) {
125 stack_
->setCurrentWidget(w
);
130 void PanelStack::itemSelected(QTreeWidgetItem
* item
, int)
132 // de-select the category if a child is selected
133 if (item
->childCount() > 0 && item
->child(0)->isSelected())
134 item
->setSelected(false);
138 QSize
PanelStack::sizeHint() const
140 return QSize(list_
->width() + stack_
->width(),
141 qMax(list_
->height(), stack_
->height()));
144 } // namespace frontend
147 #include "moc_PanelStack.cpp"