2006-03-05 17:24:44 +00:00
|
|
|
/**
|
2007-04-26 03:53:02 +00:00
|
|
|
* \file PanelStack.cpp
|
2006-03-05 17:24:44 +00:00
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
* Licence details can be found in the file COPYING.
|
|
|
|
*
|
|
|
|
* \author John Levon
|
|
|
|
*
|
|
|
|
* Full author contact details are available in file CREDITS.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
2007-04-26 03:53:02 +00:00
|
|
|
#include "PanelStack.h"
|
2006-03-05 17:24:44 +00:00
|
|
|
|
2011-06-05 17:54:27 +00:00
|
|
|
#include "GuiApplication.h"
|
2006-03-05 17:24:44 +00:00
|
|
|
#include "qt_helpers.h"
|
|
|
|
|
2007-11-29 07:04:28 +00:00
|
|
|
#include "support/debug.h"
|
2011-06-05 17:54:27 +00:00
|
|
|
#include "support/lassert.h"
|
2006-10-30 14:39:05 +00:00
|
|
|
|
2011-06-05 17:54:27 +00:00
|
|
|
#include <QAbstractButton>
|
|
|
|
#include <QApplication>
|
|
|
|
#include <QComboBox>
|
2006-11-05 12:30:12 +00:00
|
|
|
#include <QFontMetrics>
|
2011-06-05 17:54:27 +00:00
|
|
|
#include <QGroupBox>
|
|
|
|
#include <QHideEvent>
|
|
|
|
#include <QHash>
|
2006-03-05 17:24:44 +00:00
|
|
|
#include <QHBoxLayout>
|
2006-10-24 11:32:20 +00:00
|
|
|
#include <QHeaderView>
|
2011-06-05 17:54:27 +00:00
|
|
|
#include <QLabel>
|
|
|
|
#include <QLineEdit>
|
|
|
|
#include <QListWidget>
|
|
|
|
#include <QPalette>
|
|
|
|
#include <QPushButton>
|
2008-09-21 09:11:21 +00:00
|
|
|
#include <QStackedWidget>
|
2011-06-05 17:54:27 +00:00
|
|
|
#include <QTimer>
|
2008-09-21 09:11:21 +00:00
|
|
|
#include <QTreeWidget>
|
2011-06-05 17:54:27 +00:00
|
|
|
#include <QVBoxLayout>
|
2006-03-05 17:24:44 +00:00
|
|
|
|
2007-12-12 10:16:00 +00:00
|
|
|
using namespace std;
|
2006-03-05 17:24:44 +00:00
|
|
|
|
2006-10-30 14:39:05 +00:00
|
|
|
namespace lyx {
|
|
|
|
namespace frontend {
|
|
|
|
|
2006-03-05 17:24:44 +00:00
|
|
|
|
|
|
|
PanelStack::PanelStack(QWidget * parent)
|
|
|
|
: QWidget(parent)
|
|
|
|
{
|
2011-06-05 17:54:27 +00:00
|
|
|
delay_search_ = new QTimer(this);
|
2011-09-24 16:39:26 +00:00
|
|
|
search_ = new FancyLineEdit(this);
|
2006-03-05 17:24:44 +00:00
|
|
|
list_ = new QTreeWidget(this);
|
|
|
|
stack_ = new QStackedWidget(this);
|
2011-06-05 17:54:27 +00:00
|
|
|
|
|
|
|
// Configure the timer
|
|
|
|
delay_search_->setSingleShot(true);
|
|
|
|
connect(delay_search_, SIGNAL(timeout()), this, SLOT(search()));
|
2006-03-05 17:24:44 +00:00
|
|
|
|
2011-06-05 17:54:27 +00:00
|
|
|
// Configure tree
|
2007-08-30 14:22:56 +00:00
|
|
|
list_->setRootIsDecorated(false);
|
2008-03-19 13:11:14 +00:00
|
|
|
list_->setColumnCount(1);
|
2006-11-05 12:30:12 +00:00
|
|
|
list_->header()->hide();
|
2013-02-03 11:23:31 +00:00
|
|
|
setSectionResizeMode(list_->header(), QHeaderView::ResizeToContents);
|
2011-02-15 09:37:19 +00:00
|
|
|
list_->header()->setStretchLastSection(false);
|
|
|
|
list_->setMinimumSize(list_->viewport()->size());
|
2006-03-05 17:24:44 +00:00
|
|
|
|
2008-09-21 09:11:21 +00:00
|
|
|
connect(list_, SIGNAL(currentItemChanged(QTreeWidgetItem*, QTreeWidgetItem*)),
|
2006-04-05 23:56:29 +00:00
|
|
|
this, SLOT(switchPanel(QTreeWidgetItem *, QTreeWidgetItem*)));
|
2008-03-19 18:15:16 +00:00
|
|
|
connect(list_, SIGNAL(itemClicked (QTreeWidgetItem*, int)),
|
|
|
|
this, SLOT(itemSelected(QTreeWidgetItem *, int)));
|
2006-03-05 17:24:44 +00:00
|
|
|
|
2011-06-05 17:54:27 +00:00
|
|
|
// Configure the search box
|
|
|
|
#if QT_VERSION >= 0x040700
|
|
|
|
search_->setPlaceholderText(qt_("Search"));
|
|
|
|
#endif
|
|
|
|
|
2011-06-05 23:24:56 +00:00
|
|
|
#if QT_VERSION >= 0x040600
|
2015-03-12 23:47:21 +00:00
|
|
|
search_->setButtonPixmap(FancyLineEdit::Right, getPixmap("images/", "editclear", "svgz,png"));
|
2011-06-05 17:54:27 +00:00
|
|
|
search_->setButtonVisible(FancyLineEdit::Right, true);
|
|
|
|
search_->setButtonToolTip(FancyLineEdit::Right, qt_("Clear text"));
|
|
|
|
search_->setAutoHideButton(FancyLineEdit::Right, true);
|
2011-06-05 23:24:56 +00:00
|
|
|
#endif
|
2011-06-05 17:54:27 +00:00
|
|
|
connect(search_, SIGNAL(rightButtonClicked()), this, SLOT(resetSearch()));
|
|
|
|
connect(search_, SIGNAL(textEdited(QString)), this, SLOT(filterChanged(QString)));
|
2017-02-26 21:15:50 +00:00
|
|
|
connect(search_, SIGNAL(downPressed()),
|
|
|
|
list_, SLOT(setFocus()));
|
2011-06-05 17:54:27 +00:00
|
|
|
|
|
|
|
// Create the output layout, horizontal plus a VBox on the left with the search
|
|
|
|
// box and the tree
|
2012-10-26 00:42:27 +00:00
|
|
|
QVBoxLayout * left_layout = new QVBoxLayout;
|
2011-06-05 17:54:27 +00:00
|
|
|
left_layout->addWidget(search_, 0);
|
|
|
|
left_layout->addWidget(list_, 1);
|
|
|
|
|
|
|
|
QHBoxLayout * main_layout = new QHBoxLayout(this);
|
|
|
|
main_layout->addLayout(left_layout, 0);
|
|
|
|
main_layout->addWidget(stack_, 1);
|
2006-03-05 17:24:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-03-05 20:48:19 +00:00
|
|
|
void PanelStack::addCategory(QString const & name, QString const & parent)
|
2006-03-05 17:24:44 +00:00
|
|
|
{
|
2007-04-25 16:39:21 +00:00
|
|
|
QTreeWidgetItem * item = 0;
|
2006-03-05 17:24:44 +00:00
|
|
|
|
2008-05-02 22:07:51 +00:00
|
|
|
LYXERR(Debug::GUI, "addCategory n= " << name << " parent= ");
|
2006-03-05 17:24:44 +00:00
|
|
|
|
2006-11-05 12:30:12 +00:00
|
|
|
int depth = 1;
|
|
|
|
|
2008-03-05 20:48:19 +00:00
|
|
|
if (parent.isEmpty()) {
|
2006-03-05 17:24:44 +00:00
|
|
|
item = new QTreeWidgetItem(list_);
|
2013-05-12 16:30:59 +00:00
|
|
|
item->setText(0, qt_(name));
|
2006-03-05 17:24:44 +00:00
|
|
|
}
|
|
|
|
else {
|
2008-03-05 20:48:19 +00:00
|
|
|
if (!panel_map_.contains(parent))
|
2006-05-20 11:49:53 +00:00
|
|
|
addCategory(parent);
|
2008-03-05 20:48:19 +00:00
|
|
|
item = new QTreeWidgetItem(panel_map_.value(parent));
|
2013-05-12 16:30:59 +00:00
|
|
|
item->setText(0, qt_(name));
|
2006-11-05 12:30:12 +00:00
|
|
|
depth = 2;
|
2008-03-19 13:11:14 +00:00
|
|
|
list_->setRootIsDecorated(true);
|
2006-03-05 17:24:44 +00:00
|
|
|
}
|
|
|
|
|
2008-03-05 20:48:19 +00:00
|
|
|
panel_map_[name] = item;
|
2006-03-20 17:25:02 +00:00
|
|
|
|
2006-11-05 12:30:12 +00:00
|
|
|
QFontMetrics fm(list_->font());
|
2008-03-19 13:11:14 +00:00
|
|
|
|
2006-11-05 12:30:12 +00:00
|
|
|
// calculate the real size the current item needs in the listview
|
2017-02-05 17:02:34 +00:00
|
|
|
int itemsize = fm.width(qt_(name)) + 10 + list_->indentation() * depth;
|
2006-11-05 12:30:12 +00:00
|
|
|
// adjust the listview width to the max. itemsize
|
|
|
|
if (itemsize > list_->minimumWidth())
|
|
|
|
list_->setMinimumWidth(itemsize);
|
2006-03-05 17:24:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-03-05 20:48:19 +00:00
|
|
|
void PanelStack::addPanel(QWidget * panel, QString const & name, QString const & parent)
|
2006-03-05 17:24:44 +00:00
|
|
|
{
|
|
|
|
addCategory(name, parent);
|
2008-03-05 20:48:19 +00:00
|
|
|
QTreeWidgetItem * item = panel_map_.value(name);
|
2006-03-05 17:24:44 +00:00
|
|
|
widget_map_[item] = panel;
|
|
|
|
stack_->addWidget(panel);
|
2006-11-06 18:04:51 +00:00
|
|
|
stack_->setMinimumSize(panel->minimumSize());
|
2006-03-05 17:24:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-01-10 14:46:39 +00:00
|
|
|
void PanelStack::showPanel(QString const & name, bool show)
|
|
|
|
{
|
|
|
|
QTreeWidgetItem * item = panel_map_.value(name, 0);
|
|
|
|
LASSERT(item, return);
|
|
|
|
|
|
|
|
item->setHidden(!show);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-03-05 20:48:19 +00:00
|
|
|
void PanelStack::setCurrentPanel(QString const & name)
|
2006-03-05 17:24:44 +00:00
|
|
|
{
|
2008-03-05 20:48:19 +00:00
|
|
|
QTreeWidgetItem * item = panel_map_.value(name, 0);
|
2008-09-21 09:11:21 +00:00
|
|
|
LASSERT(item, return);
|
2006-03-05 17:24:44 +00:00
|
|
|
|
|
|
|
// force on first set
|
2008-03-05 20:48:19 +00:00
|
|
|
if (list_->currentItem() == item)
|
|
|
|
switchPanel(item);
|
2006-03-05 17:24:44 +00:00
|
|
|
|
2008-03-05 20:48:19 +00:00
|
|
|
list_->setCurrentItem(item);
|
2006-03-05 17:24:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-01-10 14:46:39 +00:00
|
|
|
bool PanelStack::isCurrentPanel(QString const & name) const
|
|
|
|
{
|
|
|
|
QTreeWidgetItem * item = panel_map_.value(name, 0);
|
|
|
|
LASSERT(item, return false);
|
|
|
|
|
|
|
|
return (list_->currentItem() == item);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-07-06 08:18:51 +00:00
|
|
|
void PanelStack::switchPanel(QTreeWidgetItem * item,
|
2008-03-19 18:15:16 +00:00
|
|
|
QTreeWidgetItem * previous)
|
2006-03-05 17:24:44 +00:00
|
|
|
{
|
2008-08-04 15:50:56 +00:00
|
|
|
// do nothing when clicked on whitespace (item=NULL)
|
2011-06-05 17:54:27 +00:00
|
|
|
if (!item)
|
2008-08-04 15:50:56 +00:00
|
|
|
return;
|
|
|
|
|
2008-03-19 13:11:14 +00:00
|
|
|
// if we have a category, expand the tree and go to the
|
2011-06-05 17:54:27 +00:00
|
|
|
// first enabled item
|
2008-03-19 13:11:14 +00:00
|
|
|
if (item->childCount() > 0) {
|
|
|
|
item->setExpanded(true);
|
2011-06-05 17:54:27 +00:00
|
|
|
if (previous && previous->parent() != item) {
|
|
|
|
// Looks for a child not disabled
|
|
|
|
for (int i = 0; i < item->childCount(); ++i) {
|
|
|
|
if (item->child(i)->flags() & Qt::ItemIsEnabled) {
|
|
|
|
switchPanel(item->child(i), previous);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-03-19 13:11:14 +00:00
|
|
|
}
|
2008-09-21 09:11:21 +00:00
|
|
|
else if (QWidget * w = widget_map_.value(item, 0)) {
|
|
|
|
stack_->setCurrentWidget(w);
|
|
|
|
}
|
2006-03-05 17:24:44 +00:00
|
|
|
}
|
2006-05-18 08:51:12 +00:00
|
|
|
|
2011-06-05 17:54:27 +00:00
|
|
|
static bool matches(QString const & input, QString const & search)
|
|
|
|
{
|
|
|
|
QString text = input;
|
|
|
|
|
|
|
|
// Check if the input contains the search string
|
|
|
|
return text.remove('&').contains(search, Qt::CaseInsensitive);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void setTreeItemStatus(QTreeWidgetItem * tree_item, bool enabled)
|
|
|
|
{
|
|
|
|
// Enable/disable the item
|
|
|
|
tree_item->setDisabled(!enabled);
|
|
|
|
|
|
|
|
// Change the color from black to gray or viceversa
|
|
|
|
QPalette::ColorGroup new_color = enabled ? QPalette::Active : QPalette::Disabled;
|
|
|
|
tree_item->setTextColor(0, QApplication::palette().color(new_color, QPalette::Text));
|
|
|
|
}
|
|
|
|
|
|
|
|
void PanelStack::hideEvent(QHideEvent * event)
|
|
|
|
{
|
|
|
|
QWidget::hideEvent(event);
|
|
|
|
|
|
|
|
// Programatically hidden (not simply minimized by the user)
|
|
|
|
if (!event->spontaneous()) {
|
|
|
|
resetSearch();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PanelStack::resetSearch()
|
|
|
|
{
|
|
|
|
search_->setText(QString());
|
|
|
|
search();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PanelStack::filterChanged(QString const & /*search*/)
|
|
|
|
{
|
|
|
|
// The text in the search box is changed, reset the timer
|
|
|
|
// and then search in the widgets
|
|
|
|
delay_search_->start(300);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PanelStack::search()
|
|
|
|
{
|
|
|
|
QString search = search_->text();
|
|
|
|
bool enable_all = search.isEmpty();
|
|
|
|
|
|
|
|
// If the search string is empty we enable all the items
|
|
|
|
// otherwise we disable everything and then selectively
|
|
|
|
// re-enable matching items
|
2016-06-09 20:54:31 +00:00
|
|
|
for (QTreeWidgetItem * tree_item : panel_map_) {
|
2011-06-05 17:54:27 +00:00
|
|
|
setTreeItemStatus(tree_item, enable_all);
|
|
|
|
}
|
|
|
|
|
2016-06-09 20:54:31 +00:00
|
|
|
for (QTreeWidgetItem * tree_item : panel_map_) {
|
2011-06-05 17:54:27 +00:00
|
|
|
// Current widget
|
|
|
|
QWidget * pane_widget = widget_map_[tree_item];
|
|
|
|
|
|
|
|
// First of all we look in the pane name
|
|
|
|
bool pane_matches = tree_item->text(0).contains(search, Qt::CaseInsensitive);
|
|
|
|
|
|
|
|
// If the tree item has an associated pane
|
|
|
|
if (pane_widget) {
|
|
|
|
// Loops on the list of children widgets (recursive)
|
|
|
|
QWidgetList children = pane_widget->findChildren<QWidget *>();
|
2016-06-09 20:54:31 +00:00
|
|
|
for (QWidget * child_widget : children) {
|
2011-06-05 17:54:27 +00:00
|
|
|
bool widget_matches = false;
|
|
|
|
|
|
|
|
// Try to cast to the most common widgets and looks in it's content
|
|
|
|
// It's bad OOP, it would be nice to have a QWidget::toString() overloaded by
|
|
|
|
// each widget, but this would require to change Qt or subclass each widget.
|
|
|
|
// Note that we have to ignore the amperstand symbol
|
|
|
|
if (QAbstractButton * button = qobject_cast<QAbstractButton *>(child_widget)) {
|
|
|
|
widget_matches = matches(button->text(), search);
|
|
|
|
|
|
|
|
} else if (QGroupBox * group_box = qobject_cast<QGroupBox *>(child_widget)) {
|
|
|
|
widget_matches = matches(group_box->title(), search);
|
|
|
|
|
|
|
|
} else if (QLabel * label = qobject_cast<QLabel *>(child_widget)) {
|
|
|
|
widget_matches = matches(label->text(), search);
|
|
|
|
|
|
|
|
} else if (QLineEdit * line_edit = qobject_cast<QLineEdit *>(child_widget)) {
|
|
|
|
widget_matches = matches(line_edit->text(), search);
|
|
|
|
|
|
|
|
} else if (QListWidget * list_widget = qobject_cast<QListWidget *>(child_widget)) {
|
|
|
|
widget_matches = (list_widget->findItems(search, Qt::MatchContains)).count() > 0;
|
|
|
|
|
|
|
|
} else if (QTreeWidget * tree_view = qobject_cast<QTreeWidget *>(child_widget)) {
|
|
|
|
widget_matches = (tree_view->findItems(search, Qt::MatchContains)).count() > 0;
|
|
|
|
|
|
|
|
} else if (QComboBox * combo_box = qobject_cast<QComboBox *>(child_widget)) {
|
|
|
|
widget_matches = (combo_box->findText(search, Qt::MatchContains)) != -1;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If this widget meets the search criteria
|
|
|
|
if (widget_matches && !enable_all) {
|
|
|
|
// The pane too meets the search criteria
|
|
|
|
pane_matches = true;
|
|
|
|
|
|
|
|
// Highlight the widget
|
|
|
|
QPalette widget_palette = child_widget->palette();
|
|
|
|
widget_palette.setColor(child_widget->foregroundRole(), Qt::red);
|
|
|
|
child_widget->setPalette(widget_palette);
|
|
|
|
} else {
|
|
|
|
// Reset the color of the widget
|
|
|
|
child_widget->setPalette(QApplication::palette(child_widget));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the pane meets the search criteria
|
|
|
|
if (pane_matches && !enable_all) {
|
|
|
|
// Expand and enable the pane and his ancestors (typically just the parent)
|
|
|
|
QTreeWidgetItem * item = tree_item;
|
|
|
|
do {
|
|
|
|
item->setExpanded(true);
|
|
|
|
setTreeItemStatus(item, true);
|
|
|
|
item = item->parent();
|
|
|
|
} while (item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2006-10-21 00:16:43 +00:00
|
|
|
|
2008-03-19 18:15:16 +00:00
|
|
|
void PanelStack::itemSelected(QTreeWidgetItem * item, int)
|
|
|
|
{
|
|
|
|
// de-select the category if a child is selected
|
|
|
|
if (item->childCount() > 0 && item->child(0)->isSelected())
|
|
|
|
item->setSelected(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-10-24 11:32:20 +00:00
|
|
|
QSize PanelStack::sizeHint() const
|
|
|
|
{
|
|
|
|
return QSize(list_->width() + stack_->width(),
|
|
|
|
qMax(list_->height(), stack_->height()));
|
|
|
|
}
|
2006-10-21 00:16:43 +00:00
|
|
|
|
2006-10-30 14:39:05 +00:00
|
|
|
} // namespace frontend
|
|
|
|
} // namespace lyx
|
|
|
|
|
2008-11-14 14:28:50 +00:00
|
|
|
#include "moc_PanelStack.cpp"
|