ComboBoxModel.h 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * ComboBoxModel.h - declaration of class ComboBoxModel
  3. *
  4. * Copyright (c) 2008-2014 Tobias Doerffel <tobydox/at/users.sourceforge.net>
  5. *
  6. * This file is part of LMMS - https://lmms.io
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2 of the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public
  19. * License along with this program (see COPYING); if not, write to the
  20. * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  21. * Boston, MA 02110-1301 USA.
  22. *
  23. */
  24. #ifndef COMBOBOX_MODEL_H
  25. #define COMBOBOX_MODEL_H
  26. #include <cassert>
  27. #include <memory>
  28. #include <utility>
  29. #include <vector>
  30. #include "AutomatableModel.h"
  31. #include "embed.h"
  32. class LMMS_EXPORT ComboBoxModel : public IntModel
  33. {
  34. Q_OBJECT
  35. MODEL_IS_VISITABLE
  36. public:
  37. ComboBoxModel( Model* parent = nullptr,
  38. const QString& displayName = QString(),
  39. bool isDefaultConstructed = false ) :
  40. IntModel( 0, 0, 0, parent, displayName, isDefaultConstructed )
  41. {
  42. }
  43. virtual ~ComboBoxModel()
  44. {
  45. clear();
  46. }
  47. void addItem( QString item, std::unique_ptr<PixmapLoader> loader = nullptr );
  48. void replaceItem(std::size_t index, QString item, std::unique_ptr<PixmapLoader> loader = nullptr);
  49. void clear();
  50. int findText( const QString& txt ) const;
  51. QString currentText() const
  52. {
  53. return ( size() > 0 && value() < size() ) ? m_items[value()].first : QString();
  54. }
  55. const PixmapLoader* currentData() const
  56. {
  57. return m_items[value()].second.get();
  58. }
  59. const QString & itemText( int i ) const
  60. {
  61. return m_items[qBound<int>( minValue(), i, maxValue() )].first;
  62. }
  63. const PixmapLoader* itemPixmap( int i ) const
  64. {
  65. return m_items[qBound<int>( minValue(), i, maxValue() )].second.get();
  66. }
  67. int size() const
  68. {
  69. return m_items.size();
  70. }
  71. private:
  72. typedef std::pair<QString, std::unique_ptr<PixmapLoader> > Item;
  73. std::vector<Item> m_items;
  74. } ;
  75. #endif