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*, QTreeWidgetItem*)),
67 this, SLOT(switchPanel(QTreeWidgetItem *, QTreeWidgetItem*)));
68 connect(list_, SIGNAL(itemClicked (QTreeWidgetItem*, int)),
69 this, SLOT(itemSelected(QTreeWidgetItem *, int)));
71 // Configure the search box
72 #if QT_VERSION >= 0x040700
73 search_->setPlaceholderText(qt_("Search"));
76 #if QT_VERSION >= 0x040600
77 search_->setButtonPixmap(FancyLineEdit::Right, 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);
82 connect(search_, SIGNAL(rightButtonClicked()), this, SLOT(resetSearch()));
83 connect(search_, SIGNAL(textEdited(QString)), this, SLOT(filterChanged(QString)));
85 // Create the output layout, horizontal plus a VBox on the left with the search
87 QVBoxLayout * left_layout = new QVBoxLayout;
88 left_layout->addWidget(search_, 0);
89 left_layout->addWidget(list_, 1);
91 QHBoxLayout * main_layout = new QHBoxLayout(this);
92 main_layout->addLayout(left_layout, 0);
93 main_layout->addWidget(stack_, 1);
97 void PanelStack::addCategory(QString const & name, QString const & parent)
99 QTreeWidgetItem * item = 0;
101 LYXERR(Debug::GUI, "addCategory n= " << name << " parent= ");
105 if (parent.isEmpty()) {
106 item = new QTreeWidgetItem(list_);
107 item->setText(0, qt_(name));
110 if (!panel_map_.contains(parent))
112 item = new QTreeWidgetItem(panel_map_.value(parent));
113 item->setText(0, qt_(name));
115 list_->setRootIsDecorated(true);
118 panel_map_[name] = item;
120 QFontMetrics fm(list_->font());
122 // calculate the real size the current item needs in the listview
123 int itemsize = fm.width(name) + 10 + list_->indentation() * depth;
124 // adjust the listview width to the max. itemsize
125 if (itemsize > list_->minimumWidth())
126 list_->setMinimumWidth(itemsize);
130 void PanelStack::addPanel(QWidget * panel, QString const & name, 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::setCurrentPanel(QString const & name)
151 QTreeWidgetItem * item = panel_map_.value(name, 0);
152 LASSERT(item, return);
154 // force on first set
155 if (list_->currentItem() == item)
158 list_->setCurrentItem(item);
162 bool PanelStack::isCurrentPanel(QString const & name) const
164 QTreeWidgetItem * item = panel_map_.value(name, 0);
165 LASSERT(item, return false);
167 return (list_->currentItem() == item);
171 void PanelStack::switchPanel(QTreeWidgetItem * item,
172 QTreeWidgetItem * previous)
174 // do nothing when clicked on whitespace (item=NULL)
178 // if we have a category, expand the tree and go to the
179 // first enabled item
180 if (item->childCount() > 0) {
181 item->setExpanded(true);
182 if (previous && previous->parent() != item) {
183 // Looks for a child not disabled
184 for (int i = 0; i < item->childCount(); ++i) {
185 if (item->child(i)->flags() & Qt::ItemIsEnabled) {
186 switchPanel(item->child(i), previous);
192 else if (QWidget * w = widget_map_.value(item, 0)) {
193 stack_->setCurrentWidget(w);
197 static bool matches(QString const & input, QString const & search)
199 QString text = input;
201 // Check if the input contains the search string
202 return text.remove('&').contains(search, Qt::CaseInsensitive);
205 static void setTreeItemStatus(QTreeWidgetItem * tree_item, bool enabled)
207 // Enable/disable the item
208 tree_item->setDisabled(!enabled);
210 // Change the color from black to gray or viceversa
211 QPalette::ColorGroup new_color = enabled ? QPalette::Active : QPalette::Disabled;
212 tree_item->setTextColor(0, QApplication::palette().color(new_color, QPalette::Text));
215 void PanelStack::hideEvent(QHideEvent * event)
217 QWidget::hideEvent(event);
219 // Programatically hidden (not simply minimized by the user)
220 if (!event->spontaneous()) {
225 void PanelStack::resetSearch()
227 search_->setText(QString());
231 void PanelStack::filterChanged(QString const & /*search*/)
233 // The text in the search box is changed, reset the timer
234 // and then search in the widgets
235 delay_search_->start(300);
238 void PanelStack::search()
240 QString search = search_->text();
241 bool enable_all = search.isEmpty();
243 // If the search string is empty we enable all the items
244 // otherwise we disable everything and then selectively
245 // re-enable matching items
246 for (QTreeWidgetItem * tree_item : panel_map_) {
247 setTreeItemStatus(tree_item, enable_all);
250 for (QTreeWidgetItem * tree_item : panel_map_) {
252 QWidget * pane_widget = widget_map_[tree_item];
254 // First of all we look in the pane name
255 bool pane_matches = tree_item->text(0).contains(search, Qt::CaseInsensitive);
257 // If the tree item has an associated pane
259 // Loops on the list of children widgets (recursive)
260 QWidgetList children = pane_widget->findChildren<QWidget *>();
261 for (QWidget * child_widget : children) {
262 bool widget_matches = false;
264 // Try to cast to the most common widgets and looks in it's content
265 // It's bad OOP, it would be nice to have a QWidget::toString() overloaded by
266 // each widget, but this would require to change Qt or subclass each widget.
267 // Note that we have to ignore the amperstand symbol
268 if (QAbstractButton * button = qobject_cast<QAbstractButton *>(child_widget)) {
269 widget_matches = matches(button->text(), search);
271 } else if (QGroupBox * group_box = qobject_cast<QGroupBox *>(child_widget)) {
272 widget_matches = matches(group_box->title(), search);
274 } else if (QLabel * label = qobject_cast<QLabel *>(child_widget)) {
275 widget_matches = matches(label->text(), search);
277 } else if (QLineEdit * line_edit = qobject_cast<QLineEdit *>(child_widget)) {
278 widget_matches = matches(line_edit->text(), search);
280 } else if (QListWidget * list_widget = qobject_cast<QListWidget *>(child_widget)) {
281 widget_matches = (list_widget->findItems(search, Qt::MatchContains)).count() > 0;
283 } else if (QTreeWidget * tree_view = qobject_cast<QTreeWidget *>(child_widget)) {
284 widget_matches = (tree_view->findItems(search, Qt::MatchContains)).count() > 0;
286 } else if (QComboBox * combo_box = qobject_cast<QComboBox *>(child_widget)) {
287 widget_matches = (combo_box->findText(search, Qt::MatchContains)) != -1;
293 // If this widget meets the search criteria
294 if (widget_matches && !enable_all) {
295 // The pane too meets the search criteria
298 // Highlight the widget
299 QPalette widget_palette = child_widget->palette();
300 widget_palette.setColor(child_widget->foregroundRole(), Qt::red);
301 child_widget->setPalette(widget_palette);
303 // Reset the color of the widget
304 child_widget->setPalette(QApplication::palette(child_widget));
308 // If the pane meets the search criteria
309 if (pane_matches && !enable_all) {
310 // Expand and enable the pane and his ancestors (typically just the parent)
311 QTreeWidgetItem * item = tree_item;
313 item->setExpanded(true);
314 setTreeItemStatus(item, true);
315 item = item->parent();
323 void PanelStack::itemSelected(QTreeWidgetItem * item, int)
325 // de-select the category if a child is selected
326 if (item->childCount() > 0 && item->child(0)->isSelected())
327 item->setSelected(false);
331 QSize PanelStack::sizeHint() const
333 return QSize(list_->width() + stack_->width(),
334 qMax(list_->height(), stack_->height()));
337 } // namespace frontend
340 #include "moc_PanelStack.cpp"