ComboBox.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /******************************************************************************
  2. Copyright (C) 2024 by Taylor Giampaolo <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ******************************************************************************/
  14. #include <Idian/ComboBox.hpp>
  15. #include <Idian/Row.hpp>
  16. #include <QTimer>
  17. #include <Idian/moc_ComboBox.cpp>
  18. using idian::ComboBox;
  19. ComboBox::ComboBox(QWidget *parent) : QComboBox(parent), Utils(this)
  20. {
  21. Utils::applyStateStylingEventFilter(this);
  22. }
  23. void ComboBox::showPopup()
  24. {
  25. if (allowOpeningPopup) {
  26. QComboBox::showPopup();
  27. }
  28. }
  29. void ComboBox::hidePopup()
  30. {
  31. // It would be nice to find a better way to do this.
  32. //
  33. // When the dropdown is closed, block attempts to open it
  34. // again for a short time. This is so clicking a GenericRow
  35. // with the dropdown open doesn't immediately close and re-open it.
  36. // I have tried all sorts of things involving handling mouse events
  37. // and event filters on both GenericRow and ComboBox.
  38. //
  39. // All my efforts have failed so we get this instead.
  40. allowOpeningPopup = false;
  41. QTimer::singleShot(120, this, [this]() { allowOpeningPopup = true; });
  42. QComboBox::hidePopup();
  43. }
  44. void ComboBox::mousePressEvent(QMouseEvent *event)
  45. {
  46. QComboBox::mousePressEvent(event);
  47. }
  48. void ComboBox::togglePopup()
  49. {
  50. if (view()->isVisible()) {
  51. ComboBox::hidePopup();
  52. } else {
  53. ComboBox::showPopup();
  54. }
  55. }