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>
24 #include <QColorGroup>
26 #include <QFontMetrics>
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()->setResizeMode(QHeaderView::ResizeToContents);
65 list_->header()->setStretchLastSection(false);
66 list_->setMinimumSize(list_->viewport()->size());
68 connect(list_, SIGNAL(currentItemChanged(QTreeWidgetItem*, QTreeWidgetItem*)),
69 this, SLOT(switchPanel(QTreeWidgetItem *, QTreeWidgetItem*)));
70 connect(list_, SIGNAL(itemClicked (QTreeWidgetItem*, int)),
71 this, SLOT(itemSelected(QTreeWidgetItem *, int)));
73 // Configure the search box
74 #if QT_VERSION >= 0x040700
75 search_->setPlaceholderText(qt_("Search"));
78 #if QT_VERSION >= 0x040600
79 search_->setButtonPixmap(FancyLineEdit::Right, getPixmap("images/", "editclear", "png"));
80 search_->setButtonVisible(FancyLineEdit::Right, true);
81 search_->setButtonToolTip(FancyLineEdit::Right, qt_("Clear text"));
82 search_->setAutoHideButton(FancyLineEdit::Right, true);
84 connect(search_, SIGNAL(rightButtonClicked()), this, SLOT(resetSearch()));
85 connect(search_, SIGNAL(textEdited(QString)), this, SLOT(filterChanged(QString)));
87 // Create the output layout, horizontal plus a VBox on the left with the search
89 QVBoxLayout * left_layout = new QVBoxLayout;
90 left_layout->addWidget(search_, 0);
91 left_layout->addWidget(list_, 1);
93 QHBoxLayout * main_layout = new QHBoxLayout(this);
94 main_layout->addLayout(left_layout, 0);
95 main_layout->addWidget(stack_, 1);
99 void PanelStack::addCategory(QString const & name, QString const & parent)
101 QTreeWidgetItem * item = 0;
103 LYXERR(Debug::GUI, "addCategory n= " << name << " parent= ");
107 if (parent.isEmpty()) {
108 item = new QTreeWidgetItem(list_);
109 item->setText(0, name);
112 if (!panel_map_.contains(parent))
114 item = new QTreeWidgetItem(panel_map_.value(parent));
115 item->setText(0, name);
117 list_->setRootIsDecorated(true);
120 panel_map_[name] = item;
122 QFontMetrics fm(list_->font());
124 // calculate the real size the current item needs in the listview
125 int itemsize = fm.width(name) + 10 + list_->indentation() * depth;
126 // adjust the listview width to the max. itemsize
127 if (itemsize > list_->minimumWidth())
128 list_->setMinimumWidth(itemsize);
132 void PanelStack::addPanel(QWidget * panel, QString const & name, 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 = enabled ? QPalette::Active : QPalette::Disabled;
214 tree_item->setTextColor(0, QApplication::palette().color(new_color, QPalette::Text));
217 void PanelStack::hideEvent(QHideEvent * event)
219 QWidget::hideEvent(event);
221 // Programatically hidden (not simply minimized by the user)
222 if (!event->spontaneous()) {
227 void PanelStack::resetSearch()
229 search_->setText(QString());
233 void PanelStack::filterChanged(QString const & /*search*/)
235 // The text in the search box is changed, reset the timer
236 // and then search in the widgets
237 delay_search_->start(300);
240 void PanelStack::search()
242 QString search = search_->text();
243 bool enable_all = search.isEmpty();
245 // If the search string is empty we enable all the items
246 // otherwise we disable everything and then selectively
247 // re-enable matching items
248 foreach (QTreeWidgetItem * tree_item, panel_map_) {
249 setTreeItemStatus(tree_item, enable_all);
252 foreach (QTreeWidgetItem * tree_item, panel_map_) {
254 QWidget * pane_widget = widget_map_[tree_item];
256 // First of all we look in the pane name
257 bool pane_matches = tree_item->text(0).contains(search, Qt::CaseInsensitive);
259 // If the tree item has an associated pane
261 // Loops on the list of children widgets (recursive)
262 QWidgetList children = pane_widget->findChildren<QWidget *>();
263 foreach (QWidget * child_widget, children) {
264 bool widget_matches = false;
266 // Try to cast to the most common widgets and looks in it's content
267 // It's bad OOP, it would be nice to have a QWidget::toString() overloaded by
268 // each widget, but this would require to change Qt or subclass each widget.
269 // Note that we have to ignore the amperstand symbol
270 if (QAbstractButton * button = qobject_cast<QAbstractButton *>(child_widget)) {
271 widget_matches = matches(button->text(), search);
273 } else if (QGroupBox * group_box = qobject_cast<QGroupBox *>(child_widget)) {
274 widget_matches = matches(group_box->title(), search);
276 } else if (QLabel * label = qobject_cast<QLabel *>(child_widget)) {
277 widget_matches = matches(label->text(), search);
279 } else if (QLineEdit * line_edit = qobject_cast<QLineEdit *>(child_widget)) {
280 widget_matches = matches(line_edit->text(), search);
282 } else if (QListWidget * list_widget = qobject_cast<QListWidget *>(child_widget)) {
283 widget_matches = (list_widget->findItems(search, Qt::MatchContains)).count() > 0;
285 } else if (QTreeWidget * tree_view = qobject_cast<QTreeWidget *>(child_widget)) {
286 widget_matches = (tree_view->findItems(search, Qt::MatchContains)).count() > 0;
288 } else if (QComboBox * combo_box = qobject_cast<QComboBox *>(child_widget)) {
289 widget_matches = (combo_box->findText(search, Qt::MatchContains)) != -1;
295 // If this widget meets the search criteria
296 if (widget_matches && !enable_all) {
297 // The pane too meets the search criteria
300 // Highlight the widget
301 QPalette widget_palette = child_widget->palette();
302 widget_palette.setColor(child_widget->foregroundRole(), Qt::red);
303 child_widget->setPalette(widget_palette);
305 // Reset the color of the widget
306 child_widget->setPalette(QApplication::palette(child_widget));
310 // If the pane meets the search criteria
311 if (pane_matches && !enable_all) {
312 // Expand and enable the pane and his ancestors (typically just the parent)
313 QTreeWidgetItem * item = tree_item;
315 item->setExpanded(true);
316 setTreeItemStatus(item, true);
317 item = item->parent();
325 void PanelStack::itemSelected(QTreeWidgetItem * item, int)
327 // de-select the category if a child is selected
328 if (item->childCount() > 0 && item->child(0)->isSelected())
329 item->setSelected(false);
333 QSize PanelStack::sizeHint() const
335 return QSize(list_->width() + stack_->width(),
336 qMax(list_->height(), stack_->height()));
339 } // namespace frontend
342 #include "moc_PanelStack.cpp"