]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/panelstack.C
This commit saves the need to check for lyx::use_gui in a number of places.
[lyx.git] / src / frontends / qt4 / 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 <QStackedWidget>
18 #include <QFontMetrics>
19 #include <QTreeWidget>
20 #include <QHBoxLayout>
21 #include <QLayout>
22
23 #include <boost/assert.hpp>
24
25 #include <iostream>
26
27 using lyx::docstring;
28
29 using std::endl;
30 using std::cout;
31
32
33 PanelStack::PanelStack(QWidget * parent)
34         : QWidget(parent)
35 {
36         list_ = new QTreeWidget(this);
37         stack_ = new QStackedWidget(this);
38
39         list_->setColumnCount(1);
40         list_->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding));
41         stack_->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding));
42
43         list_->setSortingEnabled(false);
44 //      list_->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
45 //      list_->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
46 //      list_->addColumn("");
47 //      list_->setColumnWidthMode(0, QTreeWidget::Maximum);
48
49 //      list_->setResizeMode(QTreeWidget::AllColumns);
50
51         QStringList HeaderLabels; HeaderLabels << QString("Category");
52         list_->setHeaderLabels(HeaderLabels);
53
54         connect(list_, SIGNAL(currentItemChanged (QTreeWidgetItem*, QTreeWidgetItem*)),
55                 this, SLOT(switchPanel(QTreeWidgetItem *, QTreeWidgetItem*)));
56
57         QHBoxLayout * layout = new QHBoxLayout(this);
58         layout->addWidget(list_, 0);
59         layout->addWidget(stack_, 1);
60 }
61
62
63 void PanelStack::addCategory(docstring const & n, docstring const & parent)
64 {
65         QTreeWidgetItem * item;
66
67         QString name;
68         ucs4_to_qstring(n, name);
69
70         cout << "addCategory n= " << lyx::to_utf8(n) << "   parent= " << endl;
71
72         if (parent.empty()) {
73                 item = new QTreeWidgetItem(list_);
74                 item->setText(0, name);
75                 //list_->addTopLevelItem(item);
76         }
77         else {
78                 PanelMap::iterator it = panel_map_.find(parent);
79                 //BOOST_ASSERT(it != panel_map_.end());
80                 if (it == panel_map_.end()) {
81                         addCategory(parent);
82                         it = panel_map_.find(parent);
83                 }
84                 BOOST_ASSERT(it != panel_map_.end());
85
86                 item = new QTreeWidgetItem(it->second);
87                 item->setText(0, name);
88                 //it->second->addChild(item);
89         }
90
91         panel_map_[n] = item;
92
93         list_->setFixedWidth(list_->sizeHint().width());
94 /*
95         item->setFlags(false);
96         item->setOpen(true);
97
98         // calculate the real size the current item needs in the listview
99         int itemsize = item->width(list_->fontMetrics(), list_, 0) + 10
100                    + list_->treeStepSize() * (item->depth() + 1) + list_->itemMargin();
101         // adjust the listview width to the max. itemsize
102         if (itemsize > list_->minimumWidth())
103                 list_->setMinimumWidth(itemsize);
104                 */
105 }
106
107
108 void PanelStack::addPanel(QWidget * panel, docstring const & name, docstring const & parent)
109 {
110         addCategory(name, parent);
111         QTreeWidgetItem * item = panel_map_.find(name)->second;
112
113         // reset the selectability set by addCategory
114 //      item->setSelectable(true);
115
116         widget_map_[item] = panel;
117         stack_->addWidget(panel);
118         stack_->setMinimumSize(panel->minimumSize());
119 }
120
121
122 void PanelStack::setCurrentPanel(docstring const & name)
123 {
124         PanelMap::const_iterator cit = panel_map_.find(name);
125         BOOST_ASSERT(cit != panel_map_.end());
126
127         // force on first set
128         if (list_->currentItem() ==  cit->second)
129                 switchPanel(cit->second);
130
131         list_->setCurrentItem(cit->second);
132 }
133
134
135 void PanelStack::switchPanel(QTreeWidgetItem * item,
136                              QTreeWidgetItem * /*previous*/)
137 {
138         WidgetMap::const_iterator cit = widget_map_.find(item);
139         if (cit == widget_map_.end())
140                 return;
141
142         stack_->setCurrentWidget(cit->second);
143 }
144
145 #include "panelstack_moc.cpp"