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