]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/panelstack.C
enable Font cache only for MacOSX and inline width() for other platform.
[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 <QStackedWidget>
18 #include <QFontMetrics>
19 #include <QTreeWidget>
20 #include <QHBoxLayout>
21 #include <QLayout>
22
23 #include <boost/assert.hpp>
24
25 using std::string;
26
27 #include <iostream>
28 using std::endl;
29 using std::cout;
30
31
32 PanelStack::PanelStack(QWidget * parent)
33         : QWidget(parent)
34 {
35         list_ = new QTreeWidget(this);
36         stack_ = new QStackedWidget(this);
37
38         list_->setColumnCount(1);
39         list_->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding));
40         stack_->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding));
41
42         list_->setSortingEnabled(false);
43 //      list_->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
44 //      list_->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
45 //      list_->addColumn("");
46 //      list_->setColumnWidthMode(0, QTreeWidget::Maximum);
47
48 //      list_->setResizeMode(QTreeWidget::AllColumns);
49
50         QStringList HeaderLabels; HeaderLabels << QString("Category");
51         list_->setHeaderLabels(HeaderLabels);
52
53         connect(list_, SIGNAL(currentItemChanged (QTreeWidgetItem*, QTreeWidgetItem*)),
54                 this, SLOT(switchPanel(QTreeWidgetItem *, QTreeWidgetItem*)));
55
56         QHBoxLayout * layout = new QHBoxLayout(this);
57         layout->addWidget(list_, 0);
58         layout->addWidget(stack_, 1);
59 }
60
61
62 void PanelStack::addCategory(string const & n, string const & parent)
63 {
64         QTreeWidgetItem * item;
65
66         QString name = toqstr(n);
67
68         cout << "addCategory n= " << n << "   parent= " << endl;
69
70         if (parent.empty()) {
71                 item = new QTreeWidgetItem(list_);
72                 item->setText(0, name);
73                 //list_->addTopLevelItem(item);
74         }
75         else {
76                 PanelMap::iterator it = panel_map_.find(parent);
77                 //BOOST_ASSERT(it != panel_map_.end());
78                 if (it == panel_map_.end()) {
79                         addCategory(parent);
80                         it = panel_map_.find(parent);
81                 }
82                 BOOST_ASSERT(it != panel_map_.end());
83
84                 item = new QTreeWidgetItem(it->second);
85                 item->setText(0, name);
86                 //it->second->addChild(item);
87         }
88
89         panel_map_[n] = item;
90
91         list_->setFixedWidth(list_->sizeHint().width());
92 /*
93         item->setFlags(false);
94         item->setOpen(true);
95
96         // calculate the real size the current item needs in the listview
97         int itemsize = item->width(list_->fontMetrics(), list_, 0) + 10
98                    + list_->treeStepSize() * (item->depth() + 1) + list_->itemMargin();
99         // adjust the listview width to the max. itemsize
100         if (itemsize > list_->minimumWidth())
101                 list_->setMinimumWidth(itemsize);
102                 */
103 }
104
105
106 void PanelStack::addPanel(QWidget * panel, string const & name, string const & parent)
107 {
108         addCategory(name, parent);
109         QTreeWidgetItem * item = panel_map_.find(name)->second;
110
111         // reset the selectability set by addCategory
112 //      item->setSelectable(true);
113
114         widget_map_[item] = panel;
115         stack_->addWidget(panel);
116         stack_->setMinimumSize(panel->minimumSize());
117 }
118
119
120 void PanelStack::setCurrentPanel(string const & name)
121 {
122         PanelMap::const_iterator cit = panel_map_.find(name);
123         BOOST_ASSERT(cit != panel_map_.end());
124
125         // force on first set
126         if (list_->currentItem() ==  cit->second)
127                 switchPanel(cit->second);
128
129         list_->setCurrentItem(cit->second);
130 }
131
132
133 void PanelStack::switchPanel(QTreeWidgetItem * item,
134                              QTreeWidgetItem * /*previous*/)
135 {
136         WidgetMap::const_iterator cit = widget_map_.find(item);
137         if (cit == widget_map_.end())
138                 return;
139
140         stack_->setCurrentWidget(cit->second);
141 }
142
143 #include "panelstack_moc.cpp"