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