]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/PanelStack.cpp
If we are in a closeEvent, we don't want to close all buffers, because these may...
[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 <QHBoxLayout>
21 #include <QHeaderView>
22 #include <QStackedWidget>
23 #include <QTreeWidget>
24
25 #include "support/lassert.h"
26
27 using namespace std;
28
29 namespace lyx {
30 namespace frontend {
31
32
33 PanelStack::PanelStack(QWidget * parent)
34         : QWidget(parent)
35 {
36         list_ = new QTreeWidget(this);
37         stack_ = new QStackedWidget(this);
38
39         list_->setRootIsDecorated(false);
40         list_->setColumnCount(1);
41         list_->header()->hide();
42
43         connect(list_, SIGNAL(currentItemChanged(QTreeWidgetItem*, QTreeWidgetItem*)),
44                 this, SLOT(switchPanel(QTreeWidgetItem *, QTreeWidgetItem*)));
45         connect(list_, SIGNAL(itemClicked (QTreeWidgetItem*, int)),
46                 this, SLOT(itemSelected(QTreeWidgetItem *, int)));
47
48         QHBoxLayout * layout = new QHBoxLayout(this);
49         layout->addWidget(list_, 0);
50         layout->addWidget(stack_, 1);
51 }
52
53
54 void PanelStack::addCategory(QString const & name, QString const & parent)
55 {
56         QTreeWidgetItem * item = 0;
57
58         LYXERR(Debug::GUI, "addCategory n= " << name << "   parent= ");
59
60         int depth = 1;
61
62         if (parent.isEmpty()) {
63                 item = new QTreeWidgetItem(list_);
64                 item->setText(0, name);
65         }
66         else {
67                 if (!panel_map_.contains(parent))
68                         addCategory(parent);
69                 item = new QTreeWidgetItem(panel_map_.value(parent));
70                 item->setText(0, name);
71                 depth = 2;
72                 list_->setRootIsDecorated(true);
73         }
74
75         panel_map_[name] = item;
76
77         QFontMetrics fm(list_->font());
78                 
79         // calculate the real size the current item needs in the listview
80         int itemsize = fm.width(name) + 10 + list_->indentation() * depth;
81         // adjust the listview width to the max. itemsize
82         if (itemsize > list_->minimumWidth())
83                 list_->setMinimumWidth(itemsize);
84 }
85
86
87 void PanelStack::addPanel(QWidget * panel, QString const & name, QString const & parent)
88 {
89         addCategory(name, parent);
90         QTreeWidgetItem * item = panel_map_.value(name);
91         widget_map_[item] = panel;
92         stack_->addWidget(panel);
93         stack_->setMinimumSize(panel->minimumSize());
94 }
95
96
97 void PanelStack::setCurrentPanel(QString const & name)
98 {
99         QTreeWidgetItem * item = panel_map_.value(name, 0);
100         LASSERT(item, return);
101
102         // force on first set
103         if (list_->currentItem() == item)
104                 switchPanel(item);
105
106         list_->setCurrentItem(item);
107 }
108
109
110 void PanelStack::switchPanel(QTreeWidgetItem * item,
111                              QTreeWidgetItem * previous)
112 {
113         // do nothing when clicked on whitespace (item=NULL)
114         if( !item )
115                 return;
116
117         // if we have a category, expand the tree and go to the
118         // first item
119         if (item->childCount() > 0) {
120                 item->setExpanded(true);
121                 if (previous && previous->parent() != item)
122                         switchPanel( item->child(0), previous );
123         }
124         else if (QWidget * w = widget_map_.value(item, 0)) {
125                 stack_->setCurrentWidget(w);
126         }
127 }
128
129
130 void PanelStack::itemSelected(QTreeWidgetItem * item, int)
131 {
132         // de-select the category if a child is selected
133         if (item->childCount() > 0 && item->child(0)->isSelected())
134                 item->setSelected(false);
135 }
136
137
138 QSize PanelStack::sizeHint() const
139 {
140         return QSize(list_->width() + stack_->width(),
141                 qMax(list_->height(), stack_->height()));
142 }
143
144 } // namespace frontend
145 } // namespace lyx
146
147 #include "moc_PanelStack.cpp"