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