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 "qt_helpers.h"
18 #include "support/debug.h"
19 #include "support/lassert.h"
21 #include <QAbstractButton>
22 #include <QApplication>
24 #include <QFontMetrics>
28 #include <QHBoxLayout>
29 #include <QHeaderView>
32 #include <QListWidget>
34 #include <QPushButton>
35 #include <QStackedWidget>
37 #include <QTreeWidget>
38 #include <QVBoxLayout>
46 PanelStack::PanelStack(QWidget * parent)
49 delay_search_ = new QTimer(this);
50 search_ = new FancyLineEdit(this);
51 list_ = new QTreeWidget(this);
52 stack_ = new QStackedWidget(this);
54 // Configure the timer
55 delay_search_->setSingleShot(true);
56 connect(delay_search_, SIGNAL(timeout()), this, SLOT(search()));
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());
66 connect(list_, SIGNAL(currentItemChanged(QTreeWidgetItem *,
68 this, SLOT(switchPanel(QTreeWidgetItem *, QTreeWidgetItem *)));
69 connect(list_, SIGNAL(itemClicked (QTreeWidgetItem*, int)),
70 this, SLOT(itemSelected(QTreeWidgetItem *, int)));
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()));
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);
92 QHBoxLayout * main_layout = new QHBoxLayout(this);
93 main_layout->addLayout(left_layout, 0);
94 main_layout->addWidget(stack_, 1);
98 void PanelStack::addCategory(QString const & name, QString const & parent)
100 QTreeWidgetItem * item = 0;
102 LYXERR(Debug::GUI, "addCategory n= " << name << " parent= ");
106 if (parent.isEmpty()) {
107 item = new QTreeWidgetItem(list_);
108 item->setText(0, qt_(name));
111 if (!panel_map_.contains(parent))
113 item = new QTreeWidgetItem(panel_map_.value(parent));
114 item->setText(0, qt_(name));
116 list_->setRootIsDecorated(true);
119 panel_map_[name] = item;
121 QFontMetrics fm(list_->font());
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);
131 void PanelStack::addPanel(QWidget * panel, QString const & name,
132 QString const & parent)
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());
142 void PanelStack::showPanel(QString const & name, bool show)
144 QTreeWidgetItem * item = panel_map_.value(name, 0);
145 LASSERT(item, return);
147 item->setHidden(!show);
151 void PanelStack::setCurrentPanel(QString const & name)
153 QTreeWidgetItem * item = panel_map_.value(name, 0);
154 LASSERT(item, return);
156 // force on first set
157 if (list_->currentItem() == item)
160 list_->setCurrentItem(item);
164 bool PanelStack::isCurrentPanel(QString const & name) const
166 QTreeWidgetItem * item = panel_map_.value(name, 0);
167 LASSERT(item, return false);
169 return (list_->currentItem() == item);
173 void PanelStack::switchPanel(QTreeWidgetItem * item,
174 QTreeWidgetItem * previous)
176 // do nothing when clicked on whitespace (item=NULL)
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);
194 else if (QWidget * w = widget_map_.value(item, 0)) {
195 stack_->setCurrentWidget(w);
199 static bool matches(QString const & input, QString const & search)
201 QString text = input;
203 // Check if the input contains the search string
204 return text.remove('&').contains(search, Qt::CaseInsensitive);
207 static void setTreeItemStatus(QTreeWidgetItem * tree_item, bool enabled)
209 // Enable/disable the item
210 tree_item->setDisabled(!enabled);
212 // Change the color from black to gray or viceversa
213 QPalette::ColorGroup new_color =
214 enabled ? QPalette::Active : QPalette::Disabled;
215 tree_item->setForeground(0, QApplication::palette().color(new_color,
219 void PanelStack::hideEvent(QHideEvent * event)
221 QWidget::hideEvent(event);
223 // Programatically hidden (not simply minimized by the user)
224 if (!event->spontaneous()) {
229 void PanelStack::resetSearch()
231 search_->setText(QString());
235 void PanelStack::filterChanged(QString const & /*search*/)
237 // The text in the search box is changed, reset the timer
238 // and then search in the widgets
239 delay_search_->start(300);
242 void PanelStack::search()
244 QString search = search_->text();
245 bool enable_all = search.isEmpty();
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);
254 for (QTreeWidgetItem * tree_item : panel_map_) {
256 QWidget * pane_widget = widget_map_[tree_item];
258 // First of all we look in the pane name
259 bool pane_matches = tree_item->text(0).contains(search,
260 Qt::CaseInsensitive);
262 // If the tree item has an associated pane
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;
269 // Try to cast to the most common widgets and looks in it's
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);
279 } else if (QGroupBox * group_box =
280 qobject_cast<QGroupBox *>(child_widget)) {
281 widget_matches = matches(group_box->title(), search);
283 } else if (QLabel * label =
284 qobject_cast<QLabel *>(child_widget)) {
285 widget_matches = matches(label->text(), search);
287 } else if (QLineEdit * line_edit =
288 qobject_cast<QLineEdit *>(child_widget)) {
289 widget_matches = matches(line_edit->text(), search);
291 } else if (QListWidget * list_widget =
292 qobject_cast<QListWidget *>(child_widget)) {
294 list_widget->findItems(search,
295 Qt::MatchContains).count() > 0;
297 } else if (QTreeWidget * tree_view =
298 qobject_cast<QTreeWidget *>(child_widget)) {
300 tree_view->findItems(search,
301 Qt::MatchContains).count() > 0;
303 } else if (QComboBox * combo_box =
304 qobject_cast<QComboBox *>(child_widget)) {
306 combo_box->findText(search,
307 Qt::MatchContains) != -1;
313 // If this widget meets the search criteria
314 if (widget_matches && !enable_all) {
315 // The pane too meets the search criteria
318 // Highlight the widget
319 QPalette widget_palette = child_widget->palette();
320 widget_palette.setColor(child_widget->foregroundRole(),
322 child_widget->setPalette(widget_palette);
324 // Reset the color of the widget
325 child_widget->setPalette(QApplication::palette(child_widget));
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
333 QTreeWidgetItem * item = tree_item;
335 item->setExpanded(true);
336 setTreeItemStatus(item, true);
337 item = item->parent();
345 void PanelStack::itemSelected(QTreeWidgetItem * item, int)
347 // de-select the category if a child is selected
348 if (item->childCount() > 0 && item->child(0)->isSelected())
349 item->setSelected(false);
353 QSize PanelStack::sizeHint() const
355 return QSize(list_->width() + stack_->width(),
356 qMax(list_->height(), stack_->height()));
359 } // namespace frontend
362 #include "moc_PanelStack.cpp"