]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/panelstack.C
* Painter.h:
[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 <QTreeWidget>
19 #include <QHBoxLayout>
20 #include <QHeaderView>
21
22 #include <boost/assert.hpp>
23
24 #include <iostream>
25
26 using lyx::docstring;
27
28 using std::endl;
29 using std::cout;
30
31
32 PanelStack::PanelStack(QWidget * parent)
33         : QWidget(parent)
34 {
35         list_ = new QTreeWidget(this);
36         stack_ = new QStackedWidget(this);
37
38         list_->setColumnCount(1);
39         QStringList HeaderLabels; HeaderLabels << QString("Category");
40         list_->setHeaderLabels(HeaderLabels);
41
42         connect(list_, SIGNAL(currentItemChanged (QTreeWidgetItem*, QTreeWidgetItem*)),
43                 this, SLOT(switchPanel(QTreeWidgetItem *, QTreeWidgetItem*)));
44
45         QHBoxLayout * layout = new QHBoxLayout(this);
46         layout->addWidget(list_, 0);
47         layout->addWidget(stack_, 1);
48 }
49
50
51 void PanelStack::addCategory(docstring const & n, docstring const & parent)
52 {
53         QTreeWidgetItem * item;
54
55         QString name;
56         lyx::ucs4_to_qstring(n, name);
57
58         cout << "addCategory n= " << lyx::to_utf8(n) << "   parent= " << endl;
59
60         if (parent.empty()) {
61                 item = new QTreeWidgetItem(list_);
62                 item->setText(0, name);
63         }
64         else {
65                 PanelMap::iterator it = panel_map_.find(parent);
66                 //BOOST_ASSERT(it != panel_map_.end());
67                 if (it == panel_map_.end()) {
68                         addCategory(parent);
69                         it = panel_map_.find(parent);
70                 }
71                 BOOST_ASSERT(it != panel_map_.end());
72
73                 item = new QTreeWidgetItem(it->second);
74                 item->setText(0, name);
75         }
76
77         panel_map_[n] = item;
78
79         list_->setMinimumWidth(list_->header()->sectionSize(0) + list_->indentation());
80 }
81
82
83 void PanelStack::addPanel(QWidget * panel, docstring const & name, docstring const & parent)
84 {
85         addCategory(name, parent);
86         QTreeWidgetItem * item = panel_map_.find(name)->second;
87
88         widget_map_[item] = panel;
89         stack_->addWidget(panel);
90         stack_->setMinimumSize(panel->minimumSize());
91 }
92
93
94 void PanelStack::setCurrentPanel(docstring const & name)
95 {
96         PanelMap::const_iterator cit = panel_map_.find(name);
97         BOOST_ASSERT(cit != panel_map_.end());
98
99         // force on first set
100         if (list_->currentItem() ==  cit->second)
101                 switchPanel(cit->second);
102
103         list_->setCurrentItem(cit->second);
104 }
105
106
107 void PanelStack::switchPanel(QTreeWidgetItem * item,
108                              QTreeWidgetItem * /*previous*/)
109 {
110         WidgetMap::const_iterator cit = widget_map_.find(item);
111         if (cit == widget_map_.end())
112                 return;
113
114         stack_->setCurrentWidget(cit->second);
115 }
116
117
118 QSize PanelStack::sizeHint() const
119 {
120         return QSize(list_->width() + stack_->width(),
121                 qMax(list_->height(), stack_->height()));
122 }
123
124 #include "panelstack_moc.cpp"