ComboBox.cpp 2.0 KB

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