]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/PanelStack.cpp
rename assert.h to lassert.h
[lyx.git] / src / frontends / qt4 / PanelStack.cpp
1 /**
2  * \file PanelStack.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author John Levon
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "PanelStack.h"
14
15 #include "qt_helpers.h"
16
17 #include "support/debug.h"
18
19 #include <QFontMetrics>
20 #include <QStackedWidget>
21 #include <QTreeWidget>
22 #include <QHBoxLayout>
23 #include <QHeaderView>
24
25 #include "support/lassert.h"
26
27 #include <iostream>
28
29 using namespace std;
30
31 namespace lyx {
32 namespace frontend {
33
34
35 PanelStack::PanelStack(QWidget * parent)
36         : QWidget(parent)
37 {
38         list_ = new QTreeWidget(this);
39         stack_ = new QStackedWidget(this);
40
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);
48
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)));
53
54         QHBoxLayout * layout = new QHBoxLayout(this);
55         layout->addWidget(list_, 0);
56         layout->addWidget(stack_, 1);
57 }
58
59
60 void PanelStack::addCategory(QString const & name, QString const & parent)
61 {
62         QTreeWidgetItem * item = 0;
63
64         LYXERR(Debug::GUI, "addCategory n= " << fromqstr(name) << "   parent= ");
65
66         int depth = 1;
67
68         if (parent.isEmpty()) {
69                 item = new QTreeWidgetItem(list_);
70                 item->setText(0, name);
71         }
72         else {
73                 if (!panel_map_.contains(parent))
74                         addCategory(parent);
75                 item = new QTreeWidgetItem(panel_map_.value(parent));
76                 item->setText(0, name);
77                 depth = 2;
78                 list_->setRootIsDecorated(true);
79         }
80
81         panel_map_[name] = item;
82
83         QFontMetrics fm(list_->font());
84                 
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);
91 }
92
93
94 void PanelStack::addPanel(QWidget * panel, QString const & name, QString const & parent)
95 {
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());
101 }
102
103
104 void PanelStack::setCurrentPanel(QString const & name)
105 {
106         QTreeWidgetItem * item = panel_map_.value(name, 0);
107         LASSERT(item, /**/);
108
109         // force on first set
110         if (list_->currentItem() == item)
111                 switchPanel(item);
112
113         list_->setCurrentItem(item);
114 }
115
116
117 void PanelStack::switchPanel(QTreeWidgetItem * item,
118                              QTreeWidgetItem * previous)
119 {
120         // if we have a category, expand the tree and go to the
121         // first item
122         if (item->childCount() > 0) {
123                 item->setExpanded(true);
124                 if (previous != item->child(0))
125                         list_->setCurrentItem(item->child(0));
126         }
127         if (QWidget * w = widget_map_.value(item, 0))
128                 stack_->setCurrentWidget(w);
129 }
130
131
132 void PanelStack::itemSelected(QTreeWidgetItem * item, int)
133 {
134         // de-select the category if a child is selected
135         if (item->childCount() > 0 && item->child(0)->isSelected())
136                 item->setSelected(false);
137 }
138
139
140 QSize PanelStack::sizeHint() const
141 {
142         return QSize(list_->width() + stack_->width(),
143                 qMax(list_->height(), stack_->height()));
144 }
145
146 } // namespace frontend
147 } // namespace lyx
148
149 #include "PanelStack_moc.cpp"