]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/PanelStack.cpp
Fix minor whitespace error.
[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/lassert.h"
20
21 #include <QAbstractButton>
22 #include <QApplication>
23 #include <QComboBox>
24 #include <QFontMetrics>
25 #include <QGroupBox>
26 #include <QHideEvent>
27 #include <QHash>
28 #include <QHBoxLayout>
29 #include <QHeaderView>
30 #include <QLabel>
31 #include <QLineEdit>
32 #include <QListWidget>
33 #include <QPalette>
34 #include <QPushButton>
35 #include <QStackedWidget>
36 #include <QTimer>
37 #include <QTreeWidget>
38 #include <QVBoxLayout>
39
40 using namespace std;
41
42 namespace lyx {
43 namespace frontend {
44
45
46 PanelStack::PanelStack(QWidget * parent)
47         : QWidget(parent)
48 {
49         delay_search_ = new QTimer(this);
50         search_ = new FancyLineEdit(this);
51         list_ = new QTreeWidget(this);
52         stack_ = new QStackedWidget(this);
53
54         // Configure the timer
55         delay_search_->setSingleShot(true);
56         connect(delay_search_, SIGNAL(timeout()), this, SLOT(search()));
57
58         // Configure tree
59         list_->setRootIsDecorated(false);
60         list_->setColumnCount(1);
61         list_->header()->hide();
62         setSectionResizeMode(list_->header(), QHeaderView::ResizeToContents);
63         list_->header()->setStretchLastSection(false);
64         list_->setMinimumSize(list_->viewport()->size());
65
66         connect(list_, SIGNAL(currentItemChanged(QTreeWidgetItem *,
67                                                  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         search_->setPlaceholderText(qt_("Search"));
74         search_->setButtonPixmap(FancyLineEdit::Right,
75                                  getPixmap("images/", "editclear", "svgz,png"));
76         search_->setButtonVisible(FancyLineEdit::Right, true);
77         search_->setButtonToolTip(FancyLineEdit::Right, qt_("Clear text"));
78         search_->setAutoHideButton(FancyLineEdit::Right, true);
79         connect(search_, SIGNAL(rightButtonClicked()),
80                 this, SLOT(resetSearch()));
81         connect(search_, SIGNAL(textEdited(QString)),
82                 this, SLOT(filterChanged(QString)));
83         connect(search_, SIGNAL(downPressed()),
84                 list_, SLOT(setFocus()));
85
86         // Create the output layout, horizontal plus a VBox on the left with the
87         // search 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(qt_(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,
132                           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 =
214                 enabled ? QPalette::Active : QPalette::Disabled;
215         tree_item->setTextColor(0, QApplication::palette().color(new_color,
216                                                                  QPalette::Text));
217 }
218
219 void PanelStack::hideEvent(QHideEvent * event)
220 {
221         QWidget::hideEvent(event);
222
223         // Programatically hidden (not simply minimized by the user)
224         if (!event->spontaneous()) {
225                 resetSearch();
226         }
227 }
228
229 void PanelStack::resetSearch()
230 {
231         search_->setText(QString());
232         search();
233 }
234
235 void PanelStack::filterChanged(QString const & /*search*/)
236 {
237         // The text in the search box is changed, reset the timer
238         // and then search in the widgets
239         delay_search_->start(300);
240 }
241
242 void PanelStack::search()
243 {
244         QString search = search_->text();
245         bool enable_all = search.isEmpty();
246
247         // If the search string is empty we enable all the items
248         // otherwise we disable everything and then selectively
249         // re-enable matching items
250         for (QTreeWidgetItem * tree_item : panel_map_) {
251                 setTreeItemStatus(tree_item, enable_all);
252         }
253
254         for (QTreeWidgetItem * tree_item : panel_map_) {
255                 // Current widget
256                 QWidget * pane_widget = widget_map_[tree_item];
257
258                 // First of all we look in the pane name
259                 bool pane_matches = tree_item->text(0).contains(search,
260                                                                 Qt::CaseInsensitive);
261
262                 // If the tree item has an associated pane
263                 if (pane_widget) {
264                         // Loops on the list of children widgets (recursive)
265                         QWidgetList children = pane_widget->findChildren<QWidget *>();
266                         for (QWidget * child_widget : children) {
267                                 bool widget_matches = false;
268
269                                 // Try to cast to the most common widgets and looks in it's
270                                 // content.
271                                 // It's bad OOP, it would be nice to have a QWidget::toString()
272                                 // overloaded by each widget, but this would require to change
273                                 // Qt or subclass each widget.
274                                 // Note that we have to ignore the amperstand symbol
275                                 if (QAbstractButton * button =
276                                     qobject_cast<QAbstractButton *>(child_widget)) {
277                                         widget_matches = matches(button->text(), search);
278
279                                 } else if (QGroupBox * group_box =
280                                            qobject_cast<QGroupBox *>(child_widget)) {
281                                         widget_matches = matches(group_box->title(), search);
282
283                                 } else if (QLabel * label =
284                                            qobject_cast<QLabel *>(child_widget)) {
285                                         widget_matches = matches(label->text(), search);
286
287                                 } else if (QLineEdit * line_edit =
288                                            qobject_cast<QLineEdit *>(child_widget)) {
289                                         widget_matches = matches(line_edit->text(), search);
290
291                                 } else if (QListWidget * list_widget =
292                                            qobject_cast<QListWidget *>(child_widget)) {
293                                         widget_matches =
294                                                 list_widget->findItems(search,
295                                                                        Qt::MatchContains).count() > 0;
296
297                                 } else if (QTreeWidget * tree_view =
298                                            qobject_cast<QTreeWidget *>(child_widget)) {
299                                         widget_matches =
300                                                 tree_view->findItems(search,
301                                                                      Qt::MatchContains).count() > 0;
302
303                                 } else if (QComboBox * combo_box =
304                                            qobject_cast<QComboBox *>(child_widget)) {
305                                         widget_matches =
306                                                 combo_box->findText(search,
307                                                                     Qt::MatchContains) != -1;
308
309                                 } else {
310                                         continue;
311                                 }
312
313                                 // If this widget meets the search criteria
314                                 if (widget_matches && !enable_all) {
315                                         // The pane too meets the search criteria
316                                         pane_matches = true;
317
318                                         // Highlight the widget
319                                         QPalette widget_palette = child_widget->palette();
320                                         widget_palette.setColor(child_widget->foregroundRole(),
321                                                                 Qt::red);
322                                         child_widget->setPalette(widget_palette);
323                                 } else {
324                                         // Reset the color of the widget
325                                         child_widget->setPalette(QApplication::palette(child_widget));
326                                 }
327                         }
328
329                         // If the pane meets the search criteria
330                         if (pane_matches && !enable_all) {
331                                 // Expand and enable the pane and his ancestors (typically just
332                                 // the parent)
333                                 QTreeWidgetItem * item = tree_item;
334                                 do {
335                                         item->setExpanded(true);
336                                         setTreeItemStatus(item, true);
337                                         item = item->parent();
338                                 } while (item);
339                         }
340                 }
341
342         }
343 }
344
345 void PanelStack::itemSelected(QTreeWidgetItem * item, int)
346 {
347         // de-select the category if a child is selected
348         if (item->childCount() > 0 && item->child(0)->isSelected())
349                 item->setSelected(false);
350 }
351
352
353 QSize PanelStack::sizeHint() const
354 {
355         return QSize(list_->width() + stack_->width(),
356                 qMax(list_->height(), stack_->height()));
357 }
358
359 } // namespace frontend
360 } // namespace lyx
361
362 #include "moc_PanelStack.cpp"