]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/PanelStack.cpp
Substack support for XHTML.
[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 <QHBoxLayout>
21 #include <QHeaderView>
22 #include <QStackedWidget>
23 #include <QTreeWidget>
24
25 #include "support/lassert.h"
26
27 using namespace std;
28
29 namespace lyx {
30 namespace frontend {
31
32
33 PanelStack::PanelStack(QWidget * parent)
34         : QWidget(parent)
35 {
36         list_ = new QTreeWidget(this);
37         stack_ = new QStackedWidget(this);
38
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());
45
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)));
50
51         QHBoxLayout * layout = new QHBoxLayout(this);
52         layout->addWidget(list_, 0);
53         layout->addWidget(stack_, 1);
54 }
55
56
57 void PanelStack::addCategory(QString const & name, QString const & parent)
58 {
59         QTreeWidgetItem * item = 0;
60
61         LYXERR(Debug::GUI, "addCategory n= " << name << "   parent= ");
62
63         int depth = 1;
64
65         if (parent.isEmpty()) {
66                 item = new QTreeWidgetItem(list_);
67                 item->setText(0, name);
68         }
69         else {
70                 if (!panel_map_.contains(parent))
71                         addCategory(parent);
72                 item = new QTreeWidgetItem(panel_map_.value(parent));
73                 item->setText(0, name);
74                 depth = 2;
75                 list_->setRootIsDecorated(true);
76         }
77
78         panel_map_[name] = item;
79
80         QFontMetrics fm(list_->font());
81                 
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);
87 }
88
89
90 void PanelStack::addPanel(QWidget * panel, QString const & name, QString const & parent)
91 {
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());
97 }
98
99
100 void PanelStack::showPanel(QString const & name, bool show)
101 {
102         QTreeWidgetItem * item = panel_map_.value(name, 0);
103         LASSERT(item, return);
104
105         item->setHidden(!show);
106 }
107
108
109 void PanelStack::setCurrentPanel(QString const & name)
110 {
111         QTreeWidgetItem * item = panel_map_.value(name, 0);
112         LASSERT(item, return);
113
114         // force on first set
115         if (list_->currentItem() == item)
116                 switchPanel(item);
117
118         list_->setCurrentItem(item);
119 }
120
121
122 bool PanelStack::isCurrentPanel(QString const & name) const
123 {
124         QTreeWidgetItem * item = panel_map_.value(name, 0);
125         LASSERT(item, return false);
126
127         return (list_->currentItem() == item);
128 }
129
130
131 void PanelStack::switchPanel(QTreeWidgetItem * item,
132                              QTreeWidgetItem * previous)
133 {
134         // do nothing when clicked on whitespace (item=NULL)
135         if( !item )
136                 return;
137
138         // if we have a category, expand the tree and go to the
139         // first item
140         if (item->childCount() > 0) {
141                 item->setExpanded(true);
142                 if (previous && previous->parent() != item)
143                         switchPanel( item->child(0), previous );
144         }
145         else if (QWidget * w = widget_map_.value(item, 0)) {
146                 stack_->setCurrentWidget(w);
147         }
148 }
149
150
151 void PanelStack::itemSelected(QTreeWidgetItem * item, int)
152 {
153         // de-select the category if a child is selected
154         if (item->childCount() > 0 && item->child(0)->isSelected())
155                 item->setSelected(false);
156 }
157
158
159 QSize PanelStack::sizeHint() const
160 {
161         return QSize(list_->width() + stack_->width(),
162                 qMax(list_->height(), stack_->height()));
163 }
164
165 } // namespace frontend
166 } // namespace lyx
167
168 #include "moc_PanelStack.cpp"