]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/PanelStack.cpp
some remnaming
[lyx.git] / src / frontends / qt4 / PanelStack.cpp
1 /**
2  * \file PanelStack.cpp
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 "debug.h"
18
19 #include <QFontMetrics>
20 #include <QStackedWidget>
21 #include <QTreeWidget>
22 #include <QHBoxLayout>
23 #include <QHeaderView>
24
25 #include <boost/assert.hpp>
26
27 #include <iostream>
28
29
30 using std::endl;
31 using std::cout;
32
33 namespace lyx {
34 namespace frontend {
35
36
37 PanelStack::PanelStack(QWidget * parent)
38         : QWidget(parent)
39 {
40         list_ = new QTreeWidget(this);
41         stack_ = new QStackedWidget(this);
42
43         list_->setColumnCount(1);
44         list_->setRootIsDecorated(false);
45         // Hide the pointless list header
46         list_->header()->hide();
47 //      QStringList HeaderLabels;
48 //      HeaderLabels << QString("Category");
49 //      list_->setHeaderLabels(HeaderLabels);
50
51         connect(list_, SIGNAL(currentItemChanged (QTreeWidgetItem*, QTreeWidgetItem*)),
52                 this, SLOT(switchPanel(QTreeWidgetItem *, QTreeWidgetItem*)));
53
54         QHBoxLayout * layout = new QHBoxLayout(this);
55         layout->addWidget(list_, 0);
56         layout->addWidget(stack_, 1);
57 }
58
59
60 void PanelStack::addCategory(docstring const & n, docstring const & parent)
61 {
62         QTreeWidgetItem * item = 0;
63         QString const name = toqstr(n);
64
65         LYXERR(Debug::GUI) << "addCategory n= " << to_utf8(n) << "   parent= " << endl;
66
67         int depth = 1;
68
69         if (parent.empty()) {
70                 item = new QTreeWidgetItem(list_);
71                 item->setText(0, name);
72         }
73         else {
74                 PanelMap::iterator it = panel_map_.find(parent);
75                 //BOOST_ASSERT(it != panel_map_.end());
76                 if (it == panel_map_.end()) {
77                         addCategory(parent);
78                         it = panel_map_.find(parent);
79                 }
80                 BOOST_ASSERT(it != panel_map_.end());
81
82                 item = new QTreeWidgetItem(it->second);
83                 item->setText(0, name);
84                 depth = 2;
85         }
86
87         panel_map_[n] = item;
88
89         QFontMetrics fm(list_->font());
90         // calculate the real size the current item needs in the listview
91         int itemsize = fm.width(name) + 10
92                 + list_->indentation() * depth;
93         // adjust the listview width to the max. itemsize
94         if (itemsize > list_->minimumWidth())
95                 list_->setMinimumWidth(itemsize);
96 }
97
98
99 void PanelStack::addPanel(QWidget * panel, docstring const & name, docstring const & parent)
100 {
101         addCategory(name, parent);
102         QTreeWidgetItem * item = panel_map_.find(name)->second;
103
104         widget_map_[item] = panel;
105         stack_->addWidget(panel);
106         stack_->setMinimumSize(panel->minimumSize());
107 }
108
109
110 void PanelStack::setCurrentPanel(docstring 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(QTreeWidgetItem * item,
124                              QTreeWidgetItem * /*previous*/)
125 {
126         WidgetMap::const_iterator cit = widget_map_.find(item);
127         if (cit == widget_map_.end())
128                 return;
129
130         stack_->setCurrentWidget(cit->second);
131 }
132
133
134 QSize PanelStack::sizeHint() const
135 {
136         return QSize(list_->width() + stack_->width(),
137                 qMax(list_->height(), stack_->height()));
138 }
139
140 } // namespace frontend
141 } // namespace lyx
142
143 #include "PanelStack_moc.cpp"