| 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 | |
|---|
| 31 | const QString LCDCombo::SEPARATOR("--sep--"); |
|---|
| 32 | |
|---|
| 33 | LCDCombo::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 | |
|---|
| 65 | LCDCombo::~LCDCombo() |
|---|
| 66 | { |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | |
|---|
| 70 | QString LCDCombo::getText() |
|---|
| 71 | { |
|---|
| 72 | return display->getText(); |
|---|
| 73 | }; |
|---|
| 74 | |
|---|
| 75 | |
|---|
| 76 | void 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 | |
|---|
| 86 | void LCDCombo::onClick(Button*) |
|---|
| 87 | { |
|---|
| 88 | pop->popup( display->mapToGlobal( QPoint( 1, display->height() + 2 ) ) ); |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | |
|---|
| 92 | |
|---|
| 93 | int LCDCombo::length() |
|---|
| 94 | { |
|---|
| 95 | return size; |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | |
|---|
| 99 | |
|---|
| 100 | void 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 | |
|---|
| 117 | int LCDCombo::count() |
|---|
| 118 | { |
|---|
| 119 | return items.size(); |
|---|
| 120 | } |
|---|
| 121 | |
|---|
| 122 | |
|---|
| 123 | |
|---|
| 124 | bool 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 | |
|---|
| 138 | void LCDCombo::addSeparator() |
|---|
| 139 | { |
|---|
| 140 | items.append( SEPARATOR ); |
|---|
| 141 | } |
|---|
| 142 | |
|---|
| 143 | |
|---|
| 144 | |
|---|
| 145 | inline 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 | |
|---|
| 155 | void LCDCombo::mousePressEvent(QMouseEvent *ev) |
|---|
| 156 | { |
|---|
| 157 | UNUSED( ev ); |
|---|
| 158 | pop->popup( display->mapToGlobal( QPoint( 1, display->height() + 2 ) ) ); |
|---|
| 159 | } |
|---|
| 160 | |
|---|
| 161 | void 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 | |
|---|
| 173 | |
|---|
| 174 | void LCDCombo::set_text( const QString &text) |
|---|
| 175 | { |
|---|
| 176 | if (display->getText() == text) { |
|---|
| 177 | return; |
|---|
| 178 | } |
|---|
| 179 | //INFOLOG( text ); |
|---|
| 180 | display->setText( text ); |
|---|
| 181 | for ( int i = 0; i < items.size(); i++ ) { |
|---|
| 182 | if ( items.at(i) == text ) |
|---|
| 183 | active = i; |
|---|
| 184 | } |
|---|
| 185 | emit valueChanged( text ); |
|---|
| 186 | } |
|---|
| 187 | |
|---|
| 188 | |
|---|