LibrePCB Developers Documentation
comboboxdelegate.h
Go to the documentation of this file.
1 /*
2  * LibrePCB - Professional EDA for everyone!
3  * Copyright (C) 2013 LibrePCB Developers, see AUTHORS.md for contributors.
4  * https://librepcb.org/
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef LIBREPCB_COMBOBOXDELEGATE_H
21 #define LIBREPCB_COMBOBOXDELEGATE_H
22 
23 /*******************************************************************************
24  * Includes
25  ******************************************************************************/
26 #include <QtCore>
27 #include <QtWidgets>
28 
29 /*******************************************************************************
30  * Namespace / Forward Declarations
31  ******************************************************************************/
32 namespace librepcb {
33 
34 class GraphicsLayer;
35 
36 /*******************************************************************************
37  * Class ComboBoxDelegate
38  ******************************************************************************/
39 
43 class ComboBoxDelegate final : public QStyledItemDelegate {
44  Q_OBJECT
45 
46 public:
47  // Types
48  struct Item final {
49  QString text;
50  QIcon icon;
51  QVariant data;
52  };
53  struct Items final : public QVector<Item> {
54  void sort() noexcept {
55  QCollator collator;
56  collator.setCaseSensitivity(Qt::CaseInsensitive);
57  collator.setIgnorePunctuation(false);
58  collator.setNumericMode(true);
59  std::sort(begin(), end(), [&collator](const Item& lhs, const Item& rhs) {
60  return collator(lhs.text, rhs.text);
61  });
62  }
63  };
64 
65  // Constructors / Destructor
66  explicit ComboBoxDelegate(bool editable = false,
67  QObject* parent = nullptr) noexcept;
68  ComboBoxDelegate(const ComboBoxDelegate& other) = delete;
69  ~ComboBoxDelegate() noexcept;
70 
71  // Inherited from QStyledItemDelegate
72  QWidget* createEditor(QWidget* parent, const QStyleOptionViewItem& option,
73  const QModelIndex& index) const override;
74  void setEditorData(QWidget* editor, const QModelIndex& index) const override;
75  void setModelData(QWidget* editor, QAbstractItemModel* model,
76  const QModelIndex& index) const override;
77  void updateEditorGeometry(QWidget* editor, const QStyleOptionViewItem& option,
78  const QModelIndex& index) const override;
79 
80  // Operator Overloadings
81  ComboBoxDelegate& operator=(const ComboBoxDelegate& rhs) = delete;
82 
83 private: // Data
84  bool mEditable;
85 };
86 
87 /*******************************************************************************
88  * End of File
89  ******************************************************************************/
90 
91 } // namespace librepcb
92 
93 Q_DECLARE_METATYPE(librepcb::ComboBoxDelegate::Items)
94 
95 #endif // LIBREPCB_COMBOBOXDELEGATE_H
ComboBoxDelegate & operator=(const ComboBoxDelegate &rhs)=delete
Subclass of QStyledItemDelegate which uses QComboBox as item editor.
Definition: comboboxdelegate.h:43
Definition: airwiresbuilder.cpp:32
bool mEditable
Definition: comboboxdelegate.h:84
Definition: comboboxdelegate.h:48
Definition: comboboxdelegate.h:53
QWidget * createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override
Definition: comboboxdelegate.cpp:47
QString text
Definition: comboboxdelegate.h:49
~ComboBoxDelegate() noexcept
Definition: comboboxdelegate.cpp:40
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override
Definition: comboboxdelegate.cpp:75
QVariant data
Definition: comboboxdelegate.h:51
void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const override
Definition: comboboxdelegate.cpp:85
ComboBoxDelegate(bool editable=false, QObject *parent=nullptr) noexcept
Definition: comboboxdelegate.cpp:36
void sort() noexcept
Definition: comboboxdelegate.h:54
QIcon icon
Definition: comboboxdelegate.h:50
void setEditorData(QWidget *editor, const QModelIndex &index) const override
Definition: comboboxdelegate.cpp:63