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