ComboBox.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. void ComboBox::showPopup()
  21. {
  22. if (allowOpeningPopup) {
  23. QComboBox::showPopup();
  24. }
  25. }
  26. void ComboBox::hidePopup()
  27. {
  28. // It would be nice to find a better way to do this.
  29. //
  30. // When the dropdown is closed, block attempts to open it
  31. // again for a short time. This is so clicking a GenericRow
  32. // with the dropdown open doesn't immediately close and re-open it.
  33. // I have tried all sorts of things involving handling mouse events
  34. // and event filters on both GenericRow and ComboBox.
  35. //
  36. // All my efforts have failed so we get this instead.
  37. allowOpeningPopup = false;
  38. QTimer::singleShot(120, this, [this]() { allowOpeningPopup = true; });
  39. QComboBox::hidePopup();
  40. }
  41. void ComboBox::mousePressEvent(QMouseEvent *event)
  42. {
  43. QComboBox::mousePressEvent(event);
  44. }
  45. void ComboBox::togglePopup()
  46. {
  47. if (view()->isVisible()) {
  48. ComboBox::hidePopup();
  49. } else {
  50. ComboBox::showPopup();
  51. }
  52. }