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