Skip to content

Commit

Permalink
Fix includes and added some documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasKroes committed Dec 10, 2024
1 parent a21fa94 commit 0046621
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 53 deletions.
2 changes: 1 addition & 1 deletion ManiVault/src/actions/OptionsAction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ OptionsAction::ComboBoxWidget::ComboBoxWidget(QWidget* parent, OptionsAction* op
setLayout(&_layout);
}

void OptionsAction::ComboBoxWidget::updateCurrentText()
void OptionsAction::ComboBoxWidget::updateCurrentText() const
{
const auto selectedOptions = _optionsAction->getSelectedOptions();

Expand Down
2 changes: 1 addition & 1 deletion ManiVault/src/actions/OptionsAction.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ class CORE_EXPORT OptionsAction : public WidgetAction
bool eventFilter(QObject* target, QEvent* event) override;

/** Updates the line edit text to the joined selected strings */
void updateCurrentText();
void updateCurrentText() const;

protected:
OptionsAction* _optionsAction; /** Pointer to owning options action */
Expand Down
54 changes: 9 additions & 45 deletions ManiVault/src/widgets/MultiSelectComboBox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,25 @@

#include "MultiSelectComboBox.h"

#include <QAbstractItemView>
#include <QMouseEvent>

namespace mv::gui {

MultiSelectComboBox::MultiSelectComboBox(QWidget* parent) :
QComboBox(parent),
_preventHidePopup(true)
{
setEditable(true);

// Connect the activated signal to handle item toggling

updateDisplayText();
}

void MultiSelectComboBox::init()
{
// Install an event filter to prevent automated popup hide
view()->installEventFilter(this);
view()->parentWidget()->installEventFilter(this);
// Install an event filter to track clicks outside
//QApplication::instance()->installEventFilter(this);
}

void MultiSelectComboBox::showPopup()
{
QComboBox::showPopup();

_popupOpen = true;
// Install an event filter to hide the popup when clicked outside of it
view()->parentWidget()->installEventFilter(this);
}

void MultiSelectComboBox::hidePopup()
Expand All @@ -46,34 +38,23 @@ void MultiSelectComboBox::hidePopup()
bool MultiSelectComboBox::eventFilter(QObject* watched, QEvent* event)
{
if (event->type() == QEvent::MouseButtonPress) {
//auto* mouseEvent = static_cast<QMouseEvent*>(event);
//QWidget* popup = view()->window(); // Get the popup window
//if (popup && _popupOpen && !popup->geometry().contains(mouseEvent->globalPos())) {
// hidePopup(); // Close the popup if the click is outside
//}

const auto mouseEvent = dynamic_cast<QMouseEvent*>(event);

if (watched == view()) {
qDebug() << "Clicked inside";

const auto index = view()->indexAt(mouseEvent->pos());
const auto index = view()->indexAt(mouseEvent->pos());

if (index.isValid()) {
toggleItemCheckState(index);

return true;
}
} else {
qDebug() << "Clicked outside";
}

if (watched == view()->parentWidget()) {
_preventHidePopup = false;
hidePopup();
}
}

if (event->type() == QEvent::FocusOut)
qDebug() << "Focus out";

return QComboBox::eventFilter(watched, event);
}

Expand All @@ -83,21 +64,4 @@ void MultiSelectComboBox::toggleItemCheckState(const QModelIndex& index) const
model()->setData(index, !model()->data(index, Qt::CheckStateRole).toBool(), Qt::CheckStateRole);
}

void MultiSelectComboBox::updateDisplayText()
{
QStringList selectedItems;

auto* model = dynamic_cast<QStandardItemModel*>(this->model());

for (int i = 0; i < model->rowCount(); ++i) {
auto* item = model->item(i);

if (item && item->checkState() == Qt::Checked) {
selectedItems << item->text();
}
}

setEditText(selectedItems.join(", "));
}

}
36 changes: 30 additions & 6 deletions ManiVault/src/widgets/MultiSelectComboBox.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,58 @@

#include "ManiVaultGlobals.h"

class QWidget;
#include <QComboBox>

namespace mv::gui {

/**
* Multi-select combobox widget class
*
* Allows to select multiple items during a single popup sessions.
*
* @author Thomas Kroes
*/
class CORE_EXPORT MultiSelectComboBox : public QComboBox {

Q_OBJECT

public:


/**
* Construct with pointer to \p parent widget
* @param parent Pointer to parent widget
*/
explicit MultiSelectComboBox(QWidget* parent = nullptr);

/** Call this post QComboBox::setView() to setup connections */
void init();

protected:
void showPopup() override;

/** Override to customize popup hide behaviour */
void hidePopup() override;


/**
* Called when \p event happens for \p watched object
* @param watched Pointer to watched object
* @param event Pointer to event that occurred
* @return Whether the event was handled or not
*/
bool eventFilter(QObject* watched, QEvent* event) override;

private:
void toggleItemCheckState(const QModelIndex& index) const;

void updateDisplayText();

/**
* Toggle the check state of \p index
* @param index Index of which to toggle the check state
*/
void toggleItemCheckState(const QModelIndex& index) const;

private:
bool _popupOpen = false;
bool _preventHidePopup = false;
bool _preventHidePopup = false; /** Whether to prevent the popup from closing or not */
};

}

0 comments on commit 0046621

Please sign in to comment.