]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/PanelStack.cpp
On Linux show in crash message box the backtrace
[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 "GuiApplication.h"
16 #include "qt_helpers.h"
17
18 #include "support/debug.h"
19 #include "support/foreach.h"
20 #include "support/lassert.h"
21
22 #include <QAbstractButton>
23 #include <QApplication>
24 #include <QComboBox>
25 #include <QFontMetrics>
26 #include <QGroupBox>
27 #include <QHideEvent>
28 #include <QHash>
29 #include <QHBoxLayout>
30 #include <QHeaderView>
31 #include <QLabel>
32 #include <QLineEdit>
33 #include <QListWidget>
34 #include <QPalette>
35 #include <QPushButton>
36 #include <QStackedWidget>
37 #include <QTimer>
38 #include <QTreeWidget>
39 #include <QVBoxLayout>
40
41 using namespace std;
42
43 namespace lyx {
44 namespace frontend {
45
46
47 PanelStack::PanelStack(QWidget * parent)
48         : QWidget(parent)
49 {
50         delay_search_ = new QTimer(this);
51         search_ = new FancyLineEdit(this);
52         list_ = new QTreeWidget(this);
53         stack_ = new QStackedWidget(this);
54
55         // Configure the timer
56         delay_search_->setSingleShot(true);
57         connect(delay_search_, SIGNAL(timeout()), this, SLOT(search()));
58
59         // Configure tree
60         list_->setRootIsDecorated(false);
61         list_->setColumnCount(1);
62         list_->header()->hide();
63         setSectionResizeMode(list_->header(), QHeaderView::ResizeToContents);
64         list_->header()->setStretchLastSection(false);
65         list_->setMinimumSize(list_->viewport()->size());
66
67         connect(list_, SIGNAL(currentItemChanged(QTreeWidgetItem*, QTreeWidgetItem*)),
68                 this, SLOT(switchPanel(QTreeWidgetItem *, QTreeWidgetItem*)));
69         connect(list_, SIGNAL(itemClicked (QTreeWidgetItem*, int)),
70                 this, SLOT(itemSelected(QTreeWidgetItem *, int)));
71
72         // Configure the search box
73 #if QT_VERSION >= 0x040700
74         search_->setPlaceholderText(qt_("Search"));
75 #endif
76
77 #if QT_VERSION >= 0x040600
78         search_->setButtonPixmap(FancyLineEdit::Right, getPixmap("images/", "editclear", "png"));
79         search_->setButtonVisible(FancyLineEdit::Right, true);
80         search_->setButtonToolTip(FancyLineEdit::Right, qt_("Clear text"));
81         search_->setAutoHideButton(FancyLineEdit::Right, true);
82 #endif
83         connect(search_, SIGNAL(rightButtonClicked()), this, SLOT(resetSearch()));
84         connect(search_, SIGNAL(textEdited(QString)), this, SLOT(filterChanged(QString)));
85
86         // Create the output layout, horizontal plus a VBox on the left with the search
87         // box and the tree
88         QVBoxLayout * left_layout = new QVBoxLayout;
89         left_layout->addWidget(search_, 0);
90         left_layout->addWidget(list_, 1);
91
92         QHBoxLayout * main_layout = new QHBoxLayout(this);
93         main_layout->addLayout(left_layout, 0);
94         main_layout->addWidget(stack_, 1);
95 }
96
97
98 void PanelStack::addCategory(QString const & name, QString const & parent)
99 {
100         QTreeWidgetItem * item = 0;
101
102         LYXERR(Debug::GUI, "addCategory n= " << name << "   parent= ");
103
104         int depth = 1;
105
106         if (parent.isEmpty()) {
107                 item = new QTreeWidgetItem(list_);
108                 item->setText(0, qt_(name));
109         }
110         else {
111                 if (!panel_map_.contains(parent))
112                         addCategory(parent);
113                 item = new QTreeWidgetItem(panel_map_.value(parent));
114                 item->setText(0, qt_(name));
115                 depth = 2;
116                 list_->setRootIsDecorated(true);
117         }
118
119         panel_map_[name] = item;
120
121         QFontMetrics fm(list_->font());
122                 
123         // calculate the real size the current item needs in the listview
124         int itemsize = fm.width(name) + 10 + list_->indentation() * depth;
125         // adjust the listview width to the max. itemsize
126         if (itemsize > list_->minimumWidth())
127                 list_->setMinimumWidth(itemsize);
128 }
129
130
131 void PanelStack::addPanel(QWidget * panel, QString const & name, QString const & parent)
132 {
133         addCategory(name, parent);
134         QTreeWidgetItem * item = panel_map_.value(name);
135         widget_map_[item] = panel;
136         stack_->addWidget(panel);
137         stack_->setMinimumSize(panel->minimumSize());
138 }
139
140
141 void PanelStack::showPanel(QString const & name, bool show)
142 {
143         QTreeWidgetItem * item = panel_map_.value(name, 0);
144         LASSERT(item, return);
145
146         item->setHidden(!show);
147 }
148
149
150 void PanelStack::setCurrentPanel(QString const & name)
151 {
152         QTreeWidgetItem * item = panel_map_.value(name, 0);
153         LASSERT(item, return);
154
155         // force on first set
156         if (list_->currentItem() == item)
157                 switchPanel(item);
158
159         list_->setCurrentItem(item);
160 }
161
162
163 bool PanelStack::isCurrentPanel(QString const & name) const
164 {
165         QTreeWidgetItem * item = panel_map_.value(name, 0);
166         LASSERT(item, return false);
167
168         return (list_->currentItem() == item);
169 }
170
171
172 void PanelStack::switchPanel(QTreeWidgetItem * item,
173                              QTreeWidgetItem * previous)
174 {
175         // do nothing when clicked on whitespace (item=NULL)
176         if (!item)
177                 return;
178
179         // if we have a category, expand the tree and go to the
180         // first enabled item
181         if (item->childCount() > 0) {
182                 item->setExpanded(true);
183                 if (previous && previous->parent() != item) {
184                         // Looks for a child not disabled
185                         for (int i = 0; i < item->childCount(); ++i) {
186                                 if (item->child(i)->flags() & Qt::ItemIsEnabled) {
187                                         switchPanel(item->child(i), previous);
188                                         break;
189                                 }
190                         }
191                 }
192         }
193         else if (QWidget * w = widget_map_.value(item, 0)) {
194                 stack_->setCurrentWidget(w);
195         }
196 }
197
198 static bool matches(QString const & input, QString const & search)
199 {
200         QString text = input;
201
202         // Check if the input contains the search string
203         return text.remove('&').contains(search, Qt::CaseInsensitive);
204 }
205
206 static void setTreeItemStatus(QTreeWidgetItem * tree_item, bool enabled)
207 {
208         // Enable/disable the item
209         tree_item->setDisabled(!enabled);
210
211         // Change the color from black to gray or viceversa
212         QPalette::ColorGroup new_color = enabled ? QPalette::Active : QPalette::Disabled;
213         tree_item->setTextColor(0, QApplication::palette().color(new_color, QPalette::Text));
214 }
215
216 void PanelStack::hideEvent(QHideEvent * event)
217 {
218         QWidget::hideEvent(event);
219
220         // Programatically hidden (not simply minimized by the user)
221         if (!event->spontaneous()) {
222                 resetSearch();
223         }
224 }
225
226 void PanelStack::resetSearch()
227 {
228         search_->setText(QString());
229         search();
230 }
231
232 void PanelStack::filterChanged(QString const & /*search*/)
233 {
234         // The text in the search box is changed, reset the timer
235         // and then search in the widgets
236         delay_search_->start(300);
237 }
238
239 void PanelStack::search()
240 {
241         QString search = search_->text();
242         bool enable_all = search.isEmpty();
243
244         // If the search string is empty we enable all the items
245         // otherwise we disable everything and then selectively
246         // re-enable matching items
247         foreach (QTreeWidgetItem * tree_item, panel_map_) {
248                 setTreeItemStatus(tree_item, enable_all);
249         }
250
251         foreach (QTreeWidgetItem * tree_item, panel_map_) {
252                 // Current widget
253                 QWidget * pane_widget = widget_map_[tree_item];
254
255                 // First of all we look in the pane name
256                 bool pane_matches = tree_item->text(0).contains(search, Qt::CaseInsensitive);
257
258                 // If the tree item has an associated pane
259                 if (pane_widget) {
260                         // Loops on the list of children widgets (recursive)
261                         QWidgetList children = pane_widget->findChildren<QWidget *>();
262                         foreach (QWidget * child_widget, children) {
263                                 bool widget_matches = false;
264
265                                 // Try to cast to the most common widgets and looks in it's content
266                                 // It's bad OOP, it would be nice to have a QWidget::toString() overloaded by
267                                 // each widget, but this would require to change Qt or subclass each widget.
268                                 // Note that we have to ignore the amperstand symbol
269                                 if (QAbstractButton * button = qobject_cast<QAbstractButton *>(child_widget)) {
270                                         widget_matches = matches(button->text(), search);
271
272                                 } else if (QGroupBox * group_box = qobject_cast<QGroupBox *>(child_widget)) {
273                                         widget_matches = matches(group_box->title(), search);
274
275                                 } else if (QLabel * label = qobject_cast<QLabel *>(child_widget)) {
276                                         widget_matches = matches(label->text(), search);
277
278                                 } else if (QLineEdit * line_edit = qobject_cast<QLineEdit *>(child_widget)) {
279                                         widget_matches = matches(line_edit->text(), search);
280
281                                 } else if (QListWidget * list_widget = qobject_cast<QListWidget *>(child_widget)) {
282                                         widget_matches = (list_widget->findItems(search, Qt::MatchContains)).count() > 0;
283
284                                 } else if (QTreeWidget * tree_view = qobject_cast<QTreeWidget *>(child_widget)) {
285                                         widget_matches = (tree_view->findItems(search, Qt::MatchContains)).count() > 0;
286
287                                 } else if (QComboBox * combo_box = qobject_cast<QComboBox *>(child_widget)) {
288                                         widget_matches = (combo_box->findText(search, Qt::MatchContains)) != -1;
289
290                                 } else {
291                                         continue;
292                                 }
293
294                                 // If this widget meets the search criteria
295                                 if (widget_matches && !enable_all) {
296                                         // The pane too meets the search criteria
297                                         pane_matches = true;
298
299                                         // Highlight the widget
300                                         QPalette widget_palette = child_widget->palette();
301                                         widget_palette.setColor(child_widget->foregroundRole(), Qt::red);
302                                         child_widget->setPalette(widget_palette);
303                                 } else {
304                                         // Reset the color of the widget
305                                         child_widget->setPalette(QApplication::palette(child_widget));
306                                 }
307                         }
308
309                         // If the pane meets the search criteria
310                         if (pane_matches && !enable_all) {
311                                 // Expand and enable the pane and his ancestors (typically just the parent)
312                                 QTreeWidgetItem * item = tree_item;
313                                 do {
314                                         item->setExpanded(true);
315                                         setTreeItemStatus(item, true);
316                                         item = item->parent();
317                                 } while (item);
318                         }
319                 }
320
321         }
322 }
323
324 void PanelStack::itemSelected(QTreeWidgetItem * item, int)
325 {
326         // de-select the category if a child is selected
327         if (item->childCount() > 0 && item->child(0)->isSelected())
328                 item->setSelected(false);
329 }
330
331
332 QSize PanelStack::sizeHint() const
333 {
334         return QSize(list_->width() + stack_->width(),
335                 qMax(list_->height(), stack_->height()));
336 }
337
338 } // namespace frontend
339 } // namespace lyx
340
341 #include "moc_PanelStack.cpp"