]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/PanelStack.cpp
less string conversions as long as we stay in the frontend
[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(QString const & name, QString const & parent)
59 {
60         QTreeWidgetItem * item = 0;
61
62         LYXERR(Debug::GUI, "addCategory n= " << fromqstr(name) << "   parent= ");
63
64         int depth = 1;
65
66         if (parent.isEmpty()) {
67                 item = new QTreeWidgetItem(list_);
68                 item->setText(0, name);
69         }
70         else {
71                 if (!panel_map_.contains(parent))
72                         addCategory(parent);
73                 item = new QTreeWidgetItem(panel_map_.value(parent));
74                 item->setText(0, name);
75                 depth = 2;
76         }
77
78         panel_map_[name] = item;
79
80         QFontMetrics fm(list_->font());
81         // calculate the real size the current item needs in the listview
82         int itemsize = fm.width(name) + 10
83                 + list_->indentation() * depth;
84         // adjust the listview width to the max. itemsize
85         if (itemsize > list_->minimumWidth())
86                 list_->setMinimumWidth(itemsize);
87 }
88
89
90 void PanelStack::addPanel(QWidget * panel, QString const & name, QString const & parent)
91 {
92         addCategory(name, parent);
93         QTreeWidgetItem * item = panel_map_.value(name);
94         widget_map_[item] = panel;
95         stack_->addWidget(panel);
96         stack_->setMinimumSize(panel->minimumSize());
97 }
98
99
100 void PanelStack::setCurrentPanel(QString const & name)
101 {
102         QTreeWidgetItem * item = panel_map_.value(name, 0);
103         BOOST_ASSERT(item);
104
105         // force on first set
106         if (list_->currentItem() == item)
107                 switchPanel(item);
108
109         list_->setCurrentItem(item);
110 }
111
112
113 void PanelStack::switchPanel(QTreeWidgetItem * item,
114                              QTreeWidgetItem * /*previous*/)
115 {
116         if (QWidget * w = widget_map_.value(item, 0))
117                 stack_->setCurrentWidget(w);
118 }
119
120
121 QSize PanelStack::sizeHint() const
122 {
123         return QSize(list_->width() + stack_->width(),
124                 qMax(list_->height(), stack_->height()));
125 }
126
127 } // namespace frontend
128 } // namespace lyx
129
130 #include "PanelStack_moc.cpp"