ControlLayout.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * ControlLayout.h - layout for controls
  3. *
  4. * Copyright (c) 2019-2019 Johannes Lorenz <j.git$$$lorenz-ho.me, $$$=@>
  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. /****************************************************************************
  25. **
  26. ** Copyright (C) 2016 The Qt Company Ltd.
  27. ** Contact: https://www.qt.io/licensing/
  28. **
  29. ** $QT_BEGIN_LICENSE:BSD$
  30. ** Commercial License Usage
  31. ** Licensees holding valid commercial Qt licenses may use this file in
  32. ** accordance with the commercial license agreement provided with the
  33. ** Software or, alternatively, in accordance with the terms contained in
  34. ** a written agreement between you and The Qt Company. For licensing terms
  35. ** and conditions see https://www.qt.io/terms-conditions. For further
  36. ** information use the contact form at https://www.qt.io/contact-us.
  37. **
  38. ** BSD License Usage
  39. ** Alternatively, you may use this file under the terms of the BSD license
  40. ** as follows:
  41. **
  42. ** "Redistribution and use in source and binary forms, with or without
  43. ** modification, are permitted provided that the following conditions are
  44. ** met:
  45. ** * Redistributions of source code must retain the above copyright
  46. ** notice, this list of conditions and the following disclaimer.
  47. ** * Redistributions in binary form must reproduce the above copyright
  48. ** notice, this list of conditions and the following disclaimer in
  49. ** the documentation and/or other materials provided with the
  50. ** distribution.
  51. ** * Neither the name of The Qt Company Ltd nor the names of its
  52. ** contributors may be used to endorse or promote products derived
  53. ** from this software without specific prior written permission.
  54. **
  55. **
  56. ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  57. ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  58. ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  59. ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  60. ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  61. ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  62. ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  63. ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  64. ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  65. ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  66. ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
  67. **
  68. ** $QT_END_LICENSE$
  69. **
  70. ****************************************************************************/
  71. #ifndef CONTROLLAYOUT_H
  72. #define CONTROLLAYOUT_H
  73. #include <QLayout>
  74. #include <QMultiMap>
  75. #include <QStyle>
  76. class QLayoutItem;
  77. class QRect;
  78. class QString;
  79. /**
  80. Layout for controls (models)
  81. Originally token from Qt's FlowLayout example. Modified.
  82. Features a search bar, as well as looking up widgets with string keys
  83. Keys have to be provided in the widgets' objectNames
  84. */
  85. class ControlLayout : public QLayout
  86. {
  87. Q_OBJECT
  88. public:
  89. explicit ControlLayout(QWidget *parent,
  90. int margin = -1, int hSpacing = -1, int vSpacing = -1);
  91. ~ControlLayout() override;
  92. void addItem(QLayoutItem *item) override;
  93. int horizontalSpacing() const;
  94. int verticalSpacing() const;
  95. Qt::Orientations expandingDirections() const override;
  96. bool hasHeightForWidth() const override;
  97. int heightForWidth(int) const override;
  98. int count() const override;
  99. QLayoutItem *itemAt(int index) const override;
  100. QLayoutItem *itemByString(const QString& key) const;
  101. QSize minimumSize() const override;
  102. void setGeometry(const QRect &rect) override;
  103. QSize sizeHint() const override;
  104. QLayoutItem *takeAt(int index) override;
  105. //! remove focus from QLineEdit search bar
  106. //! this may be useful if the mouse is outside the layout
  107. void removeFocusFromSearchBar();
  108. private slots:
  109. void onTextChanged(const QString&);
  110. private:
  111. int doLayout(const QRect &rect, bool testOnly) const;
  112. int smartSpacing(QStyle::PixelMetric pm) const;
  113. QMap<QString, QLayoutItem *>::const_iterator pairAt(int index) const;
  114. QMultiMap<QString, QLayoutItem *> m_itemMap;
  115. int m_hSpace;
  116. int m_vSpace;
  117. // relevant dimension is width, as later, heightForWidth() will be called
  118. // 400 looks good and is ~4 knobs in a row
  119. constexpr const static int m_minWidth = 400;
  120. class QLineEdit* m_searchBar;
  121. //! name of search bar, must be ASCII sorted before any alpha numerics
  122. static constexpr const char* s_searchBarName = "!!searchBar!!";
  123. };
  124. #endif // CONTROLLAYOUT_H