]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/panelstack.C
code cosmetics whitespace, unneeded lyx:: qualification, indentation, unneeded destru...
[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 "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         // Hide the pointless list header
45         list_->header()->hide();
46 //      QStringList HeaderLabels;
47 //      HeaderLabels << QString("Category");
48 //      list_->setHeaderLabels(HeaderLabels);
49
50         connect(list_, SIGNAL(currentItemChanged (QTreeWidgetItem*, QTreeWidgetItem*)),
51                 this, SLOT(switchPanel(QTreeWidgetItem *, QTreeWidgetItem*)));
52
53         QHBoxLayout * layout = new QHBoxLayout(this);
54         layout->addWidget(list_, 0);
55         layout->addWidget(stack_, 1);
56 }
57
58
59 void PanelStack::addCategory(docstring const & n, docstring const & parent)
60 {
61         QTreeWidgetItem * item = 0;
62         QString const name = toqstr(n);
63
64         LYXERR(Debug::GUI) << "addCategory n= " << to_utf8(n) << "   parent= " << endl;
65
66         int depth = 1;
67
68         if (parent.empty()) {
69                 item = new QTreeWidgetItem(list_);
70                 item->setText(0, name);
71         }
72         else {
73                 PanelMap::iterator it = panel_map_.find(parent);
74                 //BOOST_ASSERT(it != panel_map_.end());
75                 if (it == panel_map_.end()) {
76                         addCategory(parent);
77                         it = panel_map_.find(parent);
78                 }
79                 BOOST_ASSERT(it != panel_map_.end());
80
81                 item = new QTreeWidgetItem(it->second);
82                 item->setText(0, name);
83                 depth = 2;
84         }
85
86         panel_map_[n] = item;
87
88         QFontMetrics fm(list_->font());
89         // calculate the real size the current item needs in the listview
90         int itemsize = fm.width(name) + 10
91                 + list_->indentation() * depth;
92         // adjust the listview width to the max. itemsize
93         if (itemsize > list_->minimumWidth())
94                 list_->setMinimumWidth(itemsize);
95 }
96
97
98 void PanelStack::addPanel(QWidget * panel, docstring const & name, docstring const & parent)
99 {
100         addCategory(name, parent);
101         QTreeWidgetItem * item = panel_map_.find(name)->second;
102
103         widget_map_[item] = panel;
104         stack_->addWidget(panel);
105         stack_->setMinimumSize(panel->minimumSize());
106 }
107
108
109 void PanelStack::setCurrentPanel(docstring const & name)
110 {
111         PanelMap::const_iterator cit = panel_map_.find(name);
112         BOOST_ASSERT(cit != panel_map_.end());
113
114         // force on first set
115         if (list_->currentItem() ==  cit->second)
116                 switchPanel(cit->second);
117
118         list_->setCurrentItem(cit->second);
119 }
120
121
122 void PanelStack::switchPanel(QTreeWidgetItem * item,
123                              QTreeWidgetItem * /*previous*/)
124 {
125         WidgetMap::const_iterator cit = widget_map_.find(item);
126         if (cit == widget_map_.end())
127                 return;
128
129         stack_->setCurrentWidget(cit->second);
130 }
131
132
133 QSize PanelStack::sizeHint() const
134 {
135         return QSize(list_->width() + stack_->width(),
136                 qMax(list_->height(), stack_->height()));
137 }
138
139 } // namespace frontend
140 } // namespace lyx
141
142 #include "panelstack_moc.cpp"