1 //=========================================================
2 // MusE
3 // Linux Music Editor
4 // $Id: lcombo.cpp,v 1.1.1.1.2.3 2009/07/01 22:14:56 spamatica Exp $
5 // (C) Copyright 2000 Werner Schweer (ws@seh.de)
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License
9 // as published by the Free Software Foundation; version 2 of
10 // the License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 //
21 //=========================================================
22
23 #include "lcombo.h"
24
25 #include <QHBoxLayout>
26 #include <QLabel>
27 #include <QAbstractItemView>
28 #include <QString>
29 #include <QModelIndex>
30
31 namespace MusEGui {
32
33 //---------------------------------------------------------
34 // LabelCombo
35 //---------------------------------------------------------
36
LabelCombo(const QString & txt,QWidget * parent,const char * name)37 LabelCombo::LabelCombo(const QString& txt, QWidget* parent,
38 const char* name) : QWidget(parent)
39 {
40 setObjectName(name);
41 QHBoxLayout* layout = new QHBoxLayout(this);
42 QLabel* label = new QLabel(txt, this);
43 //label->setContentsMargins(0,0,0,0); // REMOVE Tim. Or keep.
44 box = new QComboBox(this);
45 //box->setContentsMargins(0,0,0,0); //
46 // Ignored was only solution, others were too tall. The label takes priority then. //
47 //box->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Ignored); //
48 box->setEditable(false);
49 layout->addSpacing(2);
50 layout->addWidget(label);
51 // layout->addSpacing(2);
52 layout->addWidget(box);
53 // layout->addSpacing(2);
54 layout->setContentsMargins(0, 0, 0, 0);
55 connect(box, QOverload<int>::of(&QComboBox::activated), [=](int index) { box_activated(index); } );
56 }
57
addItem(const QString & txt,const QVariant & userData)58 void LabelCombo::addItem(const QString& txt, const QVariant &userData)
59 {
60 box->addItem(txt, userData);
61 }
62
insertItem(int index,const QString & txt,const QVariant & userData)63 void LabelCombo::insertItem(int index, const QString& txt, const QVariant &userData)
64 {
65 box->insertItem(index, txt, userData);
66 }
67
view() const68 QAbstractItemView *LabelCombo::view() const
69 {
70 return box->view();
71 }
72
setView(QAbstractItemView * v)73 void LabelCombo::setView(QAbstractItemView* v)
74 {
75 box->setModel(v->model());
76 box->setView(v);
77 }
78
setFocusPolicy(Qt::FocusPolicy fp)79 void LabelCombo::setFocusPolicy (Qt::FocusPolicy fp)
80 {
81 box->setFocusPolicy(fp);
82 }
83
box_activated(int idx)84 void LabelCombo::box_activated(int idx)
85 {
86 // HACK: Force the thing to show the right item. This hack is required because
87 // if we are trying to show a table view in a combo box it normally wants
88 // to show a single given column using a list view.
89 const QAbstractItemView* iv = view();
90 if(!iv)
91 return;
92 const QModelIndex mdl_idx = iv->currentIndex();
93 if(!mdl_idx.isValid())
94 return;
95 const int row = mdl_idx.row();
96 const int col = mdl_idx.column();
97 blockSignals(true);
98 if(box->modelColumn() != col)
99 box->setModelColumn(col);
100 if(box->currentIndex() != row)
101 box->setCurrentIndex(row);
102 blockSignals(false);
103
104 emit activated(idx);
105 emit activated(mdl_idx);
106 }
107
clearFocus()108 void LabelCombo::clearFocus()
109 {
110 box->clearFocus();
111 }
112
itemData(int index,int role) const113 QVariant LabelCombo::itemData(int index, int role) const
114 {
115 return box->itemData(index, role);
116 }
117
findData(const QVariant & data,int role,Qt::MatchFlags flags) const118 int LabelCombo::findData(const QVariant &data, int role, Qt::MatchFlags flags) const
119 {
120 return box->findData(data, role, flags);
121 }
122
maxVisibleItems() const123 int LabelCombo::maxVisibleItems() const
124 {
125 return box->maxVisibleItems();
126 }
127
setMaxVisibleItems(int maxItems)128 void LabelCombo::setMaxVisibleItems(int maxItems)
129 {
130 box->setMaxVisibleItems(maxItems);
131 }
132
sizeAdjustPolicy() const133 QComboBox::SizeAdjustPolicy LabelCombo::sizeAdjustPolicy() const
134 {
135 return box->sizeAdjustPolicy();
136 }
137
setSizeAdjustPolicy(QComboBox::SizeAdjustPolicy policy)138 void LabelCombo::setSizeAdjustPolicy(QComboBox::SizeAdjustPolicy policy)
139 {
140 box->setSizeAdjustPolicy(policy);
141 }
142
currentIndex() const143 int LabelCombo::currentIndex() const
144 {
145 return box->currentIndex();
146 }
147
currentModelIndex() const148 QModelIndex LabelCombo::currentModelIndex() const
149 {
150 return view()->currentIndex();
151 }
152
setCurrentIndex(int i)153 void LabelCombo::setCurrentIndex(int i)
154 {
155 // HACK: Force the thing to show the right item. This hack is required because
156 // if we are trying to show a table view in a combo box it normally wants
157 // to show a single given column using a list view.
158 int rc = box->model()->rowCount();
159 if(rc == 0)
160 return;
161 int r = i % rc;
162 int c = i / rc;
163 if(c >= box->model()->columnCount())
164 return;
165 if(box->modelColumn() != c)
166 box->setModelColumn(c);
167 if(box->currentIndex() != r)
168 box->setCurrentIndex(r);
169 }
170
setCurrentModelIndex(const QModelIndex & mdl_idx)171 void LabelCombo::setCurrentModelIndex(const QModelIndex& mdl_idx)
172 {
173 const int row = mdl_idx.row();
174 const int col = mdl_idx.column();
175 if(col >= box->model()->columnCount())
176 return;
177 if(box->modelColumn() != col)
178 box->setModelColumn(col);
179 if(box->currentIndex() != row)
180 box->setCurrentIndex(row);
181 }
182
183 } // namespace MusEGui
184