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