Files
qt5-legatus-legacy/Lts/Common/LtsWidgets/lts_combo_box.cpp
T
2026-08-13 18:33:10 +02:00

126 lines
3.4 KiB
C++
Executable File

/**
* @RDE_HEADER@
* ComboBox, optimized for tablets (bigger dropdown/popup and more comfortable completer)
* $Id$
* $HeadURL$
* $Author$
*
* Created by Mike Wille, SLSE, 2016-03-09
*/
#include "lts_combo_box.h"
#include "lts_selection_popup.h"
#include "lts_message_dialog.h"
#include <QGuiApplication>
#include <QShowEvent>
//#define NEVER
#ifdef NEVER
# include <QDebug>
#endif // NEVER
namespace LtsWidgets
{
bool LtsComboBox::s_useSelectionDialog = false;
LtsComboBox::LtsComboBox(QWidget *parent)
: QComboBox(parent)
, m_currentIndex(-1)
{
connect(this, &LtsComboBox::signalExternallyChanged,
this, &LtsComboBox::signalRefill); // sig on sig (to fetch cbx as sender)
}
void LtsComboBox::setUseSelectionDialog(bool bUseSelectionDialog)
{
s_useSelectionDialog = bUseSelectionDialog;
}
void LtsComboBox::showEvent(QShowEvent *e)
{
QComboBox::showEvent(e);
m_currentIndex = currentIndex();
}
void LtsComboBox::enterEvent(QEvent *e)
{
BaseClass::enterEvent(e);
emit signalEnterEvent();
}
void LtsComboBox::leaveEvent(QEvent *e)
{
BaseClass::leaveEvent(e);
emit signalLeaveEvent();
}
void LtsComboBox::showPopup()
{
bool bUseSelectionDialog = s_useSelectionDialog;
if (QGuiApplication::queryKeyboardModifiers() == Qt::ControlModifier)
bUseSelectionDialog = !bUseSelectionDialog;
if (!bUseSelectionDialog)
{
QComboBox::showPopup(); // show original popup when CTRL is pressed
return;
}
if (!model()->rowCount()) // nothing to show? tell the user.
{
LtsWidgets::LtsMessageDialog::showMessage(this, tr("There are no items available!"));
return;
}
// create the touch-friendly, screen-centered custom popup and show it
LtsSelectionPopup* popup = new LtsSelectionPopup(model(), currentIndex(), this);
popup->setAttribute(Qt::WA_DeleteOnClose); // dialog must delete itself, as there is no Dialog Helper
#ifdef NEVER
// debugging :
// // highlighted -> UserInteraction
// connect(popup, static_cast<void (LtsSelectionPopup::*)(int)>(&LtsSelectionPopup::highlighted), this, [](int idx){
// qCritical() << " LtsComboBox: called: highlighted(int), idx=" << idx;
// });
// currentIndexChanged -> programmatically & UserInteraction
connect(popup, static_cast<void (LtsSelectionPopup::*)(int)>(&LtsSelectionPopup::currentIndexChanged), this, [](int idx){
qCritical() << " LtsComboBox: called: currentIndexChanged(int), idx=" << idx;
});
// activated -> UserInteraction
connect(popup, static_cast<void (LtsSelectionPopup::*)(int)>(&LtsSelectionPopup::activated), this, [](int idx){
qCritical() << " LtsComboBox: called: activated(int), idx=" << idx;
});
// : debugging
#endif // NEVER
// Although we derive from QComboBox, there seems to be no signal emitted implicitly.
// Therefore we have to listen to our popup:
connect(popup, &LtsSelectionPopup::currentIndexChanged,
this, &LtsComboBox::userChangedIndex); // listen on changes
connect(popup, &LtsSelectionPopup::activated,
this, static_cast<void (LtsComboBox::*)(int)>(&LtsComboBox::activated)); // SIGNAL on SIGNAL !
popup->show();
}
void LtsComboBox::userChangedIndex(int index)
{
m_currentIndex = index;
setCurrentIndex(index);
}
bool LtsComboBox::hasValueChanged() const
{
if (m_currentIndex == -1)
return false;
return !(currentIndex() == m_currentIndex);
}
} // namespace LtsWidgets