3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
13 #include "PanelStack.h"
15 #include "GuiApplication.h"
16 #include "GuiFontMetrics.h"
17 #include "qt_helpers.h"
19 #include "FancyLineEdit.h"
21 #include "support/debug.h"
22 #include "support/lassert.h"
24 #include <QAbstractButton>
25 #include <QApplication>
30 #include <QHBoxLayout>
31 #include <QHeaderView>
34 #include <QListWidget>
36 #include <QPushButton>
37 #include <QStackedWidget>
39 #include <QTreeWidget>
40 #include <QVBoxLayout>
48 PanelStack::PanelStack(QWidget * parent)
51 delay_search_ = new QTimer(this);
52 search_ = new FancyLineEdit(this);
53 list_ = new QTreeWidget(this);
54 stack_ = new QStackedWidget(this);
56 // Configure the timer
57 delay_search_->setSingleShot(true);
58 connect(delay_search_, SIGNAL(timeout()), this, SLOT(search()));
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());
68 connect(list_, SIGNAL(currentItemChanged(QTreeWidgetItem *,
70 this, SLOT(switchPanel(QTreeWidgetItem *, QTreeWidgetItem *)));
71 connect(list_, SIGNAL(itemClicked (QTreeWidgetItem*, int)),
72 this, SLOT(itemSelected(QTreeWidgetItem *, int)));
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()));
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);
90 QHBoxLayout * main_layout = new QHBoxLayout(this);
91 main_layout->addLayout(left_layout, 0);
92 main_layout->addWidget(stack_, 1);
96 void PanelStack::addCategory(QString const & name, QString const & parent)
98 QTreeWidgetItem * item = nullptr;
100 LYXERR(Debug::GUI, "addCategory n= " << name << " parent= ");
104 if (parent.isEmpty()) {
105 item = new QTreeWidgetItem(list_);
106 item->setText(0, qt_(name));
109 if (!panel_map_.contains(parent))
111 item = new QTreeWidgetItem(panel_map_.value(parent));
112 item->setText(0, qt_(name));
114 list_->setRootIsDecorated(true);
117 panel_map_[name] = item;
119 GuiFontMetrics fm(list_->font());
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);
129 void PanelStack::addPanel(QWidget * panel, QString const & name,
130 QString const & parent)
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());
140 void PanelStack::showPanel(QString const & name, bool show)
142 QTreeWidgetItem * item = panel_map_.value(name, 0);
143 LASSERT(item, return);
145 item->setHidden(!show);
149 void PanelStack::markPanelValid(QString const & name, bool valid)
151 QTreeWidgetItem * item = panel_map_.value(name, 0);
152 LASSERT(item, return);
155 item->setIcon(0, QIcon());
156 item->setToolTip(0, QString());
158 QIcon warn(getPixmap("images/", "emblem-shellescape", "svgz,png"));
159 item->setIcon(0, warn);
160 item->setToolTip(0, qt_("This section contains invalid input. Please fix!"));
165 void PanelStack::setCurrentPanel(QString const & name)
167 QTreeWidgetItem * item = panel_map_.value(name, 0);
168 LASSERT(item, return);
170 // force on first set
171 if (list_->currentItem() == item)
174 list_->setCurrentItem(item);
178 bool PanelStack::isCurrentPanel(QString const & name) const
180 QTreeWidgetItem * item = panel_map_.value(name, 0);
181 LASSERT(item, return false);
183 return (list_->currentItem() == item);
187 void PanelStack::switchPanel(QTreeWidgetItem * item,
188 QTreeWidgetItem * previous)
190 // do nothing when clicked on whitespace (item=NULL)
194 // if we have a category, expand the tree and go to the
195 // first enabled item
196 if (item->childCount() > 0) {
197 item->setExpanded(true);
198 if (previous && previous->parent() != item) {
199 // Looks for a child not disabled
200 for (int i = 0; i < item->childCount(); ++i) {
201 if (item->child(i)->flags() & Qt::ItemIsEnabled) {
202 switchPanel(item->child(i), previous);
208 else if (QWidget * w = widget_map_.value(item, 0)) {
209 stack_->setCurrentWidget(w);
213 static bool matches(QString const & input, QString const & search)
215 QString text = input;
217 // Check if the input contains the search string
218 return text.remove('&').contains(search, Qt::CaseInsensitive);
221 static void setTreeItemStatus(QTreeWidgetItem * tree_item, bool enabled)
223 // Enable/disable the item
224 tree_item->setDisabled(!enabled);
226 // Change the color from black to gray or viceversa
227 QPalette::ColorGroup new_color =
228 enabled ? QPalette::Active : QPalette::Disabled;
229 tree_item->setForeground(0, QApplication::palette().color(new_color,
233 void PanelStack::hideEvent(QHideEvent * event)
235 QWidget::hideEvent(event);
237 // Programmatically hidden (not simply minimized by the user)
238 if (!event->spontaneous()) {
243 void PanelStack::resetSearch()
245 search_->setText(QString());
249 void PanelStack::filterChanged(QString const & /*search*/)
251 // The text in the search box is changed, reset the timer
252 // and then search in the widgets
253 delay_search_->start(300);
256 void PanelStack::search()
258 QString search = search_->text();
259 bool enable_all = search.isEmpty();
261 // If the search string is empty we enable all the items
262 // otherwise we disable everything and then selectively
263 // re-enable matching items
264 for (QTreeWidgetItem * tree_item : panel_map_) {
265 setTreeItemStatus(tree_item, enable_all);
268 for (QTreeWidgetItem * tree_item : panel_map_) {
270 QWidget * pane_widget = widget_map_[tree_item];
272 // First of all we look in the pane name
273 bool pane_matches = tree_item->text(0).contains(search,
274 Qt::CaseInsensitive);
276 // If the tree item has an associated pane
278 // Loops on the list of children widgets (recursive)
279 QWidgetList children = pane_widget->findChildren<QWidget *>();
280 for (QWidget * child_widget : children) {
281 bool widget_matches = false;
283 // Try to cast to the most common widgets and looks in it's
285 // It's bad OOP, it would be nice to have a QWidget::toString()
286 // overloaded by each widget, but this would require to change
287 // Qt or subclass each widget.
288 // Note that we have to ignore the amperstand symbol
289 if (QAbstractButton * button =
290 qobject_cast<QAbstractButton *>(child_widget)) {
291 widget_matches = matches(button->text(), search);
293 } else if (QGroupBox * group_box =
294 qobject_cast<QGroupBox *>(child_widget)) {
295 widget_matches = matches(group_box->title(), search);
297 } else if (QLabel * label =
298 qobject_cast<QLabel *>(child_widget)) {
299 widget_matches = matches(label->text(), search);
301 } else if (QLineEdit * line_edit =
302 qobject_cast<QLineEdit *>(child_widget)) {
303 widget_matches = matches(line_edit->text(), search);
305 } else if (QListWidget * list_widget =
306 qobject_cast<QListWidget *>(child_widget)) {
308 list_widget->findItems(search,
309 Qt::MatchContains).count() > 0;
311 } else if (QTreeWidget * tree_view =
312 qobject_cast<QTreeWidget *>(child_widget)) {
314 tree_view->findItems(search,
315 Qt::MatchContains).count() > 0;
317 } else if (QComboBox * combo_box =
318 qobject_cast<QComboBox *>(child_widget)) {
320 combo_box->findText(search,
321 Qt::MatchContains) != -1;
327 // If this widget meets the search criteria
328 if (widget_matches && !enable_all) {
329 // The pane too meets the search criteria
332 // Highlight the widget
333 QPalette widget_palette = child_widget->palette();
334 widget_palette.setColor(child_widget->foregroundRole(),
336 child_widget->setPalette(widget_palette);
338 // Reset the color of the widget
339 child_widget->setPalette(QApplication::palette(child_widget));
343 // If the pane meets the search criteria
344 if (pane_matches && !enable_all) {
345 // Expand and enable the pane and his ancestors (typically just
347 QTreeWidgetItem * item = tree_item;
349 item->setExpanded(true);
350 setTreeItemStatus(item, true);
351 item = item->parent();
359 void PanelStack::itemSelected(QTreeWidgetItem * item, int)
361 // de-select the category if a child is selected
362 if (item->childCount() > 0 && item->child(0)->isSelected())
363 item->setSelected(false);
367 QSize PanelStack::sizeHint() const
369 return QSize(list_->width() + stack_->width(),
370 qMax(list_->height(), stack_->height()));
373 } // namespace frontend
376 #include "moc_PanelStack.cpp"