]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/PanelStack.cpp
Fix for preferences crash by Vincent van Ravesteijn.
[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 "support/lassert.h"
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         connect(list_, SIGNAL(itemClicked (QTreeWidgetItem*, int)),
52                 this, SLOT(itemSelected(QTreeWidgetItem *, int)));
53
54         QHBoxLayout * layout = new QHBoxLayout(this);
55         layout->addWidget(list_, 0);
56         layout->addWidget(stack_, 1);
57 }
58
59
60 void PanelStack::addCategory(QString const & name, QString const & parent)
61 {
62         QTreeWidgetItem * item = 0;
63
64         LYXERR(Debug::GUI, "addCategory n= " << name << "   parent= ");
65
66         int depth = 1;
67
68         if (parent.isEmpty()) {
69                 item = new QTreeWidgetItem(list_);
70                 item->setText(0, name);
71         }
72         else {
73                 if (!panel_map_.contains(parent))
74                         addCategory(parent);
75                 item = new QTreeWidgetItem(panel_map_.value(parent));
76                 item->setText(0, name);
77                 depth = 2;
78                 list_->setRootIsDecorated(true);
79         }
80
81         panel_map_[name] = item;
82
83         QFontMetrics fm(list_->font());
84                 
85         // calculate the real size the current item needs in the listview
86         int itemsize = fm.width(name) + 10
87                 + list_->indentation() * depth;
88         // adjust the listview width to the max. itemsize
89         if (itemsize > list_->minimumWidth())
90                 list_->setMinimumWidth(itemsize);
91 }
92
93
94 void PanelStack::addPanel(QWidget * panel, QString const & name, QString const & parent)
95 {
96         addCategory(name, parent);
97         QTreeWidgetItem * item = panel_map_.value(name);
98         widget_map_[item] = panel;
99         stack_->addWidget(panel);
100         stack_->setMinimumSize(panel->minimumSize());
101 }
102
103
104 void PanelStack::setCurrentPanel(QString const & name)
105 {
106         QTreeWidgetItem * item = panel_map_.value(name, 0);
107         LASSERT(item, /**/);
108
109         // force on first set
110         if (list_->currentItem() == item)
111                 switchPanel(item);
112
113         list_->setCurrentItem(item);
114 }
115
116
117 void PanelStack::switchPanel(QTreeWidgetItem * item,
118                              QTreeWidgetItem * previous)
119 {
120         // do nothing when clicked on whitespace (item=NULL)
121         if( !item )
122                 return;
123
124         // if we have a category, expand the tree and go to the
125         // first item
126         if (item->childCount() > 0) {
127                 item->setExpanded(true);
128                 if (previous && previous->parent() != item)
129                         switchPanel( item->child(0), previous );
130         }
131         else
132                 if (QWidget * w = widget_map_.value(item, 0))
133                         stack_->setCurrentWidget(w);
134 }
135
136
137 void PanelStack::itemSelected(QTreeWidgetItem * item, int)
138 {
139         // de-select the category if a child is selected
140         if (item->childCount() > 0 && item->child(0)->isSelected())
141                 item->setSelected(false);
142 }
143
144
145 QSize PanelStack::sizeHint() const
146 {
147         return QSize(list_->width() + stack_->width(),
148                 qMax(list_->height(), stack_->height()));
149 }
150
151 } // namespace frontend
152 } // namespace lyx
153
154 #include "PanelStack_moc.cpp"