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