]> git.lyx.org Git - lyx.git/blob - src/frontends/qt3/panelstack.C
Extracted from r14281
[lyx.git] / src / frontends / qt3 / panelstack.C
1 /**
2  * \file panelstack.C
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 <qwidgetstack.h>
18 #include <qfontmetrics.h>
19 #include <qlayout.h>
20 #include <qlistview.h>
21
22 #include <boost/assert.hpp>
23
24 using std::string;
25
26
27 PanelStack::PanelStack(QWidget * parent, const char * name)
28         : QWidget(parent, name)
29 {
30         list_ = new QListView(this);
31         stack_ = new QWidgetStack(this);
32         list_->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding));
33         stack_->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding));
34
35         list_->setSorting(-1);
36         list_->setHScrollBarMode(QScrollView::AlwaysOff);
37         list_->setVScrollBarMode(QScrollView::AlwaysOff);
38         list_->addColumn("");
39         list_->setColumnWidthMode(0, QListView::Maximum);
40         list_->setResizeMode(QListView::AllColumns);
41         list_->setRootIsDecorated(true);
42         QWidget * w = static_cast<QWidget*>(list_->child("list view header"));
43         if (w)
44                 w->hide();
45
46         connect(list_, SIGNAL(currentChanged(QListViewItem*)),
47                 this, SLOT(switchPanel(QListViewItem *)));
48
49         QHBoxLayout * layout = new QHBoxLayout(this);
50         layout->addWidget(list_, 0);
51         layout->addWidget(stack_, 1);
52 }
53
54
55 void PanelStack::addCategory(string const & n, string const & parent)
56 {
57         QListViewItem * item;
58
59         QString name = toqstr(n);
60
61         if (!parent.empty()) {
62                 PanelMap::iterator it = panel_map_.find(parent);
63                 BOOST_ASSERT(it != panel_map_.end());
64
65                 QListViewItem * before = it->second->firstChild();
66                 if (before) {
67                         while (before->nextSibling())
68                                 before = before->nextSibling();
69
70                         item = new QListViewItem(it->second, before, name);
71                 } else {
72                         item = new QListViewItem(it->second, name);
73                 }
74         } else {
75                 QListViewItem * before = list_->firstChild();
76                 if (before) {
77                         while (before->nextSibling())
78                                 before = before->nextSibling();
79                         item = new QListViewItem(list_, before, name);
80                 } else {
81                         item = new QListViewItem(list_, name);
82                 }
83         }
84
85         item->setSelectable(false);
86         item->setOpen(true);
87         panel_map_[n] = item;
88
89         // calculate the real size the current item needs in the listview
90         QFontMetrics fm(list_->font());
91         int itemsize = item->width(fm, list_, 0) + 10
92                    + list_->treeStepSize() * (item->depth() + 1) + list_->itemMargin();
93         // adjust the listview width to the max. itemsize
94         if (itemsize > list_->minimumWidth())
95                 list_->setMinimumWidth(itemsize);
96 }
97
98
99 void PanelStack::addPanel(QWidget * panel, string const & name, string const & parent)
100 {
101         addCategory(name, parent);
102         QListViewItem * item = panel_map_.find(name)->second;
103
104         // reset the selectability set by addCategory
105         item->setSelectable(true);
106
107         widget_map_[item] = panel;
108         stack_->addWidget(panel, -1);
109         stack_->setMinimumSize(panel->minimumSize());
110         resize(sizeHint());
111 }
112
113
114 void PanelStack::setCurrentPanel(string const & name)
115 {
116         PanelMap::const_iterator cit = panel_map_.find(name);
117         BOOST_ASSERT(cit != panel_map_.end());
118
119         // force on first set
120         if (list_->currentItem() ==  cit->second)
121                 switchPanel(cit->second);
122
123         list_->setCurrentItem(cit->second);
124 }
125
126
127 void PanelStack::switchPanel(QListViewItem * item)
128 {
129         WidgetMap::const_iterator cit = widget_map_.find(item);
130         if (cit == widget_map_.end())
131                 return;
132
133         stack_->raiseWidget(cit->second);
134 }