Files
ResInsight/Fwk/AppFwk/cafUserInterface/cafPdmUiTreeSelectionEditor.h
Magne Sjaastad 40080a99de 9978 Improve UI for long drop-down lists, use tree selection more
* Improve tree selection editor
- always call defineEditorAttributes
- use heightHint in editor attributes 
- use tree selection editor as default editor for std::vector

* Use tree selection editor instead of list selection editor
List selection editor must be used when editing std::vector<cvf::vec3d> and similar. Replace other use of list selection editor with tree selection editor.

* Set checked state based on text string for integer only models
For models with only integer values, use text string to define the items to be selected. The full list will always be visible, and the checked state will be updated when editing the filter text.

Example: "1, 5-7" will set items 1, 5, 6, 7 checked and all other items unchecked

* Minor fixes
- Set placeholder text after content is added (to ensure correct data type)
- Fix check of integers. `canConvert<int>()`returns true for both QString and int. Thus convert to string and then check for int conversion.

* Activate filtering when unchecking all items in list with only integers
- Reactivate filtering when uncheck of all items for a list with only integer values (to keep consistency between filter and list)
- Update function name for clarity

---------

Co-authored-by: Jørgen Herje <jorgen.herje@ceetronsolutions.com>
2023-05-22 15:44:37 +02:00

154 lines
5.2 KiB
C++

//##################################################################################################
//
// Custom Visualization Core library
//
// This library may be used under the terms of either the GNU General Public License or
// the GNU Lesser General Public License as follows:
//
// GNU General Public License Usage
// This library is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This library is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// See the GNU General Public License at <<http://www.gnu.org/licenses/gpl.html>>
// for more details.
//
// GNU Lesser General Public License Usage
// This library is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// This library is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// See the GNU Lesser General Public License at <<http://www.gnu.org/licenses/lgpl-2.1.html>>
// for more details.
//
//##################################################################################################
#pragma once
#include "cafPdmUiFieldEditorHandle.h"
#include <QAbstractItemModel>
class QLabel;
class QTreeView;
class QAbstractItemModel;
class QCheckBox;
class QLineEdit;
class QSortFilterProxyModel;
class QModelIndex;
class QItemSelection;
namespace caf
{
class PdmUiTreeSelectionQModel;
class FilterLeafNodesOnlyProxyModel;
class QTreeViewHeightHint;
//==================================================================================================
///
//==================================================================================================
class PdmUiTreeSelectionEditorAttribute : public PdmUiEditorAttribute
{
public:
bool showTextFilter;
bool showToggleAllCheckbox;
bool singleSelectionMode;
bool setCurrentIndexWhenItemIsChecked;
int heightHint;
/// currentIndexFieldHandle is used to communicate the value of current item in the tree view
/// This is useful when displaying a list of appEnums, and a dependent view is displaying content based on
/// the current item in the tree view
/// Make sure the type of the receiving field is of the same type as the field used in PdmUiTreeSelectionEditor
caf::PdmFieldHandle* currentIndexFieldHandle;
public:
PdmUiTreeSelectionEditorAttribute()
{
showTextFilter = true;
showToggleAllCheckbox = true;
singleSelectionMode = false;
setCurrentIndexWhenItemIsChecked = false;
heightHint = -1;
currentIndexFieldHandle = nullptr;
}
};
//==================================================================================================
///
//==================================================================================================
class PdmUiTreeSelectionEditor : public PdmUiFieldEditorHandle
{
Q_OBJECT
CAF_PDM_UI_FIELD_EDITOR_HEADER_INIT;
public:
PdmUiTreeSelectionEditor();
~PdmUiTreeSelectionEditor() override;
protected:
void configureAndUpdateUi( const QString& uiConfigName ) override;
QWidget* createEditorWidget( QWidget* parent ) override;
QWidget* createLabelWidget( QWidget* parent ) override;
QMargins calculateLabelContentMargins() const override;
bool isMultiRowEditor() const override;
private slots:
void customMenuRequested( const QPoint& pos );
void slotSetSelectedOn();
void slotSetSelectedOff();
void slotSetSubItemsOn();
void slotSetSubItemsOff();
void slotToggleAll();
void slotInvertCheckedStateForSelection();
void slotInvertCheckedStateOfAll();
void slotTextFilterChanged();
void slotClicked( const QModelIndex& index );
void slotScrollToFirstCheckedItem();
private:
void currentChanged( const QModelIndex& current );
void setCheckedStateOfSelected( bool checked );
void setCheckedStateForSubItemsOfSelected( bool checked );
void checkAllItems();
void unCheckAllItems();
void setCheckedStateForIntegerItemsMatchingFilter();
QModelIndexList allVisibleSourceModelIndices() const;
void recursiveAppendVisibleSourceModelIndices( const QModelIndex& parent, QModelIndexList* sourceModelIndices ) const;
static bool hasOnlyIntegers( const QAbstractItemModel* model );
private:
QPointer<QTreeViewHeightHint> m_treeView;
QPointer<QShortenedLabel> m_label;
QPointer<QCheckBox> m_toggleAllCheckBox;
QPointer<QLineEdit> m_textFilterLineEdit;
PdmUiTreeSelectionQModel* m_model;
QSortFilterProxyModel* m_proxyModel;
PdmUiTreeSelectionEditorAttribute m_attributes;
bool m_useSingleSelectionMode;
};
} // end namespace caf