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