]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/panelstack.C
compile fixes
[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 #if QT_VERSION >= 300
38         list_->setResizeMode(QListView::AllColumns);
39 #endif
40         list_->setRootIsDecorated(true);
41
42         connect(list_, SIGNAL(currentChanged(QListViewItem*)),
43                 this, SLOT(switchPanel(QListViewItem *)));
44
45         QHBoxLayout * layout = new QHBoxLayout(this);
46         layout->addWidget(list_, 0);
47         layout->addWidget(stack_, 1);
48 }
49
50
51 void PanelStack::addCategory(string const & n, string const & parent)
52 {
53         QListViewItem * item;
54
55         QString name = toqstr(n);
56
57         if (!parent.empty()) {
58                 PanelMap::iterator it = panel_map_.find(parent);
59                 lyx::Assert(it != panel_map_.end());
60
61                 QListViewItem * before = it->second->firstChild();
62                 if (before) {
63                         while (before->nextSibling())
64                                 before = before->nextSibling();
65
66                         item = new QListViewItem(it->second, before, name);
67                 } else {
68                         item = new QListViewItem(it->second, name);
69                 }
70         } else {
71                 QListViewItem * before = list_->firstChild();
72                 if (before) {
73                         while (before->nextSibling())
74                                 before = before->nextSibling();
75                         item = new QListViewItem(list_, before, name);
76                 } else {
77                         item = new QListViewItem(list_, name);
78                 }
79         }
80
81         item->setSelectable(false);
82         item->setOpen(true);
83         panel_map_[n] = item;
84
85         // Qt is just unbelievably moronic
86         list_->setMinimumSize(QSize(150, list_->minimumHeight()));
87 }
88
89
90 void PanelStack::addPanel(QWidget * panel, string const & name, string const & parent)
91 {
92         addCategory(name, parent);
93         QListViewItem * item = panel_map_.find(name)->second;
94
95         // reset the selectability set by addCategory
96         item->setSelectable(true);
97
98         widget_map_[item] = panel;
99         stack_->addWidget(panel, -1);
100         stack_->setMinimumSize(panel->minimumSize());
101         resize(sizeHint());
102 }
103
104
105 void PanelStack::setCurrentPanel(string const & name)
106 {
107         PanelMap::const_iterator cit = panel_map_.find(name);
108         lyx::Assert(cit != panel_map_.end());
109
110         // force on first set
111         if (list_->currentItem() ==  cit->second)
112                 switchPanel(cit->second);
113
114         list_->setCurrentItem(cit->second);
115 }
116
117
118 void PanelStack::switchPanel(QListViewItem * item)
119 {
120         WidgetMap::const_iterator cit = widget_map_.find(item);
121         if (cit == widget_map_.end())
122                 return;
123
124         stack_->raiseWidget(cit->second);
125 }