]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/PanelStack.cpp
* src/frontends/qt4/GuiPrefs.{cpp,h}:
[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_->setRootIsDecorated(false);
42         list_->setColumnCount(1);
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                 list_->setRootIsDecorated(true);
77         }
78
79         panel_map_[name] = item;
80
81         QFontMetrics fm(list_->font());
82                 
83         // calculate the real size the current item needs in the listview
84         int itemsize = fm.width(name) + 10
85                 + list_->indentation() * depth;
86         // adjust the listview width to the max. itemsize
87         if (itemsize > list_->minimumWidth())
88                 list_->setMinimumWidth(itemsize);
89 }
90
91
92 void PanelStack::addPanel(QWidget * panel, QString const & name, QString const & parent)
93 {
94         addCategory(name, parent);
95         QTreeWidgetItem * item = panel_map_.value(name);
96         widget_map_[item] = panel;
97         stack_->addWidget(panel);
98         stack_->setMinimumSize(panel->minimumSize());
99 }
100
101
102 void PanelStack::setCurrentPanel(QString const & name)
103 {
104         QTreeWidgetItem * item = panel_map_.value(name, 0);
105         BOOST_ASSERT(item);
106
107         // force on first set
108         if (list_->currentItem() == item)
109                 switchPanel(item);
110
111         list_->setCurrentItem(item);
112 }
113
114
115 void PanelStack::switchPanel(QTreeWidgetItem * item,
116                              QTreeWidgetItem * /*previous*/)
117 {
118         // if we have a category, expand the tree and go to the
119         // first item
120         if (item->childCount() > 0) {
121                 item->setExpanded(true);
122                 list_->setCurrentItem(item->child(0));
123         }
124         if (QWidget * w = widget_map_.value(item, 0))
125                 stack_->setCurrentWidget(w);
126 }
127
128
129 QSize PanelStack::sizeHint() const
130 {
131         return QSize(list_->width() + stack_->width(),
132                 qMax(list_->height(), stack_->height()));
133 }
134
135 } // namespace frontend
136 } // namespace lyx
137
138 #include "PanelStack_moc.cpp"