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/foreach.h"
20 #include "support/lassert.h"
22 #include <QAbstractButton>
23 #include <QApplication>
25 #include <QFontMetrics>
29 #include <QHBoxLayout>
30 #include <QHeaderView>
33 #include <QListWidget>
35 #include <QPushButton>
36 #include <QStackedWidget>
38 #include <QTreeWidget>
39 #include <QVBoxLayout>
47 PanelStack::PanelStack(QWidget * parent)
50 delay_search_ = new QTimer(this);
51 search_ = new FancyLineEdit(this);
52 list_ = new QTreeWidget(this);
53 stack_ = new QStackedWidget(this);
55 // Configure the timer
56 delay_search_->setSingleShot(true);
57 connect(delay_search_, SIGNAL(timeout()), this, SLOT(search()));
60 list_->setRootIsDecorated(false);
61 list_->setColumnCount(1);
62 list_->header()->hide();
63 setSectionResizeMode(list_->header(), QHeaderView::ResizeToContents);
64 list_->header()->setStretchLastSection(false);
65 list_->setMinimumSize(list_->viewport()->size());
67 connect(list_, SIGNAL(currentItemChanged(QTreeWidgetItem*, 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 #if QT_VERSION >= 0x040700
74 search_->setPlaceholderText(qt_("Search"));
77 #if QT_VERSION >= 0x040600
78 search_->setButtonPixmap(FancyLineEdit::Right, getPixmap("images/", "editclear", "svgz,png"));
79 search_->setButtonVisible(FancyLineEdit::Right, true);
80 search_->setButtonToolTip(FancyLineEdit::Right, qt_("Clear text"));
81 search_->setAutoHideButton(FancyLineEdit::Right, true);
83 connect(search_, SIGNAL(rightButtonClicked()), this, SLOT(resetSearch()));
84 connect(search_, SIGNAL(textEdited(QString)), this, SLOT(filterChanged(QString)));
86 // Create the output layout, horizontal plus a VBox on the left with the search
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(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, QString const & parent)
133 addCategory(name, parent);
134 QTreeWidgetItem * item = panel_map_.value(name);
135 widget_map_[item] = panel;
136 stack_->addWidget(panel);
137 stack_->setMinimumSize(panel->minimumSize());
141 void PanelStack::showPanel(QString const & name, bool show)
143 QTreeWidgetItem * item = panel_map_.value(name, 0);
144 LASSERT(item, return);
146 item->setHidden(!show);
150 void PanelStack::setCurrentPanel(QString const & name)
152 QTreeWidgetItem * item = panel_map_.value(name, 0);
153 LASSERT(item, return);
155 // force on first set
156 if (list_->currentItem() == item)
159 list_->setCurrentItem(item);
163 bool PanelStack::isCurrentPanel(QString const & name) const
165 QTreeWidgetItem * item = panel_map_.value(name, 0);
166 LASSERT(item, return false);
168 return (list_->currentItem() == item);
172 void PanelStack::switchPanel(QTreeWidgetItem * item,
173 QTreeWidgetItem * previous)
175 // do nothing when clicked on whitespace (item=NULL)
179 // if we have a category, expand the tree and go to the
180 // first enabled item
181 if (item->childCount() > 0) {
182 item->setExpanded(true);
183 if (previous && previous->parent() != item) {
184 // Looks for a child not disabled
185 for (int i = 0; i < item->childCount(); ++i) {
186 if (item->child(i)->flags() & Qt::ItemIsEnabled) {
187 switchPanel(item->child(i), previous);
193 else if (QWidget * w = widget_map_.value(item, 0)) {
194 stack_->setCurrentWidget(w);
198 static bool matches(QString const & input, QString const & search)
200 QString text = input;
202 // Check if the input contains the search string
203 return text.remove('&').contains(search, Qt::CaseInsensitive);
206 static void setTreeItemStatus(QTreeWidgetItem * tree_item, bool enabled)
208 // Enable/disable the item
209 tree_item->setDisabled(!enabled);
211 // Change the color from black to gray or viceversa
212 QPalette::ColorGroup new_color = enabled ? QPalette::Active : QPalette::Disabled;
213 tree_item->setTextColor(0, QApplication::palette().color(new_color, QPalette::Text));
216 void PanelStack::hideEvent(QHideEvent * event)
218 QWidget::hideEvent(event);
220 // Programatically hidden (not simply minimized by the user)
221 if (!event->spontaneous()) {
226 void PanelStack::resetSearch()
228 search_->setText(QString());
232 void PanelStack::filterChanged(QString const & /*search*/)
234 // The text in the search box is changed, reset the timer
235 // and then search in the widgets
236 delay_search_->start(300);
239 void PanelStack::search()
241 QString search = search_->text();
242 bool enable_all = search.isEmpty();
244 // If the search string is empty we enable all the items
245 // otherwise we disable everything and then selectively
246 // re-enable matching items
247 foreach (QTreeWidgetItem * tree_item, panel_map_) {
248 setTreeItemStatus(tree_item, enable_all);
251 foreach (QTreeWidgetItem * tree_item, panel_map_) {
253 QWidget * pane_widget = widget_map_[tree_item];
255 // First of all we look in the pane name
256 bool pane_matches = tree_item->text(0).contains(search, Qt::CaseInsensitive);
258 // If the tree item has an associated pane
260 // Loops on the list of children widgets (recursive)
261 QWidgetList children = pane_widget->findChildren<QWidget *>();
262 foreach (QWidget * child_widget, children) {
263 bool widget_matches = false;
265 // Try to cast to the most common widgets and looks in it's content
266 // It's bad OOP, it would be nice to have a QWidget::toString() overloaded by
267 // each widget, but this would require to change Qt or subclass each widget.
268 // Note that we have to ignore the amperstand symbol
269 if (QAbstractButton * button = qobject_cast<QAbstractButton *>(child_widget)) {
270 widget_matches = matches(button->text(), search);
272 } else if (QGroupBox * group_box = qobject_cast<QGroupBox *>(child_widget)) {
273 widget_matches = matches(group_box->title(), search);
275 } else if (QLabel * label = qobject_cast<QLabel *>(child_widget)) {
276 widget_matches = matches(label->text(), search);
278 } else if (QLineEdit * line_edit = qobject_cast<QLineEdit *>(child_widget)) {
279 widget_matches = matches(line_edit->text(), search);
281 } else if (QListWidget * list_widget = qobject_cast<QListWidget *>(child_widget)) {
282 widget_matches = (list_widget->findItems(search, Qt::MatchContains)).count() > 0;
284 } else if (QTreeWidget * tree_view = qobject_cast<QTreeWidget *>(child_widget)) {
285 widget_matches = (tree_view->findItems(search, Qt::MatchContains)).count() > 0;
287 } else if (QComboBox * combo_box = qobject_cast<QComboBox *>(child_widget)) {
288 widget_matches = (combo_box->findText(search, Qt::MatchContains)) != -1;
294 // If this widget meets the search criteria
295 if (widget_matches && !enable_all) {
296 // The pane too meets the search criteria
299 // Highlight the widget
300 QPalette widget_palette = child_widget->palette();
301 widget_palette.setColor(child_widget->foregroundRole(), Qt::red);
302 child_widget->setPalette(widget_palette);
304 // Reset the color of the widget
305 child_widget->setPalette(QApplication::palette(child_widget));
309 // If the pane meets the search criteria
310 if (pane_matches && !enable_all) {
311 // Expand and enable the pane and his ancestors (typically just the parent)
312 QTreeWidgetItem * item = tree_item;
314 item->setExpanded(true);
315 setTreeItemStatus(item, true);
316 item = item->parent();
324 void PanelStack::itemSelected(QTreeWidgetItem * item, int)
326 // de-select the category if a child is selected
327 if (item->childCount() > 0 && item->child(0)->isSelected())
328 item->setSelected(false);
332 QSize PanelStack::sizeHint() const
334 return QSize(list_->width() + stack_->width(),
335 qMax(list_->height(), stack_->height()));
338 } // namespace frontend
341 #include "moc_PanelStack.cpp"