[prev in list] [next in list] [prev in thread] [next in thread]
List: kde-commits
Subject: [labplot] src/commonfrontend/widgets: In TreeViewComboBox hide items not matching the filter string
From: Alexander Semke <alexander.semke () web ! de>
Date: 2016-05-14 18:34:13
Message-ID: E1b1eOD-0005AN-Qj () scm ! kde ! org
[Download RAW message or body]
Git commit 5cec0cb8569f1c6a101109157a4d0b6e3db38923 by Alexander Semke.
Committed on 14/05/2016 at 18:32.
Pushed by asemke into branch 'master'.
In TreeViewComboBox hide items not matching the filter string instead of disabling \
them.
M +78 -34 src/commonfrontend/widgets/TreeViewComboBox.cpp
M +40 -40 src/commonfrontend/widgets/TreeViewComboBox.h
http://commits.kde.org/labplot/5cec0cb8569f1c6a101109157a4d0b6e3db38923
diff --git a/src/commonfrontend/widgets/TreeViewComboBox.cpp \
b/src/commonfrontend/widgets/TreeViewComboBox.cpp index 67b69e1..6fea6d4 100644
--- a/src/commonfrontend/widgets/TreeViewComboBox.cpp
+++ b/src/commonfrontend/widgets/TreeViewComboBox.cpp
@@ -30,8 +30,14 @@
#include "commonfrontend/widgets/TreeViewComboBox.h"
#include "backend/core/AbstractAspect.h"
#include "backend/core/AspectTreeModel.h"
+
+#include <QEvent>
#include <QHeaderView>
-#include <QLayout>
+#include <QLineEdit>
+#include <QVBoxLayout>
+#include <QGroupBox>
+#include <QTreeView>
+
#include <KLocalizedString>
#include <KLineEdit>
@@ -41,20 +47,20 @@
\ingroup backend/widgets
*/
-TreeViewComboBox::TreeViewComboBox(QWidget* parent):QComboBox(parent){
+TreeViewComboBox::TreeViewComboBox(QWidget* parent) : QComboBox(parent) {
- m_layout = new QVBoxLayout;
+ QVBoxLayout* layout = new QVBoxLayout;
m_treeView = new QTreeView;
m_lineEdit = new KLineEdit;
m_groupBox = new QGroupBox;
- m_layout->setContentsMargins(0, 0, 0, 0);
- m_layout->setSpacing(0);
+ layout->setContentsMargins(0, 0, 0, 0);
+ layout->setSpacing(0);
- m_layout->addWidget(m_lineEdit);
- m_layout->addWidget(m_treeView);
+ layout->addWidget(m_lineEdit);
+ layout->addWidget(m_treeView);
- m_groupBox->setLayout(m_layout);
+ m_groupBox->setLayout(layout);
m_groupBox->setParent(parent, Qt::Popup);
m_groupBox->hide();
m_groupBox->installEventFilter(this);
@@ -71,14 +77,13 @@ TreeViewComboBox::TreeViewComboBox(QWidget* \
parent):QComboBox(parent){ setCurrentIndex(0);
connect(m_treeView, SIGNAL(activated(QModelIndex)), this, \
SLOT(treeViewIndexActivated(QModelIndex)) );
- connect(m_lineEdit, SIGNAL(textChanged(QString)), this, \
SLOT(lineEditTextEdited(QString))); + connect(m_lineEdit, \
SIGNAL(textChanged(QString)), this, SLOT(filterChanged(QString))); }
void TreeViewComboBox::setTopLevelClasses(QList<const char*> list) {
m_topLevelClasses=list;
}
-
void TreeViewComboBox::setSelectableClasses(QList<const char*> list) {
m_selectableClasses=list;
}
@@ -86,13 +91,12 @@ void TreeViewComboBox::setSelectableClasses(QList<const char*> \
list) { /*!
Sets the \a model for the view to present.
*/
-void TreeViewComboBox::setModel(QAbstractItemModel *model){
+void TreeViewComboBox::setModel(QAbstractItemModel* model) {
m_treeView->setModel(model);
//show only the first column in the combo box
- for (int i=1; i<model->columnCount(); i++){
+ for (int i=1; i<model->columnCount(); i++)
m_treeView->hideColumn(i);
- }
//Expand the complete tree in order to see everything in the first popup.
m_treeView->expandAll();
@@ -102,18 +106,16 @@ void TreeViewComboBox::setModel(QAbstractItemModel *model){
Sets the current item to be the item at \a index and selects it.
\sa currentIndex()
*/
-void TreeViewComboBox::setCurrentModelIndex(const QModelIndex& index){
-// view()->setCurrentIndex(index);
+void TreeViewComboBox::setCurrentModelIndex(const QModelIndex& index) {
m_treeView->setCurrentIndex(index);
QComboBox::setItemText(0, index.data().toString());
}
/*!
Returns the model index of the current item.
-
\sa setCurrentModelIndex()
*/
-QModelIndex TreeViewComboBox::currentModelIndex() const{
+QModelIndex TreeViewComboBox::currentModelIndex() const {
return m_treeView->currentIndex();
}
@@ -121,13 +123,14 @@ QModelIndex TreeViewComboBox::currentModelIndex() const{
Displays the tree view of items in the combobox.
Triggers showTopLevelOnly() to show toplevel items only.
*/
-void TreeViewComboBox::showPopup(){
+void TreeViewComboBox::showPopup() {
if (!m_treeView->model() || !m_treeView->model()->hasChildren())
return;
QModelIndex root = m_treeView->model()->index(0,0);
showTopLevelOnly(root);
+ m_lineEdit->setText("");
m_groupBox->show();
m_groupBox->resize(this->width(), 250);
m_groupBox->move(mapToGlobal( this->rect().topLeft() ));
@@ -137,20 +140,18 @@ void TreeViewComboBox::hidePopup() {
m_groupBox->hide();
}
-
/*!
Hides the non-toplevel items of the model used in the tree view.
*/
-void TreeViewComboBox::showTopLevelOnly(const QModelIndex & index){
+void TreeViewComboBox::showTopLevelOnly(const QModelIndex & index) {
int rows = index.model()->rowCount(index);
- QModelIndex currentChild;
bool isTopLevel;
for (int i=0; i<rows; i++) {
- currentChild = index.child(i, 0);
- showTopLevelOnly(currentChild);
- AbstractAspect* aspect = \
static_cast<AbstractAspect*>(currentChild.internalPointer()); + QModelIndex child = \
index.child(i, 0); + showTopLevelOnly(child);
+ AbstractAspect* aspect = static_cast<AbstractAspect*>(child.internalPointer());
isTopLevel = false;
- foreach(const char * classString, m_topLevelClasses)
+ foreach(const char* classString, m_topLevelClasses)
if (aspect->inherits(classString)) {
if ( strcmp(classString, "Spreadsheet")==0 ) {
if (aspect->inherits("FileDataSource"))
@@ -169,8 +170,8 @@ void TreeViewComboBox::showTopLevelOnly(const QModelIndex & \
index){ /*!
catches the MouseButtonPress-event and hides the tree view on mouse clicking.
*/
-bool TreeViewComboBox::eventFilter(QObject *object, QEvent *event){
- if ( (object==m_groupBox) && event->type()==QEvent::MouseButtonPress){
+bool TreeViewComboBox::eventFilter(QObject* object, QEvent* event) {
+ if ( (object==m_groupBox) && event->type()==QEvent::MouseButtonPress ) {
m_groupBox->hide();
this->setFocus();
return true;
@@ -180,7 +181,7 @@ bool TreeViewComboBox::eventFilter(QObject *object, QEvent \
*event){
//SLOTs
-void TreeViewComboBox::treeViewIndexActivated( const QModelIndex & index){
+void TreeViewComboBox::treeViewIndexActivated(const QModelIndex& index) {
if (index.internalPointer()) {
AbstractAspect* aspect = static_cast<AbstractAspect*>(index.internalPointer());
const char* currentClassName = aspect->metaObject()->className();
@@ -202,11 +203,54 @@ void TreeViewComboBox::treeViewIndexActivated( const \
QModelIndex & index){ m_groupBox->hide();
}
-void TreeViewComboBox::lineEditTextEdited(const QString &text) {
- AspectTreeModel * model = qobject_cast<AspectTreeModel *>(m_treeView->model());
- if(!model)
- return;
+void TreeViewComboBox::filterChanged(const QString& text) {
+ QModelIndex root = m_treeView->model()->index(0,0);
+ filter(root, text);
+}
+
+bool TreeViewComboBox::filter(const QModelIndex& index, const QString& text) {
+ bool childVisible = false;
+ const int rows = index.model()->rowCount(index);
+ for (int i=0; i<rows; i++) {
+ QModelIndex child = index.child(i, 0);
+ AbstractAspect* aspect = static_cast<AbstractAspect*>(child.internalPointer());
+ bool isTopLevel = false;
+
+ //check whether current item is one of the allowed top level types
+ foreach(const char* classString, m_topLevelClasses) {
+ if (aspect->inherits(classString)) {
+ if ( strcmp(classString, "Spreadsheet")==0 ) {
+ if (aspect->inherits("FileDataSource"))
+ isTopLevel = false;
+ else
+ isTopLevel = true;
+ } else {
+ isTopLevel = true;
+ }
+ }
+ }
+
+ bool visible = false;
+ if (isTopLevel)
+ visible = aspect->name().contains(text, Qt::CaseInsensitive);
+
+ if (visible) {
+ //current item is visible -> make all its children visible without applying the \
filter + for (int j=0; j<child.model()->rowCount(child); ++j) {
+ QModelIndex c = child.child(j, 0);
+ m_treeView->setRowHidden(j, c, false);
+ }
+
+ childVisible = true;
+ } else {
+ //check children items. if one of the children is visible, make the parent \
(current) item visible too. + visible = filter(child, text);
+ if (visible)
+ childVisible = true;
+ }
+
+ m_treeView->setRowHidden(i, index, !visible);
+ }
- model->setFilterString(text);
- m_treeView->update();
+ return childVisible;
}
diff --git a/src/commonfrontend/widgets/TreeViewComboBox.h \
b/src/commonfrontend/widgets/TreeViewComboBox.h index 5514f0a..4cba138 100644
--- a/src/commonfrontend/widgets/TreeViewComboBox.h
+++ b/src/commonfrontend/widgets/TreeViewComboBox.h
@@ -3,8 +3,7 @@
Project : LabPlot
Description : Provides a QTreeView in a QComboBox
--------------------------------------------------------------------
- Copyright : (C) 2008-2011 by Alexander Semke (alexander.semke@web.de)
- Copyright : (C) 2008 Tilman Benkert (thzs@gmx.net)
+ Copyright : (C) 2008-2016 by Alexander Semke (alexander.semke@web.de)
***************************************************************************/
@@ -30,45 +29,46 @@
#ifndef TREEVIEWCOMBOBOX_H
#define TREEVIEWCOMBOBOX_H
-#include <QTreeView>
-#include <QLineEdit>
#include <QComboBox>
-#include <QGroupBox>
-#include <QVBoxLayout>
-#include <QEvent>
-
-class AbstractAspect;
-
-class TreeViewComboBox : public QComboBox{
-Q_OBJECT
-
-public:
- explicit TreeViewComboBox(QWidget* parent = 0);
-
- void setModel(QAbstractItemModel *model);
- void setCurrentModelIndex(const QModelIndex&);
- void setTopLevelClasses(QList<const char*>);
- void setSelectableClasses(QList<const char*>);
- QModelIndex currentModelIndex() const;
- virtual void showPopup();
- virtual void hidePopup();
-
-private:
- QGroupBox *m_groupBox;
- QVBoxLayout *m_layout;
- QLineEdit *m_lineEdit;
- QTreeView *m_treeView;
- QList<const char*> m_topLevelClasses;
- QList<const char*> m_selectableClasses;
- void showTopLevelOnly(const QModelIndex & index);
- bool eventFilter(QObject *obj, QEvent *event);
-
-private slots:
- void treeViewIndexActivated(const QModelIndex&);
- void lineEditTextEdited(const QString &);
-
-signals:
- void currentModelIndexChanged(const QModelIndex&);
+
+class QGroupBox;
+class QLineEdit;
+class QTreeView;
+
+class TreeViewComboBox : public QComboBox {
+ Q_OBJECT
+
+ public:
+ explicit TreeViewComboBox(QWidget* parent = 0);
+
+ void setModel(QAbstractItemModel*);
+ void setCurrentModelIndex(const QModelIndex&);
+ QModelIndex currentModelIndex() const;
+
+ void setTopLevelClasses(QList<const char*>);
+ void setSelectableClasses(QList<const char*>);
+
+ virtual void showPopup();
+ virtual void hidePopup();
+
+ private:
+ QTreeView* m_treeView;
+ QGroupBox* m_groupBox;
+ QLineEdit* m_lineEdit;
+
+ QList<const char*> m_topLevelClasses;
+ QList<const char*> m_selectableClasses;
+
+ void showTopLevelOnly(const QModelIndex&);
+ bool eventFilter(QObject*, QEvent*);
+ bool filter(const QModelIndex&, const QString&);
+
+ private slots:
+ void treeViewIndexActivated(const QModelIndex&);
+ void filterChanged(const QString&);
+
+ signals:
+ void currentModelIndexChanged(const QModelIndex&);
};
#endif
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic