root/trunk/gui/src/widgets/LCDCombo.cpp @ 390

Revision 390, 3.8 KB (checked in by smoors, 5 years ago)

Bugfix: Set patternsize-combobox to current size if size change is not possible

Line 
1/*
2 * Hydrogen
3 * Copyright(c) 2002-2008 by Alex >Comix< Cominu [comix@users.sourceforge.net]
4 *
5 * http://www.hydrogen-music.org
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20 *
21 */
22
23#include "LCDCombo.h"
24
25#include "../Skin.h"
26#include "LCD.h"
27#include "Button.h"
28
29#include <hydrogen/globals.h>
30
31const QString LCDCombo::SEPARATOR("--sep--");
32
33LCDCombo::LCDCombo(QWidget *pParent, int digits)
34 : QWidget(pParent)
35 , Object( "LCDCombo") //, SEPARATOR("--sep--")
36{
37        INFOLOG( "INIT" );
38
39        QStringList items;
40        display = new LCDDisplay( this, LCDDigit::SMALL_BLUE, digits, false);
41        button = new Button( this,
42                        "/patternEditor/btn_dropdown_on.png",
43                        "/patternEditor/btn_dropdown_off.png",
44                        "/patternEditor/btn_dropdown_over.png",
45                        QSize(13, 13)
46        );
47        pop = new QMenu( this );
48        size = digits;
49        active = 0;
50
51        button->move( ( digits * 8 ) + 5 , 1 );
52        setFixedSize( ( digits * 8 ) + 17, display->height() );
53
54        connect( button, SIGNAL( clicked( Button* ) ), this, SLOT( onClick( Button* ) ) );
55
56        update();
57
58        connect( pop, SIGNAL( triggered(QAction*) ), this, SLOT( changeText(QAction*) ) );
59        //_WARNINGLOG("items:"+items[0]);
60}
61
62
63
64
65LCDCombo::~LCDCombo()
66{
67}
68
69
70QString LCDCombo::getText()
71{
72        return display->getText();
73};
74
75
76void LCDCombo::changeText(QAction* pAction)
77{
78        //_WARNINGLOG("triggered");
79//      display->setText(pAction->text());
80//      emit valueChanged( pAction->text() );
81        set_text( pAction->text() );
82}
83
84
85
86void LCDCombo::onClick(Button*)
87{
88        pop->popup( display->mapToGlobal( QPoint( 1, display->height() + 2 ) ) );
89}
90
91
92
93int LCDCombo::length()
94{
95        return size;
96}
97
98
99
100void LCDCombo::update()
101{
102        //INFOLOG ( "update: "+toString(items.size()) );
103        pop->clear();
104
105        for( int i = 0; i < items.size(); i++ ) {
106                if ( items.at(i) != SEPARATOR ){
107                        pop->addAction( items.at(i) );
108                }else{
109                        pop->addSeparator();
110                }
111        }
112
113}
114
115
116
117int LCDCombo::count()
118{
119        return items.size();
120}
121
122
123
124bool LCDCombo::addItem( const QString &text )
125{
126        //INFOLOG( "add item" );
127
128        if ( text.size() <= size ){
129                items.append( text );
130                return true;
131        }else{
132                return false;
133        }
134}
135
136
137
138void LCDCombo::addSeparator()
139{
140        items.append( SEPARATOR );
141}
142
143
144
145inline void LCDCombo::insertItem( int index, const QString &text )
146{
147        if(text.size()<=length()){
148                items.insert(index,text);
149                update();
150        }
151}
152
153
154
155void LCDCombo::mousePressEvent(QMouseEvent *ev)
156{
157        UNUSED( ev );
158        pop->popup( display->mapToGlobal( QPoint( 1, display->height() + 2 ) ) );
159}
160
161void LCDCombo::wheelEvent( QWheelEvent * ev )
162{
163        ev->ignore();
164        const int n = items.size();
165        const int d = ( ev->delta() > 0 ) ? -1: 1;
166        active = ( n + active + d ) % n;
167        if ( items.at( active ) == SEPARATOR )
168                active = ( n + active + d ) % n;
169        set_text( items.at( active ) );
170}
171
172
173void LCDCombo::set_text( const QString &text)
174{
175        if (display->getText() == text) {
176                return;
177        }
178        //INFOLOG( text );
179        display->setText( text );
180        for ( int i = 0; i < items.size(); i++ ) {
181                if ( items.at(i) == text )
182                        active = i;
183        }
184       
185        emit valueChanged( text );
186
187}
188
189void LCDCombo::set_text( const QString &text, bool emit_on_change)
190{
191        if (display->getText() == text) {
192                return;
193        }
194        //INFOLOG( text );
195        display->setText( text );
196        for ( int i = 0; i < items.size(); i++ ) {
197                if ( items.at(i) == text )
198                        active = i;
199        }
200       
201        if(emit_on_change)
202                emit valueChanged( text );
203}
204
205
Note: See TracBrowser for help on using the browser.